/*
This file is part of GNUnet.
- (C) 2015 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2015 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
/**
* @file transport/gnunet-service-transport_ats.h
#include "gnunet-service-transport_plugins.h"
#include "gnunet_ats_service.h"
+#define LOG(kind,...) GNUNET_log_from(kind, "transport-ats", __VA_ARGS__)
+
/**
* Information we track for each address known to ATS.
* Record with ATS API for the address.
*/
struct GNUNET_ATS_AddressRecord *ar;
+
+ /**
+ * Performance properties of this address.
+ */
+ struct GNUNET_ATS_Properties properties;
+
+ /**
+ * Time until when this address is blocked and should thus not be
+ * made available to ATS (@e ar should be NULL until this time).
+ * Used when transport determines that for some reason it
+ * (temporarily) cannot use an address, even though it has been
+ * validated.
+ */
+ struct GNUNET_TIME_Absolute blocked;
+
+ /**
+ * If an address is blocked as part of an exponential back-off,
+ * we track the current size of the backoff here.
+ */
+ struct GNUNET_TIME_Relative back_off;
+
+ /**
+ * Task scheduled to unblock an ATS-blocked address at
+ * @e blocked time, or NULL if the address is not blocked
+ * (and thus @e ar is non-NULL).
+ */
+ struct GNUNET_SCHEDULER_Task *unblock_task;
+
+ /**
+ * Set to #GNUNET_YES if the address has expired but we could
+ * not yet remove it because we still have a valid session.
+ */
+ int expired;
+
};
*/
static struct GNUNET_CONTAINER_MultiPeerMap *p2a;
+/**
+ * Number of blocked addresses.
+ */
+static unsigned int num_blocked;
/**
* Closure for #find_ai().
};
+/**
+ * Provide an update on the `p2a` map size to statistics.
+ * This function should be called whenever the `p2a` map
+ * is changed.
+ */
+static void
+publish_p2a_stat_update ()
+{
+ GNUNET_STATISTICS_set (GST_stats,
+ gettext_noop ("# Addresses given to ATS"),
+ GNUNET_CONTAINER_multipeermap_size (p2a) - num_blocked,
+ GNUNET_NO);
+ GNUNET_STATISTICS_set (GST_stats,
+ "# blocked addresses",
+ num_blocked,
+ GNUNET_NO);
+}
+
+
/**
* Find matching address info.
*
/**
- * Test if ATS knows about this address.
+ * Find matching address info, ignoring sessions.
+ *
+ * @param cls the `struct FindClosure`
+ * @param key which peer is this about
+ * @param value the `struct AddressInfo`
+ * @return #GNUNET_YES to continue to iterate, #GNUNET_NO if we found the value
+ */
+static int
+find_ai_no_session_cb (void *cls,
+ const struct GNUNET_PeerIdentity *key,
+ void *value)
+{
+ struct FindClosure *fc = cls;
+ struct AddressInfo *ai = value;
+
+ if (0 ==
+ GNUNET_HELLO_address_cmp (fc->address,
+ ai->address))
+ {
+ fc->ret = ai;
+ return GNUNET_NO;
+ }
+ return GNUNET_YES;
+}
+
+
+/**
+ * Find the address information struct for the
+ * given address (ignoring sessions)
+ *
+ * @param address address to look for
+ * @return NULL if this combination is unknown
+ */
+static struct AddressInfo *
+find_ai_no_session (const struct GNUNET_HELLO_Address *address)
+{
+ struct FindClosure fc;
+
+ fc.address = address;
+ fc.session = NULL;
+ fc.ret = NULL;
+ GNUNET_CONTAINER_multipeermap_get_multiple (p2a,
+ &address->peer,
+ &find_ai_no_session_cb,
+ &fc);
+ return fc.ret;
+}
+
+
+/**
+ * Test if ATS knows about this @a address and @a session.
*
* @param address the address
* @param session the session
- * @return #GNUNET_YES if address is known, #GNUNET_NO if not.
+ * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not.
*/
int
GST_ats_is_known (const struct GNUNET_HELLO_Address *address,
/**
- * Notify ATS about the new address including the network this address is
- * located in.
+ * Test if ATS knows about this @a address.
*
* @param address the address
- * @param session the session
- * @param ats ats information
- * @param ats_count number of @a ats information
+ * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not.
+ */
+int
+GST_ats_is_known_no_session (const struct GNUNET_HELLO_Address *address)
+{
+ return (NULL != find_ai_no_session (address)) ? GNUNET_YES : GNUNET_NO;
+}
+
+
+/**
+ * The blocking time for an address has expired, allow ATS to
+ * suggest it again.
+ *
+ * @param cls the `struct AddressInfo` of the address to unblock
+ * @param tc unused
+ */
+static void
+unblock_address (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct AddressInfo *ai = cls;
+
+ ai->unblock_task = NULL;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Unblocking address %s of peer %s\n",
+ GST_plugins_a2s (ai->address),
+ GNUNET_i2s (&ai->address->peer));
+ ai->ar = GNUNET_ATS_address_add (GST_ats,
+ ai->address,
+ ai->session,
+ &ai->properties);
+ GNUNET_break (NULL != ai->ar);
+ num_blocked--;
+ publish_p2a_stat_update ();
+}
+
+
+/**
+ * Temporarily block a valid address for use by ATS for address
+ * suggestions. This function should be called if an address was
+ * suggested by ATS but failed to perform (i.e. failure to establish a
+ * session or to exchange the PING/PONG).
+ *
+ * @param address the address to block
+ * @param session the session (can be NULL)
*/
void
-GST_ats_add_address (const struct GNUNET_HELLO_Address *address,
- struct Session *session,
- const struct GNUNET_ATS_Information *ats,
- uint32_t ats_count)
+GST_ats_block_address (const struct GNUNET_HELLO_Address *address,
+ struct Session *session)
{
- struct GNUNET_TRANSPORT_PluginFunctions *papi;
- struct GNUNET_ATS_Information ats2[ats_count + 1];
- struct GNUNET_ATS_AddressRecord *ar;
struct AddressInfo *ai;
- uint32_t net;
- /* valid new address, let ATS know! */
- if (NULL == address->transport_name)
+ ai = find_ai (address, session);
+ if (NULL == ai)
{
- GNUNET_break(0);
+ GNUNET_assert (0);
+ return;
+ }
+ if (NULL == ai->ar)
+ {
+ /* already blocked, how did it get used!? */
+ GNUNET_break (0);
return;
}
+ ai->back_off = GNUNET_TIME_STD_BACKOFF (ai->back_off);
if (GNUNET_YES ==
GNUNET_HELLO_address_check_option (address,
GNUNET_HELLO_ADDRESS_INFO_INBOUND))
- {
- GNUNET_break (NULL != session);
- }
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Removing address %s of peer %s from use (inbound died)\n",
+ GST_plugins_a2s (address),
+ GNUNET_i2s (&address->peer));
+ else
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ "Blocking address %s of peer %s from use for %s\n",
+ GST_plugins_a2s (address),
+ GNUNET_i2s (&address->peer),
+ GNUNET_STRINGS_relative_time_to_string (ai->back_off,
+ GNUNET_YES));
+ /* destroy session and address */
+ if ( (NULL == session) ||
+ (GNUNET_NO ==
+ GNUNET_ATS_address_del_session (ai->ar,
+ session)) )
+ GNUNET_ATS_address_destroy (ai->ar);
+ ai->ar = NULL;
+
+ /* determine when the address should come back to life */
+ ai->blocked = GNUNET_TIME_relative_to_absolute (ai->back_off);
+ ai->unblock_task = GNUNET_SCHEDULER_add_delayed (ai->back_off,
+ &unblock_address,
+ ai);
+ num_blocked++;
+ publish_p2a_stat_update ();
+}
+
+
+/**
+ * Reset address blocking time. Resets the exponential
+ * back-off timer for this address to zero. Done when
+ * an address was used to create a successful connection.
+ *
+ * @param address the address to reset the blocking timer
+ * @param session the session (can be NULL)
+ */
+void
+GST_ats_block_reset (const struct GNUNET_HELLO_Address *address,
+ struct Session *session)
+{
+ struct AddressInfo *ai;
+
ai = find_ai (address, session);
- if (NULL != ai)
+ if (NULL == ai)
{
GNUNET_break (0);
return;
}
- if (NULL == (papi = GST_plugins_find (address->transport_name)))
+ /* address is in successful use, so it should not be blocked right now */
+ GNUNET_break (NULL == ai->unblock_task);
+ ai->back_off = GNUNET_TIME_UNIT_ZERO;
+}
+
+
+/**
+ * Notify ATS about the a new inbound address. We may already
+ * know the address (as this is called each time we receive
+ * a message from an inbound connection). If the address is
+ * indeed new, make it available to ATS.
+ *
+ * @param address the address
+ * @param session the session
+ * @param prop performance information
+ */
+void
+GST_ats_add_inbound_address (const struct GNUNET_HELLO_Address *address,
+ struct Session *session,
+ const struct GNUNET_ATS_Properties *prop)
+{
+ struct GNUNET_ATS_AddressRecord *ar;
+ struct AddressInfo *ai;
+
+ /* valid new address, let ATS know! */
+ if (NULL == address->transport_name)
{
- /* we don't have the plugin for this address */
- GNUNET_assert (0);
+ GNUNET_break(0);
return;
}
- if (NULL != session)
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_HELLO_address_check_option (address,
+ GNUNET_HELLO_ADDRESS_INFO_INBOUND));
+ GNUNET_assert (NULL != session);
+ ai = find_ai (address, session);
+ if (NULL != ai)
{
- net = papi->get_network (papi->cls, session);
- if (GNUNET_ATS_NET_UNSPECIFIED == net)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _ ("Could not obtain a valid network for `%s' %s (%s)\n"),
- GNUNET_i2s (&address->peer),
- GST_plugins_a2s (address),
- address->transport_name);
- return;
- }
- ats2[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
- ats2[0].value = htonl (net);
- memcpy (&ats2[1],
- ats,
- sizeof(struct GNUNET_ATS_Information) * ats_count);
+ /* This should only be called for new sessions, and thus
+ we should not already have the address */
+ GNUNET_break (0);
+ return;
}
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Notifying ATS about peer `%s''s new address `%s' session %p in network %s\n",
- GNUNET_i2s (&address->peer),
- (0 == address->address_length)
- ? "<inbound>"
- : GST_plugins_a2s (address),
- session,
- GNUNET_ATS_print_network_type (net));
+ GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Notifying ATS about peer `%s''s new inbound address `%s' session %p in network %s\n",
+ GNUNET_i2s (&address->peer),
+ (0 == address->address_length)
+ ? "<inbound>"
+ : GST_plugins_a2s (address),
+ session,
+ GNUNET_ATS_print_network_type (prop->scope));
ar = GNUNET_ATS_address_add (GST_ats,
address,
session,
- (NULL != session) ? ats2 : ats,
- (NULL != session) ? ats_count + 1 : ats_count);
+ prop);
+ GNUNET_break (NULL != ar);
ai = GNUNET_new (struct AddressInfo);
ai->address = GNUNET_HELLO_address_copy (address);
ai->session = session;
+ ai->properties = *prop;
+ ai->ar = ar;
+ (void) GNUNET_CONTAINER_multipeermap_put (p2a,
+ &ai->address->peer,
+ ai,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ publish_p2a_stat_update ();
+}
+
+
+/**
+ * Notify ATS about the new address including the network this address is
+ * located in. The address must NOT be inbound and must be new to ATS.
+ *
+ * @param address the address
+ * @param prop performance information
+ */
+void
+GST_ats_add_address (const struct GNUNET_HELLO_Address *address,
+ const struct GNUNET_ATS_Properties *prop)
+{
+ struct GNUNET_ATS_AddressRecord *ar;
+ struct AddressInfo *ai;
+
+ /* valid new address, let ATS know! */
+ if (NULL == address->transport_name)
+ {
+ GNUNET_break(0);
+ return;
+ }
+ GNUNET_assert (GNUNET_YES !=
+ GNUNET_HELLO_address_check_option (address,
+ GNUNET_HELLO_ADDRESS_INFO_INBOUND));
+ ai = find_ai_no_session (address);
+ GNUNET_assert (NULL == ai);
+ GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ "Notifying ATS about peer `%s''s new address `%s'\n",
+ GNUNET_i2s (&address->peer),
+ (0 == address->address_length)
+ ? "<inbound>"
+ : GST_plugins_a2s (address));
+ ar = GNUNET_ATS_address_add (GST_ats,
+ address,
+ NULL,
+ prop);
+ GNUNET_break (NULL != ar);
+ ai = GNUNET_new (struct AddressInfo);
+ ai->address = GNUNET_HELLO_address_copy (address);
ai->ar = ar;
+ ai->properties = *prop;
(void) GNUNET_CONTAINER_multipeermap_put (p2a,
&ai->address->peer,
ai,
GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ publish_p2a_stat_update ();
}
}
GNUNET_break (NULL == ai->session);
ai->session = session;
- GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
- "transport-ats",
- "Telling ATS about new session %p for peer %s\n",
- session,
- GNUNET_i2s (&address->peer));
- GNUNET_ATS_address_add_session (ai->ar,
- session);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Telling ATS about new session for peer %s\n",
+ GNUNET_i2s (&address->peer));
+ if (NULL != ai->ar)
+ GNUNET_ATS_address_add_session (ai->ar,
+ session);
}
GNUNET_break (0);
return;
}
- ai = find_ai (address, session);
+ ai = find_ai (address,
+ session);
if (NULL == ai)
{
/* We sometimes create sessions just for sending a PING,
}
GNUNET_assert (session == ai->session);
ai->session = NULL;
- GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
- "transport-ats",
- "Telling ATS to destroy session %p from peer %s\n",
- session,
- GNUNET_i2s (&address->peer));
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Telling ATS to destroy session %p from peer %s\n",
+ session,
+ GNUNET_i2s (&address->peer));
+ if (NULL == ai->ar)
+ {
+ /* If ATS doesn't know about the address/session, and this was an
+ inbound session or one that expired, then we must forget about
+ the address as well. Otherwise, we are done as we have set
+ `ai->session` to NULL already. */
+ if ( (GNUNET_YES == ai->expired) ||
+ (GNUNET_YES ==
+ GNUNET_HELLO_address_check_option (address,
+ GNUNET_HELLO_ADDRESS_INFO_INBOUND)) )
+ GST_ats_expire_address (address);
+ return;
+ }
if (GNUNET_YES ==
- GNUNET_ATS_address_del_session (ai->ar, session))
+ GNUNET_ATS_address_del_session (ai->ar,
+ session))
{
ai->ar = NULL;
GST_ats_expire_address (address);
/**
- * Notify ATS about property changes to an address.
+ * Notify ATS about DV distance change to an address's.
*
- * @param address our information about the address
- * @param session the session
- * @param ats performance information
- * @param ats_count number of elements in @a ats
+ * @param address the address
+ * @param distance new distance value
*/
void
-GST_ats_update_metrics (const struct GNUNET_HELLO_Address *address,
- struct Session *session,
- const struct GNUNET_ATS_Information *ats,
- uint32_t ats_count)
+GST_ats_update_distance (const struct GNUNET_HELLO_Address *address,
+ uint32_t distance)
{
- struct GNUNET_ATS_Information *ats_new;
struct AddressInfo *ai;
- ai = find_ai (address, session);
+ ai = find_ai_no_session (address);
if (NULL == ai)
- {
- /* We sometimes create sessions just for sending a PING,
- and if we get metrics for those, they were never known to
- ATS which means we end up here (however, in this
- case, the address must be an outbound address). */
- GNUNET_assert (GNUNET_YES !=
- GNUNET_HELLO_address_check_option (address,
- GNUNET_HELLO_ADDRESS_INFO_INBOUND));
-
- return;
- }
- /* Call to manipulation to manipulate ATS information */
- GNUNET_assert (NULL != GST_ats);
- if ((NULL == ats) || (0 == ats_count))
return;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Updating metrics for peer `%s' address %s session %p\n",
- GNUNET_i2s (&address->peer),
- GST_plugins_a2s (address),
- session);
- ats_new = GST_manipulation_manipulate_metrics (address,
- session,
- ats,
- ats_count);
- GNUNET_ATS_address_update (ai->ar,
- ats_new, ats_count);
- GNUNET_free_non_null (ats_new);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Updated distance for peer `%s' to %u\n",
+ GNUNET_i2s (&address->peer),
+ distance);
+ ai->properties.distance = distance;
+ GST_manipulation_manipulate_metrics (address,
+ ai->session,
+ &ai->properties);
+ if (NULL != ai->ar)
+ GNUNET_ATS_address_update (ai->ar,
+ &ai->properties);
}
/**
- * Notify ATS about a new session now being in use (or not).
+ * Notify ATS about property changes to an address's properties.
*
* @param address the address
- * @param session the session
- * @param in_use #GNUNET_YES or #GNUNET_NO
+ * @param delay new delay value
*/
void
-GST_ats_set_in_use (const struct GNUNET_HELLO_Address *address,
- struct Session *session,
- int in_use)
+GST_ats_update_delay (const struct GNUNET_HELLO_Address *address,
+ struct GNUNET_TIME_Relative delay)
{
struct AddressInfo *ai;
- ai = find_ai (address, session);
+ ai = find_ai_no_session (address);
if (NULL == ai)
- {
- GNUNET_break (0);
return;
- }
- GNUNET_ATS_address_set_in_use (ai->ar, in_use);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Updated latency for peer `%s' to %s\n",
+ GNUNET_i2s (&address->peer),
+ GNUNET_STRINGS_relative_time_to_string (delay,
+ GNUNET_YES));
+ ai->properties.delay = delay;
+ GST_manipulation_manipulate_metrics (address,
+ ai->session,
+ &ai->properties);
+ if (NULL != ai->ar)
+ GNUNET_ATS_address_update (ai->ar,
+ &ai->properties);
+}
+
+
+/**
+ * Notify ATS about utilization changes to an address.
+ *
+ * @param address our information about the address
+ * @param bps_in new utilization inbound
+ * @param bps_out new utilization outbound
+ */
+void
+GST_ats_update_utilization (const struct GNUNET_HELLO_Address *address,
+ uint32_t bps_in,
+ uint32_t bps_out)
+{
+ struct AddressInfo *ai;
+
+ ai = find_ai_no_session (address);
+ if (NULL == ai)
+ return;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Updating utilization for peer `%s' address %s: %u/%u\n",
+ GNUNET_i2s (&address->peer),
+ GST_plugins_a2s (address),
+ (unsigned int) bps_in,
+ (unsigned int) bps_out);
+ ai->properties.utilization_in = bps_in;
+ ai->properties.utilization_out = bps_out;
+ GST_manipulation_manipulate_metrics (address,
+ ai->session,
+ &ai->properties);
+ if (NULL != ai->ar)
+ GNUNET_ATS_address_update (ai->ar,
+ &ai->properties);
}
{
struct AddressInfo *ai;
- ai = find_ai (address, NULL);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Address %s of peer %s expired\n",
+ GST_plugins_a2s (address),
+ GNUNET_i2s (&address->peer));
+ ai = find_ai_no_session (address);
if (NULL == ai)
{
GNUNET_assert (0);
return;
}
+ if (NULL != ai->unblock_task)
+ {
+ GNUNET_SCHEDULER_cancel (ai->unblock_task);
+ ai->unblock_task = NULL;
+ num_blocked--;
+ }
+ if (NULL != ai->session)
+ {
+ ai->expired = GNUNET_YES;
+ if (NULL != ai->ar)
+ {
+ GNUNET_ATS_address_destroy (ai->ar);
+ ai->ar = NULL;
+ }
+ return;
+ }
GNUNET_assert (GNUNET_YES ==
GNUNET_CONTAINER_multipeermap_remove (p2a,
&address->peer,
ai));
- GNUNET_break (NULL == ai->session);
- GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
- "transport-ats",
- "Telling ATS to destroy address from peer %s\n",
- GNUNET_i2s (&address->peer));
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Telling ATS to destroy address from peer %s\n",
+ GNUNET_i2s (&address->peer));
if (NULL != ai->ar)
- GNUNET_ATS_address_destroy (ai->ar);
+ {
+ /* We usually should not have a session here when we
+ expire an address, but during shutdown a session
+ may be active while validation causes the address
+ to 'expire'. So clean up both if necessary. */
+ if ( (NULL == ai->session) ||
+ (GNUNET_NO ==
+ GNUNET_ATS_address_del_session (ai->ar,
+ ai->session)) )
+ GNUNET_ATS_address_destroy (ai->ar);
+ ai->ar = NULL;
+ }
+ publish_p2a_stat_update ();
GNUNET_HELLO_address_free (ai->address);
GNUNET_free (ai);
}
{
struct AddressInfo *ai = value;
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_remove (p2a,
+ key,
+ ai));
+ if (NULL != ai->unblock_task)
+ {
+ GNUNET_SCHEDULER_cancel (ai->unblock_task);
+ ai->unblock_task = NULL;
+ num_blocked--;
+ }
+ if (NULL != ai->ar)
+ {
+ GNUNET_ATS_address_destroy (ai->ar);
+ ai->ar = NULL;
+ }
GNUNET_HELLO_address_free (ai->address);
GNUNET_free (ai);
return GNUNET_OK;
GNUNET_CONTAINER_multipeermap_iterate (p2a,
&destroy_ai,
NULL);
+ publish_p2a_stat_update ();
GNUNET_CONTAINER_multipeermap_destroy (p2a);
p2a = NULL;
}