gnunet-service-fs_push.c 16 KB

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