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"
34 #define DEBUG_FS_MIGRATION GNUNET_NO
37 * How long must content remain valid for us to consider it for migration?
38 * If content will expire too soon, there is clearly no point in pushing
39 * it to other peers. This value gives the threshold for migration. Note
40 * that if this value is increased, the migration testcase may need to be
41 * adjusted as well (especially the CONTENT_LIFETIME in fs_test_lib.c).
43 #define MIN_MIGRATION_CONTENT_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
47 * Block that is ready for migration to other peers. Actual data is at the end of the block.
49 struct MigrationReadyBlock
53 * This is a doubly-linked list.
55 struct MigrationReadyBlock *next;
58 * This is a doubly-linked list.
60 struct MigrationReadyBlock *prev;
63 * Query for the block.
65 GNUNET_HashCode query;
68 * When does this block expire?
70 struct GNUNET_TIME_Absolute expiration;
73 * Peers we already forwarded this
74 * block to. Zero for empty entries.
76 GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
84 * Number of targets already used.
86 unsigned int used_targets;
91 enum GNUNET_BLOCK_Type type;
96 * Information about a peer waiting for
99 struct MigrationReadyPeer
102 * This is a doubly-linked list.
104 struct MigrationReadyPeer *next;
107 * This is a doubly-linked list.
109 struct MigrationReadyPeer *prev;
114 struct GSF_ConnectedPeer *peer;
117 * Handle for current transmission request,
120 struct GSF_PeerTransmitHandle *th;
123 * Message we are trying to push right now (or NULL)
125 struct PutMessage *msg;
130 * Head of linked list of blocks that can be migrated.
132 static struct MigrationReadyBlock *mig_head;
135 * Tail of linked list of blocks that can be migrated.
137 static struct MigrationReadyBlock *mig_tail;
140 * Head of linked list of peers.
142 static struct MigrationReadyPeer *peer_head;
145 * Tail of linked list of peers.
147 static struct MigrationReadyPeer *peer_tail;
150 * Request to datastore for migration (or NULL).
152 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
155 * ID of task that collects blocks for migration.
157 static GNUNET_SCHEDULER_TaskIdentifier mig_task;
160 * What is the maximum frequency at which we are allowed to
161 * poll the datastore for migration content?
163 static struct GNUNET_TIME_Relative min_migration_delay;
166 * Size of the doubly-linked list of migration blocks.
168 static unsigned int mig_size;
171 * Is this module enabled?
177 * Delete the given migration block.
179 * @param mb block to delete
182 delete_migration_block (struct MigrationReadyBlock *mb)
184 GNUNET_CONTAINER_DLL_remove (mig_head,
187 GNUNET_PEER_decrement_rcs (mb->target_list,
188 MIGRATION_LIST_SIZE);
195 * Find content for migration to this peer.
198 find_content (struct MigrationReadyPeer *mrp);
202 * Transmit the message currently scheduled for
205 * @param cls the 'struct MigrationReadyPeer'
206 * @param buf_size number of bytes available in buf
207 * @param buf where to copy the message, NULL on error (peer disconnect)
208 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
211 transmit_message (void *cls,
215 struct MigrationReadyPeer *peer = cls;
216 struct PutMessage *msg;
224 #if DEBUG_FS_MIGRATION
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Failed to migrate content to another peer (disconnect)\n");
231 msize = ntohs (msg->header.size);
232 GNUNET_assert (msize <= buf_size);
233 memcpy (buf, msg, msize);
235 #if DEBUG_FS_MIGRATION
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
237 "Pushing %u bytes to another peer\n",
246 * Send the given block to the given peer.
248 * @param peer target peer
249 * @param block the block
250 * @return GNUNET_YES if the block was deleted (!)
253 transmit_content (struct MigrationReadyPeer *peer,
254 struct MigrationReadyBlock *block)
257 struct PutMessage *msg;
259 struct GSF_PeerPerformanceData *ppd;
262 ppd = GSF_get_peer_performance_data_ (peer->peer);
263 GNUNET_assert (NULL == peer->th);
264 msize = sizeof (struct PutMessage) + block->size;
265 msg = GNUNET_malloc (msize);
266 msg->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
267 msg->header.size = htons (msize);
268 msg->type = htonl (block->type);
269 msg->expiration = GNUNET_TIME_absolute_hton (block->expiration);
274 for (i=0;i<MIGRATION_LIST_SIZE;i++)
276 if (block->target_list[i] == 0)
278 block->target_list[i] = ppd->pid;
279 GNUNET_PEER_change_rc (block->target_list[i], 1);
283 if (MIGRATION_LIST_SIZE == i)
285 delete_migration_block (block);
292 #if DEBUG_FS_MIGRATION
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "Asking for transmission of %u bytes for migration\n",
297 peer->th = GSF_peer_transmit_ (peer->peer,
300 GNUNET_TIME_UNIT_FOREVER_REL,
309 * Count the number of peers this block has
310 * already been forwarded to.
312 * @param block the block
313 * @return number of times block was forwarded
316 count_targets (struct MigrationReadyBlock *block)
320 for (i=0;i<MIGRATION_LIST_SIZE;i++)
321 if (block->target_list[i] == 0)
328 * Check if sending this block to this peer would
331 * @param peer target peer
332 * @param block the block
333 * @return score (>= 0: feasible, negative: infeasible)
336 score_content (struct MigrationReadyPeer *peer,
337 struct MigrationReadyBlock *block)
340 struct GSF_PeerPerformanceData *ppd;
341 struct GNUNET_PeerIdentity id;
344 ppd = GSF_get_peer_performance_data_ (peer->peer);
345 for (i=0;i<MIGRATION_LIST_SIZE;i++)
346 if (block->target_list[i] == ppd->pid)
348 GNUNET_PEER_resolve (ppd->pid,
350 dist = GNUNET_CRYPTO_hash_distance_u32 (&block->query,
352 /* closer distance, higher score: */
353 return UINT32_MAX - dist;
358 * If the migration task is not currently running, consider
359 * (re)scheduling it with the appropriate delay.
362 consider_gathering (void);
366 * Find content for migration to this peer.
368 * @param mrp peer to find content for
371 find_content (struct MigrationReadyPeer *mrp)
373 struct MigrationReadyBlock *pos;
376 struct MigrationReadyBlock *best;
378 GNUNET_assert (NULL == mrp->th);
384 score = score_content (mrp, pos);
385 if (score > best_score)
394 if (mig_size < MAX_MIGRATION_QUEUE)
396 #if DEBUG_FS_MIGRATION
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "No content found for pushing, waiting for queue to fill\n");
400 return; /* will fill up eventually... */
402 #if DEBUG_FS_MIGRATION
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "No suitable content found, purging content from full queue\n");
406 /* failed to find migration target AND
407 queue is full, purge most-forwarded
408 block from queue to make room for more */
412 score = count_targets (pos);
413 if (score >= best_score)
420 GNUNET_assert (NULL != best);
421 delete_migration_block (best);
422 consider_gathering ();
425 #if DEBUG_FS_MIGRATION
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Preparing to push best content to peer\n");
429 transmit_content (mrp, best);
434 * Task that is run periodically to obtain blocks for content
438 * @param tc scheduler context (also unused)
441 gather_migration_blocks (void *cls,
442 const struct GNUNET_SCHEDULER_TaskContext *tc);
446 * If the migration task is not currently running, consider
447 * (re)scheduling it with the appropriate delay.
450 consider_gathering ()
452 struct GNUNET_TIME_Relative delay;
458 if (mig_task != GNUNET_SCHEDULER_NO_TASK)
460 if (mig_size >= MAX_MIGRATION_QUEUE)
462 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
464 delay = GNUNET_TIME_relative_divide (delay,
465 MAX_MIGRATION_QUEUE);
466 delay = GNUNET_TIME_relative_max (delay,
467 min_migration_delay);
468 #if DEBUG_FS_MIGRATION
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470 "Scheduling gathering task (queue size: %u)\n",
473 mig_task = GNUNET_SCHEDULER_add_delayed (delay,
474 &gather_migration_blocks,
480 * Process content offered for migration.
483 * @param key key for the content
484 * @param size number of bytes in data
485 * @param data content stored
486 * @param type type of the content
487 * @param priority priority of the content
488 * @param anonymity anonymity-level for the content
489 * @param expiration expiration time for the content
490 * @param uid unique identifier for the datum;
491 * maybe 0 if no unique identifier is available
494 process_migration_content (void *cls,
495 const GNUNET_HashCode *key,
498 enum GNUNET_BLOCK_Type type,
501 struct GNUNET_TIME_Absolute
502 expiration, uint64_t uid)
504 struct MigrationReadyBlock *mb;
505 struct MigrationReadyPeer *pos;
510 #if DEBUG_FS_MIGRATION
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
512 "No content found for migration...\n");
514 consider_gathering ();
517 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value <
518 MIN_MIGRATION_CONTENT_LIFETIME.rel_value)
520 /* content will expire soon, don't bother */
521 consider_gathering ();
524 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
527 GNUNET_FS_handle_on_demand_block (key, size, data,
528 type, priority, anonymity,
530 &process_migration_content,
532 consider_gathering ();
535 #if DEBUG_FS_MIGRATION
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 "Retrieved block `%s' of type %u for migration (queue size: %u/%u)\n",
541 MAX_MIGRATION_QUEUE);
543 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
545 mb->expiration = expiration;
548 memcpy (&mb[1], data, size);
549 GNUNET_CONTAINER_DLL_insert_after (mig_head,
559 #if DEBUG_FS_MIGRATION
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "Preparing to push best content to peer\n");
563 if (GNUNET_YES == transmit_content (pos, mb))
564 break; /* 'mb' was freed! */
568 consider_gathering ();
573 * Task that is run periodically to obtain blocks for content
577 * @param tc scheduler context (also unused)
580 gather_migration_blocks (void *cls,
581 const struct GNUNET_SCHEDULER_TaskContext *tc)
583 mig_task = GNUNET_SCHEDULER_NO_TASK;
584 if (mig_size >= MAX_MIGRATION_QUEUE)
588 #if DEBUG_FS_MIGRATION
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "Asking datastore for content for replication (queue size: %u)\n",
593 mig_qe = GNUNET_DATASTORE_get_for_replication (GSF_dsh,
595 GNUNET_TIME_UNIT_FOREVER_REL,
596 &process_migration_content, NULL);
598 consider_gathering ();
604 * A peer connected to us. Start pushing content
607 * @param peer handle for the peer that connected
610 GSF_push_start_ (struct GSF_ConnectedPeer *peer)
612 struct MigrationReadyPeer *mrp;
614 if (GNUNET_YES != enabled)
616 mrp = GNUNET_malloc (sizeof (struct MigrationReadyPeer));
619 GNUNET_CONTAINER_DLL_insert (peer_head,
626 * A peer disconnected from us. Stop pushing content
629 * @param peer handle for the peer that disconnected
632 GSF_push_stop_ (struct GSF_ConnectedPeer *peer)
634 struct MigrationReadyPeer *pos;
639 if (pos->peer == peer)
641 GNUNET_CONTAINER_DLL_remove (peer_head,
645 GSF_peer_transmit_cancel_ (pos->th);
660 enabled = GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
663 if (GNUNET_YES != enabled)
667 GNUNET_CONFIGURATION_get_value_time (GSF_cfg,
669 "MIN_MIGRATION_DELAY",
670 &min_migration_delay))
672 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
673 _("Invalid value specified for option `%s' in section `%s', content pushing disabled\n"),
674 "MIN_MIGRATION_DELAY",
678 consider_gathering ();
683 * Shutdown the module.
688 if (GNUNET_SCHEDULER_NO_TASK != mig_task)
690 GNUNET_SCHEDULER_cancel (mig_task);
691 mig_task = GNUNET_SCHEDULER_NO_TASK;
695 GNUNET_DATASTORE_cancel (mig_qe);
698 while (NULL != mig_head)
699 delete_migration_block (mig_head);
700 GNUNET_assert (0 == mig_size);
703 /* end of gnunet-service-fs_push.c */