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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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.
44 * The address (with peer identity). Must never change
45 * while this struct is in the #p2a map.
47 struct GNUNET_HELLO_Address *address;
50 * Session (can be NULL)
52 struct GNUNET_ATS_Session *session;
55 * Record with ATS API for the address.
57 struct GNUNET_ATS_AddressRecord *ar;
60 * Performance properties of this address.
62 struct GNUNET_ATS_Properties properties;
65 * Time until when this address is blocked and should thus not be
66 * made available to ATS (@e ar should be NULL until this time).
67 * Used when transport determines that for some reason it
68 * (temporarily) cannot use an address, even though it has been
71 struct GNUNET_TIME_Absolute blocked;
74 * If an address is blocked as part of an exponential back-off,
75 * we track the current size of the backoff here.
77 struct GNUNET_TIME_Relative back_off;
80 * Task scheduled to unblock an ATS-blocked address at
81 * @e blocked time, or NULL if the address is not blocked
82 * (and thus @e ar is non-NULL).
84 struct GNUNET_SCHEDULER_Task *unblock_task;
87 * Set to #GNUNET_YES if the address has expired but we could
88 * not yet remove it because we still have a valid session.
95 * Map from peer identities to one or more `struct AddressInfo` values
98 static struct GNUNET_CONTAINER_MultiPeerMap *p2a;
101 * Number of blocked addresses.
103 static unsigned int num_blocked;
107 * Closure for #find_ai_cb() and #find_ai_no_session_cb().
112 * Session to look for (only used if the address is inbound).
114 struct GNUNET_ATS_Session *session;
117 * Address to look for.
119 const struct GNUNET_HELLO_Address *address;
122 * Where to store the result.
124 struct AddressInfo *ret;
129 * Provide an update on the `p2a` map size to statistics.
130 * This function should be called whenever the `p2a` map
134 publish_p2a_stat_update ()
136 GNUNET_STATISTICS_set (GST_stats,
137 gettext_noop ("# Addresses given to ATS"),
138 GNUNET_CONTAINER_multipeermap_size (p2a) - num_blocked,
140 GNUNET_STATISTICS_set (GST_stats,
141 "# blocked addresses",
148 * Find matching address info. Both the address and the session
149 * must match; note that expired addresses are still found (as
150 * the session kind-of keeps those alive).
152 * @param cls the `struct FindClosure`
153 * @param key which peer is this about
154 * @param value the `struct AddressInfo`
155 * @return #GNUNET_YES to continue to iterate, #GNUNET_NO if we found the value
158 find_ai_cb (void *cls,
159 const struct GNUNET_PeerIdentity *key,
162 struct FindClosure *fc = cls;
163 struct AddressInfo *ai = value;
166 GNUNET_HELLO_address_cmp (fc->address,
168 (fc->session == ai->session))
178 * Find the address information struct for the
179 * given @a address and @a session.
181 * @param address address to look for
182 * @param session session to match for inbound connections
183 * @return NULL if this combination is unknown
185 static struct AddressInfo *
186 find_ai (const struct GNUNET_HELLO_Address *address,
187 struct GNUNET_ATS_Session *session)
189 struct FindClosure fc;
191 fc.address = address;
192 fc.session = session;
194 GNUNET_CONTAINER_multipeermap_get_multiple (p2a,
203 * Find matching address info, ignoring sessions and expired
206 * @param cls the `struct FindClosure`
207 * @param key which peer is this about
208 * @param value the `struct AddressInfo`
209 * @return #GNUNET_YES to continue to iterate, #GNUNET_NO if we found the value
212 find_ai_no_session_cb (void *cls,
213 const struct GNUNET_PeerIdentity *key,
216 struct FindClosure *fc = cls;
217 struct AddressInfo *ai = value;
220 return GNUNET_YES; /* expired do not count here */
222 GNUNET_HELLO_address_cmp (fc->address,
233 * Find the address information struct for the
234 * given address (ignoring sessions)
236 * @param address address to look for
237 * @return NULL if this combination is unknown
239 static struct AddressInfo *
240 find_ai_no_session (const struct GNUNET_HELLO_Address *address)
242 struct FindClosure fc;
244 fc.address = address;
247 GNUNET_CONTAINER_multipeermap_get_multiple (p2a,
249 &find_ai_no_session_cb,
256 * Test if ATS knows about this @a address and @a session.
257 * Note that even if the address is expired, we return
258 * #GNUNET_YES if the respective session matches.
260 * @param address the address
261 * @param session the session
262 * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not.
265 GST_ats_is_known (const struct GNUNET_HELLO_Address *address,
266 struct GNUNET_ATS_Session *session)
268 return (NULL != find_ai (address, session)) ? GNUNET_YES : GNUNET_NO;
273 * Test if ATS knows about this @a address. Note that
274 * expired addresses do not count.
276 * @param address the address
277 * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not.
280 GST_ats_is_known_no_session (const struct GNUNET_HELLO_Address *address)
282 return (NULL != find_ai_no_session (address))
289 * The blocking time for an address has expired, allow ATS to
292 * @param cls the `struct AddressInfo` of the address to unblock
295 unblock_address (void *cls)
297 struct AddressInfo *ai = cls;
299 ai->unblock_task = NULL;
300 LOG (GNUNET_ERROR_TYPE_DEBUG,
301 "Unblocking address %s of peer %s\n",
302 GST_plugins_a2s (ai->address),
303 GNUNET_i2s (&ai->address->peer));
304 ai->ar = GNUNET_ATS_address_add (GST_ats,
308 GNUNET_break (NULL != ai->ar);
310 publish_p2a_stat_update ();
315 * Temporarily block a valid address for use by ATS for address
316 * suggestions. This function should be called if an address was
317 * suggested by ATS but failed to perform (i.e. failure to establish a
318 * session or to exchange the PING/PONG).
320 * @param address the address to block
321 * @param session the session (can be NULL)
324 GST_ats_block_address (const struct GNUNET_HELLO_Address *address,
325 struct GNUNET_ATS_Session *session)
327 struct AddressInfo *ai;
330 memcmp (&GST_my_identity,
332 sizeof(struct GNUNET_PeerIdentity)))
333 return; /* our own, ignore! */
334 ai = find_ai (address,
336 if ((NULL == ai) || (NULL == ai->ar))
338 /* The address is already gone/blocked, this can happen during a blacklist
342 ai->back_off = GNUNET_TIME_STD_BACKOFF (ai->back_off);
344 GNUNET_HELLO_address_check_option (address,
345 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
346 LOG (GNUNET_ERROR_TYPE_DEBUG,
347 "Removing address %s of peer %s from use (inbound died)\n",
348 GST_plugins_a2s (address),
349 GNUNET_i2s (&address->peer));
351 LOG (GNUNET_ERROR_TYPE_INFO,
352 "Blocking address %s of peer %s from use for %s\n",
353 GST_plugins_a2s (address),
354 GNUNET_i2s (&address->peer),
355 GNUNET_STRINGS_relative_time_to_string (ai->back_off,
357 /* destroy session and address */
358 if ((NULL == session) ||
360 GNUNET_ATS_address_del_session (ai->ar,
363 GNUNET_ATS_address_destroy (ai->ar);
365 /* "ar" has been freed, regardless how the branch
366 above played out: it was either freed in
367 #GNUNET_ATS_address_del_session() because it was
368 incoming, or explicitly in
369 #GNUNET_ATS_address_del_session(). */ai->ar = NULL;
371 /* determine when the address should come back to life */
372 ai->blocked = GNUNET_TIME_relative_to_absolute (ai->back_off);
373 ai->unblock_task = GNUNET_SCHEDULER_add_delayed (ai->back_off,
377 publish_p2a_stat_update ();
382 * Reset address blocking time. Resets the exponential
383 * back-off timer for this address to zero. Called when
384 * an address was used to create a successful connection.
386 * @param address the address to reset the blocking timer
387 * @param session the session (can be NULL)
390 GST_ats_block_reset (const struct GNUNET_HELLO_Address *address,
391 struct GNUNET_ATS_Session *session)
393 struct AddressInfo *ai;
396 memcmp (&GST_my_identity,
398 sizeof(struct GNUNET_PeerIdentity)))
399 return; /* our own, ignore! */
400 ai = find_ai (address, session);
406 /* address is in successful use, so it should not be blocked right now */
407 GNUNET_break (NULL == ai->unblock_task);
408 ai->back_off = GNUNET_TIME_UNIT_ZERO;
413 * Notify ATS about a new inbound @a address. The @a address in
414 * combination with the @a session must be new, but this function will
415 * perform a santiy check. If the @a address is indeed new, make it
418 * @param address the address
419 * @param session the session
420 * @param prop performance information
423 GST_ats_add_inbound_address (const struct GNUNET_HELLO_Address *address,
424 struct GNUNET_ATS_Session *session,
425 const struct GNUNET_ATS_Properties *prop)
427 struct GNUNET_ATS_AddressRecord *ar;
428 struct AddressInfo *ai;
431 memcmp (&GST_my_identity,
433 sizeof(struct GNUNET_PeerIdentity)))
434 return; /* our own, ignore! */
436 /* Sanity checks for a valid inbound address */
437 if (NULL == address->transport_name)
442 GNUNET_break (GNUNET_NT_UNSPECIFIED != prop->scope);
443 GNUNET_assert (GNUNET_YES ==
444 GNUNET_HELLO_address_check_option (address,
445 GNUNET_HELLO_ADDRESS_INFO_INBOUND));
446 GNUNET_assert (NULL != session);
447 ai = find_ai (address, session);
450 /* This should only be called for new sessions, and thus
451 we should not already have the address */
455 /* Is indeed new, let's tell ATS */
456 LOG (GNUNET_ERROR_TYPE_DEBUG,
457 "Notifying ATS about peer `%s''s new inbound address `%s' session %p in network %s\n",
458 GNUNET_i2s (&address->peer),
459 GST_plugins_a2s (address),
461 GNUNET_NT_to_string (prop->scope));
462 ar = GNUNET_ATS_address_add (GST_ats,
466 GNUNET_assert (NULL != ar);
467 ai = GNUNET_new (struct AddressInfo);
468 ai->address = GNUNET_HELLO_address_copy (address);
469 ai->session = session;
470 ai->properties = *prop;
472 (void) GNUNET_CONTAINER_multipeermap_put (p2a,
475 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
476 publish_p2a_stat_update ();
481 * Notify ATS about the new address including the network this address is
482 * located in. The address must NOT be inbound and must be new to ATS.
484 * @param address the address
485 * @param prop performance information
488 GST_ats_add_address (const struct GNUNET_HELLO_Address *address,
489 const struct GNUNET_ATS_Properties *prop)
491 struct GNUNET_ATS_AddressRecord *ar;
492 struct AddressInfo *ai;
495 memcmp (&GST_my_identity,
497 sizeof(struct GNUNET_PeerIdentity)))
498 return; /* our own, ignore! */
499 /* validadte address */
500 if (NULL == address->transport_name)
505 GNUNET_assert (GNUNET_YES !=
506 GNUNET_HELLO_address_check_option (address,
507 GNUNET_HELLO_ADDRESS_INFO_INBOUND));
508 ai = find_ai_no_session (address);
509 GNUNET_assert (NULL == ai);
510 GNUNET_break (GNUNET_NT_UNSPECIFIED != prop->scope);
512 /* address seems sane, let's tell ATS */
513 LOG (GNUNET_ERROR_TYPE_INFO,
514 "Notifying ATS about peer %s's new address `%s'\n",
515 GNUNET_i2s (&address->peer),
516 GST_plugins_a2s (address));
517 ar = GNUNET_ATS_address_add (GST_ats,
521 GNUNET_assert (NULL != ar);
522 ai = GNUNET_new (struct AddressInfo);
523 ai->address = GNUNET_HELLO_address_copy (address);
525 ai->properties = *prop;
526 (void) GNUNET_CONTAINER_multipeermap_put (p2a,
529 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
530 publish_p2a_stat_update ();
535 * Notify ATS about a new @a session now existing for the given
536 * @a address. Essentially, an outbound @a address was used
537 * to establish a @a session. It is safe to call this function
538 * repeatedly for the same @a address and @a session pair.
540 * @param address the address
541 * @param session the session
544 GST_ats_new_session (const struct GNUNET_HELLO_Address *address,
545 struct GNUNET_ATS_Session *session)
547 struct AddressInfo *ai;
550 memcmp (&GST_my_identity,
552 sizeof(struct GNUNET_PeerIdentity)))
553 return; /* our own, ignore! */
554 ai = find_ai (address, NULL);
557 /* We may simply already be aware of the session, even if some
558 other part of the code could not tell if it just created a new
559 session or just got one recycled from the plugin; hence, we may
560 be called with "new" session even for an "old" session; in that
561 case, check that this is the case, but just ignore it. */GNUNET_assert (NULL != (find_ai (address, session)));
564 GNUNET_assert (NULL == ai->session);
565 ai->session = session;
566 LOG (GNUNET_ERROR_TYPE_DEBUG,
567 "Telling ATS about new session for peer %s\n",
568 GNUNET_i2s (&address->peer));
569 /* Note that the address might currently be blocked; we only
570 tell ATS about the session if the address is currently not
571 blocked; otherwise, ATS will be told about the session on
574 GNUNET_ATS_address_add_session (ai->ar,
577 GNUNET_assert (NULL != ai->unblock_task);
582 * Release memory used by the given address data.
584 * @param ai the `struct AddressInfo`
587 destroy_ai (struct AddressInfo *ai)
589 GNUNET_assert (NULL == ai->session);
590 if (NULL != ai->unblock_task)
592 GNUNET_SCHEDULER_cancel (ai->unblock_task);
593 ai->unblock_task = NULL;
596 GNUNET_assert (GNUNET_YES ==
597 GNUNET_CONTAINER_multipeermap_remove (p2a,
600 LOG (GNUNET_ERROR_TYPE_DEBUG,
601 "Telling ATS to destroy address from peer %s\n",
602 GNUNET_i2s (&ai->address->peer));
605 GNUNET_ATS_address_destroy (ai->ar);
608 publish_p2a_stat_update ();
609 GNUNET_HELLO_address_free (ai->address);
615 * Notify ATS that the @a session (but not the @a address) of
616 * a given @a address is no longer relevant. (The @a session
617 * went down.) This function may be called even if for the
618 * respective outbound address #GST_ats_new_session() was
619 * never called and thus the pair is unknown to ATS. In this
620 * case, the call is simply ignored.
622 * @param address the address
623 * @param session the session
626 GST_ats_del_session (const struct GNUNET_HELLO_Address *address,
627 struct GNUNET_ATS_Session *session)
629 struct AddressInfo *ai;
632 memcmp (&GST_my_identity,
634 sizeof(struct GNUNET_PeerIdentity)))
635 return; /* our own, ignore! */
641 ai = find_ai (address,
645 /* We sometimes create sessions just for sending a PING,
646 and if those are destroyed they were never known to
647 ATS which means we end up here (however, in this
648 case, the address must be an outbound address). */
649 GNUNET_break (GNUNET_YES !=
650 GNUNET_HELLO_address_check_option (address,
651 GNUNET_HELLO_ADDRESS_INFO_INBOUND));
654 GNUNET_assert (session == ai->session);
656 LOG (GNUNET_ERROR_TYPE_DEBUG,
657 "Telling ATS to destroy session %p from peer %s\n",
659 GNUNET_i2s (&address->peer));
660 if (GNUNET_YES == ai->expired)
662 /* last reason to keep this 'ai' around is now gone, the
663 session is dead as well, clean up */
666 /* Address expired but not blocked, and thus 'ar' was still
667 live because of the session; deleting just the session
668 will do for an inbound session, but for an outbound we
669 then also need to destroy the address with ATS. */
671 GNUNET_ATS_address_del_session (ai->ar,
674 GNUNET_ATS_address_destroy (ai->ar);
676 /* "ar" has been freed, regardless how the branch
677 above played out: it was either freed in
678 #GNUNET_ATS_address_del_session() because it was
679 incoming, or explicitly in
680 #GNUNET_ATS_address_del_session(). */ai->ar = NULL;
688 /* If ATS doesn't know about the address/session, this means
689 this address was blocked. */
691 GNUNET_HELLO_address_check_option (address,
692 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
694 /* This was a blocked inbound session, which now lost the
695 session. But inbound addresses are by themselves useless,
696 so we must forget about the address as well. */
700 /* Otherwise, we are done as we have set `ai->session` to NULL
701 already and ATS will simply not be told about the session when
702 the connection is unblocked and the outbound address becomes
703 available again. . */
707 /* This is the "simple" case where ATS knows about the session and
708 the address is neither blocked nor expired. Delete the session,
709 and if it was inbound, free the address as well. */
711 GNUNET_ATS_address_del_session (ai->ar,
714 /* This was an inbound address, the session is now gone, so we
715 need to also forget about the address itself. */
723 * Notify ATS about DV @a distance change to an @a address.
724 * Does nothing if the @a address is not known to us.
726 * @param address the address
727 * @param distance new distance value
730 GST_ats_update_distance (const struct GNUNET_HELLO_Address *address,
733 struct AddressInfo *ai;
735 ai = find_ai_no_session (address);
738 /* We do not know about this address, do nothing. */
741 LOG (GNUNET_ERROR_TYPE_DEBUG,
742 "Updated distance for peer `%s' to %u\n",
743 GNUNET_i2s (&address->peer),
745 ai->properties.distance = distance;
746 /* Give manipulation its chance to change metrics */
747 GST_manipulation_manipulate_metrics (address,
750 /* Address may be blocked, only give ATS if address is
753 GNUNET_ATS_address_update (ai->ar,
759 * Notify ATS about @a delay changes to properties of an @a address.
760 * Does nothing if the @a address is not known to us.
762 * @param address the address
763 * @param delay new delay value
766 GST_ats_update_delay (const struct GNUNET_HELLO_Address *address,
767 struct GNUNET_TIME_Relative delay)
769 struct AddressInfo *ai;
771 ai = find_ai_no_session (address);
774 /* We do not know about this address, do nothing. */
777 LOG (GNUNET_ERROR_TYPE_DEBUG,
778 "Updated latency for peer `%s' to %s\n",
779 GNUNET_i2s (&address->peer),
780 GNUNET_STRINGS_relative_time_to_string (delay,
782 ai->properties.delay = delay;
783 /* Give manipulation its chance to change metrics */
784 GST_manipulation_manipulate_metrics (address,
787 /* Address may be blocked, only give ATS if address is
790 GNUNET_ATS_address_update (ai->ar,
796 * Notify ATS about utilization changes to an @a address.
797 * Does nothing if the @a address is not known to us.
799 * @param address our information about the address
800 * @param bps_in new utilization inbound
801 * @param bps_out new utilization outbound
804 GST_ats_update_utilization (const struct GNUNET_HELLO_Address *address,
808 struct AddressInfo *ai;
810 ai = find_ai_no_session (address);
813 /* We do not know about this address, do nothing. */
816 LOG (GNUNET_ERROR_TYPE_DEBUG,
817 "Updating utilization for peer `%s' address %s: %u/%u\n",
818 GNUNET_i2s (&address->peer),
819 GST_plugins_a2s (address),
820 (unsigned int) bps_in,
821 (unsigned int) bps_out);
822 ai->properties.utilization_in = bps_in;
823 ai->properties.utilization_out = bps_out;
824 /* Give manipulation its chance to change metrics */
825 GST_manipulation_manipulate_metrics (address,
828 /* Address may be blocked, only give ATS if address is
831 GNUNET_ATS_address_update (ai->ar,
837 * Notify ATS that the address has expired and thus cannot
838 * be used any longer. This function must only be called
839 * if the corresponding session is already gone.
841 * @param address the address
844 GST_ats_expire_address (const struct GNUNET_HELLO_Address *address)
846 struct AddressInfo *ai;
849 memcmp (&GST_my_identity,
851 sizeof(struct GNUNET_PeerIdentity)))
852 return; /* our own, ignore! */
853 LOG (GNUNET_ERROR_TYPE_DEBUG,
854 "Address %s of peer %s expired\n",
855 GST_plugins_a2s (address),
856 GNUNET_i2s (&address->peer));
857 ai = find_ai_no_session (address);
863 if (NULL != ai->session)
865 /* Got an active session, just remember the expiration
866 and act upon it when the session goes down. */
867 ai->expired = GNUNET_YES;
870 /* Address expired, no session, free resources */
876 * Initialize ATS subsystem.
881 p2a = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_YES);
886 * Release memory used by the given address data.
889 * @param key which peer is this about
890 * @param value the `struct AddressInfo`
891 * @return #GNUNET_OK (continue to iterate)
894 destroy_ai_cb (void *cls,
895 const struct GNUNET_PeerIdentity *key,
898 struct AddressInfo *ai = value;
906 * Shutdown ATS subsystem.
911 GNUNET_CONTAINER_multipeermap_iterate (p2a,
914 publish_p2a_stat_update ();
915 GNUNET_CONTAINER_multipeermap_destroy (p2a);
920 /* end of gnunet-service-transport_ats.c */