2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/gnunet-service-fs_push.c
23 * @brief API to push content from our datastore to other peers
24 * ('anonymous'-content P2P migration)
25 * @author Christian Grothoff
28 #include "gnunet-service-fs.h"
29 #include "gnunet-service-fs_cp.h"
30 #include "gnunet-service-fs_indexing.h"
31 #include "gnunet-service-fs_push.h"
35 * How long must content remain valid for us to consider it for migration?
36 * If content will expire too soon, there is clearly no point in pushing
37 * it to other peers. This value gives the threshold for migration. Note
38 * that if this value is increased, the migration testcase may need to be
39 * adjusted as well (especially the CONTENT_LIFETIME in fs_test_lib.c).
41 #define MIN_MIGRATION_CONTENT_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
45 * Block that is ready for migration to other peers. Actual data is at the end of the block.
47 struct MigrationReadyBlock
51 * This is a doubly-linked list.
53 struct MigrationReadyBlock *next;
56 * This is a doubly-linked list.
58 struct MigrationReadyBlock *prev;
61 * Query for the block.
63 GNUNET_HashCode query;
66 * When does this block expire?
68 struct GNUNET_TIME_Absolute expiration;
71 * Peers we already forwarded this
72 * block to. Zero for empty entries.
74 GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
82 * Number of targets already used.
84 unsigned int used_targets;
89 enum GNUNET_BLOCK_Type type;
94 * Information about a peer waiting for
97 struct MigrationReadyPeer
100 * This is a doubly-linked list.
102 struct MigrationReadyPeer *next;
105 * This is a doubly-linked list.
107 struct MigrationReadyPeer *prev;
112 struct GSF_ConnectedPeer *peer;
115 * Handle for current transmission request,
118 struct GSF_PeerTransmitHandle *th;
121 * Message we are trying to push right now (or NULL)
123 struct PutMessage *msg;
128 * Head of linked list of blocks that can be migrated.
130 static struct MigrationReadyBlock *mig_head;
133 * Tail of linked list of blocks that can be migrated.
135 static struct MigrationReadyBlock *mig_tail;
138 * Head of linked list of peers.
140 static struct MigrationReadyPeer *peer_head;
143 * Tail of linked list of peers.
145 static struct MigrationReadyPeer *peer_tail;
148 * Request to datastore for migration (or NULL).
150 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
153 * ID of task that collects blocks for migration.
155 static GNUNET_SCHEDULER_TaskIdentifier mig_task;
158 * What is the maximum frequency at which we are allowed to
159 * poll the datastore for migration content?
161 static struct GNUNET_TIME_Relative min_migration_delay;
164 * Size of the doubly-linked list of migration blocks.
166 static unsigned int mig_size;
169 * Is this module enabled?
175 * Delete the given migration block.
177 * @param mb block to delete
180 delete_migration_block (struct MigrationReadyBlock *mb)
182 GNUNET_CONTAINER_DLL_remove (mig_head,
185 GNUNET_PEER_decrement_rcs (mb->target_list,
186 MIGRATION_LIST_SIZE);
193 * Find content for migration to this peer.
196 find_content (struct MigrationReadyPeer *mrp);
200 * Transmit the message currently scheduled for
203 * @param cls the 'struct MigrationReadyPeer'
204 * @param buf_size number of bytes available in buf
205 * @param buf where to copy the message, NULL on error (peer disconnect)
206 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
209 transmit_message (void *cls,
213 struct MigrationReadyPeer *peer = cls;
214 struct PutMessage *msg;
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Failed to migrate content to another peer (disconnect)\n");
229 msize = ntohs (msg->header.size);
230 GNUNET_assert (msize <= buf_size);
231 memcpy (buf, msg, msize);
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "Pushing %u bytes to another peer\n",
244 * Send the given block to the given peer.
246 * @param peer target peer
247 * @param block the block
248 * @return GNUNET_YES if the block was deleted (!)
251 transmit_content (struct MigrationReadyPeer *peer,
252 struct MigrationReadyBlock *block)
255 struct PutMessage *msg;
257 struct GSF_PeerPerformanceData *ppd;
260 ppd = GSF_get_peer_performance_data_ (peer->peer);
261 GNUNET_assert (NULL == peer->th);
262 msize = sizeof (struct PutMessage) + block->size;
263 msg = GNUNET_malloc (msize);
264 msg->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
265 msg->header.size = htons (msize);
266 msg->type = htonl (block->type);
267 msg->expiration = GNUNET_TIME_absolute_hton (block->expiration);
272 for (i=0;i<MIGRATION_LIST_SIZE;i++)
274 if (block->target_list[i] == 0)
276 block->target_list[i] = ppd->pid;
277 GNUNET_PEER_change_rc (block->target_list[i], 1);
281 if (MIGRATION_LIST_SIZE == i)
283 delete_migration_block (block);
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Asking for transmission of %u bytes for migration\n",
295 peer->th = GSF_peer_transmit_ (peer->peer,
298 GNUNET_TIME_UNIT_FOREVER_REL,
307 * Count the number of peers this block has
308 * already been forwarded to.
310 * @param block the block
311 * @return number of times block was forwarded
314 count_targets (struct MigrationReadyBlock *block)
318 for (i=0;i<MIGRATION_LIST_SIZE;i++)
319 if (block->target_list[i] == 0)
326 * Check if sending this block to this peer would
329 * @param peer target peer
330 * @param block the block
331 * @return score (>= 0: feasible, negative: infeasible)
334 score_content (struct MigrationReadyPeer *peer,
335 struct MigrationReadyBlock *block)
338 struct GSF_PeerPerformanceData *ppd;
339 struct GNUNET_PeerIdentity id;
342 ppd = GSF_get_peer_performance_data_ (peer->peer);
343 for (i=0;i<MIGRATION_LIST_SIZE;i++)
344 if (block->target_list[i] == ppd->pid)
346 GNUNET_PEER_resolve (ppd->pid,
348 dist = GNUNET_CRYPTO_hash_distance_u32 (&block->query,
350 /* closer distance, higher score: */
351 return UINT32_MAX - dist;
356 * If the migration task is not currently running, consider
357 * (re)scheduling it with the appropriate delay.
360 consider_gathering (void);
364 * Find content for migration to this peer.
366 * @param mrp peer to find content for
369 find_content (struct MigrationReadyPeer *mrp)
371 struct MigrationReadyBlock *pos;
374 struct MigrationReadyBlock *best;
376 GNUNET_assert (NULL == mrp->th);
382 score = score_content (mrp, pos);
383 if (score > best_score)
392 if (mig_size < MAX_MIGRATION_QUEUE)
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "No content found for pushing, waiting for queue to fill\n");
398 return; /* will fill up eventually... */
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "No suitable content found, purging content from full queue\n");
404 /* failed to find migration target AND
405 queue is full, purge most-forwarded
406 block from queue to make room for more */
411 score = count_targets (pos);
412 if (score >= best_score)
419 GNUNET_assert (NULL != best);
420 delete_migration_block (best);
421 consider_gathering ();
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "Preparing to push best content to peer\n");
428 transmit_content (mrp, best);
433 * Task that is run periodically to obtain blocks for content
437 * @param tc scheduler context (also unused)
440 gather_migration_blocks (void *cls,
441 const struct GNUNET_SCHEDULER_TaskContext *tc);
445 * If the migration task is not currently running, consider
446 * (re)scheduling it with the appropriate delay.
449 consider_gathering ()
451 struct GNUNET_TIME_Relative delay;
457 if (mig_task != GNUNET_SCHEDULER_NO_TASK)
459 if (mig_size >= MAX_MIGRATION_QUEUE)
461 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
463 delay = GNUNET_TIME_relative_divide (delay,
464 MAX_MIGRATION_QUEUE);
465 delay = GNUNET_TIME_relative_max (delay,
466 min_migration_delay);
467 mig_task = GNUNET_SCHEDULER_add_delayed (delay,
468 &gather_migration_blocks,
474 * Process content offered for migration.
477 * @param key key for the content
478 * @param size number of bytes in data
479 * @param data content stored
480 * @param type type of the content
481 * @param priority priority of the content
482 * @param anonymity anonymity-level for the content
483 * @param expiration expiration time for the content
484 * @param uid unique identifier for the datum;
485 * maybe 0 if no unique identifier is available
488 process_migration_content (void *cls,
489 const GNUNET_HashCode * key,
492 enum GNUNET_BLOCK_Type type,
495 struct GNUNET_TIME_Absolute
496 expiration, uint64_t uid)
498 struct MigrationReadyBlock *mb;
499 struct MigrationReadyPeer *pos;
504 consider_gathering ();
507 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value <
508 MIN_MIGRATION_CONTENT_LIFETIME.rel_value)
510 /* content will expire soon, don't bother */
511 GNUNET_DATASTORE_get_next (GSF_dsh);
514 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
517 GNUNET_FS_handle_on_demand_block (key, size, data,
518 type, priority, anonymity,
520 &process_migration_content,
523 GNUNET_DATASTORE_get_next (GSF_dsh);
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529 "Retrieved block `%s' of type %u for migration\n",
533 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
535 mb->expiration = expiration;
538 memcpy (&mb[1], data, size);
539 GNUNET_CONTAINER_DLL_insert_after (mig_head,
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
551 "Preparing to push best content to peer\n");
553 if (GNUNET_YES == transmit_content (pos, mb))
554 break; /* 'mb' was freed! */
558 GNUNET_DATASTORE_get_next (GSF_dsh);
563 * Task that is run periodically to obtain blocks for content
567 * @param tc scheduler context (also unused)
570 gather_migration_blocks (void *cls,
571 const struct GNUNET_SCHEDULER_TaskContext *tc)
573 mig_task = GNUNET_SCHEDULER_NO_TASK;
576 mig_qe = GNUNET_DATASTORE_get_random (GSF_dsh,
578 GNUNET_TIME_UNIT_FOREVER_REL,
579 &process_migration_content, NULL);
580 GNUNET_assert (mig_qe != NULL);
586 * A peer connected to us. Start pushing content
589 * @param peer handle for the peer that connected
592 GSF_push_start_ (struct GSF_ConnectedPeer *peer)
594 struct MigrationReadyPeer *mrp;
596 if (GNUNET_YES != enabled)
598 mrp = GNUNET_malloc (sizeof (struct MigrationReadyPeer));
601 GNUNET_CONTAINER_DLL_insert (peer_head,
608 * A peer disconnected from us. Stop pushing content
611 * @param peer handle for the peer that disconnected
614 GSF_push_stop_ (struct GSF_ConnectedPeer *peer)
616 struct MigrationReadyPeer *pos;
621 if (pos->peer == peer)
623 GNUNET_CONTAINER_DLL_remove (peer_head,
627 GSF_peer_transmit_cancel_ (pos->th);
642 enabled = GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
645 if (GNUNET_YES != enabled)
649 GNUNET_CONFIGURATION_get_value_time (GSF_cfg,
651 "MIN_MIGRATION_DELAY",
652 &min_migration_delay))
654 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
655 _("Invalid value specified for option `%s' in section `%s', content pushing disabled\n"),
656 "MIN_MIGRATION_DELAY",
660 consider_gathering ();
665 * Shutdown the module.
670 if (GNUNET_SCHEDULER_NO_TASK != mig_task)
672 GNUNET_SCHEDULER_cancel (mig_task);
673 mig_task = GNUNET_SCHEDULER_NO_TASK;
677 GNUNET_DATASTORE_cancel (mig_qe);
680 while (NULL != mig_head)
681 delete_migration_block (mig_head);
682 GNUNET_assert (0 == mig_size);
685 /* end of gnunet-service-fs_push.c */