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_cp.c
23 * @brief API to handle 'connected peers'
24 * @author Christian Grothoff
27 #include "gnunet_load_lib.h"
28 #include "gnunet-service-fs.h"
29 #include "gnunet-service-fs_cp.h"
30 #include "gnunet-service-fs_pe.h"
31 #include "gnunet-service-fs_pr.h"
32 #include "gnunet-service-fs_push.h"
36 * Ratio for moving average delay calculation. The previous
37 * average goes in with a factor of (n-1) into the calculation.
40 #define RUNAVG_DELAY_N 16
43 * How often do we flush respect values to disk?
45 #define RESPECT_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
48 * After how long do we discard a reply?
50 #define REPLY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
53 * Collect an instane number of statistics? May cause excessive IPC.
55 #define INSANE_STATISTICS GNUNET_NO
59 * Handle to cancel a transmission request.
61 struct GSF_PeerTransmitHandle
65 * Kept in a doubly-linked list.
67 struct GSF_PeerTransmitHandle *next;
70 * Kept in a doubly-linked list.
72 struct GSF_PeerTransmitHandle *prev;
75 * Time when this transmission request was issued.
77 struct GNUNET_TIME_Absolute transmission_request_start_time;
80 * Timeout for this request.
82 struct GNUNET_TIME_Absolute timeout;
85 * Task called on timeout, or 0 for none.
87 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
90 * Function to call to get the actual message.
92 GSF_GetMessageCallback gmc;
95 * Peer this request targets.
97 struct GSF_ConnectedPeer *cp;
105 * Size of the message to be transmitted.
110 * GNUNET_YES if this is a query, GNUNET_NO for content.
115 * Did we get a reservation already?
120 * Priority of this request.
128 * Handle for an entry in our delay list.
130 struct GSF_DelayedHandle
134 * Kept in a doubly-linked list.
136 struct GSF_DelayedHandle *next;
139 * Kept in a doubly-linked list.
141 struct GSF_DelayedHandle *prev;
144 * Peer this transmission belongs to.
146 struct GSF_ConnectedPeer *cp;
149 * The PUT that was delayed.
151 struct PutMessage *pm;
154 * Task for the delay.
156 GNUNET_SCHEDULER_TaskIdentifier delay_task;
159 * Size of the message.
167 * Information per peer and request.
173 * Handle to generic request.
175 struct GSF_PendingRequest *pr;
178 * Handle to specific peer.
180 struct GSF_ConnectedPeer *cp;
183 * Task for asynchronous stopping of this request.
185 GNUNET_SCHEDULER_TaskIdentifier kill_task;
193 struct GSF_ConnectedPeer
197 * Performance data for this peer.
199 struct GSF_PeerPerformanceData ppd;
202 * Time until when we blocked this peer from migrating
205 struct GNUNET_TIME_Absolute last_migration_block;
208 * Task scheduled to revive migration to this peer.
210 GNUNET_SCHEDULER_TaskIdentifier mig_revive_task;
213 * Messages (replies, queries, content migration) we would like to
214 * send to this peer in the near future. Sorted by priority, head.
216 struct GSF_PeerTransmitHandle *pth_head;
219 * Messages (replies, queries, content migration) we would like to
220 * send to this peer in the near future. Sorted by priority, tail.
222 struct GSF_PeerTransmitHandle *pth_tail;
225 * Messages (replies, queries, content migration) we would like to
226 * send to this peer in the near future. Sorted by priority, head.
228 struct GSF_DelayedHandle *delayed_head;
231 * Messages (replies, queries, content migration) we would like to
232 * send to this peer in the near future. Sorted by priority, tail.
234 struct GSF_DelayedHandle *delayed_tail;
237 * Migration stop message in our queue, or NULL if we have none pending.
239 struct GSF_PeerTransmitHandle *migration_pth;
242 * Context of our GNUNET_ATS_reserve_bandwidth call (or NULL).
244 struct GNUNET_ATS_ReservationContext *rc;
247 * Task scheduled if we need to retry bandwidth reservation later.
249 GNUNET_SCHEDULER_TaskIdentifier rc_delay_task;
252 * Active requests from this neighbour, map of query to 'struct PeerRequest'.
254 struct GNUNET_CONTAINER_MultiHashMap *request_map;
257 * Handle for an active request for transmission to this
258 * peer, or NULL (if core queue was full).
260 struct GNUNET_CORE_TransmitHandle *cth;
263 * Increase in traffic preference still to be submitted
264 * to the core service for this peer.
266 uint64_t inc_preference;
269 * Set to 1 if we're currently in the process of calling
270 * 'GNUNET_CORE_notify_transmit_ready' (so while cth is
271 * NULL, we should not call notify_transmit_ready for this
274 unsigned int cth_in_progress;
277 * Respect rating for this peer on disk.
279 uint32_t disk_respect;
282 * Which offset in "last_p2p_replies" will be updated next?
283 * (we go round-robin).
285 unsigned int last_p2p_replies_woff;
288 * Which offset in "last_client_replies" will be updated next?
289 * (we go round-robin).
291 unsigned int last_client_replies_woff;
294 * Current offset into 'last_request_times' ring buffer.
296 unsigned int last_request_times_off;
299 * GNUNET_YES if we did successfully reserve 32k bandwidth,
308 * Map from peer identities to 'struct GSF_ConnectPeer' entries.
310 static struct GNUNET_CONTAINER_MultiHashMap *cp_map;
313 * Where do we store respect information?
315 static char *respectDirectory;
319 * Get the filename under which we would store respect
320 * for the given peer.
322 * @param id peer to get the filename for
323 * @return filename of the form DIRECTORY/PEERID
326 get_respect_filename (const struct GNUNET_PeerIdentity *id)
328 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
331 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
332 GNUNET_asprintf (&fn, "%s%s%s", respectDirectory, DIR_SEPARATOR_STR, &fil);
338 * Update the latency information kept for the given peer.
340 * @param id peer record to update
341 * @param latency current latency value
344 GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
345 struct GNUNET_TIME_Relative latency)
347 struct GSF_ConnectedPeer *cp;
349 cp = GSF_peer_get_ (id);
351 return; /* we're not yet connected at the core level, ignore */
352 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay, latency);
353 /* LATER: merge atsi into cp's performance data (if we ever care...) */
358 * Return the performance data record for the given peer
360 * @param cp peer to query
361 * @return performance data record for the peer
363 struct GSF_PeerPerformanceData *
364 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
371 * Core is ready to transmit to a peer, get the message.
373 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
374 * @param size number of bytes core is willing to take
375 * @param buf where to copy the message
376 * @return number of bytes copied to buf
379 peer_transmit_ready_cb (void *cls, size_t size, void *buf);
383 * Function called by core upon success or failure of our bandwidth reservation request.
385 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
386 * @param peer identifies the peer
387 * @param amount set to the amount that was actually reserved or unreserved;
388 * either the full requested amount or zero (no partial reservations)
389 * @param res_delay if the reservation could not be satisfied (amount was 0), how
390 * long should the client wait until re-trying?
393 ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
394 int32_t amount, struct GNUNET_TIME_Relative res_delay);
398 * If ready (bandwidth reserved), try to schedule transmission via
399 * core for the given handle.
401 * @param pth transmission handle to schedule
404 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
406 struct GSF_ConnectedPeer *cp;
407 struct GNUNET_PeerIdentity target;
410 if ((NULL != cp->cth) || (0 != cp->cth_in_progress))
411 return; /* already done */
412 GNUNET_assert (0 != cp->ppd.pid);
413 GNUNET_PEER_resolve (cp->ppd.pid, &target);
415 if (0 != cp->inc_preference)
417 GNUNET_ATS_change_preference (GSF_ats, &target, GNUNET_ATS_PREFERENCE_BANDWIDTH,
418 (double) cp->inc_preference,
419 GNUNET_ATS_PREFERENCE_END);
420 cp->inc_preference = 0;
423 if ((GNUNET_YES == pth->is_query) && (GNUNET_YES != pth->was_reserved))
425 /* query, need reservation */
426 if (GNUNET_YES != cp->did_reserve)
427 return; /* not ready */
428 cp->did_reserve = GNUNET_NO;
429 /* reservation already done! */
430 pth->was_reserved = GNUNET_YES;
432 GNUNET_ATS_reserve_bandwidth (GSF_ats, &target, DBLOCK_SIZE,
433 &ats_reserve_callback, cp);
436 GNUNET_assert (NULL == cp->cth);
437 cp->cth_in_progress++;
439 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
440 GNUNET_TIME_absolute_get_remaining
441 (pth->timeout), &target, pth->size,
442 &peer_transmit_ready_cb, cp);
443 GNUNET_assert (NULL != cp->cth);
444 GNUNET_assert (0 < cp->cth_in_progress--);
449 * Core is ready to transmit to a peer, get the message.
451 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
452 * @param size number of bytes core is willing to take
453 * @param buf where to copy the message
454 * @return number of bytes copied to buf
457 peer_transmit_ready_cb (void *cls, size_t size, void *buf)
459 struct GSF_ConnectedPeer *cp = cls;
460 struct GSF_PeerTransmitHandle *pth = cp->pth_head;
461 struct GSF_PeerTransmitHandle *pos;
467 if (pth->size > size)
469 schedule_transmission (pth);
472 if (GNUNET_SCHEDULER_NO_TASK != pth->timeout_task)
474 GNUNET_SCHEDULER_cancel (pth->timeout_task);
475 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
477 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
478 if (GNUNET_YES == pth->is_query)
480 cp->ppd.last_request_times[(cp->last_request_times_off++) %
481 MAX_QUEUE_PER_PEER] =
482 GNUNET_TIME_absolute_get ();
483 GNUNET_assert (0 < cp->ppd.pending_queries--);
485 else if (GNUNET_NO == pth->is_query)
487 GNUNET_assert (0 < cp->ppd.pending_replies--);
489 GNUNET_LOAD_update (cp->ppd.transmission_delay,
490 GNUNET_TIME_absolute_get_duration
491 (pth->transmission_request_start_time).rel_value);
492 ret = pth->gmc (pth->gmc_cls, size, buf);
493 if (NULL != (pos = cp->pth_head))
495 GNUNET_assert (pos != pth);
496 schedule_transmission (pos);
504 * (re)try to reserve bandwidth from the given peer.
506 * @param cls the 'struct GSF_ConnectedPeer' to reserve from
507 * @param tc scheduler context
510 retry_reservation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
512 struct GSF_ConnectedPeer *cp = cls;
513 struct GNUNET_PeerIdentity target;
515 GNUNET_PEER_resolve (cp->ppd.pid, &target);
516 cp->rc_delay_task = GNUNET_SCHEDULER_NO_TASK;
518 GNUNET_ATS_reserve_bandwidth (GSF_ats, &target, DBLOCK_SIZE,
519 &ats_reserve_callback, cp);
524 * Function called by core upon success or failure of our bandwidth reservation request.
526 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
527 * @param peer identifies the peer
528 * @param amount set to the amount that was actually reserved or unreserved;
529 * either the full requested amount or zero (no partial reservations)
530 * @param res_delay if the reservation could not be satisfied (amount was 0), how
531 * long should the client wait until re-trying?
534 ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
535 int32_t amount, struct GNUNET_TIME_Relative res_delay)
537 struct GSF_ConnectedPeer *cp = cls;
538 struct GSF_PeerTransmitHandle *pth;
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 "Reserved %d bytes / need to wait %s for reservation\n",
543 GNUNET_STRINGS_relative_time_to_string (res_delay, GNUNET_YES));
548 GNUNET_SCHEDULER_add_delayed (res_delay, &retry_reservation, cp);
551 cp->did_reserve = GNUNET_YES;
553 if ((NULL != pth) && (NULL == cp->cth) && (0 == cp->cth_in_progress))
555 /* reservation success, try transmission now! */
556 cp->cth_in_progress++;
558 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
559 GNUNET_TIME_absolute_get_remaining
560 (pth->timeout), peer, pth->size,
561 &peer_transmit_ready_cb, cp);
562 GNUNET_assert (NULL != cp->cth);
563 GNUNET_assert (0 < cp->cth_in_progress--);
569 * A peer connected to us. Setup the connected peer
572 * @param peer identity of peer that connected
573 * @return handle to connected peer entry
575 struct GSF_ConnectedPeer *
576 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer)
578 struct GSF_ConnectedPeer *cp;
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s\n",
584 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
585 cp->ppd.pid = GNUNET_PEER_intern (peer);
586 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
588 GNUNET_ATS_reserve_bandwidth (GSF_ats, peer, DBLOCK_SIZE,
589 &ats_reserve_callback, cp);
590 fn = get_respect_filename (peer);
591 if ((GNUNET_YES == GNUNET_DISK_file_test (fn)) &&
592 (sizeof (respect) == GNUNET_DISK_fn_read (fn, &respect, sizeof (respect))))
593 cp->disk_respect = cp->ppd.respect = ntohl (respect);
595 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
596 GNUNET_break (GNUNET_OK ==
597 GNUNET_CONTAINER_multihashmap_put (cp_map,
598 &GSF_connected_peer_get_identity2_ (cp)->hashPubKey,
600 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
601 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
602 GNUNET_CONTAINER_multihashmap_size (cp_map),
604 GSF_push_start_ (cp);
610 * It may be time to re-start migrating content to this
611 * peer. Check, and if so, restart migration.
613 * @param cls the 'struct GSF_ConnectedPeer'
614 * @param tc scheduler context
617 revive_migration (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
619 struct GSF_ConnectedPeer *cp = cls;
620 struct GNUNET_TIME_Relative bt;
622 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
623 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
624 if (0 != bt.rel_value)
626 /* still time left... */
627 cp->mig_revive_task =
628 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
631 GSF_push_start_ (cp);
636 * Get a handle for a connected peer.
638 * @param peer peer's identity
639 * @return NULL if the peer is not currently connected
641 struct GSF_ConnectedPeer *
642 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
646 return GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
651 * Handle P2P "MIGRATION_STOP" message.
653 * @param cls closure, always NULL
654 * @param other the other peer involved (sender or receiver, NULL
655 * for loopback messages where we are both sender and receiver)
656 * @param message the actual message
657 * @return GNUNET_OK to keep the connection open,
658 * GNUNET_SYSERR to close it (signal serious error)
661 GSF_handle_p2p_migration_stop_ (void *cls,
662 const struct GNUNET_PeerIdentity *other,
663 const struct GNUNET_MessageHeader *message)
665 struct GSF_ConnectedPeer *cp;
666 const struct MigrationStopMessage *msm;
667 struct GNUNET_TIME_Relative bt;
669 msm = (const struct MigrationStopMessage *) message;
670 cp = GSF_peer_get_ (other);
676 GNUNET_STATISTICS_update (GSF_stats,
677 gettext_noop ("# migration stop messages received"),
679 bt = GNUNET_TIME_relative_ntoh (msm->duration);
680 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
681 _("Migration of content to peer `%s' blocked for %s\n"),
683 GNUNET_STRINGS_relative_time_to_string (bt, GNUNET_YES));
684 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
685 if (GNUNET_SCHEDULER_NO_TASK == cp->mig_revive_task)
688 cp->mig_revive_task =
689 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
696 * Copy reply and free put message.
698 * @param cls the 'struct PutMessage'
699 * @param buf_size number of bytes available in buf
700 * @param buf where to copy the message, NULL on error (peer disconnect)
701 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
704 copy_reply (void *cls, size_t buf_size, void *buf)
706 struct PutMessage *pm = cls;
711 GNUNET_assert (buf_size >= ntohs (pm->header.size));
712 size = ntohs (pm->header.size);
713 memcpy (buf, pm, size);
714 GNUNET_STATISTICS_update (GSF_stats,
716 ("# replies transmitted to other peers"), 1,
722 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# replies dropped"), 1,
731 * Free resources associated with the given peer request.
733 * @param peerreq request to free
734 * @param query associated key for the request
737 free_pending_request (struct PeerRequest *peerreq,
738 const struct GNUNET_HashCode *query)
740 struct GSF_ConnectedPeer *cp = peerreq->cp;
742 if (GNUNET_SCHEDULER_NO_TASK != peerreq->kill_task)
744 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
745 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
747 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
749 GNUNET_break (GNUNET_YES ==
750 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
752 GNUNET_free (peerreq);
757 * Cancel all requests associated with the peer.
760 * @param query hash code of the request
761 * @param value the 'struct GSF_PendingRequest'
762 * @return GNUNET_YES (continue to iterate)
765 cancel_pending_request (void *cls, const struct GNUNET_HashCode * query, void *value)
767 struct PeerRequest *peerreq = value;
768 struct GSF_PendingRequest *pr = peerreq->pr;
769 struct GSF_PendingRequestData *prd;
771 prd = GSF_pending_request_get_data_ (pr);
772 GSF_pending_request_cancel_ (pr, GNUNET_NO);
773 free_pending_request (peerreq, &prd->query);
779 * Free the given request.
781 * @param cls the request to free
782 * @param tc task context
785 peer_request_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
787 struct PeerRequest *peerreq = cls;
788 struct GSF_PendingRequest *pr = peerreq->pr;
789 struct GSF_PendingRequestData *prd;
791 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
792 prd = GSF_pending_request_get_data_ (pr);
793 cancel_pending_request (NULL, &prd->query, peerreq);
798 * The artificial delay is over, transmit the message now.
800 * @param cls the 'struct GSF_DelayedHandle' with the message
801 * @param tc scheduler context
804 transmit_delayed_now (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
806 struct GSF_DelayedHandle *dh = cls;
807 struct GSF_ConnectedPeer *cp = dh->cp;
809 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
810 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
812 GNUNET_free (dh->pm);
816 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT,
817 dh->msize, ©_reply, dh->pm);
823 * Get the randomized delay a response should be subjected to.
825 * @return desired delay
827 static struct GNUNET_TIME_Relative
828 get_randomized_delay ()
830 struct GNUNET_TIME_Relative ret;
833 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
834 GNUNET_CRYPTO_random_u32
835 (GNUNET_CRYPTO_QUALITY_WEAK,
836 2 * GSF_avg_latency.rel_value + 1));
837 #if INSANE_STATISTICS
838 GNUNET_STATISTICS_update (GSF_stats,
840 ("# artificial delays introduced (ms)"),
841 ret.rel_value, GNUNET_NO);
848 * Handle a reply to a pending request. Also called if a request
849 * expires (then with data == NULL). The handler may be called
850 * many times (depending on the request type), but will not be
851 * called during or after a call to GSF_pending_request_cancel
852 * and will also not be called anymore after a call signalling
855 * @param cls 'struct PeerRequest' this is an answer for
856 * @param eval evaluation of the result
857 * @param pr handle to the original pending request
858 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
859 * @param expiration when does 'data' expire?
860 * @param last_transmission when did we last transmit a request for this block
861 * @param type type of the block
862 * @param data response data, NULL on request expiration
863 * @param data_len number of bytes in data
866 handle_p2p_reply (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
867 struct GSF_PendingRequest *pr, uint32_t reply_anonymity_level,
868 struct GNUNET_TIME_Absolute expiration,
869 struct GNUNET_TIME_Absolute last_transmission,
870 enum GNUNET_BLOCK_Type type, const void *data,
873 struct PeerRequest *peerreq = cls;
874 struct GSF_ConnectedPeer *cp = peerreq->cp;
875 struct GSF_PendingRequestData *prd;
876 struct PutMessage *pm;
879 GNUNET_assert (data_len + sizeof (struct PutMessage) <
880 GNUNET_SERVER_MAX_MESSAGE_SIZE);
881 GNUNET_assert (peerreq->pr == pr);
882 prd = GSF_pending_request_get_data_ (pr);
885 free_pending_request (peerreq, &prd->query);
888 GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
889 if ((prd->type != type) && (GNUNET_BLOCK_TYPE_ANY != prd->type))
891 GNUNET_STATISTICS_update (GSF_stats,
893 ("# replies dropped due to type mismatch"),
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "Transmitting result for query `%s' to peer\n",
899 GNUNET_h2s (&prd->query));
900 GNUNET_STATISTICS_update (GSF_stats,
901 gettext_noop ("# replies received for other peers"),
903 msize = sizeof (struct PutMessage) + data_len;
904 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
909 if ((UINT32_MAX != reply_anonymity_level) && (reply_anonymity_level > 1))
911 if (reply_anonymity_level - 1 > GSF_cover_content_count)
913 GNUNET_STATISTICS_update (GSF_stats,
915 ("# replies dropped due to insufficient cover traffic"),
919 GSF_cover_content_count -= (reply_anonymity_level - 1);
922 pm = GNUNET_malloc (msize);
923 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
924 pm->header.size = htons (msize);
925 pm->type = htonl (type);
926 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
927 memcpy (&pm[1], data, data_len);
928 if ((UINT32_MAX != reply_anonymity_level) && (0 != reply_anonymity_level) &&
929 (GNUNET_YES == GSF_enable_randomized_delays))
931 struct GSF_DelayedHandle *dh;
933 dh = GNUNET_malloc (sizeof (struct GSF_DelayedHandle));
937 GNUNET_CONTAINER_DLL_insert (cp->delayed_head, cp->delayed_tail, dh);
939 GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
940 &transmit_delayed_now, dh);
944 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT, msize,
947 if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
949 if (GNUNET_SCHEDULER_NO_TASK == peerreq->kill_task)
951 GNUNET_STATISTICS_update (GSF_stats,
953 ("# P2P searches destroyed due to ultimate reply"),
956 GNUNET_SCHEDULER_add_now (&peer_request_destroy, peerreq);
962 * Increase the peer's respect by a value.
964 * @param cp which peer to change the respect value on
965 * @param value is the int value by which the
966 * peer's credit is to be increased or decreased
967 * @returns the actual change in respect (positive or negative)
970 change_peer_respect (struct GSF_ConnectedPeer *cp, int value)
974 GNUNET_assert (NULL != cp);
977 if (cp->ppd.respect + value < cp->ppd.respect)
979 value = UINT32_MAX - cp->ppd.respect;
980 cp->ppd.respect = UINT32_MAX;
983 cp->ppd.respect += value;
987 if (cp->ppd.respect < -value)
989 value = -cp->ppd.respect;
993 cp->ppd.respect += value;
1000 * We've received a request with the specified priority. Bound it
1001 * according to how much we respect the given peer.
1003 * @param prio_in requested priority
1004 * @param cp the peer making the request
1005 * @return effective priority
1008 bound_priority (uint32_t prio_in, struct GSF_ConnectedPeer *cp)
1010 #define N ((double)128.0)
1015 ld = GSF_test_get_load_too_high_ (0);
1016 if (GNUNET_SYSERR == ld)
1018 #if INSANE_STATISTICS
1019 GNUNET_STATISTICS_update (GSF_stats,
1021 ("# requests done for free (low load)"), 1,
1024 return 0; /* excess resources */
1026 if (prio_in > INT32_MAX)
1027 prio_in = INT32_MAX;
1028 ret = -change_peer_respect (cp, -(int) prio_in);
1031 if (ret > GSF_current_priorities + N)
1032 rret = GSF_current_priorities + N;
1035 GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
1037 if ((GNUNET_YES == ld) && (ret > 0))
1039 /* try with charging */
1040 ld = GSF_test_get_load_too_high_ (ret);
1042 if (GNUNET_YES == ld)
1044 GNUNET_STATISTICS_update (GSF_stats,
1046 ("# request dropped, priority insufficient"), 1,
1049 change_peer_respect (cp, (int) ret);
1050 return -1; /* not enough resources */
1054 GNUNET_STATISTICS_update (GSF_stats,
1056 ("# requests done for a price (normal load)"), 1,
1065 * The priority level imposes a bound on the maximum
1066 * value for the ttl that can be requested.
1068 * @param ttl_in requested ttl
1069 * @param prio given priority
1070 * @return ttl_in if ttl_in is below the limit,
1071 * otherwise the ttl-limit for the given priority
1074 bound_ttl (int32_t ttl_in, uint32_t prio)
1076 unsigned long long allowed;
1080 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1081 if (ttl_in > allowed)
1083 if (allowed >= (1 << 30))
1092 * Handle P2P "QUERY" message. Creates the pending request entry
1093 * and sets up all of the data structures to that we will
1094 * process replies properly. Does not initiate forwarding or
1095 * local database lookups.
1097 * @param other the other peer involved (sender or receiver, NULL
1098 * for loopback messages where we are both sender and receiver)
1099 * @param message the actual message
1100 * @return pending request handle, NULL on error
1102 struct GSF_PendingRequest *
1103 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1104 const struct GNUNET_MessageHeader *message)
1106 struct PeerRequest *peerreq;
1107 struct GSF_PendingRequest *pr;
1108 struct GSF_PendingRequestData *prd;
1109 struct GSF_ConnectedPeer *cp;
1110 struct GSF_ConnectedPeer *cps;
1111 const struct GNUNET_PeerIdentity *target;
1112 enum GSF_PendingRequestOptions options;
1114 const struct GetMessage *gm;
1116 const struct GNUNET_HashCode *opt;
1119 uint32_t ttl_decrement;
1122 enum GNUNET_BLOCK_Type type;
1123 GNUNET_PEER_Id spid;
1125 GNUNET_assert (other != NULL);
1126 msize = ntohs (message->size);
1127 if (msize < sizeof (struct GetMessage))
1129 GNUNET_break_op (0);
1132 GNUNET_STATISTICS_update (GSF_stats,
1134 ("# GET requests received (from other peers)"), 1,
1136 gm = (const struct GetMessage *) message;
1137 type = ntohl (gm->type);
1138 bm = ntohl (gm->hash_bitmap);
1146 if (msize < sizeof (struct GetMessage) + bits * sizeof (struct GNUNET_HashCode))
1148 GNUNET_break_op (0);
1151 opt = (const struct GNUNET_HashCode *) &gm[1];
1152 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (struct GNUNET_HashCode);
1153 /* bfsize must be power of 2, check! */
1154 if (0 != ((bfsize - 1) & bfsize))
1156 GNUNET_break_op (0);
1159 GSF_cover_query_count++;
1160 bm = ntohl (gm->hash_bitmap);
1162 cps = GSF_peer_get_ (other);
1165 /* peer must have just disconnected */
1166 GNUNET_STATISTICS_update (GSF_stats,
1168 ("# requests dropped due to initiator not being connected"),
1172 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1173 cp = GSF_peer_get_ ((const struct GNUNET_PeerIdentity *) &opt[bits++]);
1178 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1181 GNUNET_i2s ((const struct GNUNET_PeerIdentity *)
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "Failed to find peer `%4s' in connection set. Dropping query.\n",
1187 GNUNET_i2s (other));
1188 #if INSANE_STATISTICS
1189 GNUNET_STATISTICS_update (GSF_stats,
1191 ("# requests dropped due to missing reverse route"),
1196 /* note that we can really only check load here since otherwise
1197 * peers could find out that we are overloaded by not being
1198 * disconnected after sending us a malformed query... */
1199 priority = bound_priority (ntohl (gm->priority), cps);
1202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1203 "Dropping query from `%s', this peer is too busy.\n",
1204 GNUNET_i2s (other));
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1209 GNUNET_h2s (&gm->query), (unsigned int) type, GNUNET_i2s (other),
1213 (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity
1214 *) &opt[bits++]) : NULL;
1215 options = GSF_PRO_DEFAULTS;
1217 if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority))
1218 || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1219 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 +
1220 GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
1222 /* don't have BW to send to peer, or would likely take longer than we have for it,
1223 * so at best indirect the query */
1225 options |= GSF_PRO_FORWARD_ONLY;
1226 spid = GNUNET_PEER_intern (other);
1227 GNUNET_assert (0 != spid);
1229 ttl = bound_ttl (ntohl (gm->ttl), priority);
1230 /* decrement ttl (always) */
1232 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1234 if ((ttl < 0) && (((int32_t) (ttl - ttl_decrement)) > 0))
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1238 GNUNET_i2s (other), ttl, ttl_decrement);
1239 GNUNET_STATISTICS_update (GSF_stats,
1241 ("# requests dropped due TTL underflow"), 1,
1243 /* integer underflow => drop (should be very rare)! */
1246 ttl -= ttl_decrement;
1248 /* test if the request already exists */
1249 peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map, &gm->query);
1250 if (peerreq != NULL)
1253 prd = GSF_pending_request_get_data_ (pr);
1254 if (prd->type == type)
1256 if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get ().abs_value + ttl)
1258 /* existing request has higher TTL, drop new one! */
1259 prd->priority += priority;
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1261 "Have existing request with higher TTL, dropping new request.\n",
1262 GNUNET_i2s (other));
1263 GNUNET_STATISTICS_update (GSF_stats,
1265 ("# requests dropped due to higher-TTL request"),
1269 /* existing request has lower TTL, drop old one! */
1270 priority += prd->priority;
1271 GSF_pending_request_cancel_ (pr, GNUNET_YES);
1272 free_pending_request (peerreq, &gm->query);
1276 peerreq = GNUNET_malloc (sizeof (struct PeerRequest));
1278 pr = GSF_pending_request_create_ (options, type, &gm->query,
1281 0) ? (const char *) &opt[bits] : NULL,
1282 bfsize, ntohl (gm->filter_mutator),
1284 (uint32_t) priority, ttl, spid, GNUNET_PEER_intern (other), NULL, 0, /* replies_seen */
1285 &handle_p2p_reply, peerreq);
1286 GNUNET_assert (NULL != pr);
1288 GNUNET_break (GNUNET_OK ==
1289 GNUNET_CONTAINER_multihashmap_put (cp->request_map, &gm->query,
1291 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1292 GNUNET_STATISTICS_update (GSF_stats,
1294 ("# P2P query messages received and processed"), 1,
1296 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
1303 * Function called if there has been a timeout trying to satisfy
1304 * a transmission request.
1306 * @param cls the 'struct GSF_PeerTransmitHandle' of the request
1307 * @param tc scheduler context
1310 peer_transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1312 struct GSF_PeerTransmitHandle *pth = cls;
1313 struct GSF_ConnectedPeer *cp;
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1316 "Timeout trying to transmit to other peer\n");
1317 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1319 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1320 if (GNUNET_YES == pth->is_query)
1321 GNUNET_assert (0 < cp->ppd.pending_queries--);
1322 else if (GNUNET_NO == pth->is_query)
1323 GNUNET_assert (0 < cp->ppd.pending_replies--);
1324 GNUNET_LOAD_update (cp->ppd.transmission_delay, UINT64_MAX);
1325 if (NULL != cp->cth)
1327 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1330 pth->gmc (pth->gmc_cls, 0, NULL);
1331 GNUNET_assert (0 == cp->cth_in_progress);
1337 * Transmit a message to the given peer as soon as possible.
1338 * If the peer disconnects before the transmission can happen,
1339 * the callback is invoked with a 'NULL' buffer.
1341 * @param cp target peer
1342 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
1343 * @param priority how important is this request?
1344 * @param timeout when does this request timeout (call gmc with error)
1345 * @param size number of bytes we would like to send to the peer
1346 * @param gmc function to call to get the message
1347 * @param gmc_cls closure for gmc
1348 * @return handle to cancel request
1350 struct GSF_PeerTransmitHandle *
1351 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp, int is_query,
1352 uint32_t priority, struct GNUNET_TIME_Relative timeout,
1353 size_t size, GSF_GetMessageCallback gmc, void *gmc_cls)
1355 struct GSF_PeerTransmitHandle *pth;
1356 struct GSF_PeerTransmitHandle *pos;
1357 struct GSF_PeerTransmitHandle *prev;
1359 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
1360 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1361 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1363 pth->gmc_cls = gmc_cls;
1365 pth->is_query = is_query;
1366 pth->priority = priority;
1368 /* insertion sort (by priority, descending) */
1371 while ((NULL != pos) && (pos->priority > priority))
1376 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head, cp->pth_tail, prev, pth);
1377 if (GNUNET_YES == is_query)
1378 cp->ppd.pending_queries++;
1379 else if (GNUNET_NO == is_query)
1380 cp->ppd.pending_replies++;
1382 GNUNET_SCHEDULER_add_delayed (timeout, &peer_transmit_timeout, pth);
1383 schedule_transmission (pth);
1389 * Cancel an earlier request for transmission.
1391 * @param pth request to cancel
1394 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1396 struct GSF_ConnectedPeer *cp;
1398 if (GNUNET_SCHEDULER_NO_TASK != pth->timeout_task)
1400 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1401 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1404 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1405 if (GNUNET_YES == pth->is_query)
1406 GNUNET_assert (0 < cp->ppd.pending_queries--);
1407 else if (GNUNET_NO == pth->is_query)
1408 GNUNET_assert (0 < cp->ppd.pending_replies--);
1414 * Report on receiving a reply; update the performance record of the given peer.
1416 * @param cp responding peer (will be updated)
1417 * @param request_time time at which the original query was transmitted
1418 * @param request_priority priority of the original request
1421 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1422 struct GNUNET_TIME_Absolute request_time,
1423 uint32_t request_priority)
1425 struct GNUNET_TIME_Relative delay;
1427 delay = GNUNET_TIME_absolute_get_duration (request_time);
1428 cp->ppd.avg_reply_delay.rel_value =
1429 (cp->ppd.avg_reply_delay.rel_value * (RUNAVG_DELAY_N - 1) +
1430 delay.rel_value) / RUNAVG_DELAY_N;
1431 cp->ppd.avg_priority =
1432 (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
1433 request_priority) / RUNAVG_DELAY_N;
1438 * Report on receiving a reply in response to an initiating client.
1439 * Remember that this peer is good for this client.
1441 * @param cp responding peer (will be updated)
1442 * @param initiator_client local client on responsible for query
1445 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1446 struct GSF_LocalClient *initiator_client)
1448 cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
1449 CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1454 * Report on receiving a reply in response to an initiating peer.
1455 * Remember that this peer is good for this initiating peer.
1457 * @param cp responding peer (will be updated)
1458 * @param initiator_peer other peer responsible for query
1461 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1462 const struct GSF_ConnectedPeer *initiator_peer)
1466 woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
1467 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
1468 cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
1469 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1470 cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
1475 * A peer disconnected from us. Tear down the connected peer
1479 * @param peer identity of peer that connected
1482 GSF_peer_disconnect_handler_ (void *cls, const struct GNUNET_PeerIdentity *peer)
1484 struct GSF_ConnectedPeer *cp;
1485 struct GSF_PeerTransmitHandle *pth;
1486 struct GSF_DelayedHandle *dh;
1488 cp = GSF_peer_get_ (peer);
1490 return; /* must have been disconnect from core with
1491 * 'peer' == my_id, ignore */
1492 GNUNET_assert (GNUNET_YES ==
1493 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1494 &peer->hashPubKey, cp));
1495 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
1496 GNUNET_CONTAINER_multihashmap_size (cp_map),
1498 if (NULL != cp->migration_pth)
1500 GSF_peer_transmit_cancel_ (cp->migration_pth);
1501 cp->migration_pth = NULL;
1505 GNUNET_ATS_reserve_bandwidth_cancel (cp->rc);
1508 if (GNUNET_SCHEDULER_NO_TASK != cp->rc_delay_task)
1510 GNUNET_SCHEDULER_cancel (cp->rc_delay_task);
1511 cp->rc_delay_task = GNUNET_SCHEDULER_NO_TASK;
1513 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1514 &cancel_pending_request, cp);
1515 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1516 cp->request_map = NULL;
1517 GSF_plan_notify_peer_disconnect_ (cp);
1518 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1519 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1520 memset (cp->ppd.last_p2p_replies, 0, sizeof (cp->ppd.last_p2p_replies));
1521 GSF_push_stop_ (cp);
1522 if (NULL != cp->cth)
1524 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1527 GNUNET_assert (0 == cp->cth_in_progress);
1528 while (NULL != (pth = cp->pth_head))
1530 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1532 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1533 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1535 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1536 pth->gmc (pth->gmc_cls, 0, NULL);
1539 while (NULL != (dh = cp->delayed_head))
1541 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
1542 GNUNET_SCHEDULER_cancel (dh->delay_task);
1543 GNUNET_free (dh->pm);
1546 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1547 if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1549 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1550 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1557 * Closure for 'call_iterator'.
1559 struct IterationContext
1562 * Function to call on each entry.
1564 GSF_ConnectedPeerIterator it;
1574 * Function that calls the callback for each peer.
1576 * @param cls the 'struct IterationContext*'
1577 * @param key identity of the peer
1578 * @param value the 'struct GSF_ConnectedPeer*'
1579 * @return GNUNET_YES to continue iteration
1582 call_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
1584 struct IterationContext *ic = cls;
1585 struct GSF_ConnectedPeer *cp = value;
1587 ic->it (ic->it_cls, (const struct GNUNET_PeerIdentity *) key, cp, &cp->ppd);
1593 * Iterate over all connected peers.
1595 * @param it function to call for each peer
1596 * @param it_cls closure for it
1599 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls)
1601 struct IterationContext ic;
1605 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &call_iterator, &ic);
1610 * Obtain the identity of a connected peer.
1612 * @param cp peer to get identity of
1613 * @param id identity to set (written to)
1616 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1617 struct GNUNET_PeerIdentity *id)
1619 GNUNET_assert (0 != cp->ppd.pid);
1620 GNUNET_PEER_resolve (cp->ppd.pid, id);
1625 * Obtain the identity of a connected peer.
1627 * @param cp peer to get identity of
1628 * @return reference to peer identity, valid until peer disconnects (!)
1630 const struct GNUNET_PeerIdentity *
1631 GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp)
1633 GNUNET_assert (0 != cp->ppd.pid);
1634 return GNUNET_PEER_resolve2 (cp->ppd.pid);
1639 * Assemble a migration stop message for transmission.
1641 * @param cls the 'struct GSF_ConnectedPeer' to use
1642 * @param size number of bytes we're allowed to write to buf
1643 * @param buf where to copy the message
1644 * @return number of bytes copied to buf
1647 create_migration_stop_message (void *cls, size_t size, void *buf)
1649 struct GSF_ConnectedPeer *cp = cls;
1650 struct MigrationStopMessage msm;
1652 cp->migration_pth = NULL;
1655 GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1656 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1657 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1658 msm.reserved = htonl (0);
1660 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1661 (cp->last_migration_block));
1662 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1663 GNUNET_STATISTICS_update (GSF_stats,
1664 gettext_noop ("# migration stop messages sent"),
1666 return sizeof (struct MigrationStopMessage);
1671 * Ask a peer to stop migrating data to us until the given point
1674 * @param cp peer to ask
1675 * @param block_time until when to block
1678 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1679 struct GNUNET_TIME_Absolute block_time)
1681 if (cp->last_migration_block.abs_value > block_time.abs_value)
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1684 "Migration already blocked for another %s\n",
1685 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining
1686 (cp->last_migration_block), GNUNET_YES));
1687 return; /* already blocked */
1689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %llu ms\n",
1690 (unsigned long long) GNUNET_TIME_absolute_get_remaining (block_time).rel_value);
1691 cp->last_migration_block = block_time;
1692 if (NULL != cp->migration_pth)
1693 GSF_peer_transmit_cancel_ (cp->migration_pth);
1695 GSF_peer_transmit_ (cp, GNUNET_SYSERR, UINT32_MAX,
1696 GNUNET_TIME_UNIT_FOREVER_REL,
1697 sizeof (struct MigrationStopMessage),
1698 &create_migration_stop_message, cp);
1703 * Write peer-respect information to a file - flush the buffer entry!
1706 * @param key peer identity
1707 * @param value the 'struct GSF_ConnectedPeer' to flush
1708 * @return GNUNET_OK to continue iteration
1711 flush_respect (void *cls, const struct GNUNET_HashCode * key, void *value)
1713 struct GSF_ConnectedPeer *cp = value;
1716 struct GNUNET_PeerIdentity pid;
1718 if (cp->ppd.respect == cp->disk_respect)
1719 return GNUNET_OK; /* unchanged */
1720 GNUNET_assert (0 != cp->ppd.pid);
1721 GNUNET_PEER_resolve (cp->ppd.pid, &pid);
1722 fn = get_respect_filename (&pid);
1723 if (cp->ppd.respect == 0)
1725 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1726 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1727 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1731 respect = htonl (cp->ppd.respect);
1732 if (sizeof (uint32_t) ==
1733 GNUNET_DISK_fn_write (fn, &respect, sizeof (uint32_t),
1734 GNUNET_DISK_PERM_USER_READ |
1735 GNUNET_DISK_PERM_USER_WRITE |
1736 GNUNET_DISK_PERM_GROUP_READ |
1737 GNUNET_DISK_PERM_OTHER_READ))
1738 cp->disk_respect = cp->ppd.respect;
1746 * Notify core about a preference we have for the given peer
1747 * (to allocate more resources towards it). The change will
1748 * be communicated the next time we reserve bandwidth with
1749 * core (not instantly).
1751 * @param cp peer to reserve bandwidth from
1752 * @param pref preference change
1755 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1758 cp->inc_preference += pref;
1763 * Call this method periodically to flush respect information to disk.
1765 * @param cls closure, not used
1766 * @param tc task context, not used
1769 cron_flush_respect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1774 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &flush_respect, NULL);
1777 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1779 GNUNET_SCHEDULER_add_delayed_with_priority (RESPECT_FLUSH_FREQ,
1780 GNUNET_SCHEDULER_PRIORITY_HIGH,
1781 &cron_flush_respect, NULL);
1786 * Initialize peer management subsystem.
1789 GSF_connected_peer_init_ ()
1791 cp_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
1792 GNUNET_assert (GNUNET_OK ==
1793 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg, "fs",
1795 &respectDirectory));
1796 GNUNET_DISK_directory_create (respectDirectory);
1797 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1798 &cron_flush_respect, NULL);
1803 * Iterator to free peer entries.
1805 * @param cls closure, unused
1806 * @param key current key code
1807 * @param value value in the hash map (peer entry)
1808 * @return GNUNET_YES (we should continue to iterate)
1811 clean_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
1813 GSF_peer_disconnect_handler_ (NULL, (const struct GNUNET_PeerIdentity *) key);
1819 * Shutdown peer management subsystem.
1822 GSF_connected_peer_done_ ()
1824 cron_flush_respect (NULL, NULL);
1825 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_peer, NULL);
1826 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1828 GNUNET_free (respectDirectory);
1829 respectDirectory = NULL;
1834 * Iterator to remove references to LC entry.
1836 * @param cls the 'struct GSF_LocalClient*' to look for
1837 * @param key current key code
1838 * @param value value in the hash map (peer entry)
1839 * @return GNUNET_YES (we should continue to iterate)
1842 clean_local_client (void *cls, const struct GNUNET_HashCode * key, void *value)
1844 const struct GSF_LocalClient *lc = cls;
1845 struct GSF_ConnectedPeer *cp = value;
1848 for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
1849 if (cp->ppd.last_client_replies[i] == lc)
1850 cp->ppd.last_client_replies[i] = NULL;
1856 * Notification that a local client disconnected. Clean up all of our
1857 * references to the given handle.
1859 * @param lc handle to the local client (henceforth invalid)
1862 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1865 return; /* already cleaned up */
1866 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_local_client,
1871 /* end of gnunet-service-fs_cp.c */