2 This file is part of GNUnet.
3 (C) 2011-2014 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-service-ats_addresses.h"
32 #define PROP_STABILITY_FACTOR 1.25
35 #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
40 * NOTE: Do not change this documentation. This documentation is based
41 * on gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
42 * use build_txt.sh to generate plaintext output
44 * ATS addresses : proportional solver
46 * The proportional solver ("proportional") distributes the available
47 * bandwidth fair over all the addresses influenced by the
48 * preference values. For each available network type an in- and
49 * outbound quota is configured and the bandwidth available in
50 * these networks is distributed over the addresses. The solver
51 * first assigns every addresses the minimum amount of bandwidth
52 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
53 * remaining bandwidth available according to the preference
54 * values. For each peer only a single address gets bandwidth
55 * assigned and only one address marked as active. The most
56 * important functionality for the solver is implemented in: *
57 * find_address_it is an hashmap iterator returning the prefered
58 * address for an peer * update_quota_per_network distributes
59 * available bandwidth for a network over active addresses
61 * Changes to addresses automatically have an impact on the the
62 * bandwidth assigned to other addresses in the same network since
63 * the solver distributes the remaining bandwidth over the
64 * addresses in the network. When changes to the addresses occur,
65 * the solver first performs the changes, like adding or deleting
66 * addresses, and then updates bandwidth assignment for the
67 * affected network. Bandwidth assignment is only recalculated on
68 * demand when an address is requested by a client for a peer or
69 * when the addresses available have changed or an address changed
70 * the network it is located in. When the bandwidth assignment has
71 * changed the callback is called with the new bandwidth
72 * assignments. The bandwidth distribution for a network is
73 * recalculated due to: * address suggestion requests * address
74 * deletions * address switching networks during address update *
77 * 3.1 Data structures used
79 * For each ATS network (e.g. WAN, LAN, loopback) a struct Network
80 * is used to specify network related information as total adresses
81 * and active addresses in this network and the configured in- and
82 * outbound quota. Each network also contains a list of addresses
83 * added to the solver located in this network. The proportional
84 * solver uses the addresses' solver_information field to store the
85 * proportional network it belongs to for each address.
89 * When the proportional solver is initialized the solver creates a
90 * new solver handle and initializes the network structures with
91 * the quotas passed from addresses and returns the handle solver.
93 * 3.3 Adding an address
95 * When a new address is added to the solver using s_add, a lookup
96 * for the network for this address is done and the address is
97 * enqueued in in the linked list of the network.
99 * 3.4 Updating an address
101 * The main purpose of address updates is to update the ATS
102 * information for addresse selection. Important for the proportional
103 * solver is when an address switches network it is located
104 * in. This is common because addresses added by transport's
105 * validation mechanism are commonly located in
106 * GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
107 * in this network type and only if a connection is successful on
108 * return of payload data transport switches to the real network
109 * the address is located in. When an address changes networks it
110 * is first of all removed from the old network using the solver
111 * API function #GAS_proportional_address_delete() and the network in
112 * the address struct is updated. A lookup for the respective new
113 * proportional network is done and stored in the addresse's
114 * solver_information field. Next the address is re-added to the
115 * solver using the solver API function
116 * #GAS_proportional_address_add(). If the address was marked as in
117 * active, the solver checks if bandwidth is available in the
118 * network and if yes sets the address to active and updates the
119 * bandwidth distribution in this network. If no bandwidth is
120 * available it sets the bandwidth for this address to 0 and tries
121 * to suggest an alternative address. If an alternative address was
122 * found, addresses' callback is called for this address.
124 * 3.5 Deleting an address
126 * When an address is removed from the solver, it removes the
127 * respective address from the network and if the address was
128 * marked as active, it updates the bandwidth distribution for this
131 * 3.6 Requesting addresses
133 * When an address is requested for a peer the solver performs a
134 * lookup for the peer entry in addresses address hashmap and
135 * selects the best address. The selection of the most suitable
136 * address is done in the find_address_it hashmap iterator
137 * described in detail in section 3.7. If no address is returned,
138 * no address can be suggested at the moment. If the address
139 * returned is marked as active, the solver can return this
140 * address. If the address is not marked as active, the solver
141 * checks if another address belongign to this peer is marked as
142 * active and marks the address as inactive, updates the bandwidth
143 * for this address to 0, call the bandwidth changed callback for
144 * this address due to the change and updates quota assignment for
145 * the addresse's network. the now in-active address is belonging
146 * to. The solver marks the new address as active and updates the
147 * bandwidth assignment for this network.
149 * 3.7 Choosing addresses
151 * Choosing the best possible address for suggestion is done by
152 * iterating over all addresses of a peer stored in addresses'
153 * hashmap and using the hashmap iterator find_address_it to select
154 * the best available address. Several checks are done when an
155 * address is selected. First if this address is currently blocked
156 * by addresses from being suggested. An address is blocked for the
157 * duration of ATS_BLOCKING_DELTA when it is suggested to
158 * transport. Next it is checked if at least
159 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
160 * in the addresse's network, because suggesting an address without
161 * bandwidth does not make sense. This also ensures that all active
162 * addresses in this network get at least the minimum amount of
163 * bandwidth assigned. In the next step the solver ensures that for
164 * tcp connections inbound connections are prefered over outbound
165 * connections. In the next stet the solver ensures that
166 * connections are prefered in the following order: * connections
167 * are already established and have bandwidth assigned *
168 * connections with a shorter distance * connectes have a shorter
171 * 3.8 Changing preferences
175 * During shutdown all network entries and aging processes are
176 * destroyed and freed.
181 * This solver assigns in and outbound bandwidth equally for all
182 * addresses in specific network type (WAN, LAN) based on configured
183 * in and outbound quota for this network.
185 * The solver is notified by addresses about changes to the addresses
186 * and recalculates the bandwith assigned if required. The solver
187 * notifies addresses by calling the GAS_bandwidth_changed_cb
195 * For each peer only a single is selected and marked as "active" in the address
200 * You have the networks WAN and LAN and quotas
201 * WAN_TOTAL_IN, WAN_TOTAL_OUT
202 * LAN_TOTAL_IN, LAN_TOTAL_OUT
204 * If you have x addresses in the network segment LAN, the quotas are
205 * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
207 * Quotas are automatically recalculated and reported back when addresses are
212 #define PROPORTIONALITY_FACTOR 2.0
215 * A handle for the proportional solver
217 struct GAS_PROPORTIONAL_Handle
219 struct GNUNET_ATS_PluginEnvironment *env;
224 struct GNUNET_STATISTICS_Handle *stats;
227 * Hashmap containing all valid addresses
229 struct GNUNET_CONTAINER_MultiPeerMap *addresses;
232 * Pending address requests
234 struct GNUNET_CONTAINER_MultiPeerMap *requests;
237 * Bandwidth changed callback
239 GAS_bandwidth_changed_cb bw_changed;
242 * Bandwidth changed callback cls
244 void *bw_changed_cls;
247 * ATS function to get preferences
249 GAS_get_preferences get_preferences;
252 * Closure for ATS function to get preferences
254 void *get_preferences_cls;
257 * ATS function to get properties
259 GAS_get_properties get_properties;
262 * Closure for ATS function to get properties
264 void *get_properties_cls;
272 * Number of changes while solver was locked
277 * Total number of addresses for solver
279 unsigned int total_addresses;
282 * Number of active addresses for solver
284 unsigned int active_addresses;
289 struct Network *network_entries;
294 unsigned int network_count;
297 * Proportionality factor
304 double stability_factor;
308 * Representation of a network
318 * Network description
323 * Total inbound quota
326 unsigned long long total_quota_in;
329 * Total outbound quota
332 unsigned long long total_quota_out;
335 * Number of active addresses for this network
337 unsigned int active_addresses;
340 * Number of total addresses for this network
342 unsigned int total_addresses;
345 * String for statistics total addresses
350 * String for statistics active addresses
357 struct AddressWrapper *head;
362 struct AddressWrapper *tail;
366 * Address information stored in the solver
368 struct AddressSolverInformation
370 struct Network *network;
376 unsigned long long calculated_quota_in_NBO;
382 unsigned long long calculated_quota_out_NBO;
385 * When was this address activated
387 struct GNUNET_TIME_Absolute activated;
392 * Wrapper for addresses to store them in network's linked list
394 struct AddressWrapper
399 struct AddressWrapper *next;
404 struct AddressWrapper *prev;
409 struct ATS_Address *addr;
414 * Function used to unload the plugin.
416 * @param cls return value from #libgnunet_plugin_ats_proportional_init()
419 libgnunet_plugin_ats_proportional_done (void *cls)
421 struct GAS_PROPORTIONAL_Handle *s = cls;
422 struct AddressWrapper *cur;
423 struct AddressWrapper *next;
426 GNUNET_assert(s != NULL);
427 for (c = 0; c < s->network_count; c++)
429 if (s->network_entries[c].total_addresses > 0)
431 LOG(GNUNET_ERROR_TYPE_DEBUG,
432 "Had %u addresses for network `%s' not deleted during shutdown\n",
433 s->network_entries[c].total_addresses, s->network_entries[c].desc);
437 if (s->network_entries[c].active_addresses > 0)
439 LOG(GNUNET_ERROR_TYPE_DEBUG,
440 "Had %u active addresses for network `%s' not deleted during shutdown\n",
441 s->network_entries[c].active_addresses, s->network_entries[c].desc);
445 next = s->network_entries[c].head;
446 while (NULL != (cur = next))
449 GNUNET_CONTAINER_DLL_remove(s->network_entries[c].head,
450 s->network_entries[c].tail, cur);
451 GNUNET_free_non_null (cur->addr->solver_information);
454 GNUNET_free(s->network_entries[c].stat_total);
455 GNUNET_free(s->network_entries[c].stat_active);
457 if (s->total_addresses > 0)
459 LOG(GNUNET_ERROR_TYPE_DEBUG,
460 "Had %u addresses not deleted during shutdown\n", s->total_addresses);
463 if (s->active_addresses > 0)
465 LOG(GNUNET_ERROR_TYPE_DEBUG,
466 "Had %u active addresses not deleted during shutdown\n",
467 s->active_addresses);
470 GNUNET_free (s->network_entries);
471 GNUNET_CONTAINER_multipeermap_destroy (s->requests);
478 * Test if bandwidth is available in this network to add an additional address
480 * @param net the network type to update
481 * @return #GNUNET_YES or #GNUNET_NO
484 is_bandwidth_available_in_network (struct Network *net)
486 GNUNET_assert(NULL != net);
487 unsigned int na = net->active_addresses + 1;
488 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
489 if (((net->total_quota_in / na) > min_bw)
490 && ((net->total_quota_out / na) > min_bw))
492 LOG(GNUNET_ERROR_TYPE_DEBUG,
493 "Enough bandwidth available for %u active addresses in network `%s'\n",
498 LOG(GNUNET_ERROR_TYPE_DEBUG,
499 "Not enough bandwidth available for %u active addresses in network `%s'\n",
506 * Update bandwidth assigned to peers in this network
508 * @param s the solver handle
509 * @param net the network type to update
513 distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
516 struct AddressSolverInformation *asi;
517 struct AddressWrapper *cur_address;
519 unsigned long long remaining_quota_in = 0;
520 unsigned long long quota_out_used = 0;
521 unsigned long long remaining_quota_out = 0;
522 unsigned long long quota_in_used = 0;
524 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
525 double relative_peer_prefence;
526 double sum_relative_peer_prefences; /* Important: has to be double not float due to precision */
527 double cur_pref; /* Important: has to be double not float due to precision */
530 const double *peer_relative_prefs = NULL; /* Important: has to be double not float due to precision */
532 unsigned long long assigned_quota_in = 0;
533 unsigned long long assigned_quota_out = 0;
536 LOG(GNUNET_ERROR_TYPE_INFO,
537 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
538 net->desc, net->active_addresses, net->total_quota_in,
539 net->total_quota_in);
541 if (net->active_addresses == 0)
543 return; /* no addresses to update */
547 * Assign every peer in network minimum Bandwidth
548 * Distribute bandwidth left according to preference
551 if ((net->active_addresses * min_bw) > net->total_quota_in)
556 if ((net->active_addresses * min_bw) > net->total_quota_out)
562 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
563 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
564 LOG(GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
565 remaining_quota_in, remaining_quota_out);
566 sum_relative_peer_prefences = 0.0;
568 /* Calculate sum of relative preference for active addresses in this network */
570 for (cur_address = net->head; NULL != cur_address; cur_address = cur_address->next)
572 if (GNUNET_YES != cur_address->addr->active)
575 GNUNET_assert( NULL != (peer_relative_prefs = s->get_preferences (s->get_preferences_cls,
576 &cur_address->addr->peer)));
577 relative_peer_prefence = 0.0;
578 relative_peer_prefence += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
579 sum_relative_peer_prefences += relative_peer_prefence;
583 if (count_addresses != net->active_addresses)
586 LOG(GNUNET_ERROR_TYPE_WARNING,
587 "%s: Counted %u active addresses, but network says to have %u active addresses \n",
588 net->desc, count_addresses, net->active_addresses);
589 for (cur_address = net->head; NULL != cur_address; cur_address = cur_address->next)
591 if (GNUNET_YES != cur_address->addr->active)
594 LOG (GNUNET_ERROR_TYPE_WARNING, "Active: `%s' `%s' length %u\n",
595 GNUNET_i2s (&cur_address->addr->peer), cur_address->addr->plugin,
596 cur_address->addr->addr_len);
600 LOG (GNUNET_ERROR_TYPE_INFO,
601 "Total relative preference %.3f for %u addresses in network %s\n",
602 sum_relative_peer_prefences, net->active_addresses, net->desc);
604 for (cur_address = net->head; NULL != cur_address; cur_address = cur_address->next)
606 if (GNUNET_YES == cur_address->addr->active)
608 GNUNET_assert( NULL != (peer_relative_prefs =
609 s->get_preferences (s->get_preferences_cls, &cur_address->addr->peer)));
611 cur_pref = peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
612 total_weight = net->active_addresses +
613 s->prop_factor * sum_relative_peer_prefences;
614 peer_weight = (1.0 + (s->prop_factor * cur_pref));
616 assigned_quota_in = min_bw
617 + ((peer_weight / total_weight) * remaining_quota_in);
618 assigned_quota_out = min_bw
619 + ((peer_weight / total_weight) * remaining_quota_out);
621 LOG (GNUNET_ERROR_TYPE_INFO,
622 "New quota for peer `%s' with weight (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
623 GNUNET_i2s (&cur_address->addr->peer), peer_weight, total_weight,
624 assigned_quota_in, assigned_quota_out);
628 assigned_quota_in = 0;
629 assigned_quota_out = 0;
632 quota_in_used += assigned_quota_in;
633 quota_out_used += assigned_quota_out;
634 /* Prevent overflow due to rounding errors */
635 if (assigned_quota_in > UINT32_MAX)
636 assigned_quota_in = UINT32_MAX;
637 if (assigned_quota_out > UINT32_MAX)
638 assigned_quota_out = UINT32_MAX;
640 /* Compare to current bandwidth assigned */
641 asi = cur_address->addr->solver_information;
642 asi->calculated_quota_in_NBO = htonl (assigned_quota_in);
643 asi->calculated_quota_out_NBO = htonl (assigned_quota_out);
645 LOG(GNUNET_ERROR_TYPE_DEBUG,
646 "Total bandwidth assigned is (in/out): %llu /%llu\n", quota_in_used,
648 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
650 LOG(GNUNET_ERROR_TYPE_ERROR,
651 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
652 net->active_addresses, quota_out_used, net->total_quota_out);
654 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
656 LOG(GNUNET_ERROR_TYPE_ERROR,
657 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
658 net->active_addresses, quota_in_used, net->total_quota_in);
666 struct FindBestAddressCtx
668 struct GAS_PROPORTIONAL_Handle *s;
669 struct ATS_Address *best;
677 find_property_index (uint32_t type)
679 int existing_types[] = GNUNET_ATS_QualityProperties;
682 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
683 if (existing_types[c] == type)
685 return GNUNET_SYSERR;
690 * Find a "good" address to use for a peer by iterating over the addresses for this peer.
691 * If we already have an existing address, we stick to it.
692 * Otherwise, we pick by lowest distance and then by lowest latency.
694 * @param cls the `struct FindBestAddressCtx *' where we store the result
696 * @param value another `struct ATS_Address*` to consider using
697 * @return #GNUNET_OK (continue to iterate)
700 find_best_address_it (void *cls,
701 const struct GNUNET_PeerIdentity *key,
704 struct FindBestAddressCtx *ctx = cls;
705 struct ATS_Address *current = value;
706 struct ATS_Address *current_best = current;
707 struct GNUNET_TIME_Absolute now;
708 struct AddressSolverInformation *asi;
709 struct GNUNET_TIME_Relative active_time;
710 struct GNUNET_TIME_Relative min_active_time;
711 const double *norm_prop_cur;
712 const double *norm_prop_best;
714 double best_distance;
720 asi = current->solver_information;
721 now = GNUNET_TIME_absolute_get ();
723 if ((current->active == GNUNET_NO)
724 && (current->blocked_until.abs_value_us
725 == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value_us))
727 /* This address is blocked for suggestion */
728 LOG (GNUNET_ERROR_TYPE_DEBUG,
729 "Address %p blocked for suggestion for %s \n",
731 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (now, current->blocked_until),
741 if (GNUNET_NO == is_bandwidth_available_in_network (asi->network))
743 return GNUNET_OK; /* There's no bandwidth available in this network */
746 if (NULL != ctx->best)
748 /* Compare current addresses with denominated 'best' address */
749 current_best = ctx->best;
753 /* We do not have a 'best' address so take this address */
754 LOG (GNUNET_ERROR_TYPE_DEBUG,
755 "Setting initial address %p\n",
757 current_best = current;
761 if (GNUNET_YES == current->active)
763 GNUNET_assert (asi->activated.abs_value_us != GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us);
764 active_time = GNUNET_TIME_absolute_get_duration (asi->activated);
765 min_active_time.rel_value_us = ((double) GNUNET_TIME_UNIT_SECONDS.rel_value_us) *
766 PROP_STABILITY_FACTOR;
767 if (active_time.rel_value_us <= min_active_time.rel_value_us)
769 /* Keep active address for stability reasons */
775 /* Now compare ATS information */
776 norm_prop_cur = ctx->s->get_properties (ctx->s->get_properties_cls,
777 (const struct ATS_Address *) current);
778 index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
779 cur_distance = norm_prop_cur[index];
780 index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
781 cur_delay = norm_prop_cur[index];
783 norm_prop_best = ctx->s->get_properties (ctx->s->get_properties_cls,
784 (const struct ATS_Address *) ctx->best);
785 index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
786 best_distance = norm_prop_best[index];
787 index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
788 best_delay = norm_prop_best[index];
790 /* user shorter distance */
791 if (cur_distance < best_distance)
794 if (GNUNET_NO == ctx->best->active)
796 current_best = current; /* Use current */
798 else if ((best_distance / cur_distance) > ctx->s->stability_factor)
800 /* Best and active address performs worse */
801 current_best = current;
806 /* Use current best */
807 current_best = ctx->best;
810 /* User connection with less delay */
811 if (cur_delay < best_delay)
814 if (GNUNET_NO == ctx->best->active)
816 current_best = current; /* Use current */
818 else if ((best_delay / cur_delay) > ctx->s->stability_factor)
820 /* Best and active address performs worse */
821 current_best = current;
826 /* Use current best */
827 current_best = ctx->best;
831 ctx->best = current_best;
840 get_best_address (struct GAS_PROPORTIONAL_Handle *s,
841 struct GNUNET_CONTAINER_MultiPeerMap *addresses,
842 const struct GNUNET_PeerIdentity *id)
844 struct FindBestAddressCtx fba_ctx;
848 GNUNET_CONTAINER_multipeermap_get_multiple (addresses, id,
849 &find_best_address_it, &fba_ctx);
856 * Hashmap Iterator to find current active address for peer
858 * @param cls last active address
859 * @param key peer's key
860 * @param value address to check
861 * @return #GNUNET_NO on double active address else #GNUNET_YES;
864 get_active_address_it (void *cls,
865 const struct GNUNET_PeerIdentity *key,
868 struct ATS_Address **dest = cls;
869 struct ATS_Address *aa = (struct ATS_Address *) value;
871 if (GNUNET_YES == aa->active)
876 /* should never happen */
877 LOG (GNUNET_ERROR_TYPE_ERROR,
878 "Multiple active addresses for peer `%s'\n",
879 GNUNET_i2s (&aa->peer));
890 * Find current active address for peer
892 * @param solver the solver handle
893 * @param addresses the address set
894 * @param peer the peer
895 * @return active address or NULL
897 static struct ATS_Address *
898 get_active_address (void *solver,
899 const struct GNUNET_CONTAINER_MultiPeerMap * addresses,
900 const struct GNUNET_PeerIdentity *peer)
902 struct ATS_Address * dest = NULL;
904 GNUNET_CONTAINER_multipeermap_get_multiple (addresses, peer,
905 &get_active_address_it, &dest);
911 * Lookup network struct by type
913 * @param s the solver handle
914 * @param type the network type
915 * @return the network struct
917 static struct Network *
918 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
921 for (c = 0; c < s->network_count; c++)
923 if (s->network_entries[c].type == type)
924 return &s->network_entries[c];
931 * FIXME. Degermanize.
934 addresse_increment (struct GAS_PROPORTIONAL_Handle *s,
939 if (GNUNET_YES == total)
941 s->total_addresses++;
942 net->total_addresses++;
943 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
944 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
946 if (GNUNET_YES == active)
948 net->active_addresses++;
949 s->active_addresses++;
950 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1,
952 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
959 * FIXME. Degermanize.
962 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s,
969 if (GNUNET_YES == total)
971 if (s->total_addresses < 1)
978 s->total_addresses--;
979 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
982 if (net->total_addresses < 1)
989 net->total_addresses--;
990 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
994 if (GNUNET_YES == active)
996 if (net->active_addresses < 1)
1003 net->active_addresses--;
1004 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
1006 if (s->active_addresses < 1)
1009 res = GNUNET_SYSERR;
1013 s->active_addresses--;
1014 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
1026 address_eq (struct ATS_Address *a, struct ATS_Address *b)
1028 GNUNET_assert (NULL != a);
1029 GNUNET_assert (NULL != b);
1030 if (0 != strcmp(a->plugin, b->plugin))
1032 if (a->addr_len != b->addr_len)
1034 if (0 != memcmp (a->addr, b->addr, b->addr_len))
1036 if (a->session_id != b->session_id)
1046 propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
1047 struct Network *net)
1049 struct AddressWrapper *cur;
1050 struct AddressSolverInformation *asi;
1051 for (cur = net->head; NULL != cur; cur = cur->next)
1053 asi = cur->addr->solver_information;
1054 if ( (cur->addr->assigned_bw_in.value__ != asi->calculated_quota_in_NBO) ||
1055 (cur->addr->assigned_bw_out.value__ != asi->calculated_quota_out_NBO) )
1057 cur->addr->assigned_bw_in.value__ = asi->calculated_quota_in_NBO;
1058 cur->addr->assigned_bw_out.value__ = asi->calculated_quota_out_NBO;
1060 /* Reset for next iteration */
1061 asi->calculated_quota_in_NBO = htonl (0);
1062 asi->calculated_quota_out_NBO = htonl (0);
1064 LOG (GNUNET_ERROR_TYPE_DEBUG,
1065 "Bandwidth for %s address %p for peer `%s' changed to %u/%u\n",
1066 (GNUNET_NO == cur->addr->active) ? "inactive" : "active",
1068 GNUNET_i2s (&cur->addr->peer),
1069 ntohl (cur->addr->assigned_bw_in.value__),
1070 ntohl (cur->addr->assigned_bw_out.value__ ));
1072 /* Notify on change */
1073 if ((GNUNET_YES == cur->addr->active))
1075 s->bw_changed (s->bw_changed_cls, cur->addr);
1083 * Distribibute bandwidth
1085 * @param s the solver handle
1086 * @param n the network, can be NULL for all network
1087 * @param address_except do not notify for this address
1090 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
1093 if (GNUNET_YES == s->bulk_lock)
1101 LOG (GNUNET_ERROR_TYPE_INFO,
1102 "Redistributing bandwidth in network %s with %u active and %u total addresses\n",
1103 GNUNET_ATS_print_network_type(n->type),
1104 n->active_addresses, n->total_addresses);
1106 if (NULL != s->env->info_cb)
1107 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_START,
1108 GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
1111 distribute_bandwidth(s, n);
1113 if (NULL != s->env->info_cb)
1114 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_STOP,
1115 GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
1116 if (NULL != s->env->info_cb)
1117 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
1118 GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
1120 /* Do propagation */
1121 propagate_bandwidth (s, n);
1123 if (NULL != s->env->info_cb)
1124 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
1125 GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
1130 if (NULL != s->env->info_cb)
1131 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_START,
1132 GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
1133 for (i = 0; i < s->network_count; i++)
1136 distribute_bandwidth(s, &s->network_entries[i]);
1139 if (NULL != s->env->info_cb)
1140 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_STOP,
1141 GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
1142 if (NULL != s->env->info_cb)
1143 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
1144 GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
1145 for (i = 0; i < s->network_count; i++)
1147 /* Do propagation */
1148 propagate_bandwidth(s, &s->network_entries[i]);
1150 if (NULL != s->env->info_cb)
1151 s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
1152 GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
1160 static const struct ATS_Address *
1161 update_active_address (struct GAS_PROPORTIONAL_Handle *s,
1162 const struct GNUNET_PeerIdentity *peer)
1164 struct ATS_Address *best_address;
1165 struct ATS_Address *current_address;
1166 struct AddressSolverInformation *asi;
1167 struct Network *net;
1169 LOG (GNUNET_ERROR_TYPE_INFO,
1170 "Updating active address for peer `%s'\n",
1173 /* Find active address */
1174 current_address = get_active_address (s, s->addresses, peer);
1175 /* Find best address */
1176 best_address = get_best_address (s,s->addresses, peer);
1178 if (NULL != current_address)
1180 if ((NULL == best_address) || ((NULL != best_address) &&
1181 (GNUNET_NO == address_eq (current_address, best_address))))
1183 /* We switch to a new address, mark old address as inactive */
1184 LOG (GNUNET_ERROR_TYPE_INFO,
1185 "Disabling previous %s address %p for peer `%s'\n",
1186 (GNUNET_NO == current_address->active) ? "inactive" : "active",
1190 asi = current_address->solver_information;
1191 GNUNET_assert (NULL != asi);
1194 asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
1195 current_address->active = GNUNET_NO; /* No active any longer */
1196 current_address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1197 current_address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1199 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1202 /* Update network of previous address */
1203 distribute_bandwidth_in_network (s, net);
1205 if (NULL == best_address)
1207 /* We previously had an active address, but now we cannot suggest one
1208 * Therefore we have to disconnect the peer */
1210 LOG (GNUNET_ERROR_TYPE_INFO,
1211 "Disconnecting peer `%s' with previous %s address %p\n",
1213 (GNUNET_NO == current_address->active) ? "inactive" : "active",
1215 s->bw_changed (s->bw_changed_cls, current_address);
1219 if (NULL == best_address)
1221 LOG (GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1226 LOG (GNUNET_ERROR_TYPE_INFO, "Suggesting %s address %p for peer `%s'\n",
1227 (GNUNET_NO == best_address->active) ? "inactive" : "active", best_address,
1230 if ((NULL != current_address) &&
1231 (GNUNET_YES == address_eq (best_address, current_address)))
1233 if (current_address->active == GNUNET_NO)
1234 GNUNET_break (0); /* Should never happen */
1235 return best_address; /* Same same */
1238 asi = best_address->solver_information;
1239 GNUNET_assert (NULL != asi);
1242 /* Mark address as active */
1243 asi->activated = GNUNET_TIME_absolute_get();
1244 best_address->active = GNUNET_YES;
1245 addresse_increment (s, net, GNUNET_NO, GNUNET_YES);
1247 /* Distribute bandwidth */
1248 distribute_bandwidth_in_network (s, net);
1249 return best_address;
1254 * Changes the preferences for a peer in the problem
1256 * @param solver the solver handle
1257 * @param peer the peer to change the preference for
1258 * @param kind the kind to change the preference
1259 * @param pref_rel the normalized preference value for this kind over all clients
1262 GAS_proportional_address_change_preference (void *solver,
1263 const struct GNUNET_PeerIdentity *peer,
1264 enum GNUNET_ATS_PreferenceKind kind,
1267 struct GAS_PROPORTIONAL_Handle *s = solver;
1268 struct ATS_Address *best_address;
1269 struct ATS_Address *active_address;
1270 struct AddressSolverInformation *asi;
1272 GNUNET_assert(NULL != s);
1273 GNUNET_assert(NULL != peer);
1275 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
1276 return; /* Peer is not requested */
1278 /* This peer is requested, find best address */
1279 active_address = get_active_address(s, s->addresses, peer);
1280 best_address = (struct ATS_Address *) update_active_address (s, peer);
1282 if ((NULL != best_address) && ((NULL != active_address) &&
1283 (GNUNET_YES == address_eq (active_address, best_address))))
1285 asi = best_address->solver_information;
1286 GNUNET_assert (NULL != asi);
1288 /* We sticked to the same address, therefore redistribute */
1289 distribute_bandwidth_in_network (s, asi->network);
1295 * Get application feedback for a peer
1297 * @param solver the solver handle
1298 * @param application the application
1299 * @param peer the peer to change the preference for
1300 * @param scope the time interval for this feedback: [now - scope .. now]
1301 * @param kind the kind to change the preference
1302 * @param score the score
1305 GAS_proportional_address_preference_feedback (void *solver,
1307 const struct GNUNET_PeerIdentity *peer,
1308 const struct GNUNET_TIME_Relative scope,
1309 enum GNUNET_ATS_PreferenceKind kind,
1312 struct GAS_PROPORTIONAL_Handle *s = solver;
1314 GNUNET_assert(NULL != peer);
1315 GNUNET_assert(NULL != s);
1320 * Get the preferred address for a specific peer
1322 * @param solver the solver handle
1323 * @param peer the identity of the peer
1326 static const struct ATS_Address *
1327 GAS_proportional_get_preferred_address (void *solver,
1328 const struct GNUNET_PeerIdentity *peer)
1330 struct GAS_PROPORTIONAL_Handle *s = solver;
1331 const struct ATS_Address *best_address;
1334 GNUNET_assert(s != NULL);
1335 GNUNET_assert(peer != NULL);
1337 /* Add to list of pending requests */
1338 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
1341 GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (s->requests, peer, NULL,
1342 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1343 LOG (GNUNET_ERROR_TYPE_INFO, "Start suggesting addresses for peer `%s'\n",
1347 best_address = update_active_address (s, peer);
1348 if (s->bulk_lock > 0)
1349 return NULL; /* Do not suggest since bulk lock is pending */
1351 return best_address;
1356 * Stop notifying about address and bandwidth changes for this peer
1358 * @param solver the solver handle
1359 * @param peer the peer
1362 GAS_proportional_stop_get_preferred_address (void *solver,
1363 const struct GNUNET_PeerIdentity *peer)
1365 struct GAS_PROPORTIONAL_Handle *s = solver;
1366 struct ATS_Address *cur;
1367 struct AddressSolverInformation *asi;
1368 struct Network *cur_net;
1370 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
1371 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (s->requests,
1374 cur = get_active_address (s, s->addresses, peer);
1377 LOG(GNUNET_ERROR_TYPE_INFO,
1378 "Disabling %s address %p for peer `%s'\n",
1379 (GNUNET_NO == cur->active) ? "inactive" : "active", cur,
1380 GNUNET_i2s (&cur->peer));
1382 /* Disabling current address */
1383 asi = cur->solver_information;
1384 cur_net = asi->network ;
1385 asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
1386 cur->active = GNUNET_NO; /* No active any longer */
1387 cur->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1388 cur->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1390 if (GNUNET_SYSERR == addresse_decrement (s, cur_net, GNUNET_NO, GNUNET_YES))
1393 distribute_bandwidth_in_network (s, cur_net );
1400 * Remove an address from the solver
1402 * @param solver the solver handle
1403 * @param address the address to remove
1404 * @param session_only delete only session not whole address
1407 GAS_proportional_address_delete (void *solver,
1408 struct ATS_Address *address,
1411 struct GAS_PROPORTIONAL_Handle *s = solver;
1412 struct Network *net;
1413 struct AddressWrapper *aw;
1414 struct AddressSolverInformation *asi;
1416 /* Remove an adress completely, we have to:
1417 * - Remove from specific network
1418 * - Decrease number of total addresses
1420 * - decrease number of active addreses
1423 asi = address->solver_information;
1432 if (GNUNET_NO == session_only)
1434 LOG(GNUNET_ERROR_TYPE_INFO,
1435 "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1436 (GNUNET_NO == address->active) ? "inactive" : "active", address,
1437 GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1438 net->active_addresses);
1440 /* Remove address */
1441 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1442 for (aw = net->head; NULL != aw; aw = aw->next)
1444 if (aw->addr == address)
1452 GNUNET_CONTAINER_DLL_remove(net->head, net->tail, aw);
1457 /* Remove session only: remove if active and update */
1458 LOG(GNUNET_ERROR_TYPE_INFO,
1459 "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1460 (GNUNET_NO == address->active) ? "inactive" : "active", address,
1461 GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1462 net->active_addresses);
1465 if (GNUNET_YES == address->active)
1467 /* Address was active, remove from network and update quotas*/
1468 address->active = GNUNET_NO;
1469 address->assigned_bw_in = BANDWIDTH_ZERO;
1470 address->assigned_bw_out = BANDWIDTH_ZERO;
1471 asi->calculated_quota_in_NBO = htonl (0);
1472 asi->calculated_quota_out_NBO = htonl (0);
1474 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1476 distribute_bandwidth_in_network (s, net);
1478 if (NULL == update_active_address (s, &address->peer))
1480 /* No alternative address found, disconnect peer */
1481 LOG (GNUNET_ERROR_TYPE_INFO,
1482 "Disconnecting peer `%s' after deleting previous %s address %p\n",
1483 GNUNET_i2s (&address->peer),
1484 (GNUNET_NO == address->active) ? "inactive" : "active",
1486 s->bw_changed (s->bw_changed_cls, address);
1489 if (GNUNET_NO == session_only)
1491 GNUNET_free_non_null (address->solver_information);
1492 address->solver_information = NULL;
1495 LOG(GNUNET_ERROR_TYPE_INFO,
1496 "After deleting address now total %u and active %u addresses in network `%s'\n",
1497 net->total_addresses, net->active_addresses, net->desc);
1503 * Start a bulk operation
1505 * @param solver the solver
1508 GAS_proportional_bulk_start (void *solver)
1510 struct GAS_PROPORTIONAL_Handle *s = solver;
1512 LOG (GNUNET_ERROR_TYPE_DEBUG,
1513 "Locking solver for bulk operation ...\n");
1514 GNUNET_assert(NULL != solver);
1520 * Bulk operation done
1523 GAS_proportional_bulk_stop (void *solver)
1525 struct GAS_PROPORTIONAL_Handle *s = solver;
1527 LOG (GNUNET_ERROR_TYPE_DEBUG,
1528 "Unlocking solver from bulk operation ...\n");
1529 GNUNET_assert(NULL != solver);
1531 if (s->bulk_lock < 1)
1537 if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1539 LOG(GNUNET_ERROR_TYPE_INFO, "No lock pending, recalculating\n");
1540 distribute_bandwidth_in_network (s, NULL);
1541 s->bulk_requests = 0;
1547 * Add a new single address to a network
1549 * @param solver the solver Handle
1550 * @param address the address to add
1551 * @param network network type of this address
1554 GAS_proportional_address_add (void *solver,
1555 struct ATS_Address *address,
1560 * Transport properties for this address have changed
1562 * @param solver solver handle
1563 * @param address the address
1564 * @param type the ATSI type in HBO
1565 * @param abs_value the absolute value of the property
1566 * @param rel_value the normalized value
1569 GAS_proportional_address_property_changed (void *solver,
1570 struct ATS_Address *address,
1575 struct GAS_PROPORTIONAL_Handle *s = solver;
1577 struct AddressSolverInformation *asi;
1578 struct ATS_Address *best_address;
1579 struct ATS_Address *active_address;
1581 GNUNET_assert(NULL != s);
1582 GNUNET_assert(NULL != address);
1583 asi = address->solver_information;
1597 LOG(GNUNET_ERROR_TYPE_INFO,
1598 "Property `%s' for peer `%s' address %p changed to %.2f \n",
1599 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1600 address, rel_value);
1602 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1603 return; /* Peer is not requested */
1605 /* This peer is requested, find active and best address */
1606 active_address = get_active_address(s, s->addresses, &address->peer);
1607 best_address = (struct ATS_Address *) update_active_address (s, &address->peer);
1609 if ((NULL != best_address) && ((NULL != active_address) &&
1610 (GNUNET_YES == address_eq (active_address, best_address))))
1612 asi = best_address->solver_information;
1613 GNUNET_assert (NULL != asi);
1615 /* We sticked to the same address, therefore redistribute */
1616 distribute_bandwidth_in_network (s, asi->network);
1621 * Transport session for this address has changed
1623 * NOTE: values in addresses are already updated
1625 * @param solver solver handle
1626 * @param address the address
1627 * @param cur_session the current session
1628 * @param new_session the new session
1631 GAS_proportional_address_session_changed (void *solver,
1632 struct ATS_Address *address,
1633 uint32_t cur_session,
1634 uint32_t new_session)
1636 struct GAS_PROPORTIONAL_Handle *s = solver;
1637 struct ATS_Address *best_address;
1638 struct ATS_Address *active_address;
1639 struct AddressSolverInformation *asi;
1641 if (cur_session != new_session)
1643 LOG (GNUNET_ERROR_TYPE_DEBUG,
1644 "Session changed from %u to %u\n",
1649 if (NULL == address->solver_information)
1655 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1656 return; /* Peer is not requested */
1658 /* This peer is requested, find active and best address */
1659 active_address = get_active_address(s, s->addresses, &address->peer);
1660 best_address = (struct ATS_Address *) update_active_address (s, &address->peer);
1662 if ((NULL != best_address) && ((NULL != active_address) &&
1663 (GNUNET_YES == address_eq (active_address, best_address))))
1665 asi = best_address->solver_information;
1666 GNUNET_assert (NULL != asi);
1668 /* We sticked to the same address, therefore redistribute */
1669 distribute_bandwidth_in_network (s, asi->network);
1675 * Usage for this address has changed
1677 * NOTE: values in addresses are already updated
1679 * @param solver solver handle
1680 * @param address the address
1681 * @param in_use usage state
1684 GAS_proportional_address_inuse_changed (void *solver,
1685 struct ATS_Address *address,
1688 struct GAS_PROPORTIONAL_Handle *s = solver;
1690 struct AddressSolverInformation *asi;
1691 struct ATS_Address *best_address;
1692 struct ATS_Address *active_address;
1694 GNUNET_assert(NULL != s);
1695 GNUNET_assert(NULL != address);
1696 asi = address->solver_information;
1710 LOG(GNUNET_ERROR_TYPE_INFO,
1711 "Usage of peer `%s' address %p changed to %s \n",
1712 GNUNET_i2s (&address->peer), address,
1713 (GNUNET_YES == in_use) ? "YES" : "NO");
1715 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1716 return; /* Peer is not requested */
1718 /* This peer is requested, find active and best address */
1719 active_address = get_active_address(s, s->addresses, &address->peer);
1720 best_address = (struct ATS_Address *) update_active_address (s, &address->peer);
1722 if ((NULL != best_address) && ((NULL != active_address) &&
1723 (GNUNET_YES == address_eq (active_address, best_address))))
1725 asi = best_address->solver_information;
1726 GNUNET_assert (NULL != asi);
1728 /* We sticked to the same address, therefore redistribute */
1729 distribute_bandwidth_in_network (s, asi->network);
1734 * Network scope for this address has changed
1736 * NOTE: values in addresses are already updated
1738 * @param solver solver handle
1739 * @param address the address
1740 * @param current_network the current network
1741 * @param new_network the new network
1744 GAS_proportional_address_change_network (void *solver,
1745 struct ATS_Address *address,
1746 uint32_t current_network,
1747 uint32_t new_network)
1749 struct GAS_PROPORTIONAL_Handle *s = solver;
1750 struct AddressSolverInformation *asi;
1751 int save_active = GNUNET_NO;
1753 if (current_network == new_network)
1759 asi = address->solver_information;
1766 /* Network changed */
1767 LOG(GNUNET_ERROR_TYPE_DEBUG,
1768 "Network type changed, moving %s address from `%s' to `%s'\n",
1769 (GNUNET_YES == address->active) ? "active" : "inactive",
1770 GNUNET_ATS_print_network_type (current_network),
1771 GNUNET_ATS_print_network_type (new_network));
1774 /* Start bulk to prevent disconnect */
1775 GAS_proportional_bulk_start(s);
1777 save_active = address->active;
1779 /* Disable and assign no bandwidth */
1780 address->active = GNUNET_NO;
1781 address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1782 address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1784 /* Remove from old network */
1785 GAS_proportional_address_delete (solver, address, GNUNET_NO);
1787 /* Set new network type */
1788 if (NULL == get_network (solver, new_network))
1790 /* Address changed to invalid network... */
1791 LOG(GNUNET_ERROR_TYPE_ERROR,
1792 _("Invalid network type `%u' `%s': Disconnect!\n"), new_network,
1793 GNUNET_ATS_print_network_type (new_network));
1794 s->bw_changed (s->bw_changed_cls, address);
1798 /* Add to new network and update*/
1799 GAS_proportional_address_add (solver, address, new_network);
1801 GAS_proportional_bulk_stop (s);
1803 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1804 return; /* Peer is not requested */
1806 /* Find new address to suggest */
1807 if (GNUNET_YES == save_active)
1809 /* No address available, therefore disconnect */
1810 if (NULL == update_active_address (s, &address->peer))
1811 s->bw_changed (s->bw_changed_cls, address);
1817 * Add a new single address to a network
1819 * @param solver the solver Handle
1820 * @param address the address to add
1821 * @param network network type of this address
1824 GAS_proportional_address_add (void *solver,
1825 struct ATS_Address *address,
1828 struct GAS_PROPORTIONAL_Handle *s = solver;
1829 struct Network *net = NULL;
1830 struct AddressWrapper *aw = NULL;
1831 struct AddressSolverInformation *asi;
1833 GNUNET_assert(NULL != s);
1834 net = get_network (s, network);
1839 LOG(GNUNET_ERROR_TYPE_ERROR,
1840 "Unknown network %u `%s' for new address %p for peer `%s'\n",
1841 network, GNUNET_ATS_print_network_type(network),
1842 address, GNUNET_i2s(&address->peer));
1847 aw = GNUNET_new (struct AddressWrapper);
1849 GNUNET_CONTAINER_DLL_insert(net->head, net->tail, aw);
1850 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1852 asi = GNUNET_new (struct AddressSolverInformation);
1854 asi->calculated_quota_in_NBO = htonl (0);
1855 asi->calculated_quota_out_NBO = htonl (0);
1856 aw->addr->solver_information = asi;
1858 LOG(GNUNET_ERROR_TYPE_INFO,
1859 "Adding new address %p for peer `%s', now total %u and active %u addresses in network `%s'\n",
1860 address, GNUNET_i2s(&address->peer), net->total_addresses, net->active_addresses, net->desc);
1862 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1863 return; /* Peer is not requested */
1865 /* This peer is requested, find best address */
1866 update_active_address (s, &address->peer);
1872 * Function invoked when the plugin is loaded.
1874 * @param cls[in,out] the `struct GNUNET_ATS_PluginEnvironment *` to use;
1875 * modified to return the API functions (ugh).
1876 * @return the `struct GAS_PROPORTIONAL_Handle` to pass as a closure
1879 libgnunet_plugin_ats_proportional_init (void *cls)
1881 struct GNUNET_ATS_PluginEnvironment *env = cls;
1882 struct GAS_PROPORTIONAL_Handle *s;
1883 struct Network * cur;
1884 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1888 GNUNET_assert (NULL != env);
1889 GNUNET_assert (NULL != env->cfg);
1890 GNUNET_assert (NULL != env->bandwidth_changed_cb);
1891 GNUNET_assert (NULL != env->get_preferences);
1892 GNUNET_assert (NULL != env->get_property);
1894 s = GNUNET_new (struct GAS_PROPORTIONAL_Handle);
1896 env->sf.s_add = &GAS_proportional_address_add;
1897 env->sf.s_address_update_property = &GAS_proportional_address_property_changed;
1898 env->sf.s_address_update_session = &GAS_proportional_address_session_changed;
1899 env->sf.s_address_update_inuse = &GAS_proportional_address_inuse_changed;
1900 env->sf.s_address_update_network = &GAS_proportional_address_change_network;
1901 env->sf.s_get = &GAS_proportional_get_preferred_address;
1902 env->sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
1903 env->sf.s_pref = &GAS_proportional_address_change_preference;
1904 env->sf.s_feedback = &GAS_proportional_address_preference_feedback;
1905 env->sf.s_del = &GAS_proportional_address_delete;
1906 env->sf.s_bulk_start = &GAS_proportional_bulk_start;
1907 env->sf.s_bulk_stop = &GAS_proportional_bulk_stop;
1909 s->stats = (struct GNUNET_STATISTICS_Handle *) env->stats;
1910 s->bw_changed = env->bandwidth_changed_cb;
1911 s->bw_changed_cls = env->bw_changed_cb_cls;
1912 s->get_preferences = env->get_preferences;
1913 s->get_preferences_cls = env->get_preference_cls;
1914 s->get_properties = env->get_property;
1915 s->get_properties_cls = env->get_property_cls;
1916 s->network_count = env->network_count;
1917 s->network_entries = GNUNET_malloc (env->network_count * sizeof (struct Network));
1920 s->active_addresses = 0;
1921 s->total_addresses = 0;
1922 s->bulk_lock = GNUNET_NO;
1923 s->addresses = env->addresses;
1924 s->requests = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1926 s->stability_factor = PROP_STABILITY_FACTOR;
1927 if (GNUNET_SYSERR !=
1928 GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
1929 "PROP_STABILITY_FACTOR", &f_tmp))
1931 if ((f_tmp < 1.0) || (f_tmp > 2.0))
1933 LOG (GNUNET_ERROR_TYPE_ERROR,
1934 _("Invalid %s configuration %f \n"),
1935 "PROP_STABILITY_FACTOR",
1940 s->stability_factor = f_tmp;
1941 LOG (GNUNET_ERROR_TYPE_INFO,
1942 "Using %s of %.3f\n",
1943 "PROP_STABILITY_FACTOR", f_tmp);
1947 s->prop_factor = PROPORTIONALITY_FACTOR;
1948 if (GNUNET_SYSERR !=
1949 GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
1950 "PROP_STABILITY_FACTOR", &f_tmp))
1954 LOG (GNUNET_ERROR_TYPE_ERROR,
1955 _("Invalid %s configuration %f \n"),
1956 "PROP_PROPORTIONALITY_FACTOR", f_tmp);
1960 s->prop_factor = f_tmp;
1961 LOG (GNUNET_ERROR_TYPE_INFO,
1962 "Using %s of %.3f\n",
1963 "PROP_PROPORTIONALITY_FACTOR", f_tmp);
1967 for (c = 0; c < env->network_count; c++)
1969 cur = &s->network_entries[c];
1970 cur->total_addresses = 0;
1971 cur->active_addresses = 0;
1972 cur->type = env->networks[c];
1973 cur->total_quota_in = env->in_quota[c];
1974 cur->total_quota_out = env->out_quota[c];
1975 cur->desc = net_str[c];
1976 GNUNET_asprintf (&cur->stat_total,
1977 "# ATS addresses %s total", cur->desc);
1978 GNUNET_asprintf (&cur->stat_active,
1979 "# ATS active addresses %s total", cur->desc);
1980 LOG (GNUNET_ERROR_TYPE_INFO,
1981 "Added network %u `%s' (%llu/%llu)\n",
1982 c, cur->desc, cur->total_quota_in, cur->total_quota_out);
1988 /* end of plugin_ats_proportional.c */