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 * Maximum number of blocks we keep in memory for migration.
37 #define MAX_MIGRATION_QUEUE 8
40 * Blocks are at most migrated to this number of peers
41 * plus one, each time they are fetched from the database.
43 #define MIGRATION_LIST_SIZE 2
46 * How long must content remain valid for us to consider it for migration?
47 * If content will expire too soon, there is clearly no point in pushing
48 * it to other peers. This value gives the threshold for migration. Note
49 * that if this value is increased, the migration testcase may need to be
50 * adjusted as well (especially the CONTENT_LIFETIME in fs_test_lib.c).
52 #define MIN_MIGRATION_CONTENT_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
56 * Block that is ready for migration to other peers. Actual data is at the end of the block.
58 struct MigrationReadyBlock
62 * This is a doubly-linked list.
64 struct MigrationReadyBlock *next;
67 * This is a doubly-linked list.
69 struct MigrationReadyBlock *prev;
72 * Query for the block.
74 GNUNET_HashCode query;
77 * When does this block expire?
79 struct GNUNET_TIME_Absolute expiration;
82 * Peers we already forwarded this
83 * block to. Zero for empty entries.
85 GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
93 * Number of targets already used.
95 unsigned int used_targets;
100 enum GNUNET_BLOCK_Type type;
105 * Information about a peer waiting for
108 struct MigrationReadyPeer
111 * This is a doubly-linked list.
113 struct MigrationReadyPeer *next;
116 * This is a doubly-linked list.
118 struct MigrationReadyPeer *prev;
123 struct GSF_ConnectedPeer *peer;
126 * Handle for current transmission request,
129 struct GSF_PeerTransmitHandle *th;
132 * Message we are trying to push right now (or NULL)
134 struct PutMessage *msg;
139 * Head of linked list of blocks that can be migrated.
141 static struct MigrationReadyBlock *mig_head;
144 * Tail of linked list of blocks that can be migrated.
146 static struct MigrationReadyBlock *mig_tail;
149 * Head of linked list of peers.
151 static struct MigrationReadyPeer *peer_head;
154 * Tail of linked list of peers.
156 static struct MigrationReadyPeer *peer_tail;
159 * Request to datastore for migration (or NULL).
161 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
164 * ID of task that collects blocks for migration.
166 static GNUNET_SCHEDULER_TaskIdentifier mig_task;
169 * What is the maximum frequency at which we are allowed to
170 * poll the datastore for migration content?
172 static struct GNUNET_TIME_Relative min_migration_delay;
175 * Size of the doubly-linked list of migration blocks.
177 static unsigned int mig_size;
180 * Is this module enabled?
186 * Delete the given migration block.
188 * @param mb block to delete
191 delete_migration_block (struct MigrationReadyBlock *mb)
193 GNUNET_CONTAINER_DLL_remove (mig_head, mig_tail, mb);
194 GNUNET_PEER_decrement_rcs (mb->target_list, MIGRATION_LIST_SIZE);
201 * Find content for migration to this peer.
204 find_content (struct MigrationReadyPeer *mrp);
208 * Transmit the message currently scheduled for
211 * @param cls the 'struct MigrationReadyPeer'
212 * @param buf_size number of bytes available in buf
213 * @param buf where to copy the message, NULL on error (peer disconnect)
214 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
217 transmit_message (void *cls, size_t buf_size, void *buf)
219 struct MigrationReadyPeer *peer = cls;
220 struct PutMessage *msg;
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229 "Failed to migrate content to another peer (disconnect)\n");
233 msize = ntohs (msg->header.size);
234 GNUNET_assert (msize <= buf_size);
235 memcpy (buf, msg, msize);
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Pushing %u bytes to another peer\n",
245 * Send the given block to the given peer.
247 * @param peer target peer
248 * @param block the block
249 * @return GNUNET_YES if the block was deleted (!)
252 transmit_content (struct MigrationReadyPeer *peer,
253 struct MigrationReadyBlock *block)
256 struct PutMessage *msg;
258 struct GSF_PeerPerformanceData *ppd;
261 ppd = GSF_get_peer_performance_data_ (peer->peer);
262 GNUNET_assert (NULL == peer->th);
263 msize = sizeof (struct PutMessage) + block->size;
264 msg = GNUNET_malloc (msize);
265 msg->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
266 msg->header.size = htons (msize);
267 msg->type = htonl (block->type);
268 msg->expiration = GNUNET_TIME_absolute_hton (block->expiration);
269 memcpy (&msg[1], &block[1], block->size);
271 for (i = 0; i < MIGRATION_LIST_SIZE; i++)
273 if (block->target_list[i] == 0)
275 block->target_list[i] = ppd->pid;
276 GNUNET_PEER_change_rc (block->target_list[i], 1);
280 if (MIGRATION_LIST_SIZE == i)
282 delete_migration_block (block);
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "Asking for transmission of %u bytes for migration\n", msize);
291 peer->th = GSF_peer_transmit_ (peer->peer, GNUNET_NO, 0 /* priority */ ,
292 GNUNET_TIME_UNIT_FOREVER_REL, msize,
293 &transmit_message, peer);
299 * Count the number of peers this block has
300 * already been forwarded to.
302 * @param block the block
303 * @return number of times block was forwarded
306 count_targets (struct MigrationReadyBlock *block)
310 for (i = 0; i < MIGRATION_LIST_SIZE; i++)
311 if (block->target_list[i] == 0)
318 * Check if sending this block to this peer would
321 * @param peer target peer
322 * @param block the block
323 * @return score (>= 0: feasible, negative: infeasible)
326 score_content (struct MigrationReadyPeer *peer,
327 struct MigrationReadyBlock *block)
330 struct GSF_PeerPerformanceData *ppd;
331 struct GNUNET_PeerIdentity id;
334 ppd = GSF_get_peer_performance_data_ (peer->peer);
335 for (i = 0; i < MIGRATION_LIST_SIZE; i++)
336 if (block->target_list[i] == ppd->pid)
338 GNUNET_assert (0 != ppd->pid);
339 GNUNET_PEER_resolve (ppd->pid, &id);
340 dist = GNUNET_CRYPTO_hash_distance_u32 (&block->query, &id.hashPubKey);
341 /* closer distance, higher score: */
342 return UINT32_MAX - dist;
347 * If the migration task is not currently running, consider
348 * (re)scheduling it with the appropriate delay.
351 consider_gathering (void);
355 * Find content for migration to this peer.
357 * @param mrp peer to find content for
360 find_content (struct MigrationReadyPeer *mrp)
362 struct MigrationReadyBlock *pos;
365 struct MigrationReadyBlock *best;
367 GNUNET_assert (NULL == mrp->th);
373 score = score_content (mrp, pos);
374 if (score > best_score)
383 if (mig_size < MAX_MIGRATION_QUEUE)
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386 "No content found for pushing, waiting for queue to fill\n");
387 return; /* will fill up eventually... */
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "No suitable content found, purging content from full queue\n");
391 /* failed to find migration target AND
392 * queue is full, purge most-forwarded
393 * block from queue to make room for more */
397 score = count_targets (pos);
398 if (score >= best_score)
405 GNUNET_assert (NULL != best);
406 delete_migration_block (best);
407 consider_gathering ();
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Preparing to push best content to peer\n");
412 transmit_content (mrp, best);
417 * Task that is run periodically to obtain blocks for content
421 * @param tc scheduler context (also unused)
424 gather_migration_blocks (void *cls,
425 const struct GNUNET_SCHEDULER_TaskContext *tc);
429 * If the migration task is not currently running, consider
430 * (re)scheduling it with the appropriate delay.
433 consider_gathering ()
435 struct GNUNET_TIME_Relative delay;
441 if (mig_task != GNUNET_SCHEDULER_NO_TASK)
443 if (mig_size >= MAX_MIGRATION_QUEUE)
445 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, mig_size);
446 delay = GNUNET_TIME_relative_divide (delay, MAX_MIGRATION_QUEUE);
447 delay = GNUNET_TIME_relative_max (delay, min_migration_delay);
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449 "Scheduling gathering task (queue size: %u)\n", mig_size);
451 GNUNET_SCHEDULER_add_delayed (delay, &gather_migration_blocks, NULL);
456 * Process content offered for migration.
459 * @param key key for the content
460 * @param size number of bytes in data
461 * @param data content stored
462 * @param type type of the content
463 * @param priority priority of the content
464 * @param anonymity anonymity-level for the content
465 * @param expiration expiration time for the content
466 * @param uid unique identifier for the datum;
467 * maybe 0 if no unique identifier is available
470 process_migration_content (void *cls, const GNUNET_HashCode * key, size_t size,
471 const void *data, enum GNUNET_BLOCK_Type type,
472 uint32_t priority, uint32_t anonymity,
473 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
475 struct MigrationReadyBlock *mb;
476 struct MigrationReadyPeer *pos;
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No content found for migration...\n");
482 consider_gathering ();
485 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value <
486 MIN_MIGRATION_CONTENT_LIFETIME.rel_value)
488 /* content will expire soon, don't bother */
489 consider_gathering ();
492 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
495 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
496 anonymity, expiration, uid,
497 &process_migration_content, NULL))
498 consider_gathering ();
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Retrieved block `%s' of type %u for migration (queue size: %u/%u)\n",
503 GNUNET_h2s (key), type, mig_size + 1, MAX_MIGRATION_QUEUE);
504 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
506 mb->expiration = expiration;
509 memcpy (&mb[1], data, size);
510 GNUNET_CONTAINER_DLL_insert_after (mig_head, mig_tail, mig_tail, mb);
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "Preparing to push best content to peer\n");
519 if (GNUNET_YES == transmit_content (pos, mb))
520 break; /* 'mb' was freed! */
524 consider_gathering ();
529 * Task that is run periodically to obtain blocks for content
533 * @param tc scheduler context (also unused)
536 gather_migration_blocks (void *cls,
537 const struct GNUNET_SCHEDULER_TaskContext *tc)
539 mig_task = GNUNET_SCHEDULER_NO_TASK;
540 if (mig_size >= MAX_MIGRATION_QUEUE)
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "Asking datastore for content for replication (queue size: %u)\n",
548 GNUNET_DATASTORE_get_for_replication (GSF_dsh, 0, UINT_MAX,
549 GNUNET_TIME_UNIT_FOREVER_REL,
550 &process_migration_content, NULL);
552 consider_gathering ();
558 * A peer connected to us. Start pushing content
561 * @param peer handle for the peer that connected
564 GSF_push_start_ (struct GSF_ConnectedPeer *peer)
566 struct MigrationReadyPeer *mrp;
568 if (GNUNET_YES != enabled)
570 mrp = GNUNET_malloc (sizeof (struct MigrationReadyPeer));
573 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, mrp);
578 * A peer disconnected from us. Stop pushing content
581 * @param peer handle for the peer that disconnected
584 GSF_push_stop_ (struct GSF_ConnectedPeer *peer)
586 struct MigrationReadyPeer *pos;
591 if (pos->peer == peer)
593 GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
596 GSF_peer_transmit_cancel_ (pos->th);
599 if (NULL != pos->msg)
601 GNUNET_free (pos->msg);
619 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_PUSHING");
620 if (GNUNET_YES != enabled)
624 GNUNET_CONFIGURATION_get_value_time (GSF_cfg, "fs", "MIN_MIGRATION_DELAY",
625 &min_migration_delay))
627 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
629 ("Invalid value specified for option `%s' in section `%s', content pushing disabled\n"),
630 "MIN_MIGRATION_DELAY", "fs");
633 consider_gathering ();
638 * Shutdown the module.
643 if (GNUNET_SCHEDULER_NO_TASK != mig_task)
645 GNUNET_SCHEDULER_cancel (mig_task);
646 mig_task = GNUNET_SCHEDULER_NO_TASK;
650 GNUNET_DATASTORE_cancel (mig_qe);
653 while (NULL != mig_head)
654 delete_migration_block (mig_head);
655 GNUNET_assert (0 == mig_size);
658 /* end of gnunet-service-fs_push.c */