2 This file is part of GNUnet.
3 Copyright (C) 2015 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file transport/gnunet-service-transport_ats.h
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"
32 #define LOG(kind,...) GNUNET_log_from(kind, "transport-ats", __VA_ARGS__)
36 * Information we track for each address known to ATS.
42 * The address (with peer identity).
44 struct GNUNET_HELLO_Address *address;
47 * Session (can be NULL)
49 struct Session *session;
52 * Record with ATS API for the address.
54 struct GNUNET_ATS_AddressRecord *ar;
57 * Performance properties of this address.
59 struct GNUNET_ATS_Properties properties;
62 * Time until when this address is blocked and should thus not be
63 * made available to ATS (@e ar should be NULL until this time).
64 * Used when transport determines that for some reason it
65 * (temporarily) cannot use an address, even though it has been
68 struct GNUNET_TIME_Absolute blocked;
71 * If an address is blocked as part of an exponential back-off,
72 * we track the current size of the backoff here.
74 struct GNUNET_TIME_Relative back_off;
77 * Task scheduled to unblock an ATS-blocked address at
78 * @e blocked time, or NULL if the address is not blocked
79 * (and thus @e ar is non-NULL).
81 struct GNUNET_SCHEDULER_Task *unblock_task;
84 * Set to #GNUNET_YES if the address has expired but we could
85 * not yet remove it because we still have a valid session.
93 * Map from peer identities to one or more `struct AddressInfo` values
96 static struct GNUNET_CONTAINER_MultiPeerMap *p2a;
99 * Number of blocked addresses.
101 static unsigned int num_blocked;
104 * Closure for #find_ai().
110 * Session to look for (only used if the address is inbound).
112 struct Session *session;
115 * Address to look for.
117 const struct GNUNET_HELLO_Address *address;
120 * Where to store the result.
122 struct AddressInfo *ret;
128 * Provide an update on the `p2a` map size to statistics.
129 * This function should be called whenever the `p2a` map
133 publish_p2a_stat_update ()
135 GNUNET_STATISTICS_set (GST_stats,
136 gettext_noop ("# Addresses given to ATS"),
137 GNUNET_CONTAINER_multipeermap_size (p2a) - num_blocked,
139 GNUNET_STATISTICS_set (GST_stats,
140 "# blocked addresses",
147 * Find matching address info.
149 * @param cls the `struct FindClosure`
150 * @param key which peer is this about
151 * @param value the `struct AddressInfo`
152 * @return #GNUNET_YES to continue to iterate, #GNUNET_NO if we found the value
155 find_ai_cb (void *cls,
156 const struct GNUNET_PeerIdentity *key,
159 struct FindClosure *fc = cls;
160 struct AddressInfo *ai = value;
163 GNUNET_HELLO_address_cmp (fc->address,
165 (fc->session == ai->session) )
170 GNUNET_assert ( (fc->session != ai->session) ||
171 (NULL == ai->session) );
177 * Find the address information struct for the
178 * given address and session.
180 * @param address address to look for
181 * @param session session to match for inbound connections
182 * @return NULL if this combination is unknown
184 static struct AddressInfo *
185 find_ai (const struct GNUNET_HELLO_Address *address,
186 struct Session *session)
188 struct FindClosure fc;
190 fc.address = address;
191 fc.session = session;
193 GNUNET_CONTAINER_multipeermap_get_multiple (p2a,
202 * Find matching address info, ignoring sessions and expired
205 * @param cls the `struct FindClosure`
206 * @param key which peer is this about
207 * @param value the `struct AddressInfo`
208 * @return #GNUNET_YES to continue to iterate, #GNUNET_NO if we found the value
211 find_ai_no_session_cb (void *cls,
212 const struct GNUNET_PeerIdentity *key,
215 struct FindClosure *fc = cls;
216 struct AddressInfo *ai = value;
219 return GNUNET_YES; /* expired do not count here */
221 GNUNET_HELLO_address_cmp (fc->address,
232 * Find the address information struct for the
233 * given address (ignoring sessions)
235 * @param address address to look for
236 * @return NULL if this combination is unknown
238 static struct AddressInfo *
239 find_ai_no_session (const struct GNUNET_HELLO_Address *address)
241 struct FindClosure fc;
243 fc.address = address;
246 GNUNET_CONTAINER_multipeermap_get_multiple (p2a,
248 &find_ai_no_session_cb,
255 * Test if ATS knows about this @a address and @a session.
257 * @param address the address
258 * @param session the session
259 * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not.
262 GST_ats_is_known (const struct GNUNET_HELLO_Address *address,
263 struct Session *session)
265 return (NULL != find_ai (address, session)) ? GNUNET_YES : GNUNET_NO;
270 * Test if ATS knows about this @a address.
272 * @param address the address
273 * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not.
276 GST_ats_is_known_no_session (const struct GNUNET_HELLO_Address *address)
278 return (NULL != find_ai_no_session (address))
285 * The blocking time for an address has expired, allow ATS to
288 * @param cls the `struct AddressInfo` of the address to unblock
292 unblock_address (void *cls,
293 const struct GNUNET_SCHEDULER_TaskContext *tc)
295 struct AddressInfo *ai = cls;
297 ai->unblock_task = NULL;
298 LOG (GNUNET_ERROR_TYPE_DEBUG,
299 "Unblocking address %s of peer %s\n",
300 GST_plugins_a2s (ai->address),
301 GNUNET_i2s (&ai->address->peer));
302 ai->ar = GNUNET_ATS_address_add (GST_ats,
306 GNUNET_break (NULL != ai->ar);
308 publish_p2a_stat_update ();
313 * Temporarily block a valid address for use by ATS for address
314 * suggestions. This function should be called if an address was
315 * suggested by ATS but failed to perform (i.e. failure to establish a
316 * session or to exchange the PING/PONG).
318 * @param address the address to block
319 * @param session the session (can be NULL)
322 GST_ats_block_address (const struct GNUNET_HELLO_Address *address,
323 struct Session *session)
325 struct AddressInfo *ai;
327 ai = find_ai (address, session);
335 /* already blocked, how did it get used!? */
339 ai->back_off = GNUNET_TIME_STD_BACKOFF (ai->back_off);
341 GNUNET_HELLO_address_check_option (address,
342 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
343 LOG (GNUNET_ERROR_TYPE_DEBUG,
344 "Removing address %s of peer %s from use (inbound died)\n",
345 GST_plugins_a2s (address),
346 GNUNET_i2s (&address->peer));
348 LOG (GNUNET_ERROR_TYPE_INFO,
349 "Blocking address %s of peer %s from use for %s\n",
350 GST_plugins_a2s (address),
351 GNUNET_i2s (&address->peer),
352 GNUNET_STRINGS_relative_time_to_string (ai->back_off,
354 /* destroy session and address */
355 if ( (NULL == session) ||
357 GNUNET_ATS_address_del_session (ai->ar,
359 GNUNET_ATS_address_destroy (ai->ar);
362 /* determine when the address should come back to life */
363 ai->blocked = GNUNET_TIME_relative_to_absolute (ai->back_off);
364 ai->unblock_task = GNUNET_SCHEDULER_add_delayed (ai->back_off,
368 publish_p2a_stat_update ();
373 * Reset address blocking time. Resets the exponential
374 * back-off timer for this address to zero. Done when
375 * an address was used to create a successful connection.
377 * @param address the address to reset the blocking timer
378 * @param session the session (can be NULL)
381 GST_ats_block_reset (const struct GNUNET_HELLO_Address *address,
382 struct Session *session)
384 struct AddressInfo *ai;
386 ai = find_ai (address, session);
392 /* address is in successful use, so it should not be blocked right now */
393 GNUNET_break (NULL == ai->unblock_task);
394 ai->back_off = GNUNET_TIME_UNIT_ZERO;
399 * Notify ATS about the a new inbound address. We may already
400 * know the address (as this is called each time we receive
401 * a message from an inbound connection). If the address is
402 * indeed new, make it available to ATS.
404 * @param address the address
405 * @param session the session
406 * @param prop performance information
409 GST_ats_add_inbound_address (const struct GNUNET_HELLO_Address *address,
410 struct Session *session,
411 const struct GNUNET_ATS_Properties *prop)
413 struct GNUNET_ATS_AddressRecord *ar;
414 struct AddressInfo *ai;
416 /* valid new address, let ATS know! */
417 if (NULL == address->transport_name)
422 GNUNET_assert (GNUNET_YES ==
423 GNUNET_HELLO_address_check_option (address,
424 GNUNET_HELLO_ADDRESS_INFO_INBOUND));
425 GNUNET_assert (NULL != session);
426 ai = find_ai (address, session);
429 /* This should only be called for new sessions, and thus
430 we should not already have the address */
434 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
435 LOG (GNUNET_ERROR_TYPE_DEBUG,
436 "Notifying ATS about peer `%s''s new inbound address `%s' session %p in network %s\n",
437 GNUNET_i2s (&address->peer),
438 (0 == address->address_length)
440 : GST_plugins_a2s (address),
442 GNUNET_ATS_print_network_type (prop->scope));
443 ar = GNUNET_ATS_address_add (GST_ats,
447 GNUNET_break (NULL != ar);
448 ai = GNUNET_new (struct AddressInfo);
449 ai->address = GNUNET_HELLO_address_copy (address);
450 ai->session = session;
451 ai->properties = *prop;
453 (void) GNUNET_CONTAINER_multipeermap_put (p2a,
456 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
457 publish_p2a_stat_update ();
462 * Notify ATS about the new address including the network this address is
463 * located in. The address must NOT be inbound and must be new to ATS.
465 * @param address the address
466 * @param prop performance information
469 GST_ats_add_address (const struct GNUNET_HELLO_Address *address,
470 const struct GNUNET_ATS_Properties *prop)
472 struct GNUNET_ATS_AddressRecord *ar;
473 struct AddressInfo *ai;
475 /* valid new address, let ATS know! */
476 if (NULL == address->transport_name)
481 GNUNET_assert (GNUNET_YES !=
482 GNUNET_HELLO_address_check_option (address,
483 GNUNET_HELLO_ADDRESS_INFO_INBOUND));
484 ai = find_ai_no_session (address);
485 GNUNET_assert (NULL == ai);
486 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
487 LOG (GNUNET_ERROR_TYPE_INFO,
488 "Notifying ATS about peer `%s''s new address `%s'\n",
489 GNUNET_i2s (&address->peer),
490 (0 == address->address_length)
492 : GST_plugins_a2s (address));
493 ar = GNUNET_ATS_address_add (GST_ats,
497 GNUNET_break (NULL != ar);
498 ai = GNUNET_new (struct AddressInfo);
499 ai->address = GNUNET_HELLO_address_copy (address);
501 ai->properties = *prop;
502 (void) GNUNET_CONTAINER_multipeermap_put (p2a,
505 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
506 publish_p2a_stat_update ();
511 * Notify ATS about a new session now existing for the given
514 * @param address the address
515 * @param session the session
518 GST_ats_new_session (const struct GNUNET_HELLO_Address *address,
519 struct Session *session)
521 struct AddressInfo *ai;
523 ai = find_ai (address, NULL);
526 /* We may already be aware of the session, even if some other part
527 of the code could not tell if it just created a new session or
528 just got one recycled from the plugin; hence, we may be called
529 with "new" session even for an "old" session; in that case,
530 check that this is the case, but just ignore it. */
531 GNUNET_assert (NULL != (find_ai (address, session)));
534 GNUNET_break (NULL == ai->session);
535 ai->session = session;
536 LOG (GNUNET_ERROR_TYPE_DEBUG,
537 "Telling ATS about new session for peer %s\n",
538 GNUNET_i2s (&address->peer));
540 GNUNET_ATS_address_add_session (ai->ar,
546 * Release memory used by the given address data.
548 * @param ai the `struct AddressInfo`
551 destroy_ai (struct AddressInfo *ai)
553 GNUNET_assert (NULL == ai->session);
554 if (NULL != ai->unblock_task)
556 GNUNET_SCHEDULER_cancel (ai->unblock_task);
557 ai->unblock_task = NULL;
560 GNUNET_assert (GNUNET_YES ==
561 GNUNET_CONTAINER_multipeermap_remove (p2a,
564 LOG (GNUNET_ERROR_TYPE_DEBUG,
565 "Telling ATS to destroy address from peer %s\n",
566 GNUNET_i2s (&ai->address->peer));
569 GNUNET_ATS_address_destroy (ai->ar);
572 publish_p2a_stat_update ();
573 GNUNET_HELLO_address_free (ai->address);
579 * Notify ATS that the session (but not the address) of
580 * a given address is no longer relevant.
582 * @param address the address
583 * @param session the session
586 GST_ats_del_session (const struct GNUNET_HELLO_Address *address,
587 struct Session *session)
589 struct AddressInfo *ai;
596 ai = find_ai (address,
600 /* We sometimes create sessions just for sending a PING,
601 and if those are destroyed they were never known to
602 ATS which means we end up here (however, in this
603 case, the address must be an outbound address). */
604 GNUNET_break (GNUNET_YES !=
605 GNUNET_HELLO_address_check_option (address,
606 GNUNET_HELLO_ADDRESS_INFO_INBOUND));
610 GNUNET_assert (session == ai->session);
612 LOG (GNUNET_ERROR_TYPE_DEBUG,
613 "Telling ATS to destroy session %p from peer %s\n",
615 GNUNET_i2s (&address->peer));
616 if (GNUNET_YES == ai->expired)
618 /* last reason to keep this 'ai' around is now gone, the
619 session is dead as well, clean up */
622 GNUNET_break (GNUNET_YES ==
623 GNUNET_ATS_address_del_session (ai->ar,
631 /* If ATS doesn't know about the address/session, and this was an
632 inbound session, so we must forget about the address as well.
633 Otherwise, we are done as we have set `ai->session` to NULL
636 GNUNET_HELLO_address_check_option (address,
637 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
638 GST_ats_expire_address (address);
642 GNUNET_ATS_address_del_session (ai->ar,
646 GST_ats_expire_address (address);
652 * Notify ATS about DV distance change to an address's.
654 * @param address the address
655 * @param distance new distance value
658 GST_ats_update_distance (const struct GNUNET_HELLO_Address *address,
661 struct AddressInfo *ai;
663 ai = find_ai_no_session (address);
666 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "Updated distance for peer `%s' to %u\n",
668 GNUNET_i2s (&address->peer),
670 ai->properties.distance = distance;
671 GST_manipulation_manipulate_metrics (address,
675 GNUNET_ATS_address_update (ai->ar,
681 * Notify ATS about property changes to an address's properties.
683 * @param address the address
684 * @param delay new delay value
687 GST_ats_update_delay (const struct GNUNET_HELLO_Address *address,
688 struct GNUNET_TIME_Relative delay)
690 struct AddressInfo *ai;
692 ai = find_ai_no_session (address);
695 LOG (GNUNET_ERROR_TYPE_DEBUG,
696 "Updated latency for peer `%s' to %s\n",
697 GNUNET_i2s (&address->peer),
698 GNUNET_STRINGS_relative_time_to_string (delay,
700 ai->properties.delay = delay;
701 GST_manipulation_manipulate_metrics (address,
705 GNUNET_ATS_address_update (ai->ar,
711 * Notify ATS about utilization changes to an address.
713 * @param address our information about the address
714 * @param bps_in new utilization inbound
715 * @param bps_out new utilization outbound
718 GST_ats_update_utilization (const struct GNUNET_HELLO_Address *address,
722 struct AddressInfo *ai;
724 ai = find_ai_no_session (address);
727 LOG (GNUNET_ERROR_TYPE_DEBUG,
728 "Updating utilization for peer `%s' address %s: %u/%u\n",
729 GNUNET_i2s (&address->peer),
730 GST_plugins_a2s (address),
731 (unsigned int) bps_in,
732 (unsigned int) bps_out);
733 ai->properties.utilization_in = bps_in;
734 ai->properties.utilization_out = bps_out;
735 GST_manipulation_manipulate_metrics (address,
739 GNUNET_ATS_address_update (ai->ar,
745 * Notify ATS that the address has expired and thus cannot
746 * be used any longer. This function must only be called
747 * if the corresponding session is already gone.
749 * @param address the address
752 GST_ats_expire_address (const struct GNUNET_HELLO_Address *address)
754 struct AddressInfo *ai;
756 LOG (GNUNET_ERROR_TYPE_DEBUG,
757 "Address %s of peer %s expired\n",
758 GST_plugins_a2s (address),
759 GNUNET_i2s (&address->peer));
760 ai = find_ai_no_session (address);
766 if (NULL != ai->session)
768 ai->expired = GNUNET_YES;
776 * Initialize ATS subsystem.
781 p2a = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_YES);
786 * Release memory used by the given address data.
789 * @param key which peer is this about
790 * @param value the `struct AddressInfo`
791 * @return #GNUNET_OK (continue to iterate)
794 destroy_ai_cb (void *cls,
795 const struct GNUNET_PeerIdentity *key,
798 struct AddressInfo *ai = value;
806 * Shutdown ATS subsystem.
811 GNUNET_CONTAINER_multipeermap_iterate (p2a,
814 publish_p2a_stat_update ();
815 GNUNET_CONTAINER_multipeermap_destroy (p2a);
819 /* end of gnunet-service-transport_ats.c */