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/gnunet-service-ats-solver_proportional.c
23 * @brief ATS proportional solver
24 * @author Matthias Wachs
25 * @author Christian Grothoff
27 #include "libgnunet_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 * Wrapper for addresses to store them in network's linked list
351 struct AddressWrapper
356 struct AddressWrapper *next;
361 struct AddressWrapper *prev;
366 struct ATS_Address *addr;
370 * Important solver functions
371 * ---------------------------
375 libgnunet_plugin_ats_proportional_init (void *cls)
377 struct GNUNET_ATS_PluginEnvironment *env = cls;
378 struct GAS_PROPORTIONAL_Handle *s;
379 struct Network * cur;
380 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
383 GNUNET_assert (NULL != env);
384 GNUNET_assert(NULL != env->cfg);
385 GNUNET_assert(NULL != env->stats);
386 GNUNET_assert(NULL != env->bandwidth_changed_cb);
387 GNUNET_assert(NULL != env->get_preferences);
388 GNUNET_assert(NULL != env->get_property);
390 s = GNUNET_malloc (sizeof (struct GAS_PROPORTIONAL_Handle));
392 env->sf.s_add = &GAS_proportional_address_add;
393 env->sf.s_address_update_property = &GAS_proportional_address_property_changed;
394 env->sf.s_address_update_session = &GAS_proportional_address_session_changed;
395 env->sf.s_address_update_inuse = &GAS_proportional_address_inuse_changed;
396 env->sf.s_address_update_network = &GAS_proportional_address_change_network;
397 env->sf.s_get = &GAS_proportional_get_preferred_address;
398 env->sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
399 env->sf.s_pref = &GAS_proportional_address_change_preference;
400 env->sf.s_feedback = &GAS_proportional_address_preference_feedback;
401 env->sf.s_del = &GAS_proportional_address_delete;
402 env->sf.s_bulk_start = &GAS_proportional_bulk_start;
403 env->sf.s_bulk_stop = &GAS_proportional_bulk_stop;
405 s->stats = (struct GNUNET_STATISTICS_Handle *) env->stats;
406 s->bw_changed = env->bandwidth_changed_cb;
407 s->bw_changed_cls = env->bw_changed_cb_cls;
408 s->get_preferences = env->get_preferences;
409 s->get_preferences_cls = env->get_preference_cls;
410 s->get_properties = env->get_property;
411 s->get_properties_cls = env->get_property_cls;
412 s->network_count = env->network_count;
413 s->network_entries = GNUNET_malloc (env->network_count * sizeof (struct Network));
416 s->active_addresses = 0;
417 s->total_addresses = 0;
418 s->bulk_lock = GNUNET_NO;
419 s->addresses = env->addresses;
420 s->requests = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
422 for (c = 0; c < env->network_count; c++)
424 cur = &s->network_entries[c];
425 cur->total_addresses = 0;
426 cur->active_addresses = 0;
427 cur->type = env->networks[c];
428 cur->total_quota_in = env->in_quota[c];
429 cur->total_quota_out = env->out_quota[c];
430 cur->desc = net_str[c];
431 GNUNET_asprintf (&cur->stat_total,
432 "# ATS addresses %s total", cur->desc);
433 GNUNET_asprintf (&cur->stat_active,
434 "# ATS active addresses %s total", cur->desc);
435 LOG (GNUNET_ERROR_TYPE_INFO, "Added network %u `%s' %p\n", c, cur->desc, s);
441 libgnunet_plugin_ats_proportional_done (void *cls)
443 struct GAS_PROPORTIONAL_Handle *s = cls;
444 struct AddressWrapper *cur;
445 struct AddressWrapper *next;
447 GNUNET_assert(s != NULL);
448 for (c = 0; c < s->network_count; c++)
450 if (s->network_entries[c].total_addresses > 0)
452 LOG(GNUNET_ERROR_TYPE_DEBUG,
453 "Had %u addresses for network `%s' not deleted during shutdown\n",
454 s->network_entries[c].total_addresses, s->network_entries[c].desc);
458 if (s->network_entries[c].active_addresses > 0)
460 LOG(GNUNET_ERROR_TYPE_ERROR,
461 "Had %u active addresses for network `%s' not deleted during shutdown\n",
462 s->network_entries[c].active_addresses, s->network_entries[c].desc);
466 next = s->network_entries[c].head;
467 while (NULL != (cur = next))
470 GNUNET_CONTAINER_DLL_remove(s->network_entries[c].head,
471 s->network_entries[c].tail, cur);
474 GNUNET_free(s->network_entries[c].stat_total);
475 GNUNET_free(s->network_entries[c].stat_active);
477 if (s->total_addresses > 0)
479 LOG(GNUNET_ERROR_TYPE_ERROR,
480 "Had %u addresses not deleted during shutdown\n", s->total_addresses);
483 if (s->active_addresses > 0)
485 LOG(GNUNET_ERROR_TYPE_ERROR,
486 "Had %u active addresses not deleted during shutdown\n",
487 s->active_addresses);
490 GNUNET_free (s->network_entries);
491 GNUNET_CONTAINER_multipeermap_destroy (s->requests);
498 * Test if bandwidth is available in this network to add an additional address
500 * @param net the network type to update
501 * @return GNUNET_YES or GNUNET_NO
504 is_bandwidth_available_in_network (struct Network *net)
506 GNUNET_assert(NULL != net);
507 unsigned int na = net->active_addresses + 1;
508 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
509 if (((net->total_quota_in / na) > min_bw)
510 && ((net->total_quota_out / na) > min_bw))
512 LOG(GNUNET_ERROR_TYPE_DEBUG,
513 "Enough bandwidth available for %u active addresses in network `%s'\n",
518 LOG(GNUNET_ERROR_TYPE_DEBUG,
519 "Not enough bandwidth available for %u active addresses in network `%s'\n",
525 * Update bandwidth assigned to peers in this network
527 * @param s the solver handle
528 * @param net the network type to update
529 * @param address_except address excluded from notification, since we suggest
533 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
534 struct Network *net, struct ATS_Address *address_except)
536 unsigned long long remaining_quota_in = 0;
537 unsigned long long quota_out_used = 0;
539 unsigned long long remaining_quota_out = 0;
540 unsigned long long quota_in_used = 0;
541 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
543 double total_prefs; /* Important: has to be double not float due to precision */
544 double cur_pref; /* Important: has to be double not float due to precision */
545 const double *t = NULL; /* Important: has to be double not float due to precision */
548 unsigned long long assigned_quota_in = 0;
549 unsigned long long assigned_quota_out = 0;
550 struct AddressWrapper *cur;
552 if (GNUNET_YES == s->bulk_lock)
558 LOG(GNUNET_ERROR_TYPE_DEBUG,
559 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
560 net->desc, net->active_addresses, net->total_quota_in,
561 net->total_quota_in);
563 if (net->active_addresses == 0)
564 return; /* no addresses to update */
567 * Assign every peer in network minimum Bandwidth
568 * Distribute bandwidth left according to preference
571 if ((net->active_addresses * min_bw) > net->total_quota_in)
576 if ((net->active_addresses * min_bw) > net->total_quota_out)
582 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
583 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
584 LOG(GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
585 remaining_quota_in, remaining_quota_out);
587 for (cur = net->head; NULL != cur; cur = cur->next)
589 if (GNUNET_YES == cur->addr->active)
592 NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
595 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
597 if (c != GNUNET_ATS_PREFERENCE_END)
599 //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
603 total_prefs += (peer_prefs / (GNUNET_ATS_PreferenceCount - 1));
606 for (cur = net->head; NULL != cur; cur = cur->next)
608 if (GNUNET_YES == cur->addr->active)
612 NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
614 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
616 if (c != GNUNET_ATS_PREFERENCE_END)
621 assigned_quota_in = min_bw
622 + ((cur_pref / total_prefs) * remaining_quota_in);
623 assigned_quota_out = min_bw
624 + ((cur_pref / total_prefs) * remaining_quota_out);
626 LOG(GNUNET_ERROR_TYPE_DEBUG,
627 "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
628 GNUNET_i2s (&cur->addr->peer), cur_pref, total_prefs,
629 assigned_quota_in, assigned_quota_out);
633 assigned_quota_in = 0;
634 assigned_quota_out = 0;
637 quota_in_used += assigned_quota_in;
638 quota_out_used += assigned_quota_out;
639 /* Prevent overflow due to rounding errors */
640 if (assigned_quota_in > UINT32_MAX)
641 assigned_quota_in = UINT32_MAX;
642 if (assigned_quota_out > UINT32_MAX)
643 assigned_quota_out = UINT32_MAX;
645 /* Compare to current bandwidth assigned */
646 if ((assigned_quota_in != ntohl (cur->addr->assigned_bw_in.value__))
647 || (assigned_quota_out != ntohl (cur->addr->assigned_bw_out.value__)))
649 cur->addr->assigned_bw_in.value__ = htonl (assigned_quota_in);
650 cur->addr->assigned_bw_out.value__ = htonl (assigned_quota_out);
651 /* Notify on change */
652 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
653 s->bw_changed (s->bw_changed_cls, cur->addr);
657 LOG(GNUNET_ERROR_TYPE_DEBUG,
658 "Total bandwidth assigned is (in/out): %llu /%llu\n", quota_in_used,
660 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
662 LOG(GNUNET_ERROR_TYPE_ERROR,
663 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
664 net->active_addresses, quota_out_used, net->total_quota_out);
666 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
668 LOG(GNUNET_ERROR_TYPE_ERROR,
669 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
670 net->active_addresses, quota_in_used, net->total_quota_in);
674 struct FindBestAddressCtx
676 struct GAS_PROPORTIONAL_Handle *s;
677 struct ATS_Address *best;
681 find_property_index (uint32_t type)
683 int existing_types[] = GNUNET_ATS_QualityProperties;
685 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
686 if (existing_types[c] == type)
688 return GNUNET_SYSERR;
692 * Find a "good" address to use for a peer by iterating over the addresses for this peer.
693 * If we already have an existing address, we stick to it.
694 * Otherwise, we pick by lowest distance and then by lowest latency.
696 * @param cls the 'struct ATS_Address**' where we store the result
698 * @param value another 'struct ATS_Address*' to consider using
699 * @return GNUNET_OK (continue to iterate)
702 find_best_address_it (void *cls,
703 const struct GNUNET_PeerIdentity *key,
706 struct FindBestAddressCtx *fba_ctx = (struct FindBestAddressCtx *) cls;
707 struct ATS_Address *current = (struct ATS_Address *) value;
708 struct GNUNET_TIME_Absolute now;
709 struct Network *net = (struct Network *) current->solver_information;
710 const double *norm_prop_cur;
711 const double *norm_prop_prev;
714 now = GNUNET_TIME_absolute_get ();
716 if (current->blocked_until.abs_value_us
717 == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value_us)
719 /* This address is blocked for suggestion */
720 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address %p blocked for suggestion for %s \n",
722 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (now, current->blocked_until), GNUNET_YES));
725 if (GNUNET_NO == is_bandwidth_available_in_network (net))
726 return GNUNET_OK; /* There's no bandwidth available in this network */
727 if (NULL != fba_ctx->best)
729 GNUNET_assert(NULL != fba_ctx->best->plugin);
730 GNUNET_assert(NULL != current->plugin);
731 if (0 == strcmp (fba_ctx->best->plugin, current->plugin))
733 if ((0 != fba_ctx->best->addr_len) && (0 == current->addr_len))
735 /* saved address was an outbound address, but we have an inbound address */
736 fba_ctx->best = current;
739 if (0 == fba_ctx->best->addr_len)
741 /* saved address was an inbound address, so do not overwrite */
746 if (NULL == fba_ctx->best)
748 fba_ctx->best = current;
751 if ((ntohl (fba_ctx->best->assigned_bw_in.value__) == 0)
752 && (ntohl (current->assigned_bw_in.value__) > 0))
754 /* stick to existing connection */
755 fba_ctx->best = current;
759 norm_prop_cur = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
760 (const struct ATS_Address *) current);
761 norm_prop_prev = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
762 (const struct ATS_Address *) fba_ctx->best);
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s previous %.2f current %.2f\n",
765 "DISTANCE", norm_prop_cur[1], norm_prop_cur[1]);
767 index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
768 if (GNUNET_SYSERR == index)
773 if (norm_prop_cur[index] < norm_prop_prev[index])
775 /* user shorter distance */
776 fba_ctx->best = current;
780 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s previous %.2f current %.2f\n",
781 "DELAY", norm_prop_cur[1], norm_prop_cur[1]);
783 index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
784 if (GNUNET_SYSERR == index)
789 if (norm_prop_cur[index] < norm_prop_prev[index])
791 /* user shorter delay */
792 fba_ctx->best = current;
802 * ---------------------------
806 * Update bandwidth assignment for all networks
808 * @param s the solver handle
811 distribute_bandwidth_in_all_networks (struct GAS_PROPORTIONAL_Handle *s)
814 for (i = 0; i < s->network_count; i++)
815 distribute_bandwidth_in_network (s, &s->network_entries[i], NULL );
819 * Lookup network struct by type
821 * @param s the solver handle
822 * @param type the network type
823 * @return the network struct
825 static struct Network *
826 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
829 for (c = 0; c < s->network_count; c++)
831 if (s->network_entries[c].type == type)
832 return &s->network_entries[c];
839 * Hashmap Iterator to find current active address for peer
841 * @param cls last active address
842 * @param key peer's key
843 * @param value address to check
844 * @return #GNUNET_NO on double active address else #GNUNET_YES;
847 get_active_address_it (void *cls,
848 const struct GNUNET_PeerIdentity *key,
851 struct ATS_Address **dest = cls;
852 struct ATS_Address *aa = (struct ATS_Address *) value;
854 if (GNUNET_YES == aa->active)
859 /* should never happen */
860 LOG(GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n",
861 GNUNET_i2s (&aa->peer));
871 * Find current active address for peer
873 * @param solver the solver handle
874 * @param addresses the address set
875 * @param peer the peer
876 * @return active address or NULL
878 static struct ATS_Address *
879 get_active_address (void *solver,
880 const struct GNUNET_CONTAINER_MultiPeerMap * addresses,
881 const struct GNUNET_PeerIdentity *peer)
883 struct ATS_Address * dest = NULL;
885 GNUNET_CONTAINER_multipeermap_get_multiple (addresses, peer,
886 &get_active_address_it, &dest);
892 addresse_increment (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
893 int total, int active)
895 if (GNUNET_YES == total)
897 s->total_addresses++;
898 net->total_addresses++;
899 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
900 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
902 if (GNUNET_YES == active)
904 net->active_addresses++;
905 s->active_addresses++;
906 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1,
908 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
915 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
916 int total, int active)
919 if (GNUNET_YES == total)
921 if (s->total_addresses < 1)
928 s->total_addresses--;
929 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
932 if (net->total_addresses < 1)
939 net->total_addresses--;
940 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
944 if (GNUNET_YES == active)
946 if (net->active_addresses < 1)
953 net->active_addresses--;
954 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
956 if (s->active_addresses < 1)
963 s->active_addresses--;
964 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
972 * Solver API functions
973 * ---------------------------
977 * Changes the preferences for a peer in the problem
979 * @param solver the solver handle
980 * @param peer the peer to change the preference for
981 * @param kind the kind to change the preference
982 * @param pref_rel the normalized preference value for this kind over all clients
985 GAS_proportional_address_change_preference (void *solver,
986 const struct GNUNET_PeerIdentity *peer,
987 enum GNUNET_ATS_PreferenceKind kind,
990 struct GAS_PROPORTIONAL_Handle *s = solver;
991 GNUNET_assert(NULL != solver);
992 GNUNET_assert(NULL != peer);
994 distribute_bandwidth_in_all_networks (s);
999 * Get application feedback for a peer
1001 * @param solver the solver handle
1002 * @param application the application
1003 * @param peer the peer to change the preference for
1004 * @param scope the time interval for this feedback: [now - scope .. now]
1005 * @param kind the kind to change the preference
1006 * @param score the score
1009 GAS_proportional_address_preference_feedback (void *solver, void *application,
1010 const struct GNUNET_PeerIdentity *peer,
1011 const struct GNUNET_TIME_Relative scope,
1012 enum GNUNET_ATS_PreferenceKind kind, double score)
1014 struct GAS_PROPORTIONAL_Handle *s = solver;
1015 GNUNET_assert(NULL != solver);
1016 GNUNET_assert(NULL != peer);
1018 GNUNET_assert(NULL != s);
1023 * Get the preferred address for a specific peer
1025 * @param solver the solver handle
1026 * @param peer the identity of the peer
1028 const struct ATS_Address *
1029 GAS_proportional_get_preferred_address (void *solver,
1030 const struct GNUNET_PeerIdentity *peer)
1032 struct GAS_PROPORTIONAL_Handle *s = solver;
1033 struct Network *net_prev;
1034 struct Network *net_cur;
1035 struct ATS_Address *prev;
1036 struct FindBestAddressCtx fba_ctx;
1038 GNUNET_assert(s != NULL);
1039 GNUNET_assert(peer != NULL);
1041 /* Add to list of pending requests */
1042 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests,
1045 GNUNET_assert (GNUNET_OK ==
1046 GNUNET_CONTAINER_multipeermap_put (s->requests,
1048 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1051 /* Get address with: stick to current address, lower distance, lower latency */
1053 fba_ctx.best = NULL;
1055 GNUNET_CONTAINER_multipeermap_get_multiple (s->addresses, peer,
1056 &find_best_address_it, &fba_ctx);
1057 if (NULL == fba_ctx.best)
1059 LOG(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1064 LOG(GNUNET_ERROR_TYPE_INFO, "Suggesting %s address %p for peer `%s'\n",
1065 (GNUNET_NO == fba_ctx.best->active) ? "inactive" : "active", fba_ctx.best,
1067 net_cur = (struct Network *) fba_ctx.best->solver_information;
1068 if (NULL == fba_ctx.best)
1070 LOG(GNUNET_ERROR_TYPE_ERROR,
1071 "Trying to suggesting unknown address peer `%s'\n", GNUNET_i2s (peer));
1075 if (GNUNET_YES == fba_ctx.best->active)
1077 /* This address was selected previously, so no need to update quotas */
1078 return fba_ctx.best;
1081 /* This address was not active, so we have to:
1083 * - mark previous active address as not active
1084 * - update quota for previous address network
1085 * - update quota for this address network
1087 prev = get_active_address (s,
1088 s->addresses, peer);
1091 net_prev = (struct Network *) prev->solver_information;
1092 prev->active = GNUNET_NO; /* No active any longer */
1093 prev->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1094 prev->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1095 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
1097 distribute_bandwidth_in_network (s, net_prev, NULL );
1100 if (GNUNET_NO == (is_bandwidth_available_in_network (fba_ctx.best->solver_information)))
1102 GNUNET_break(0); /* This should never happen*/
1106 fba_ctx.best->active = GNUNET_YES;
1107 addresse_increment (s, net_cur, GNUNET_NO, GNUNET_YES);
1108 distribute_bandwidth_in_network (s, net_cur, fba_ctx.best );
1109 return fba_ctx.best;
1113 * Stop notifying about address and bandwidth changes for this peer
1115 * @param solver the solver handle
1116 * @param peer the peer
1119 GAS_proportional_stop_get_preferred_address (void *solver,
1120 const struct GNUNET_PeerIdentity *peer)
1122 struct GAS_PROPORTIONAL_Handle *s = solver;
1123 struct ATS_Address *cur;
1124 struct Network *cur_net;
1127 == GNUNET_CONTAINER_multipeermap_contains (s->requests,
1129 GNUNET_CONTAINER_multipeermap_remove (s->requests, peer,
1132 cur = get_active_address (s,
1133 s->addresses, peer);
1136 /* Disabling current address */
1137 cur_net = (struct Network *) cur->solver_information;
1138 cur->active = GNUNET_NO; /* No active any longer */
1139 cur->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1140 cur->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1141 if (GNUNET_SYSERR == addresse_decrement (s, cur_net, GNUNET_NO, GNUNET_YES))
1143 distribute_bandwidth_in_network (s, cur_net, NULL );
1149 * Remove an address from the solver
1151 * @param solver the solver handle
1152 * @param address the address to remove
1153 * @param session_only delete only session not whole address
1156 GAS_proportional_address_delete (void *solver, struct ATS_Address *address,
1159 struct GAS_PROPORTIONAL_Handle *s = solver;
1160 struct Network *net;
1161 struct AddressWrapper *aw;
1162 const struct ATS_Address *new_address;
1165 /* Remove an adress completely, we have to:
1166 * - Remove from specific network
1167 * - Decrease number of total addresses
1169 * - decrease number of active addreses
1173 net = (struct Network *) address->solver_information;
1175 if (GNUNET_NO == session_only)
1177 LOG(GNUNET_ERROR_TYPE_INFO,
1178 "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1179 (GNUNET_NO == address->active) ? "inactive" : "active", address,
1180 GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1181 net->active_addresses);
1183 /* Remove address */
1184 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1185 for (aw = net->head; NULL != aw; aw = aw->next)
1187 if (aw->addr == address)
1195 GNUNET_CONTAINER_DLL_remove(net->head, net->tail, aw);
1200 /* Remove session only: remove if active and update */
1201 LOG(GNUNET_ERROR_TYPE_INFO,
1202 "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1203 (GNUNET_NO == address->active) ? "inactive" : "active", address,
1204 GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1205 net->active_addresses);
1208 if (GNUNET_YES == address->active)
1210 /* Address was active, remove from network and update quotas*/
1211 address->active = GNUNET_NO;
1212 address->assigned_bw_in = BANDWIDTH_ZERO;
1213 address->assigned_bw_out = BANDWIDTH_ZERO;
1215 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1217 distribute_bandwidth_in_network (s, net, NULL );
1218 if (NULL == (new_address = GAS_proportional_get_preferred_address (s, &address->peer)))
1220 /* No alternative address found, disconnect peer */
1221 s->bw_changed (s->bw_changed_cls, address);
1225 s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) new_address);
1228 LOG(GNUNET_ERROR_TYPE_DEBUG,
1229 "After deleting address now total %u and active %u addresses in network `%s'\n",
1230 net->total_addresses, net->active_addresses, net->desc);
1235 * Start a bulk operation
1237 * @param solver the solver
1240 GAS_proportional_bulk_start (void *solver)
1242 LOG(GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1243 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1245 GNUNET_assert(NULL != solver);
1251 * Bulk operation done
1254 GAS_proportional_bulk_stop (void *solver)
1256 LOG(GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1258 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1259 GNUNET_assert(NULL != solver);
1261 if (s->bulk_lock < 1)
1267 if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1269 LOG(GNUNET_ERROR_TYPE_DEBUG, "No lock pending, recalculating\n");
1270 distribute_bandwidth_in_all_networks (s);
1271 s->bulk_requests = 0;
1277 * Add a new single address to a network
1279 * @param solver the solver Handle
1280 * @param address the address to add
1281 * @param network network type of this address
1284 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1288 * Transport properties for this address have changed
1290 * @param solver solver handle
1291 * @param address the address
1292 * @param type the ATSI type in HBO
1293 * @param abs_value the absolute value of the property
1294 * @param rel_value the normalized value
1297 GAS_proportional_address_property_changed (void *solver,
1298 struct ATS_Address *address, uint32_t type, uint32_t abs_value,
1301 struct GAS_PROPORTIONAL_Handle *s;
1304 GNUNET_assert(NULL != solver);
1305 GNUNET_assert(NULL != address);
1307 s = (struct GAS_PROPORTIONAL_Handle *) solver;
1308 n = (struct Network *) address->solver_information;
1316 LOG(GNUNET_ERROR_TYPE_DEBUG,
1317 "Property `%s' for peer `%s' address %p changed to %.2f \n",
1318 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1319 address, rel_value);
1322 case GNUNET_ATS_UTILIZATION_UP:
1323 case GNUNET_ATS_UTILIZATION_DOWN:
1324 case GNUNET_ATS_QUALITY_NET_DELAY:
1325 case GNUNET_ATS_QUALITY_NET_DISTANCE:
1326 case GNUNET_ATS_COST_WAN:
1327 case GNUNET_ATS_COST_LAN:
1328 case GNUNET_ATS_COST_WLAN:
1329 distribute_bandwidth_in_network (s, n, GNUNET_NO);
1335 * Transport session for this address has changed
1337 * NOTE: values in addresses are already updated
1339 * @param solver solver handle
1340 * @param address the address
1341 * @param cur_session the current session
1342 * @param new_session the new session
1345 GAS_proportional_address_session_changed (void *solver,
1346 struct ATS_Address *address, uint32_t cur_session, uint32_t new_session)
1348 if (cur_session != new_session)
1350 LOG(GNUNET_ERROR_TYPE_DEBUG, "Session changed from %u to %u\n", cur_session,
1356 * Usage for this address has changed
1358 * NOTE: values in addresses are already updated
1360 * @param solver solver handle
1361 * @param address the address
1362 * @param in_use usage state
1365 GAS_proportional_address_inuse_changed (void *solver,
1366 struct ATS_Address *address, int in_use)
1368 LOG(GNUNET_ERROR_TYPE_DEBUG, "Usage changed to %s\n",
1369 (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1373 * Network scope for this address has changed
1375 * NOTE: values in addresses are already updated
1377 * @param solver solver handle
1378 * @param address the address
1379 * @param current_network the current network
1380 * @param new_network the new network
1383 GAS_proportional_address_change_network (void *solver,
1384 struct ATS_Address *address, uint32_t current_network, uint32_t new_network)
1386 struct ATS_Address *new;
1387 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1388 int save_active = GNUNET_NO;
1389 struct Network *new_net = NULL;
1391 if (current_network == new_network)
1397 /* Network changed */
1398 LOG(GNUNET_ERROR_TYPE_DEBUG,
1399 "Network type changed, moving %s address from `%s' to `%s'\n",
1400 (GNUNET_YES == address->active) ? "active" : "inactive",
1401 GNUNET_ATS_print_network_type (current_network),
1402 GNUNET_ATS_print_network_type (new_network));
1404 save_active = address->active;
1406 /* Disable and assign no bandwidth */
1407 address->active = GNUNET_NO;
1408 address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1409 address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1411 /* Remove from old network */
1412 GAS_proportional_address_delete (solver, address, GNUNET_NO);
1414 /* Set new network type */
1415 if (NULL == (new_net = get_network (solver, new_network)))
1417 /* Address changed to invalid network... */
1418 LOG(GNUNET_ERROR_TYPE_ERROR,
1419 _("Invalid network type `%u' `%s': Disconnect!\n"), new_network,
1420 GNUNET_ATS_print_network_type (new_network));
1422 /* Find new address to suggest since no bandwidth in network*/
1423 if (NULL == (new = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, &address->peer)))
1425 /* No alternative address found, disconnect peer */
1426 s->bw_changed (s->bw_changed_cls, address);
1431 /* Add to new network and update*/
1432 address->solver_information = new_net;
1433 GAS_proportional_address_add (solver, address, new_network);
1434 if (GNUNET_YES == save_active)
1436 /* check if bandwidth available in new network */
1437 if (GNUNET_YES == (is_bandwidth_available_in_network (new_net)))
1439 /* Assign bandwidth to updated address */
1440 address->active = GNUNET_YES;
1441 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
1442 distribute_bandwidth_in_network (solver, new_net, NULL );
1446 LOG(GNUNET_ERROR_TYPE_DEBUG,
1447 "Not enough bandwidth in new network, suggesting alternative address ..\n");
1448 /* Find new address to suggest since no bandwidth in network*/
1451 (struct ATS_Address *) GAS_proportional_get_preferred_address (s,
1454 /* No alternative address found, disconnect peer */
1455 s->bw_changed (s->bw_changed_cls, address);
1462 * Add a new single address to a network
1464 * @param solver the solver Handle
1465 * @param address the address to add
1466 * @param network network type of this address
1469 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1472 struct GAS_PROPORTIONAL_Handle *s = solver;
1473 struct Network *net = NULL;
1474 struct AddressWrapper *aw = NULL;
1475 const struct ATS_Address *new_address;
1477 GNUNET_assert(NULL != s);
1480 net = get_network (s, network);
1487 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
1489 GNUNET_CONTAINER_DLL_insert(net->head, net->tail, aw);
1490 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1491 aw->addr->solver_information = net;
1493 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1495 if (NULL == get_active_address (s, s->addresses, &address->peer))
1497 if (NULL != (new_address = GAS_proportional_get_preferred_address (s, &address->peer)))
1498 s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) address);
1501 LOG(GNUNET_ERROR_TYPE_DEBUG,
1502 "After adding address now total %u and active %u addresses in network `%s'\n",
1503 net->total_addresses, net->active_addresses, net->desc);
1507 /* end of gnunet-service-ats-solver_proportional.c */