2 This file is part of GNUnet.
3 (C) 2010,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 transport/gnunet-service-transport_neighbours.c
23 * @brief neighbour management
24 * @author Christian Grothoff
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_neighbours.h"
29 #include "gnunet-service-transport_plugins.h"
30 #include "gnunet-service-transport_validation.h"
31 #include "gnunet-service-transport.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_constants.h"
34 #include "transport.h"
38 * Size of the neighbour hash map.
40 #define NEIGHBOUR_TABLE_SIZE 256
43 * How often must a peer violate bandwidth quotas before we start
44 * to simply drop its messages?
46 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
50 * Entry in neighbours.
52 struct NeighbourMapEntry;
56 * For each neighbour we keep a list of messages
57 * that we still want to transmit to the neighbour.
63 * This is a doubly linked list.
65 struct MessageQueue *next;
68 * This is a doubly linked list.
70 struct MessageQueue *prev;
73 * Once this message is actively being transmitted, which
74 * neighbour is it associated with?
76 struct NeighbourMapEntry *n;
79 * Function to call once we're done.
81 GST_NeighbourSendContinuation cont;
89 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
90 * stuck together in memory. Allocated at the end of this struct.
92 const char *message_buf;
95 * Size of the message buf
97 size_t message_buf_size;
100 * At what time should we fail?
102 struct GNUNET_TIME_Absolute timeout;
108 * Entry in neighbours.
110 struct NeighbourMapEntry
114 * Head of list of messages we would like to send to this peer;
115 * must contain at most one message per client.
117 struct MessageQueue *messages_head;
120 * Tail of list of messages we would like to send to this peer; must
121 * contain at most one message per client.
123 struct MessageQueue *messages_tail;
126 * Context for address suggestion.
127 * NULL after we are connected.
129 struct GNUNET_ATS_SuggestionContext *asc;
132 * Performance data for the peer.
134 struct GNUNET_TRANSPORT_ATS_Information *ats;
137 * Are we currently trying to send a message? If so, which one?
139 struct MessageQueue *is_active;
142 * Active session for communicating with the peer.
144 struct Session *session;
147 * Name of the plugin we currently use.
152 * Address used for communicating with the peer, NULL for inbound connections.
157 * Number of bytes in 'addr'.
162 * Identity of this neighbour.
164 struct GNUNET_PeerIdentity id;
167 * ID of task scheduled to run when this peer is about to
168 * time out (will free resources associated with the peer).
170 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
173 * ID of task scheduled to run when we should try transmitting
174 * the head of the message queue.
176 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
179 * Tracker for inbound bandwidth.
181 struct GNUNET_BANDWIDTH_Tracker in_tracker;
184 * How often has the other peer (recently) violated the inbound
185 * traffic limit? Incremented by 10 per violation, decremented by 1
186 * per non-violation (for each time interval).
188 unsigned int quota_violation_count;
191 * Number of values in 'ats' array.
193 unsigned int ats_count;
196 * Are we already in the process of disconnecting this neighbour?
201 * Do we currently consider this neighbour connected? (as far as
202 * the connect/disconnect callbacks are concerned)?
210 * All known neighbours and their HELLOs.
212 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
215 * Closure for connect_notify_cb and disconnect_notify_cb
217 static void *callback_cls;
220 * Function to call when we connected to a neighbour.
222 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
225 * Function to call when we disconnected from a neighbour.
227 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
231 * Lookup a neighbour entry in the neighbours hash map.
233 * @param pid identity of the peer to look up
234 * @return the entry, NULL if there is no existing record
236 static struct NeighbourMapEntry *
237 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
239 return GNUNET_CONTAINER_multihashmap_get (neighbours,
245 * Task invoked to start a transmission to another peer.
247 * @param cls the 'struct NeighbourMapEntry'
248 * @param tc scheduler context
251 transmission_task (void *cls,
252 const struct GNUNET_SCHEDULER_TaskContext *tc);
256 * We're done with our transmission attempt, continue processing.
258 * @param cls the 'struct MessageQueue' of the message
259 * @param receiver intended receiver
260 * @param success whether it worked or not
263 transmit_send_continuation (void *cls,
264 const struct GNUNET_PeerIdentity *receiver,
267 struct MessageQueue *mq;
268 struct NeighbourMapEntry *n;
274 GNUNET_assert (n->is_active == mq);
276 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
277 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task,
280 if (NULL != mq->cont)
281 mq->cont (mq->cont_cls,
288 * Check the ready list for the given neighbour and if a plugin is
289 * ready for transmission (and if we have a message), do so!
291 * @param neighbour target peer for which to transmit
294 try_transmission_to_peer (struct NeighbourMapEntry *n)
296 struct MessageQueue *mq;
297 struct GNUNET_TIME_Relative timeout;
299 struct GNUNET_TRANSPORT_PluginFunctions *papi;
301 if (n->is_active != NULL)
302 return; /* transmission already pending */
303 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
304 return; /* currently waiting for bandwidth */
305 mq = n->messages_head;
306 while (NULL != (mq = n->messages_head))
308 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
309 if (timeout.rel_value > 0)
311 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
314 return; /* no more messages */
316 papi = GST_plugins_find (n->plugin_name);
322 GNUNET_CONTAINER_DLL_remove (n->messages_head,
327 ret = papi->send (papi->cls,
330 mq->message_buf_size,
331 0 /* priority -- remove from plugin API? */,
337 &transmit_send_continuation, mq);
340 /* failure, but 'send' would not call continuation in this case,
341 so we need to do it here! */
342 transmit_send_continuation (mq,
345 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task,
352 * Task invoked to start a transmission to another peer.
354 * @param cls the 'struct NeighbourMapEntry'
355 * @param tc scheduler context
358 transmission_task (void *cls,
359 const struct GNUNET_SCHEDULER_TaskContext *tc)
361 struct NeighbourMapEntry *n = cls;
363 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
364 try_transmission_to_peer (n);
369 * Initialize the neighbours subsystem.
371 * @param cls closure for callbacks
372 * @param connect_cb function to call if we connect to a peer
373 * @param disconnect_cb function to call if we disconnect from a peer
376 GST_neighbours_start (void *cls,
377 GNUNET_TRANSPORT_NotifyConnect connect_cb,
378 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
381 connect_notify_cb = connect_cb;
382 disconnect_notify_cb = disconnect_cb;
383 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
388 * Disconnect from the given neighbour, clean up the record.
390 * @param n neighbour to disconnect from
393 disconnect_neighbour (struct NeighbourMapEntry *n)
395 struct MessageQueue *mq;
397 if (GNUNET_YES == n->in_disconnect)
399 n->in_disconnect = GNUNET_YES;
400 while (NULL != (mq = n->messages_head))
402 GNUNET_CONTAINER_DLL_remove (n->messages_head,
405 mq->cont (mq->cont_cls, GNUNET_SYSERR);
408 if (NULL != n->is_active)
410 n->is_active->n = NULL;
413 if (GNUNET_YES == n->is_connected)
415 n->is_connected = GNUNET_NO;
416 disconnect_notify_cb (callback_cls,
419 GNUNET_assert (GNUNET_YES ==
420 GNUNET_CONTAINER_multihashmap_remove (neighbours,
423 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
425 GNUNET_SCHEDULER_cancel (n->timeout_task);
426 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
428 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
430 GNUNET_SCHEDULER_cancel (n->timeout_task);
431 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
435 GNUNET_ATS_suggest_address_cancel (n->asc);
438 GNUNET_array_grow (n->ats,
441 if (NULL != n->plugin_name)
443 GNUNET_free (n->plugin_name);
444 n->plugin_name = NULL;
448 GNUNET_free (n->addr);
458 * Peer has been idle for too long. Disconnect.
460 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
461 * @param tc scheduler context
464 neighbour_timeout_task (void *cls,
465 const struct GNUNET_SCHEDULER_TaskContext *tc)
467 struct NeighbourMapEntry *n = cls;
469 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
470 disconnect_neighbour (n);
475 * Disconnect from the given neighbour.
478 * @param key hash of neighbour's public key (not used)
479 * @param value the 'struct NeighbourMapEntry' of the neighbour
482 disconnect_all_neighbours (void *cls,
483 const GNUNET_HashCode *key,
486 struct NeighbourMapEntry *n = value;
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "Disconnecting peer `%4s', %s\n",
494 disconnect_neighbour (n);
500 * Cleanup the neighbours subsystem.
503 GST_neighbours_stop ()
505 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
506 &disconnect_all_neighbours,
508 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
511 connect_notify_cb = NULL;
512 disconnect_notify_cb = NULL;
517 * For an existing neighbour record, set the active connection to
520 * @param plugin_name name of transport that delivered the PONG
521 * @param address address of the other peer, NULL if other peer
523 * @param address_len number of bytes in address
524 * @param ats performance data
525 * @param ats_count number of entries in ats (excluding 0-termination)
528 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
529 const char *plugin_name,
532 struct Session *session,
533 const struct GNUNET_TRANSPORT_ATS_Information *ats,
536 struct NeighbourMapEntry *n;
537 struct GNUNET_MessageHeader connect_msg;
539 n = lookup_neighbour (peer);
545 GNUNET_free_non_null (n->addr);
546 n->addr = GNUNET_malloc (address_len);
547 memcpy (n->addr, address, address_len);
548 n->addrlen = address_len;
549 n->session = session;
550 GNUNET_array_grow (n->ats,
555 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
556 GNUNET_free_non_null (n->plugin_name);
557 n->plugin_name = GNUNET_strdup (plugin_name);
558 GNUNET_SCHEDULER_cancel (n->timeout_task);
560 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
561 &neighbour_timeout_task, n);
562 connect_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
563 connect_msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
564 GST_neighbours_send (peer,
566 sizeof (connect_msg),
567 GNUNET_TIME_UNIT_FOREVER_REL,
573 * Try to connect to the target peer using the given address
575 * @param cls the 'struct NeighbourMapEntry' of the target
576 * @param target identity of the target peer
577 * @param plugin_name name of the plugin
578 * @param plugin_address binary address
579 * @param plugin_address_len length of address
580 * @param bandwidth available bandwidth
581 * @param ats performance data for the address (as far as known)
582 * @param ats_count number of performance records in 'ats'
585 try_connect_using_address (void *cls,
586 const struct GNUNET_PeerIdentity *target,
587 const char *plugin_name,
588 const void *plugin_address,
589 size_t plugin_address_len,
590 struct GNUNET_BANDWIDTH_Value32NBO bandwidth,
591 const struct GNUNET_TRANSPORT_ATS_Information *ats,
594 struct NeighbourMapEntry *n = cls;
597 GST_neighbours_switch_to_address (target,
603 if (GNUNET_YES == n->is_connected)
605 n->is_connected = GNUNET_YES;
606 connect_notify_cb (callback_cls,
614 * Try to create a connection to the given target (eventually).
616 * @param target peer to try to connect to
619 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
621 struct NeighbourMapEntry *n;
623 GNUNET_assert (0 != memcmp (target,
625 sizeof (struct GNUNET_PeerIdentity)));
626 n = lookup_neighbour (target);
628 (GNUNET_YES == n->is_connected) )
629 return; /* already connected */
632 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
634 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
635 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
636 MAX_BANDWIDTH_CARRY_S);
637 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
638 &neighbour_timeout_task, n);
639 GNUNET_assert (GNUNET_OK ==
640 GNUNET_CONTAINER_multihashmap_put (neighbours,
643 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
646 return; /* already trying */
647 n->asc = GNUNET_ATS_suggest_address (GST_ats,
649 &try_connect_using_address,
655 * Test if we're connected to the given peer.
657 * @param target peer to test
658 * @return GNUNET_YES if we are connected, GNUNET_NO if not
661 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
663 struct NeighbourMapEntry *n;
665 n = lookup_neighbour (target);
667 (n->is_connected == GNUNET_YES) )
668 return GNUNET_NO; /* not connected */
674 * Transmit a message to the given target using the active connection.
676 * @param target destination
677 * @param msg message to send
678 * @param msg_size number of bytes in msg
679 * @param timeout when to fail with timeout
680 * @param cont function to call when done
681 * @param cont_cls closure for 'cont'
684 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
687 struct GNUNET_TIME_Relative timeout,
688 GST_NeighbourSendContinuation cont,
691 struct NeighbourMapEntry *n;
692 struct MessageQueue *mq;
694 n = lookup_neighbour (target);
696 (GNUNET_YES != n->is_connected) )
698 GNUNET_STATISTICS_update (GST_stats,
699 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
707 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
708 GNUNET_STATISTICS_update (GST_stats,
709 gettext_noop ("# bytes in message queue for other peers"),
712 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
714 mq->cont_cls = cont_cls;
715 /* FIXME: this memcpy can be up to 7% of our total runtime! */
716 memcpy (&mq[1], msg, msg_size);
717 mq->message_buf = (const char*) &mq[1];
718 mq->message_buf_size = msg_size;
719 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
720 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head,
723 if ( (GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
724 (NULL == n->is_active) )
725 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task,
731 * We have received a message from the given sender. How long should
732 * we delay before receiving more? (Also used to keep the peer marked
735 * @param sender sender of the message
736 * @param size size of the message
737 * @return how long to wait before reading more from this sender
739 struct GNUNET_TIME_Relative
740 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
743 struct NeighbourMapEntry *n;
744 struct GNUNET_TIME_Relative ret;
746 n = lookup_neighbour (sender);
748 return GNUNET_TIME_UNIT_ZERO;
749 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
752 n->quota_violation_count++;
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
755 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
756 n->in_tracker.available_bytes_per_s__,
757 n->quota_violation_count);
759 /* Discount 32k per violation */
760 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
765 if (n->quota_violation_count > 0)
767 /* try to add 32k back */
768 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
770 n->quota_violation_count--;
773 GNUNET_SCHEDULER_cancel (n->timeout_task);
775 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
776 &neighbour_timeout_task, n);
777 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
779 GNUNET_STATISTICS_update (GST_stats,
780 gettext_noop ("# bandwidth quota violations by other peers"),
783 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
785 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
786 if (ret.rel_value > 0)
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
791 (unsigned long long) n->in_tracker.consumption_since_last_update__,
792 (unsigned int) n->in_tracker.available_bytes_per_s__,
793 (unsigned long long) ret.rel_value);
795 GNUNET_STATISTICS_update (GST_stats,
796 gettext_noop ("# ms throttling suggested"),
797 (int64_t) ret.rel_value,
805 * Change the incoming quota for the given peer.
807 * @param neighbour identity of peer to change qutoa for
808 * @param quota new quota
811 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
812 struct GNUNET_BANDWIDTH_Value32NBO quota)
814 struct NeighbourMapEntry *n;
816 n = lookup_neighbour (neighbour);
819 GNUNET_STATISTICS_update (GST_stats,
820 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
825 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
827 if (0 != ntohl (quota.value__))
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
831 "Disconnecting peer `%4s' due to `%s'\n",
835 GNUNET_STATISTICS_update (GST_stats,
836 gettext_noop ("# disconnects due to quota of 0"),
839 disconnect_neighbour (n);
844 * Closure for the neighbours_iterate function.
846 struct IteratorContext
849 * Function to call on each connected neighbour.
851 GST_NeighbourIterator cb;
861 * Call the callback from the closure for each connected neighbour.
863 * @param cls the 'struct IteratorContext'
864 * @param key the hash of the public key of the neighbour
865 * @param value the 'struct NeighbourMapEntry'
866 * @return GNUNET_OK (continue to iterate)
869 neighbours_iterate (void *cls,
870 const GNUNET_HashCode *key,
873 struct IteratorContext *ic = cls;
874 struct NeighbourMapEntry *n = value;
876 if (GNUNET_YES != n->is_connected)
878 GNUNET_assert (n->ats_count > 0);
888 * Iterate over all connected neighbours.
890 * @param cb function to call
891 * @param cb_cls closure for cb
894 GST_neighbours_iterate (GST_NeighbourIterator cb,
897 struct IteratorContext ic;
901 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
908 * If we have an active connection to the given target, it must be shutdown.
910 * @param target peer to disconnect from
913 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
915 struct NeighbourMapEntry *n;
916 struct GNUNET_TRANSPORT_PluginFunctions *papi;
917 struct GNUNET_MessageHeader disconnect_msg;
919 n = lookup_neighbour (target);
920 disconnect_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
921 disconnect_msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
922 papi = GST_plugins_find (n->plugin_name);
924 papi->send (papi->cls,
926 (const void*) &disconnect_msg,
927 sizeof (struct GNUNET_MessageHeader),
928 UINT32_MAX /* priority */,
929 GNUNET_TIME_UNIT_FOREVER_REL,
935 disconnect_neighbour (n);
939 /* end of file gnunet-service-transport_neighbours.c */