gnunet-service-fs_push.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2011 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/gnunet-service-fs_push.c
  19. * @brief API to push content from our datastore to other peers
  20. * ('anonymous'-content P2P migration)
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet-service-fs.h"
  25. #include "gnunet-service-fs_cp.h"
  26. #include "gnunet-service-fs_indexing.h"
  27. #include "gnunet-service-fs_push.h"
  28. /**
  29. * Maximum number of blocks we keep in memory for migration.
  30. */
  31. #define MAX_MIGRATION_QUEUE 8
  32. /**
  33. * Blocks are at most migrated to this number of peers
  34. * plus one, each time they are fetched from the database.
  35. */
  36. #define MIGRATION_LIST_SIZE 2
  37. /**
  38. * How long must content remain valid for us to consider it for migration?
  39. * If content will expire too soon, there is clearly no point in pushing
  40. * it to other peers. This value gives the threshold for migration. Note
  41. * that if this value is increased, the migration testcase may need to be
  42. * adjusted as well (especially the CONTENT_LIFETIME in fs_test_lib.c).
  43. */
  44. #define MIN_MIGRATION_CONTENT_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
  45. /**
  46. * Block that is ready for migration to other peers. Actual data is at the end of the block.
  47. */
  48. struct MigrationReadyBlock
  49. {
  50. /**
  51. * This is a doubly-linked list.
  52. */
  53. struct MigrationReadyBlock *next;
  54. /**
  55. * This is a doubly-linked list.
  56. */
  57. struct MigrationReadyBlock *prev;
  58. /**
  59. * Query for the block.
  60. */
  61. struct GNUNET_HashCode query;
  62. /**
  63. * When does this block expire?
  64. */
  65. struct GNUNET_TIME_Absolute expiration;
  66. /**
  67. * Peers we already forwarded this
  68. * block to. Zero for empty entries.
  69. */
  70. GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
  71. /**
  72. * Size of the block.
  73. */
  74. size_t size;
  75. /**
  76. * Number of targets already used.
  77. */
  78. unsigned int used_targets;
  79. /**
  80. * Type of the block.
  81. */
  82. enum GNUNET_BLOCK_Type type;
  83. };
  84. /**
  85. * Information about a peer waiting for
  86. * migratable data.
  87. */
  88. struct MigrationReadyPeer
  89. {
  90. /**
  91. * This is a doubly-linked list.
  92. */
  93. struct MigrationReadyPeer *next;
  94. /**
  95. * This is a doubly-linked list.
  96. */
  97. struct MigrationReadyPeer *prev;
  98. /**
  99. * Handle to peer.
  100. */
  101. struct GSF_ConnectedPeer *peer;
  102. /**
  103. * Handle for current transmission request,
  104. * or NULL for none.
  105. */
  106. struct GSF_PeerTransmitHandle *th;
  107. /**
  108. * Message we are trying to push right now (or NULL)
  109. */
  110. struct PutMessage *msg;
  111. };
  112. /**
  113. * Head of linked list of blocks that can be migrated.
  114. */
  115. static struct MigrationReadyBlock *mig_head;
  116. /**
  117. * Tail of linked list of blocks that can be migrated.
  118. */
  119. static struct MigrationReadyBlock *mig_tail;
  120. /**
  121. * Head of linked list of peers.
  122. */
  123. static struct MigrationReadyPeer *peer_head;
  124. /**
  125. * Tail of linked list of peers.
  126. */
  127. static struct MigrationReadyPeer *peer_tail;
  128. /**
  129. * Request to datastore for migration (or NULL).
  130. */
  131. static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
  132. /**
  133. * ID of task that collects blocks for migration.
  134. */
  135. static GNUNET_SCHEDULER_TaskIdentifier mig_task;
  136. /**
  137. * What is the maximum frequency at which we are allowed to
  138. * poll the datastore for migration content?
  139. */
  140. static struct GNUNET_TIME_Relative min_migration_delay;
  141. /**
  142. * Size of the doubly-linked list of migration blocks.
  143. */
  144. static unsigned int mig_size;
  145. /**
  146. * Is this module enabled?
  147. */
  148. static int enabled;
  149. /**
  150. * Delete the given migration block.
  151. *
  152. * @param mb block to delete
  153. */
  154. static void
  155. delete_migration_block (struct MigrationReadyBlock *mb)
  156. {
  157. GNUNET_CONTAINER_DLL_remove (mig_head, mig_tail, mb);
  158. GNUNET_PEER_decrement_rcs (mb->target_list, MIGRATION_LIST_SIZE);
  159. mig_size--;
  160. GNUNET_free (mb);
  161. }
  162. /**
  163. * Find content for migration to this peer.
  164. */
  165. static void
  166. find_content (struct MigrationReadyPeer *mrp);
  167. /**
  168. * Transmit the message currently scheduled for
  169. * transmission.
  170. *
  171. * @param cls the 'struct MigrationReadyPeer'
  172. * @param buf_size number of bytes available in buf
  173. * @param buf where to copy the message, NULL on error (peer disconnect)
  174. * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
  175. */
  176. static size_t
  177. transmit_message (void *cls, size_t buf_size, void *buf)
  178. {
  179. struct MigrationReadyPeer *peer = cls;
  180. struct PutMessage *msg;
  181. uint16_t msize;
  182. peer->th = NULL;
  183. msg = peer->msg;
  184. peer->msg = NULL;
  185. if (buf == NULL)
  186. {
  187. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  188. "Failed to migrate content to another peer (disconnect)\n");
  189. GNUNET_free (msg);
  190. return 0;
  191. }
  192. msize = ntohs (msg->header.size);
  193. GNUNET_assert (msize <= buf_size);
  194. memcpy (buf, msg, msize);
  195. GNUNET_free (msg);
  196. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Pushing %u bytes to another peer\n",
  197. msize);
  198. find_content (peer);
  199. return msize;
  200. }
  201. /**
  202. * Send the given block to the given peer.
  203. *
  204. * @param peer target peer
  205. * @param block the block
  206. * @return GNUNET_YES if the block was deleted (!)
  207. */
  208. static int
  209. transmit_content (struct MigrationReadyPeer *peer,
  210. struct MigrationReadyBlock *block)
  211. {
  212. size_t msize;
  213. struct PutMessage *msg;
  214. unsigned int i;
  215. struct GSF_PeerPerformanceData *ppd;
  216. int ret;
  217. ppd = GSF_get_peer_performance_data_ (peer->peer);
  218. GNUNET_assert (NULL == peer->th);
  219. msize = sizeof (struct PutMessage) + block->size;
  220. msg = GNUNET_malloc (msize);
  221. msg->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
  222. msg->header.size = htons (msize);
  223. msg->type = htonl (block->type);
  224. msg->expiration = GNUNET_TIME_absolute_hton (block->expiration);
  225. memcpy (&msg[1], &block[1], block->size);
  226. peer->msg = msg;
  227. for (i = 0; i < MIGRATION_LIST_SIZE; i++)
  228. {
  229. if (block->target_list[i] == 0)
  230. {
  231. block->target_list[i] = ppd->pid;
  232. GNUNET_PEER_change_rc (block->target_list[i], 1);
  233. break;
  234. }
  235. }
  236. if (MIGRATION_LIST_SIZE == i)
  237. {
  238. delete_migration_block (block);
  239. ret = GNUNET_YES;
  240. }
  241. else
  242. {
  243. ret = GNUNET_NO;
  244. }
  245. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  246. "Asking for transmission of %u bytes for migration\n", msize);
  247. peer->th = GSF_peer_transmit_ (peer->peer, GNUNET_NO, 0 /* priority */ ,
  248. GNUNET_TIME_UNIT_FOREVER_REL, msize,
  249. &transmit_message, peer);
  250. return ret;
  251. }
  252. /**
  253. * Count the number of peers this block has
  254. * already been forwarded to.
  255. *
  256. * @param block the block
  257. * @return number of times block was forwarded
  258. */
  259. static unsigned int
  260. count_targets (struct MigrationReadyBlock *block)
  261. {
  262. unsigned int i;
  263. for (i = 0; i < MIGRATION_LIST_SIZE; i++)
  264. if (block->target_list[i] == 0)
  265. return i;
  266. return i;
  267. }
  268. /**
  269. * Check if sending this block to this peer would
  270. * be a good idea.
  271. *
  272. * @param peer target peer
  273. * @param block the block
  274. * @return score (>= 0: feasible, negative: infeasible)
  275. */
  276. static long
  277. score_content (struct MigrationReadyPeer *peer,
  278. struct MigrationReadyBlock *block)
  279. {
  280. unsigned int i;
  281. struct GSF_PeerPerformanceData *ppd;
  282. struct GNUNET_PeerIdentity id;
  283. struct GNUNET_HashCode hc;
  284. uint32_t dist;
  285. ppd = GSF_get_peer_performance_data_ (peer->peer);
  286. for (i = 0; i < MIGRATION_LIST_SIZE; i++)
  287. if (block->target_list[i] == ppd->pid)
  288. return -1;
  289. GNUNET_assert (0 != ppd->pid);
  290. GNUNET_PEER_resolve (ppd->pid, &id);
  291. GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hc);
  292. dist = GNUNET_CRYPTO_hash_distance_u32 (&block->query, &hc);
  293. /* closer distance, higher score: */
  294. return UINT32_MAX - dist;
  295. }
  296. /**
  297. * If the migration task is not currently running, consider
  298. * (re)scheduling it with the appropriate delay.
  299. */
  300. static void
  301. consider_gathering (void);
  302. /**
  303. * Find content for migration to this peer.
  304. *
  305. * @param mrp peer to find content for
  306. */
  307. static void
  308. find_content (struct MigrationReadyPeer *mrp)
  309. {
  310. struct MigrationReadyBlock *pos;
  311. long score;
  312. long best_score;
  313. struct MigrationReadyBlock *best;
  314. GNUNET_assert (NULL == mrp->th);
  315. best = NULL;
  316. best_score = -1;
  317. pos = mig_head;
  318. while (NULL != pos)
  319. {
  320. score = score_content (mrp, pos);
  321. if (score > best_score)
  322. {
  323. best_score = score;
  324. best = pos;
  325. }
  326. pos = pos->next;
  327. }
  328. if (NULL == best)
  329. {
  330. if (mig_size < MAX_MIGRATION_QUEUE)
  331. {
  332. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  333. "No content found for pushing, waiting for queue to fill\n");
  334. return; /* will fill up eventually... */
  335. }
  336. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  337. "No suitable content found, purging content from full queue\n");
  338. /* failed to find migration target AND
  339. * queue is full, purge most-forwarded
  340. * block from queue to make room for more */
  341. pos = mig_head;
  342. while (NULL != pos)
  343. {
  344. score = count_targets (pos);
  345. if (score >= best_score)
  346. {
  347. best_score = score;
  348. best = pos;
  349. }
  350. pos = pos->next;
  351. }
  352. GNUNET_assert (NULL != best);
  353. delete_migration_block (best);
  354. consider_gathering ();
  355. return;
  356. }
  357. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  358. "Preparing to push best content to peer\n");
  359. transmit_content (mrp, best);
  360. }
  361. /**
  362. * Task that is run periodically to obtain blocks for content
  363. * migration
  364. *
  365. * @param cls unused
  366. * @param tc scheduler context (also unused)
  367. */
  368. static void
  369. gather_migration_blocks (void *cls,
  370. const struct GNUNET_SCHEDULER_TaskContext *tc);
  371. /**
  372. * If the migration task is not currently running, consider
  373. * (re)scheduling it with the appropriate delay.
  374. */
  375. static void
  376. consider_gathering ()
  377. {
  378. struct GNUNET_TIME_Relative delay;
  379. if (GSF_dsh == NULL)
  380. return;
  381. if (mig_qe != NULL)
  382. return;
  383. if (mig_task != GNUNET_SCHEDULER_NO_TASK)
  384. return;
  385. if (mig_size >= MAX_MIGRATION_QUEUE)
  386. return;
  387. delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, mig_size);
  388. delay = GNUNET_TIME_relative_divide (delay, MAX_MIGRATION_QUEUE);
  389. delay = GNUNET_TIME_relative_max (delay, min_migration_delay);
  390. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  391. "Scheduling gathering task (queue size: %u)\n", mig_size);
  392. mig_task =
  393. GNUNET_SCHEDULER_add_delayed (delay, &gather_migration_blocks, NULL);
  394. }
  395. /**
  396. * Process content offered for migration.
  397. *
  398. * @param cls closure
  399. * @param key key for the content
  400. * @param size number of bytes in data
  401. * @param data content stored
  402. * @param type type of the content
  403. * @param priority priority of the content
  404. * @param anonymity anonymity-level for the content
  405. * @param expiration expiration time for the content
  406. * @param uid unique identifier for the datum;
  407. * maybe 0 if no unique identifier is available
  408. */
  409. static void
  410. process_migration_content (void *cls, const struct GNUNET_HashCode * key, size_t size,
  411. const void *data, enum GNUNET_BLOCK_Type type,
  412. uint32_t priority, uint32_t anonymity,
  413. struct GNUNET_TIME_Absolute expiration, uint64_t uid)
  414. {
  415. struct MigrationReadyBlock *mb;
  416. struct MigrationReadyPeer *pos;
  417. mig_qe = NULL;
  418. if (key == NULL)
  419. {
  420. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No content found for migration...\n");
  421. consider_gathering ();
  422. return;
  423. }
  424. if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us <
  425. MIN_MIGRATION_CONTENT_LIFETIME.rel_value_us)
  426. {
  427. /* content will expire soon, don't bother */
  428. consider_gathering ();
  429. return;
  430. }
  431. if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
  432. {
  433. if (GNUNET_OK !=
  434. GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
  435. anonymity, expiration, uid,
  436. &process_migration_content, NULL))
  437. consider_gathering ();
  438. return;
  439. }
  440. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  441. "Retrieved block `%s' of type %u for migration (queue size: %u/%u)\n",
  442. GNUNET_h2s (key), type, mig_size + 1, MAX_MIGRATION_QUEUE);
  443. mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
  444. mb->query = *key;
  445. mb->expiration = expiration;
  446. mb->size = size;
  447. mb->type = type;
  448. memcpy (&mb[1], data, size);
  449. GNUNET_CONTAINER_DLL_insert_after (mig_head, mig_tail, mig_tail, mb);
  450. mig_size++;
  451. pos = peer_head;
  452. while (pos != NULL)
  453. {
  454. if (NULL == pos->th)
  455. {
  456. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  457. "Preparing to push best content to peer\n");
  458. if (GNUNET_YES == transmit_content (pos, mb))
  459. break; /* 'mb' was freed! */
  460. }
  461. pos = pos->next;
  462. }
  463. consider_gathering ();
  464. }
  465. /**
  466. * Task that is run periodically to obtain blocks for content
  467. * migration
  468. *
  469. * @param cls unused
  470. * @param tc scheduler context (also unused)
  471. */
  472. static void
  473. gather_migration_blocks (void *cls,
  474. const struct GNUNET_SCHEDULER_TaskContext *tc)
  475. {
  476. mig_task = GNUNET_SCHEDULER_NO_TASK;
  477. if (mig_size >= MAX_MIGRATION_QUEUE)
  478. return;
  479. if (GSF_dsh != NULL)
  480. {
  481. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  482. "Asking datastore for content for replication (queue size: %u)\n",
  483. mig_size);
  484. mig_qe =
  485. GNUNET_DATASTORE_get_for_replication (GSF_dsh, 0, UINT_MAX,
  486. GNUNET_TIME_UNIT_FOREVER_REL,
  487. &process_migration_content, NULL);
  488. if (NULL == mig_qe)
  489. consider_gathering ();
  490. }
  491. }
  492. /**
  493. * A peer connected to us. Start pushing content
  494. * to this peer.
  495. *
  496. * @param peer handle for the peer that connected
  497. */
  498. void
  499. GSF_push_start_ (struct GSF_ConnectedPeer *peer)
  500. {
  501. struct MigrationReadyPeer *mrp;
  502. if (GNUNET_YES != enabled)
  503. return;
  504. mrp = GNUNET_new (struct MigrationReadyPeer);
  505. mrp->peer = peer;
  506. find_content (mrp);
  507. GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, mrp);
  508. }
  509. /**
  510. * A peer disconnected from us. Stop pushing content
  511. * to this peer.
  512. *
  513. * @param peer handle for the peer that disconnected
  514. */
  515. void
  516. GSF_push_stop_ (struct GSF_ConnectedPeer *peer)
  517. {
  518. struct MigrationReadyPeer *pos;
  519. pos = peer_head;
  520. while (pos != NULL)
  521. {
  522. if (pos->peer == peer)
  523. {
  524. GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
  525. if (NULL != pos->th)
  526. {
  527. GSF_peer_transmit_cancel_ (pos->th);
  528. pos->th = NULL;
  529. }
  530. if (NULL != pos->msg)
  531. {
  532. GNUNET_free (pos->msg);
  533. pos->msg = NULL;
  534. }
  535. GNUNET_free (pos);
  536. return;
  537. }
  538. pos = pos->next;
  539. }
  540. }
  541. /**
  542. * Setup the module.
  543. */
  544. void
  545. GSF_push_init_ ()
  546. {
  547. enabled =
  548. GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_PUSHING");
  549. if (GNUNET_YES != enabled)
  550. return;
  551. if (GNUNET_OK !=
  552. GNUNET_CONFIGURATION_get_value_time (GSF_cfg, "fs", "MIN_MIGRATION_DELAY",
  553. &min_migration_delay))
  554. {
  555. GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
  556. "fs", "MIN_MIGRATION_DELAY",
  557. _("time required, content pushing disabled"));
  558. return;
  559. }
  560. consider_gathering ();
  561. }
  562. /**
  563. * Shutdown the module.
  564. */
  565. void
  566. GSF_push_done_ ()
  567. {
  568. if (GNUNET_SCHEDULER_NO_TASK != mig_task)
  569. {
  570. GNUNET_SCHEDULER_cancel (mig_task);
  571. mig_task = GNUNET_SCHEDULER_NO_TASK;
  572. }
  573. if (NULL != mig_qe)
  574. {
  575. GNUNET_DATASTORE_cancel (mig_qe);
  576. mig_qe = NULL;
  577. }
  578. while (NULL != mig_head)
  579. delete_migration_block (mig_head);
  580. GNUNET_assert (0 == mig_size);
  581. }
  582. /* end of gnunet-service-fs_push.c */