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 * A session was terminated. Take note.
519 * @param peer identity of the peer where the session died
520 * @param session session that is gone
523 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
524 struct Session *session)
526 struct NeighbourMapEntry *n;
528 n = lookup_neighbour (peer);
531 if (session == n->session)
537 * For an existing neighbour record, set the active connection to
540 * @param peer identity of the peer to switch the address for
541 * @param plugin_name name of transport that delivered the PONG
542 * @param address address of the other peer, NULL if other peer
544 * @param address_len number of bytes in address
545 * @param session session to use (or NULL)
546 * @param ats performance data
547 * @param ats_count number of entries in ats (excluding 0-termination)
550 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
551 const char *plugin_name,
554 struct Session *session,
555 const struct GNUNET_TRANSPORT_ATS_Information *ats,
558 struct NeighbourMapEntry *n;
559 struct GNUNET_MessageHeader connect_msg;
561 n = lookup_neighbour (peer);
567 GNUNET_free_non_null (n->addr);
568 n->addr = GNUNET_malloc (address_len);
569 memcpy (n->addr, address, address_len);
570 n->addrlen = address_len;
571 n->session = session;
572 GNUNET_array_grow (n->ats,
577 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
578 GNUNET_free_non_null (n->plugin_name);
579 n->plugin_name = GNUNET_strdup (plugin_name);
580 GNUNET_SCHEDULER_cancel (n->timeout_task);
582 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
583 &neighbour_timeout_task, n);
584 connect_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
585 connect_msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
586 GST_neighbours_send (peer,
588 sizeof (connect_msg),
589 GNUNET_TIME_UNIT_FOREVER_REL,
595 * Try to connect to the target peer using the given address
597 * @param cls the 'struct NeighbourMapEntry' of the target
598 * @param target identity of the target peer
599 * @param plugin_name name of the plugin
600 * @param plugin_address binary address
601 * @param plugin_address_len length of address
602 * @param bandwidth available bandwidth
603 * @param ats performance data for the address (as far as known)
604 * @param ats_count number of performance records in 'ats'
607 try_connect_using_address (void *cls,
608 const struct GNUNET_PeerIdentity *target,
609 const char *plugin_name,
610 const void *plugin_address,
611 size_t plugin_address_len,
612 struct GNUNET_BANDWIDTH_Value32NBO bandwidth,
613 const struct GNUNET_TRANSPORT_ATS_Information *ats,
616 struct NeighbourMapEntry *n = cls;
619 GST_neighbours_switch_to_address (target,
625 if (GNUNET_YES == n->is_connected)
627 n->is_connected = GNUNET_YES;
628 connect_notify_cb (callback_cls,
636 * Try to create a connection to the given target (eventually).
638 * @param target peer to try to connect to
641 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
643 struct NeighbourMapEntry *n;
645 GNUNET_assert (0 != memcmp (target,
647 sizeof (struct GNUNET_PeerIdentity)));
648 n = lookup_neighbour (target);
650 (GNUNET_YES == n->is_connected) )
651 return; /* already connected */
654 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
656 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
657 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
658 MAX_BANDWIDTH_CARRY_S);
659 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
660 &neighbour_timeout_task, n);
661 GNUNET_assert (GNUNET_OK ==
662 GNUNET_CONTAINER_multihashmap_put (neighbours,
665 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
668 return; /* already trying */
669 n->asc = GNUNET_ATS_suggest_address (GST_ats,
671 &try_connect_using_address,
677 * Test if we're connected to the given peer.
679 * @param target peer to test
680 * @return GNUNET_YES if we are connected, GNUNET_NO if not
683 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
685 struct NeighbourMapEntry *n;
687 n = lookup_neighbour (target);
689 (n->is_connected == GNUNET_YES) )
690 return GNUNET_NO; /* not connected */
696 * Transmit a message to the given target using the active connection.
698 * @param target destination
699 * @param msg message to send
700 * @param msg_size number of bytes in msg
701 * @param timeout when to fail with timeout
702 * @param cont function to call when done
703 * @param cont_cls closure for 'cont'
706 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
709 struct GNUNET_TIME_Relative timeout,
710 GST_NeighbourSendContinuation cont,
713 struct NeighbourMapEntry *n;
714 struct MessageQueue *mq;
716 n = lookup_neighbour (target);
718 (GNUNET_YES != n->is_connected) )
720 GNUNET_STATISTICS_update (GST_stats,
721 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
729 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
730 GNUNET_STATISTICS_update (GST_stats,
731 gettext_noop ("# bytes in message queue for other peers"),
734 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
736 mq->cont_cls = cont_cls;
737 /* FIXME: this memcpy can be up to 7% of our total runtime! */
738 memcpy (&mq[1], msg, msg_size);
739 mq->message_buf = (const char*) &mq[1];
740 mq->message_buf_size = msg_size;
741 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
742 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head,
745 if ( (GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
746 (NULL == n->is_active) )
747 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task,
753 * We have received a message from the given sender. How long should
754 * we delay before receiving more? (Also used to keep the peer marked
757 * @param sender sender of the message
758 * @param size size of the message
759 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
760 * GNUNET_NO if the neighbour is not connected or violates the quota
761 * @return how long to wait before reading more from this sender
763 struct GNUNET_TIME_Relative
764 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
768 struct NeighbourMapEntry *n;
769 struct GNUNET_TIME_Relative ret;
771 n = lookup_neighbour (sender);
774 *do_forward = GNUNET_NO;
775 return GNUNET_TIME_UNIT_ZERO;
777 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
780 n->quota_violation_count++;
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
784 n->in_tracker.available_bytes_per_s__,
785 n->quota_violation_count);
787 /* Discount 32k per violation */
788 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
793 if (n->quota_violation_count > 0)
795 /* try to add 32k back */
796 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
798 n->quota_violation_count--;
801 GNUNET_SCHEDULER_cancel (n->timeout_task);
803 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
804 &neighbour_timeout_task, n);
805 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
807 GNUNET_STATISTICS_update (GST_stats,
808 gettext_noop ("# bandwidth quota violations by other peers"),
811 *do_forward = GNUNET_NO;
812 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
814 *do_forward = GNUNET_YES;
815 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
816 if (ret.rel_value > 0)
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
820 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
821 (unsigned long long) n->in_tracker.consumption_since_last_update__,
822 (unsigned int) n->in_tracker.available_bytes_per_s__,
823 (unsigned long long) ret.rel_value);
825 GNUNET_STATISTICS_update (GST_stats,
826 gettext_noop ("# ms throttling suggested"),
827 (int64_t) ret.rel_value,
835 * Change the incoming quota for the given peer.
837 * @param neighbour identity of peer to change qutoa for
838 * @param quota new quota
841 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
842 struct GNUNET_BANDWIDTH_Value32NBO quota)
844 struct NeighbourMapEntry *n;
846 n = lookup_neighbour (neighbour);
849 GNUNET_STATISTICS_update (GST_stats,
850 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
855 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
857 if (0 != ntohl (quota.value__))
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "Disconnecting peer `%4s' due to `%s'\n",
865 GNUNET_STATISTICS_update (GST_stats,
866 gettext_noop ("# disconnects due to quota of 0"),
869 disconnect_neighbour (n);
874 * Closure for the neighbours_iterate function.
876 struct IteratorContext
879 * Function to call on each connected neighbour.
881 GST_NeighbourIterator cb;
891 * Call the callback from the closure for each connected neighbour.
893 * @param cls the 'struct IteratorContext'
894 * @param key the hash of the public key of the neighbour
895 * @param value the 'struct NeighbourMapEntry'
896 * @return GNUNET_OK (continue to iterate)
899 neighbours_iterate (void *cls,
900 const GNUNET_HashCode *key,
903 struct IteratorContext *ic = cls;
904 struct NeighbourMapEntry *n = value;
906 if (GNUNET_YES != n->is_connected)
908 GNUNET_assert (n->ats_count > 0);
918 * Iterate over all connected neighbours.
920 * @param cb function to call
921 * @param cb_cls closure for cb
924 GST_neighbours_iterate (GST_NeighbourIterator cb,
927 struct IteratorContext ic;
931 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
938 * If we have an active connection to the given target, it must be shutdown.
940 * @param target peer to disconnect from
943 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
945 struct NeighbourMapEntry *n;
946 struct GNUNET_TRANSPORT_PluginFunctions *papi;
947 struct GNUNET_MessageHeader disconnect_msg;
949 n = lookup_neighbour (target);
950 disconnect_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
951 disconnect_msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
952 papi = GST_plugins_find (n->plugin_name);
954 papi->send (papi->cls,
956 (const void*) &disconnect_msg,
957 sizeof (struct GNUNET_MessageHeader),
958 UINT32_MAX /* priority */,
959 GNUNET_TIME_UNIT_FOREVER_REL,
965 disconnect_neighbour (n);
969 /* end of file gnunet-service-transport_neighbours.c */