2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file ats/plugin_ats_proportional.c
23 * @brief ATS proportional solver
24 * @author Matthias Wachs
25 * @author Christian Grothoff
27 #include "plugin_ats_proportional.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
34 * NOTE: Do not change this documentation. This documentation is based
35 * on gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
36 * use build_txt.sh to generate plaintext output
38 * ATS addresses : proportional solver
40 * The proportional solver ("proportional") distributes the available
41 * bandwidth fair over all the addresses influenced by the
42 * preference values. For each available network type an in- and
43 * outbound quota is configured and the bandwidth available in
44 * these networks is distributed over the addresses. The solver
45 * first assigns every addresses the minimum amount of bandwidth
46 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
47 * remaining bandwidth available according to the preference
48 * values. For each peer only a single address gets bandwidth
49 * assigned and only one address marked as active. The most
50 * important functionality for the solver is implemented in: *
51 * find_address_it is an hashmap iterator returning the prefered
52 * address for an peer * update_quota_per_network distributes
53 * available bandwidth for a network over active addresses
55 * Changes to addresses automatically have an impact on the the
56 * bandwidth assigned to other addresses in the same network since
57 * the solver distributes the remaining bandwidth over the
58 * addresses in the network. When changes to the addresses occur,
59 * the solver first performs the changes, like adding or deleting
60 * addresses, and then updates bandwidth assignment for the
61 * affected network. Bandwidth assignment is only recalculated on
62 * demand when an address is requested by a client for a peer or
63 * when the addresses available have changed or an address changed
64 * the network it is located in. When the bandwidth assignment has
65 * changed the callback is called with the new bandwidth
66 * assignments. The bandwidth distribution for a network is
67 * recalculated due to: * address suggestion requests * address
68 * deletions * address switching networks during address update *
71 * 3.1 Data structures used
73 * For each ATS network (e.g. WAN, LAN, loopback) a struct Network
74 * is used to specify network related information as total adresses
75 * and active addresses in this network and the configured in- and
76 * outbound quota. Each network also contains a list of addresses
77 * added to the solver located in this network. The proportional
78 * solver uses the addresses' solver_information field to store the
79 * proportional network it belongs to for each address.
83 * When the proportional solver is initialized the solver creates a
84 * new solver handle and initializes the network structures with
85 * the quotas passed from addresses and returns the handle solver.
87 * 3.3 Adding an address
89 * When a new address is added to the solver using s_add, a lookup
90 * for the network for this address is done and the address is
91 * enqueued in in the linked list of the network.
93 * 3.4 Updating an address
95 * The main purpose of address updates is to update the ATS
96 * information for addresse selection. Important for the proportional
97 * solver is when an address switches network it is located
98 * in. This is common because addresses added by transport's
99 * validation mechanism are commonly located in
100 * GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
101 * in this network type and only if a connection is successful on
102 * return of payload data transport switches to the real network
103 * the address is located in. When an address changes networks it
104 * is first of all removed from the old network using the solver
105 * API function GAS_proportional_address_delete and the network in
106 * the address struct is updated. A lookup for the respective new
107 * proportional network is done and stored in the addresse's
108 * solver_information field. Next the address is re-added to the
109 * solver using the solver API function
110 * GAS_proportional_address_add. If the address was marked as in
111 * active, the solver checks if bandwidth is available in the
112 * network and if yes sets the address to active and updates the
113 * bandwidth distribution in this network. If no bandwidth is
114 * available it sets the bandwidth for this address to 0 and tries
115 * to suggest an alternative address. If an alternative address was
116 * found, addresses' callback is called for this address.
118 * 3.5 Deleting an address
120 * When an address is removed from the solver, it removes the
121 * respective address from the network and if the address was
122 * marked as active, it updates the bandwidth distribution for this
125 * 3.6 Requesting addresses
127 * When an address is requested for a peer the solver performs a
128 * lookup for the peer entry in addresses address hashmap and
129 * selects the best address. The selection of the most suitable
130 * address is done in the find_address_it hashmap iterator
131 * described in detail in section 3.7. If no address is returned,
132 * no address can be suggested at the moment. If the address
133 * returned is marked as active, the solver can return this
134 * address. If the address is not marked as active, the solver
135 * checks if another address belongign to this peer is marked as
136 * active and marks the address as inactive, updates the bandwidth
137 * for this address to 0, call the bandwidth changed callback for
138 * this address due to the change and updates quota assignment for
139 * the addresse's network. the now in-active address is belonging
140 * to. The solver marks the new address as active and updates the
141 * bandwidth assignment for this network.
143 * 3.7 Choosing addresses
145 * Choosing the best possible address for suggestion is done by
146 * iterating over all addresses of a peer stored in addresses'
147 * hashmap and using the hashmap iterator find_address_it to select
148 * the best available address. Several checks are done when an
149 * address is selected. First if this address is currently blocked
150 * by addresses from being suggested. An address is blocked for the
151 * duration of ATS_BLOCKING_DELTA when it is suggested to
152 * transport. Next it is checked if at least
153 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
154 * in the addresse's network, because suggesting an address without
155 * bandwidth does not make sense. This also ensures that all active
156 * addresses in this network get at least the minimum amount of
157 * bandwidth assigned. In the next step the solver ensures that for
158 * tcp connections inbound connections are prefered over outbound
159 * connections. In the next stet the solver ensures that
160 * connections are prefered in the following order: * connections
161 * are already established and have bandwidth assigned *
162 * connections with a shorter distance * connectes have a shorter
165 * 3.8 Changing preferences
169 * During shutdown all network entries and aging processes are
170 * destroyed and freed.
175 * This solver assigns in and outbound bandwidth equally for all
176 * addresses in specific network type (WAN, LAN) based on configured
177 * in and outbound quota for this network.
179 * The solver is notified by addresses about changes to the addresses
180 * and recalculates the bandwith assigned if required. The solver
181 * notifies addresses by calling the GAS_bandwidth_changed_cb
189 * For each peer only a single is selected and marked as "active" in the address
194 * You have the networks WAN and LAN and quotas
195 * WAN_TOTAL_IN, WAN_TOTAL_OUT
196 * LAN_TOTAL_IN, LAN_TOTAL_OUT
198 * If you have x addresses in the network segment LAN, the quotas are
199 * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
201 * Quotas are automatically recalculated and reported back when addresses are
206 #define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
207 #define PREF_AGING_FACTOR 0.95
209 #define DEFAULT_REL_PREFERENCE 1.0
210 #define DEFAULT_ABS_PREFERENCE 0.0
211 #define MIN_UPDATE_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
214 * A handle for the proportional solver
216 struct GAS_PROPORTIONAL_Handle
218 struct GNUNET_ATS_PluginEnvironment *env;
223 struct GNUNET_STATISTICS_Handle *stats;
226 * Hashmap containing all valid addresses
228 const struct GNUNET_CONTAINER_MultiPeerMap *addresses;
231 * Pending address requests
233 struct GNUNET_CONTAINER_MultiPeerMap *requests;
236 * Bandwidth changed callback
238 GAS_bandwidth_changed_cb bw_changed;
241 * Bandwidth changed callback cls
243 void *bw_changed_cls;
246 * ATS function to get preferences
248 GAS_get_preferences get_preferences;
251 * Closure for ATS function to get preferences
253 void *get_preferences_cls;
256 * ATS function to get properties
258 GAS_get_properties get_properties;
261 * Closure for ATS function to get properties
263 void *get_properties_cls;
271 * Number of changes while solver was locked
276 * Total number of addresses for solver
278 unsigned int total_addresses;
281 * Number of active addresses for solver
283 unsigned int active_addresses;
288 struct Network *network_entries;
293 unsigned int network_count;
298 * Representation of a network
308 * Network description
313 * Total inbound quota
316 unsigned long long total_quota_in;
319 * Total outbound quota
322 unsigned long long total_quota_out;
325 * Number of active addresses for this network
327 unsigned int active_addresses;
330 * Number of total addresses for this network
332 unsigned int total_addresses;
335 * String for statistics total addresses
340 * String for statistics active addresses
344 struct AddressWrapper *head;
345 struct AddressWrapper *tail;
349 * Address information stored in the solver
351 struct AddressSolverInformation
353 struct Network *network;
359 unsigned long long calculated_quota_in_NBO;
365 unsigned long long calculated_quota_out_NBO;
371 * Wrapper for addresses to store them in network's linked list
373 struct AddressWrapper
378 struct AddressWrapper *next;
383 struct AddressWrapper *prev;
388 struct ATS_Address *addr;
392 * Important solver functions
393 * ---------------------------
397 libgnunet_plugin_ats_proportional_init (void *cls)
399 struct GNUNET_ATS_PluginEnvironment *env = cls;
400 struct GAS_PROPORTIONAL_Handle *s;
401 struct Network * cur;
402 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
405 GNUNET_assert (NULL != env);
406 GNUNET_assert(NULL != env->cfg);
407 GNUNET_assert(NULL != env->bandwidth_changed_cb);
408 GNUNET_assert(NULL != env->get_preferences);
409 GNUNET_assert(NULL != env->get_property);
411 s = GNUNET_new (struct GAS_PROPORTIONAL_Handle);
413 env->sf.s_add = &GAS_proportional_address_add;
414 env->sf.s_address_update_property = &GAS_proportional_address_property_changed;
415 env->sf.s_address_update_session = &GAS_proportional_address_session_changed;
416 env->sf.s_address_update_inuse = &GAS_proportional_address_inuse_changed;
417 env->sf.s_address_update_network = &GAS_proportional_address_change_network;
418 env->sf.s_get = &GAS_proportional_get_preferred_address;
419 env->sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
420 env->sf.s_pref = &GAS_proportional_address_change_preference;
421 env->sf.s_feedback = &GAS_proportional_address_preference_feedback;
422 env->sf.s_del = &GAS_proportional_address_delete;
423 env->sf.s_bulk_start = &GAS_proportional_bulk_start;
424 env->sf.s_bulk_stop = &GAS_proportional_bulk_stop;
426 s->stats = (struct GNUNET_STATISTICS_Handle *) env->stats;
427 s->bw_changed = env->bandwidth_changed_cb;
428 s->bw_changed_cls = env->bw_changed_cb_cls;
429 s->get_preferences = env->get_preferences;
430 s->get_preferences_cls = env->get_preference_cls;
431 s->get_properties = env->get_property;
432 s->get_properties_cls = env->get_property_cls;
433 s->network_count = env->network_count;
434 s->network_entries = GNUNET_malloc (env->network_count * sizeof (struct Network));
437 s->active_addresses = 0;
438 s->total_addresses = 0;
439 s->bulk_lock = GNUNET_NO;
440 s->addresses = env->addresses;
441 s->requests = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
443 for (c = 0; c < env->network_count; c++)
445 cur = &s->network_entries[c];
446 cur->total_addresses = 0;
447 cur->active_addresses = 0;
448 cur->type = env->networks[c];
449 cur->total_quota_in = env->in_quota[c];
450 cur->total_quota_out = env->out_quota[c];
451 cur->desc = net_str[c];
452 GNUNET_asprintf (&cur->stat_total,
453 "# ATS addresses %s total", cur->desc);
454 GNUNET_asprintf (&cur->stat_active,
455 "# ATS active addresses %s total", cur->desc);
456 LOG (GNUNET_ERROR_TYPE_INFO, "Added network %u `%s' %p\n", c, cur->desc, s);
462 libgnunet_plugin_ats_proportional_done (void *cls)
464 struct GAS_PROPORTIONAL_Handle *s = cls;
465 struct AddressWrapper *cur;
466 struct AddressWrapper *next;
468 GNUNET_assert(s != NULL);
469 for (c = 0; c < s->network_count; c++)
471 if (s->network_entries[c].total_addresses > 0)
473 LOG(GNUNET_ERROR_TYPE_DEBUG,
474 "Had %u addresses for network `%s' not deleted during shutdown\n",
475 s->network_entries[c].total_addresses, s->network_entries[c].desc);
479 if (s->network_entries[c].active_addresses > 0)
481 LOG(GNUNET_ERROR_TYPE_DEBUG,
482 "Had %u active addresses for network `%s' not deleted during shutdown\n",
483 s->network_entries[c].active_addresses, s->network_entries[c].desc);
487 next = s->network_entries[c].head;
488 while (NULL != (cur = next))
491 GNUNET_CONTAINER_DLL_remove(s->network_entries[c].head,
492 s->network_entries[c].tail, cur);
493 GNUNET_free_non_null (cur->addr->solver_information);
496 GNUNET_free(s->network_entries[c].stat_total);
497 GNUNET_free(s->network_entries[c].stat_active);
499 if (s->total_addresses > 0)
501 LOG(GNUNET_ERROR_TYPE_DEBUG,
502 "Had %u addresses not deleted during shutdown\n", s->total_addresses);
505 if (s->active_addresses > 0)
507 LOG(GNUNET_ERROR_TYPE_DEBUG,
508 "Had %u active addresses not deleted during shutdown\n",
509 s->active_addresses);
512 GNUNET_free (s->network_entries);
513 GNUNET_CONTAINER_multipeermap_destroy (s->requests);
520 * Test if bandwidth is available in this network to add an additional address
522 * @param net the network type to update
523 * @return GNUNET_YES or GNUNET_NO
526 is_bandwidth_available_in_network (struct Network *net)
528 GNUNET_assert(NULL != net);
529 unsigned int na = net->active_addresses + 1;
530 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
531 if (((net->total_quota_in / na) > min_bw)
532 && ((net->total_quota_out / na) > min_bw))
534 LOG(GNUNET_ERROR_TYPE_DEBUG,
535 "Enough bandwidth available for %u active addresses in network `%s'\n",
540 LOG(GNUNET_ERROR_TYPE_DEBUG,
541 "Not enough bandwidth available for %u active addresses in network `%s'\n",
547 * Update bandwidth assigned to peers in this network
549 * @param s the solver handle
550 * @param net the network type to update
551 * @param address_except address excluded from notification, since we suggest
555 distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
556 struct Network *net, struct ATS_Address *address_except)
558 struct AddressSolverInformation *asi;
559 struct AddressWrapper *cur;
561 unsigned long long remaining_quota_in = 0;
562 unsigned long long quota_out_used = 0;
563 unsigned long long remaining_quota_out = 0;
564 unsigned long long quota_in_used = 0;
565 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
567 double total_prefs; /* Important: has to be double not float due to precision */
568 double cur_pref; /* Important: has to be double not float due to precision */
569 const double *t = NULL; /* Important: has to be double not float due to precision */
571 unsigned long long assigned_quota_in = 0;
572 unsigned long long assigned_quota_out = 0;
575 LOG(GNUNET_ERROR_TYPE_DEBUG,
576 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
577 net->desc, net->active_addresses, net->total_quota_in,
578 net->total_quota_in);
580 if (net->active_addresses == 0)
581 return; /* no addresses to update */
584 * Assign every peer in network minimum Bandwidth
585 * Distribute bandwidth left according to preference
588 if ((net->active_addresses * min_bw) > net->total_quota_in)
593 if ((net->active_addresses * min_bw) > net->total_quota_out)
599 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
600 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
601 LOG(GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
602 remaining_quota_in, remaining_quota_out);
604 for (cur = net->head; NULL != cur; cur = cur->next)
606 if (GNUNET_YES == cur->addr->active)
609 NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
612 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
614 if (c != GNUNET_ATS_PREFERENCE_END)
616 //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
620 total_prefs += (peer_prefs / (GNUNET_ATS_PreferenceCount - 1));
623 for (cur = net->head; NULL != cur; cur = cur->next)
625 if (GNUNET_YES == cur->addr->active)
629 NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
631 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
633 if (c != GNUNET_ATS_PREFERENCE_END)
638 assigned_quota_in = min_bw
639 + ((cur_pref / total_prefs) * remaining_quota_in);
640 assigned_quota_out = min_bw
641 + ((cur_pref / total_prefs) * remaining_quota_out);
643 LOG(GNUNET_ERROR_TYPE_DEBUG,
644 "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
645 GNUNET_i2s (&cur->addr->peer), cur_pref, total_prefs,
646 assigned_quota_in, assigned_quota_out);
650 assigned_quota_in = 0;
651 assigned_quota_out = 0;
654 quota_in_used += assigned_quota_in;
655 quota_out_used += assigned_quota_out;
656 /* Prevent overflow due to rounding errors */
657 if (assigned_quota_in > UINT32_MAX)
658 assigned_quota_in = UINT32_MAX;
659 if (assigned_quota_out > UINT32_MAX)
660 assigned_quota_out = UINT32_MAX;
662 /* Compare to current bandwidth assigned */
663 asi = cur->addr->solver_information;
664 asi->calculated_quota_in_NBO = htonl (assigned_quota_in);
665 asi->calculated_quota_out_NBO = htonl (assigned_quota_out);
667 LOG(GNUNET_ERROR_TYPE_DEBUG,
668 "Total bandwidth assigned is (in/out): %llu /%llu\n", quota_in_used,
670 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
672 LOG(GNUNET_ERROR_TYPE_ERROR,
673 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
674 net->active_addresses, quota_out_used, net->total_quota_out);
676 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
678 LOG(GNUNET_ERROR_TYPE_ERROR,
679 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
680 net->active_addresses, quota_in_used, net->total_quota_in);
684 struct FindBestAddressCtx
686 struct GAS_PROPORTIONAL_Handle *s;
687 struct ATS_Address *best;
691 find_property_index (uint32_t type)
693 int existing_types[] = GNUNET_ATS_QualityProperties;
695 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
696 if (existing_types[c] == type)
698 return GNUNET_SYSERR;
702 * Find a "good" address to use for a peer by iterating over the addresses for this peer.
703 * If we already have an existing address, we stick to it.
704 * Otherwise, we pick by lowest distance and then by lowest latency.
706 * @param cls the 'struct ATS_Address**' where we store the result
708 * @param value another 'struct ATS_Address*' to consider using
709 * @return GNUNET_OK (continue to iterate)
712 find_best_address_it (void *cls,
713 const struct GNUNET_PeerIdentity *key,
716 struct FindBestAddressCtx *ctx = (struct FindBestAddressCtx *) cls;
717 struct ATS_Address *current = (struct ATS_Address *) value;
718 struct ATS_Address *current_best = (struct ATS_Address *) value;
719 struct GNUNET_TIME_Absolute now;
720 struct AddressSolverInformation *asi;
721 const double *norm_prop_cur;
722 const double *norm_prop_best;
726 asi = current->solver_information;
727 now = GNUNET_TIME_absolute_get ();
729 if (current->blocked_until.abs_value_us
730 == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value_us)
732 /* This address is blocked for suggestion */
733 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address %p blocked for suggestion for %s \n",
735 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (now, current->blocked_until), GNUNET_YES));
738 if (GNUNET_NO == is_bandwidth_available_in_network (asi->network))
739 return GNUNET_OK; /* There's no bandwidth available in this network */
741 if (NULL != ctx->best)
743 /* Compare current addresses with denominated 'best' address */
745 current_best = ctx->best;
746 if ((0 != ctx->best->addr_len) && (0 == current->addr_len))
748 /* saved address was an outbound address, but we have an inbound address */
749 current_best = current;
752 if (ctx->best->t_last_activity.abs_value_us < current->t_last_activity.abs_value_us)
754 /* Current address is newer */
755 current_best = current;
757 if (ctx->best->t_added.abs_value_us < current->t_added.abs_value_us)
759 /* Current address is newer */
760 current_best = current;
764 if (NULL == ctx->best)
766 /* We do not have a 'best' address so take this address */
767 current_best = current;
771 if ( (ntohl (ctx->best->assigned_bw_in.value__) == 0) &&
772 (ntohl (current->assigned_bw_in.value__) > 0) )
774 /* stick to existing connection */
775 current_best = current;
778 /* Now compare ATS information */
779 norm_prop_cur = ctx->s->get_properties (ctx->s->get_properties_cls,
780 (const struct ATS_Address *) current);
781 norm_prop_best = ctx->s->get_properties (ctx->s->get_properties_cls,
782 (const struct ATS_Address *) ctx->best);
784 index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
785 if (GNUNET_SYSERR != index)
787 /* user shorter distance */
788 if (norm_prop_cur[index] < norm_prop_best[index])
789 current_best = current;
791 current_best = ctx->best;
794 index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
795 if (GNUNET_SYSERR != index)
797 /* User connection with less delay */
798 if (norm_prop_cur[index] < norm_prop_best[index])
799 current_best = current;
801 current_best = ctx->best;
804 ctx->best = current_best;
810 * ---------------------------
813 propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
814 struct Network *net, struct ATS_Address *address_except)
816 struct AddressWrapper *cur;
817 struct AddressSolverInformation *asi;
818 for (cur = net->head; NULL != cur; cur = cur->next)
820 asi = cur->addr->solver_information;
821 if ( (cur->addr->assigned_bw_in.value__ != asi->calculated_quota_in_NBO) ||
822 (cur->addr->assigned_bw_out.value__ != asi->calculated_quota_out_NBO) )
824 cur->addr->assigned_bw_in.value__ = asi->calculated_quota_in_NBO;
825 cur->addr->assigned_bw_out.value__ = asi->calculated_quota_in_NBO;
826 /* Notify on change */
827 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
828 s->bw_changed (s->bw_changed_cls, cur->addr);
834 * Distribibute bandwidth
836 * @param s the solver handle
837 * @param n the network, can be NULL for all network
838 * @param address_except do not notify for this address
841 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
842 struct Network *n, struct ATS_Address *address_except)
844 if (GNUNET_YES == s->bulk_lock)
852 if (NULL != s->env->info_cb)
853 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_START,
854 GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
855 distribute_bandwidth(s, n, address_except);
856 if (NULL != s->env->info_cb)
857 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_STOP,
858 GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
859 if (NULL != s->env->info_cb)
860 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
861 GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
862 propagate_bandwidth(s, n, address_except);
863 if (NULL != s->env->info_cb)
864 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
865 GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
870 if (NULL != s->env->info_cb)
871 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_START,
872 GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
873 for (i = 0; i < s->network_count; i++)
874 distribute_bandwidth(s, &s->network_entries[i], NULL);
875 if (NULL != s->env->info_cb)
876 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_STOP,
877 GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
878 if (NULL != s->env->info_cb)
879 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
880 GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
881 for (i = 0; i < s->network_count; i++)
883 propagate_bandwidth(s, &s->network_entries[i], address_except);
885 if (NULL != s->env->info_cb)
886 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
887 GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
892 * Lookup network struct by type
894 * @param s the solver handle
895 * @param type the network type
896 * @return the network struct
898 static struct Network *
899 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
902 for (c = 0; c < s->network_count; c++)
904 if (s->network_entries[c].type == type)
905 return &s->network_entries[c];
911 * Hashmap Iterator to find current active address for peer
913 * @param cls last active address
914 * @param key peer's key
915 * @param value address to check
916 * @return #GNUNET_NO on double active address else #GNUNET_YES;
919 get_active_address_it (void *cls,
920 const struct GNUNET_PeerIdentity *key,
923 struct ATS_Address **dest = cls;
924 struct ATS_Address *aa = (struct ATS_Address *) value;
926 if (GNUNET_YES == aa->active)
931 /* should never happen */
932 LOG(GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n",
933 GNUNET_i2s (&aa->peer));
943 * Find current active address for peer
945 * @param solver the solver handle
946 * @param addresses the address set
947 * @param peer the peer
948 * @return active address or NULL
950 static struct ATS_Address *
951 get_active_address (void *solver,
952 const struct GNUNET_CONTAINER_MultiPeerMap * addresses,
953 const struct GNUNET_PeerIdentity *peer)
955 struct ATS_Address * dest = NULL;
957 GNUNET_CONTAINER_multipeermap_get_multiple (addresses, peer,
958 &get_active_address_it, &dest);
964 addresse_increment (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
965 int total, int active)
967 if (GNUNET_YES == total)
969 s->total_addresses++;
970 net->total_addresses++;
971 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
972 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
974 if (GNUNET_YES == active)
976 net->active_addresses++;
977 s->active_addresses++;
978 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1,
980 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
987 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
988 int total, int active)
991 if (GNUNET_YES == total)
993 if (s->total_addresses < 1)
1000 s->total_addresses--;
1001 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
1004 if (net->total_addresses < 1)
1007 res = GNUNET_SYSERR;
1011 net->total_addresses--;
1012 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
1016 if (GNUNET_YES == active)
1018 if (net->active_addresses < 1)
1021 res = GNUNET_SYSERR;
1025 net->active_addresses--;
1026 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
1028 if (s->active_addresses < 1)
1031 res = GNUNET_SYSERR;
1035 s->active_addresses--;
1036 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
1044 * Solver API functions
1045 * ---------------------------
1049 * Changes the preferences for a peer in the problem
1051 * @param solver the solver handle
1052 * @param peer the peer to change the preference for
1053 * @param kind the kind to change the preference
1054 * @param pref_rel the normalized preference value for this kind over all clients
1057 GAS_proportional_address_change_preference (void *solver,
1058 const struct GNUNET_PeerIdentity *peer,
1059 enum GNUNET_ATS_PreferenceKind kind,
1062 struct GAS_PROPORTIONAL_Handle *s = solver;
1063 GNUNET_assert(NULL != solver);
1064 GNUNET_assert(NULL != peer);
1066 distribute_bandwidth_in_network (s, NULL, NULL);
1071 * Get application feedback for a peer
1073 * @param solver the solver handle
1074 * @param application the application
1075 * @param peer the peer to change the preference for
1076 * @param scope the time interval for this feedback: [now - scope .. now]
1077 * @param kind the kind to change the preference
1078 * @param score the score
1081 GAS_proportional_address_preference_feedback (void *solver, void *application,
1082 const struct GNUNET_PeerIdentity *peer,
1083 const struct GNUNET_TIME_Relative scope,
1084 enum GNUNET_ATS_PreferenceKind kind, double score)
1086 struct GAS_PROPORTIONAL_Handle *s = solver;
1087 GNUNET_assert(NULL != solver);
1088 GNUNET_assert(NULL != peer);
1090 GNUNET_assert(NULL != s);
1095 * Get the preferred address for a specific peer
1097 * @param solver the solver handle
1098 * @param peer the identity of the peer
1100 const struct ATS_Address *
1101 GAS_proportional_get_preferred_address (void *solver,
1102 const struct GNUNET_PeerIdentity *peer)
1104 struct GAS_PROPORTIONAL_Handle *s = solver;
1105 struct Network *net_prev;
1106 struct Network *net_cur;
1107 struct ATS_Address *prev;
1108 struct FindBestAddressCtx fba_ctx;
1109 struct AddressSolverInformation *asi;
1110 struct AddressSolverInformation *asi_prev;
1112 GNUNET_assert(s != NULL);
1113 GNUNET_assert(peer != NULL);
1115 /* Add to list of pending requests */
1116 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests,
1119 GNUNET_assert (GNUNET_OK ==
1120 GNUNET_CONTAINER_multipeermap_put (s->requests,
1122 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1125 /* Get address with: stick to current address, lower distance, lower latency */
1127 fba_ctx.best = NULL;
1129 GNUNET_CONTAINER_multipeermap_get_multiple (s->addresses, peer,
1130 &find_best_address_it, &fba_ctx);
1131 if (NULL == fba_ctx.best)
1133 LOG(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1138 LOG(GNUNET_ERROR_TYPE_INFO, "Suggesting %s address %p for peer `%s'\n",
1139 (GNUNET_NO == fba_ctx.best->active) ? "inactive" : "active", fba_ctx.best,
1141 asi = fba_ctx.best->solver_information;
1142 net_cur = asi->network ;
1143 if (NULL == fba_ctx.best)
1145 LOG(GNUNET_ERROR_TYPE_ERROR,
1146 "Trying to suggesting unknown address peer `%s'\n", GNUNET_i2s (peer));
1150 if (GNUNET_YES == fba_ctx.best->active)
1152 /* This address was selected previously, so no need to update quotas */
1153 return fba_ctx.best;
1156 /* This address was not active, so we have to:
1158 * - mark previous active address as not active
1159 * - update quota for previous address network
1160 * - update quota for this address network
1162 prev = get_active_address (s, s->addresses, peer);
1165 asi_prev = prev->solver_information;
1166 net_prev = (struct Network *) asi_prev->network;
1167 prev->active = GNUNET_NO; /* No active any longer */
1168 prev->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1169 prev->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1170 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
1172 distribute_bandwidth_in_network (s, net_prev, NULL);
1175 if (GNUNET_NO == (is_bandwidth_available_in_network (net_cur)))
1177 GNUNET_break(0); /* This should never happen*/
1181 fba_ctx.best->active = GNUNET_YES;
1182 addresse_increment (s, net_cur, GNUNET_NO, GNUNET_YES);
1183 distribute_bandwidth_in_network (s, net_cur, fba_ctx.best);
1184 return fba_ctx.best;
1188 * Stop notifying about address and bandwidth changes for this peer
1190 * @param solver the solver handle
1191 * @param peer the peer
1194 GAS_proportional_stop_get_preferred_address (void *solver,
1195 const struct GNUNET_PeerIdentity *peer)
1197 struct GAS_PROPORTIONAL_Handle *s = solver;
1198 struct ATS_Address *cur;
1199 struct AddressSolverInformation *asi;
1200 struct Network *cur_net;
1203 == GNUNET_CONTAINER_multipeermap_contains (s->requests,
1205 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (s->requests, peer,
1208 cur = get_active_address (s,
1209 s->addresses, peer);
1212 /* Disabling current address */
1213 asi = cur->solver_information;
1214 cur_net = asi->network ;
1215 cur->active = GNUNET_NO; /* No active any longer */
1216 cur->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1217 cur->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1218 if (GNUNET_SYSERR == addresse_decrement (s, cur_net, GNUNET_NO, GNUNET_YES))
1220 distribute_bandwidth_in_network (s, cur_net, NULL );
1226 * Remove an address from the solver
1228 * @param solver the solver handle
1229 * @param address the address to remove
1230 * @param session_only delete only session not whole address
1233 GAS_proportional_address_delete (void *solver, struct ATS_Address *address,
1236 struct GAS_PROPORTIONAL_Handle *s = solver;
1237 struct Network *net;
1238 struct AddressWrapper *aw;
1239 struct AddressSolverInformation *asi;
1240 const struct ATS_Address *new_address;
1243 /* Remove an adress completely, we have to:
1244 * - Remove from specific network
1245 * - Decrease number of total addresses
1247 * - decrease number of active addreses
1250 asi = address->solver_information;
1260 if (GNUNET_NO == session_only)
1262 LOG(GNUNET_ERROR_TYPE_INFO,
1263 "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1264 (GNUNET_NO == address->active) ? "inactive" : "active", address,
1265 GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1266 net->active_addresses);
1268 /* Remove address */
1269 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1270 for (aw = net->head; NULL != aw; aw = aw->next)
1272 if (aw->addr == address)
1280 GNUNET_CONTAINER_DLL_remove(net->head, net->tail, aw);
1281 GNUNET_free_non_null (aw->addr->solver_information);
1286 /* Remove session only: remove if active and update */
1287 LOG(GNUNET_ERROR_TYPE_INFO,
1288 "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1289 (GNUNET_NO == address->active) ? "inactive" : "active", address,
1290 GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1291 net->active_addresses);
1294 if (GNUNET_YES == address->active)
1296 /* Address was active, remove from network and update quotas*/
1297 address->active = GNUNET_NO;
1298 address->assigned_bw_in = BANDWIDTH_ZERO;
1299 address->assigned_bw_out = BANDWIDTH_ZERO;
1301 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1303 distribute_bandwidth_in_network (s, net, NULL);
1304 if (NULL == (new_address = GAS_proportional_get_preferred_address (s, &address->peer)))
1306 /* No alternative address found, disconnect peer */
1307 s->bw_changed (s->bw_changed_cls, address);
1311 s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) new_address);
1314 LOG(GNUNET_ERROR_TYPE_INFO,
1315 "After deleting address now total %u and active %u addresses in network `%s'\n",
1316 net->total_addresses, net->active_addresses, net->desc);
1321 * Start a bulk operation
1323 * @param solver the solver
1326 GAS_proportional_bulk_start (void *solver)
1328 LOG(GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1329 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1331 GNUNET_assert(NULL != solver);
1337 * Bulk operation done
1340 GAS_proportional_bulk_stop (void *solver)
1342 LOG(GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1344 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1345 GNUNET_assert(NULL != solver);
1347 if (s->bulk_lock < 1)
1353 if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1355 LOG(GNUNET_ERROR_TYPE_DEBUG, "No lock pending, recalculating\n");
1356 distribute_bandwidth_in_network (s, NULL, NULL);
1357 s->bulk_requests = 0;
1363 * Add a new single address to a network
1365 * @param solver the solver Handle
1366 * @param address the address to add
1367 * @param network network type of this address
1370 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1374 * Transport properties for this address have changed
1376 * @param solver solver handle
1377 * @param address the address
1378 * @param type the ATSI type in HBO
1379 * @param abs_value the absolute value of the property
1380 * @param rel_value the normalized value
1383 GAS_proportional_address_property_changed (void *solver,
1384 struct ATS_Address *address, uint32_t type, uint32_t abs_value,
1387 struct GAS_PROPORTIONAL_Handle *s;
1389 struct AddressSolverInformation *asi;
1391 GNUNET_assert(NULL != solver);
1392 GNUNET_assert(NULL != address);
1394 s = (struct GAS_PROPORTIONAL_Handle *) solver;
1395 asi = address->solver_information;
1404 LOG(GNUNET_ERROR_TYPE_DEBUG,
1405 "Property `%s' for peer `%s' address %p changed to %.2f \n",
1406 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1407 address, rel_value);
1410 case GNUNET_ATS_UTILIZATION_OUT:
1411 case GNUNET_ATS_UTILIZATION_IN:
1412 case GNUNET_ATS_UTILIZATION_PAYLOAD_IN:
1413 case GNUNET_ATS_UTILIZATION_PAYLOAD_OUT:
1414 case GNUNET_ATS_QUALITY_NET_DELAY:
1415 case GNUNET_ATS_QUALITY_NET_DISTANCE:
1416 case GNUNET_ATS_COST_WAN:
1417 case GNUNET_ATS_COST_LAN:
1418 case GNUNET_ATS_COST_WLAN:
1419 distribute_bandwidth_in_network (s, n, NULL);
1425 * Transport session for this address has changed
1427 * NOTE: values in addresses are already updated
1429 * @param solver solver handle
1430 * @param address the address
1431 * @param cur_session the current session
1432 * @param new_session the new session
1435 GAS_proportional_address_session_changed (void *solver,
1436 struct ATS_Address *address, uint32_t cur_session, uint32_t new_session)
1438 if (cur_session != new_session)
1440 LOG(GNUNET_ERROR_TYPE_DEBUG, "Session changed from %u to %u\n", cur_session,
1446 * Usage for this address has changed
1448 * NOTE: values in addresses are already updated
1450 * @param solver solver handle
1451 * @param address the address
1452 * @param in_use usage state
1455 GAS_proportional_address_inuse_changed (void *solver,
1456 struct ATS_Address *address, int in_use)
1458 LOG(GNUNET_ERROR_TYPE_DEBUG, "Usage changed to %s\n",
1459 (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1463 * Network scope for this address has changed
1465 * NOTE: values in addresses are already updated
1467 * @param solver solver handle
1468 * @param address the address
1469 * @param current_network the current network
1470 * @param new_network the new network
1473 GAS_proportional_address_change_network (void *solver,
1474 struct ATS_Address *address, uint32_t current_network, uint32_t new_network)
1476 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1477 struct AddressSolverInformation *asi;
1478 int save_active = GNUNET_NO;
1480 struct Network *new_net = NULL;
1482 if (current_network == new_network)
1488 /* Network changed */
1489 LOG(GNUNET_ERROR_TYPE_DEBUG,
1490 "Network type changed, moving %s address from `%s' to `%s'\n",
1491 (GNUNET_YES == address->active) ? "active" : "inactive",
1492 GNUNET_ATS_print_network_type (current_network),
1493 GNUNET_ATS_print_network_type (new_network));
1495 save_active = address->active;
1497 /* Disable and assign no bandwidth */
1498 address->active = GNUNET_NO;
1499 address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1500 address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1502 /* Remove from old network */
1503 GAS_proportional_address_delete (solver, address, GNUNET_NO);
1505 /* Set new network type */
1506 if (NULL == (new_net = get_network (solver, new_network)))
1508 /* Address changed to invalid network... */
1509 LOG(GNUNET_ERROR_TYPE_ERROR,
1510 _("Invalid network type `%u' `%s': Disconnect!\n"), new_network,
1511 GNUNET_ATS_print_network_type (new_network));
1513 /* Find new address to suggest since no bandwidth in network*/
1514 if (NULL == GAS_proportional_get_preferred_address (s, &address->peer))
1516 /* No alternative address found, disconnect peer */
1517 s->bw_changed (s->bw_changed_cls, address);
1522 /* Add to new network and update*/
1523 asi = address->solver_information;
1524 asi->network = new_net;
1525 GAS_proportional_address_add (solver, address, new_network);
1526 if (GNUNET_YES == save_active)
1528 /* check if bandwidth available in new network */
1529 if (GNUNET_YES == (is_bandwidth_available_in_network (new_net)))
1531 /* Assign bandwidth to updated address */
1532 address->active = GNUNET_YES;
1533 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
1534 distribute_bandwidth_in_network (solver, new_net, NULL);
1538 LOG(GNUNET_ERROR_TYPE_DEBUG,
1539 "Not enough bandwidth in new network, suggesting alternative address ..\n");
1540 /* Find new address to suggest since no bandwidth in network*/
1541 if (NULL == GAS_proportional_get_preferred_address (s, &address->peer))
1543 /* No alternative address found, disconnect peer */
1544 s->bw_changed (s->bw_changed_cls, address);
1551 * Add a new single address to a network
1553 * @param solver the solver Handle
1554 * @param address the address to add
1555 * @param network network type of this address
1558 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1561 struct GAS_PROPORTIONAL_Handle *s = solver;
1562 struct Network *net = NULL;
1563 struct AddressWrapper *aw = NULL;
1564 struct AddressSolverInformation *asi;
1566 GNUNET_assert(NULL != s);
1567 net = get_network (s, network);
1574 aw = GNUNET_new (struct AddressWrapper);
1576 GNUNET_CONTAINER_DLL_insert(net->head, net->tail, aw);
1577 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1579 asi = GNUNET_new (struct AddressSolverInformation);
1581 asi->calculated_quota_in_NBO = 0;
1582 asi->calculated_quota_out_NBO = 0;
1583 aw->addr->solver_information = asi;
1585 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1587 if (NULL == get_active_address (s, s->addresses, &address->peer))
1589 if (NULL != GAS_proportional_get_preferred_address (s, &address->peer))
1590 s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) address);
1594 LOG(GNUNET_ERROR_TYPE_INFO,
1595 "Adding new address %p for peer `%s', now total %u and active %u addresses in network `%s'\n",
1596 address, GNUNET_i2s(&address->peer), net->total_addresses, net->active_addresses, net->desc);
1600 /* end of plugin_ats_proportional.c */