X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fats%2Fplugin_ats_proportional.c;h=9309bc2ea799df575b99b5884aea081cd2ab8552;hb=7c8c596991f6397ec3ca1911638acb2de721c7a7;hp=0c593ad966923b2e195fef0fa482f8f6f5562d02;hpb=689e3a6dc270c150b2fbe24c8a4325634d2f3fc8;p=oweals%2Fgnunet.git diff --git a/src/ats/plugin_ats_proportional.c b/src/ats/plugin_ats_proportional.c index 0c593ad96..9309bc2ea 100644 --- a/src/ats/plugin_ats_proportional.c +++ b/src/ats/plugin_ats_proportional.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2011 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 @@ -14,1047 +14,664 @@ 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 * @author Matthias Wachs * @author Christian Grothoff */ -#include "plugin_ats_proportional.h" +#include "platform.h" +#include "gnunet_statistics_service.h" +#include "gnunet_ats_service.h" +#include "gnunet_ats_plugin.h" +#include "gnunet-service-ats_addresses.h" #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. 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 + /** - * A handle for the proportional solver + * Address information stored for the proportional solver in the + * `solver_information` member of `struct GNUNET_ATS_Address`. + * + * They are also stored in the respective `struct Network`'s linked + * list. */ -struct GAS_PROPORTIONAL_Handle +struct AddressWrapper { - struct GNUNET_ATS_PluginEnvironment *env; - /** - * Statistics handle + * Next in DLL */ - struct GNUNET_STATISTICS_Handle *stats; + struct AddressWrapper *next; /** - * Hashmap containing all valid addresses + * Previous in DLL */ - struct GNUNET_CONTAINER_MultiPeerMap *addresses; + struct AddressWrapper *prev; /** - * Pending address requests + * The address */ - struct GNUNET_CONTAINER_MultiPeerMap *requests; + struct ATS_Address *addr; - /** - * Bandwidth changed callback + /** + * Network scope this address is in */ - GAS_bandwidth_changed_cb bw_changed; + struct Network *network; /** - * Bandwidth changed callback cls + * Inbound quota */ - void *bw_changed_cls; + uint32_t calculated_quota_in; /** - * ATS function to get preferences + * Outbound quota */ - GAS_get_preferences get_preferences; + uint32_t calculated_quota_out; /** - * Closure for ATS function to get preferences + * When was this address activated */ - void *get_preferences_cls; + struct GNUNET_TIME_Absolute activated; +}; + + +/** + * Representation of a network + */ +struct Network +{ /** - * ATS function to get properties + * Network description */ - GAS_get_properties get_properties; + const char *desc; /** - * Closure for ATS function to get properties + * String for statistics total addresses */ - void *get_properties_cls; + char *stat_total; /** - * Bulk lock + * String for statistics active addresses */ - int bulk_lock; + char *stat_active; /** - * Number of changes while solver was locked + * Linked list of addresses in this network: head */ - int bulk_requests; + struct AddressWrapper *head; /** - * Total number of addresses for solver + * Linked list of addresses in this network: tail */ - unsigned int total_addresses; + struct AddressWrapper *tail; /** - * Number of active addresses for solver + * Total inbound quota */ - unsigned int active_addresses; + unsigned long long total_quota_in; /** - * Networks array + * Total outbound quota */ - struct Network *network_entries; + unsigned long long total_quota_out; /** - * Number of networks + * ATS network type */ - unsigned int network_count; + enum GNUNET_ATS_Network_Type type; /** - * Proportionality factor + * Number of active addresses for this network */ - double prop_factor; + unsigned int active_addresses; /** - * Stability factor + * Number of total addresses for this network */ - double stability_factor; + unsigned int total_addresses; + }; + /** - * Representation of a network + * A handle for the proportional solver */ -struct Network +struct GAS_PROPORTIONAL_Handle { - /** - * ATS network type - */ - unsigned int type; /** - * Network description + * Our execution environment. */ - char *desc; + struct GNUNET_ATS_PluginEnvironment *env; /** - * Total inbound quota - * + * Networks array */ - unsigned long long total_quota_in; + struct Network *network_entries; /** - * Total outbound quota - * + * Proportionality factor */ - unsigned long long total_quota_out; + double prop_factor; /** - * Number of active addresses for this network + * Stability factor */ - unsigned int active_addresses; + double stability_factor; /** - * Number of total addresses for this network + * Bulk lock counter. If zero, we are not locked. */ - unsigned int total_addresses; + unsigned int bulk_lock; /** - * String for statistics total addresses + * Number of changes made while solver was locked. We really only + * use 0/non-zero to check on unlock if we have to run the update. */ - char *stat_total; + unsigned int bulk_requests; /** - * String for statistics active addresses + * Number of active addresses for solver */ - char *stat_active; + unsigned int active_addresses; - struct AddressWrapper *head; - struct AddressWrapper *tail; }; + /** - * Address information stored in the solver + * Test if bandwidth is available in this network to add an additional address. + * + * @param net the network type to check + * @param extra for how many extra addresses do we check? + * @return #GNUNET_YES or #GNUNET_NO */ -struct AddressSolverInformation +static int +is_bandwidth_available_in_network (struct Network *net, + int extra) { - struct Network *network; - - /** - * Inbound quota - * - */ - unsigned long long calculated_quota_in_NBO; - - /** - * Outbound quota - * - */ - unsigned long long calculated_quota_out_NBO; + unsigned int na; + uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__); + GNUNET_assert (((int)net->active_addresses) + extra >= 0); + na = net->active_addresses + extra; + if (0 == na) + return GNUNET_YES; + if ( ((net->total_quota_in / na) > min_bw) && + ((net->total_quota_out / na) > min_bw) ) + return GNUNET_YES; + return GNUNET_NO; +} -}; /** - * Wrapper for addresses to store them in network's linked list + * Test if all peers in this network require connectivity at level at + * least @a con. + * + * @param s the solver handle + * @param net the network type to check + * @param con connection return value threshold to check + * @return #GNUNET_YES or #GNUNET_NO */ -struct AddressWrapper +static int +all_require_connectivity (struct GAS_PROPORTIONAL_Handle *s, + struct Network *net, + unsigned int con) { - /** - * Next in DLL - */ - struct AddressWrapper *next; + struct AddressWrapper *aw; - /** - * Previous in DLL - */ - struct AddressWrapper *prev; + for (aw = net->head; NULL != aw; aw = aw->next) + if (con > + s->env->get_connectivity (s->env->cls, + &aw->addr->peer)) + return GNUNET_NO; + return GNUNET_YES; +} - /** - * The address - */ - struct ATS_Address *addr; -}; /** - * Important solver functions - * --------------------------- + * Update bandwidth assigned to peers in this network. The basic idea + * is to assign every peer in the network the minimum bandwidth, and + * then distribute the remaining bandwidth proportional to application + * preferences. + * + * @param s the solver handle + * @param net the network type to update */ - -void * -libgnunet_plugin_ats_proportional_init (void *cls) +static void +distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s, + struct Network *net) { - struct GNUNET_ATS_PluginEnvironment *env = cls; - struct GAS_PROPORTIONAL_Handle *s; - struct Network * cur; - char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString; - unsigned long long prop_factor; - unsigned long long stability_factor; - int c; + const uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__); + struct AddressWrapper *aw; + unsigned long long remaining_quota_in; + unsigned long long quota_out_used; + unsigned long long remaining_quota_out; + unsigned long long quota_in_used; + unsigned int count_addresses; + double sum_relative_peer_prefences; + double peer_weight; + double total_weight; + const double *peer_relative_prefs; - GNUNET_assert (NULL != env); - GNUNET_assert(NULL != env->cfg); - GNUNET_assert(NULL != env->bandwidth_changed_cb); - GNUNET_assert(NULL != env->get_preferences); - GNUNET_assert(NULL != env->get_property); + LOG (GNUNET_ERROR_TYPE_INFO, + "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n", + net->desc, + net->active_addresses, + net->total_quota_in, + net->total_quota_in); - s = GNUNET_new (struct GAS_PROPORTIONAL_Handle); - s->env = env; - env->sf.s_add = &GAS_proportional_address_add; - env->sf.s_address_update_property = &GAS_proportional_address_property_changed; - env->sf.s_address_update_session = &GAS_proportional_address_session_changed; - env->sf.s_address_update_inuse = &GAS_proportional_address_inuse_changed; - env->sf.s_address_update_network = &GAS_proportional_address_change_network; - env->sf.s_get = &GAS_proportional_get_preferred_address; - env->sf.s_get_stop = &GAS_proportional_stop_get_preferred_address; - env->sf.s_pref = &GAS_proportional_address_change_preference; - env->sf.s_feedback = &GAS_proportional_address_preference_feedback; - env->sf.s_del = &GAS_proportional_address_delete; - env->sf.s_bulk_start = &GAS_proportional_bulk_start; - env->sf.s_bulk_stop = &GAS_proportional_bulk_stop; - - s->stats = (struct GNUNET_STATISTICS_Handle *) env->stats; - s->bw_changed = env->bandwidth_changed_cb; - s->bw_changed_cls = env->bw_changed_cb_cls; - s->get_preferences = env->get_preferences; - s->get_preferences_cls = env->get_preference_cls; - s->get_properties = env->get_property; - s->get_properties_cls = env->get_property_cls; - s->network_count = env->network_count; - s->network_entries = GNUNET_malloc (env->network_count * sizeof (struct Network)); - - /* Init */ - s->active_addresses = 0; - s->total_addresses = 0; - s->bulk_lock = GNUNET_NO; - s->addresses = env->addresses; - s->requests = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO); - - if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number(s->env->cfg, "ats", - "PROP_STABILITY_FACTOR", &stability_factor)) + if (0 == net->active_addresses) + return; /* no addresses to update */ + + /* sanity checks */ + if ((net->active_addresses * min_bw) > net->total_quota_in) { - if ((stability_factor >= 100) && (stability_factor <= 200)) - { - s->stability_factor = ((double) stability_factor) / 100; - } - else - { - GNUNET_break (0); - s->stability_factor = PROP_STABILITY_FACTOR; - } + GNUNET_break(0); + return; } - else + if ((net->active_addresses * min_bw) > net->total_quota_out) { - GNUNET_break (0); - s->stability_factor = PROP_STABILITY_FACTOR; + GNUNET_break(0); + return; } - LOG (GNUNET_ERROR_TYPE_INFO, "Using stability factor %.3f\n", - s->stability_factor); - if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number(s->env->cfg, "ats", - "PROP_PROPORTIONALITY_FACTOR", &prop_factor)) + /* Calculate sum of relative preference for active addresses in this + network */ + sum_relative_peer_prefences = 0.0; + count_addresses = 0; + for (aw = net->head; NULL != aw; aw = aw->next) { - if (prop_factor >= 100) - s->prop_factor = ((double) prop_factor) / 100; - else - { - GNUNET_break (0); - s->prop_factor = PROPORTIONALITY_FACTOR; - } + if (GNUNET_YES != aw->addr->active) + 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]; + count_addresses++; } - else - s->prop_factor = PROPORTIONALITY_FACTOR; - LOG (GNUNET_ERROR_TYPE_INFO, "Using proportionality factor %.3f\n", - s->prop_factor); - - - for (c = 0; c < env->network_count; c++) + if (count_addresses != net->active_addresses) { - cur = &s->network_entries[c]; - cur->total_addresses = 0; - cur->active_addresses = 0; - cur->type = env->networks[c]; - cur->total_quota_in = env->in_quota[c]; - cur->total_quota_out = env->out_quota[c]; - cur->desc = net_str[c]; - GNUNET_asprintf (&cur->stat_total, - "# ATS addresses %s total", cur->desc); - GNUNET_asprintf (&cur->stat_active, - "# ATS active addresses %s total", cur->desc); - LOG (GNUNET_ERROR_TYPE_INFO, "Added network %u `%s' (%llu/%llu)\n", - c, cur->desc, cur->total_quota_in, cur->total_quota_out); + GNUNET_break (0); + LOG (GNUNET_ERROR_TYPE_WARNING, + "%s: Counted %u active addresses, expected %u active addresses\n", + net->desc, + count_addresses, + net->active_addresses); + /* try to fix... */ + net->active_addresses = count_addresses; } - return s; -} + LOG (GNUNET_ERROR_TYPE_INFO, + "Total relative preference %.3f for %u addresses in network %s\n", + sum_relative_peer_prefences, + net->active_addresses, + net->desc); -void * -libgnunet_plugin_ats_proportional_done (void *cls) -{ - struct GAS_PROPORTIONAL_Handle *s = cls; - struct AddressWrapper *cur; - struct AddressWrapper *next; - int c; - GNUNET_assert(s != NULL); - for (c = 0; c < s->network_count; c++) + /* check how much we have to distribute */ + remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw); + remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Proportionally distributable bandwidth (in/out): %llu/%llu\n", + remaining_quota_in, + remaining_quota_out); + + /* distribute remaining quota; we do not do it exactly proportional, + but balance "even" distribution ("net->active_addresses") with + the preference sum using the "prop_factor". */ + total_weight = net->active_addresses + + s->prop_factor * sum_relative_peer_prefences; + quota_out_used = 0; + quota_in_used = 0; + for (aw = net->head; NULL != aw; aw = aw->next) { - if (s->network_entries[c].total_addresses > 0) + if (GNUNET_YES != aw->addr->active) { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Had %u addresses for network `%s' not deleted during shutdown\n", - s->network_entries[c].total_addresses, s->network_entries[c].desc); - //GNUNET_break(0); + /* set to 0, just to be sure */ + aw->calculated_quota_in = 0; + aw->calculated_quota_out = 0; + continue; } + peer_relative_prefs = s->env->get_preferences (s->env->cls, + &aw->addr->peer); + peer_weight = 1.0 + + s->prop_factor * peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH]; - if (s->network_entries[c].active_addresses > 0) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Had %u active addresses for network `%s' not deleted during shutdown\n", - s->network_entries[c].active_addresses, s->network_entries[c].desc); - //GNUNET_break(0); - } + aw->calculated_quota_in = min_bw + + (peer_weight / total_weight) * remaining_quota_in; + aw->calculated_quota_out = min_bw + + (peer_weight / total_weight) * remaining_quota_out; - next = s->network_entries[c].head; - while (NULL != (cur = next)) - { - next = cur->next; - GNUNET_CONTAINER_DLL_remove(s->network_entries[c].head, - s->network_entries[c].tail, cur); - GNUNET_free_non_null (cur->addr->solver_information); - GNUNET_free(cur); - } - GNUNET_free(s->network_entries[c].stat_total); - GNUNET_free(s->network_entries[c].stat_active); - } - if (s->total_addresses > 0) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Had %u addresses not deleted during shutdown\n", s->total_addresses); - // GNUNET_break(0); - } - if (s->active_addresses > 0) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Had %u active addresses not deleted during shutdown\n", - s->active_addresses); - // GNUNET_break (0); + LOG (GNUNET_ERROR_TYPE_INFO, + "New quotas for peer `%s' with weight (cur/total) %.3f/%.3f (in/out) are: %u/%u\n", + GNUNET_i2s (&aw->addr->peer), + peer_weight, + total_weight, + (unsigned int) aw->calculated_quota_in, + (unsigned int) aw->calculated_quota_out); + quota_in_used += aw->calculated_quota_in; + quota_out_used += aw->calculated_quota_out; } - GNUNET_free (s->network_entries); - GNUNET_CONTAINER_multipeermap_destroy (s->requests); - GNUNET_free (s); - return NULL; + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Total bandwidth assigned is (in/out): %llu /%llu\n", + quota_in_used, + quota_out_used); + /* +1 due to possible rounding errors */ + GNUNET_break (quota_out_used <= net->total_quota_out + 1); + GNUNET_break (quota_in_used <= net->total_quota_in + 1); } /** - * Test if bandwidth is available in this network to add an additional address + * Notify ATS service of bandwidth changes to addresses. * - * @param net the network type to update - * @return GNUNET_YES or GNUNET_NO + * @param s solver handle + * @param net the network to propagate changes in */ -static int -is_bandwidth_available_in_network (struct Network *net) +static void +propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s, + struct Network *net) { - GNUNET_assert(NULL != net); - unsigned int na = net->active_addresses + 1; - uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__); - if (((net->total_quota_in / na) > min_bw) - && ((net->total_quota_out / na) > min_bw)) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Enough bandwidth available for %u active addresses in network `%s'\n", - na, net->desc); + struct AddressWrapper *cur; - return GNUNET_YES; + for (cur = net->head; NULL != cur; cur = cur->next) + { + if ( (cur->addr->assigned_bw_in == cur->calculated_quota_in) && + (cur->addr->assigned_bw_out == cur->calculated_quota_out) ) + continue; + cur->addr->assigned_bw_in = cur->calculated_quota_in; + cur->addr->assigned_bw_out = cur->calculated_quota_out; + if (GNUNET_YES == cur->addr->active) + s->env->bandwidth_changed_cb (s->env->cls, + cur->addr); } - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Not enough bandwidth available for %u active addresses in network `%s'\n", - na, net->desc); - return GNUNET_NO; } + +/** + * Distribute bandwidth. The addresses have already been selected, + * this is merely distributed the bandwidth among the addresses. + * + * @param s the solver handle + * @param n the network, can be NULL for all networks + */ static void -bandwidth_changed (struct GAS_PROPORTIONAL_Handle *s) +distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s, + struct Network *n) { - if (GNUNET_YES == s->bulk_lock) + unsigned int i; + + if (0 != s->bulk_lock) { s->bulk_requests++; return; } - - -} - -/** - * Update bandwidth assigned to peers in this network - * - * @param s the solver handle - * @param net the network type to update - * @param address_except address excluded from notification, since we suggest - * this address - */ -static void -distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s, - struct Network *net, struct ATS_Address *address_except) -{ - struct AddressSolverInformation *asi; - struct AddressWrapper *cur_address; - - unsigned long long remaining_quota_in = 0; - unsigned long long quota_out_used = 0; - unsigned long long remaining_quota_out = 0; - unsigned long long quota_in_used = 0; - int count_addresses; - uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__); - double relative_peer_prefence; - double sum_relative_peer_prefences; /* Important: has to be double not float due to precision */ - double cur_pref; /* Important: has to be double not float due to precision */ - double peer_weight; - double total_weight; - const double *peer_relative_prefs = NULL; /* Important: has to be double not float due to precision */ - - unsigned long long assigned_quota_in = 0; - unsigned long long assigned_quota_out = 0; - - - LOG(GNUNET_ERROR_TYPE_INFO, - "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n", - net->desc, net->active_addresses, net->total_quota_in, - net->total_quota_in); - - if (net->active_addresses == 0) - { - return; /* no addresses to update */ - } - - /* Idea - * Assign every peer in network minimum Bandwidth - * Distribute bandwidth left according to preference - */ - - if ((net->active_addresses * min_bw) > net->total_quota_in) - { - GNUNET_break(0); - return; - } - if ((net->active_addresses * min_bw) > net->total_quota_out) - { - GNUNET_break(0); - return; - } - - remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw); - remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw); - LOG(GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n", - remaining_quota_in, remaining_quota_out); - sum_relative_peer_prefences = 0.0; - - /* Calculate sum of relative preference for active addresses in this network */ - count_addresses = 0; - for (cur_address = net->head; NULL != cur_address; cur_address = cur_address->next) - { - if (GNUNET_YES != cur_address->addr->active) - continue; - - GNUNET_assert( NULL != (peer_relative_prefs = s->get_preferences (s->get_preferences_cls, - &cur_address->addr->peer))); - relative_peer_prefence = 0.0; - relative_peer_prefence += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH]; - sum_relative_peer_prefences += relative_peer_prefence; - count_addresses ++; - } - - GNUNET_assert (count_addresses == net->active_addresses); - - LOG (GNUNET_ERROR_TYPE_INFO, - "Total relative preference %.3f for %u addresses in network %s\n", - sum_relative_peer_prefences, net->active_addresses, net->desc); - - for (cur_address = net->head; NULL != cur_address; cur_address = cur_address->next) - { - if (GNUNET_YES == cur_address->addr->active) - { - GNUNET_assert( NULL != (peer_relative_prefs = - s->get_preferences (s->get_preferences_cls, &cur_address->addr->peer))); - - cur_pref = peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH]; - total_weight = net->active_addresses + - s->prop_factor * sum_relative_peer_prefences; - peer_weight = (1.0 + (s->prop_factor * cur_pref)); - - assigned_quota_in = min_bw - + ((peer_weight / total_weight) * remaining_quota_in); - assigned_quota_out = min_bw - + ((peer_weight / total_weight) * remaining_quota_out); - - LOG (GNUNET_ERROR_TYPE_INFO, - "New quota for peer `%s' with weight (cur/total) %.3f/%.3f (in/out): %llu / %llu\n", - GNUNET_i2s (&cur_address->addr->peer), peer_weight, total_weight, - assigned_quota_in, assigned_quota_out); - } - else - { - assigned_quota_in = 0; - assigned_quota_out = 0; - } - - quota_in_used += assigned_quota_in; - quota_out_used += assigned_quota_out; - /* Prevent overflow due to rounding errors */ - if (assigned_quota_in > UINT32_MAX) - assigned_quota_in = UINT32_MAX; - if (assigned_quota_out > UINT32_MAX) - assigned_quota_out = UINT32_MAX; - - /* Compare to current bandwidth assigned */ - asi = cur_address->addr->solver_information; - asi->calculated_quota_in_NBO = htonl (assigned_quota_in); - asi->calculated_quota_out_NBO = htonl (assigned_quota_out); - } - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Total bandwidth assigned is (in/out): %llu /%llu\n", quota_in_used, - quota_out_used); - if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */ + if (NULL != n) { - LOG(GNUNET_ERROR_TYPE_ERROR, - "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n", - net->active_addresses, quota_out_used, net->total_quota_out); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Redistributing bandwidth in network %s with %u active and %u total addresses\n", + GNUNET_ATS_print_network_type(n->type), + n->active_addresses, + n->total_addresses); + s->env->info_cb (s->env->cls, + GAS_OP_SOLVE_START, + GAS_STAT_SUCCESS, + GAS_INFO_PROP_SINGLE); + distribute_bandwidth(s, + n); + s->env->info_cb (s->env->cls, + GAS_OP_SOLVE_STOP, + GAS_STAT_SUCCESS, + GAS_INFO_PROP_SINGLE); + s->env->info_cb (s->env->cls, + GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, + GAS_STAT_SUCCESS, + GAS_INFO_PROP_SINGLE); + propagate_bandwidth (s, + n); + + s->env->info_cb (s->env->cls, + GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, + GAS_STAT_SUCCESS, + GAS_INFO_PROP_SINGLE); } - if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */ + else { - LOG(GNUNET_ERROR_TYPE_ERROR, - "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n", - net->active_addresses, quota_in_used, net->total_quota_in); + 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->env->network_count; i++) + distribute_bandwidth (s, + &s->network_entries[i]); + s->env->info_cb (s->env->cls, + GAS_OP_SOLVE_STOP, + GAS_STAT_SUCCESS, + GAS_INFO_PROP_ALL); + s->env->info_cb (s->env->cls, + GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, + GAS_STAT_SUCCESS, + GAS_INFO_PROP_ALL); + for (i = 0; i < s->env->network_count; i++) + propagate_bandwidth (s, + &s->network_entries[i]); + s->env->info_cb (s->env->cls, + GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, + GAS_STAT_SUCCESS, + GAS_INFO_PROP_ALL); } } + +/** + * Context for finding the best address* Linked list of addresses in this network: head + */ struct FindBestAddressCtx { + /** + * The solver handle + */ struct GAS_PROPORTIONAL_Handle *s; + + /** + * The currently best address + */ struct ATS_Address *best; }; -static int -find_property_index (uint32_t type) -{ - int existing_types[] = GNUNET_ATS_QualityProperties; - int c; - for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++) - if (existing_types[c] == type) - return c; - 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, we stick to it. - * Otherwise, we pick by lowest distance and then by lowest latency. + * Find a "good" address to use for a peer by iterating over the + * addresses for this peer. If we already have an existing address, + * we stick to it. Otherwise, we pick by lowest distance and then by + * lowest latency. * - * @param cls the 'struct ATS_Address**' where we store the result - * @param key unused - * @param value another 'struct ATS_Address*' to consider using - * @return GNUNET_OK (continue to iterate) + * @param cls the `struct FindBestAddressCtx *' where we store the result + * @param key the peer we are trying to find the best address for + * @param value another `struct ATS_Address*` to consider using + * @return #GNUNET_OK (continue to iterate) */ static int find_best_address_it (void *cls, const struct GNUNET_PeerIdentity *key, void *value) { - struct FindBestAddressCtx *ctx = (struct FindBestAddressCtx *) cls; - struct ATS_Address *current = (struct ATS_Address *) value; - struct ATS_Address *current_best = (struct ATS_Address *) value; - struct GNUNET_TIME_Absolute now; - struct AddressSolverInformation *asi; - const double *norm_prop_cur; - const double *norm_prop_best; + struct FindBestAddressCtx *ctx = cls; + struct ATS_Address *current = value; + struct AddressWrapper *asi = current->solver_information; + struct GNUNET_TIME_Relative active_time; double best_delay; double best_distance; double cur_delay; double cur_distance; - int index; - - current_best = NULL; - asi = current->solver_information; - now = GNUNET_TIME_absolute_get (); - - if (current->blocked_until.abs_value_us - == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value_us) + unsigned int con; + int bw_available; + int need; + + /* we need +1 slot if 'current' is not yet active */ + need = (GNUNET_YES == current->active) ? 0 : 1; + /* we save -1 slot if 'best' is active and belongs + to the same network (as we would replace it) */ + if ( (NULL != ctx->best) && + (GNUNET_YES == ctx->best->active) && + (((struct AddressWrapper *) ctx->best->solver_information)->network == + asi->network) ) + need--; + /* we can gain -1 slot if this peers connectivity + requirement is higher than that of another peer + in that network scope */ + con = ctx->s->env->get_connectivity (ctx->s->env->cls, + key); + if (GNUNET_YES != + all_require_connectivity (ctx->s, + asi->network, + con)) + need--; + /* test if minimum bandwidth for 'current' would be available */ + bw_available + = is_bandwidth_available_in_network (asi->network, + need); + if (! bw_available) { - /* This address is blocked for suggestion */ - LOG(GNUNET_ERROR_TYPE_DEBUG, "Address %p blocked for suggestion for %s \n", - current, - GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (now, current->blocked_until), GNUNET_YES)); + /* Bandwidth for this address is unavailable, so we cannot use + it. */ return GNUNET_OK; } - if (NULL == asi) + if (GNUNET_YES == current->active) { - GNUNET_break (0); - return GNUNET_OK; - } - - if (GNUNET_NO == is_bandwidth_available_in_network (asi->network)) - return GNUNET_OK; /* There's no bandwidth available in this network */ - - if (NULL != ctx->best) - { - /* Compare current addresses with denominated 'best' address */ - - current_best = ctx->best; - if ((0 != ctx->best->addr_len) && (0 == current->addr_len)) + active_time = GNUNET_TIME_absolute_get_duration (asi->activated); + if (active_time.rel_value_us <= + ((double) GNUNET_TIME_UNIT_SECONDS.rel_value_us) * ctx->s->stability_factor) { - /* saved address was an outbound address, but we have an inbound address */ - current_best = current; - goto end; - } - if (ctx->best->t_last_activity.abs_value_us < current->t_last_activity.abs_value_us) - { - /* Current address is newer */ - current_best = current; - } - if (ctx->best->t_added.abs_value_us < current->t_added.abs_value_us) - { - /* Current address is newer */ - current_best = current; - goto end; + /* Keep active address for stability reasons */ + ctx->best = current; + return GNUNET_NO; } } - else + if (NULL == ctx->best) { - /* We do not have a 'best' address so take this address */ - LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting initial address %p\n", current); - current_best = current; - goto end; + /* We so far have nothing else, so go with it! */ + ctx->best = current; + return GNUNET_OK; } /* Now compare ATS information */ - norm_prop_cur = ctx->s->get_properties (ctx->s->get_properties_cls, - (const struct ATS_Address *) current); - index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE); - cur_distance = norm_prop_cur[index]; - index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY); - cur_delay = norm_prop_cur[index]; - - norm_prop_best = ctx->s->get_properties (ctx->s->get_properties_cls, - (const struct ATS_Address *) ctx->best); - index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE); - best_distance = norm_prop_best[index]; - index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY); - best_delay = norm_prop_best[index]; + 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) { - if (GNUNET_NO == ctx->best->active) { - current_best = current; /* Use current */ + /* Activity doesn't influence the equation, use current */ + ctx->best = current; } else if ((best_distance / cur_distance) > ctx->s->stability_factor) { - /* Best and active address performs worse */ - current_best = current; + /* Distance change is significant, switch active address! */ + ctx->best = current; } } - else - { - /* Use current best */ - current_best = ctx->best; - } /* User connection with less delay */ if (cur_delay < best_delay) { - if (GNUNET_NO == ctx->best->active) { - current_best = current; /* Use current */ + /* Activity doesn't influence the equation, use current */ + ctx->best = current; } else if ((best_delay / cur_delay) > ctx->s->stability_factor) { - /* Best and active address performs worse */ - current_best = current; + /* Latency change is significant, switch active address! */ + ctx->best = current; } - else - { - //GNUNET_break (0); - } - } - else - { - /* Use current best */ - current_best = ctx->best; } - -end: - ctx->best = current_best; return GNUNET_OK; } + +/** + * Find the currently best address for a peer from the set of + * addresses available or return NULL of no address is available. + * + * @param s the proportional handle + * @param addresses the address hashmap + * @param id the peer id + * @return the address or NULL + */ struct ATS_Address * get_best_address (struct GAS_PROPORTIONAL_Handle *s, - struct GNUNET_CONTAINER_MultiPeerMap *addresses, - const struct GNUNET_PeerIdentity *id) + struct GNUNET_CONTAINER_MultiPeerMap *addresses, + const struct GNUNET_PeerIdentity *id) { struct FindBestAddressCtx fba_ctx; + fba_ctx.best = NULL; fba_ctx.s = s; - - GNUNET_CONTAINER_multipeermap_get_multiple (addresses, id, - &find_best_address_it, &fba_ctx); - + GNUNET_CONTAINER_multipeermap_get_multiple (addresses, + id, + &find_best_address_it, + &fba_ctx); return fba_ctx.best; } -/** - * Helper functions - * --------------------------- - */ -static void -propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s, - struct Network *net, struct ATS_Address *address_except) -{ - struct AddressWrapper *cur; - struct AddressSolverInformation *asi; - for (cur = net->head; NULL != cur; cur = cur->next) - { - asi = cur->addr->solver_information; - if ( (cur->addr->assigned_bw_in.value__ != asi->calculated_quota_in_NBO) || - (cur->addr->assigned_bw_out.value__ != asi->calculated_quota_out_NBO) ) - { - cur->addr->assigned_bw_in.value__ = asi->calculated_quota_in_NBO; - cur->addr->assigned_bw_out.value__ = asi->calculated_quota_out_NBO; - - /* Reset for next iteration */ - asi->calculated_quota_in_NBO = htonl (0); - asi->calculated_quota_out_NBO = htonl (0); - - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Bandwidth for %s address %p for peer `%s' changed to %u/%u\n", - (GNUNET_NO == cur->addr->active) ? "inactive" : "active", - cur->addr, - GNUNET_i2s (&cur->addr->peer), - ntohl (cur->addr->assigned_bw_in.value__), - ntohl (cur->addr->assigned_bw_out.value__ )); - - /* Notify on change */ - if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except)) - s->bw_changed (s->bw_changed_cls, cur->addr); - } - } -} /** - * Distribibute bandwidth + * Decrease number of active addresses in network. * * @param s the solver handle - * @param n the network, can be NULL for all network - * @param address_except do not notify for this address + * @param net the network type */ static void -distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s, - struct Network *n, struct ATS_Address *address_except) +address_decrement_active (struct GAS_PROPORTIONAL_Handle *s, + struct Network *net) { - if (GNUNET_YES == s->bulk_lock) - { - s->bulk_requests++; - return; - } - - if (NULL != n) - { - LOG (GNUNET_ERROR_TYPE_INFO, - "Redistributing bandwidth in network %s with %u active and %u total addresses\n", - GNUNET_ATS_print_network_type(n->type), - n->active_addresses, n->total_addresses); - - if (NULL != s->env->info_cb) - s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_START, - GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE); - - /* Distribute */ - distribute_bandwidth(s, n, address_except); - - if (NULL != s->env->info_cb) - s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_STOP, - GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE); - if (NULL != s->env->info_cb) - s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, - GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE); - - /* Do propagation */ - propagate_bandwidth (s, n, address_except); - - if (NULL != s->env->info_cb) - s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, - GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE); - } - else - { - int i; - if (NULL != s->env->info_cb) - s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_START, - GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL); - for (i = 0; i < s->network_count; i++) - { - /* Distribute */ - distribute_bandwidth(s, &s->network_entries[i], NULL); - } - - if (NULL != s->env->info_cb) - s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_STOP, - GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL); - if (NULL != s->env->info_cb) - s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, - GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL); - for (i = 0; i < s->network_count; i++) - { - /* Do propagation */ - propagate_bandwidth(s, &s->network_entries[i], address_except); - } - if (NULL != s->env->info_cb) - s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, - GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL); - } + GNUNET_assert (net->active_addresses > 0); + net->active_addresses--; + GNUNET_STATISTICS_update (s->env->stats, + net->stat_active, + -1, + GNUNET_NO); + GNUNET_assert (s->active_addresses > 0); + s->active_addresses--; + GNUNET_STATISTICS_update (s->env->stats, + "# ATS addresses total", + -1, + GNUNET_NO); } -/** - * Lookup network struct by type - * - * @param s the solver handle - * @param type the network type - * @return the network struct - */ -static struct Network * -get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type) -{ - int c; - for (c = 0; c < s->network_count; c++) - { - if (s->network_entries[c].type == type) - return &s->network_entries[c]; - } - return NULL ; -} /** - * Hashmap Iterator to find current active address for peer + * Address map iterator to find current active address for peer. + * Asserts that only one address is active per peer. * * @param cls last active address * @param key peer's key @@ -1063,191 +680,190 @@ get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type) */ static int get_active_address_it (void *cls, - const struct GNUNET_PeerIdentity *key, - void *value) + const struct GNUNET_PeerIdentity *key, + void *value) { struct ATS_Address **dest = cls; - struct ATS_Address *aa = (struct ATS_Address *) value; + struct ATS_Address *aa = value; - if (GNUNET_YES == aa->active) - { - - if (NULL != (*dest)) - { - /* should never happen */ - LOG(GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", - GNUNET_i2s (&aa->peer)); - GNUNET_break(0); - return GNUNET_NO; - } - (*dest) = aa; - } + if (GNUNET_YES != aa->active) + return GNUNET_OK; + GNUNET_assert (NULL == (*dest)); + (*dest) = aa; return GNUNET_OK; } + /** * Find current active address for peer * - * @param solver the solver handle - * @param addresses the address set + * @param s the solver handle * @param peer the peer * @return active address or NULL */ static struct ATS_Address * -get_active_address (void *solver, - const struct GNUNET_CONTAINER_MultiPeerMap * addresses, - const struct GNUNET_PeerIdentity *peer) +get_active_address (struct GAS_PROPORTIONAL_Handle *s, + const struct GNUNET_PeerIdentity *peer) { - struct ATS_Address * dest = NULL; + struct ATS_Address *dest; - GNUNET_CONTAINER_multipeermap_get_multiple (addresses, peer, - &get_active_address_it, &dest); + dest = NULL; + GNUNET_CONTAINER_multipeermap_get_multiple (s->env->addresses, + peer, + &get_active_address_it, + &dest); return dest; } +/** + * Update active address for a peer. Check if active address exists + * and what the best address is, if addresses are different switch. + * Then reallocate bandwidth within the affected network scopes. + * + * @param s solver handle + * @param current_address the address currently active for the peer, + * NULL for none + * @param peer the peer to check + */ static void -addresse_increment (struct GAS_PROPORTIONAL_Handle *s, struct Network *net, - int total, int active) +update_active_address (struct GAS_PROPORTIONAL_Handle *s, + struct ATS_Address *current_address, + const struct GNUNET_PeerIdentity *peer) { - if (GNUNET_YES == total) - { - s->total_addresses++; - net->total_addresses++; - GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO); - GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO); - } - if (GNUNET_YES == active) - { - net->active_addresses++; - s->active_addresses++; - GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, - GNUNET_NO); - GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO); - } - -} - - -static int -addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net, - int total, int active) -{ - int res = GNUNET_OK; - if (GNUNET_YES == total) + struct ATS_Address *best_address; + struct AddressWrapper *asi_cur; + struct AddressWrapper *asi_best; + struct AddressWrapper *aw; + struct AddressWrapper *aw_min; + unsigned int a_con; + unsigned int con_min; + + best_address = get_best_address (s, + s->env->addresses, + peer); + if (NULL != best_address) + asi_best = best_address->solver_information; + else + asi_best = NULL; + if (current_address == best_address) + return; /* no changes */ + if (NULL != current_address) { - if (s->total_addresses < 1) - { - GNUNET_break(0); - res = GNUNET_SYSERR; - } - else - { - s->total_addresses--; - GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, - GNUNET_NO); - } - if (net->total_addresses < 1) - { - GNUNET_break(0); - res = GNUNET_SYSERR; - } - else + /* We switch to a new address (or to none); + mark old address as inactive. */ + asi_cur = current_address->solver_information; + GNUNET_assert (GNUNET_YES == current_address->active); + LOG (GNUNET_ERROR_TYPE_INFO, + "Disabling previous active address for peer `%s'\n", + GNUNET_i2s (peer)); + asi_cur->activated = GNUNET_TIME_UNIT_ZERO_ABS; + current_address->active = GNUNET_NO; + current_address->assigned_bw_in = 0; + current_address->assigned_bw_out = 0; + address_decrement_active (s, + asi_cur->network); + if ( (NULL == best_address) || + (asi_best->network != asi_cur->network) ) + distribute_bandwidth_in_network (s, + asi_cur->network); + if (NULL == best_address) { - net->total_addresses--; - GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO); + /* We previously had an active address, but now we cannot + * suggest one. Therefore we have to disconnect the peer. + * The above call to "distribute_bandwidth_in_network() + * does not see 'current_address' so we need to trigger + * the update here. */ + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Disconnecting peer `%s'.\n", + GNUNET_i2s (peer)); + s->env->bandwidth_changed_cb (s->env->cls, + current_address); + return; } } + if (NULL == best_address) + { + /* We do not have a new address, so we are done. */ + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Cannot suggest address for peer `%s'\n", + GNUNET_i2s (peer)); + return; + } + /* We do have a new address, activate it */ + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Selecting new address %p for peer `%s'\n", + best_address, + GNUNET_i2s (peer)); + /* Mark address as active */ + best_address->active = GNUNET_YES; + asi_best->activated = GNUNET_TIME_absolute_get (); + asi_best->network->active_addresses++; + s->active_addresses++; + GNUNET_STATISTICS_update (s->env->stats, + "# ATS active addresses total", + 1, + GNUNET_NO); + GNUNET_STATISTICS_update (s->env->stats, + asi_best->network->stat_active, + 1, + GNUNET_NO); + LOG (GNUNET_ERROR_TYPE_INFO, + "Address %p for peer `%s' is now active\n", + best_address, + GNUNET_i2s (peer)); - if (GNUNET_YES == active) + if (GNUNET_NO == + is_bandwidth_available_in_network (asi_best->network, + 0)) { - if (net->active_addresses < 1) - { - GNUNET_break(0); - res = GNUNET_SYSERR; - } - else - { - net->active_addresses--; - GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO); - } - if (s->active_addresses < 1) + /* we went over the maximum number of addresses for + this scope; remove the address with the smallest + connectivity requirement */ + con_min = UINT32_MAX; + aw_min = NULL; + for (aw = asi_best->network->head; NULL != aw; aw = aw->next) { - GNUNET_break(0); - res = GNUNET_SYSERR; - } - else - { - s->active_addresses--; - GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, - GNUNET_NO); + 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; + if (0 == con_min) + break; + } } + update_active_address (s, + aw_min->addr, + &aw_min->addr->peer); } - return res; -} - -static int -address_eq (struct ATS_Address *a, struct ATS_Address *b) -{ - GNUNET_assert (NULL != a); - GNUNET_assert (NULL != b); - if (0 != strcmp(a->plugin, b->plugin)) - return GNUNET_NO; - if (a->addr_len != b->addr_len) - return GNUNET_NO; - if (0 != memcmp (a->addr, b->addr, b->addr_len)) - return GNUNET_NO; - if (a->session_id != b->session_id) - return GNUNET_NO; - return GNUNET_YES; + distribute_bandwidth_in_network (s, + asi_best->network); } -/** - * Solver API functions - * --------------------------- - */ /** - * Changes the preferences for a peer in the problem + * The preferences for a peer in the problem changed. * * @param solver the solver handle * @param peer the peer to change the preference for * @param kind the kind to change the preference * @param pref_rel the normalized preference value for this kind over all clients */ -void -GAS_proportional_address_change_preference (void *solver, - const struct GNUNET_PeerIdentity *peer, - enum GNUNET_ATS_PreferenceKind kind, - double pref_rel) +static void +GAS_proportional_change_preference (void *solver, + const struct GNUNET_PeerIdentity *peer, + enum GNUNET_ATS_PreferenceKind kind, + double pref_rel) { struct GAS_PROPORTIONAL_Handle *s = solver; - struct ATS_Address *best_address; - struct ATS_Address *active_address; - - GNUNET_assert(NULL != solver); - GNUNET_assert(NULL != peer); - - if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, peer)) - return; /* Peer is not requested */ - - /* This peer is requested, find best address */ - active_address = get_active_address(s, s->addresses, peer); - best_address = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, peer); - - if (NULL == best_address) - return; /* No address to suggest */ - - if ((NULL == active_address) || ((NULL != active_address) && - (GNUNET_NO == address_eq (active_address, best_address)))) - { - /* We now have an active address */ - s->bw_changed (s->bw_changed_cls, best_address); - return; - } - /* Preferences changed, we have to recalculate bandwidth distribution */ - distribute_bandwidth_in_network (s, NULL, NULL); + if (GNUNET_ATS_PREFERENCE_BANDWIDTH != kind) + return; /* we do not care */ + distribute_bandwidth_in_network (s, + NULL); } @@ -1261,618 +877,365 @@ GAS_proportional_address_change_preference (void *solver, * @param kind the kind to change the preference * @param score the score */ -void -GAS_proportional_address_preference_feedback (void *solver, void *application, - const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_TIME_Relative scope, - enum GNUNET_ATS_PreferenceKind kind, double score) +static void +GAS_proportional_feedback (void *solver, + struct GNUNET_SERVICE_Client *application, + const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_TIME_Relative scope, + enum GNUNET_ATS_PreferenceKind kind, + double score) { - struct GAS_PROPORTIONAL_Handle *s = solver; - GNUNET_assert(NULL != solver); - GNUNET_assert(NULL != peer); - - GNUNET_assert(NULL != s); - GNUNET_break(0); + /* Proportional does not care about feedback */ } + /** * Get the preferred address for a specific peer * - * - * * @param solver the solver handle * @param peer the identity of the peer */ -const struct ATS_Address * -GAS_proportional_get_preferred_address (void *solver, - const struct GNUNET_PeerIdentity *peer) +static void +GAS_proportional_start_get_address (void *solver, + const struct GNUNET_PeerIdentity *peer) { struct GAS_PROPORTIONAL_Handle *s = solver; - struct Network *net_prev; - struct Network *net_cur; - struct ATS_Address *prev; - struct ATS_Address *best_address; - struct AddressSolverInformation *asi; - struct AddressSolverInformation *asi_prev; - GNUNET_assert(s != NULL); - GNUNET_assert(peer != NULL); - - /* Add to list of pending requests */ - if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, peer)) - { - GNUNET_assert( - GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (s->requests, peer, NULL, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); - } - - /* Find best address */ - best_address = get_best_address (s,s->addresses, peer); - if (NULL == best_address) - { - LOG (GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n", - GNUNET_i2s (peer)); - return NULL ; - } - - LOG (GNUNET_ERROR_TYPE_INFO, "Suggesting %s address %p for peer `%s'\n", - (GNUNET_NO == best_address->active) ? "inactive" : "active", best_address, - GNUNET_i2s (peer)); - - asi = best_address->solver_information; - net_cur = asi->network ; - if (GNUNET_YES == best_address->active) - { - /* This address was selected previously, so no need to update quotas */ - return best_address; - } - - /* This address was not active, so we have to: - * - * - mark previous active address as not active - * - update quota for previous address network - * - update quota for this address network - */ - prev = get_active_address (s, s->addresses, peer); - if (NULL != prev) - { - asi_prev = prev->solver_information; - net_prev = asi_prev->network; - prev->active = GNUNET_NO; /* No active any longer */ - prev->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */ - prev->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */ - - if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES)) - GNUNET_break(0); - - /* Update network of previous address */ - distribute_bandwidth_in_network (s, net_prev, NULL); - } - - if (GNUNET_NO == (is_bandwidth_available_in_network (net_cur))) - { - /* This should never happen, because we checked when finding best address */ - GNUNET_break(0); - return NULL ; - } - - /* Mark address as active */ - best_address->active = GNUNET_YES; - addresse_increment (s, net_cur, GNUNET_NO, GNUNET_YES); - - /* Distribute bandwidth */ - distribute_bandwidth_in_network (s, net_cur, best_address); - return best_address; + update_active_address (s, + get_active_address (s, + peer), + peer); } + /** * Stop notifying about address and bandwidth changes for this peer * * @param solver the solver handle * @param peer the peer */ -void -GAS_proportional_stop_get_preferred_address (void *solver, - const struct GNUNET_PeerIdentity *peer) +static void +GAS_proportional_stop_get_address (void *solver, + const struct GNUNET_PeerIdentity *peer) { struct GAS_PROPORTIONAL_Handle *s = solver; struct ATS_Address *cur; - struct AddressSolverInformation *asi; - struct Network *cur_net; - - if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (s->requests, peer)) - GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (s->requests, - peer, NULL)); - - cur = get_active_address (s, s->addresses, peer); - if (NULL != cur) - { - LOG(GNUNET_ERROR_TYPE_INFO, - "Disabling %s address %p for peer `%s'\n", - (GNUNET_NO == cur->active) ? "inactive" : "active", cur, - GNUNET_i2s (&cur->peer)); - - /* Disabling current address */ - asi = cur->solver_information; - cur_net = asi->network ; - cur->active = GNUNET_NO; /* No active any longer */ - cur->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */ - cur->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */ - - if (GNUNET_SYSERR == addresse_decrement (s, cur_net, GNUNET_NO, GNUNET_YES)) - GNUNET_break(0); - - distribute_bandwidth_in_network (s, cur_net, NULL ); - } - return; -} - -/** - * Remove an address from the solver - * - * @param solver the solver handle - * @param address the address to remove - * @param session_only delete only session not whole address - */ -void -GAS_proportional_address_delete (void *solver, struct ATS_Address *address, - int session_only) -{ - struct GAS_PROPORTIONAL_Handle *s = solver; - struct Network *net; - struct AddressWrapper *aw; - struct AddressSolverInformation *asi; - const struct ATS_Address *new_address; - - - /* Remove an adress completely, we have to: - * - Remove from specific network - * - Decrease number of total addresses - * - If active: - * - decrease number of active addreses - * - update quotas - */ - asi = address->solver_information; + struct AddressWrapper *asi; - if (NULL == asi) - { - GNUNET_break (0); + cur = get_active_address (s, + peer); + if (NULL == cur) return; - } - net = asi->network; - - if (GNUNET_NO == session_only) - { - LOG(GNUNET_ERROR_TYPE_INFO, - "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n", - (GNUNET_NO == address->active) ? "inactive" : "active", address, - GNUNET_i2s (&address->peer), net->desc, net->total_addresses, - net->active_addresses); - - /* Remove address */ - addresse_decrement (s, net, GNUNET_YES, GNUNET_NO); - for (aw = net->head; NULL != aw; aw = aw->next) - { - if (aw->addr == address) - break; - } - if (NULL == aw) - { - GNUNET_break(0); - return; - } - GNUNET_CONTAINER_DLL_remove(net->head, net->tail, aw); - GNUNET_free(aw); - } - else - { - /* Remove session only: remove if active and update */ - LOG(GNUNET_ERROR_TYPE_INFO, - "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n", - (GNUNET_NO == address->active) ? "inactive" : "active", address, - GNUNET_i2s (&address->peer), net->desc, net->total_addresses, - net->active_addresses); - } - - if (GNUNET_YES == address->active) - { - /* Address was active, remove from network and update quotas*/ - address->active = GNUNET_NO; - address->assigned_bw_in = BANDWIDTH_ZERO; - address->assigned_bw_out = BANDWIDTH_ZERO; - asi->calculated_quota_in_NBO = htonl (0); - asi->calculated_quota_out_NBO = htonl (0); - - if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES)) - GNUNET_break(0); - distribute_bandwidth_in_network (s, net, NULL); - - if (NULL == (new_address = GAS_proportional_get_preferred_address (s, &address->peer))) - { - /* No alternative address found, disconnect peer */ - s->bw_changed (s->bw_changed_cls, address); - } - else - { - /* We found an alternative, notify about it */ - s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) new_address); - } - } - if (GNUNET_NO == session_only) - { - GNUNET_free_non_null (address->solver_information); - address->solver_information = NULL; - } - - LOG(GNUNET_ERROR_TYPE_INFO, - "After deleting address now total %u and active %u addresses in network `%s'\n", - net->total_addresses, net->active_addresses, net->desc); - + asi = cur->solver_information; + distribute_bandwidth_in_network (s, + asi->network); } + /** * Start a bulk operation * * @param solver the solver */ -void +static void GAS_proportional_bulk_start (void *solver) { - LOG(GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n"); - struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver; + struct GAS_PROPORTIONAL_Handle *s = solver; - GNUNET_assert(NULL != solver); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Locking solver for bulk operation ...\n"); + GNUNET_assert (NULL != solver); s->bulk_lock++; } /** - * Bulk operation done + * Bulk operation done. + * + * @param solver our `struct GAS_PROPORTIONAL_Handle *` */ -void +static void GAS_proportional_bulk_stop (void *solver) { - LOG(GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n"); - - struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver; - GNUNET_assert(NULL != solver); + struct GAS_PROPORTIONAL_Handle *s = solver; + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Unlocking solver from bulk operation ...\n"); if (s->bulk_lock < 1) { GNUNET_break(0); return; } s->bulk_lock--; - if ((0 == s->bulk_lock) && (0 < s->bulk_requests)) + if ( (0 == s->bulk_lock) && + (0 < s->bulk_requests) ) { - LOG(GNUNET_ERROR_TYPE_DEBUG, "No lock pending, recalculating\n"); - distribute_bandwidth_in_network (s, NULL, NULL); + LOG (GNUNET_ERROR_TYPE_INFO, + "No lock pending, recalculating\n"); + distribute_bandwidth_in_network (s, + NULL); s->bulk_requests = 0; } } -/** - * Add a new single address to a network - * - * @param solver the solver Handle - * @param address the address to add - * @param network network type of this address - */ -void -GAS_proportional_address_add (void *solver, struct ATS_Address *address, - uint32_t network); - /** * Transport properties for this address have changed * * @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 */ -void +static void GAS_proportional_address_property_changed (void *solver, - struct ATS_Address *address, uint32_t type, uint32_t abs_value, - double rel_value) + struct ATS_Address *address) { - struct GAS_PROPORTIONAL_Handle *s; - struct Network *n; - struct AddressSolverInformation *asi; - struct ATS_Address *best_address; - struct ATS_Address *active_address; - - GNUNET_assert(NULL != solver); - GNUNET_assert(NULL != address); - - s = (struct GAS_PROPORTIONAL_Handle *) solver; - asi = address->solver_information; - if (NULL == asi) - { - GNUNET_break(0); - return; - } - - n = asi->network; - if (NULL == n) - { - GNUNET_break(0); - return; - } - - LOG(GNUNET_ERROR_TYPE_INFO, - "Property `%s' for peer `%s' address %p changed to %.2f \n", - GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer), - address, rel_value); - - if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer)) - return; /* Peer is not requested */ - - /* This peer is requested, find active and best address */ - active_address = get_active_address(s, s->addresses, &address->peer); - best_address = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, &address->peer); - - if (NULL == best_address) - return; /* No address to suggest */ - - if ((NULL != active_address) && GNUNET_YES == address_eq (active_address, best_address)) - { - /* We kept the active address, just redistribute */ - distribute_bandwidth_in_network (s, n, NULL); - return; - } + struct GAS_PROPORTIONAL_Handle *s = solver; + struct AddressWrapper *asi = address->solver_information; - if ((NULL == active_address) || ((NULL != active_address) && - (GNUNET_NO == address_eq (active_address, best_address)))) - { - /* We switched active addresses */ - s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) best_address); - } + distribute_bandwidth_in_network (s, + asi->network); } + /** - * Transport session for this address has changed - * - * NOTE: values in addresses are already updated + * Add a new single address to a network * - * @param solver solver handle - * @param address the address - * @param cur_session the current session - * @param new_session the new session + * @param solver the solver Handle + * @param address the address to add + * @param network network type of this address */ -void -GAS_proportional_address_session_changed (void *solver, - struct ATS_Address *address, uint32_t cur_session, uint32_t new_session) +static void +GAS_proportional_address_add (void *solver, + struct ATS_Address *address, + enum GNUNET_ATS_Network_Type network) { struct GAS_PROPORTIONAL_Handle *s = solver; - struct ATS_Address *best_address; - struct ATS_Address *active_address; - struct AddressSolverInformation *asi; - struct Network *net_cur; - - s = (struct GAS_PROPORTIONAL_Handle *) solver; - if (cur_session != new_session) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, "Session changed from %u to %u\n", cur_session, - new_session); - } - - if (NULL == address->solver_information) - { - GNUNET_break (0); - return; - } - - if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer)) - return; /* Peer is not requested */ - - /* This peer is requested, find active and best address */ - active_address = get_active_address(s, s->addresses, &address->peer); - best_address = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, &address->peer); - - asi = active_address->solver_information; - if (NULL == asi) - { - GNUNET_break (0); - return; - } - - net_cur = asi->network ; - - if (NULL == best_address) - return; /* No address to suggest */ + struct Network *net; + struct AddressWrapper *aw; - if ((NULL != active_address) && GNUNET_YES == address_eq (active_address, best_address)) - { - /* We kept the active address, just redistribute */ - distribute_bandwidth_in_network (s, net_cur, NULL); - return; - } + GNUNET_assert (network < s->env->network_count); + net = &s->network_entries[network]; + net->total_addresses++; - if ((NULL == active_address) || ((NULL != active_address) && - (GNUNET_NO == address_eq (active_address, best_address)))) - { - /* We switched active addresses */ - s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) best_address); - } + aw = GNUNET_new (struct AddressWrapper); + aw->addr = address; + aw->network = net; + address->solver_information = aw; + GNUNET_CONTAINER_DLL_insert (net->head, + net->tail, + aw); + GNUNET_STATISTICS_update (s->env->stats, + "# ATS addresses total", + 1, + GNUNET_NO); + GNUNET_STATISTICS_update (s->env->stats, + net->stat_total, + 1, + GNUNET_NO); + 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), + net->total_addresses, + net->active_addresses, + net->desc); } + /** - * Usage for this address has changed - * - * NOTE: values in addresses are already updated + * Remove an address from the solver. To do so, we: + * - Removed it from specific network + * - Decrease the number of total addresses + * - If active: + * - decrease number of active addreses + * - update quotas * - * @param solver solver handle - * @param address the address - * @param in_use usage state + * @param solver the solver handle + * @param address the address to remove */ -void -GAS_proportional_address_inuse_changed (void *solver, - struct ATS_Address *address, int in_use) +static void +GAS_proportional_address_delete (void *solver, + struct ATS_Address *address) { - LOG(GNUNET_ERROR_TYPE_DEBUG, "Usage changed to %s\n", - (GNUNET_YES == in_use) ? "USED" : "UNUSED"); + struct GAS_PROPORTIONAL_Handle *s = solver; + struct AddressWrapper *aw = address->solver_information; + struct Network *net = aw->network; + + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Deleting %s address for peer `%s' from network `%s' (total: %u/active: %u)\n", + (GNUNET_NO == address->active) ? "inactive" : "active", + GNUNET_i2s (&address->peer), + net->desc, + net->total_addresses, + net->active_addresses); + + GNUNET_CONTAINER_DLL_remove (net->head, + net->tail, + aw); + GNUNET_assert (net->total_addresses > 0); + net->total_addresses--; + GNUNET_STATISTICS_update (s->env->stats, + net->stat_total, + -1, + GNUNET_NO); + if (GNUNET_YES == address->active) + { + /* Address was active, remove from network and update quotas */ + update_active_address (s, + address, + &address->peer); + distribute_bandwidth_in_network (s, net); + } + GNUNET_free (aw); + address->solver_information = NULL; + LOG (GNUNET_ERROR_TYPE_DEBUG, + "After deleting address now total %u and active %u addresses in network `%s'\n", + net->total_addresses, + net->active_addresses, + net->desc); } + /** - * Network scope for this address has changed - * - * NOTE: values in addresses are already updated + * Function invoked when the plugin is loaded. * - * @param solver solver handle - * @param address the address - * @param current_network the current network - * @param new_network the new network + * @param[in,out] cls the `struct GNUNET_ATS_PluginEnvironment *` to use; + * modified to return the API functions (ugh). + * @return the `struct GAS_PROPORTIONAL_Handle` to pass as a closure */ -void -GAS_proportional_address_change_network (void *solver, - struct ATS_Address *address, uint32_t current_network, uint32_t new_network) +void * +libgnunet_plugin_ats_proportional_init (void *cls) { - struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver; - struct AddressSolverInformation *asi; - int save_active = GNUNET_NO; - - struct Network *new_net = NULL; - - if (current_network == new_network) - { - GNUNET_break(0); - return; - } - - asi = address->solver_information; - if (NULL == asi) - { - GNUNET_break(0); - return; - } - - /* Network changed */ - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Network type changed, moving %s address from `%s' to `%s'\n", - (GNUNET_YES == address->active) ? "active" : "inactive", - GNUNET_ATS_print_network_type (current_network), - GNUNET_ATS_print_network_type (new_network)); - - save_active = address->active; - - /* Disable and assign no bandwidth */ - address->active = GNUNET_NO; - address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */ - address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */ - - /* Remove from old network */ - GAS_proportional_address_delete (solver, address, GNUNET_NO); + static struct GNUNET_ATS_SolverFunctions sf; + struct GNUNET_ATS_PluginEnvironment *env = cls; + struct GAS_PROPORTIONAL_Handle *s; + struct Network * cur; + float f_tmp; + unsigned int c; - /* Set new network type */ - if (NULL == (new_net = get_network (solver, new_network))) + s = GNUNET_new (struct GAS_PROPORTIONAL_Handle); + s->env = env; + sf.cls = s; + sf.s_add = &GAS_proportional_address_add; + sf.s_address_update_property = &GAS_proportional_address_property_changed; + sf.s_get = &GAS_proportional_start_get_address; + sf.s_get_stop = &GAS_proportional_stop_get_address; + sf.s_pref = &GAS_proportional_change_preference; + sf.s_feedback = &GAS_proportional_feedback; + sf.s_del = &GAS_proportional_address_delete; + sf.s_bulk_start = &GAS_proportional_bulk_start; + sf.s_bulk_stop = &GAS_proportional_bulk_stop; + s->stability_factor = PROP_STABILITY_FACTOR; + if (GNUNET_SYSERR != + GNUNET_CONFIGURATION_get_value_float (env->cfg, + "ats", + "PROP_STABILITY_FACTOR", + &f_tmp)) { - /* Address changed to invalid network... */ - LOG(GNUNET_ERROR_TYPE_ERROR, - _("Invalid network type `%u' `%s': Disconnect!\n"), new_network, - GNUNET_ATS_print_network_type (new_network)); - - /* Find new address to suggest since no bandwidth in network*/ - if (NULL == GAS_proportional_get_preferred_address (s, &address->peer)) + if ((f_tmp < 1.0) || (f_tmp > 2.0)) { - /* No alternative address found, disconnect peer */ - s->bw_changed (s->bw_changed_cls, address); + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Invalid %s configuration %f \n"), + "PROP_STABILITY_FACTOR", + f_tmp); + } + else + { + s->stability_factor = f_tmp; + LOG (GNUNET_ERROR_TYPE_INFO, + "Using %s of %.3f\n", + "PROP_STABILITY_FACTOR", + f_tmp); } - return; } - - /* Add to new network and update*/ - asi->network = new_net; - GAS_proportional_address_add (solver, address, new_network); - if (GNUNET_YES == save_active) + s->prop_factor = PROPORTIONALITY_FACTOR; + if (GNUNET_SYSERR != + GNUNET_CONFIGURATION_get_value_float (env->cfg, + "ats", + "PROP_PROPORTIONALITY_FACTOR", + &f_tmp)) { - /* check if bandwidth available in new network */ - if (GNUNET_YES == (is_bandwidth_available_in_network (new_net))) + if (f_tmp < 1.0) { - /* Assign bandwidth to updated address */ - address->active = GNUNET_YES; - addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES); - distribute_bandwidth_in_network (solver, new_net, NULL); + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Invalid %s configuration %f\n"), + "PROP_PROPORTIONALITY_FACTOR", + f_tmp); } else { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Not enough bandwidth in new network, suggesting alternative address ..\n"); - /* Find new address to suggest since no bandwidth in network*/ - if (NULL == GAS_proportional_get_preferred_address (s, &address->peer)) - { - /* No alternative address found, disconnect peer */ - s->bw_changed (s->bw_changed_cls, address); - } + s->prop_factor = f_tmp; + LOG (GNUNET_ERROR_TYPE_INFO, + "Using %s of %.3f\n", + "PROP_PROPORTIONALITY_FACTOR", + f_tmp); } } + + s->network_entries = GNUNET_malloc (env->network_count * + sizeof (struct Network)); + for (c = 0; c < env->network_count; c++) + { + cur = &s->network_entries[c]; + cur->type = c; + cur->total_quota_in = env->in_quota[c]; + cur->total_quota_out = env->out_quota[c]; + cur->desc = GNUNET_ATS_print_network_type (c); + GNUNET_asprintf (&cur->stat_total, + "# ATS addresses %s total", + cur->desc); + GNUNET_asprintf (&cur->stat_active, + "# ATS active addresses %s total", + cur->desc); + LOG (GNUNET_ERROR_TYPE_INFO, + "Added network %u `%s' (%llu/%llu)\n", + c, + cur->desc, + cur->total_quota_in, + cur->total_quota_out); + } + return &sf; } + /** - * Add a new single address to a network + * Function used to unload the plugin. * - * @param solver the solver Handle - * @param address the address to add - * @param network network type of this address + * @param cls return value from #libgnunet_plugin_ats_proportional_init() */ -void -GAS_proportional_address_add (void *solver, struct ATS_Address *address, - uint32_t network) +void * +libgnunet_plugin_ats_proportional_done (void *cls) { - struct GAS_PROPORTIONAL_Handle *s = solver; - struct Network *net = NULL; - struct AddressWrapper *aw = NULL; - struct AddressSolverInformation *asi; - struct ATS_Address *best_address; - struct ATS_Address *active_address; - - GNUNET_assert(NULL != s); - net = get_network (s, network); - if (NULL == net) - { - GNUNET_break(0); - - LOG(GNUNET_ERROR_TYPE_ERROR, - "Unknown network %u `%s' for new address %p for peer `%s'\n", - network, GNUNET_ATS_print_network_type(network), - address, GNUNET_i2s(&address->peer)); - - return; - } - - aw = GNUNET_new (struct AddressWrapper); - aw->addr = address; - GNUNET_CONTAINER_DLL_insert(net->head, net->tail, aw); - addresse_increment (s, net, GNUNET_YES, GNUNET_NO); - - asi = GNUNET_new (struct AddressSolverInformation); - asi->network = net; - asi->calculated_quota_in_NBO = htonl (0); - asi->calculated_quota_out_NBO = htonl (0); - aw->addr->solver_information = asi; - - LOG(GNUNET_ERROR_TYPE_INFO, - "Adding new address %p for peer `%s', now total %u and active %u addresses in network `%s'\n", - address, GNUNET_i2s(&address->peer), net->total_addresses, net->active_addresses, net->desc); - - if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer)) - return; /* Peer is not requested */ - - /* This peer is requested, find best address */ - active_address = get_active_address(s, s->addresses, &address->peer); - best_address = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, &address->peer); - - if (NULL == best_address) - return; /* No address to suggest */ + struct GNUNET_ATS_SolverFunctions *sf = cls; + struct GAS_PROPORTIONAL_Handle *s = sf->cls; + struct AddressWrapper *cur; + struct AddressWrapper *next; + unsigned int c; - if ((NULL == active_address) - || ((NULL != active_address) - && (GNUNET_NO == address_eq (active_address, best_address)))) + for (c = 0; c < s->env->network_count; c++) { - /* We now have an active address or the active address changed */ - s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) best_address); + GNUNET_break (0 == s->network_entries[c].total_addresses); + GNUNET_break (0 == s->network_entries[c].active_addresses); + next = s->network_entries[c].head; + while (NULL != (cur = next)) + { + next = cur->next; + GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head, + s->network_entries[c].tail, + cur); + GNUNET_free_non_null (cur->addr->solver_information); + GNUNET_free(cur); + } + GNUNET_free (s->network_entries[c].stat_total); + GNUNET_free (s->network_entries[c].stat_active); } + GNUNET_break (0 == s->active_addresses); + GNUNET_free (s->network_entries); + GNUNET_free (s); + return NULL; }