use NULL as flag for evaluation of query, ensure we pass non-NULL for reply_block...
[oweals/gnunet.git] / src / ats / plugin_ats_proportional.c
index bacb57e021e41ef4faf417cb3ab8e16cec70942f..9309bc2ea799df575b99b5884aea081cd2ab8552 100644 (file)
@@ -1,6 +1,6 @@
 /*
  This file is part of GNUnet.
- Copyright (C) 2011-2015 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2011-2015 GNUnet e.V.
 
  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 ats/plugin_ats_proportional.c
  * @brief ATS proportional solver
  */
 #include "platform.h"
 #include "gnunet_statistics_service.h"
-#include "gnunet_ats_plugin.h"
 #include "gnunet_ats_service.h"
+#include "gnunet_ats_plugin.h"
 #include "gnunet-service-ats_addresses.h"
 
-#define PROP_STABILITY_FACTOR 1.25
-
-
 #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
 
-
 /**
- *
- * NOTE: Do not change this documentation. This documentation is based
- * on gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
- * use build_txt.sh to generate plaintext output
- *
- * ATS addresses : proportional solver
- *
- *    The proportional solver ("proportional") distributes the available
- *    bandwidth fair over all the addresses influenced by the
- *    preference values. For each available network type an in- and
- *    outbound quota is configured and the bandwidth available in
- *    these networks is distributed over the addresses.  The solver
- *    first assigns every addresses the minimum amount of bandwidth
- *    #GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
- *    remaining bandwidth available according to the preference
- *    values. For each peer only a single address gets bandwidth
- *    assigned and only one address marked as active.  The most
- *    important functionality for the solver is implemented in: *
- *    find_address_it is an hashmap iterator returning the prefered
- *    address for an peer * update_quota_per_network distributes
- *    available bandwidth for a network over active addresses
- *
- *    Changes to addresses automatically have an impact on the the
- *    bandwidth assigned to other addresses in the same network since
- *    the solver distributes the remaining bandwidth over the
- *    addresses in the network.  When changes to the addresses occur,
- *    the solver first performs the changes, like adding or deleting
- *    addresses, and then updates bandwidth assignment for the
- *    affected network. Bandwidth assignment is only recalculated on
- *    demand when an address is requested by a client for a peer or
- *    when the addresses available have changed or an address changed
- *    the network it is located in. When the bandwidth assignment has
- *    changed the callback is called with the new bandwidth
- *    assignments. The bandwidth distribution for a network is
- *    recalculated due to: * address suggestion requests * address
- *    deletions * address switching networks during address update *
- *    preference changes
- *
- *     3.1 Data structures used
- *
- *    For each ATS network (e.g. WAN, LAN, loopback) a struct Network
- *    is used to specify network related information as total adresses
- *    and active addresses in this network and the configured in- and
- *    outbound quota. Each network also contains a list of addresses
- *    added to the solver located in this network. The proportional
- *    solver uses the addresses' solver_information field to store the
- *    proportional network it belongs to for each address.
- *
- *     3.2 Initializing
- *
- *    When the proportional solver is initialized the solver creates a
- *    new solver handle and initializes the network structures with
- *    the quotas passed from addresses and returns the handle solver.
- *
- *     3.3 Adding an address
- *
- *    When a new address is added to the solver using s_add, a lookup
- *    for the network for this address is done and the address is
- *    enqueued in in the linked list of the network.
- *
- *     3.4 Updating an address
- *
- *    The main purpose of address updates is to update the ATS
- *    information for addresse selection. Important for the proportional
- *    solver is when an address switches network it is located
- *    in. This is common because addresses added by transport's
- *    validation mechanism are commonly located in
- *    #GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
- *    in this network type and only if a connection is successful on
- *    return of payload data transport switches to the real network
- *    the address is located in.  When an address changes networks it
- *    is first of all removed from the old network using the solver
- *    API function #GAS_proportional_address_delete() and the network in
- *    the address struct is updated. A lookup for the respective new
- *    proportional network is done and stored in the addresse's
- *    solver_information field. Next the address is re-added to the
- *    solver using the solver API function
- *    #GAS_proportional_address_add(). If the address was marked as in
- *    active, the solver checks if bandwidth is available in the
- *    network and if yes sets the address to active and updates the
- *    bandwidth distribution in this network. If no bandwidth is
- *    available it sets the bandwidth for this address to 0 and tries
- *    to suggest an alternative address. If an alternative address was
- *    found, addresses' callback is called for this address.
- *
- *     3.5 Deleting an address
- *
- *    When an address is removed from the solver, it removes the
- *    respective address from the network and if the address was
- *    marked as active, it updates the bandwidth distribution for this
- *    network.
- *
- *     3.6 Requesting addresses
- *
- *    When an address is requested for a peer the solver performs a
- *    lookup for the peer entry in addresses address hashmap and
- *    selects the best address.  The selection of the most suitable
- *    address is done in the find_address_it hashmap iterator
- *    described in detail in section 3.7. If no address is returned,
- *    no address can be suggested at the moment. If the address
- *    returned is marked as active, the solver can return this
- *    address. If the address is not marked as active, the solver
- *    checks if another address belongign to this peer is marked as
- *    active and marks the address as inactive, updates the bandwidth
- *    for this address to 0, call the bandwidth changed callback for
- *    this address due to the change and updates quota assignment for
- *    the addresse's network. the now in-active address is belonging
- *    to. The solver marks the new address as active and updates the
- *    bandwidth assignment for this network.
- *
- *     3.7 Choosing addresses
- *
- *    Choosing the best possible address for suggestion is done by
- *    iterating over all addresses of a peer stored in addresses'
- *    hashmap and using the hashmap iterator find_address_it to select
- *    the best available address.  Several checks are done when an
- *    address is selected. First if this address is currently blocked
- *    by addresses from being suggested. An address is blocked for the
- *    duration of #ATS_BLOCKING_DELTA when it is suggested to
- *    transport. Next it is checked if at least
- *    #GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
- *    in the addresse's network, because suggesting an address without
- *    bandwidth does not make sense. This also ensures that all active
- *    addresses in this network get at least the minimum amount of
- *    bandwidth assigned. In the next step the solver ensures that for
- *    tcp connections inbound connections are prefered over outbound
- *    connections. In the next stet the solver ensures that
- *    connections are prefered in the following order: * connections
- *    are already established and have bandwidth assigned *
- *    connections with a shorter distance * connectes have a shorter
- *    latency
- *
- *     3.8 Changing preferences
- *
- *     3.9 Shutdown
- *
- *    During shutdown all network entries and aging processes are
- *    destroyed and freed.
- *
- *
- * OLD DOCUMENTATION
- *
- * This solver assigns in and outbound bandwidth equally for all
- * addresses in specific network type (WAN, LAN) based on configured
- * in and outbound quota for this network.
- *
- * The solver is notified by addresses about changes to the addresses
- * and recalculates the bandwith assigned if required. The solver
- * notifies addresses by calling the GAS_bandwidth_changed_cb
- * callback.
- *
- * - Initialization
- *
- *
- *
- *
- * For each peer only a single is selected and marked as "active" in the address
- * struct.
- *
- * E.g.:
- *
- * You have the networks WAN and LAN and quotas
- * WAN_TOTAL_IN, WAN_TOTAL_OUT
- * LAN_TOTAL_IN, LAN_TOTAL_OUT
- *
- * If you have x addresses in the network segment LAN, the quotas are
- * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
- *
- * Quotas are automatically recalculated and reported back when addresses are
- * - requested
- *
+ * How much do we value stability over adaptation by default.  A low
+ * value (close to 1.0) means we adapt as soon as possible, a larger
+ * value means that we have to have the respective factor of an
+ * advantage (or delay) before we adapt and sacrifice stability.
  */
+#define PROP_STABILITY_FACTOR 1.25
+
 
 /**
- * Default value to assume for the proportionality factor,
- * if none is given in the configuration.
+ * Default value to assume for the proportionality factor, if none is
+ * given in the configuration.  This factor determines how strong the
+ * bandwidth allocation will orient itself on the application
+ * preferences.  A lower factor means a more balanced bandwidth
+ * distribution while a larger number means a distribution more in
+ * line with application (bandwidth) preferences.
  */
 #define PROPORTIONALITY_FACTOR 2.0
 
@@ -364,10 +198,6 @@ struct GAS_PROPORTIONAL_Handle
    */
   unsigned int active_addresses;
 
-  /**
-   * Number of networks in @a network_entries
-   */
-  unsigned int network_count;
 };
 
 
@@ -385,7 +215,7 @@ is_bandwidth_available_in_network (struct Network *net,
   unsigned int na;
   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
 
-  GNUNET_assert (net->active_addresses + extra >= 0);
+  GNUNET_assert (((int)net->active_addresses) + extra >= 0);
   na = net->active_addresses + extra;
   if (0 == na)
     return GNUNET_YES;
@@ -478,7 +308,8 @@ distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
       continue;
     peer_relative_prefs = s->env->get_preferences (s->env->cls,
                                                    &aw->addr->peer);
-    sum_relative_peer_prefences += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
+    sum_relative_peer_prefences
+      += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
     count_addresses++;
   }
   if (count_addresses != net->active_addresses)
@@ -591,7 +422,7 @@ distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
 {
   unsigned int i;
 
-  if (GNUNET_YES == s->bulk_lock)
+  if (0 != s->bulk_lock)
   {
     s->bulk_requests++;
     return;
@@ -627,11 +458,14 @@ distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
   }
   else
   {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Redistributing bandwidth in all %u networks\n",
+         s->env->network_count);
     s->env->info_cb (s->env->cls,
                      GAS_OP_SOLVE_START,
                      GAS_STAT_SUCCESS,
                      GAS_INFO_PROP_ALL);
-    for (i = 0; i < s->network_count; i++)
+    for (i = 0; i < s->env->network_count; i++)
       distribute_bandwidth (s,
                             &s->network_entries[i]);
     s->env->info_cb (s->env->cls,
@@ -642,7 +476,7 @@ distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
                      GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
                      GAS_STAT_SUCCESS,
                      GAS_INFO_PROP_ALL);
-    for (i = 0; i < s->network_count; i++)
+    for (i = 0; i < s->env->network_count; i++)
       propagate_bandwidth (s,
                            &s->network_entries[i]);
     s->env->info_cb (s->env->cls,
@@ -670,27 +504,6 @@ struct FindBestAddressCtx
 };
 
 
-/**
- * Find index of a ATS property type in the quality properties array.
- *
- * @param type ATS property type
- * @return index in the quality array, #GNUNET_SYSERR if the type
- *         was not a quality property
- */
-static int
-find_quality_property_index (enum GNUNET_ATS_Property type)
-{
-  enum GNUNET_ATS_Property existing_types[] = GNUNET_ATS_QualityProperties;
-  unsigned int c;
-
-  for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
-    if (existing_types[c] == type)
-      return c;
-  GNUNET_break (0);
-  return GNUNET_SYSERR;
-}
-
-
 /**
  * Find a "good" address to use for a peer by iterating over the
  * addresses for this peer.  If we already have an existing address,
@@ -715,7 +528,6 @@ find_best_address_it (void *cls,
   double best_distance;
   double cur_delay;
   double cur_distance;
-  int index;
   unsigned int con;
   int bw_available;
   int need;
@@ -768,12 +580,10 @@ find_best_address_it (void *cls,
   }
 
   /* Now compare ATS information */
-  index = find_quality_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
-  cur_distance = current->atsin[index].norm;
-  best_distance = ctx->best->atsin[index].norm;
-  index = find_quality_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
-  cur_delay = current->atsin[index].norm;
-  best_delay = ctx->best->atsin[index].norm;
+  cur_distance = current->norm_distance.norm;
+  best_distance = ctx->best->norm_distance.norm;
+  cur_delay = current->norm_delay.norm;
+  best_delay = ctx->best->norm_delay.norm;
 
   /* user shorter distance */
   if (cur_distance < best_distance)
@@ -982,7 +792,7 @@ update_active_address (struct GAS_PROPORTIONAL_Handle *s,
   }
   /* We do have a new address, activate it */
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Suggesting new address %p for peer `%s'\n",
+       "Selecting new address %p for peer `%s'\n",
        best_address,
        GNUNET_i2s (peer));
   /* Mark address as active */
@@ -1003,7 +813,6 @@ update_active_address (struct GAS_PROPORTIONAL_Handle *s,
        best_address,
        GNUNET_i2s (peer));
 
-
   if (GNUNET_NO ==
       is_bandwidth_available_in_network (asi_best->network,
                                          0))
@@ -1015,9 +824,10 @@ update_active_address (struct GAS_PROPORTIONAL_Handle *s,
     aw_min = NULL;
     for (aw = asi_best->network->head; NULL != aw; aw = aw->next)
     {
-      if (con_min >
-          (a_con = s->env->get_connectivity (s->env->cls,
-                                             &aw->addr->peer)))
+      if ( (con_min >
+            (a_con = s->env->get_connectivity (s->env->cls,
+                                               &aw->addr->peer))) &&
+           (GNUNET_YES == aw->addr->active) )
       {
         aw_min = aw;
         con_min = a_con;
@@ -1027,7 +837,7 @@ update_active_address (struct GAS_PROPORTIONAL_Handle *s,
     }
     update_active_address (s,
                            aw_min->addr,
-                           &aw->addr->peer);
+                           &aw_min->addr->peer);
   }
   distribute_bandwidth_in_network (s,
                                    asi_best->network);
@@ -1050,6 +860,8 @@ GAS_proportional_change_preference (void *solver,
 {
   struct GAS_PROPORTIONAL_Handle *s = solver;
 
+  if (GNUNET_ATS_PREFERENCE_BANDWIDTH != kind)
+    return; /* we do not care */
   distribute_bandwidth_in_network (s,
                                    NULL);
 }
@@ -1067,7 +879,7 @@ GAS_proportional_change_preference (void *solver,
  */
 static void
 GAS_proportional_feedback (void *solver,
-                           struct GNUNET_SERVER_Client *application,
+                           struct GNUNET_SERVICE_Client *application,
                            const struct GNUNET_PeerIdentity *peer,
                            const struct GNUNET_TIME_Relative scope,
                            enum GNUNET_ATS_PreferenceKind kind,
@@ -1172,16 +984,10 @@ GAS_proportional_bulk_stop (void *solver)
  *
  * @param solver solver handle
  * @param address the address
- * @param type the ATSI type in HBO
- * @param abs_value the absolute value of the property
- * @param rel_value the normalized value
  */
 static void
 GAS_proportional_address_property_changed (void *solver,
-                                           struct ATS_Address *address,
-                                           enum GNUNET_ATS_Property type,
-                                           uint32_t abs_value,
-                                           double rel_value)
+                                           struct ATS_Address *address)
 {
   struct GAS_PROPORTIONAL_Handle *s = solver;
   struct AddressWrapper *asi = address->solver_information;
@@ -1226,16 +1032,10 @@ GAS_proportional_address_add (void *solver,
                             net->stat_total,
                             1,
                             GNUNET_NO);
-  if (0 !=
-      s->env->get_connectivity (s->env->cls,
-                                &address->peer))
-  {
-    /* This peer is requested, find best address */
-    update_active_address (s,
-                           get_active_address (s,
-                                               &address->peer),
-                           &address->peer);
-  }
+  update_active_address (s,
+                         get_active_address (s,
+                                             &address->peer),
+                         &address->peer);
   LOG (GNUNET_ERROR_TYPE_INFO,
        "Added new address for `%s', now total %u and active %u addresses in network `%s'\n",
        GNUNET_i2s (&address->peer),
@@ -1415,7 +1215,7 @@ libgnunet_plugin_ats_proportional_done (void *cls)
   struct AddressWrapper *next;
   unsigned int c;
 
-  for (c = 0; c < s->network_count; c++)
+  for (c = 0; c < s->env->network_count; c++)
   {
     GNUNET_break (0 == s->network_entries[c].total_addresses);
     GNUNET_break (0 == s->network_entries[c].active_addresses);