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 PROPORTIONALITY_FACTOR 2.0
209 * A handle for the proportional solver
211 struct GAS_PROPORTIONAL_Handle
213 struct GNUNET_ATS_PluginEnvironment *env;
218 struct GNUNET_STATISTICS_Handle *stats;
221 * Hashmap containing all valid addresses
223 struct GNUNET_CONTAINER_MultiPeerMap *addresses;
226 * Pending address requests
228 struct GNUNET_CONTAINER_MultiPeerMap *requests;
231 * Bandwidth changed callback
233 GAS_bandwidth_changed_cb bw_changed;
236 * Bandwidth changed callback cls
238 void *bw_changed_cls;
241 * ATS function to get preferences
243 GAS_get_preferences get_preferences;
246 * Closure for ATS function to get preferences
248 void *get_preferences_cls;
251 * ATS function to get properties
253 GAS_get_properties get_properties;
256 * Closure for ATS function to get properties
258 void *get_properties_cls;
266 * Number of changes while solver was locked
271 * Total number of addresses for solver
273 unsigned int total_addresses;
276 * Number of active addresses for solver
278 unsigned int active_addresses;
283 struct Network *network_entries;
288 unsigned int network_count;
291 * Proportionality factor
298 double stability_factor;
302 * Representation of a network
312 * Network description
317 * Total inbound quota
320 unsigned long long total_quota_in;
323 * Total outbound quota
326 unsigned long long total_quota_out;
329 * Number of active addresses for this network
331 unsigned int active_addresses;
334 * Number of total addresses for this network
336 unsigned int total_addresses;
339 * String for statistics total addresses
344 * String for statistics active addresses
348 struct AddressWrapper *head;
349 struct AddressWrapper *tail;
353 * Address information stored in the solver
355 struct AddressSolverInformation
357 struct Network *network;
363 unsigned long long calculated_quota_in_NBO;
369 unsigned long long calculated_quota_out_NBO;
372 * When was this address activated
374 struct GNUNET_TIME_Absolute activated;
379 * Wrapper for addresses to store them in network's linked list
381 struct AddressWrapper
386 struct AddressWrapper *next;
391 struct AddressWrapper *prev;
396 struct ATS_Address *addr;
400 * Important solver functions
401 * ---------------------------
405 libgnunet_plugin_ats_proportional_init (void *cls)
407 struct GNUNET_ATS_PluginEnvironment *env = cls;
408 struct GAS_PROPORTIONAL_Handle *s;
409 struct Network * cur;
410 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
414 GNUNET_assert (NULL != env);
415 GNUNET_assert(NULL != env->cfg);
416 GNUNET_assert(NULL != env->bandwidth_changed_cb);
417 GNUNET_assert(NULL != env->get_preferences);
418 GNUNET_assert(NULL != env->get_property);
420 s = GNUNET_new (struct GAS_PROPORTIONAL_Handle);
422 env->sf.s_add = &GAS_proportional_address_add;
423 env->sf.s_address_update_property = &GAS_proportional_address_property_changed;
424 env->sf.s_address_update_session = &GAS_proportional_address_session_changed;
425 env->sf.s_address_update_inuse = &GAS_proportional_address_inuse_changed;
426 env->sf.s_address_update_network = &GAS_proportional_address_change_network;
427 env->sf.s_get = &GAS_proportional_get_preferred_address;
428 env->sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
429 env->sf.s_pref = &GAS_proportional_address_change_preference;
430 env->sf.s_feedback = &GAS_proportional_address_preference_feedback;
431 env->sf.s_del = &GAS_proportional_address_delete;
432 env->sf.s_bulk_start = &GAS_proportional_bulk_start;
433 env->sf.s_bulk_stop = &GAS_proportional_bulk_stop;
435 s->stats = (struct GNUNET_STATISTICS_Handle *) env->stats;
436 s->bw_changed = env->bandwidth_changed_cb;
437 s->bw_changed_cls = env->bw_changed_cb_cls;
438 s->get_preferences = env->get_preferences;
439 s->get_preferences_cls = env->get_preference_cls;
440 s->get_properties = env->get_property;
441 s->get_properties_cls = env->get_property_cls;
442 s->network_count = env->network_count;
443 s->network_entries = GNUNET_malloc (env->network_count * sizeof (struct Network));
446 s->active_addresses = 0;
447 s->total_addresses = 0;
448 s->bulk_lock = GNUNET_NO;
449 s->addresses = env->addresses;
450 s->requests = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
452 s->stability_factor = PROP_STABILITY_FACTOR;
453 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
454 "PROP_STABILITY_FACTOR", &f_tmp))
456 if ((f_tmp < 1.0) || (f_tmp > 2.0))
458 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
459 "PROP_STABILITY_FACTOR", f_tmp);
463 s->stability_factor = f_tmp;
464 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
465 "PROP_STABILITY_FACTOR", f_tmp);
469 s->prop_factor = PROPORTIONALITY_FACTOR;
470 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
471 "PROP_STABILITY_FACTOR", &f_tmp))
475 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
476 "PROP_PROPORTIONALITY_FACTOR", f_tmp);
480 s->prop_factor = f_tmp;
481 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
482 "PROP_PROPORTIONALITY_FACTOR", f_tmp);
486 for (c = 0; c < env->network_count; c++)
488 cur = &s->network_entries[c];
489 cur->total_addresses = 0;
490 cur->active_addresses = 0;
491 cur->type = env->networks[c];
492 cur->total_quota_in = env->in_quota[c];
493 cur->total_quota_out = env->out_quota[c];
494 cur->desc = net_str[c];
495 GNUNET_asprintf (&cur->stat_total,
496 "# ATS addresses %s total", cur->desc);
497 GNUNET_asprintf (&cur->stat_active,
498 "# ATS active addresses %s total", cur->desc);
499 LOG (GNUNET_ERROR_TYPE_INFO, "Added network %u `%s' (%llu/%llu)\n",
500 c, cur->desc, cur->total_quota_in, cur->total_quota_out);
506 libgnunet_plugin_ats_proportional_done (void *cls)
508 struct GAS_PROPORTIONAL_Handle *s = cls;
509 struct AddressWrapper *cur;
510 struct AddressWrapper *next;
512 GNUNET_assert(s != NULL);
513 for (c = 0; c < s->network_count; c++)
515 if (s->network_entries[c].total_addresses > 0)
517 LOG(GNUNET_ERROR_TYPE_DEBUG,
518 "Had %u addresses for network `%s' not deleted during shutdown\n",
519 s->network_entries[c].total_addresses, s->network_entries[c].desc);
523 if (s->network_entries[c].active_addresses > 0)
525 LOG(GNUNET_ERROR_TYPE_DEBUG,
526 "Had %u active addresses for network `%s' not deleted during shutdown\n",
527 s->network_entries[c].active_addresses, s->network_entries[c].desc);
531 next = s->network_entries[c].head;
532 while (NULL != (cur = next))
535 GNUNET_CONTAINER_DLL_remove(s->network_entries[c].head,
536 s->network_entries[c].tail, cur);
537 GNUNET_free_non_null (cur->addr->solver_information);
540 GNUNET_free(s->network_entries[c].stat_total);
541 GNUNET_free(s->network_entries[c].stat_active);
543 if (s->total_addresses > 0)
545 LOG(GNUNET_ERROR_TYPE_DEBUG,
546 "Had %u addresses not deleted during shutdown\n", s->total_addresses);
549 if (s->active_addresses > 0)
551 LOG(GNUNET_ERROR_TYPE_DEBUG,
552 "Had %u active addresses not deleted during shutdown\n",
553 s->active_addresses);
556 GNUNET_free (s->network_entries);
557 GNUNET_CONTAINER_multipeermap_destroy (s->requests);
564 * Test if bandwidth is available in this network to add an additional address
566 * @param net the network type to update
567 * @return GNUNET_YES or GNUNET_NO
570 is_bandwidth_available_in_network (struct Network *net)
572 GNUNET_assert(NULL != net);
573 unsigned int na = net->active_addresses + 1;
574 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
575 if (((net->total_quota_in / na) > min_bw)
576 && ((net->total_quota_out / na) > min_bw))
578 LOG(GNUNET_ERROR_TYPE_DEBUG,
579 "Enough bandwidth available for %u active addresses in network `%s'\n",
584 LOG(GNUNET_ERROR_TYPE_DEBUG,
585 "Not enough bandwidth available for %u active addresses in network `%s'\n",
591 * Update bandwidth assigned to peers in this network
593 * @param s the solver handle
594 * @param net the network type to update
598 distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
601 struct AddressSolverInformation *asi;
602 struct AddressWrapper *cur_address;
604 unsigned long long remaining_quota_in = 0;
605 unsigned long long quota_out_used = 0;
606 unsigned long long remaining_quota_out = 0;
607 unsigned long long quota_in_used = 0;
609 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
610 double relative_peer_prefence;
611 double sum_relative_peer_prefences; /* Important: has to be double not float due to precision */
612 double cur_pref; /* Important: has to be double not float due to precision */
615 const double *peer_relative_prefs = NULL; /* Important: has to be double not float due to precision */
617 unsigned long long assigned_quota_in = 0;
618 unsigned long long assigned_quota_out = 0;
621 LOG(GNUNET_ERROR_TYPE_INFO,
622 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
623 net->desc, net->active_addresses, net->total_quota_in,
624 net->total_quota_in);
626 if (net->active_addresses == 0)
628 return; /* no addresses to update */
632 * Assign every peer in network minimum Bandwidth
633 * Distribute bandwidth left according to preference
636 if ((net->active_addresses * min_bw) > net->total_quota_in)
641 if ((net->active_addresses * min_bw) > net->total_quota_out)
647 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
648 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
649 LOG(GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
650 remaining_quota_in, remaining_quota_out);
651 sum_relative_peer_prefences = 0.0;
653 /* Calculate sum of relative preference for active addresses in this network */
655 for (cur_address = net->head; NULL != cur_address; cur_address = cur_address->next)
657 if (GNUNET_YES != cur_address->addr->active)
660 GNUNET_assert( NULL != (peer_relative_prefs = s->get_preferences (s->get_preferences_cls,
661 &cur_address->addr->peer)));
662 relative_peer_prefence = 0.0;
663 relative_peer_prefence += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
664 sum_relative_peer_prefences += relative_peer_prefence;
668 GNUNET_assert (count_addresses == net->active_addresses);
670 LOG (GNUNET_ERROR_TYPE_INFO,
671 "Total relative preference %.3f for %u addresses in network %s\n",
672 sum_relative_peer_prefences, net->active_addresses, net->desc);
674 for (cur_address = net->head; NULL != cur_address; cur_address = cur_address->next)
676 if (GNUNET_YES == cur_address->addr->active)
678 GNUNET_assert( NULL != (peer_relative_prefs =
679 s->get_preferences (s->get_preferences_cls, &cur_address->addr->peer)));
681 cur_pref = peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
682 total_weight = net->active_addresses +
683 s->prop_factor * sum_relative_peer_prefences;
684 peer_weight = (1.0 + (s->prop_factor * cur_pref));
686 assigned_quota_in = min_bw
687 + ((peer_weight / total_weight) * remaining_quota_in);
688 assigned_quota_out = min_bw
689 + ((peer_weight / total_weight) * remaining_quota_out);
691 LOG (GNUNET_ERROR_TYPE_INFO,
692 "New quota for peer `%s' with weight (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
693 GNUNET_i2s (&cur_address->addr->peer), peer_weight, total_weight,
694 assigned_quota_in, assigned_quota_out);
698 assigned_quota_in = 0;
699 assigned_quota_out = 0;
702 quota_in_used += assigned_quota_in;
703 quota_out_used += assigned_quota_out;
704 /* Prevent overflow due to rounding errors */
705 if (assigned_quota_in > UINT32_MAX)
706 assigned_quota_in = UINT32_MAX;
707 if (assigned_quota_out > UINT32_MAX)
708 assigned_quota_out = UINT32_MAX;
710 /* Compare to current bandwidth assigned */
711 asi = cur_address->addr->solver_information;
712 asi->calculated_quota_in_NBO = htonl (assigned_quota_in);
713 asi->calculated_quota_out_NBO = htonl (assigned_quota_out);
715 LOG(GNUNET_ERROR_TYPE_DEBUG,
716 "Total bandwidth assigned is (in/out): %llu /%llu\n", quota_in_used,
718 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
720 LOG(GNUNET_ERROR_TYPE_ERROR,
721 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
722 net->active_addresses, quota_out_used, net->total_quota_out);
724 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
726 LOG(GNUNET_ERROR_TYPE_ERROR,
727 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
728 net->active_addresses, quota_in_used, net->total_quota_in);
732 struct FindBestAddressCtx
734 struct GAS_PROPORTIONAL_Handle *s;
735 struct ATS_Address *best;
739 find_property_index (uint32_t type)
741 int existing_types[] = GNUNET_ATS_QualityProperties;
743 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
744 if (existing_types[c] == type)
746 return GNUNET_SYSERR;
750 * Find a "good" address to use for a peer by iterating over the addresses for this peer.
751 * If we already have an existing address, we stick to it.
752 * Otherwise, we pick by lowest distance and then by lowest latency.
754 * @param cls the 'struct ATS_Address**' where we store the result
756 * @param value another 'struct ATS_Address*' to consider using
757 * @return GNUNET_OK (continue to iterate)
760 find_best_address_it (void *cls,
761 const struct GNUNET_PeerIdentity *key,
764 struct FindBestAddressCtx *ctx = (struct FindBestAddressCtx *) cls;
765 struct ATS_Address *current = (struct ATS_Address *) value;
766 struct ATS_Address *current_best = (struct ATS_Address *) value;
767 struct GNUNET_TIME_Absolute now;
768 struct AddressSolverInformation *asi;
769 struct GNUNET_TIME_Relative active_time;
770 struct GNUNET_TIME_Relative min_active_time;
771 const double *norm_prop_cur;
772 const double *norm_prop_best;
774 double best_distance;
780 asi = current->solver_information;
781 now = GNUNET_TIME_absolute_get ();
783 if ((current->active == GNUNET_NO)
784 && (current->blocked_until.abs_value_us
785 == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value_us))
787 /* This address is blocked for suggestion */
788 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address %p blocked for suggestion for %s \n",
790 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (now, current->blocked_until), GNUNET_YES));
799 if (GNUNET_NO == is_bandwidth_available_in_network (asi->network))
801 return GNUNET_OK; /* There's no bandwidth available in this network */
804 if (NULL != ctx->best)
806 /* Compare current addresses with denominated 'best' address */
807 current_best = ctx->best;
811 /* We do not have a 'best' address so take this address */
812 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting initial address %p\n", current);
813 current_best = current;
817 if (GNUNET_YES == current->active)
819 GNUNET_assert (asi->activated.abs_value_us != GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us);
820 active_time = GNUNET_TIME_absolute_get_duration (asi->activated);
821 min_active_time.rel_value_us = ((double) GNUNET_TIME_UNIT_SECONDS.rel_value_us) *
822 PROP_STABILITY_FACTOR;
823 if (active_time.rel_value_us <= min_active_time.rel_value_us)
825 /* Keep active address for stability reasons */
831 /* Now compare ATS information */
832 norm_prop_cur = ctx->s->get_properties (ctx->s->get_properties_cls,
833 (const struct ATS_Address *) current);
834 index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
835 cur_distance = norm_prop_cur[index];
836 index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
837 cur_delay = norm_prop_cur[index];
839 norm_prop_best = ctx->s->get_properties (ctx->s->get_properties_cls,
840 (const struct ATS_Address *) ctx->best);
841 index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
842 best_distance = norm_prop_best[index];
843 index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
844 best_delay = norm_prop_best[index];
846 /* user shorter distance */
847 if (cur_distance < best_distance)
850 if (GNUNET_NO == ctx->best->active)
852 current_best = current; /* Use current */
854 else if ((best_distance / cur_distance) > ctx->s->stability_factor)
856 /* Best and active address performs worse */
857 current_best = current;
862 /* Use current best */
863 current_best = ctx->best;
866 /* User connection with less delay */
867 if (cur_delay < best_delay)
870 if (GNUNET_NO == ctx->best->active)
872 current_best = current; /* Use current */
874 else if ((best_delay / cur_delay) > ctx->s->stability_factor)
876 /* Best and active address performs worse */
877 current_best = current;
882 /* Use current best */
883 current_best = ctx->best;
887 ctx->best = current_best;
892 get_best_address (struct GAS_PROPORTIONAL_Handle *s,
893 struct GNUNET_CONTAINER_MultiPeerMap *addresses,
894 const struct GNUNET_PeerIdentity *id)
896 struct FindBestAddressCtx fba_ctx;
900 GNUNET_CONTAINER_multipeermap_get_multiple (addresses, id,
901 &find_best_address_it, &fba_ctx);
907 * Hashmap Iterator to find current active address for peer
909 * @param cls last active address
910 * @param key peer's key
911 * @param value address to check
912 * @return #GNUNET_NO on double active address else #GNUNET_YES;
915 get_active_address_it (void *cls,
916 const struct GNUNET_PeerIdentity *key,
919 struct ATS_Address **dest = cls;
920 struct ATS_Address *aa = (struct ATS_Address *) value;
922 if (GNUNET_YES == aa->active)
927 /* should never happen */
928 LOG(GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n",
929 GNUNET_i2s (&aa->peer));
939 * Find current active address for peer
941 * @param solver the solver handle
942 * @param addresses the address set
943 * @param peer the peer
944 * @return active address or NULL
946 static struct ATS_Address *
947 get_active_address (void *solver,
948 const struct GNUNET_CONTAINER_MultiPeerMap * addresses,
949 const struct GNUNET_PeerIdentity *peer)
951 struct ATS_Address * dest = NULL;
953 GNUNET_CONTAINER_multipeermap_get_multiple (addresses, peer,
954 &get_active_address_it, &dest);
960 * Lookup network struct by type
962 * @param s the solver handle
963 * @param type the network type
964 * @return the network struct
966 static struct Network *
967 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
970 for (c = 0; c < s->network_count; c++)
972 if (s->network_entries[c].type == type)
973 return &s->network_entries[c];
980 addresse_increment (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
981 int total, int active)
983 if (GNUNET_YES == total)
985 s->total_addresses++;
986 net->total_addresses++;
987 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
988 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
990 if (GNUNET_YES == active)
992 net->active_addresses++;
993 s->active_addresses++;
994 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1,
996 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
1003 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
1004 int total, int active)
1006 int res = GNUNET_OK;
1007 if (GNUNET_YES == total)
1009 if (s->total_addresses < 1)
1012 res = GNUNET_SYSERR;
1016 s->total_addresses--;
1017 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
1020 if (net->total_addresses < 1)
1023 res = GNUNET_SYSERR;
1027 net->total_addresses--;
1028 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
1032 if (GNUNET_YES == active)
1034 if (net->active_addresses < 1)
1037 res = GNUNET_SYSERR;
1041 net->active_addresses--;
1042 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
1044 if (s->active_addresses < 1)
1047 res = GNUNET_SYSERR;
1051 s->active_addresses--;
1052 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
1060 address_eq (struct ATS_Address *a, struct ATS_Address *b)
1062 GNUNET_assert (NULL != a);
1063 GNUNET_assert (NULL != b);
1064 if (0 != strcmp(a->plugin, b->plugin))
1066 if (a->addr_len != b->addr_len)
1068 if (0 != memcmp (a->addr, b->addr, b->addr_len))
1070 if (a->session_id != b->session_id)
1077 * ---------------------------
1080 propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
1081 struct Network *net)
1083 struct AddressWrapper *cur;
1084 struct AddressSolverInformation *asi;
1085 for (cur = net->head; NULL != cur; cur = cur->next)
1087 asi = cur->addr->solver_information;
1088 if ( (cur->addr->assigned_bw_in.value__ != asi->calculated_quota_in_NBO) ||
1089 (cur->addr->assigned_bw_out.value__ != asi->calculated_quota_out_NBO) )
1091 cur->addr->assigned_bw_in.value__ = asi->calculated_quota_in_NBO;
1092 cur->addr->assigned_bw_out.value__ = asi->calculated_quota_out_NBO;
1094 /* Reset for next iteration */
1095 asi->calculated_quota_in_NBO = htonl (0);
1096 asi->calculated_quota_out_NBO = htonl (0);
1098 LOG (GNUNET_ERROR_TYPE_DEBUG,
1099 "Bandwidth for %s address %p for peer `%s' changed to %u/%u\n",
1100 (GNUNET_NO == cur->addr->active) ? "inactive" : "active",
1102 GNUNET_i2s (&cur->addr->peer),
1103 ntohl (cur->addr->assigned_bw_in.value__),
1104 ntohl (cur->addr->assigned_bw_out.value__ ));
1106 /* Notify on change */
1107 if ((GNUNET_YES == cur->addr->active))
1109 s->bw_changed (s->bw_changed_cls, cur->addr);
1116 * Distribibute bandwidth
1118 * @param s the solver handle
1119 * @param n the network, can be NULL for all network
1120 * @param address_except do not notify for this address
1123 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
1126 if (GNUNET_YES == s->bulk_lock)
1134 LOG (GNUNET_ERROR_TYPE_INFO,
1135 "Redistributing bandwidth in network %s with %u active and %u total addresses\n",
1136 GNUNET_ATS_print_network_type(n->type),
1137 n->active_addresses, n->total_addresses);
1139 if (NULL != s->env->info_cb)
1140 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_START,
1141 GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
1144 distribute_bandwidth(s, n);
1146 if (NULL != s->env->info_cb)
1147 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_STOP,
1148 GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
1149 if (NULL != s->env->info_cb)
1150 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
1151 GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
1153 /* Do propagation */
1154 propagate_bandwidth (s, n);
1156 if (NULL != s->env->info_cb)
1157 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
1158 GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
1163 if (NULL != s->env->info_cb)
1164 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_START,
1165 GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
1166 for (i = 0; i < s->network_count; i++)
1169 distribute_bandwidth(s, &s->network_entries[i]);
1172 if (NULL != s->env->info_cb)
1173 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_STOP,
1174 GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
1175 if (NULL != s->env->info_cb)
1176 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
1177 GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
1178 for (i = 0; i < s->network_count; i++)
1180 /* Do propagation */
1181 propagate_bandwidth(s, &s->network_entries[i]);
1183 if (NULL != s->env->info_cb)
1184 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
1185 GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
1189 static const struct ATS_Address *
1190 update_active_address (struct GAS_PROPORTIONAL_Handle *s,
1191 const struct GNUNET_PeerIdentity *peer)
1193 struct ATS_Address *best_address;
1194 struct ATS_Address *current_address;
1195 struct AddressSolverInformation *asi;
1196 struct Network *net;
1198 LOG (GNUNET_ERROR_TYPE_INFO, "Updating active address for peer `%s'\n",
1201 /* Find active address */
1202 current_address = get_active_address (s, s->addresses, peer);
1203 /* Find best address */
1204 best_address = get_best_address (s,s->addresses, peer);
1206 if (NULL != current_address)
1208 if ((NULL == best_address) || ((NULL != best_address) && (GNUNET_NO == address_eq (current_address, best_address))))
1210 /* We switch to a new address, mark old address as inactive */
1211 LOG (GNUNET_ERROR_TYPE_INFO,
1212 "Disabling previous %s address %p for peer `%s'\n",
1213 (GNUNET_NO == current_address->active) ? "inactive" : "active",
1217 asi = current_address->solver_information;
1218 GNUNET_assert (NULL != asi);
1221 asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
1222 current_address->active = GNUNET_NO; /* No active any longer */
1223 current_address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1224 current_address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1226 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1229 /* Update network of previous address */
1230 distribute_bandwidth_in_network (s, net);
1232 if (NULL == best_address)
1234 /* We previously had an active address, but now we cannot suggest one
1235 * Therefore we have to disconnect the peer */
1237 LOG (GNUNET_ERROR_TYPE_INFO,
1238 "Disconnecting peer `%s' with previous %s address %p\n",
1240 (GNUNET_NO == current_address->active) ? "inactive" : "active",
1242 s->bw_changed (s->bw_changed_cls, current_address);
1246 if (NULL == best_address)
1248 LOG (GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1253 LOG (GNUNET_ERROR_TYPE_INFO, "Suggesting %s address %p for peer `%s'\n",
1254 (GNUNET_NO == best_address->active) ? "inactive" : "active", best_address,
1257 if ((NULL != current_address) &&
1258 (GNUNET_YES == address_eq (best_address, current_address)))
1260 if (current_address->active == GNUNET_NO)
1261 GNUNET_break (0); /* Should never happen */
1262 return best_address; /* Same same */
1265 asi = best_address->solver_information;
1266 GNUNET_assert (NULL != asi);
1269 /* Mark address as active */
1270 asi->activated = GNUNET_TIME_absolute_get();
1271 best_address->active = GNUNET_YES;
1272 addresse_increment (s, net, GNUNET_NO, GNUNET_YES);
1274 /* Distribute bandwidth */
1275 distribute_bandwidth_in_network (s, net);
1276 return best_address;
1280 * Solver API functions
1281 * ---------------------------
1285 * Changes the preferences for a peer in the problem
1287 * @param solver the solver handle
1288 * @param peer the peer to change the preference for
1289 * @param kind the kind to change the preference
1290 * @param pref_rel the normalized preference value for this kind over all clients
1293 GAS_proportional_address_change_preference (void *solver,
1294 const struct GNUNET_PeerIdentity *peer,
1295 enum GNUNET_ATS_PreferenceKind kind,
1298 struct GAS_PROPORTIONAL_Handle *s = solver;
1299 struct ATS_Address *best_address;
1300 struct ATS_Address *active_address;
1301 struct AddressSolverInformation *asi;
1303 GNUNET_assert(NULL != solver);
1304 GNUNET_assert(NULL != peer);
1306 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
1307 return; /* Peer is not requested */
1309 /* This peer is requested, find best address */
1310 active_address = get_active_address(s, s->addresses, peer);
1311 best_address = (struct ATS_Address *) update_active_address (s, peer);
1313 if ((NULL != best_address) && ((NULL != active_address) &&
1314 (GNUNET_YES == address_eq (active_address, best_address))))
1316 asi = best_address->solver_information;
1317 GNUNET_assert (NULL != asi);
1319 /* We sticked to the same address, therefore redistribute */
1320 distribute_bandwidth_in_network (s, asi->network);
1326 * Get application feedback for a peer
1328 * @param solver the solver handle
1329 * @param application the application
1330 * @param peer the peer to change the preference for
1331 * @param scope the time interval for this feedback: [now - scope .. now]
1332 * @param kind the kind to change the preference
1333 * @param score the score
1336 GAS_proportional_address_preference_feedback (void *solver, void *application,
1337 const struct GNUNET_PeerIdentity *peer,
1338 const struct GNUNET_TIME_Relative scope,
1339 enum GNUNET_ATS_PreferenceKind kind, double score)
1341 struct GAS_PROPORTIONAL_Handle *s = solver;
1342 GNUNET_assert(NULL != solver);
1343 GNUNET_assert(NULL != peer);
1345 GNUNET_assert(NULL != s);
1350 * Get the preferred address for a specific peer
1354 * @param solver the solver handle
1355 * @param peer the identity of the peer
1357 const struct ATS_Address *
1358 GAS_proportional_get_preferred_address (void *solver,
1359 const struct GNUNET_PeerIdentity *peer)
1361 struct GAS_PROPORTIONAL_Handle *s = solver;
1362 const struct ATS_Address *best_address;
1365 GNUNET_assert(s != NULL);
1366 GNUNET_assert(peer != NULL);
1368 /* Add to list of pending requests */
1369 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
1372 GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (s->requests, peer, NULL,
1373 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1374 LOG (GNUNET_ERROR_TYPE_INFO, "Start suggesting addresses for peer `%s'\n",
1378 best_address = update_active_address (s, peer);
1379 if (s->bulk_lock > 0)
1380 return NULL; /* Do not suggest since bulk lock is pending */
1382 return best_address;
1386 * Stop notifying about address and bandwidth changes for this peer
1388 * @param solver the solver handle
1389 * @param peer the peer
1392 GAS_proportional_stop_get_preferred_address (void *solver,
1393 const struct GNUNET_PeerIdentity *peer)
1395 struct GAS_PROPORTIONAL_Handle *s = solver;
1396 struct ATS_Address *cur;
1397 struct AddressSolverInformation *asi;
1398 struct Network *cur_net;
1400 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
1401 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (s->requests,
1404 cur = get_active_address (s, s->addresses, peer);
1407 LOG(GNUNET_ERROR_TYPE_INFO,
1408 "Disabling %s address %p for peer `%s'\n",
1409 (GNUNET_NO == cur->active) ? "inactive" : "active", cur,
1410 GNUNET_i2s (&cur->peer));
1412 /* Disabling current address */
1413 asi = cur->solver_information;
1414 cur_net = asi->network ;
1415 asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
1416 cur->active = GNUNET_NO; /* No active any longer */
1417 cur->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1418 cur->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1420 if (GNUNET_SYSERR == addresse_decrement (s, cur_net, GNUNET_NO, GNUNET_YES))
1423 distribute_bandwidth_in_network (s, cur_net );
1429 * Remove an address from the solver
1431 * @param solver the solver handle
1432 * @param address the address to remove
1433 * @param session_only delete only session not whole address
1436 GAS_proportional_address_delete (void *solver, struct ATS_Address *address,
1439 struct GAS_PROPORTIONAL_Handle *s = solver;
1440 struct Network *net;
1441 struct AddressWrapper *aw;
1442 struct AddressSolverInformation *asi;
1444 /* Remove an adress completely, we have to:
1445 * - Remove from specific network
1446 * - Decrease number of total addresses
1448 * - decrease number of active addreses
1451 asi = address->solver_information;
1460 if (GNUNET_NO == session_only)
1462 LOG(GNUNET_ERROR_TYPE_INFO,
1463 "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1464 (GNUNET_NO == address->active) ? "inactive" : "active", address,
1465 GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1466 net->active_addresses);
1468 /* Remove address */
1469 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1470 for (aw = net->head; NULL != aw; aw = aw->next)
1472 if (aw->addr == address)
1480 GNUNET_CONTAINER_DLL_remove(net->head, net->tail, aw);
1485 /* Remove session only: remove if active and update */
1486 LOG(GNUNET_ERROR_TYPE_INFO,
1487 "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1488 (GNUNET_NO == address->active) ? "inactive" : "active", address,
1489 GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1490 net->active_addresses);
1493 if (GNUNET_YES == address->active)
1495 /* Address was active, remove from network and update quotas*/
1496 address->active = GNUNET_NO;
1497 address->assigned_bw_in = BANDWIDTH_ZERO;
1498 address->assigned_bw_out = BANDWIDTH_ZERO;
1499 asi->calculated_quota_in_NBO = htonl (0);
1500 asi->calculated_quota_out_NBO = htonl (0);
1502 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1504 distribute_bandwidth_in_network (s, net);
1506 if (NULL == update_active_address (s, &address->peer))
1508 /* No alternative address found, disconnect peer */
1509 LOG (GNUNET_ERROR_TYPE_INFO,
1510 "Disconnecting peer `%s' after deleting previous %s address %p\n",
1511 GNUNET_i2s (&address->peer),
1512 (GNUNET_NO == address->active) ? "inactive" : "active",
1514 s->bw_changed (s->bw_changed_cls, address);
1517 if (GNUNET_NO == session_only)
1519 GNUNET_free_non_null (address->solver_information);
1520 address->solver_information = NULL;
1523 LOG(GNUNET_ERROR_TYPE_INFO,
1524 "After deleting address now total %u and active %u addresses in network `%s'\n",
1525 net->total_addresses, net->active_addresses, net->desc);
1530 * Start a bulk operation
1532 * @param solver the solver
1535 GAS_proportional_bulk_start (void *solver)
1537 LOG(GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1538 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1540 GNUNET_assert(NULL != solver);
1546 * Bulk operation done
1549 GAS_proportional_bulk_stop (void *solver)
1551 LOG(GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1553 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1554 GNUNET_assert(NULL != solver);
1556 if (s->bulk_lock < 1)
1562 if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1564 LOG(GNUNET_ERROR_TYPE_INFO, "No lock pending, recalculating\n");
1565 distribute_bandwidth_in_network (s, NULL);
1566 s->bulk_requests = 0;
1572 * Add a new single address to a network
1574 * @param solver the solver Handle
1575 * @param address the address to add
1576 * @param network network type of this address
1579 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1583 * Transport properties for this address have changed
1585 * @param solver solver handle
1586 * @param address the address
1587 * @param type the ATSI type in HBO
1588 * @param abs_value the absolute value of the property
1589 * @param rel_value the normalized value
1592 GAS_proportional_address_property_changed (void *solver,
1593 struct ATS_Address *address, uint32_t type, uint32_t abs_value,
1596 struct GAS_PROPORTIONAL_Handle *s;
1598 struct AddressSolverInformation *asi;
1599 struct ATS_Address *best_address;
1600 struct ATS_Address *active_address;
1602 GNUNET_assert(NULL != solver);
1603 GNUNET_assert(NULL != address);
1605 s = (struct GAS_PROPORTIONAL_Handle *) solver;
1606 asi = address->solver_information;
1620 LOG(GNUNET_ERROR_TYPE_INFO,
1621 "Property `%s' for peer `%s' address %p changed to %.2f \n",
1622 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1623 address, rel_value);
1625 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1626 return; /* Peer is not requested */
1628 /* This peer is requested, find active and best address */
1629 active_address = get_active_address(s, s->addresses, &address->peer);
1630 best_address = (struct ATS_Address *) update_active_address (s, &address->peer);
1632 if ((NULL != best_address) && ((NULL != active_address) &&
1633 (GNUNET_YES == address_eq (active_address, best_address))))
1635 asi = best_address->solver_information;
1636 GNUNET_assert (NULL != asi);
1638 /* We sticked to the same address, therefore redistribute */
1639 distribute_bandwidth_in_network (s, asi->network);
1644 * Transport session for this address has changed
1646 * NOTE: values in addresses are already updated
1648 * @param solver solver handle
1649 * @param address the address
1650 * @param cur_session the current session
1651 * @param new_session the new session
1654 GAS_proportional_address_session_changed (void *solver,
1655 struct ATS_Address *address, uint32_t cur_session, uint32_t new_session)
1657 struct GAS_PROPORTIONAL_Handle *s = solver;
1658 struct ATS_Address *best_address;
1659 struct ATS_Address *active_address;
1660 struct AddressSolverInformation *asi;
1662 s = (struct GAS_PROPORTIONAL_Handle *) solver;
1663 if (cur_session != new_session)
1665 LOG(GNUNET_ERROR_TYPE_DEBUG, "Session changed from %u to %u\n", cur_session,
1669 if (NULL == address->solver_information)
1675 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1676 return; /* Peer is not requested */
1678 /* This peer is requested, find active and best address */
1679 active_address = get_active_address(s, s->addresses, &address->peer);
1680 best_address = (struct ATS_Address *) update_active_address (s, &address->peer);
1682 if ((NULL != best_address) && ((NULL != active_address) &&
1683 (GNUNET_YES == address_eq (active_address, best_address))))
1685 asi = best_address->solver_information;
1686 GNUNET_assert (NULL != asi);
1688 /* We sticked to the same address, therefore redistribute */
1689 distribute_bandwidth_in_network (s, asi->network);
1694 * Usage for this address has changed
1696 * NOTE: values in addresses are already updated
1698 * @param solver solver handle
1699 * @param address the address
1700 * @param in_use usage state
1703 GAS_proportional_address_inuse_changed (void *solver,
1704 struct ATS_Address *address, int in_use)
1706 struct GAS_PROPORTIONAL_Handle *s;
1708 struct AddressSolverInformation *asi;
1709 struct ATS_Address *best_address;
1710 struct ATS_Address *active_address;
1712 GNUNET_assert(NULL != solver);
1713 GNUNET_assert(NULL != address);
1715 s = (struct GAS_PROPORTIONAL_Handle *) solver;
1716 asi = address->solver_information;
1730 LOG(GNUNET_ERROR_TYPE_INFO,
1731 "Usage of peer `%s' address %p changed to %s \n",
1732 GNUNET_i2s (&address->peer), address,
1733 (GNUNET_YES == in_use) ? "YES" : "NO");
1735 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1736 return; /* Peer is not requested */
1738 /* This peer is requested, find active and best address */
1739 active_address = get_active_address(s, s->addresses, &address->peer);
1740 best_address = (struct ATS_Address *) update_active_address (s, &address->peer);
1742 if ((NULL != best_address) && ((NULL != active_address) &&
1743 (GNUNET_YES == address_eq (active_address, best_address))))
1745 asi = best_address->solver_information;
1746 GNUNET_assert (NULL != asi);
1748 /* We sticked to the same address, therefore redistribute */
1749 distribute_bandwidth_in_network (s, asi->network);
1754 * Network scope for this address has changed
1756 * NOTE: values in addresses are already updated
1758 * @param solver solver handle
1759 * @param address the address
1760 * @param current_network the current network
1761 * @param new_network the new network
1764 GAS_proportional_address_change_network (void *solver,
1765 struct ATS_Address *address, uint32_t current_network, uint32_t new_network)
1767 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1768 struct AddressSolverInformation *asi;
1769 int save_active = GNUNET_NO;
1771 struct Network *new_net = NULL;
1773 if (current_network == new_network)
1779 asi = address->solver_information;
1786 /* Network changed */
1787 LOG(GNUNET_ERROR_TYPE_DEBUG,
1788 "Network type changed, moving %s address from `%s' to `%s'\n",
1789 (GNUNET_YES == address->active) ? "active" : "inactive",
1790 GNUNET_ATS_print_network_type (current_network),
1791 GNUNET_ATS_print_network_type (new_network));
1794 /* Start bulk to prevent disconnect */
1795 GAS_proportional_bulk_start(s);
1797 save_active = address->active;
1799 /* Disable and assign no bandwidth */
1800 address->active = GNUNET_NO;
1801 address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1802 address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1804 /* Remove from old network */
1805 GAS_proportional_address_delete (solver, address, GNUNET_NO);
1807 /* Set new network type */
1808 if (NULL == (new_net = get_network (solver, new_network)))
1810 /* Address changed to invalid network... */
1811 LOG(GNUNET_ERROR_TYPE_ERROR,
1812 _("Invalid network type `%u' `%s': Disconnect!\n"), new_network,
1813 GNUNET_ATS_print_network_type (new_network));
1814 s->bw_changed (s->bw_changed_cls, address);
1818 /* Add to new network and update*/
1819 GAS_proportional_address_add (solver, address, new_network);
1821 GAS_proportional_bulk_stop (s);
1823 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1824 return; /* Peer is not requested */
1826 /* Find new address to suggest */
1827 if (GNUNET_YES == save_active)
1829 /* No address available, therefore disconnect */
1830 if (NULL == update_active_address (s, &address->peer))
1831 s->bw_changed (s->bw_changed_cls, address);
1837 * Add a new single address to a network
1839 * @param solver the solver Handle
1840 * @param address the address to add
1841 * @param network network type of this address
1844 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1847 struct GAS_PROPORTIONAL_Handle *s = solver;
1848 struct Network *net = NULL;
1849 struct AddressWrapper *aw = NULL;
1850 struct AddressSolverInformation *asi;
1852 GNUNET_assert(NULL != s);
1853 net = get_network (s, network);
1858 LOG(GNUNET_ERROR_TYPE_ERROR,
1859 "Unknown network %u `%s' for new address %p for peer `%s'\n",
1860 network, GNUNET_ATS_print_network_type(network),
1861 address, GNUNET_i2s(&address->peer));
1866 aw = GNUNET_new (struct AddressWrapper);
1868 GNUNET_CONTAINER_DLL_insert(net->head, net->tail, aw);
1869 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1871 asi = GNUNET_new (struct AddressSolverInformation);
1873 asi->calculated_quota_in_NBO = htonl (0);
1874 asi->calculated_quota_out_NBO = htonl (0);
1875 aw->addr->solver_information = asi;
1877 LOG(GNUNET_ERROR_TYPE_INFO,
1878 "Adding new address %p for peer `%s', now total %u and active %u addresses in network `%s'\n",
1879 address, GNUNET_i2s(&address->peer), net->total_addresses, net->active_addresses, net->desc);
1881 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1882 return; /* Peer is not requested */
1884 /* This peer is requested, find best address */
1885 update_active_address (s, &address->peer);
1889 /* end of plugin_ats_proportional.c */