2 This file is part of GNUnet.
3 Copyright (C) 2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file transport/gnunet-service-transport_ats.c
22 * @brief interfacing between transport and ATS service
23 * @author Christian Grothoff
26 #include "gnunet-service-transport.h"
27 #include "gnunet-service-transport_ats.h"
28 #include "gnunet-service-transport_manipulation.h"
29 #include "gnunet-service-transport_plugins.h"
30 #include "gnunet_ats_service.h"
33 * Log convenience function.
35 #define LOG(kind,...) GNUNET_log_from(kind, "transport-ats", __VA_ARGS__)
39 * Information we track for each address known to ATS.
45 * The address (with peer identity). Must never change
46 * while this struct is in the #p2a map.
48 struct GNUNET_HELLO_Address *address;
51 * Session (can be NULL)
53 struct GNUNET_ATS_Session *session;
56 * Record with ATS API for the address.
58 struct GNUNET_ATS_AddressRecord *ar;
61 * Performance properties of this address.
63 struct GNUNET_ATS_Properties properties;
66 * Time until when this address is blocked and should thus not be
67 * made available to ATS (@e ar should be NULL until this time).
68 * Used when transport determines that for some reason it
69 * (temporarily) cannot use an address, even though it has been
72 struct GNUNET_TIME_Absolute blocked;
75 * If an address is blocked as part of an exponential back-off,
76 * we track the current size of the backoff here.
78 struct GNUNET_TIME_Relative back_off;
81 * Task scheduled to unblock an ATS-blocked address at
82 * @e blocked time, or NULL if the address is not blocked
83 * (and thus @e ar is non-NULL).
85 struct GNUNET_SCHEDULER_Task *unblock_task;
88 * Set to #GNUNET_YES if the address has expired but we could
89 * not yet remove it because we still have a valid session.
97 * Map from peer identities to one or more `struct AddressInfo` values
100 static struct GNUNET_CONTAINER_MultiPeerMap *p2a;
103 * Number of blocked addresses.
105 static unsigned int num_blocked;
109 * Closure for #find_ai_cb() and #find_ai_no_session_cb().
115 * Session to look for (only used if the address is inbound).
117 struct GNUNET_ATS_Session *session;
120 * Address to look for.
122 const struct GNUNET_HELLO_Address *address;
125 * Where to store the result.
127 struct AddressInfo *ret;
133 * Provide an update on the `p2a` map size to statistics.
134 * This function should be called whenever the `p2a` map
138 publish_p2a_stat_update ()
140 GNUNET_STATISTICS_set (GST_stats,
141 gettext_noop ("# Addresses given to ATS"),
142 GNUNET_CONTAINER_multipeermap_size (p2a) - num_blocked,
144 GNUNET_STATISTICS_set (GST_stats,
145 "# blocked addresses",
152 * Find matching address info. Both the address and the session
153 * must match; note that expired addresses are still found (as
154 * the session kind-of keeps those alive).
156 * @param cls the `struct FindClosure`
157 * @param key which peer is this about
158 * @param value the `struct AddressInfo`
159 * @return #GNUNET_YES to continue to iterate, #GNUNET_NO if we found the value
162 find_ai_cb (void *cls,
163 const struct GNUNET_PeerIdentity *key,
166 struct FindClosure *fc = cls;
167 struct AddressInfo *ai = value;
170 GNUNET_HELLO_address_cmp (fc->address,
172 (fc->session == ai->session) )
182 * Find the address information struct for the
183 * given @a address and @a session.
185 * @param address address to look for
186 * @param session session to match for inbound connections
187 * @return NULL if this combination is unknown
189 static struct AddressInfo *
190 find_ai (const struct GNUNET_HELLO_Address *address,
191 struct GNUNET_ATS_Session *session)
193 struct FindClosure fc;
195 fc.address = address;
196 fc.session = session;
198 GNUNET_CONTAINER_multipeermap_get_multiple (p2a,
207 * Find matching address info, ignoring sessions and expired
210 * @param cls the `struct FindClosure`
211 * @param key which peer is this about
212 * @param value the `struct AddressInfo`
213 * @return #GNUNET_YES to continue to iterate, #GNUNET_NO if we found the value
216 find_ai_no_session_cb (void *cls,
217 const struct GNUNET_PeerIdentity *key,
220 struct FindClosure *fc = cls;
221 struct AddressInfo *ai = value;
224 return GNUNET_YES; /* expired do not count here */
226 GNUNET_HELLO_address_cmp (fc->address,
237 * Find the address information struct for the
238 * given address (ignoring sessions)
240 * @param address address to look for
241 * @return NULL if this combination is unknown
243 static struct AddressInfo *
244 find_ai_no_session (const struct GNUNET_HELLO_Address *address)
246 struct FindClosure fc;
248 fc.address = address;
251 GNUNET_CONTAINER_multipeermap_get_multiple (p2a,
253 &find_ai_no_session_cb,
260 * Test if ATS knows about this @a address and @a session.
261 * Note that even if the address is expired, we return
262 * #GNUNET_YES if the respective session matches.
264 * @param address the address
265 * @param session the session
266 * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not.
269 GST_ats_is_known (const struct GNUNET_HELLO_Address *address,
270 struct GNUNET_ATS_Session *session)
272 return (NULL != find_ai (address, session)) ? GNUNET_YES : GNUNET_NO;
277 * Test if ATS knows about this @a address. Note that
278 * expired addresses do not count.
280 * @param address the address
281 * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not.
284 GST_ats_is_known_no_session (const struct GNUNET_HELLO_Address *address)
286 return (NULL != find_ai_no_session (address))
293 * The blocking time for an address has expired, allow ATS to
296 * @param cls the `struct AddressInfo` of the address to unblock
299 unblock_address (void *cls)
301 struct AddressInfo *ai = cls;
303 ai->unblock_task = NULL;
304 LOG (GNUNET_ERROR_TYPE_DEBUG,
305 "Unblocking address %s of peer %s\n",
306 GST_plugins_a2s (ai->address),
307 GNUNET_i2s (&ai->address->peer));
308 ai->ar = GNUNET_ATS_address_add (GST_ats,
312 GNUNET_break (NULL != ai->ar);
314 publish_p2a_stat_update ();
319 * Temporarily block a valid address for use by ATS for address
320 * suggestions. This function should be called if an address was
321 * suggested by ATS but failed to perform (i.e. failure to establish a
322 * session or to exchange the PING/PONG).
324 * @param address the address to block
325 * @param session the session (can be NULL)
328 GST_ats_block_address (const struct GNUNET_HELLO_Address *address,
329 struct GNUNET_ATS_Session *session)
331 struct AddressInfo *ai;
334 memcmp (&GST_my_identity,
336 sizeof (struct GNUNET_PeerIdentity)))
337 return; /* our own, ignore! */
338 ai = find_ai (address,
347 /* already blocked, how did it get used!? */
351 ai->back_off = GNUNET_TIME_STD_BACKOFF (ai->back_off);
353 GNUNET_HELLO_address_check_option (address,
354 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
355 LOG (GNUNET_ERROR_TYPE_DEBUG,
356 "Removing address %s of peer %s from use (inbound died)\n",
357 GST_plugins_a2s (address),
358 GNUNET_i2s (&address->peer));
360 LOG (GNUNET_ERROR_TYPE_INFO,
361 "Blocking address %s of peer %s from use for %s\n",
362 GST_plugins_a2s (address),
363 GNUNET_i2s (&address->peer),
364 GNUNET_STRINGS_relative_time_to_string (ai->back_off,
366 /* destroy session and address */
367 if ( (NULL == session) ||
369 GNUNET_ATS_address_del_session (ai->ar,
372 GNUNET_ATS_address_destroy (ai->ar);
374 /* "ar" has been freed, regardless how the branch
375 above played out: it was either freed in
376 #GNUNET_ATS_address_del_session() because it was
377 incoming, or explicitly in
378 #GNUNET_ATS_address_del_session(). */
381 /* determine when the address should come back to life */
382 ai->blocked = GNUNET_TIME_relative_to_absolute (ai->back_off);
383 ai->unblock_task = GNUNET_SCHEDULER_add_delayed (ai->back_off,
387 publish_p2a_stat_update ();
392 * Reset address blocking time. Resets the exponential
393 * back-off timer for this address to zero. Called when
394 * an address was used to create a successful connection.
396 * @param address the address to reset the blocking timer
397 * @param session the session (can be NULL)
400 GST_ats_block_reset (const struct GNUNET_HELLO_Address *address,
401 struct GNUNET_ATS_Session *session)
403 struct AddressInfo *ai;
406 memcmp (&GST_my_identity,
408 sizeof (struct GNUNET_PeerIdentity)))
409 return; /* our own, ignore! */
410 ai = find_ai (address, session);
416 /* address is in successful use, so it should not be blocked right now */
417 GNUNET_break (NULL == ai->unblock_task);
418 ai->back_off = GNUNET_TIME_UNIT_ZERO;
423 * Notify ATS about a new inbound @a address. The @a address in
424 * combination with the @a session must be new, but this function will
425 * perform a santiy check. If the @a address is indeed new, make it
428 * @param address the address
429 * @param session the session
430 * @param prop performance information
433 GST_ats_add_inbound_address (const struct GNUNET_HELLO_Address *address,
434 struct GNUNET_ATS_Session *session,
435 const struct GNUNET_ATS_Properties *prop)
437 struct GNUNET_ATS_AddressRecord *ar;
438 struct AddressInfo *ai;
441 memcmp (&GST_my_identity,
443 sizeof (struct GNUNET_PeerIdentity)))
444 return; /* our own, ignore! */
446 /* Sanity checks for a valid inbound address */
447 if (NULL == address->transport_name)
452 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
453 GNUNET_assert (GNUNET_YES ==
454 GNUNET_HELLO_address_check_option (address,
455 GNUNET_HELLO_ADDRESS_INFO_INBOUND));
456 GNUNET_assert (NULL != session);
457 ai = find_ai (address, session);
460 /* This should only be called for new sessions, and thus
461 we should not already have the address */
465 /* Is indeed new, let's tell ATS */
466 LOG (GNUNET_ERROR_TYPE_DEBUG,
467 "Notifying ATS about peer `%s''s new inbound address `%s' session %p in network %s\n",
468 GNUNET_i2s (&address->peer),
469 GST_plugins_a2s (address),
471 GNUNET_ATS_print_network_type (prop->scope));
472 ar = GNUNET_ATS_address_add (GST_ats,
476 GNUNET_assert (NULL != ar);
477 ai = GNUNET_new (struct AddressInfo);
478 ai->address = GNUNET_HELLO_address_copy (address);
479 ai->session = session;
480 ai->properties = *prop;
482 (void) GNUNET_CONTAINER_multipeermap_put (p2a,
485 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
486 publish_p2a_stat_update ();
491 * Notify ATS about the new address including the network this address is
492 * located in. The address must NOT be inbound and must be new to ATS.
494 * @param address the address
495 * @param prop performance information
498 GST_ats_add_address (const struct GNUNET_HELLO_Address *address,
499 const struct GNUNET_ATS_Properties *prop)
501 struct GNUNET_ATS_AddressRecord *ar;
502 struct AddressInfo *ai;
505 memcmp (&GST_my_identity,
507 sizeof (struct GNUNET_PeerIdentity)))
508 return; /* our own, ignore! */
509 /* validadte address */
510 if (NULL == address->transport_name)
515 GNUNET_assert (GNUNET_YES !=
516 GNUNET_HELLO_address_check_option (address,
517 GNUNET_HELLO_ADDRESS_INFO_INBOUND));
518 ai = find_ai_no_session (address);
519 GNUNET_assert (NULL == ai);
520 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
522 /* address seems sane, let's tell ATS */
523 LOG (GNUNET_ERROR_TYPE_INFO,
524 "Notifying ATS about peer %s's new address `%s'\n",
525 GNUNET_i2s (&address->peer),
526 GST_plugins_a2s (address));
527 ar = GNUNET_ATS_address_add (GST_ats,
531 GNUNET_assert (NULL != ar);
532 ai = GNUNET_new (struct AddressInfo);
533 ai->address = GNUNET_HELLO_address_copy (address);
535 ai->properties = *prop;
536 (void) GNUNET_CONTAINER_multipeermap_put (p2a,
539 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
540 publish_p2a_stat_update ();
545 * Notify ATS about a new @a session now existing for the given
546 * @a address. Essentially, an outbound @a address was used
547 * to establish a @a session. It is safe to call this function
548 * repeatedly for the same @a address and @a session pair.
550 * @param address the address
551 * @param session the session
554 GST_ats_new_session (const struct GNUNET_HELLO_Address *address,
555 struct GNUNET_ATS_Session *session)
557 struct AddressInfo *ai;
560 memcmp (&GST_my_identity,
562 sizeof (struct GNUNET_PeerIdentity)))
563 return; /* our own, ignore! */
564 ai = find_ai (address, NULL);
567 /* We may simply already be aware of the session, even if some
568 other part of the code could not tell if it just created a new
569 session or just got one recycled from the plugin; hence, we may
570 be called with "new" session even for an "old" session; in that
571 case, check that this is the case, but just ignore it. */
572 GNUNET_assert (NULL != (find_ai (address, session)));
575 GNUNET_assert (NULL == ai->session);
576 ai->session = session;
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "Telling ATS about new session for peer %s\n",
579 GNUNET_i2s (&address->peer));
580 /* Note that the address might currently be blocked; we only
581 tell ATS about the session if the address is currently not
582 blocked; otherwise, ATS will be told about the session on
585 GNUNET_ATS_address_add_session (ai->ar,
588 GNUNET_assert (NULL != ai->unblock_task);
593 * Release memory used by the given address data.
595 * @param ai the `struct AddressInfo`
598 destroy_ai (struct AddressInfo *ai)
600 GNUNET_assert (NULL == ai->session);
601 if (NULL != ai->unblock_task)
603 GNUNET_SCHEDULER_cancel (ai->unblock_task);
604 ai->unblock_task = NULL;
607 GNUNET_assert (GNUNET_YES ==
608 GNUNET_CONTAINER_multipeermap_remove (p2a,
611 LOG (GNUNET_ERROR_TYPE_DEBUG,
612 "Telling ATS to destroy address from peer %s\n",
613 GNUNET_i2s (&ai->address->peer));
616 GNUNET_ATS_address_destroy (ai->ar);
619 publish_p2a_stat_update ();
620 GNUNET_HELLO_address_free (ai->address);
626 * Notify ATS that the @a session (but not the @a address) of
627 * a given @a address is no longer relevant. (The @a session
628 * went down.) This function may be called even if for the
629 * respective outbound address #GST_ats_new_session() was
630 * never called and thus the pair is unknown to ATS. In this
631 * case, the call is simply ignored.
633 * @param address the address
634 * @param session the session
637 GST_ats_del_session (const struct GNUNET_HELLO_Address *address,
638 struct GNUNET_ATS_Session *session)
640 struct AddressInfo *ai;
643 memcmp (&GST_my_identity,
645 sizeof (struct GNUNET_PeerIdentity)))
646 return; /* our own, ignore! */
652 ai = find_ai (address,
656 /* We sometimes create sessions just for sending a PING,
657 and if those are destroyed they were never known to
658 ATS which means we end up here (however, in this
659 case, the address must be an outbound address). */
660 GNUNET_break (GNUNET_YES !=
661 GNUNET_HELLO_address_check_option (address,
662 GNUNET_HELLO_ADDRESS_INFO_INBOUND));
665 GNUNET_assert (session == ai->session);
667 LOG (GNUNET_ERROR_TYPE_DEBUG,
668 "Telling ATS to destroy session %p from peer %s\n",
670 GNUNET_i2s (&address->peer));
671 if (GNUNET_YES == ai->expired)
673 /* last reason to keep this 'ai' around is now gone, the
674 session is dead as well, clean up */
677 /* Address expired but not blocked, and thus 'ar' was still
678 live because of the session; deleting just the session
679 will do for an inbound session, but for an outbound we
680 then also need to destroy the address with ATS. */
682 GNUNET_ATS_address_del_session (ai->ar,
685 GNUNET_ATS_address_destroy (ai->ar);
687 /* "ar" has been freed, regardless how the branch
688 above played out: it was either freed in
689 #GNUNET_ATS_address_del_session() because it was
690 incoming, or explicitly in
691 #GNUNET_ATS_address_del_session(). */
700 /* If ATS doesn't know about the address/session, this means
701 this address was blocked. */
703 GNUNET_HELLO_address_check_option (address,
704 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
706 /* This was a blocked inbound session, which now lost the
707 session. But inbound addresses are by themselves useless,
708 so we must forget about the address as well. */
712 /* Otherwise, we are done as we have set `ai->session` to NULL
713 already and ATS will simply not be told about the session when
714 the connection is unblocked and the outbound address becomes
715 available again. . */
719 /* This is the "simple" case where ATS knows about the session and
720 the address is neither blocked nor expired. Delete the session,
721 and if it was inbound, free the address as well. */
723 GNUNET_ATS_address_del_session (ai->ar,
726 /* This was an inbound address, the session is now gone, so we
727 need to also forget about the address itself. */
735 * Notify ATS about DV @a distance change to an @a address.
736 * Does nothing if the @a address is not known to us.
738 * @param address the address
739 * @param distance new distance value
742 GST_ats_update_distance (const struct GNUNET_HELLO_Address *address,
745 struct AddressInfo *ai;
747 ai = find_ai_no_session (address);
750 /* We do not know about this address, do nothing. */
753 LOG (GNUNET_ERROR_TYPE_DEBUG,
754 "Updated distance for peer `%s' to %u\n",
755 GNUNET_i2s (&address->peer),
757 ai->properties.distance = distance;
758 /* Give manipulation its chance to change metrics */
759 GST_manipulation_manipulate_metrics (address,
762 /* Address may be blocked, only give ATS if address is
765 GNUNET_ATS_address_update (ai->ar,
771 * Notify ATS about @a delay changes to properties of an @a address.
772 * Does nothing if the @a address is not known to us.
774 * @param address the address
775 * @param delay new delay value
778 GST_ats_update_delay (const struct GNUNET_HELLO_Address *address,
779 struct GNUNET_TIME_Relative delay)
781 struct AddressInfo *ai;
783 ai = find_ai_no_session (address);
786 /* We do not know about this address, do nothing. */
789 LOG (GNUNET_ERROR_TYPE_DEBUG,
790 "Updated latency for peer `%s' to %s\n",
791 GNUNET_i2s (&address->peer),
792 GNUNET_STRINGS_relative_time_to_string (delay,
794 ai->properties.delay = delay;
795 /* Give manipulation its chance to change metrics */
796 GST_manipulation_manipulate_metrics (address,
799 /* Address may be blocked, only give ATS if address is
802 GNUNET_ATS_address_update (ai->ar,
808 * Notify ATS about utilization changes to an @a address.
809 * Does nothing if the @a address is not known to us.
811 * @param address our information about the address
812 * @param bps_in new utilization inbound
813 * @param bps_out new utilization outbound
816 GST_ats_update_utilization (const struct GNUNET_HELLO_Address *address,
820 struct AddressInfo *ai;
822 ai = find_ai_no_session (address);
825 /* We do not know about this address, do nothing. */
828 LOG (GNUNET_ERROR_TYPE_DEBUG,
829 "Updating utilization for peer `%s' address %s: %u/%u\n",
830 GNUNET_i2s (&address->peer),
831 GST_plugins_a2s (address),
832 (unsigned int) bps_in,
833 (unsigned int) bps_out);
834 ai->properties.utilization_in = bps_in;
835 ai->properties.utilization_out = bps_out;
836 /* Give manipulation its chance to change metrics */
837 GST_manipulation_manipulate_metrics (address,
840 /* Address may be blocked, only give ATS if address is
843 GNUNET_ATS_address_update (ai->ar,
849 * Notify ATS that the address has expired and thus cannot
850 * be used any longer. This function must only be called
851 * if the corresponding session is already gone.
853 * @param address the address
856 GST_ats_expire_address (const struct GNUNET_HELLO_Address *address)
858 struct AddressInfo *ai;
861 memcmp (&GST_my_identity,
863 sizeof (struct GNUNET_PeerIdentity)))
864 return; /* our own, ignore! */
865 LOG (GNUNET_ERROR_TYPE_DEBUG,
866 "Address %s of peer %s expired\n",
867 GST_plugins_a2s (address),
868 GNUNET_i2s (&address->peer));
869 ai = find_ai_no_session (address);
875 if (NULL != ai->session)
877 /* Got an active session, just remember the expiration
878 and act upon it when the session goes down. */
879 ai->expired = GNUNET_YES;
882 /* Address expired, no session, free resources */
888 * Initialize ATS subsystem.
893 p2a = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_YES);
898 * Release memory used by the given address data.
901 * @param key which peer is this about
902 * @param value the `struct AddressInfo`
903 * @return #GNUNET_OK (continue to iterate)
906 destroy_ai_cb (void *cls,
907 const struct GNUNET_PeerIdentity *key,
910 struct AddressInfo *ai = value;
918 * Shutdown ATS subsystem.
923 GNUNET_CONTAINER_multipeermap_iterate (p2a,
926 publish_p2a_stat_update ();
927 GNUNET_CONTAINER_multipeermap_destroy (p2a);
931 /* end of gnunet-service-transport_ats.c */