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_ats_service.h"
29 #include "gnunet-service-fs.h"
30 #include "gnunet-service-fs_cp.h"
31 #include "gnunet-service-fs_pe.h"
32 #include "gnunet-service-fs_pr.h"
33 #include "gnunet-service-fs_push.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)
55 * Handle to cancel a transmission request.
57 struct GSF_PeerTransmitHandle
61 * Kept in a doubly-linked list.
63 struct GSF_PeerTransmitHandle *next;
66 * Kept in a doubly-linked list.
68 struct GSF_PeerTransmitHandle *prev;
71 * Time when this transmission request was issued.
73 struct GNUNET_TIME_Absolute transmission_request_start_time;
76 * Timeout for this request.
78 struct GNUNET_TIME_Absolute timeout;
81 * Task called on timeout, or 0 for none.
83 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
86 * Function to call to get the actual message.
88 GSF_GetMessageCallback gmc;
91 * Peer this request targets.
93 struct GSF_ConnectedPeer *cp;
101 * Size of the message to be transmitted.
106 * GNUNET_YES if this is a query, GNUNET_NO for content.
111 * Did we get a reservation already?
116 * Priority of this request.
124 * Handle for an entry in our delay list.
126 struct GSF_DelayedHandle
130 * Kept in a doubly-linked list.
132 struct GSF_DelayedHandle *next;
135 * Kept in a doubly-linked list.
137 struct GSF_DelayedHandle *prev;
140 * Peer this transmission belongs to.
142 struct GSF_ConnectedPeer *cp;
145 * The PUT that was delayed.
147 struct PutMessage *pm;
150 * Task for the delay.
152 GNUNET_SCHEDULER_TaskIdentifier delay_task;
155 * Size of the message.
163 * Information per peer and request.
169 * Handle to generic request.
171 struct GSF_PendingRequest *pr;
174 * Handle to specific peer.
176 struct GSF_ConnectedPeer *cp;
179 * Task for asynchronous stopping of this request.
181 GNUNET_SCHEDULER_TaskIdentifier kill_task;
189 struct GSF_ConnectedPeer
193 * Performance data for this peer.
195 struct GSF_PeerPerformanceData ppd;
198 * Time until when we blocked this peer from migrating
201 struct GNUNET_TIME_Absolute last_migration_block;
204 * Task scheduled to revive migration to this peer.
206 GNUNET_SCHEDULER_TaskIdentifier mig_revive_task;
209 * Messages (replies, queries, content migration) we would like to
210 * send to this peer in the near future. Sorted by priority, head.
212 struct GSF_PeerTransmitHandle *pth_head;
215 * Messages (replies, queries, content migration) we would like to
216 * send to this peer in the near future. Sorted by priority, tail.
218 struct GSF_PeerTransmitHandle *pth_tail;
221 * Messages (replies, queries, content migration) we would like to
222 * send to this peer in the near future. Sorted by priority, head.
224 struct GSF_DelayedHandle *delayed_head;
227 * Messages (replies, queries, content migration) we would like to
228 * send to this peer in the near future. Sorted by priority, tail.
230 struct GSF_DelayedHandle *delayed_tail;
233 * Migration stop message in our queue, or NULL if we have none pending.
235 struct GSF_PeerTransmitHandle *migration_pth;
238 * Context of our GNUNET_ATS_reserve_bandwidth call (or NULL).
240 struct GNUNET_ATS_ReservationContext *rc;
243 * Task scheduled if we need to retry bandwidth reservation later.
245 GNUNET_SCHEDULER_TaskIdentifier rc_delay_task;
248 * Active requests from this neighbour, map of query to 'struct PeerRequest'.
250 struct GNUNET_CONTAINER_MultiHashMap *request_map;
253 * Handle for an active request for transmission to this
254 * peer, or NULL (if core queue was full).
256 struct GNUNET_CORE_TransmitHandle *cth;
259 * Increase in traffic preference still to be submitted
260 * to the core service for this peer.
262 uint64_t inc_preference;
265 * Set to 1 if we're currently in the process of calling
266 * 'GNUNET_CORE_notify_transmit_ready' (so while cth is
267 * NULL, we should not call notify_transmit_ready for this
270 unsigned int cth_in_progress;
273 * Respect rating for this peer on disk.
275 uint32_t disk_respect;
278 * Which offset in "last_p2p_replies" will be updated next?
279 * (we go round-robin).
281 unsigned int last_p2p_replies_woff;
284 * Which offset in "last_client_replies" will be updated next?
285 * (we go round-robin).
287 unsigned int last_client_replies_woff;
290 * Current offset into 'last_request_times' ring buffer.
292 unsigned int last_request_times_off;
295 * GNUNET_YES if we did successfully reserve 32k bandwidth,
304 * Map from peer identities to 'struct GSF_ConnectPeer' entries.
306 static struct GNUNET_CONTAINER_MultiHashMap *cp_map;
309 * Where do we store respect information?
311 static char *respectDirectory;
314 * Handle to ATS service.
316 static struct GNUNET_ATS_PerformanceHandle *ats;
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 * Find latency information in 'atsi'.
340 * @param atsi performance data
341 * @param atsi_count number of records in 'atsi'
342 * @return connection latency
344 static struct GNUNET_TIME_Relative
345 get_latency (const struct GNUNET_ATS_Information *atsi, unsigned int atsi_count)
349 for (i = 0; i < atsi_count; i++)
350 if (ntohl (atsi->type) == GNUNET_ATS_QUALITY_NET_DELAY)
351 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
352 ntohl (atsi->value));
353 return GNUNET_TIME_UNIT_SECONDS;
358 * Update the performance information kept for the given peer.
360 * @param cp peer record to update
361 * @param atsi transport performance data
362 * @param atsi_count number of records in 'atsi'
365 update_atsi (struct GSF_ConnectedPeer *cp,
366 const struct GNUNET_ATS_Information *atsi, unsigned int atsi_count)
368 struct GNUNET_TIME_Relative latency;
370 latency = get_latency (atsi, atsi_count);
371 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay, latency);
372 /* LATER: merge atsi into cp's performance data (if we ever care...) */
377 * Return the performance data record for the given peer
379 * @param cp peer to query
380 * @return performance data record for the peer
382 struct GSF_PeerPerformanceData *
383 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
390 * Core is ready to transmit to a peer, get the message.
392 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
393 * @param size number of bytes core is willing to take
394 * @param buf where to copy the message
395 * @return number of bytes copied to buf
398 peer_transmit_ready_cb (void *cls, size_t size, void *buf);
402 * Function called by core upon success or failure of our bandwidth reservation request.
404 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
405 * @param peer identifies the peer
406 * @param amount set to the amount that was actually reserved or unreserved;
407 * either the full requested amount or zero (no partial reservations)
408 * @param res_delay if the reservation could not be satisfied (amount was 0), how
409 * long should the client wait until re-trying?
412 ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
413 int32_t amount, struct GNUNET_TIME_Relative res_delay);
417 * If ready (bandwidth reserved), try to schedule transmission via
418 * core for the given handle.
420 * @param pth transmission handle to schedule
423 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
425 struct GSF_ConnectedPeer *cp;
426 struct GNUNET_PeerIdentity target;
429 if ((NULL != cp->cth) || (0 != cp->cth_in_progress))
430 return; /* already done */
431 GNUNET_assert (0 != cp->ppd.pid);
432 GNUNET_PEER_resolve (cp->ppd.pid, &target);
434 if (0 != cp->inc_preference)
436 GNUNET_ATS_change_preference (ats, &target, GNUNET_ATS_PREFERENCE_BANDWIDTH,
437 (double) cp->inc_preference,
438 GNUNET_ATS_PREFERENCE_END);
439 cp->inc_preference = 0;
442 if ((GNUNET_YES == pth->is_query) && (GNUNET_YES != pth->was_reserved))
444 /* query, need reservation */
445 if (GNUNET_YES != cp->did_reserve)
446 return; /* not ready */
447 cp->did_reserve = GNUNET_NO;
448 /* reservation already done! */
449 pth->was_reserved = GNUNET_YES;
451 GNUNET_ATS_reserve_bandwidth (ats, &target, DBLOCK_SIZE,
452 &ats_reserve_callback, cp);
455 GNUNET_assert (NULL == cp->cth);
456 cp->cth_in_progress++;
458 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
459 GNUNET_TIME_absolute_get_remaining
460 (pth->timeout), &target, pth->size,
461 &peer_transmit_ready_cb, cp);
462 GNUNET_assert (NULL != cp->cth);
463 GNUNET_assert (0 < cp->cth_in_progress--);
468 * Core is ready to transmit to a peer, get the message.
470 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
471 * @param size number of bytes core is willing to take
472 * @param buf where to copy the message
473 * @return number of bytes copied to buf
476 peer_transmit_ready_cb (void *cls, size_t size, void *buf)
478 struct GSF_ConnectedPeer *cp = cls;
479 struct GSF_PeerTransmitHandle *pth = cp->pth_head;
480 struct GSF_PeerTransmitHandle *pos;
486 if (pth->size > size)
488 schedule_transmission (pth);
491 if (GNUNET_SCHEDULER_NO_TASK != pth->timeout_task)
493 GNUNET_SCHEDULER_cancel (pth->timeout_task);
494 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
496 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
497 if (GNUNET_YES == pth->is_query)
499 cp->ppd.last_request_times[(cp->last_request_times_off++) %
500 MAX_QUEUE_PER_PEER] =
501 GNUNET_TIME_absolute_get ();
502 GNUNET_assert (0 < cp->ppd.pending_queries--);
504 else if (GNUNET_NO == pth->is_query)
506 GNUNET_assert (0 < cp->ppd.pending_replies--);
508 GNUNET_LOAD_update (cp->ppd.transmission_delay,
509 GNUNET_TIME_absolute_get_duration
510 (pth->transmission_request_start_time).rel_value);
511 ret = pth->gmc (pth->gmc_cls, size, buf);
512 if (NULL != (pos = cp->pth_head))
514 GNUNET_assert (pos != pth);
515 schedule_transmission (pos);
523 * (re)try to reserve bandwidth from the given peer.
525 * @param cls the 'struct GSF_ConnectedPeer' to reserve from
526 * @param tc scheduler context
529 retry_reservation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
531 struct GSF_ConnectedPeer *cp = cls;
532 struct GNUNET_PeerIdentity target;
534 GNUNET_PEER_resolve (cp->ppd.pid, &target);
535 cp->rc_delay_task = GNUNET_SCHEDULER_NO_TASK;
537 GNUNET_ATS_reserve_bandwidth (ats, &target, DBLOCK_SIZE,
538 &ats_reserve_callback, cp);
543 * Function called by core upon success or failure of our bandwidth reservation request.
545 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
546 * @param peer identifies the peer
547 * @param amount set to the amount that was actually reserved or unreserved;
548 * either the full requested amount or zero (no partial reservations)
549 * @param res_delay if the reservation could not be satisfied (amount was 0), how
550 * long should the client wait until re-trying?
553 ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
554 int32_t amount, struct GNUNET_TIME_Relative res_delay)
556 struct GSF_ConnectedPeer *cp = cls;
557 struct GSF_PeerTransmitHandle *pth;
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "Reserved %d bytes / need to wait %llu ms for reservation\n",
561 (int) amount, (unsigned long long) res_delay.rel_value);
566 GNUNET_SCHEDULER_add_delayed (res_delay, &retry_reservation, cp);
569 cp->did_reserve = GNUNET_YES;
571 if ((NULL != pth) && (NULL == cp->cth) && (0 == cp->cth_in_progress))
573 /* reservation success, try transmission now! */
574 cp->cth_in_progress++;
576 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
577 GNUNET_TIME_absolute_get_remaining
578 (pth->timeout), peer, pth->size,
579 &peer_transmit_ready_cb, cp);
580 GNUNET_assert (NULL != cp->cth);
581 GNUNET_assert (0 < cp->cth_in_progress--);
587 * A peer connected to us. Setup the connected peer
590 * @param peer identity of peer that connected
591 * @param atsi performance data for the connection
592 * @param atsi_count number of records in 'atsi'
593 * @return handle to connected peer entry
595 struct GSF_ConnectedPeer *
596 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
597 const struct GNUNET_ATS_Information *atsi,
598 unsigned int atsi_count)
600 struct GSF_ConnectedPeer *cp;
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s\n",
606 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
607 cp->ppd.pid = GNUNET_PEER_intern (peer);
608 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
610 GNUNET_ATS_reserve_bandwidth (ats, peer, DBLOCK_SIZE,
611 &ats_reserve_callback, cp);
612 fn = get_respect_filename (peer);
613 if ((GNUNET_YES == GNUNET_DISK_file_test (fn)) &&
614 (sizeof (respect) == GNUNET_DISK_fn_read (fn, &respect, sizeof (respect))))
615 cp->disk_respect = cp->ppd.respect = ntohl (respect);
617 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128);
618 GNUNET_break (GNUNET_OK ==
619 GNUNET_CONTAINER_multihashmap_put (cp_map, &peer->hashPubKey,
621 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
622 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
623 GNUNET_CONTAINER_multihashmap_size (cp_map),
625 update_atsi (cp, atsi, atsi_count);
626 GSF_push_start_ (cp);
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'
636 * @param tc scheduler context
639 revive_migration (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
641 struct GSF_ConnectedPeer *cp = cls;
642 struct GNUNET_TIME_Relative bt;
644 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
645 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
646 if (0 != bt.rel_value)
648 /* still time left... */
649 cp->mig_revive_task =
650 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
653 GSF_push_start_ (cp);
658 * Get a handle for a connected peer.
660 * @param peer peer's identity
661 * @return NULL if the peer is not currently connected
663 struct GSF_ConnectedPeer *
664 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
668 return GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
673 * Handle P2P "MIGRATION_STOP" message.
675 * @param cls closure, always NULL
676 * @param other the other peer involved (sender or receiver, NULL
677 * for loopback messages where we are both sender and receiver)
678 * @param message the actual message
679 * @param atsi performance information
680 * @param atsi_count number of records in 'atsi'
681 * @return GNUNET_OK to keep the connection open,
682 * GNUNET_SYSERR to close it (signal serious error)
685 GSF_handle_p2p_migration_stop_ (void *cls,
686 const struct GNUNET_PeerIdentity *other,
687 const struct GNUNET_MessageHeader *message,
688 const struct GNUNET_ATS_Information *atsi,
689 unsigned int atsi_count)
691 struct GSF_ConnectedPeer *cp;
692 const struct MigrationStopMessage *msm;
693 struct GNUNET_TIME_Relative bt;
695 msm = (const struct MigrationStopMessage *) message;
696 cp = GSF_peer_get_ (other);
702 GNUNET_STATISTICS_update (GSF_stats,
703 gettext_noop ("# migration stop messages received"),
705 bt = GNUNET_TIME_relative_ntoh (msm->duration);
706 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
707 _("Migration of content to peer `%s' blocked for %llu ms\n"),
708 GNUNET_i2s (other), (unsigned long long) bt.rel_value);
709 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
710 if (GNUNET_SCHEDULER_NO_TASK == cp->mig_revive_task)
713 cp->mig_revive_task =
714 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
716 update_atsi (cp, atsi, atsi_count);
722 * Copy reply and free put message.
724 * @param cls the 'struct PutMessage'
725 * @param buf_size number of bytes available in buf
726 * @param buf where to copy the message, NULL on error (peer disconnect)
727 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
730 copy_reply (void *cls, size_t buf_size, void *buf)
732 struct PutMessage *pm = cls;
737 GNUNET_assert (buf_size >= ntohs (pm->header.size));
738 size = ntohs (pm->header.size);
739 memcpy (buf, pm, size);
740 GNUNET_STATISTICS_update (GSF_stats,
742 ("# replies transmitted to other peers"), 1,
748 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# replies dropped"), 1,
757 * Free resources associated with the given peer request.
759 * @param peerreq request to free
760 * @param query associated key for the request
763 free_pending_request (struct PeerRequest *peerreq,
764 const struct GNUNET_HashCode *query)
766 struct GSF_ConnectedPeer *cp = peerreq->cp;
768 if (GNUNET_SCHEDULER_NO_TASK != peerreq->kill_task)
770 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
771 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
773 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
775 GNUNET_break (GNUNET_YES ==
776 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
778 GNUNET_free (peerreq);
783 * Cancel all requests associated with the peer.
786 * @param query hash code of the request
787 * @param value the 'struct GSF_PendingRequest'
788 * @return GNUNET_YES (continue to iterate)
791 cancel_pending_request (void *cls, const struct GNUNET_HashCode * query, void *value)
793 struct PeerRequest *peerreq = value;
794 struct GSF_PendingRequest *pr = peerreq->pr;
795 struct GSF_PendingRequestData *prd;
797 prd = GSF_pending_request_get_data_ (pr);
798 GSF_pending_request_cancel_ (pr, GNUNET_NO);
799 free_pending_request (peerreq, &prd->query);
805 * Free the given request.
807 * @param cls the request to free
808 * @param tc task context
811 peer_request_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
813 struct PeerRequest *peerreq = cls;
814 struct GSF_PendingRequest *pr = peerreq->pr;
815 struct GSF_PendingRequestData *prd;
817 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
818 prd = GSF_pending_request_get_data_ (pr);
819 cancel_pending_request (NULL, &prd->query, peerreq);
824 * The artificial delay is over, transmit the message now.
826 * @param cls the 'struct GSF_DelayedHandle' with the message
827 * @param tc scheduler context
830 transmit_delayed_now (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
832 struct GSF_DelayedHandle *dh = cls;
833 struct GSF_ConnectedPeer *cp = dh->cp;
835 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
836 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
838 GNUNET_free (dh->pm);
842 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT,
843 dh->msize, ©_reply, dh->pm);
849 * Get the randomized delay a response should be subjected to.
851 * @return desired delay
853 static struct GNUNET_TIME_Relative
854 get_randomized_delay ()
856 struct GNUNET_TIME_Relative ret;
859 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
860 GNUNET_CRYPTO_random_u32
861 (GNUNET_CRYPTO_QUALITY_WEAK,
862 2 * GSF_avg_latency.rel_value + 1));
863 GNUNET_STATISTICS_update (GSF_stats,
865 ("# artificial delays introduced (ms)"),
866 ret.rel_value, GNUNET_NO);
873 * Handle a reply to a pending request. Also called if a request
874 * expires (then with data == NULL). The handler may be called
875 * many times (depending on the request type), but will not be
876 * called during or after a call to GSF_pending_request_cancel
877 * and will also not be called anymore after a call signalling
880 * @param cls 'struct PeerRequest' this is an answer for
881 * @param eval evaluation of the result
882 * @param pr handle to the original pending request
883 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
884 * @param expiration when does 'data' expire?
885 * @param last_transmission when did we last transmit a request for this block
886 * @param type type of the block
887 * @param data response data, NULL on request expiration
888 * @param data_len number of bytes in data
891 handle_p2p_reply (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
892 struct GSF_PendingRequest *pr, uint32_t reply_anonymity_level,
893 struct GNUNET_TIME_Absolute expiration,
894 struct GNUNET_TIME_Absolute last_transmission,
895 enum GNUNET_BLOCK_Type type, const void *data,
898 struct PeerRequest *peerreq = cls;
899 struct GSF_ConnectedPeer *cp = peerreq->cp;
900 struct GSF_PendingRequestData *prd;
901 struct PutMessage *pm;
904 GNUNET_assert (data_len + sizeof (struct PutMessage) <
905 GNUNET_SERVER_MAX_MESSAGE_SIZE);
906 GNUNET_assert (peerreq->pr == pr);
907 prd = GSF_pending_request_get_data_ (pr);
910 free_pending_request (peerreq, &prd->query);
913 GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
914 if ((prd->type != type) && (GNUNET_BLOCK_TYPE_ANY != prd->type))
916 GNUNET_STATISTICS_update (GSF_stats,
918 ("# replies dropped due to type mismatch"),
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "Transmitting result for query `%s' to peer\n",
924 GNUNET_h2s (&prd->query));
925 GNUNET_STATISTICS_update (GSF_stats,
926 gettext_noop ("# replies received for other peers"),
928 msize = sizeof (struct PutMessage) + data_len;
929 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
934 if ((UINT32_MAX != reply_anonymity_level) && (reply_anonymity_level > 1))
936 if (reply_anonymity_level - 1 > GSF_cover_content_count)
938 GNUNET_STATISTICS_update (GSF_stats,
940 ("# replies dropped due to insufficient cover traffic"),
944 GSF_cover_content_count -= (reply_anonymity_level - 1);
947 pm = GNUNET_malloc (msize);
948 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
949 pm->header.size = htons (msize);
950 pm->type = htonl (type);
951 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
952 memcpy (&pm[1], data, data_len);
953 if ((UINT32_MAX != reply_anonymity_level) && (0 != reply_anonymity_level) &&
954 (GNUNET_YES == GSF_enable_randomized_delays))
956 struct GSF_DelayedHandle *dh;
958 dh = GNUNET_malloc (sizeof (struct GSF_DelayedHandle));
962 GNUNET_CONTAINER_DLL_insert (cp->delayed_head, cp->delayed_tail, dh);
964 GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
965 &transmit_delayed_now, dh);
969 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT, msize,
972 if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
974 if (GNUNET_SCHEDULER_NO_TASK == peerreq->kill_task)
976 GNUNET_STATISTICS_update (GSF_stats,
978 ("# P2P searches destroyed due to ultimate reply"),
981 GNUNET_SCHEDULER_add_now (&peer_request_destroy, peerreq);
987 * Increase the peer's respect by a value.
989 * @param cp which peer to change the respect value on
990 * @param value is the int value by which the
991 * peer's credit is to be increased or decreased
992 * @returns the actual change in respect (positive or negative)
995 change_peer_respect (struct GSF_ConnectedPeer *cp, int value)
999 GNUNET_assert (NULL != cp);
1002 if (cp->ppd.respect + value < cp->ppd.respect)
1004 value = UINT32_MAX - cp->ppd.respect;
1005 cp->ppd.respect = UINT32_MAX;
1008 cp->ppd.respect += value;
1012 if (cp->ppd.respect < -value)
1014 value = -cp->ppd.respect;
1015 cp->ppd.respect = 0;
1018 cp->ppd.respect += value;
1025 * We've received a request with the specified priority. Bound it
1026 * according to how much we respect the given peer.
1028 * @param prio_in requested priority
1029 * @param cp the peer making the request
1030 * @return effective priority
1033 bound_priority (uint32_t prio_in, struct GSF_ConnectedPeer *cp)
1035 #define N ((double)128.0)
1040 ld = GSF_test_get_load_too_high_ (0);
1041 if (GNUNET_SYSERR == ld)
1043 GNUNET_STATISTICS_update (GSF_stats,
1045 ("# requests done for free (low load)"), 1,
1047 return 0; /* excess resources */
1049 if (prio_in > INT32_MAX)
1050 prio_in = INT32_MAX;
1051 ret = -change_peer_respect (cp, -(int) prio_in);
1054 if (ret > GSF_current_priorities + N)
1055 rret = GSF_current_priorities + N;
1058 GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
1060 if ((GNUNET_YES == ld) && (ret > 0))
1062 /* try with charging */
1063 ld = GSF_test_get_load_too_high_ (ret);
1065 if (GNUNET_YES == ld)
1067 GNUNET_STATISTICS_update (GSF_stats,
1069 ("# request dropped, priority insufficient"), 1,
1072 change_peer_respect (cp, (int) ret);
1073 return -1; /* not enough resources */
1077 GNUNET_STATISTICS_update (GSF_stats,
1079 ("# requests done for a price (normal load)"), 1,
1088 * The priority level imposes a bound on the maximum
1089 * value for the ttl that can be requested.
1091 * @param ttl_in requested ttl
1092 * @param prio given priority
1093 * @return ttl_in if ttl_in is below the limit,
1094 * otherwise the ttl-limit for the given priority
1097 bound_ttl (int32_t ttl_in, uint32_t prio)
1099 unsigned long long allowed;
1103 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1104 if (ttl_in > allowed)
1106 if (allowed >= (1 << 30))
1115 * Handle P2P "QUERY" message. Creates the pending request entry
1116 * and sets up all of the data structures to that we will
1117 * process replies properly. Does not initiate forwarding or
1118 * local database lookups.
1120 * @param other the other peer involved (sender or receiver, NULL
1121 * for loopback messages where we are both sender and receiver)
1122 * @param message the actual message
1123 * @return pending request handle, NULL on error
1125 struct GSF_PendingRequest *
1126 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1127 const struct GNUNET_MessageHeader *message)
1129 struct PeerRequest *peerreq;
1130 struct GSF_PendingRequest *pr;
1131 struct GSF_PendingRequestData *prd;
1132 struct GSF_ConnectedPeer *cp;
1133 struct GSF_ConnectedPeer *cps;
1134 const struct GNUNET_HashCode *namespace;
1135 const struct GNUNET_PeerIdentity *target;
1136 enum GSF_PendingRequestOptions options;
1138 const struct GetMessage *gm;
1140 const struct GNUNET_HashCode *opt;
1143 uint32_t ttl_decrement;
1146 enum GNUNET_BLOCK_Type type;
1147 GNUNET_PEER_Id spid;
1149 GNUNET_assert (other != NULL);
1150 msize = ntohs (message->size);
1151 if (msize < sizeof (struct GetMessage))
1153 GNUNET_break_op (0);
1156 GNUNET_STATISTICS_update (GSF_stats,
1158 ("# GET requests received (from other peers)"), 1,
1160 gm = (const struct GetMessage *) message;
1161 type = ntohl (gm->type);
1162 bm = ntohl (gm->hash_bitmap);
1170 if (msize < sizeof (struct GetMessage) + bits * sizeof (struct GNUNET_HashCode))
1172 GNUNET_break_op (0);
1175 opt = (const struct GNUNET_HashCode *) &gm[1];
1176 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (struct GNUNET_HashCode);
1177 /* bfsize must be power of 2, check! */
1178 if (0 != ((bfsize - 1) & bfsize))
1180 GNUNET_break_op (0);
1183 GSF_cover_query_count++;
1184 bm = ntohl (gm->hash_bitmap);
1186 cps = GSF_peer_get_ (other);
1189 /* peer must have just disconnected */
1190 GNUNET_STATISTICS_update (GSF_stats,
1192 ("# requests dropped due to initiator not being connected"),
1196 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1197 cp = GSF_peer_get_ ((const struct GNUNET_PeerIdentity *) &opt[bits++]);
1202 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1205 GNUNET_i2s ((const struct GNUNET_PeerIdentity *)
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1210 "Failed to find peer `%4s' in connection set. Dropping query.\n",
1211 GNUNET_i2s (other));
1212 GNUNET_STATISTICS_update (GSF_stats,
1214 ("# requests dropped due to missing reverse route"),
1218 /* note that we can really only check load here since otherwise
1219 * peers could find out that we are overloaded by not being
1220 * disconnected after sending us a malformed query... */
1221 priority = bound_priority (ntohl (gm->priority), cps);
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "Dropping query from `%s', this peer is too busy.\n",
1226 GNUNET_i2s (other));
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1231 GNUNET_h2s (&gm->query), (unsigned int) type, GNUNET_i2s (other),
1233 namespace = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) ? &opt[bits++] : NULL;
1234 if ((GNUNET_BLOCK_TYPE_FS_SBLOCK == type) && (NULL == namespace))
1236 GNUNET_break_op (0);
1239 if ((GNUNET_BLOCK_TYPE_FS_SBLOCK != type) && (NULL != namespace))
1241 GNUNET_break_op (0);
1246 (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity
1247 *) &opt[bits++]) : NULL;
1248 options = GSF_PRO_DEFAULTS;
1250 if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority))
1251 || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1252 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 +
1253 GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
1255 /* don't have BW to send to peer, or would likely take longer than we have for it,
1256 * so at best indirect the query */
1258 options |= GSF_PRO_FORWARD_ONLY;
1259 spid = GNUNET_PEER_intern (other);
1260 GNUNET_assert (0 != spid);
1262 ttl = bound_ttl (ntohl (gm->ttl), priority);
1263 /* decrement ttl (always) */
1265 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1267 if ((ttl < 0) && (((int32_t) (ttl - ttl_decrement)) > 0))
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1271 GNUNET_i2s (other), ttl, ttl_decrement);
1272 GNUNET_STATISTICS_update (GSF_stats,
1274 ("# requests dropped due TTL underflow"), 1,
1276 /* integer underflow => drop (should be very rare)! */
1279 ttl -= ttl_decrement;
1281 /* test if the request already exists */
1282 peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map, &gm->query);
1283 if (peerreq != NULL)
1286 prd = GSF_pending_request_get_data_ (pr);
1287 if ((prd->type == type) &&
1288 ((type != GNUNET_BLOCK_TYPE_FS_SBLOCK) ||
1289 (0 == memcmp (&prd->namespace, namespace, sizeof (struct GNUNET_HashCode)))))
1291 if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get ().abs_value + ttl)
1293 /* existing request has higher TTL, drop new one! */
1294 prd->priority += priority;
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296 "Have existing request with higher TTL, dropping new request.\n",
1297 GNUNET_i2s (other));
1298 GNUNET_STATISTICS_update (GSF_stats,
1300 ("# requests dropped due to higher-TTL request"),
1304 /* existing request has lower TTL, drop old one! */
1305 priority += prd->priority;
1306 GSF_pending_request_cancel_ (pr, GNUNET_YES);
1307 free_pending_request (peerreq, &gm->query);
1311 peerreq = GNUNET_malloc (sizeof (struct PeerRequest));
1313 pr = GSF_pending_request_create_ (options, type, &gm->query, namespace,
1316 0) ? (const char *) &opt[bits] : NULL,
1317 bfsize, ntohl (gm->filter_mutator),
1319 (uint32_t) priority, ttl, spid, GNUNET_PEER_intern (other), NULL, 0, /* replies_seen */
1320 &handle_p2p_reply, peerreq);
1321 GNUNET_assert (NULL != pr);
1323 GNUNET_break (GNUNET_OK ==
1324 GNUNET_CONTAINER_multihashmap_put (cp->request_map, &gm->query,
1326 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1327 GNUNET_STATISTICS_update (GSF_stats,
1329 ("# P2P query messages received and processed"), 1,
1331 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
1338 * Function called if there has been a timeout trying to satisfy
1339 * a transmission request.
1341 * @param cls the 'struct GSF_PeerTransmitHandle' of the request
1342 * @param tc scheduler context
1345 peer_transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1347 struct GSF_PeerTransmitHandle *pth = cls;
1348 struct GSF_ConnectedPeer *cp;
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1351 "Timeout trying to transmit to other peer\n");
1352 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1354 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1355 if (GNUNET_YES == pth->is_query)
1356 GNUNET_assert (0 < cp->ppd.pending_queries--);
1357 else if (GNUNET_NO == pth->is_query)
1358 GNUNET_assert (0 < cp->ppd.pending_replies--);
1359 GNUNET_LOAD_update (cp->ppd.transmission_delay, UINT64_MAX);
1360 if (NULL != cp->cth)
1362 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1365 pth->gmc (pth->gmc_cls, 0, NULL);
1366 GNUNET_assert (0 == cp->cth_in_progress);
1372 * Transmit a message to the given peer as soon as possible.
1373 * If the peer disconnects before the transmission can happen,
1374 * the callback is invoked with a 'NULL' buffer.
1376 * @param cp target peer
1377 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
1378 * @param priority how important is this request?
1379 * @param timeout when does this request timeout (call gmc with error)
1380 * @param size number of bytes we would like to send to the peer
1381 * @param gmc function to call to get the message
1382 * @param gmc_cls closure for gmc
1383 * @return handle to cancel request
1385 struct GSF_PeerTransmitHandle *
1386 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp, int is_query,
1387 uint32_t priority, struct GNUNET_TIME_Relative timeout,
1388 size_t size, GSF_GetMessageCallback gmc, void *gmc_cls)
1390 struct GSF_PeerTransmitHandle *pth;
1391 struct GSF_PeerTransmitHandle *pos;
1392 struct GSF_PeerTransmitHandle *prev;
1394 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
1395 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1396 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1398 pth->gmc_cls = gmc_cls;
1400 pth->is_query = is_query;
1401 pth->priority = priority;
1403 /* insertion sort (by priority, descending) */
1406 while ((NULL != pos) && (pos->priority > priority))
1411 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head, cp->pth_tail, prev, pth);
1412 if (GNUNET_YES == is_query)
1413 cp->ppd.pending_queries++;
1414 else if (GNUNET_NO == is_query)
1415 cp->ppd.pending_replies++;
1417 GNUNET_SCHEDULER_add_delayed (timeout, &peer_transmit_timeout, pth);
1418 schedule_transmission (pth);
1424 * Cancel an earlier request for transmission.
1426 * @param pth request to cancel
1429 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1431 struct GSF_ConnectedPeer *cp;
1433 if (GNUNET_SCHEDULER_NO_TASK != pth->timeout_task)
1435 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1436 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1439 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1440 if (GNUNET_YES == pth->is_query)
1441 GNUNET_assert (0 < cp->ppd.pending_queries--);
1442 else if (GNUNET_NO == pth->is_query)
1443 GNUNET_assert (0 < cp->ppd.pending_replies--);
1449 * Report on receiving a reply; update the performance record of the given peer.
1451 * @param cp responding peer (will be updated)
1452 * @param request_time time at which the original query was transmitted
1453 * @param request_priority priority of the original request
1456 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1457 struct GNUNET_TIME_Absolute request_time,
1458 uint32_t request_priority)
1460 struct GNUNET_TIME_Relative delay;
1462 delay = GNUNET_TIME_absolute_get_duration (request_time);
1463 cp->ppd.avg_reply_delay.rel_value =
1464 (cp->ppd.avg_reply_delay.rel_value * (RUNAVG_DELAY_N - 1) +
1465 delay.rel_value) / RUNAVG_DELAY_N;
1466 cp->ppd.avg_priority =
1467 (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
1468 request_priority) / RUNAVG_DELAY_N;
1473 * Report on receiving a reply in response to an initiating client.
1474 * Remember that this peer is good for this client.
1476 * @param cp responding peer (will be updated)
1477 * @param initiator_client local client on responsible for query
1480 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1481 struct GSF_LocalClient *initiator_client)
1483 cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
1484 CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1489 * Report on receiving a reply in response to an initiating peer.
1490 * Remember that this peer is good for this initiating peer.
1492 * @param cp responding peer (will be updated)
1493 * @param initiator_peer other peer responsible for query
1496 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1497 const struct GSF_ConnectedPeer *initiator_peer)
1501 woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
1502 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
1503 cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
1504 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1505 cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
1510 * A peer disconnected from us. Tear down the connected peer
1514 * @param peer identity of peer that connected
1517 GSF_peer_disconnect_handler_ (void *cls, const struct GNUNET_PeerIdentity *peer)
1519 struct GSF_ConnectedPeer *cp;
1520 struct GSF_PeerTransmitHandle *pth;
1521 struct GSF_DelayedHandle *dh;
1523 cp = GSF_peer_get_ (peer);
1525 return; /* must have been disconnect from core with
1526 * 'peer' == my_id, ignore */
1527 GNUNET_assert (GNUNET_YES ==
1528 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1529 &peer->hashPubKey, cp));
1530 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
1531 GNUNET_CONTAINER_multihashmap_size (cp_map),
1533 if (NULL != cp->migration_pth)
1535 GSF_peer_transmit_cancel_ (cp->migration_pth);
1536 cp->migration_pth = NULL;
1540 GNUNET_ATS_reserve_bandwidth_cancel (cp->rc);
1543 if (GNUNET_SCHEDULER_NO_TASK != cp->rc_delay_task)
1545 GNUNET_SCHEDULER_cancel (cp->rc_delay_task);
1546 cp->rc_delay_task = GNUNET_SCHEDULER_NO_TASK;
1548 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1549 &cancel_pending_request, cp);
1550 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1551 cp->request_map = NULL;
1552 GSF_plan_notify_peer_disconnect_ (cp);
1553 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1554 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1555 memset (cp->ppd.last_p2p_replies, 0, sizeof (cp->ppd.last_p2p_replies));
1556 GSF_push_stop_ (cp);
1557 if (NULL != cp->cth)
1559 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1562 GNUNET_assert (0 == cp->cth_in_progress);
1563 while (NULL != (pth = cp->pth_head))
1565 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1567 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1568 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1570 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1571 pth->gmc (pth->gmc_cls, 0, NULL);
1574 while (NULL != (dh = cp->delayed_head))
1576 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
1577 GNUNET_SCHEDULER_cancel (dh->delay_task);
1578 GNUNET_free (dh->pm);
1581 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1582 if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1584 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1585 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1592 * Closure for 'call_iterator'.
1594 struct IterationContext
1597 * Function to call on each entry.
1599 GSF_ConnectedPeerIterator it;
1609 * Function that calls the callback for each peer.
1611 * @param cls the 'struct IterationContext*'
1612 * @param key identity of the peer
1613 * @param value the 'struct GSF_ConnectedPeer*'
1614 * @return GNUNET_YES to continue iteration
1617 call_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
1619 struct IterationContext *ic = cls;
1620 struct GSF_ConnectedPeer *cp = value;
1622 ic->it (ic->it_cls, (const struct GNUNET_PeerIdentity *) key, cp, &cp->ppd);
1628 * Iterate over all connected peers.
1630 * @param it function to call for each peer
1631 * @param it_cls closure for it
1634 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls)
1636 struct IterationContext ic;
1640 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &call_iterator, &ic);
1645 * Obtain the identity of a connected peer.
1647 * @param cp peer to reserve bandwidth from
1648 * @param id identity to set (written to)
1651 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1652 struct GNUNET_PeerIdentity *id)
1654 GNUNET_assert (0 != cp->ppd.pid);
1655 GNUNET_PEER_resolve (cp->ppd.pid, id);
1660 * Assemble a migration stop message for transmission.
1662 * @param cls the 'struct GSF_ConnectedPeer' to use
1663 * @param size number of bytes we're allowed to write to buf
1664 * @param buf where to copy the message
1665 * @return number of bytes copied to buf
1668 create_migration_stop_message (void *cls, size_t size, void *buf)
1670 struct GSF_ConnectedPeer *cp = cls;
1671 struct MigrationStopMessage msm;
1673 cp->migration_pth = NULL;
1676 GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1677 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1678 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1679 msm.reserved = htonl (0);
1681 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1682 (cp->last_migration_block));
1683 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1684 GNUNET_STATISTICS_update (GSF_stats,
1685 gettext_noop ("# migration stop messages sent"),
1687 return sizeof (struct MigrationStopMessage);
1692 * Ask a peer to stop migrating data to us until the given point
1695 * @param cp peer to ask
1696 * @param block_time until when to block
1699 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1700 struct GNUNET_TIME_Absolute block_time)
1702 if (cp->last_migration_block.abs_value > block_time.abs_value)
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705 "Migration already blocked for another %llu ms\n",
1706 (unsigned long long)
1707 GNUNET_TIME_absolute_get_remaining
1708 (cp->last_migration_block).rel_value);
1709 return; /* already blocked */
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %llu ms\n",
1712 (unsigned long long) GNUNET_TIME_absolute_get_remaining (block_time).rel_value);
1713 cp->last_migration_block = block_time;
1714 if (NULL != cp->migration_pth)
1715 GSF_peer_transmit_cancel_ (cp->migration_pth);
1717 GSF_peer_transmit_ (cp, GNUNET_SYSERR, UINT32_MAX,
1718 GNUNET_TIME_UNIT_FOREVER_REL,
1719 sizeof (struct MigrationStopMessage),
1720 &create_migration_stop_message, cp);
1725 * Write peer-respect information to a file - flush the buffer entry!
1728 * @param key peer identity
1729 * @param value the 'struct GSF_ConnectedPeer' to flush
1730 * @return GNUNET_OK to continue iteration
1733 flush_respect (void *cls, const struct GNUNET_HashCode * key, void *value)
1735 struct GSF_ConnectedPeer *cp = value;
1738 struct GNUNET_PeerIdentity pid;
1740 if (cp->ppd.respect == cp->disk_respect)
1741 return GNUNET_OK; /* unchanged */
1742 GNUNET_assert (0 != cp->ppd.pid);
1743 GNUNET_PEER_resolve (cp->ppd.pid, &pid);
1744 fn = get_respect_filename (&pid);
1745 if (cp->ppd.respect == 0)
1747 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1748 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1749 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1753 respect = htonl (cp->ppd.respect);
1754 if (sizeof (uint32_t) ==
1755 GNUNET_DISK_fn_write (fn, &respect, sizeof (uint32_t),
1756 GNUNET_DISK_PERM_USER_READ |
1757 GNUNET_DISK_PERM_USER_WRITE |
1758 GNUNET_DISK_PERM_GROUP_READ |
1759 GNUNET_DISK_PERM_OTHER_READ))
1760 cp->disk_respect = cp->ppd.respect;
1768 * Notify core about a preference we have for the given peer
1769 * (to allocate more resources towards it). The change will
1770 * be communicated the next time we reserve bandwidth with
1771 * core (not instantly).
1773 * @param cp peer to reserve bandwidth from
1774 * @param pref preference change
1777 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1780 cp->inc_preference += pref;
1785 * Call this method periodically to flush respect information to disk.
1787 * @param cls closure, not used
1788 * @param tc task context, not used
1791 cron_flush_respect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1796 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &flush_respect, NULL);
1799 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1801 GNUNET_SCHEDULER_add_delayed_with_priority (RESPECT_FLUSH_FREQ,
1802 GNUNET_SCHEDULER_PRIORITY_HIGH,
1803 &cron_flush_respect, NULL);
1808 * Initialize peer management subsystem.
1811 GSF_connected_peer_init_ ()
1813 cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1814 ats = GNUNET_ATS_performance_init (GSF_cfg, NULL, NULL);
1815 GNUNET_assert (GNUNET_OK ==
1816 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg, "fs",
1818 &respectDirectory));
1819 GNUNET_DISK_directory_create (respectDirectory);
1820 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1821 &cron_flush_respect, NULL);
1826 * Iterator to free peer entries.
1828 * @param cls closure, unused
1829 * @param key current key code
1830 * @param value value in the hash map (peer entry)
1831 * @return GNUNET_YES (we should continue to iterate)
1834 clean_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
1836 GSF_peer_disconnect_handler_ (NULL, (const struct GNUNET_PeerIdentity *) key);
1842 * Shutdown peer management subsystem.
1845 GSF_connected_peer_done_ ()
1847 cron_flush_respect (NULL, NULL);
1848 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_peer, NULL);
1849 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1851 GNUNET_free (respectDirectory);
1852 respectDirectory = NULL;
1853 GNUNET_ATS_performance_done (ats);
1859 * Iterator to remove references to LC entry.
1861 * @param cls the 'struct GSF_LocalClient*' to look for
1862 * @param key current key code
1863 * @param value value in the hash map (peer entry)
1864 * @return GNUNET_YES (we should continue to iterate)
1867 clean_local_client (void *cls, const struct GNUNET_HashCode * key, void *value)
1869 const struct GSF_LocalClient *lc = cls;
1870 struct GSF_ConnectedPeer *cp = value;
1873 for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
1874 if (cp->ppd.last_client_replies[i] == lc)
1875 cp->ppd.last_client_replies[i] = NULL;
1881 * Notification that a local client disconnected. Clean up all of our
1882 * references to the given handle.
1884 * @param lc handle to the local client (henceforth invalid)
1887 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1890 return; /* already cleaned up */
1891 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_local_client,
1896 /* end of gnunet-service-fs_cp.c */