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 peer identity of the peer to switch the address for
521 * @param plugin_name name of transport that delivered the PONG
522 * @param address address of the other peer, NULL if other peer
524 * @param address_len number of bytes in address
525 * @param session session to use (or NULL)
526 * @param ats performance data
527 * @param ats_count number of entries in ats (excluding 0-termination)
530 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
531 const char *plugin_name,
534 struct Session *session,
535 const struct GNUNET_TRANSPORT_ATS_Information *ats,
538 struct NeighbourMapEntry *n;
539 struct GNUNET_MessageHeader connect_msg;
541 n = lookup_neighbour (peer);
547 GNUNET_free_non_null (n->addr);
548 n->addr = GNUNET_malloc (address_len);
549 memcpy (n->addr, address, address_len);
550 n->addrlen = address_len;
551 n->session = session;
552 GNUNET_array_grow (n->ats,
557 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
558 GNUNET_free_non_null (n->plugin_name);
559 n->plugin_name = GNUNET_strdup (plugin_name);
560 GNUNET_SCHEDULER_cancel (n->timeout_task);
562 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
563 &neighbour_timeout_task, n);
564 connect_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
565 connect_msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
566 GST_neighbours_send (peer,
568 sizeof (connect_msg),
569 GNUNET_TIME_UNIT_FOREVER_REL,
575 * Try to connect to the target peer using the given address
577 * @param cls the 'struct NeighbourMapEntry' of the target
578 * @param target identity of the target peer
579 * @param plugin_name name of the plugin
580 * @param plugin_address binary address
581 * @param plugin_address_len length of address
582 * @param bandwidth available bandwidth
583 * @param ats performance data for the address (as far as known)
584 * @param ats_count number of performance records in 'ats'
587 try_connect_using_address (void *cls,
588 const struct GNUNET_PeerIdentity *target,
589 const char *plugin_name,
590 const void *plugin_address,
591 size_t plugin_address_len,
592 struct GNUNET_BANDWIDTH_Value32NBO bandwidth,
593 const struct GNUNET_TRANSPORT_ATS_Information *ats,
596 struct NeighbourMapEntry *n = cls;
599 GST_neighbours_switch_to_address (target,
605 if (GNUNET_YES == n->is_connected)
607 n->is_connected = GNUNET_YES;
608 connect_notify_cb (callback_cls,
616 * Try to create a connection to the given target (eventually).
618 * @param target peer to try to connect to
621 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
623 struct NeighbourMapEntry *n;
625 GNUNET_assert (0 != memcmp (target,
627 sizeof (struct GNUNET_PeerIdentity)));
628 n = lookup_neighbour (target);
630 (GNUNET_YES == n->is_connected) )
631 return; /* already connected */
634 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
636 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
637 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
638 MAX_BANDWIDTH_CARRY_S);
639 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
640 &neighbour_timeout_task, n);
641 GNUNET_assert (GNUNET_OK ==
642 GNUNET_CONTAINER_multihashmap_put (neighbours,
645 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
648 return; /* already trying */
649 n->asc = GNUNET_ATS_suggest_address (GST_ats,
651 &try_connect_using_address,
657 * Test if we're connected to the given peer.
659 * @param target peer to test
660 * @return GNUNET_YES if we are connected, GNUNET_NO if not
663 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
665 struct NeighbourMapEntry *n;
667 n = lookup_neighbour (target);
669 (n->is_connected == GNUNET_YES) )
670 return GNUNET_NO; /* not connected */
676 * A session was terminated. Take note.
678 * @param peer identity of the peer where the session died
679 * @param session session that is gone
682 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
683 struct Session *session)
685 struct NeighbourMapEntry *n;
687 n = lookup_neighbour (peer);
690 if (session != n->session)
691 return; /* doesn't affect us */
693 if (GNUNET_YES != n->is_connected)
694 return; /* not connected anymore anyway, shouldn't matter */
695 /* try QUICKLY to re-establish a connection, reduce timeout! */
698 /* how can this be!? */
702 GNUNET_SCHEDULER_cancel (n->timeout_task);
703 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
704 &neighbour_timeout_task, n);
705 n->asc = GNUNET_ATS_suggest_address (GST_ats,
707 &try_connect_using_address,
713 * Transmit a message to the given target using the active connection.
715 * @param target destination
716 * @param msg message to send
717 * @param msg_size number of bytes in msg
718 * @param timeout when to fail with timeout
719 * @param cont function to call when done
720 * @param cont_cls closure for 'cont'
723 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
726 struct GNUNET_TIME_Relative timeout,
727 GST_NeighbourSendContinuation cont,
730 struct NeighbourMapEntry *n;
731 struct MessageQueue *mq;
733 n = lookup_neighbour (target);
735 (GNUNET_YES != n->is_connected) )
737 GNUNET_STATISTICS_update (GST_stats,
738 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
746 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
747 GNUNET_STATISTICS_update (GST_stats,
748 gettext_noop ("# bytes in message queue for other peers"),
751 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
753 mq->cont_cls = cont_cls;
754 /* FIXME: this memcpy can be up to 7% of our total runtime! */
755 memcpy (&mq[1], msg, msg_size);
756 mq->message_buf = (const char*) &mq[1];
757 mq->message_buf_size = msg_size;
758 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
759 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head,
762 if ( (GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
763 (NULL == n->is_active) )
764 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task,
770 * We have received a message from the given sender. How long should
771 * we delay before receiving more? (Also used to keep the peer marked
774 * @param sender sender of the message
775 * @param size size of the message
776 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
777 * GNUNET_NO if the neighbour is not connected or violates the quota
778 * @return how long to wait before reading more from this sender
780 struct GNUNET_TIME_Relative
781 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
785 struct NeighbourMapEntry *n;
786 struct GNUNET_TIME_Relative ret;
788 n = lookup_neighbour (sender);
791 *do_forward = GNUNET_NO;
792 return GNUNET_TIME_UNIT_ZERO;
794 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
797 n->quota_violation_count++;
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
801 n->in_tracker.available_bytes_per_s__,
802 n->quota_violation_count);
804 /* Discount 32k per violation */
805 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
810 if (n->quota_violation_count > 0)
812 /* try to add 32k back */
813 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
815 n->quota_violation_count--;
818 GNUNET_SCHEDULER_cancel (n->timeout_task);
820 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
821 &neighbour_timeout_task, n);
822 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
824 GNUNET_STATISTICS_update (GST_stats,
825 gettext_noop ("# bandwidth quota violations by other peers"),
828 *do_forward = GNUNET_NO;
829 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
831 *do_forward = GNUNET_YES;
832 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
833 if (ret.rel_value > 0)
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
837 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
838 (unsigned long long) n->in_tracker.consumption_since_last_update__,
839 (unsigned int) n->in_tracker.available_bytes_per_s__,
840 (unsigned long long) ret.rel_value);
842 GNUNET_STATISTICS_update (GST_stats,
843 gettext_noop ("# ms throttling suggested"),
844 (int64_t) ret.rel_value,
852 * Change the incoming quota for the given peer.
854 * @param neighbour identity of peer to change qutoa for
855 * @param quota new quota
858 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
859 struct GNUNET_BANDWIDTH_Value32NBO quota)
861 struct NeighbourMapEntry *n;
863 n = lookup_neighbour (neighbour);
866 GNUNET_STATISTICS_update (GST_stats,
867 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
872 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
874 if (0 != ntohl (quota.value__))
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "Disconnecting peer `%4s' due to `%s'\n",
882 GNUNET_STATISTICS_update (GST_stats,
883 gettext_noop ("# disconnects due to quota of 0"),
886 disconnect_neighbour (n);
891 * Closure for the neighbours_iterate function.
893 struct IteratorContext
896 * Function to call on each connected neighbour.
898 GST_NeighbourIterator cb;
908 * Call the callback from the closure for each connected neighbour.
910 * @param cls the 'struct IteratorContext'
911 * @param key the hash of the public key of the neighbour
912 * @param value the 'struct NeighbourMapEntry'
913 * @return GNUNET_OK (continue to iterate)
916 neighbours_iterate (void *cls,
917 const GNUNET_HashCode *key,
920 struct IteratorContext *ic = cls;
921 struct NeighbourMapEntry *n = value;
923 if (GNUNET_YES != n->is_connected)
925 GNUNET_assert (n->ats_count > 0);
935 * Iterate over all connected neighbours.
937 * @param cb function to call
938 * @param cb_cls closure for cb
941 GST_neighbours_iterate (GST_NeighbourIterator cb,
944 struct IteratorContext ic;
948 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
955 * If we have an active connection to the given target, it must be shutdown.
957 * @param target peer to disconnect from
960 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
962 struct NeighbourMapEntry *n;
963 struct GNUNET_TRANSPORT_PluginFunctions *papi;
964 struct GNUNET_MessageHeader disconnect_msg;
966 n = lookup_neighbour (target);
968 return; /* not active */
969 if (GNUNET_YES == n->is_connected)
971 /* we're actually connected, send DISCONNECT message */
972 disconnect_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
973 disconnect_msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
974 papi = GST_plugins_find (n->plugin_name);
976 papi->send (papi->cls,
978 (const void*) &disconnect_msg,
979 sizeof (struct GNUNET_MessageHeader),
980 UINT32_MAX /* priority */,
981 GNUNET_TIME_UNIT_FOREVER_REL,
988 disconnect_neighbour (n);
992 /* end of file gnunet-service-transport_neighbours.c */