2 This file is part of GNUnet.
3 Copyright (C) 2011, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file fs/gnunet-service-fs_cp.c
22 * @brief API to handle 'connected peers'
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
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"
33 #include "gnunet_peerstore_service.h"
37 * Ratio for moving average delay calculation. The previous
38 * average goes in with a factor of (n-1) into the calculation.
41 #define RUNAVG_DELAY_N 16
44 * How often do we flush respect values to disk?
46 #define RESPECT_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
49 * After how long do we discard a reply?
51 #define REPLY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
54 * Collect an instane number of statistics? May cause excessive IPC.
56 #define INSANE_STATISTICS GNUNET_NO
60 * Handle to cancel a transmission request.
62 struct GSF_PeerTransmitHandle
66 * Kept in a doubly-linked list.
68 struct GSF_PeerTransmitHandle *next;
71 * Kept in a doubly-linked list.
73 struct GSF_PeerTransmitHandle *prev;
76 * Time when this transmission request was issued.
78 struct GNUNET_TIME_Absolute transmission_request_start_time;
81 * Envelope with the actual message.
83 struct GNUNET_MQ_Envelope *env;
86 * Peer this request targets.
88 struct GSF_ConnectedPeer *cp;
91 * #GNUNET_YES if this is a query, #GNUNET_NO for content.
96 * Did we get a reservation already?
101 * Priority of this request.
109 * Handle for an entry in our delay list.
111 struct GSF_DelayedHandle
115 * Kept in a doubly-linked list.
117 struct GSF_DelayedHandle *next;
120 * Kept in a doubly-linked list.
122 struct GSF_DelayedHandle *prev;
125 * Peer this transmission belongs to.
127 struct GSF_ConnectedPeer *cp;
130 * Envelope of the message that was delayed.
132 struct GNUNET_MQ_Envelope *env;
135 * Task for the delay.
137 struct GNUNET_SCHEDULER_Task *delay_task;
140 * Size of the message.
148 * Information per peer and request.
154 * Handle to generic request (generic: from peer or local client).
156 struct GSF_PendingRequest *pr;
159 * Which specific peer issued this request?
161 struct GSF_ConnectedPeer *cp;
164 * Task for asynchronous stopping of this request.
166 struct GNUNET_SCHEDULER_Task *kill_task;
174 struct GSF_ConnectedPeer
178 * Performance data for this peer.
180 struct GSF_PeerPerformanceData ppd;
183 * Time until when we blocked this peer from migrating
186 struct GNUNET_TIME_Absolute last_migration_block;
189 * Task scheduled to revive migration to this peer.
191 struct GNUNET_SCHEDULER_Task *mig_revive_task;
194 * Messages (replies, queries, content migration) we would like to
195 * send to this peer in the near future. Sorted by priority, head.
197 struct GSF_PeerTransmitHandle *pth_head;
200 * Messages (replies, queries, content migration) we would like to
201 * send to this peer in the near future. Sorted by priority, tail.
203 struct GSF_PeerTransmitHandle *pth_tail;
206 * Messages (replies, queries, content migration) we would like to
207 * send to this peer in the near future. Sorted by priority, head.
209 struct GSF_DelayedHandle *delayed_head;
212 * Messages (replies, queries, content migration) we would like to
213 * send to this peer in the near future. Sorted by priority, tail.
215 struct GSF_DelayedHandle *delayed_tail;
218 * Context of our GNUNET_ATS_reserve_bandwidth call (or NULL).
220 struct GNUNET_ATS_ReservationContext *rc;
223 * Task scheduled if we need to retry bandwidth reservation later.
225 struct GNUNET_SCHEDULER_Task *rc_delay_task;
228 * Active requests from this neighbour, map of query to `struct PeerRequest`.
230 struct GNUNET_CONTAINER_MultiHashMap *request_map;
233 * Handle for an active request for transmission to this
236 struct GNUNET_MQ_Handle *mq;
239 * Increase in traffic preference still to be submitted
240 * to the core service for this peer.
242 uint64_t inc_preference;
245 * Number of entries in @e delayed_head DLL.
247 unsigned int delay_queue_size;
250 * Respect rating for this peer on disk.
252 uint32_t disk_respect;
255 * Which offset in @e last_p2p_replies will be updated next?
256 * (we go round-robin).
258 unsigned int last_p2p_replies_woff;
261 * Which offset in @e last_client_replies will be updated next?
262 * (we go round-robin).
264 unsigned int last_client_replies_woff;
267 * Current offset into @e last_request_times ring buffer.
269 unsigned int last_request_times_off;
272 * #GNUNET_YES if we did successfully reserve 32k bandwidth,
278 * Handle to the PEERSTORE iterate request for peer respect value
280 struct GNUNET_PEERSTORE_IterateContext *respect_iterate_req;
286 * Map from peer identities to `struct GSF_ConnectPeer` entries.
288 static struct GNUNET_CONTAINER_MultiPeerMap *cp_map;
291 * Handle to peerstore service.
293 static struct GNUNET_PEERSTORE_Handle *peerstore;
296 * Task used to flush respect values to disk.
298 static struct GNUNET_SCHEDULER_Task *fr_task;
302 * Update the latency information kept for the given peer.
304 * @param id peer record to update
305 * @param latency current latency value
308 GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
309 struct GNUNET_TIME_Relative latency)
311 struct GSF_ConnectedPeer *cp;
313 cp = GSF_peer_get_ (id);
315 return; /* we're not yet connected at the core level, ignore */
316 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay,
322 * Return the performance data record for the given peer
324 * @param cp peer to query
325 * @return performance data record for the peer
327 struct GSF_PeerPerformanceData *
328 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
335 * Core is ready to transmit to a peer, get the message.
337 * @param cp which peer to send a message to
340 peer_transmit (struct GSF_ConnectedPeer *cp);
344 * Function called by core upon success or failure of our bandwidth reservation request.
346 * @param cls the `struct GSF_ConnectedPeer` of the peer for which we made the request
347 * @param peer identifies the peer
348 * @param amount set to the amount that was actually reserved or unreserved;
349 * either the full requested amount or zero (no partial reservations)
350 * @param res_delay if the reservation could not be satisfied (amount was 0), how
351 * long should the client wait until re-trying?
354 ats_reserve_callback (void *cls,
355 const struct GNUNET_PeerIdentity *peer,
357 struct GNUNET_TIME_Relative res_delay);
361 * If ready (bandwidth reserved), try to schedule transmission via
362 * core for the given handle.
364 * @param pth transmission handle to schedule
367 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
369 struct GSF_ConnectedPeer *cp;
370 struct GNUNET_PeerIdentity target;
373 GNUNET_assert (0 != cp->ppd.pid);
374 GNUNET_PEER_resolve (cp->ppd.pid, &target);
376 if (0 != cp->inc_preference)
378 GNUNET_ATS_performance_change_preference (GSF_ats,
380 GNUNET_ATS_PREFERENCE_BANDWIDTH,
381 (double) cp->inc_preference,
382 GNUNET_ATS_PREFERENCE_END);
383 cp->inc_preference = 0;
386 if ( (GNUNET_YES == pth->is_query) &&
387 (GNUNET_YES != pth->was_reserved) )
389 /* query, need reservation */
390 if (GNUNET_YES != cp->did_reserve)
391 return; /* not ready */
392 cp->did_reserve = GNUNET_NO;
393 /* reservation already done! */
394 pth->was_reserved = GNUNET_YES;
395 cp->rc = GNUNET_ATS_reserve_bandwidth (GSF_ats,
398 &ats_reserve_callback,
407 * Core is ready to transmit to a peer, get the message.
409 * @param cp which peer to send a message to
412 peer_transmit (struct GSF_ConnectedPeer *cp)
414 struct GSF_PeerTransmitHandle *pth = cp->pth_head;
415 struct GSF_PeerTransmitHandle *pos;
419 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
422 if (GNUNET_YES == pth->is_query)
424 cp->ppd.last_request_times[(cp->last_request_times_off++) %
425 MAX_QUEUE_PER_PEER] =
426 GNUNET_TIME_absolute_get ();
427 GNUNET_assert (0 < cp->ppd.pending_queries--);
429 else if (GNUNET_NO == pth->is_query)
431 GNUNET_assert (0 < cp->ppd.pending_replies--);
433 GNUNET_LOAD_update (cp->ppd.transmission_delay,
434 GNUNET_TIME_absolute_get_duration
435 (pth->transmission_request_start_time).rel_value_us);
436 GNUNET_MQ_send (cp->mq,
439 if (NULL != (pos = cp->pth_head))
441 GNUNET_assert (pos != pth);
442 schedule_transmission (pos);
448 * (re)try to reserve bandwidth from the given peer.
450 * @param cls the `struct GSF_ConnectedPeer` to reserve from
453 retry_reservation (void *cls)
455 struct GSF_ConnectedPeer *cp = cls;
456 struct GNUNET_PeerIdentity target;
458 GNUNET_PEER_resolve (cp->ppd.pid, &target);
459 cp->rc_delay_task = NULL;
461 GNUNET_ATS_reserve_bandwidth (GSF_ats,
464 &ats_reserve_callback, cp);
469 * Function called by core upon success or failure of our bandwidth reservation request.
471 * @param cls the `struct GSF_ConnectedPeer` of the peer for which we made the request
472 * @param peer identifies the peer
473 * @param amount set to the amount that was actually reserved or unreserved;
474 * either the full requested amount or zero (no partial reservations)
475 * @param res_delay if the reservation could not be satisfied (amount was 0), how
476 * long should the client wait until re-trying?
479 ats_reserve_callback (void *cls,
480 const struct GNUNET_PeerIdentity *peer,
482 struct GNUNET_TIME_Relative res_delay)
484 struct GSF_ConnectedPeer *cp = cls;
485 struct GSF_PeerTransmitHandle *pth;
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488 "Reserved %d bytes / need to wait %s for reservation\n",
490 GNUNET_STRINGS_relative_time_to_string (res_delay, GNUNET_YES));
495 GNUNET_SCHEDULER_add_delayed (res_delay,
500 cp->did_reserve = GNUNET_YES;
504 /* reservation success, try transmission now! */
511 * Function called by PEERSTORE with peer respect record
513 * @param cls handle to connected peer entry
514 * @param record peerstore record information
515 * @param emsg error message, or NULL if no errors
518 peer_respect_cb (void *cls,
519 const struct GNUNET_PEERSTORE_Record *record,
522 struct GSF_ConnectedPeer *cp = cls;
524 GNUNET_assert (NULL != cp->respect_iterate_req);
525 if ( (NULL != record) &&
526 (sizeof (cp->disk_respect) == record->value_size))
528 cp->disk_respect = *((uint32_t *)record->value);
529 cp->ppd.respect += *((uint32_t *)record->value);
531 GSF_push_start_ (cp);
533 GNUNET_PEERSTORE_iterate_cancel (cp->respect_iterate_req);
534 cp->respect_iterate_req = NULL;
539 * Function called for each pending request whenever a new
540 * peer connects, giving us a chance to decide about submitting
541 * the existing request to the new peer.
543 * @param cls the `struct GSF_ConnectedPeer` of the new peer
544 * @param key query for the request
545 * @param pr handle to the pending request
546 * @return #GNUNET_YES to continue to iterate
549 consider_peer_for_forwarding (void *cls,
550 const struct GNUNET_HashCode *key,
551 struct GSF_PendingRequest *pr)
553 struct GSF_ConnectedPeer *cp = cls;
554 struct GNUNET_PeerIdentity pid;
557 GSF_pending_request_test_active_ (pr))
558 return GNUNET_YES; /* request is not actually active, skip! */
559 GSF_connected_peer_get_identity_ (cp, &pid);
561 GSF_pending_request_test_target_ (pr, &pid))
563 GNUNET_STATISTICS_update (GSF_stats,
564 gettext_noop ("# Loopback routes suppressed"),
569 GSF_plan_add_ (cp, pr);
575 * A peer connected to us. Setup the connected peer
579 * @param peer identity of peer that connected
580 * @param mq message queue for talking to @a peer
581 * @return our internal handle for the peer
584 GSF_peer_connect_handler (void *cls,
585 const struct GNUNET_PeerIdentity *peer,
586 struct GNUNET_MQ_Handle *mq)
588 struct GSF_ConnectedPeer *cp;
591 GNUNET_CRYPTO_cmp_peer_identity (&GSF_my_id,
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
595 "Connected to peer %s\n",
597 cp = GNUNET_new (struct GSF_ConnectedPeer);
598 cp->ppd.pid = GNUNET_PEER_intern (peer);
601 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
603 GNUNET_ATS_reserve_bandwidth (GSF_ats,
606 &ats_reserve_callback, cp);
607 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128,
609 GNUNET_break (GNUNET_OK ==
610 GNUNET_CONTAINER_multipeermap_put (cp_map,
611 GSF_connected_peer_get_identity2_ (cp),
613 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
614 GNUNET_STATISTICS_set (GSF_stats,
615 gettext_noop ("# peers connected"),
616 GNUNET_CONTAINER_multipeermap_size (cp_map),
618 cp->respect_iterate_req
619 = GNUNET_PEERSTORE_iterate (peerstore,
625 GSF_iterate_pending_requests_ (&consider_peer_for_forwarding,
632 * It may be time to re-start migrating content to this
633 * peer. Check, and if so, restart migration.
635 * @param cls the `struct GSF_ConnectedPeer`
638 revive_migration (void *cls)
640 struct GSF_ConnectedPeer *cp = cls;
641 struct GNUNET_TIME_Relative bt;
643 cp->mig_revive_task = NULL;
644 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
645 if (0 != bt.rel_value_us)
647 /* still time left... */
648 cp->mig_revive_task =
649 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
652 GSF_push_start_ (cp);
657 * Get a handle for a connected peer.
659 * @param peer peer's identity
660 * @return NULL if the peer is not currently connected
662 struct GSF_ConnectedPeer *
663 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
667 return GNUNET_CONTAINER_multipeermap_get (cp_map, peer);
672 * Handle P2P #GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP message.
674 * @param cls closure, the `struct GSF_ConnectedPeer`
675 * @param msm the actual message
678 handle_p2p_migration_stop (void *cls,
679 const struct MigrationStopMessage *msm)
681 struct GSF_ConnectedPeer *cp = cls;
682 struct GNUNET_TIME_Relative bt;
684 GNUNET_STATISTICS_update (GSF_stats,
685 gettext_noop ("# migration stop messages received"),
687 bt = GNUNET_TIME_relative_ntoh (msm->duration);
688 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
689 _("Migration of content to peer `%s' blocked for %s\n"),
690 GNUNET_i2s (cp->ppd.peer),
691 GNUNET_STRINGS_relative_time_to_string (bt, GNUNET_YES));
692 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
693 if ( (NULL == cp->mig_revive_task) &&
694 (NULL == cp->respect_iterate_req) )
697 cp->mig_revive_task =
698 GNUNET_SCHEDULER_add_delayed (bt,
699 &revive_migration, cp);
705 * Free resources associated with the given peer request.
707 * @param peerreq request to free
710 free_pending_request (struct PeerRequest *peerreq)
712 struct GSF_ConnectedPeer *cp = peerreq->cp;
713 struct GSF_PendingRequestData *prd;
715 prd = GSF_pending_request_get_data_ (peerreq->pr);
716 if (NULL != peerreq->kill_task)
718 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
719 peerreq->kill_task = NULL;
721 GNUNET_STATISTICS_update (GSF_stats,
722 gettext_noop ("# P2P searches active"),
725 GNUNET_break (GNUNET_YES ==
726 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
729 GNUNET_free (peerreq);
734 * Cancel all requests associated with the peer.
737 * @param query hash code of the request
738 * @param value the `struct GSF_PendingRequest`
739 * @return #GNUNET_YES (continue to iterate)
742 cancel_pending_request (void *cls,
743 const struct GNUNET_HashCode *query,
746 struct PeerRequest *peerreq = value;
747 struct GSF_PendingRequest *pr = peerreq->pr;
749 free_pending_request (peerreq);
750 GSF_pending_request_cancel_ (pr,
757 * Free the given request.
759 * @param cls the request to free
762 peer_request_destroy (void *cls)
764 struct PeerRequest *peerreq = cls;
765 struct GSF_PendingRequest *pr = peerreq->pr;
766 struct GSF_PendingRequestData *prd;
768 peerreq->kill_task = NULL;
769 prd = GSF_pending_request_get_data_ (pr);
770 cancel_pending_request (NULL,
777 * The artificial delay is over, transmit the message now.
779 * @param cls the `struct GSF_DelayedHandle` with the message
782 transmit_delayed_now (void *cls)
784 struct GSF_DelayedHandle *dh = cls;
785 struct GSF_ConnectedPeer *cp = dh->cp;
787 GNUNET_CONTAINER_DLL_remove (cp->delayed_head,
790 cp->delay_queue_size--;
791 GSF_peer_transmit_ (cp,
800 * Get the randomized delay a response should be subjected to.
802 * @return desired delay
804 static struct GNUNET_TIME_Relative
805 get_randomized_delay ()
807 struct GNUNET_TIME_Relative ret;
810 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
811 GNUNET_CRYPTO_random_u32
812 (GNUNET_CRYPTO_QUALITY_WEAK,
813 2 * GSF_avg_latency.rel_value_us + 1));
814 #if INSANE_STATISTICS
815 GNUNET_STATISTICS_update (GSF_stats,
817 ("# artificial delays introduced (ms)"),
818 ret.rel_value_us / 1000LL, GNUNET_NO);
825 * Handle a reply to a pending request. Also called if a request
826 * expires (then with data == NULL). The handler may be called
827 * many times (depending on the request type), but will not be
828 * called during or after a call to GSF_pending_request_cancel
829 * and will also not be called anymore after a call signalling
832 * @param cls `struct PeerRequest` this is an answer for
833 * @param eval evaluation of the result
834 * @param pr handle to the original pending request
835 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
836 * @param expiration when does @a data expire?
837 * @param last_transmission when did we last transmit a request for this block
838 * @param type type of the block
839 * @param data response data, NULL on request expiration
840 * @param data_len number of bytes in @a data
843 handle_p2p_reply (void *cls,
844 enum GNUNET_BLOCK_EvaluationResult eval,
845 struct GSF_PendingRequest *pr,
846 uint32_t reply_anonymity_level,
847 struct GNUNET_TIME_Absolute expiration,
848 struct GNUNET_TIME_Absolute last_transmission,
849 enum GNUNET_BLOCK_Type type,
853 struct PeerRequest *peerreq = cls;
854 struct GSF_ConnectedPeer *cp = peerreq->cp;
855 struct GSF_PendingRequestData *prd;
856 struct GNUNET_MQ_Envelope *env;
857 struct PutMessage *pm;
860 GNUNET_assert (data_len + sizeof (struct PutMessage) <
861 GNUNET_MAX_MESSAGE_SIZE);
862 GNUNET_assert (peerreq->pr == pr);
863 prd = GSF_pending_request_get_data_ (pr);
866 free_pending_request (peerreq);
869 GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
870 if ((prd->type != type) && (GNUNET_BLOCK_TYPE_ANY != prd->type))
872 GNUNET_STATISTICS_update (GSF_stats,
874 ("# replies dropped due to type mismatch"),
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Transmitting result for query `%s' to peer\n",
880 GNUNET_h2s (&prd->query));
881 GNUNET_STATISTICS_update (GSF_stats,
882 gettext_noop ("# replies received for other peers"),
884 msize = sizeof (struct PutMessage) + data_len;
885 if (msize >= GNUNET_MAX_MESSAGE_SIZE)
890 if ((UINT32_MAX != reply_anonymity_level) && (reply_anonymity_level > 1))
892 if (reply_anonymity_level - 1 > GSF_cover_content_count)
894 GNUNET_STATISTICS_update (GSF_stats,
896 ("# replies dropped due to insufficient cover traffic"),
900 GSF_cover_content_count -= (reply_anonymity_level - 1);
903 env = GNUNET_MQ_msg_extra (pm,
905 GNUNET_MESSAGE_TYPE_FS_PUT);
906 pm->type = htonl (type);
907 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
908 GNUNET_memcpy (&pm[1],
911 if ( (UINT32_MAX != reply_anonymity_level) &&
912 (0 != reply_anonymity_level) &&
913 (GNUNET_YES == GSF_enable_randomized_delays) )
915 struct GSF_DelayedHandle *dh;
917 dh = GNUNET_new (struct GSF_DelayedHandle);
921 GNUNET_CONTAINER_DLL_insert (cp->delayed_head,
924 cp->delay_queue_size++;
926 GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
927 &transmit_delayed_now,
932 GSF_peer_transmit_ (cp,
937 if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
939 if (NULL == peerreq->kill_task)
941 GNUNET_STATISTICS_update (GSF_stats,
943 ("# P2P searches destroyed due to ultimate reply"),
947 GNUNET_SCHEDULER_add_now (&peer_request_destroy,
954 * Increase the peer's respect by a value.
956 * @param cp which peer to change the respect value on
957 * @param value is the int value by which the
958 * peer's credit is to be increased or decreased
959 * @returns the actual change in respect (positive or negative)
962 change_peer_respect (struct GSF_ConnectedPeer *cp, int value)
966 GNUNET_assert (NULL != cp);
969 if (cp->ppd.respect + value < cp->ppd.respect)
971 value = UINT32_MAX - cp->ppd.respect;
972 cp->ppd.respect = UINT32_MAX;
975 cp->ppd.respect += value;
979 if (cp->ppd.respect < -value)
981 value = -cp->ppd.respect;
985 cp->ppd.respect += value;
992 * We've received a request with the specified priority. Bound it
993 * according to how much we respect the given peer.
995 * @param prio_in requested priority
996 * @param cp the peer making the request
997 * @return effective priority
1000 bound_priority (uint32_t prio_in,
1001 struct GSF_ConnectedPeer *cp)
1003 #define N ((double)128.0)
1008 ld = GSF_test_get_load_too_high_ (0);
1009 if (GNUNET_SYSERR == ld)
1011 #if INSANE_STATISTICS
1012 GNUNET_STATISTICS_update (GSF_stats,
1014 ("# requests done for free (low load)"), 1,
1017 return 0; /* excess resources */
1019 if (prio_in > INT32_MAX)
1020 prio_in = INT32_MAX;
1021 ret = -change_peer_respect (cp, -(int) prio_in);
1024 if (ret > GSF_current_priorities + N)
1025 rret = GSF_current_priorities + N;
1028 GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
1030 if ((GNUNET_YES == ld) && (ret > 0))
1032 /* try with charging */
1033 ld = GSF_test_get_load_too_high_ (ret);
1035 if (GNUNET_YES == ld)
1037 GNUNET_STATISTICS_update (GSF_stats,
1039 ("# request dropped, priority insufficient"), 1,
1042 change_peer_respect (cp, (int) ret);
1043 return -1; /* not enough resources */
1047 GNUNET_STATISTICS_update (GSF_stats,
1049 ("# requests done for a price (normal load)"), 1,
1058 * The priority level imposes a bound on the maximum
1059 * value for the ttl that can be requested.
1061 * @param ttl_in requested ttl
1062 * @param prio given priority
1063 * @return @a ttl_in if @a ttl_in is below the limit,
1064 * otherwise the ttl-limit for the given @a prio
1067 bound_ttl (int32_t ttl_in,
1070 unsigned long long allowed;
1074 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1075 if (ttl_in > allowed)
1077 if (allowed >= (1 << 30))
1086 * Closure for #test_exist_cb().
1088 struct TestExistClosure
1092 * Priority of the incoming request.
1097 * Relative TTL of the incoming request.
1102 * Type of the incoming request.
1104 enum GNUNET_BLOCK_Type type;
1107 * Set to #GNUNET_YES if we are done handling the query.
1115 * Test if the query already exists. If so, merge it, otherwise
1116 * keep `finished` at #GNUNET_NO.
1118 * @param cls our `struct TestExistClosure`
1119 * @param hc the key of the query
1120 * @param value the existing `struct PeerRequest`.
1121 * @return #GNUNET_YES to continue to iterate,
1122 * #GNUNET_NO if we successfully merged
1125 test_exist_cb (void *cls,
1126 const struct GNUNET_HashCode *hc,
1129 struct TestExistClosure *tec = cls;
1130 struct PeerRequest *peerreq = value;
1131 struct GSF_PendingRequest *pr;
1132 struct GSF_PendingRequestData *prd;
1135 prd = GSF_pending_request_get_data_ (pr);
1136 if (prd->type != tec->type)
1138 if (prd->ttl.abs_value_us >=
1139 GNUNET_TIME_absolute_get ().abs_value_us + tec->ttl * 1000LL)
1141 /* existing request has higher TTL, drop new one! */
1142 prd->priority += tec->priority;
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1144 "Have existing request with higher TTL, dropping new request.\n");
1145 GNUNET_STATISTICS_update (GSF_stats,
1147 ("# requests dropped due to higher-TTL request"),
1149 tec->finished = GNUNET_YES;
1152 /* existing request has lower TTL, drop old one! */
1153 tec->priority += prd->priority;
1154 free_pending_request (peerreq);
1155 GSF_pending_request_cancel_ (pr,
1162 * Handle P2P "QUERY" message. Creates the pending request entry
1163 * and sets up all of the data structures to that we will
1164 * process replies properly. Does not initiate forwarding or
1165 * local database lookups.
1167 * @param cls the other peer involved (sender of the message)
1168 * @param gm the GET message
1171 handle_p2p_get (void *cls,
1172 const struct GetMessage *gm)
1174 struct GSF_ConnectedPeer *cps = cls;
1175 struct PeerRequest *peerreq;
1176 struct GSF_PendingRequest *pr;
1177 struct GSF_ConnectedPeer *cp;
1178 const struct GNUNET_PeerIdentity *target;
1179 enum GSF_PendingRequestOptions options;
1182 const struct GNUNET_PeerIdentity *opt;
1185 uint32_t ttl_decrement;
1186 struct TestExistClosure tec;
1187 GNUNET_PEER_Id spid;
1188 const struct GSF_PendingRequestData *prd;
1190 msize = ntohs (gm->header.size);
1191 tec.type = ntohl (gm->type);
1192 bm = ntohl (gm->hash_bitmap);
1200 opt = (const struct GNUNET_PeerIdentity *) &gm[1];
1201 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (struct GNUNET_PeerIdentity);
1202 GNUNET_STATISTICS_update (GSF_stats,
1204 ("# GET requests received (from other peers)"),
1207 GSF_cover_query_count++;
1208 bm = ntohl (gm->hash_bitmap);
1210 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1211 cp = GSF_peer_get_ (&opt[bits++]);
1216 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1218 "Failed to find RETURN-TO peer `%s' in connection set. Dropping query.\n",
1219 GNUNET_i2s (&opt[bits - 1]));
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Failed to find peer `%s' in connection set. Dropping query.\n",
1224 GNUNET_i2s (cps->ppd.peer));
1225 GNUNET_STATISTICS_update (GSF_stats,
1227 ("# requests dropped due to missing reverse route"),
1232 unsigned int queue_size = GNUNET_MQ_get_length (cp->mq);
1233 queue_size += cp->ppd.pending_replies + cp->delay_queue_size;
1234 if (queue_size > MAX_QUEUE_PER_PEER)
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "Peer `%s' has too many replies queued already. Dropping query.\n",
1238 GNUNET_i2s (cps->ppd.peer));
1239 GNUNET_STATISTICS_update (GSF_stats,
1240 gettext_noop ("# requests dropped due to full reply queue"),
1245 /* note that we can really only check load here since otherwise
1246 * peers could find out that we are overloaded by not being
1247 * disconnected after sending us a malformed query... */
1248 tec.priority = bound_priority (ntohl (gm->priority),
1250 if (tec.priority < 0)
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "Dropping query from `%s', this peer is too busy.\n",
1254 GNUNET_i2s (cps->ppd.peer));
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 "Received request for `%s' of type %u from peer `%s' with flags %u\n",
1259 GNUNET_h2s (&gm->query),
1260 (unsigned int) tec.type,
1261 GNUNET_i2s (cps->ppd.peer),
1265 (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? (&opt[bits++]) : NULL;
1266 options = GSF_PRO_DEFAULTS;
1268 if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + tec.priority))
1269 || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1270 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value_us * 2 +
1271 GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
1273 /* don't have BW to send to peer, or would likely take longer than we have for it,
1274 * so at best indirect the query */
1276 options |= GSF_PRO_FORWARD_ONLY;
1277 spid = GNUNET_PEER_intern (cps->ppd.peer);
1278 GNUNET_assert (0 != spid);
1280 tec.ttl = bound_ttl (ntohl (gm->ttl),
1282 /* decrement ttl (always) */
1284 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1286 if ( (tec.ttl < 0) &&
1287 (((int32_t) (tec.ttl - ttl_decrement)) > 0) )
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1291 GNUNET_i2s (cps->ppd.peer),
1294 GNUNET_STATISTICS_update (GSF_stats,
1296 ("# requests dropped due TTL underflow"), 1,
1298 /* integer underflow => drop (should be very rare)! */
1301 tec.ttl -= ttl_decrement;
1303 /* test if the request already exists */
1304 tec.finished = GNUNET_NO;
1305 GNUNET_CONTAINER_multihashmap_get_multiple (cp->request_map,
1309 if (GNUNET_YES == tec.finished)
1310 return; /* merged into existing request, we're done */
1312 peerreq = GNUNET_new (struct PeerRequest);
1314 pr = GSF_pending_request_create_ (options,
1319 ? (const char *) &opt[bits]
1322 ntohl (gm->filter_mutator),
1324 (uint32_t) tec.priority,
1327 GNUNET_PEER_intern (cps->ppd.peer),
1328 NULL, 0, /* replies_seen */
1331 GNUNET_assert (NULL != pr);
1332 prd = GSF_pending_request_get_data_ (pr);
1334 GNUNET_break (GNUNET_OK ==
1335 GNUNET_CONTAINER_multihashmap_put (cp->request_map,
1338 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1339 GNUNET_STATISTICS_update (GSF_stats,
1340 gettext_noop ("# P2P query messages received and processed"),
1343 GNUNET_STATISTICS_update (GSF_stats,
1344 gettext_noop ("# P2P searches active"),
1347 GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES;
1348 GSF_local_lookup_ (pr,
1349 &GSF_consider_forwarding,
1355 * Transmit a message to the given peer as soon as possible.
1356 * If the peer disconnects before the transmission can happen,
1357 * the callback is invoked with a `NULL` @a buffer.
1359 * @param cp target peer
1360 * @param is_query is this a query (#GNUNET_YES) or content (#GNUNET_NO) or neither (#GNUNET_SYSERR)
1361 * @param priority how important is this request?
1362 * @param timeout when does this request timeout
1363 * @param size number of bytes we would like to send to the peer
1364 * @param env message to send
1367 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
1370 struct GNUNET_MQ_Envelope *env)
1372 struct GSF_PeerTransmitHandle *pth;
1373 struct GSF_PeerTransmitHandle *pos;
1374 struct GSF_PeerTransmitHandle *prev;
1376 pth = GNUNET_new (struct GSF_PeerTransmitHandle);
1377 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1379 pth->is_query = is_query;
1380 pth->priority = priority;
1382 /* insertion sort (by priority, descending) */
1385 while ((NULL != pos) && (pos->priority > priority))
1390 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head,
1394 if (GNUNET_YES == is_query)
1395 cp->ppd.pending_queries++;
1396 else if (GNUNET_NO == is_query)
1397 cp->ppd.pending_replies++;
1398 schedule_transmission (pth);
1403 * Report on receiving a reply; update the performance record of the given peer.
1405 * @param cp responding peer (will be updated)
1406 * @param request_time time at which the original query was transmitted
1407 * @param request_priority priority of the original request
1410 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1411 struct GNUNET_TIME_Absolute request_time,
1412 uint32_t request_priority)
1414 struct GNUNET_TIME_Relative delay;
1416 delay = GNUNET_TIME_absolute_get_duration (request_time);
1417 cp->ppd.avg_reply_delay.rel_value_us =
1418 (cp->ppd.avg_reply_delay.rel_value_us * (RUNAVG_DELAY_N - 1) +
1419 delay.rel_value_us) / RUNAVG_DELAY_N;
1420 cp->ppd.avg_priority =
1421 (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
1422 request_priority) / RUNAVG_DELAY_N;
1427 * Report on receiving a reply in response to an initiating client.
1428 * Remember that this peer is good for this client.
1430 * @param cp responding peer (will be updated)
1431 * @param initiator_client local client on responsible for query
1434 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1435 struct GSF_LocalClient *initiator_client)
1437 cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
1438 CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1443 * Report on receiving a reply in response to an initiating peer.
1444 * Remember that this peer is good for this initiating peer.
1446 * @param cp responding peer (will be updated)
1447 * @param initiator_peer other peer responsible for query
1450 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1451 const struct GSF_ConnectedPeer *initiator_peer)
1455 woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
1456 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
1457 cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
1458 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1459 cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
1464 * Write peer-respect information to a file - flush the buffer entry!
1467 * @param key peer identity
1468 * @param value the `struct GSF_ConnectedPeer` to flush
1469 * @return #GNUNET_OK to continue iteration
1472 flush_respect (void *cls,
1473 const struct GNUNET_PeerIdentity *key,
1476 struct GSF_ConnectedPeer *cp = value;
1477 struct GNUNET_PeerIdentity pid;
1479 if (cp->ppd.respect == cp->disk_respect)
1480 return GNUNET_OK; /* unchanged */
1481 GNUNET_assert (0 != cp->ppd.pid);
1482 GNUNET_PEER_resolve (cp->ppd.pid, &pid);
1483 GNUNET_PEERSTORE_store (peerstore, "fs", &pid, "respect", &cp->ppd.respect,
1484 sizeof (cp->ppd.respect),
1485 GNUNET_TIME_UNIT_FOREVER_ABS,
1486 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
1494 * A peer disconnected from us. Tear down the connected peer
1498 * @param peer identity of peer that disconnected
1499 * @param internal_cls the corresponding `struct GSF_ConnectedPeer`
1502 GSF_peer_disconnect_handler (void *cls,
1503 const struct GNUNET_PeerIdentity *peer,
1506 struct GSF_ConnectedPeer *cp = internal_cls;
1507 struct GSF_PeerTransmitHandle *pth;
1508 struct GSF_DelayedHandle *dh;
1511 return; /* must have been disconnect from core with
1512 * 'peer' == my_id, ignore */
1513 flush_respect (NULL,
1516 GNUNET_assert (GNUNET_YES ==
1517 GNUNET_CONTAINER_multipeermap_remove (cp_map,
1520 GNUNET_STATISTICS_set (GSF_stats,
1521 gettext_noop ("# peers connected"),
1522 GNUNET_CONTAINER_multipeermap_size (cp_map),
1524 if (NULL != cp->respect_iterate_req)
1526 GNUNET_PEERSTORE_iterate_cancel (cp->respect_iterate_req);
1527 cp->respect_iterate_req = NULL;
1531 GNUNET_ATS_reserve_bandwidth_cancel (cp->rc);
1534 if (NULL != cp->rc_delay_task)
1536 GNUNET_SCHEDULER_cancel (cp->rc_delay_task);
1537 cp->rc_delay_task = NULL;
1539 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1540 &cancel_pending_request,
1542 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1543 cp->request_map = NULL;
1544 GSF_plan_notify_peer_disconnect_ (cp);
1545 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1546 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies,
1547 P2P_SUCCESS_LIST_SIZE);
1548 memset (cp->ppd.last_p2p_replies,
1550 sizeof (cp->ppd.last_p2p_replies));
1551 GSF_push_stop_ (cp);
1552 while (NULL != (pth = cp->pth_head))
1554 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1557 if (GNUNET_YES == pth->is_query)
1558 GNUNET_assert (0 < cp->ppd.pending_queries--);
1559 else if (GNUNET_NO == pth->is_query)
1560 GNUNET_assert (0 < cp->ppd.pending_replies--);
1563 while (NULL != (dh = cp->delayed_head))
1565 GNUNET_CONTAINER_DLL_remove (cp->delayed_head,
1568 GNUNET_MQ_discard (dh->env);
1569 cp->delay_queue_size--;
1570 GNUNET_SCHEDULER_cancel (dh->delay_task);
1573 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1574 if (NULL != cp->mig_revive_task)
1576 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1577 cp->mig_revive_task = NULL;
1579 GNUNET_break (0 == cp->ppd.pending_queries);
1580 GNUNET_break (0 == cp->ppd.pending_replies);
1586 * Closure for #call_iterator().
1588 struct IterationContext
1591 * Function to call on each entry.
1593 GSF_ConnectedPeerIterator it;
1596 * Closure for @e it.
1603 * Function that calls the callback for each peer.
1605 * @param cls the `struct IterationContext *`
1606 * @param key identity of the peer
1607 * @param value the `struct GSF_ConnectedPeer *`
1608 * @return #GNUNET_YES to continue iteration
1611 call_iterator (void *cls,
1612 const struct GNUNET_PeerIdentity *key,
1615 struct IterationContext *ic = cls;
1616 struct GSF_ConnectedPeer *cp = value;
1626 * Iterate over all connected peers.
1628 * @param it function to call for each peer
1629 * @param it_cls closure for @a it
1632 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it,
1635 struct IterationContext ic;
1639 GNUNET_CONTAINER_multipeermap_iterate (cp_map,
1646 * Obtain the identity of a connected peer.
1648 * @param cp peer to get identity of
1649 * @param id identity to set (written to)
1652 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1653 struct GNUNET_PeerIdentity *id)
1655 GNUNET_assert (0 != cp->ppd.pid);
1656 GNUNET_PEER_resolve (cp->ppd.pid, id);
1661 * Obtain the identity of a connected peer.
1663 * @param cp peer to get identity of
1664 * @return reference to peer identity, valid until peer disconnects (!)
1666 const struct GNUNET_PeerIdentity *
1667 GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp)
1669 GNUNET_assert (0 != cp->ppd.pid);
1670 return GNUNET_PEER_resolve2 (cp->ppd.pid);
1675 * Ask a peer to stop migrating data to us until the given point
1678 * @param cp peer to ask
1679 * @param block_time until when to block
1682 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1683 struct GNUNET_TIME_Absolute block_time)
1685 struct GNUNET_MQ_Envelope *env;
1686 struct MigrationStopMessage *msm;
1688 if (cp->last_migration_block.abs_value_us > block_time.abs_value_us)
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1691 "Migration already blocked for another %s\n",
1692 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining
1693 (cp->last_migration_block), GNUNET_YES));
1694 return; /* already blocked */
1696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %s\n",
1697 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (block_time),
1699 cp->last_migration_block = block_time;
1700 env = GNUNET_MQ_msg (msm,
1701 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1702 msm->reserved = htonl (0);
1704 = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1705 (cp->last_migration_block));
1706 GNUNET_STATISTICS_update (GSF_stats,
1707 gettext_noop ("# migration stop messages sent"),
1710 GSF_peer_transmit_ (cp,
1718 * Notify core about a preference we have for the given peer
1719 * (to allocate more resources towards it). The change will
1720 * be communicated the next time we reserve bandwidth with
1721 * core (not instantly).
1723 * @param cp peer to reserve bandwidth from
1724 * @param pref preference change
1727 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1730 cp->inc_preference += pref;
1735 * Call this method periodically to flush respect information to disk.
1737 * @param cls closure, not used
1740 cron_flush_respect (void *cls)
1743 GNUNET_CONTAINER_multipeermap_iterate (cp_map,
1746 fr_task = GNUNET_SCHEDULER_add_delayed_with_priority (RESPECT_FLUSH_FREQ,
1747 GNUNET_SCHEDULER_PRIORITY_HIGH,
1748 &cron_flush_respect, NULL);
1753 * Initialize peer management subsystem.
1756 GSF_connected_peer_init_ ()
1758 cp_map = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
1759 peerstore = GNUNET_PEERSTORE_connect (GSF_cfg);
1760 fr_task = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1761 &cron_flush_respect, NULL);
1766 * Shutdown peer management subsystem.
1769 GSF_connected_peer_done_ ()
1771 GNUNET_CONTAINER_multipeermap_iterate (cp_map,
1774 GNUNET_SCHEDULER_cancel (fr_task);
1776 GNUNET_CONTAINER_multipeermap_destroy (cp_map);
1778 GNUNET_PEERSTORE_disconnect (peerstore,
1785 * Iterator to remove references to LC entry.
1787 * @param cls the `struct GSF_LocalClient *` to look for
1788 * @param key current key code
1789 * @param value value in the hash map (peer entry)
1790 * @return #GNUNET_YES (we should continue to iterate)
1793 clean_local_client (void *cls,
1794 const struct GNUNET_PeerIdentity *key,
1797 const struct GSF_LocalClient *lc = cls;
1798 struct GSF_ConnectedPeer *cp = value;
1801 for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
1802 if (cp->ppd.last_client_replies[i] == lc)
1803 cp->ppd.last_client_replies[i] = NULL;
1809 * Notification that a local client disconnected. Clean up all of our
1810 * references to the given handle.
1812 * @param lc handle to the local client (henceforth invalid)
1815 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1818 return; /* already cleaned up */
1819 GNUNET_CONTAINER_multipeermap_iterate (cp_map,
1820 &clean_local_client,
1825 /* end of gnunet-service-fs_cp.c */