X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fats%2Fplugin_ats_proportional.c;h=9309bc2ea799df575b99b5884aea081cd2ab8552;hb=7c8c596991f6397ec3ca1911638acb2de721c7a7;hp=7f0fcd0427401413ac7a5576809bf9bbe72bfbbc;hpb=e1669fabbcb9f21c509e03811fe2d8b5b568b15c;p=oweals%2Fgnunet.git diff --git a/src/ats/plugin_ats_proportional.c b/src/ats/plugin_ats_proportional.c index 7f0fcd042..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. - 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 @@ -14,10 +14,9 @@ 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 @@ -26,193 +25,28 @@ */ #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,71 +198,83 @@ struct GAS_PROPORTIONAL_Handle */ unsigned int active_addresses; - /** - * Number of networks in @a network_entries - */ - unsigned int network_count; }; /** - * Test if bandwidth is available in this network to add an additional address + * Test if bandwidth is available in this network to add an additional address. * - * @param net the network type to update + * @param net the network type to check + * @param extra for how many extra addresses do we check? * @return #GNUNET_YES or #GNUNET_NO */ static int -is_bandwidth_available_in_network (struct Network *net) +is_bandwidth_available_in_network (struct Network *net, + int extra) { - unsigned int na = net->active_addresses + 1; + 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) ) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Enough bandwidth available for %u active addresses in network `%s'\n", - na, - net->desc); - return GNUNET_YES; - } - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Not enough bandwidth available for %u active addresses in network `%s'\n", - na, - net->desc); return GNUNET_NO; } /** - * Update bandwidth assigned to peers in this network + * 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 + */ +static int +all_require_connectivity (struct GAS_PROPORTIONAL_Handle *s, + struct Network *net, + unsigned int con) +{ + struct AddressWrapper *aw; + + 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; +} + + +/** + * 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 - * this address */ static void distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s, struct Network *net) { + const uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__); struct AddressWrapper *aw; - 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 */ + 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 = NULL; /* Important: has to be double not float due to precision */ - - uint32_t assigned_quota_in = 0; - uint32_t assigned_quota_out = 0; - + const double *peer_relative_prefs; LOG (GNUNET_ERROR_TYPE_INFO, "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n", @@ -437,16 +283,10 @@ distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s, net->total_quota_in, net->total_quota_in); - if (net->active_addresses == 0) - { + if (0 == net->active_addresses) return; /* no addresses to update */ - } - - /* Idea - * Assign every peer in network minimum Bandwidth - * Distribute bandwidth left according to preference - */ + /* sanity checks */ if ((net->active_addresses * min_bw) > net->total_quota_in) { GNUNET_break(0); @@ -458,117 +298,88 @@ distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s, 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); + /* Calculate sum of relative preference for active addresses in this + network */ sum_relative_peer_prefences = 0.0; - - /* Calculate sum of relative preference for active addresses in this network */ count_addresses = 0; for (aw = net->head; NULL != aw; aw = aw->next) { if (GNUNET_YES != aw->addr->active) continue; - peer_relative_prefs = s->env->get_preferences (s->env->cls, &aw->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 ++; + sum_relative_peer_prefences + += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH]; + count_addresses++; } - if (count_addresses != net->active_addresses) { GNUNET_break (0); LOG (GNUNET_ERROR_TYPE_WARNING, - "%s: Counted %u active addresses, but network says to have %u active addresses\n", + "%s: Counted %u active addresses, expected %u active addresses\n", net->desc, count_addresses, net->active_addresses); - for (aw = net->head; NULL != aw; aw = aw->next) - { - if (GNUNET_YES != aw->addr->active) - continue; - - LOG (GNUNET_ERROR_TYPE_WARNING, - "Active: `%s' `%s' length %u\n", - GNUNET_i2s (&aw->addr->peer), - aw->addr->plugin, - aw->addr->addr_len); - } + /* try to fix... */ + net->active_addresses = count_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); + /* 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 (GNUNET_YES == aw->addr->active) - { - peer_relative_prefs = s->env->get_preferences (s->env->cls, - &aw->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 (&aw->addr->peer), - peer_weight, - total_weight, - assigned_quota_in, - assigned_quota_out); - } - else + if (GNUNET_YES != aw->addr->active) { - assigned_quota_in = 0; - assigned_quota_out = 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]; - 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; - - /* Store for later propagation */ - aw->calculated_quota_in = assigned_quota_in; - aw->calculated_quota_out = assigned_quota_out; + 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; + + 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; } 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 */ - { - 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); - } - if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */ - { - 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); - } + /* +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); } @@ -591,16 +402,6 @@ propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s, continue; cur->addr->assigned_bw_in = cur->calculated_quota_in; cur->addr->assigned_bw_out = cur->calculated_quota_out; - - 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), - cur->addr->assigned_bw_in, - cur->addr->assigned_bw_out); - - /* Notify on change */ if (GNUNET_YES == cur->addr->active) s->env->bandwidth_changed_cb (s->env->cls, cur->addr); @@ -621,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; @@ -657,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, @@ -672,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, @@ -700,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, @@ -745,13 +528,37 @@ find_best_address_it (void *cls, double best_distance; double cur_delay; double cur_distance; - int index; - - if (GNUNET_NO == - is_bandwidth_available_in_network (asi->network)) + 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) { - /* There's no bandwidth available in this network, - so we cannot use this address. */ + /* Bandwidth for this address is unavailable, so we cannot use + it. */ return GNUNET_OK; } if (GNUNET_YES == current->active) @@ -773,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) @@ -912,131 +717,135 @@ get_active_address (struct GAS_PROPORTIONAL_Handle *s, /** - * Update active address for a peer: - * Check if active address exists and what the best address is, if addresses - * are different switch + * 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 - * return the new address or NULL if no update was performed - */ -static struct ATS_Address * + */ +static void update_active_address (struct GAS_PROPORTIONAL_Handle *s, + struct ATS_Address *current_address, const struct GNUNET_PeerIdentity *peer) { struct ATS_Address *best_address; - struct ATS_Address *current_address; - struct AddressWrapper *asi; - struct Network *net; - - LOG (GNUNET_ERROR_TYPE_INFO, - "Updating active address for peer `%s'\n", - GNUNET_i2s (peer)); - - /* Find active address */ - current_address = get_active_address (s, - peer); - - LOG (GNUNET_ERROR_TYPE_INFO, - "Peer `%s' has active address %p\n", - GNUNET_i2s (peer), - current_address); + struct AddressWrapper *asi_cur; + struct AddressWrapper *asi_best; + struct AddressWrapper *aw; + struct AddressWrapper *aw_min; + unsigned int a_con; + unsigned int con_min; - /* Find best address */ best_address = get_best_address (s, s->env->addresses, peer); - LOG (GNUNET_ERROR_TYPE_INFO, - "Peer `%s' has best address %p\n", - GNUNET_i2s (peer), - best_address); - + 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) { + /* 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) || - ( (NULL != best_address) && - (current_address != best_address) ) ) - { - /* We switch to a new address (or to none), - mark old address as inactive */ - LOG (GNUNET_ERROR_TYPE_INFO, - "Disabling previous %s address %p for peer `%s'\n", - (GNUNET_NO == current_address->active) ? "inactive" : "active", - current_address, - GNUNET_i2s (peer)); - - asi = current_address->solver_information; - net = asi->network; - asi->activated = GNUNET_TIME_UNIT_ZERO_ABS; - current_address->active = GNUNET_NO; /* No active any longer */ - current_address->assigned_bw_in = 0; /* no bandwidth assigned */ - current_address->assigned_bw_out = 0; /* no bandwidth assigned */ - address_decrement_active (s, net); - distribute_bandwidth_in_network (s, net); - } + (asi_best->network != asi_cur->network) ) + distribute_bandwidth_in_network (s, + asi_cur->network); if (NULL == best_address) { - /* We previously had an active address, but now we cannot suggest one - * Therefore we have to disconnect the peer */ - LOG (GNUNET_ERROR_TYPE_INFO, - "Disconnecting peer `%s' with previous address %p\n", - GNUNET_i2s (peer), - current_address); + /* 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) { - LOG (GNUNET_ERROR_TYPE_INFO, + /* 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 NULL; + return; } - - LOG (GNUNET_ERROR_TYPE_INFO, - "Suggesting new address %p for peer `%s'\n", + /* 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)); - - if ( (NULL != current_address) && - (best_address == current_address) ) - { - GNUNET_break (GNUNET_NO != current_address->active); - return best_address; /* Same same */ - } - - asi = best_address->solver_information; - GNUNET_assert (NULL != asi); - net = asi->network; - /* Mark address as active */ - asi->activated = GNUNET_TIME_absolute_get (); best_address->active = GNUNET_YES; - - net->active_addresses++; + 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, - net->stat_active, + 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)); - /* Distribute bandwidth */ - distribute_bandwidth_in_network (s, net); - return best_address; + + if (GNUNET_NO == + is_bandwidth_available_in_network (asi_best->network, + 0)) + { + /* 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) + { + 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); + } + distribute_bandwidth_in_network (s, + asi_best->network); } /** - * 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 @@ -1044,35 +853,17 @@ update_active_address (struct GAS_PROPORTIONAL_Handle *s, * @param pref_rel the normalized preference value for this kind over all clients */ static void -GAS_proportional_address_change_preference (void *solver, - const struct GNUNET_PeerIdentity *peer, - enum GNUNET_ATS_PreferenceKind kind, - double pref_rel) +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; - struct AddressWrapper *asi; - - if (0 == - s->env->get_connectivity (s->env->cls, - peer)) - return; /* Peer is not requested */ - /* This peer is requested, find best address */ - active_address = get_active_address (s, - peer); - best_address = update_active_address (s, peer); - - if ( (NULL != best_address) && - (active_address == best_address) ) - { - asi = best_address->solver_information; - GNUNET_assert (NULL != asi); - - /* We sticked to the same address, therefore redistribute */ - distribute_bandwidth_in_network (s, asi->network); - } + if (GNUNET_ATS_PREFERENCE_BANDWIDTH != kind) + return; /* we do not care */ + distribute_bandwidth_in_network (s, + NULL); } @@ -1087,17 +878,14 @@ GAS_proportional_address_change_preference (void *solver, * @param score the score */ static void -GAS_proportional_address_preference_feedback (void *solver, - struct GNUNET_SERVER_Client *application, - const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_TIME_Relative scope, - enum GNUNET_ATS_PreferenceKind kind, - double score) +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 != peer); - GNUNET_assert(NULL != s); + /* Proportional does not care about feedback */ } @@ -1106,19 +894,17 @@ GAS_proportional_address_preference_feedback (void *solver, * * @param solver the solver handle * @param peer the identity of the peer - * @return best address */ -static 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; - const struct ATS_Address *best_address; - best_address = update_active_address (s, peer); - if (s->bulk_lock > 0) - return NULL; - return best_address; + update_active_address (s, + get_active_address (s, + peer), + peer); } @@ -1129,36 +915,20 @@ GAS_proportional_get_preferred_address (void *solver, * @param peer the peer */ static void -GAS_proportional_stop_get_preferred_address (void *solver, - const struct GNUNET_PeerIdentity *peer) +GAS_proportional_stop_get_address (void *solver, + const struct GNUNET_PeerIdentity *peer) { struct GAS_PROPORTIONAL_Handle *s = solver; struct ATS_Address *cur; struct AddressWrapper *asi; - struct Network *cur_net; cur = get_active_address (s, 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; - asi->activated = GNUNET_TIME_UNIT_ZERO_ABS; - cur->active = GNUNET_NO; /* No active any longer */ - cur->assigned_bw_in = 0; /* no bandwidth assigned */ - cur->assigned_bw_out = 0; /* no bandwidth assigned */ - - address_decrement_active (s, cur_net); - - distribute_bandwidth_in_network (s, cur_net); - } + if (NULL == cur) + return; + asi = cur->solver_information; + distribute_bandwidth_in_network (s, + asi->network); } @@ -1197,11 +967,13 @@ GAS_proportional_bulk_stop (void *solver) 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_INFO, "No lock pending, recalculating\n"); - distribute_bandwidth_in_network (s, NULL); + distribute_bandwidth_in_network (s, + NULL); s->bulk_requests = 0; } } @@ -1212,47 +984,16 @@ 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; - struct ATS_Address *best_address; - struct ATS_Address *active_address; + struct AddressWrapper *asi = address->solver_information; - asi = address->solver_information; - 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 (0 == - s->env->get_connectivity (s->env->cls, - &address->peer)) - return; /* Peer is not requested */ - - /* This peer is requested, find active and best address */ - active_address = get_active_address (s, - &address->peer); - best_address = update_active_address (s, - &address->peer); - if (active_address == best_address) - { - /* We stuck to the same address, therefore redistribute - (NOTE: because otherwise update_active_address() already - calls distribute_bandwidth_in_network() )*/ - distribute_bandwidth_in_network (s, asi->network); - } + distribute_bandwidth_in_network (s, + asi->network); } @@ -1291,23 +1032,16 @@ GAS_proportional_address_add (void *solver, net->stat_total, 1, GNUNET_NO); - + update_active_address (s, + get_active_address (s, + &address->peer), + &address->peer); LOG (GNUNET_ERROR_TYPE_INFO, - "Adding new address %p for peer `%s', now total %u and active %u addresses in network `%s'\n", - address, + "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); - - if (0 == - s->env->get_connectivity (s->env->cls, - &address->peer)) - return; /* Peer is not requested */ - - /* This peer is requested, find best address */ - update_active_address (s, - &address->peer); } @@ -1349,27 +1083,11 @@ GAS_proportional_address_delete (void *solver, GNUNET_NO); if (GNUNET_YES == address->active) { - /* Address was active, remove from network and update quotas*/ - address->active = GNUNET_NO; - address->assigned_bw_in = 0; - address->assigned_bw_out = 0; - address_decrement_active (s, net); - /* FIXME: this may trigger the solver unnecessarily, - especially if update_active_address() succeeds! */ + /* Address was active, remove from network and update quotas */ + update_active_address (s, + address, + &address->peer); distribute_bandwidth_in_network (s, net); - - if (NULL == - update_active_address (s, - &address->peer)) - { - /* No alternative address found, disconnect peer */ - LOG (GNUNET_ERROR_TYPE_INFO, - "Disconnecting peer `%s' after deleting previous address %p\n", - GNUNET_i2s (&address->peer), - address); - s->env->bandwidth_changed_cb (s->env->cls, - address); - } } GNUNET_free (aw); address->solver_information = NULL; @@ -1403,10 +1121,10 @@ libgnunet_plugin_ats_proportional_init (void *cls) 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_get_preferred_address; - sf.s_get_stop = &GAS_proportional_stop_get_preferred_address; - sf.s_pref = &GAS_proportional_address_change_preference; - sf.s_feedback = &GAS_proportional_address_preference_feedback; + 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; @@ -1497,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);