2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 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
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_ats_plugin.h"
30 #include "gnunet_ats_service.h"
31 #include "gnunet-service-ats_addresses.h"
33 #define PROP_STABILITY_FACTOR 1.25
36 #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
41 * NOTE: Do not change this documentation. This documentation is based
42 * on gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
43 * use build_txt.sh to generate plaintext output
45 * ATS addresses : proportional solver
47 * The proportional solver ("proportional") distributes the available
48 * bandwidth fair over all the addresses influenced by the
49 * preference values. For each available network type an in- and
50 * outbound quota is configured and the bandwidth available in
51 * these networks is distributed over the addresses. The solver
52 * first assigns every addresses the minimum amount of bandwidth
53 * #GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
54 * remaining bandwidth available according to the preference
55 * values. For each peer only a single address gets bandwidth
56 * assigned and only one address marked as active. The most
57 * important functionality for the solver is implemented in: *
58 * find_address_it is an hashmap iterator returning the prefered
59 * address for an peer * update_quota_per_network distributes
60 * available bandwidth for a network over active addresses
62 * Changes to addresses automatically have an impact on the the
63 * bandwidth assigned to other addresses in the same network since
64 * the solver distributes the remaining bandwidth over the
65 * addresses in the network. When changes to the addresses occur,
66 * the solver first performs the changes, like adding or deleting
67 * addresses, and then updates bandwidth assignment for the
68 * affected network. Bandwidth assignment is only recalculated on
69 * demand when an address is requested by a client for a peer or
70 * when the addresses available have changed or an address changed
71 * the network it is located in. When the bandwidth assignment has
72 * changed the callback is called with the new bandwidth
73 * assignments. The bandwidth distribution for a network is
74 * recalculated due to: * address suggestion requests * address
75 * deletions * address switching networks during address update *
78 * 3.1 Data structures used
80 * For each ATS network (e.g. WAN, LAN, loopback) a struct Network
81 * is used to specify network related information as total adresses
82 * and active addresses in this network and the configured in- and
83 * outbound quota. Each network also contains a list of addresses
84 * added to the solver located in this network. The proportional
85 * solver uses the addresses' solver_information field to store the
86 * proportional network it belongs to for each address.
90 * When the proportional solver is initialized the solver creates a
91 * new solver handle and initializes the network structures with
92 * the quotas passed from addresses and returns the handle solver.
94 * 3.3 Adding an address
96 * When a new address is added to the solver using s_add, a lookup
97 * for the network for this address is done and the address is
98 * enqueued in in the linked list of the network.
100 * 3.4 Updating an address
102 * The main purpose of address updates is to update the ATS
103 * information for addresse selection. Important for the proportional
104 * solver is when an address switches network it is located
105 * in. This is common because addresses added by transport's
106 * validation mechanism are commonly located in
107 * #GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
108 * in this network type and only if a connection is successful on
109 * return of payload data transport switches to the real network
110 * the address is located in. When an address changes networks it
111 * is first of all removed from the old network using the solver
112 * API function #GAS_proportional_address_delete() and the network in
113 * the address struct is updated. A lookup for the respective new
114 * proportional network is done and stored in the addresse's
115 * solver_information field. Next the address is re-added to the
116 * solver using the solver API function
117 * #GAS_proportional_address_add(). If the address was marked as in
118 * active, the solver checks if bandwidth is available in the
119 * network and if yes sets the address to active and updates the
120 * bandwidth distribution in this network. If no bandwidth is
121 * available it sets the bandwidth for this address to 0 and tries
122 * to suggest an alternative address. If an alternative address was
123 * found, addresses' callback is called for this address.
125 * 3.5 Deleting an address
127 * When an address is removed from the solver, it removes the
128 * respective address from the network and if the address was
129 * marked as active, it updates the bandwidth distribution for this
132 * 3.6 Requesting addresses
134 * When an address is requested for a peer the solver performs a
135 * lookup for the peer entry in addresses address hashmap and
136 * selects the best address. The selection of the most suitable
137 * address is done in the find_address_it hashmap iterator
138 * described in detail in section 3.7. If no address is returned,
139 * no address can be suggested at the moment. If the address
140 * returned is marked as active, the solver can return this
141 * address. If the address is not marked as active, the solver
142 * checks if another address belongign to this peer is marked as
143 * active and marks the address as inactive, updates the bandwidth
144 * for this address to 0, call the bandwidth changed callback for
145 * this address due to the change and updates quota assignment for
146 * the addresse's network. the now in-active address is belonging
147 * to. The solver marks the new address as active and updates the
148 * bandwidth assignment for this network.
150 * 3.7 Choosing addresses
152 * Choosing the best possible address for suggestion is done by
153 * iterating over all addresses of a peer stored in addresses'
154 * hashmap and using the hashmap iterator find_address_it to select
155 * the best available address. Several checks are done when an
156 * address is selected. First if this address is currently blocked
157 * by addresses from being suggested. An address is blocked for the
158 * duration of #ATS_BLOCKING_DELTA when it is suggested to
159 * transport. Next it is checked if at least
160 * #GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
161 * in the addresse's network, because suggesting an address without
162 * bandwidth does not make sense. This also ensures that all active
163 * addresses in this network get at least the minimum amount of
164 * bandwidth assigned. In the next step the solver ensures that for
165 * tcp connections inbound connections are prefered over outbound
166 * connections. In the next stet the solver ensures that
167 * connections are prefered in the following order: * connections
168 * are already established and have bandwidth assigned *
169 * connections with a shorter distance * connectes have a shorter
172 * 3.8 Changing preferences
176 * During shutdown all network entries and aging processes are
177 * destroyed and freed.
182 * This solver assigns in and outbound bandwidth equally for all
183 * addresses in specific network type (WAN, LAN) based on configured
184 * in and outbound quota for this network.
186 * The solver is notified by addresses about changes to the addresses
187 * and recalculates the bandwith assigned if required. The solver
188 * notifies addresses by calling the GAS_bandwidth_changed_cb
196 * For each peer only a single is selected and marked as "active" in the address
201 * You have the networks WAN and LAN and quotas
202 * WAN_TOTAL_IN, WAN_TOTAL_OUT
203 * LAN_TOTAL_IN, LAN_TOTAL_OUT
205 * If you have x addresses in the network segment LAN, the quotas are
206 * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
208 * Quotas are automatically recalculated and reported back when addresses are
214 * Default value to assume for the proportionality factor,
215 * if none is given in the configuration.
217 #define PROPORTIONALITY_FACTOR 2.0
221 * Address information stored for the proportional solver in the
222 * `solver_information` member of `struct GNUNET_ATS_Address`.
224 * They are also stored in the respective `struct Network`'s linked
227 struct AddressWrapper
232 struct AddressWrapper *next;
237 struct AddressWrapper *prev;
242 struct ATS_Address *addr;
245 * Network scope this address is in
247 struct Network *network;
252 uint32_t calculated_quota_in;
257 uint32_t calculated_quota_out;
260 * When was this address activated
262 struct GNUNET_TIME_Absolute activated;
268 * Representation of a network
273 * Network description
278 * String for statistics total addresses
283 * String for statistics active addresses
288 * Linked list of addresses in this network: head
290 struct AddressWrapper *head;
293 * Linked list of addresses in this network: tail
295 struct AddressWrapper *tail;
298 * Total inbound quota
300 unsigned long long total_quota_in;
303 * Total outbound quota
305 unsigned long long total_quota_out;
310 enum GNUNET_ATS_Network_Type type;
313 * Number of active addresses for this network
315 unsigned int active_addresses;
318 * Number of total addresses for this network
320 unsigned int total_addresses;
326 * A handle for the proportional solver
328 struct GAS_PROPORTIONAL_Handle
332 * Our execution environment.
334 struct GNUNET_ATS_PluginEnvironment *env;
339 struct Network *network_entries;
342 * Proportionality factor
349 double stability_factor;
352 * Bulk lock counter. If zero, we are not locked.
354 unsigned int bulk_lock;
357 * Number of changes made while solver was locked. We really only
358 * use 0/non-zero to check on unlock if we have to run the update.
360 unsigned int bulk_requests;
363 * Number of active addresses for solver
365 unsigned int active_addresses;
368 * Number of networks in @a network_entries
370 unsigned int network_count;
375 * Test if bandwidth is available in this network to add an additional address.
377 * @param net the network type to check
378 * @param extra for how many extra addresses do we check?
379 * @return #GNUNET_YES or #GNUNET_NO
382 is_bandwidth_available_in_network (struct Network *net,
386 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
388 GNUNET_assert (net->active_addresses + extra >= 0);
389 na = net->active_addresses + extra;
392 if ( ((net->total_quota_in / na) > min_bw) &&
393 ((net->total_quota_out / na) > min_bw) )
400 * Test if all peers in this network require connectivity at level at
403 * @param s the solver handle
404 * @param net the network type to check
405 * @param con connection return value threshold to check
406 * @return #GNUNET_YES or #GNUNET_NO
409 all_require_connectivity (struct GAS_PROPORTIONAL_Handle *s,
413 struct AddressWrapper *aw;
415 for (aw = net->head; NULL != aw; aw = aw->next)
417 s->env->get_connectivity (s->env->cls,
425 * Update bandwidth assigned to peers in this network. The basic idea
426 * is to assign every peer in the network the minimum bandwidth, and
427 * then distribute the remaining bandwidth proportional to application
430 * @param s the solver handle
431 * @param net the network type to update
434 distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
437 const uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
438 struct AddressWrapper *aw;
439 unsigned long long remaining_quota_in;
440 unsigned long long quota_out_used;
441 unsigned long long remaining_quota_out;
442 unsigned long long quota_in_used;
443 unsigned int count_addresses;
444 double sum_relative_peer_prefences;
447 const double *peer_relative_prefs;
449 LOG (GNUNET_ERROR_TYPE_INFO,
450 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
452 net->active_addresses,
454 net->total_quota_in);
456 if (0 == net->active_addresses)
457 return; /* no addresses to update */
460 if ((net->active_addresses * min_bw) > net->total_quota_in)
465 if ((net->active_addresses * min_bw) > net->total_quota_out)
471 /* Calculate sum of relative preference for active addresses in this
473 sum_relative_peer_prefences = 0.0;
475 for (aw = net->head; NULL != aw; aw = aw->next)
477 if (GNUNET_YES != aw->addr->active)
479 peer_relative_prefs = s->env->get_preferences (s->env->cls,
481 sum_relative_peer_prefences += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
484 if (count_addresses != net->active_addresses)
487 LOG (GNUNET_ERROR_TYPE_WARNING,
488 "%s: Counted %u active addresses, expected %u active addresses\n",
491 net->active_addresses);
493 net->active_addresses = count_addresses;
495 LOG (GNUNET_ERROR_TYPE_INFO,
496 "Total relative preference %.3f for %u addresses in network %s\n",
497 sum_relative_peer_prefences,
498 net->active_addresses,
501 /* check how much we have to distribute */
502 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
503 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
504 LOG (GNUNET_ERROR_TYPE_DEBUG,
505 "Proportionally distributable bandwidth (in/out): %llu/%llu\n",
507 remaining_quota_out);
509 /* distribute remaining quota; we do not do it exactly proportional,
510 but balance "even" distribution ("net->active_addresses") with
511 the preference sum using the "prop_factor". */
512 total_weight = net->active_addresses +
513 s->prop_factor * sum_relative_peer_prefences;
516 for (aw = net->head; NULL != aw; aw = aw->next)
518 if (GNUNET_YES != aw->addr->active)
520 /* set to 0, just to be sure */
521 aw->calculated_quota_in = 0;
522 aw->calculated_quota_out = 0;
525 peer_relative_prefs = s->env->get_preferences (s->env->cls,
528 + s->prop_factor * peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
530 aw->calculated_quota_in = min_bw
531 + (peer_weight / total_weight) * remaining_quota_in;
532 aw->calculated_quota_out = min_bw
533 + (peer_weight / total_weight) * remaining_quota_out;
535 LOG (GNUNET_ERROR_TYPE_INFO,
536 "New quotas for peer `%s' with weight (cur/total) %.3f/%.3f (in/out) are: %u/%u\n",
537 GNUNET_i2s (&aw->addr->peer),
540 (unsigned int) aw->calculated_quota_in,
541 (unsigned int) aw->calculated_quota_out);
542 quota_in_used += aw->calculated_quota_in;
543 quota_out_used += aw->calculated_quota_out;
545 LOG (GNUNET_ERROR_TYPE_DEBUG,
546 "Total bandwidth assigned is (in/out): %llu /%llu\n",
549 /* +1 due to possible rounding errors */
550 GNUNET_break (quota_out_used <= net->total_quota_out + 1);
551 GNUNET_break (quota_in_used <= net->total_quota_in + 1);
556 * Notify ATS service of bandwidth changes to addresses.
558 * @param s solver handle
559 * @param net the network to propagate changes in
562 propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
565 struct AddressWrapper *cur;
567 for (cur = net->head; NULL != cur; cur = cur->next)
569 if ( (cur->addr->assigned_bw_in == cur->calculated_quota_in) &&
570 (cur->addr->assigned_bw_out == cur->calculated_quota_out) )
572 cur->addr->assigned_bw_in = cur->calculated_quota_in;
573 cur->addr->assigned_bw_out = cur->calculated_quota_out;
574 if (GNUNET_YES == cur->addr->active)
575 s->env->bandwidth_changed_cb (s->env->cls,
582 * Distribute bandwidth. The addresses have already been selected,
583 * this is merely distributed the bandwidth among the addresses.
585 * @param s the solver handle
586 * @param n the network, can be NULL for all networks
589 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
594 if (GNUNET_YES == s->bulk_lock)
601 LOG (GNUNET_ERROR_TYPE_DEBUG,
602 "Redistributing bandwidth in network %s with %u active and %u total addresses\n",
603 GNUNET_ATS_print_network_type(n->type),
606 s->env->info_cb (s->env->cls,
609 GAS_INFO_PROP_SINGLE);
610 distribute_bandwidth(s,
612 s->env->info_cb (s->env->cls,
615 GAS_INFO_PROP_SINGLE);
616 s->env->info_cb (s->env->cls,
617 GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
619 GAS_INFO_PROP_SINGLE);
620 propagate_bandwidth (s,
623 s->env->info_cb (s->env->cls,
624 GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
626 GAS_INFO_PROP_SINGLE);
630 s->env->info_cb (s->env->cls,
634 for (i = 0; i < s->network_count; i++)
635 distribute_bandwidth (s,
636 &s->network_entries[i]);
637 s->env->info_cb (s->env->cls,
641 s->env->info_cb (s->env->cls,
642 GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
645 for (i = 0; i < s->network_count; i++)
646 propagate_bandwidth (s,
647 &s->network_entries[i]);
648 s->env->info_cb (s->env->cls,
649 GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
657 * Context for finding the best address* Linked list of addresses in this network: head
659 struct FindBestAddressCtx
664 struct GAS_PROPORTIONAL_Handle *s;
667 * The currently best address
669 struct ATS_Address *best;
674 * Find index of a ATS property type in the quality properties array.
676 * @param type ATS property type
677 * @return index in the quality array, #GNUNET_SYSERR if the type
678 * was not a quality property
681 find_quality_property_index (enum GNUNET_ATS_Property type)
683 enum GNUNET_ATS_Property existing_types[] = GNUNET_ATS_QualityProperties;
686 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
687 if (existing_types[c] == type)
690 return GNUNET_SYSERR;
695 * Find a "good" address to use for a peer by iterating over the
696 * addresses for this peer. If we already have an existing address,
697 * we stick to it. Otherwise, we pick by lowest distance and then by
700 * @param cls the `struct FindBestAddressCtx *' where we store the result
701 * @param key the peer we are trying to find the best address for
702 * @param value another `struct ATS_Address*` to consider using
703 * @return #GNUNET_OK (continue to iterate)
706 find_best_address_it (void *cls,
707 const struct GNUNET_PeerIdentity *key,
710 struct FindBestAddressCtx *ctx = cls;
711 struct ATS_Address *current = value;
712 struct AddressWrapper *asi = current->solver_information;
713 struct GNUNET_TIME_Relative active_time;
715 double best_distance;
723 /* we need +1 slot if 'current' is not yet active */
724 need = (GNUNET_YES == current->active) ? 0 : 1;
725 /* we save -1 slot if 'best' is active and belongs
726 to the same network (as we would replace it) */
727 if ( (NULL != ctx->best) &&
728 (GNUNET_YES == ctx->best->active) &&
729 (((struct AddressWrapper *) ctx->best->solver_information)->network ==
732 /* we can gain -1 slot if this peers connectivity
733 requirement is higher than that of another peer
734 in that network scope */
735 con = ctx->s->env->get_connectivity (ctx->s->env->cls,
738 all_require_connectivity (ctx->s,
742 /* test if minimum bandwidth for 'current' would be available */
744 = is_bandwidth_available_in_network (asi->network,
748 /* Bandwidth for this address is unavailable, so we cannot use
752 if (GNUNET_YES == current->active)
754 active_time = GNUNET_TIME_absolute_get_duration (asi->activated);
755 if (active_time.rel_value_us <=
756 ((double) GNUNET_TIME_UNIT_SECONDS.rel_value_us) * ctx->s->stability_factor)
758 /* Keep active address for stability reasons */
763 if (NULL == ctx->best)
765 /* We so far have nothing else, so go with it! */
770 /* Now compare ATS information */
771 index = find_quality_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
772 cur_distance = current->atsin[index].norm;
773 best_distance = ctx->best->atsin[index].norm;
774 index = find_quality_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
775 cur_delay = current->atsin[index].norm;
776 best_delay = ctx->best->atsin[index].norm;
778 /* user shorter distance */
779 if (cur_distance < best_distance)
781 if (GNUNET_NO == ctx->best->active)
783 /* Activity doesn't influence the equation, use current */
786 else if ((best_distance / cur_distance) > ctx->s->stability_factor)
788 /* Distance change is significant, switch active address! */
793 /* User connection with less delay */
794 if (cur_delay < best_delay)
796 if (GNUNET_NO == ctx->best->active)
798 /* Activity doesn't influence the equation, use current */
801 else if ((best_delay / cur_delay) > ctx->s->stability_factor)
803 /* Latency change is significant, switch active address! */
812 * Find the currently best address for a peer from the set of
813 * addresses available or return NULL of no address is available.
815 * @param s the proportional handle
816 * @param addresses the address hashmap
817 * @param id the peer id
818 * @return the address or NULL
821 get_best_address (struct GAS_PROPORTIONAL_Handle *s,
822 struct GNUNET_CONTAINER_MultiPeerMap *addresses,
823 const struct GNUNET_PeerIdentity *id)
825 struct FindBestAddressCtx fba_ctx;
829 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
831 &find_best_address_it,
838 * Decrease number of active addresses in network.
840 * @param s the solver handle
841 * @param net the network type
844 address_decrement_active (struct GAS_PROPORTIONAL_Handle *s,
847 GNUNET_assert (net->active_addresses > 0);
848 net->active_addresses--;
849 GNUNET_STATISTICS_update (s->env->stats,
853 GNUNET_assert (s->active_addresses > 0);
854 s->active_addresses--;
855 GNUNET_STATISTICS_update (s->env->stats,
856 "# ATS addresses total",
863 * Address map iterator to find current active address for peer.
864 * Asserts that only one address is active per peer.
866 * @param cls last active address
867 * @param key peer's key
868 * @param value address to check
869 * @return #GNUNET_NO on double active address else #GNUNET_YES;
872 get_active_address_it (void *cls,
873 const struct GNUNET_PeerIdentity *key,
876 struct ATS_Address **dest = cls;
877 struct ATS_Address *aa = value;
879 if (GNUNET_YES != aa->active)
881 GNUNET_assert (NULL == (*dest));
888 * Find current active address for peer
890 * @param s the solver handle
891 * @param peer the peer
892 * @return active address or NULL
894 static struct ATS_Address *
895 get_active_address (struct GAS_PROPORTIONAL_Handle *s,
896 const struct GNUNET_PeerIdentity *peer)
898 struct ATS_Address *dest;
901 GNUNET_CONTAINER_multipeermap_get_multiple (s->env->addresses,
903 &get_active_address_it,
910 * Update active address for a peer. Check if active address exists
911 * and what the best address is, if addresses are different switch.
912 * Then reallocate bandwidth within the affected network scopes.
914 * @param s solver handle
915 * @param current_address the address currently active for the peer,
917 * @param peer the peer to check
920 update_active_address (struct GAS_PROPORTIONAL_Handle *s,
921 struct ATS_Address *current_address,
922 const struct GNUNET_PeerIdentity *peer)
924 struct ATS_Address *best_address;
925 struct AddressWrapper *asi_cur;
926 struct AddressWrapper *asi_best;
927 struct AddressWrapper *aw;
928 struct AddressWrapper *aw_min;
930 unsigned int con_min;
932 best_address = get_best_address (s,
935 if (NULL != best_address)
936 asi_best = best_address->solver_information;
939 if (current_address == best_address)
940 return; /* no changes */
941 if (NULL != current_address)
943 /* We switch to a new address (or to none);
944 mark old address as inactive. */
945 asi_cur = current_address->solver_information;
946 GNUNET_assert (GNUNET_YES == current_address->active);
947 LOG (GNUNET_ERROR_TYPE_INFO,
948 "Disabling previous active address for peer `%s'\n",
950 asi_cur->activated = GNUNET_TIME_UNIT_ZERO_ABS;
951 current_address->active = GNUNET_NO;
952 current_address->assigned_bw_in = 0;
953 current_address->assigned_bw_out = 0;
954 address_decrement_active (s,
956 if ( (NULL == best_address) ||
957 (asi_best->network != asi_cur->network) )
958 distribute_bandwidth_in_network (s,
960 if (NULL == best_address)
962 /* We previously had an active address, but now we cannot
963 * suggest one. Therefore we have to disconnect the peer.
964 * The above call to "distribute_bandwidth_in_network()
965 * does not see 'current_address' so we need to trigger
966 * the update here. */
967 LOG (GNUNET_ERROR_TYPE_DEBUG,
968 "Disconnecting peer `%s'.\n",
970 s->env->bandwidth_changed_cb (s->env->cls,
975 if (NULL == best_address)
977 /* We do not have a new address, so we are done. */
978 LOG (GNUNET_ERROR_TYPE_DEBUG,
979 "Cannot suggest address for peer `%s'\n",
983 /* We do have a new address, activate it */
984 LOG (GNUNET_ERROR_TYPE_DEBUG,
985 "Suggesting new address %p for peer `%s'\n",
988 /* Mark address as active */
989 best_address->active = GNUNET_YES;
990 asi_best->activated = GNUNET_TIME_absolute_get ();
991 asi_best->network->active_addresses++;
992 s->active_addresses++;
993 GNUNET_STATISTICS_update (s->env->stats,
994 "# ATS active addresses total",
997 GNUNET_STATISTICS_update (s->env->stats,
998 asi_best->network->stat_active,
1001 LOG (GNUNET_ERROR_TYPE_INFO,
1002 "Address %p for peer `%s' is now active\n",
1008 is_bandwidth_available_in_network (asi_best->network,
1011 /* we went over the maximum number of addresses for
1012 this scope; remove the address with the smallest
1013 connectivity requirement */
1014 con_min = UINT32_MAX;
1016 for (aw = asi_best->network->head; NULL != aw; aw = aw->next)
1019 (a_con = s->env->get_connectivity (s->env->cls,
1028 update_active_address (s,
1032 distribute_bandwidth_in_network (s,
1038 * The preferences for a peer in the problem changed.
1040 * @param solver the solver handle
1041 * @param peer the peer to change the preference for
1042 * @param kind the kind to change the preference
1043 * @param pref_rel the normalized preference value for this kind over all clients
1046 GAS_proportional_change_preference (void *solver,
1047 const struct GNUNET_PeerIdentity *peer,
1048 enum GNUNET_ATS_PreferenceKind kind,
1051 struct GAS_PROPORTIONAL_Handle *s = solver;
1053 distribute_bandwidth_in_network (s,
1059 * Get application feedback for a peer
1061 * @param solver the solver handle
1062 * @param application the application
1063 * @param peer the peer to change the preference for
1064 * @param scope the time interval for this feedback: [now - scope .. now]
1065 * @param kind the kind to change the preference
1066 * @param score the score
1069 GAS_proportional_feedback (void *solver,
1070 struct GNUNET_SERVER_Client *application,
1071 const struct GNUNET_PeerIdentity *peer,
1072 const struct GNUNET_TIME_Relative scope,
1073 enum GNUNET_ATS_PreferenceKind kind,
1076 /* Proportional does not care about feedback */
1081 * Get the preferred address for a specific peer
1083 * @param solver the solver handle
1084 * @param peer the identity of the peer
1087 GAS_proportional_start_get_address (void *solver,
1088 const struct GNUNET_PeerIdentity *peer)
1090 struct GAS_PROPORTIONAL_Handle *s = solver;
1092 update_active_address (s,
1093 get_active_address (s,
1100 * Stop notifying about address and bandwidth changes for this peer
1102 * @param solver the solver handle
1103 * @param peer the peer
1106 GAS_proportional_stop_get_address (void *solver,
1107 const struct GNUNET_PeerIdentity *peer)
1109 struct GAS_PROPORTIONAL_Handle *s = solver;
1110 struct ATS_Address *cur;
1111 struct AddressWrapper *asi;
1113 cur = get_active_address (s,
1117 asi = cur->solver_information;
1118 distribute_bandwidth_in_network (s,
1124 * Start a bulk operation
1126 * @param solver the solver
1129 GAS_proportional_bulk_start (void *solver)
1131 struct GAS_PROPORTIONAL_Handle *s = solver;
1133 LOG (GNUNET_ERROR_TYPE_DEBUG,
1134 "Locking solver for bulk operation ...\n");
1135 GNUNET_assert (NULL != solver);
1141 * Bulk operation done.
1143 * @param solver our `struct GAS_PROPORTIONAL_Handle *`
1146 GAS_proportional_bulk_stop (void *solver)
1148 struct GAS_PROPORTIONAL_Handle *s = solver;
1150 LOG (GNUNET_ERROR_TYPE_DEBUG,
1151 "Unlocking solver from bulk operation ...\n");
1152 if (s->bulk_lock < 1)
1158 if ( (0 == s->bulk_lock) &&
1159 (0 < s->bulk_requests) )
1161 LOG (GNUNET_ERROR_TYPE_INFO,
1162 "No lock pending, recalculating\n");
1163 distribute_bandwidth_in_network (s,
1165 s->bulk_requests = 0;
1171 * Transport properties for this address have changed
1173 * @param solver solver handle
1174 * @param address the address
1175 * @param type the ATSI type in HBO
1176 * @param abs_value the absolute value of the property
1177 * @param rel_value the normalized value
1180 GAS_proportional_address_property_changed (void *solver,
1181 struct ATS_Address *address,
1182 enum GNUNET_ATS_Property type,
1186 struct GAS_PROPORTIONAL_Handle *s = solver;
1187 struct AddressWrapper *asi = address->solver_information;
1189 distribute_bandwidth_in_network (s,
1195 * Add a new single address to a network
1197 * @param solver the solver Handle
1198 * @param address the address to add
1199 * @param network network type of this address
1202 GAS_proportional_address_add (void *solver,
1203 struct ATS_Address *address,
1204 enum GNUNET_ATS_Network_Type network)
1206 struct GAS_PROPORTIONAL_Handle *s = solver;
1207 struct Network *net;
1208 struct AddressWrapper *aw;
1210 GNUNET_assert (network < s->env->network_count);
1211 net = &s->network_entries[network];
1212 net->total_addresses++;
1214 aw = GNUNET_new (struct AddressWrapper);
1217 address->solver_information = aw;
1218 GNUNET_CONTAINER_DLL_insert (net->head,
1221 GNUNET_STATISTICS_update (s->env->stats,
1222 "# ATS addresses total",
1225 GNUNET_STATISTICS_update (s->env->stats,
1230 s->env->get_connectivity (s->env->cls,
1233 /* This peer is requested, find best address */
1234 update_active_address (s,
1235 get_active_address (s,
1239 LOG (GNUNET_ERROR_TYPE_INFO,
1240 "Added new address for `%s', now total %u and active %u addresses in network `%s'\n",
1241 GNUNET_i2s (&address->peer),
1242 net->total_addresses,
1243 net->active_addresses,
1249 * Remove an address from the solver. To do so, we:
1250 * - Removed it from specific network
1251 * - Decrease the number of total addresses
1253 * - decrease number of active addreses
1256 * @param solver the solver handle
1257 * @param address the address to remove
1260 GAS_proportional_address_delete (void *solver,
1261 struct ATS_Address *address)
1263 struct GAS_PROPORTIONAL_Handle *s = solver;
1264 struct AddressWrapper *aw = address->solver_information;
1265 struct Network *net = aw->network;
1267 LOG (GNUNET_ERROR_TYPE_DEBUG,
1268 "Deleting %s address for peer `%s' from network `%s' (total: %u/active: %u)\n",
1269 (GNUNET_NO == address->active) ? "inactive" : "active",
1270 GNUNET_i2s (&address->peer),
1272 net->total_addresses,
1273 net->active_addresses);
1275 GNUNET_CONTAINER_DLL_remove (net->head,
1278 GNUNET_assert (net->total_addresses > 0);
1279 net->total_addresses--;
1280 GNUNET_STATISTICS_update (s->env->stats,
1284 if (GNUNET_YES == address->active)
1286 /* Address was active, remove from network and update quotas */
1287 update_active_address (s,
1290 distribute_bandwidth_in_network (s, net);
1293 address->solver_information = NULL;
1294 LOG (GNUNET_ERROR_TYPE_DEBUG,
1295 "After deleting address now total %u and active %u addresses in network `%s'\n",
1296 net->total_addresses,
1297 net->active_addresses,
1303 * Function invoked when the plugin is loaded.
1305 * @param[in,out] cls the `struct GNUNET_ATS_PluginEnvironment *` to use;
1306 * modified to return the API functions (ugh).
1307 * @return the `struct GAS_PROPORTIONAL_Handle` to pass as a closure
1310 libgnunet_plugin_ats_proportional_init (void *cls)
1312 static struct GNUNET_ATS_SolverFunctions sf;
1313 struct GNUNET_ATS_PluginEnvironment *env = cls;
1314 struct GAS_PROPORTIONAL_Handle *s;
1315 struct Network * cur;
1319 s = GNUNET_new (struct GAS_PROPORTIONAL_Handle);
1322 sf.s_add = &GAS_proportional_address_add;
1323 sf.s_address_update_property = &GAS_proportional_address_property_changed;
1324 sf.s_get = &GAS_proportional_start_get_address;
1325 sf.s_get_stop = &GAS_proportional_stop_get_address;
1326 sf.s_pref = &GAS_proportional_change_preference;
1327 sf.s_feedback = &GAS_proportional_feedback;
1328 sf.s_del = &GAS_proportional_address_delete;
1329 sf.s_bulk_start = &GAS_proportional_bulk_start;
1330 sf.s_bulk_stop = &GAS_proportional_bulk_stop;
1331 s->stability_factor = PROP_STABILITY_FACTOR;
1332 if (GNUNET_SYSERR !=
1333 GNUNET_CONFIGURATION_get_value_float (env->cfg,
1335 "PROP_STABILITY_FACTOR",
1338 if ((f_tmp < 1.0) || (f_tmp > 2.0))
1340 LOG (GNUNET_ERROR_TYPE_ERROR,
1341 _("Invalid %s configuration %f \n"),
1342 "PROP_STABILITY_FACTOR",
1347 s->stability_factor = f_tmp;
1348 LOG (GNUNET_ERROR_TYPE_INFO,
1349 "Using %s of %.3f\n",
1350 "PROP_STABILITY_FACTOR",
1354 s->prop_factor = PROPORTIONALITY_FACTOR;
1355 if (GNUNET_SYSERR !=
1356 GNUNET_CONFIGURATION_get_value_float (env->cfg,
1358 "PROP_PROPORTIONALITY_FACTOR",
1363 LOG (GNUNET_ERROR_TYPE_ERROR,
1364 _("Invalid %s configuration %f\n"),
1365 "PROP_PROPORTIONALITY_FACTOR",
1370 s->prop_factor = f_tmp;
1371 LOG (GNUNET_ERROR_TYPE_INFO,
1372 "Using %s of %.3f\n",
1373 "PROP_PROPORTIONALITY_FACTOR",
1378 s->network_entries = GNUNET_malloc (env->network_count *
1379 sizeof (struct Network));
1380 for (c = 0; c < env->network_count; c++)
1382 cur = &s->network_entries[c];
1384 cur->total_quota_in = env->in_quota[c];
1385 cur->total_quota_out = env->out_quota[c];
1386 cur->desc = GNUNET_ATS_print_network_type (c);
1387 GNUNET_asprintf (&cur->stat_total,
1388 "# ATS addresses %s total",
1390 GNUNET_asprintf (&cur->stat_active,
1391 "# ATS active addresses %s total",
1393 LOG (GNUNET_ERROR_TYPE_INFO,
1394 "Added network %u `%s' (%llu/%llu)\n",
1397 cur->total_quota_in,
1398 cur->total_quota_out);
1405 * Function used to unload the plugin.
1407 * @param cls return value from #libgnunet_plugin_ats_proportional_init()
1410 libgnunet_plugin_ats_proportional_done (void *cls)
1412 struct GNUNET_ATS_SolverFunctions *sf = cls;
1413 struct GAS_PROPORTIONAL_Handle *s = sf->cls;
1414 struct AddressWrapper *cur;
1415 struct AddressWrapper *next;
1418 for (c = 0; c < s->network_count; c++)
1420 GNUNET_break (0 == s->network_entries[c].total_addresses);
1421 GNUNET_break (0 == s->network_entries[c].active_addresses);
1422 next = s->network_entries[c].head;
1423 while (NULL != (cur = next))
1426 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
1427 s->network_entries[c].tail,
1429 GNUNET_free_non_null (cur->addr->solver_information);
1432 GNUNET_free (s->network_entries[c].stat_total);
1433 GNUNET_free (s->network_entries[c].stat_active);
1435 GNUNET_break (0 == s->active_addresses);
1436 GNUNET_free (s->network_entries);
1442 /* end of plugin_ats_proportional.c */