gnunet-service-fs_push.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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,
  150. mig_tail,
  151. mb);
  152. GNUNET_PEER_decrement_rcs (mb->target_list,
  153. MIGRATION_LIST_SIZE);
  154. mig_size--;
  155. GNUNET_free (mb);
  156. }
  157. /**
  158. * Find content for migration to this peer.
  159. */
  160. static void
  161. find_content (struct MigrationReadyPeer *mrp);
  162. /**
  163. * Transmit the message currently scheduled for
  164. * transmission.
  165. *
  166. * @param cls the 'struct MigrationReadyPeer'
  167. * @param buf_size number of bytes available in buf
  168. * @param buf where to copy the message, NULL on error (peer disconnect)
  169. * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
  170. */
  171. static size_t
  172. transmit_message (void *cls,
  173. size_t buf_size,
  174. void *buf)
  175. {
  176. struct MigrationReadyPeer *peer = cls;
  177. struct PutMessage *msg;
  178. uint16_t msize;
  179. peer->th = NULL;
  180. msg = peer->msg;
  181. peer->msg = NULL;
  182. if (buf == NULL)
  183. {
  184. #if DEBUG_FS_MIGRATION
  185. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  186. "Failed to migrate content to another peer (disconnect)\n");
  187. #endif
  188. GNUNET_free (msg);
  189. return 0;
  190. }
  191. msize = ntohs (msg->header.size);
  192. GNUNET_assert (msize <= buf_size);
  193. memcpy (buf, msg, msize);
  194. GNUNET_free (msg);
  195. #if DEBUG_FS_MIGRATION
  196. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  197. "Pushing %u bytes to another peer\n",
  198. msize);
  199. #endif
  200. find_content (peer);
  201. return msize;
  202. }
  203. /**
  204. * Send the given block to the given peer.
  205. *
  206. * @param peer target peer
  207. * @param block the block
  208. * @return GNUNET_YES if the block was deleted (!)
  209. */
  210. static int
  211. transmit_content (struct MigrationReadyPeer *peer,
  212. struct MigrationReadyBlock *block)
  213. {
  214. size_t msize;
  215. struct PutMessage *msg;
  216. unsigned int i;
  217. struct GSF_PeerPerformanceData *ppd;
  218. int ret;
  219. ppd = GSF_get_peer_performance_data_ (peer->peer);
  220. GNUNET_assert (NULL == peer->th);
  221. msize = sizeof (struct PutMessage) + block->size;
  222. msg = GNUNET_malloc (msize);
  223. msg->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
  224. msg->header.size = htons (msize);
  225. msg->type = htonl (block->type);
  226. msg->expiration = GNUNET_TIME_absolute_hton (block->expiration);
  227. memcpy (&msg[1],
  228. &block[1],
  229. block->size);
  230. peer->msg = msg;
  231. for (i=0;i<MIGRATION_LIST_SIZE;i++)
  232. {
  233. if (block->target_list[i] == 0)
  234. {
  235. block->target_list[i] = ppd->pid;
  236. GNUNET_PEER_change_rc (block->target_list[i], 1);
  237. break;
  238. }
  239. }
  240. if (MIGRATION_LIST_SIZE == i)
  241. {
  242. delete_migration_block (block);
  243. ret = GNUNET_YES;
  244. }
  245. else
  246. {
  247. ret = GNUNET_NO;
  248. }
  249. #if DEBUG_FS_MIGRATION
  250. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  251. "Asking for transmission of %u bytes for migration\n",
  252. msize);
  253. #endif
  254. peer->th = GSF_peer_transmit_ (peer->peer,
  255. GNUNET_NO,
  256. 0 /* priority */,
  257. GNUNET_TIME_UNIT_FOREVER_REL,
  258. msize,
  259. &transmit_message,
  260. peer);
  261. return ret;
  262. }
  263. /**
  264. * Count the number of peers this block has
  265. * already been forwarded to.
  266. *
  267. * @param block the block
  268. * @return number of times block was forwarded
  269. */
  270. static unsigned int
  271. count_targets (struct MigrationReadyBlock *block)
  272. {
  273. unsigned int i;
  274. for (i=0;i<MIGRATION_LIST_SIZE;i++)
  275. if (block->target_list[i] == 0)
  276. return i;
  277. return i;
  278. }
  279. /**
  280. * Check if sending this block to this peer would
  281. * be a good idea.
  282. *
  283. * @param peer target peer
  284. * @param block the block
  285. * @return score (>= 0: feasible, negative: infeasible)
  286. */
  287. static long
  288. score_content (struct MigrationReadyPeer *peer,
  289. struct MigrationReadyBlock *block)
  290. {
  291. unsigned int i;
  292. struct GSF_PeerPerformanceData *ppd;
  293. struct GNUNET_PeerIdentity id;
  294. uint32_t dist;
  295. ppd = GSF_get_peer_performance_data_ (peer->peer);
  296. for (i=0;i<MIGRATION_LIST_SIZE;i++)
  297. if (block->target_list[i] == ppd->pid)
  298. return -1;
  299. GNUNET_assert (0 != ppd->pid);
  300. GNUNET_PEER_resolve (ppd->pid,
  301. &id);
  302. dist = GNUNET_CRYPTO_hash_distance_u32 (&block->query,
  303. &id.hashPubKey);
  304. /* closer distance, higher score: */
  305. return UINT32_MAX - dist;
  306. }
  307. /**
  308. * If the migration task is not currently running, consider
  309. * (re)scheduling it with the appropriate delay.
  310. */
  311. static void
  312. consider_gathering (void);
  313. /**
  314. * Find content for migration to this peer.
  315. *
  316. * @param mrp peer to find content for
  317. */
  318. static void
  319. find_content (struct MigrationReadyPeer *mrp)
  320. {
  321. struct MigrationReadyBlock *pos;
  322. long score;
  323. long best_score;
  324. struct MigrationReadyBlock *best;
  325. GNUNET_assert (NULL == mrp->th);
  326. best = NULL;
  327. best_score = -1;
  328. pos = mig_head;
  329. while (NULL != pos)
  330. {
  331. score = score_content (mrp, pos);
  332. if (score > best_score)
  333. {
  334. best_score = score;
  335. best = pos;
  336. }
  337. pos = pos->next;
  338. }
  339. if (NULL == best)
  340. {
  341. if (mig_size < MAX_MIGRATION_QUEUE)
  342. {
  343. #if DEBUG_FS_MIGRATION
  344. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  345. "No content found for pushing, waiting for queue to fill\n");
  346. #endif
  347. return; /* will fill up eventually... */
  348. }
  349. #if DEBUG_FS_MIGRATION
  350. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  351. "No suitable content found, purging content from full queue\n");
  352. #endif
  353. /* failed to find migration target AND
  354. queue is full, purge most-forwarded
  355. block from queue to make room for more */
  356. pos = mig_head;
  357. while (NULL != pos)
  358. {
  359. score = count_targets (pos);
  360. if (score >= best_score)
  361. {
  362. best_score = score;
  363. best = pos;
  364. }
  365. pos = pos->next;
  366. }
  367. GNUNET_assert (NULL != best);
  368. delete_migration_block (best);
  369. consider_gathering ();
  370. return;
  371. }
  372. #if DEBUG_FS_MIGRATION
  373. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  374. "Preparing to push best content to peer\n");
  375. #endif
  376. transmit_content (mrp, best);
  377. }
  378. /**
  379. * Task that is run periodically to obtain blocks for content
  380. * migration
  381. *
  382. * @param cls unused
  383. * @param tc scheduler context (also unused)
  384. */
  385. static void
  386. gather_migration_blocks (void *cls,
  387. const struct GNUNET_SCHEDULER_TaskContext *tc);
  388. /**
  389. * If the migration task is not currently running, consider
  390. * (re)scheduling it with the appropriate delay.
  391. */
  392. static void
  393. consider_gathering ()
  394. {
  395. struct GNUNET_TIME_Relative delay;
  396. if (GSF_dsh == NULL)
  397. return;
  398. if (mig_qe != NULL)
  399. return;
  400. if (mig_task != GNUNET_SCHEDULER_NO_TASK)
  401. return;
  402. if (mig_size >= MAX_MIGRATION_QUEUE)
  403. return;
  404. delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
  405. mig_size);
  406. delay = GNUNET_TIME_relative_divide (delay,
  407. MAX_MIGRATION_QUEUE);
  408. delay = GNUNET_TIME_relative_max (delay,
  409. min_migration_delay);
  410. #if DEBUG_FS_MIGRATION
  411. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  412. "Scheduling gathering task (queue size: %u)\n",
  413. mig_size);
  414. #endif
  415. mig_task = GNUNET_SCHEDULER_add_delayed (delay,
  416. &gather_migration_blocks,
  417. NULL);
  418. }
  419. /**
  420. * Process content offered for migration.
  421. *
  422. * @param cls closure
  423. * @param key key for the content
  424. * @param size number of bytes in data
  425. * @param data content stored
  426. * @param type type of the content
  427. * @param priority priority of the content
  428. * @param anonymity anonymity-level for the content
  429. * @param expiration expiration time for the content
  430. * @param uid unique identifier for the datum;
  431. * maybe 0 if no unique identifier is available
  432. */
  433. static void
  434. process_migration_content (void *cls,
  435. const GNUNET_HashCode *key,
  436. size_t size,
  437. const void *data,
  438. enum GNUNET_BLOCK_Type type,
  439. uint32_t priority,
  440. uint32_t anonymity,
  441. struct GNUNET_TIME_Absolute
  442. expiration, uint64_t uid)
  443. {
  444. struct MigrationReadyBlock *mb;
  445. struct MigrationReadyPeer *pos;
  446. mig_qe = NULL;
  447. if (key == NULL)
  448. {
  449. #if DEBUG_FS_MIGRATION
  450. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  451. "No content found for migration...\n");
  452. #endif
  453. consider_gathering ();
  454. return;
  455. }
  456. if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value <
  457. MIN_MIGRATION_CONTENT_LIFETIME.rel_value)
  458. {
  459. /* content will expire soon, don't bother */
  460. consider_gathering ();
  461. return;
  462. }
  463. if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
  464. {
  465. if (GNUNET_OK !=
  466. GNUNET_FS_handle_on_demand_block (key, size, data,
  467. type, priority, anonymity,
  468. expiration, uid,
  469. &process_migration_content,
  470. NULL))
  471. consider_gathering ();
  472. return;
  473. }
  474. #if DEBUG_FS_MIGRATION
  475. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  476. "Retrieved block `%s' of type %u for migration (queue size: %u/%u)\n",
  477. GNUNET_h2s (key),
  478. type,
  479. mig_size + 1,
  480. MAX_MIGRATION_QUEUE);
  481. #endif
  482. mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
  483. mb->query = *key;
  484. mb->expiration = expiration;
  485. mb->size = size;
  486. mb->type = type;
  487. memcpy (&mb[1], data, size);
  488. GNUNET_CONTAINER_DLL_insert_after (mig_head,
  489. mig_tail,
  490. mig_tail,
  491. mb);
  492. mig_size++;
  493. pos = peer_head;
  494. while (pos != NULL)
  495. {
  496. if (NULL == pos->th)
  497. {
  498. #if DEBUG_FS_MIGRATION
  499. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  500. "Preparing to push best content to peer\n");
  501. #endif
  502. if (GNUNET_YES == transmit_content (pos, mb))
  503. break; /* 'mb' was freed! */
  504. }
  505. pos = pos->next;
  506. }
  507. consider_gathering ();
  508. }
  509. /**
  510. * Task that is run periodically to obtain blocks for content
  511. * migration
  512. *
  513. * @param cls unused
  514. * @param tc scheduler context (also unused)
  515. */
  516. static void
  517. gather_migration_blocks (void *cls,
  518. const struct GNUNET_SCHEDULER_TaskContext *tc)
  519. {
  520. mig_task = GNUNET_SCHEDULER_NO_TASK;
  521. if (mig_size >= MAX_MIGRATION_QUEUE)
  522. return;
  523. if (GSF_dsh != NULL)
  524. {
  525. #if DEBUG_FS_MIGRATION
  526. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  527. "Asking datastore for content for replication (queue size: %u)\n",
  528. mig_size);
  529. #endif
  530. mig_qe = GNUNET_DATASTORE_get_for_replication (GSF_dsh,
  531. 0, UINT_MAX,
  532. GNUNET_TIME_UNIT_FOREVER_REL,
  533. &process_migration_content, NULL);
  534. if (NULL == mig_qe)
  535. consider_gathering ();
  536. }
  537. }
  538. /**
  539. * A peer connected to us. Start pushing content
  540. * to this peer.
  541. *
  542. * @param peer handle for the peer that connected
  543. */
  544. void
  545. GSF_push_start_ (struct GSF_ConnectedPeer *peer)
  546. {
  547. struct MigrationReadyPeer *mrp;
  548. if (GNUNET_YES != enabled)
  549. return;
  550. mrp = GNUNET_malloc (sizeof (struct MigrationReadyPeer));
  551. mrp->peer = peer;
  552. find_content (mrp);
  553. GNUNET_CONTAINER_DLL_insert (peer_head,
  554. peer_tail,
  555. mrp);
  556. }
  557. /**
  558. * A peer disconnected from us. Stop pushing content
  559. * to this peer.
  560. *
  561. * @param peer handle for the peer that disconnected
  562. */
  563. void
  564. GSF_push_stop_ (struct GSF_ConnectedPeer *peer)
  565. {
  566. struct MigrationReadyPeer *pos;
  567. pos = peer_head;
  568. while (pos != NULL)
  569. {
  570. if (pos->peer == peer)
  571. {
  572. GNUNET_CONTAINER_DLL_remove (peer_head,
  573. peer_tail,
  574. pos);
  575. if (NULL != pos->th)
  576. {
  577. GSF_peer_transmit_cancel_ (pos->th);
  578. pos->th = NULL;
  579. }
  580. if (NULL != pos->msg)
  581. {
  582. GNUNET_free (pos->msg);
  583. pos->msg = NULL;
  584. }
  585. GNUNET_free (pos);
  586. return;
  587. }
  588. pos = pos->next;
  589. }
  590. }
  591. /**
  592. * Setup the module.
  593. */
  594. void
  595. GSF_push_init_ ()
  596. {
  597. enabled = GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
  598. "FS",
  599. "CONTENT_PUSHING");
  600. if (GNUNET_YES != enabled)
  601. return;
  602. if (GNUNET_OK !=
  603. GNUNET_CONFIGURATION_get_value_time (GSF_cfg,
  604. "fs",
  605. "MIN_MIGRATION_DELAY",
  606. &min_migration_delay))
  607. {
  608. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  609. _("Invalid value specified for option `%s' in section `%s', content pushing disabled\n"),
  610. "MIN_MIGRATION_DELAY",
  611. "fs");
  612. return;
  613. }
  614. consider_gathering ();
  615. }
  616. /**
  617. * Shutdown the module.
  618. */
  619. void
  620. GSF_push_done_ ()
  621. {
  622. if (GNUNET_SCHEDULER_NO_TASK != mig_task)
  623. {
  624. GNUNET_SCHEDULER_cancel (mig_task);
  625. mig_task = GNUNET_SCHEDULER_NO_TASK;
  626. }
  627. if (NULL != mig_qe)
  628. {
  629. GNUNET_DATASTORE_cancel (mig_qe);
  630. mig_qe = NULL;
  631. }
  632. while (NULL != mig_head)
  633. delete_migration_block (mig_head);
  634. GNUNET_assert (0 == mig_size);
  635. }
  636. /* end of gnunet-service-fs_push.c */