2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file ats/gnunet-service-ats-solver_proportional.c
23 * @brief ATS proportional solver
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet_statistics_service.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
36 * NOTE: Do not change this documentation. This documentation is based
37 * on gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
38 * use build_txt.sh to generate plaintext output
40 * ATS addresses : simplistic solver
42 * The simplistic solver ("simplistic") distributes the available
43 * bandwidth fair over all the addresses influenced by the
44 * preference values. For each available network type an in- and
45 * outbound quota is configured and the bandwidth available in
46 * these networks is distributed over the addresses. The solver
47 * first assigns every addresses the minimum amount of bandwidth
48 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
49 * remaining bandwidth available according to the preference
50 * values. For each peer only a single address gets bandwidth
51 * assigned and only one address marked as active. The most
52 * important functionality for the solver is implemented in: *
53 * find_address_it is an hashmap iterator returning the prefered
54 * address for an peer * update_quota_per_network distributes
55 * available bandwidth for a network over active addresses
57 * Changes to addresses automatically have an impact on the the
58 * bandwidth assigned to other addresses in the same network since
59 * the solver distributes the remaining bandwidth over the
60 * addresses in the network. When changes to the addresses occur,
61 * the solver first performs the changes, like adding or deleting
62 * addresses, and then updates bandwidth assignment for the
63 * affected network. Bandwidth assignment is only recalculated on
64 * demand when an address is requested by a client for a peer or
65 * when the addresses available have changed or an address changed
66 * the network it is located in. When the bandwidth assignment has
67 * changed the callback is called with the new bandwidth
68 * assignments. The bandwidth distribution for a network is
69 * recalculated due to: * address suggestion requests * address
70 * deletions * address switching networks during address update *
73 * 3.1 Data structures used
75 * For each ATS network (e.g. WAN, LAN, loopback) a struct Network
76 * is used to specify network related information as total adresses
77 * and active addresses in this network and the configured in- and
78 * outbound quota. Each network also contains a list of addresses
79 * added to the solver located in this network. The simplistic
80 * solver uses the addresses' solver_information field to store the
81 * simplistic network it belongs to for each address.
85 * When the simplistic solver is initialized the solver creates a
86 * new solver handle and initializes the network structures with
87 * the quotas passed from addresses and returns the handle solver.
89 * 3.3 Adding an address
91 * When a new address is added to the solver using s_add, a lookup
92 * for the network for this address is done and the address is
93 * enqueued in in the linked list of the network.
95 * 3.4 Updating an address
97 * The main purpose of address updates is to update the ATS
98 * information for addresse selection. Important for the simplistic
99 * solver is when an address switches network it is located
100 * in. This is common because addresses added by transport's
101 * validation mechanism are commonly located in
102 * GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
103 * in this network type and only if a connection is successful on
104 * return of payload data transport switches to the real network
105 * the address is located in. When an address changes networks it
106 * is first of all removed from the old network using the solver
107 * API function GAS_simplistic_address_delete and the network in
108 * the address struct is updated. A lookup for the respective new
109 * simplistic network is done and stored in the addresse's
110 * solver_information field. Next the address is re-added to the
111 * solver using the solver API function
112 * GAS_simplistic_address_add. If the address was marked as in
113 * active, the solver checks if bandwidth is available in the
114 * network and if yes sets the address to active and updates the
115 * bandwidth distribution in this network. If no bandwidth is
116 * available it sets the bandwidth for this address to 0 and tries
117 * to suggest an alternative address. If an alternative address was
118 * found, addresses' callback is called for this address.
120 * 3.5 Deleting an address
122 * When an address is removed from the solver, it removes the
123 * respective address from the network and if the address was
124 * marked as active, it updates the bandwidth distribution for this
127 * 3.6 Requesting addresses
129 * When an address is requested for a peer the solver performs a
130 * lookup for the peer entry in addresses address hashmap and
131 * selects the best address. The selection of the most suitable
132 * address is done in the find_address_it hashmap iterator
133 * described in detail in section 3.7. If no address is returned,
134 * no address can be suggested at the moment. If the address
135 * returned is marked as active, the solver can return this
136 * address. If the address is not marked as active, the solver
137 * checks if another address belongign to this peer is marked as
138 * active and marks the address as inactive, updates the bandwidth
139 * for this address to 0, call the bandwidth changed callback for
140 * this address due to the change and updates quota assignment for
141 * the addresse's network. the now in-active address is belonging
142 * to. The solver marks the new address as active and updates the
143 * bandwidth assignment for this network.
145 * 3.7 Choosing addresses
147 * Choosing the best possible address for suggestion is done by
148 * iterating over all addresses of a peer stored in addresses'
149 * hashmap and using the hashmap iterator find_address_it to select
150 * the best available address. Several checks are done when an
151 * address is selected. First if this address is currently blocked
152 * by addresses from being suggested. An address is blocked for the
153 * duration of ATS_BLOCKING_DELTA when it is suggested to
154 * transport. Next it is checked if at least
155 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
156 * in the addresse's network, because suggesting an address without
157 * bandwidth does not make sense. This also ensures that all active
158 * addresses in this network get at least the minimum amount of
159 * bandwidth assigned. In the next step the solver ensures that for
160 * tcp connections inbound connections are prefered over outbound
161 * connections. In the next stet the solver ensures that
162 * connections are prefered in the following order: * connections
163 * are already established and have bandwidth assigned *
164 * connections with a shorter distance * connectes have a shorter
167 * 3.8 Changing preferences
171 * During shutdown all network entries and aging processes are
172 * destroyed and freed.
177 * This solver assigns in and outbound bandwidth equally for all
178 * addresses in specific network type (WAN, LAN) based on configured
179 * in and outbound quota for this network.
181 * The solver is notified by addresses about changes to the addresses
182 * and recalculates the bandwith assigned if required. The solver
183 * notifies addresses by calling the GAS_bandwidth_changed_cb
191 * For each peer only a single is selected and marked as "active" in the address
196 * You have the networks WAN and LAN and quotas
197 * WAN_TOTAL_IN, WAN_TOTAL_OUT
198 * LAN_TOTAL_IN, LAN_TOTAL_OUT
200 * If you have x addresses in the network segment LAN, the quotas are
201 * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
203 * Quotas are automatically recalculated and reported back when addresses are
208 #define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
209 #define PREF_AGING_FACTOR 0.95
211 #define DEFAULT_REL_PREFERENCE 1.0
212 #define DEFAULT_ABS_PREFERENCE 0.0
213 #define MIN_UPDATE_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
217 * A handle for the proportional solver
219 struct GAS_PROPORTIONAL_Handle
224 struct GNUNET_STATISTICS_Handle *stats;
227 * Hashmap containing all valid addresses
229 const struct GNUNET_CONTAINER_MultiHashMap *addresses;
232 * Bandwidth changed callback
234 GAS_bandwidth_changed_cb bw_changed;
237 * Bandwidth changed callback cls
239 void *bw_changed_cls;
242 * ATS function to get preferences
244 GAS_get_preferences get_preferences;
247 * Closure for ATS function to get preferences
249 void *get_preferences_cls;
252 * ATS function to get properties
254 GAS_get_properties get_properties;
257 * Closure for ATS function to get properties
259 void *get_properties_cls;
267 * Number of changes while solver was locked
273 * Total number of addresses for solver
275 unsigned int total_addresses;
278 * Number of active addresses for solver
280 unsigned int active_addresses;
285 struct Network *network_entries;
290 unsigned int networks;
296 * Representation of a network
306 * Network description
311 * Total inbound quota
314 unsigned long long total_quota_in;
317 * Total outbound quota
320 unsigned long long total_quota_out;
323 * Number of active addresses for this network
325 unsigned int active_addresses;
328 * Number of total addresses for this network
330 unsigned int total_addresses;
333 * String for statistics total addresses
338 * String for statistics active addresses
342 struct AddressWrapper *head;
343 struct AddressWrapper *tail;
348 * Wrapper for addresses to store them in network's linked list
350 struct AddressWrapper
355 struct AddressWrapper *next;
360 struct AddressWrapper *prev;
365 struct ATS_Address *addr;
371 * Important solver functions
372 * ---------------------------
376 * Test if bandwidth is available in this network to add an additional address
378 * @param net the network type to update
379 * @return GNUNET_YES or GNUNET_NO
382 is_bandwidth_available_in_network (struct Network *net)
384 GNUNET_assert (NULL != net);
385 unsigned int na = net->active_addresses + 1;
386 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
387 if (((net->total_quota_in / na) > min_bw) &&
388 ((net->total_quota_out / na) > min_bw))
390 LOG (GNUNET_ERROR_TYPE_DEBUG,
391 "Enough bandwidth available for %u active addresses in network `%s'\n",
397 LOG (GNUNET_ERROR_TYPE_DEBUG,
398 "Not enough bandwidth available for %u active addresses in network `%s'\n",
406 * Update bandwidth assigned to peers in this network
408 * @param s the solver handle
409 * @param net the network type to update
410 * @param address_except address excluded from notification, since we suggest
414 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
416 struct ATS_Address *address_except)
418 unsigned long long remaining_quota_in = 0;
419 unsigned long long quota_out_used = 0;
421 unsigned long long remaining_quota_out = 0;
422 unsigned long long quota_in_used = 0;
423 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
425 double total_prefs; /* Important: has to be double not float due to precision */
426 double cur_pref; /* Important: has to be double not float due to precision */
427 const double *t = NULL; /* Important: has to be double not float due to precision */
430 unsigned long long assigned_quota_in = 0;
431 unsigned long long assigned_quota_out = 0;
432 struct AddressWrapper *cur;
434 if (GNUNET_YES == s->bulk_lock)
440 LOG (GNUNET_ERROR_TYPE_DEBUG,
441 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
442 net->desc, net->active_addresses, net->total_quota_in, net->total_quota_in);
444 if (net->active_addresses == 0)
445 return; /* no addresses to update */
448 * Assign every peer in network minimum Bandwidth
449 * Distribute bandwidth left according to preference
452 if ((net->active_addresses * min_bw) > net->total_quota_in)
457 if ((net->active_addresses * min_bw) > net->total_quota_out)
463 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
464 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
465 LOG (GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
466 remaining_quota_in, remaining_quota_out);
468 for (cur = net->head; NULL != cur; cur = cur->next)
470 if (GNUNET_YES == cur->addr->active)
472 GNUNET_assert (NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
475 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
477 if (c != GNUNET_ATS_PREFERENCE_END)
479 //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
483 total_prefs += (peer_prefs / (GNUNET_ATS_PreferenceCount -1));
486 for (cur = net->head; NULL != cur; cur = cur->next)
488 if (GNUNET_YES == cur->addr->active)
491 GNUNET_assert (NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
493 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
495 if (c != GNUNET_ATS_PREFERENCE_END)
500 assigned_quota_in = min_bw + ((cur_pref / total_prefs) * remaining_quota_in);
501 assigned_quota_out = min_bw + ((cur_pref / total_prefs) * remaining_quota_out);
503 LOG (GNUNET_ERROR_TYPE_DEBUG,
504 "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
505 GNUNET_i2s (&cur->addr->peer),
506 cur_pref, total_prefs,
507 assigned_quota_in, assigned_quota_out);
511 assigned_quota_in = 0;
512 assigned_quota_out = 0;
515 quota_in_used += assigned_quota_in;
516 quota_out_used += assigned_quota_out;
517 /* Prevent overflow due to rounding errors */
518 if (assigned_quota_in > UINT32_MAX)
519 assigned_quota_in = UINT32_MAX;
520 if (assigned_quota_out > UINT32_MAX)
521 assigned_quota_out = UINT32_MAX;
523 /* Compare to current bandwidth assigned */
524 if ((assigned_quota_in != ntohl(cur->addr->assigned_bw_in.value__)) ||
525 (assigned_quota_out != ntohl(cur->addr->assigned_bw_out.value__)))
527 cur->addr->assigned_bw_in.value__ = htonl (assigned_quota_in);
528 cur->addr->assigned_bw_out.value__ = htonl (assigned_quota_out);
529 /* Notify on change */
530 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
531 s->bw_changed (s->bw_changed_cls, cur->addr);
535 LOG (GNUNET_ERROR_TYPE_DEBUG,
536 "Total bandwidth assigned is (in/out): %llu /%llu\n",
539 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
541 LOG (GNUNET_ERROR_TYPE_ERROR,
542 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
543 net->active_addresses,
545 net->total_quota_out);
547 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
549 LOG (GNUNET_ERROR_TYPE_ERROR,
550 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
551 net->active_addresses,
553 net->total_quota_in);
558 struct FindBestAddressCtx
560 struct GAS_PROPORTIONAL_Handle *s;
561 struct ATS_Address *best;
566 find_property_index (uint32_t type)
568 int existing_types[] = GNUNET_ATS_QualityProperties;
570 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
571 if (existing_types[c] == type)
573 return GNUNET_SYSERR;
577 * Find a "good" address to use for a peer by iterating over the addresses for this peer.
578 * If we already have an existing address, we stick to it.
579 * Otherwise, we pick by lowest distance and then by lowest latency.
581 * @param cls the 'struct ATS_Address**' where we store the result
583 * @param value another 'struct ATS_Address*' to consider using
584 * @return GNUNET_OK (continue to iterate)
587 find_best_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
589 struct FindBestAddressCtx *fba_ctx = (struct FindBestAddressCtx *) cls;
590 struct ATS_Address *current = (struct ATS_Address *) value;
591 struct GNUNET_TIME_Absolute now;
592 struct Network *net = (struct Network *) current->solver_information;
593 const double *norm_prop_cur;
594 const double *norm_prop_prev;
597 now = GNUNET_TIME_absolute_get();
599 if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
601 /* This address is blocked for suggestion */
602 LOG (GNUNET_ERROR_TYPE_DEBUG,
603 "Address %p blocked for suggestion for %llu ms \n",
605 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
608 if (GNUNET_NO == is_bandwidth_available_in_network (net))
609 return GNUNET_OK; /* There's no bandwidth available in this network */
610 if (NULL != fba_ctx->best)
612 GNUNET_assert (NULL != fba_ctx->best->plugin);
613 GNUNET_assert (NULL != current->plugin);
614 if (0 == strcmp (fba_ctx->best->plugin, current->plugin))
616 if ((0 != fba_ctx->best->addr_len) &&
617 (0 == current->addr_len))
619 /* saved address was an outbound address, but we have an inbound address */
620 fba_ctx->best = current;
623 if (0 == fba_ctx->best->addr_len)
625 /* saved address was an inbound address, so do not overwrite */
630 if (NULL == fba_ctx->best)
632 fba_ctx->best = current;
635 if ((ntohl (fba_ctx->best->assigned_bw_in.value__) == 0) &&
636 (ntohl (current->assigned_bw_in.value__) > 0))
638 /* stick to existing connection */
639 fba_ctx->best = current;
643 norm_prop_cur = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
644 (const struct ATS_Address *) current);
645 norm_prop_prev = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
646 (const struct ATS_Address *) fba_ctx->best);
648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s previous %.2f current %.2f\n",
649 "DISTANCE", norm_prop_cur[1], norm_prop_cur[1]);
651 index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
652 if (GNUNET_SYSERR == index)
657 if (norm_prop_cur[index] < norm_prop_prev[index])
659 /* user shorter distance */
660 fba_ctx->best = current;
664 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s previous %.2f current %.2f\n",
665 "DELAY", norm_prop_cur[1], norm_prop_cur[1]);
667 index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
668 if (GNUNET_SYSERR == index)
673 if (norm_prop_cur[index] < norm_prop_prev[index])
675 /* user shorter delay */
676 fba_ctx->best = current;
686 * ---------------------------
690 * Update bandwidth assignment for all networks
692 * @param s the solver handle
695 distribute_bandwidth_in_all_networks (struct GAS_PROPORTIONAL_Handle *s)
699 for (i = 0; i < s->networks; i++)
700 distribute_bandwidth_in_network (s, &s->network_entries[i], NULL);
706 * Lookup network struct by type
708 * @param s the solver handle
709 * @param type the network type
710 * @return the network struct
712 static struct Network *
713 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
716 for (c = 0 ; c < s->networks; c++)
718 if (s->network_entries[c].type == type)
719 return &s->network_entries[c];
727 * Hashmap Iterator to find current active address for peer
729 * @param cls last active address
730 * @param key peer's key
731 * @param value address to check
732 * @return GNUNET_NO on double active address else GNUNET_YES;
735 get_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
737 struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
738 struct ATS_Address * aa = (struct ATS_Address *) value;
740 if (GNUNET_YES == aa->active)
744 /* should never happen */
745 LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
756 * Find current active address for peer
758 * @param solver the solver handle
759 * @param addresses the address set
760 * @param peer the peer
761 * @return active address or NULL
763 static struct ATS_Address *
764 get_active_address (void *solver,
765 struct GNUNET_CONTAINER_MultiHashMap * addresses,
766 const struct GNUNET_PeerIdentity *peer)
768 struct ATS_Address * dest = NULL;
770 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
772 &get_active_address_it, &dest);
779 addresse_increment (struct GAS_PROPORTIONAL_Handle *s,
784 if (GNUNET_YES == total)
786 s->total_addresses ++;
787 net->total_addresses ++;
788 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
789 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
791 if (GNUNET_YES == active)
793 net->active_addresses ++;
794 s->active_addresses ++;
795 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
796 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
803 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s,
809 if (GNUNET_YES == total)
811 if (s->total_addresses < 1)
818 s->total_addresses --;
819 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
821 if (net->total_addresses < 1)
828 net->total_addresses --;
829 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
833 if (GNUNET_YES == active)
835 if (net->active_addresses < 1)
842 net->active_addresses --;
843 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
845 if (s->active_addresses < 1)
852 s->active_addresses --;
853 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
861 * Solver API functions
862 * ---------------------------
866 * Changes the preferences for a peer in the problem
868 * @param solver the solver handle
869 * @param peer the peer to change the preference for
870 * @param kind the kind to change the preference
871 * @param pref_rel the normalized preference value for this kind over all clients
874 GAS_proportional_address_change_preference (void *solver,
875 const struct GNUNET_PeerIdentity *peer,
876 enum GNUNET_ATS_PreferenceKind kind,
879 struct GAS_PROPORTIONAL_Handle *s = solver;
880 GNUNET_assert (NULL != solver);
881 GNUNET_assert (NULL != peer);
883 distribute_bandwidth_in_all_networks (s);
888 * Get the preferred address for a specific peer
890 * @param solver the solver handle
891 * @param addresses the address hashmap containing all addresses
892 * @param peer the identity of the peer
894 const struct ATS_Address *
895 GAS_proportional_get_preferred_address (void *solver,
896 const struct GNUNET_PeerIdentity *peer)
898 struct GAS_PROPORTIONAL_Handle *s = solver;
899 struct Network *net_prev;
900 struct Network *net_cur;
901 struct ATS_Address *prev;
902 struct FindBestAddressCtx fba_ctx;
904 GNUNET_assert (s != NULL);
905 GNUNET_assert (peer != NULL);
907 /* Get address with: stick to current address, lower distance, lower latency */
911 GNUNET_CONTAINER_multihashmap_get_multiple (s->addresses, &peer->hashPubKey,
912 &find_best_address_it, &fba_ctx);
913 if (NULL == fba_ctx.best)
915 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
919 LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
920 (GNUNET_NO == fba_ctx.best->active) ? "inactive" : "active",
921 fba_ctx.best, GNUNET_i2s (peer));
922 net_cur = (struct Network *) fba_ctx.best->solver_information;
923 if (NULL == fba_ctx.best)
925 LOG (GNUNET_ERROR_TYPE_ERROR, "Trying to suggesting unknown address peer `%s'\n",
930 if (GNUNET_YES == fba_ctx.best->active)
932 /* This address was selected previously, so no need to update quotas */
936 /* This address was not active, so we have to:
938 * - mark previous active address as not active
939 * - update quota for previous address network
940 * - update quota for this address network
943 prev = get_active_address (s, (struct GNUNET_CONTAINER_MultiHashMap *) s->addresses, peer);
946 net_prev = (struct Network *) prev->solver_information;
947 prev->active = GNUNET_NO; /* No active any longer */
948 prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
949 prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
950 s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
951 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
953 distribute_bandwidth_in_network (s, net_prev, NULL);
956 if (GNUNET_NO == (is_bandwidth_available_in_network (fba_ctx.best->solver_information)))
958 GNUNET_break (0); /* This should never happen*/
962 fba_ctx.best->active = GNUNET_YES;
963 addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
964 distribute_bandwidth_in_network (s, net_cur, fba_ctx.best);
970 * Stop notifying about address and bandwidth changes for this peer
972 * @param solver the solver handle
973 * @param addresses address hashmap
974 * @param peer the peer
977 GAS_proportional_stop_get_preferred_address (void *solver,
978 const struct GNUNET_PeerIdentity *peer)
985 * Remove an address from the solver
987 * @param solver the solver handle
988 * @param addresses the address hashmap containing all addresses
989 * @param address the address to remove
990 * @param session_only delete only session not whole address
993 GAS_proportional_address_delete (void *solver,
994 struct ATS_Address *address,
997 struct GAS_PROPORTIONAL_Handle *s = solver;
999 struct AddressWrapper *aw;
1001 /* Remove an adress completely, we have to:
1002 * - Remove from specific network
1003 * - Decrease number of total addresses
1005 * - decrease number of active addreses
1009 net = (struct Network *) address->solver_information;
1011 if (GNUNET_NO == session_only)
1013 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1014 (GNUNET_NO == address->active) ? "inactive" : "active",
1015 address, GNUNET_i2s (&address->peer),
1016 net->desc, net->total_addresses, net->active_addresses);
1018 /* Remove address */
1019 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1020 for (aw = net->head; NULL != aw; aw = aw->next)
1022 if (aw->addr == address)
1030 GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
1035 /* Remove session only: remove if active and update */
1036 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1037 (GNUNET_NO == address->active) ? "inactive" : "active",
1038 address, GNUNET_i2s (&address->peer),
1039 net->desc, net->total_addresses, net->active_addresses);
1042 if (GNUNET_YES == address->active)
1044 /* Address was active, remove from network and update quotas*/
1045 address->active = GNUNET_NO;
1046 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1048 distribute_bandwidth_in_network (s, net, NULL);
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
1051 net->total_addresses,
1052 net->active_addresses,
1059 * Start a bulk operation
1061 * @param solver the solver
1064 GAS_proportional_bulk_start (void *solver)
1066 LOG (GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1067 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1069 GNUNET_assert (NULL != solver);
1074 * Bulk operation done
1077 GAS_proportional_bulk_stop (void *solver)
1079 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1081 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1082 GNUNET_assert (NULL != solver);
1084 if (s->bulk_lock < 1)
1090 if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1092 LOG (GNUNET_ERROR_TYPE_DEBUG, "No lock pending, recalculating\n");
1093 distribute_bandwidth_in_all_networks (s);
1094 s->bulk_requests = 0;
1100 * Add a new single address to a network
1102 * @param solver the solver Handle
1103 * @param address the address to add
1104 * @param network network type of this address
1107 GAS_proportional_address_add (void *solver,
1108 struct ATS_Address *address,
1113 GAS_proportional_address_property_changed (void *solver,
1114 struct ATS_Address *address,
1119 struct GAS_PROPORTIONAL_Handle *s;
1122 GNUNET_assert (NULL != solver);
1123 GNUNET_assert (NULL != address);
1125 s = (struct GAS_PROPORTIONAL_Handle *) solver;
1126 n = (struct Network *) address->solver_information;
1134 LOG (GNUNET_ERROR_TYPE_DEBUG,
1135 "Property `%s' for peer `%s' address %p changed to %.2f \n",
1136 GNUNET_ATS_print_property_type (type),
1137 GNUNET_i2s (&address->peer),
1138 address, rel_value);
1141 case GNUNET_ATS_UTILIZATION_UP:
1142 case GNUNET_ATS_UTILIZATION_DOWN:
1143 case GNUNET_ATS_QUALITY_NET_DELAY:
1144 case GNUNET_ATS_QUALITY_NET_DISTANCE:
1145 case GNUNET_ATS_COST_WAN:
1146 case GNUNET_ATS_COST_LAN:
1147 case GNUNET_ATS_COST_WLAN:
1148 distribute_bandwidth_in_network (s, n, GNUNET_NO);
1155 GAS_proportional_address_session_changed (void *solver,
1156 struct ATS_Address *address,
1157 uint32_t cur_session,
1158 uint32_t new_session)
1160 if (cur_session!= new_session)
1162 LOG (GNUNET_ERROR_TYPE_DEBUG,
1163 "Session changed from %u to %u\n", cur_session, new_session);
1168 GAS_proportional_address_inuse_changed (void *solver,
1169 struct ATS_Address *address,
1172 LOG (GNUNET_ERROR_TYPE_DEBUG,
1173 "Usage changed to %s\n",
1174 (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1178 GAS_proportional_address_change_network (void *solver,
1179 struct ATS_Address *address,
1180 uint32_t current_network,
1181 uint32_t new_network)
1183 struct ATS_Address *new;
1184 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1185 int save_active = GNUNET_NO;
1186 struct Network *new_net = NULL;
1188 if (current_network == new_network)
1194 /* Network changed */
1195 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
1196 (GNUNET_YES == address->active) ? "active" : "inactive",
1197 GNUNET_ATS_print_network_type (current_network),
1198 GNUNET_ATS_print_network_type (new_network));
1200 save_active = address->active;
1201 /* remove from old network */
1202 GAS_proportional_address_delete (solver, address, GNUNET_NO);
1204 /* set new network type */
1205 new_net = get_network (solver, new_network);
1206 if (NULL == new_net)
1208 /* Address changed to invalid network... */
1209 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot find network of type `%u' %s\n"),
1210 new_network, GNUNET_ATS_print_network_type (new_network));
1211 address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
1212 address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
1213 s->bw_changed (s->bw_changed_cls, address);
1216 address->solver_information = new_net;
1218 /* Add to new network and update*/
1219 GAS_proportional_address_add (solver, address, new_network);
1220 if (GNUNET_YES == save_active)
1222 /* check if bandwidth available in new network */
1223 if (GNUNET_YES == (is_bandwidth_available_in_network (new_net)))
1225 /* Suggest updated address */
1226 address->active = GNUNET_YES;
1227 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
1228 distribute_bandwidth_in_network (solver, new_net, NULL);
1232 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not enough bandwidth in new network, suggesting alternative address ..\n");
1234 /* Set old address to zero bw */
1235 address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
1236 address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
1237 s->bw_changed (s->bw_changed_cls, address);
1239 /* Find new address to suggest since no bandwidth in network*/
1240 new = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, &address->peer);
1243 /* Have an alternative address to suggest */
1244 s->bw_changed (s->bw_changed_cls, new);
1251 * Add a new single address to a network
1253 * @param solver the solver Handle
1254 * @param addresses the address hashmap containing all addresses
1255 * @param address the address to add
1256 * @param network network type of this address
1259 GAS_proportional_address_add (void *solver,
1260 struct ATS_Address *address,
1263 struct GAS_PROPORTIONAL_Handle *s = solver;
1264 struct Network *net = NULL;
1265 struct AddressWrapper *aw = NULL;
1266 GNUNET_assert (NULL != s);
1268 net = get_network (s, network);
1275 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
1277 GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
1278 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1279 aw->addr->solver_information = net;
1281 LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
1282 net->total_addresses,
1283 net->active_addresses,
1289 * Init the proportional problem solver
1292 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1293 * out_quota[i] contains outbound quota for network type i
1294 * in_quota[i] contains inbound quota for network type i
1297 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1298 * network[2] == GNUNET_ATS_NET_LAN
1299 * out_quota[2] == 65353
1300 * in_quota[2] == 65353
1302 * @param cfg configuration handle
1303 * @param stats the GNUNET_STATISTICS handle
1304 * @param addresses hashmap containing all addresses
1305 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1306 * @param out_quota array of outbound quotas
1307 * @param in_quota array of outbound quota
1308 * @param dest_length array length for quota arrays
1309 * @param bw_changed_cb callback for changed bandwidth amounts
1310 * @param bw_changed_cb_cls cls for callback
1311 * @param get_preference callback to get relative preferences for a peer
1312 * @param get_preference_cls cls for callback to get relative preferences
1313 * @return handle for the solver on success, NULL on fail
1316 GAS_proportional_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1317 const struct GNUNET_STATISTICS_Handle *stats,
1318 const struct GNUNET_CONTAINER_MultiHashMap *addresses,
1320 unsigned long long *out_quota,
1321 unsigned long long *in_quota,
1323 GAS_bandwidth_changed_cb bw_changed_cb,
1324 void *bw_changed_cb_cls,
1325 GAS_get_preferences get_preference,
1326 void *get_preference_cls,
1327 GAS_get_properties get_properties,
1328 void *get_properties_cls)
1331 struct GAS_PROPORTIONAL_Handle *s = GNUNET_malloc (sizeof (struct GAS_PROPORTIONAL_Handle));
1332 struct Network * cur;
1333 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1335 GNUNET_assert (NULL != cfg);
1336 GNUNET_assert (NULL != stats);
1337 GNUNET_assert (NULL != network);
1338 GNUNET_assert (NULL != bw_changed_cb);
1339 GNUNET_assert (NULL != get_preference);
1340 GNUNET_assert (NULL != get_properties);
1342 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1343 s->bw_changed = bw_changed_cb;
1344 s->bw_changed_cls = bw_changed_cb_cls;
1345 s->get_preferences = get_preference;
1346 s->get_preferences_cls = get_preference_cls;
1347 s->get_properties = get_properties;
1348 s->get_properties_cls = get_properties_cls;
1349 s->networks = dest_length;
1350 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
1351 s->active_addresses = 0;
1352 s->total_addresses = 0;
1353 s->bulk_lock = GNUNET_NO;
1354 s->addresses = addresses;
1356 for (c = 0; c < dest_length; c++)
1358 cur = &s->network_entries[c];
1359 cur->total_addresses = 0;
1360 cur->active_addresses = 0;
1361 cur->type = network[c];
1362 cur->total_quota_in = in_quota[c];
1363 cur->total_quota_out = out_quota[c];
1364 cur->desc = net_str[c];
1365 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
1366 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
1373 * Shutdown the proportional problem solver
1375 * @param solver the respective handle to shutdown
1378 GAS_proportional_done (void *solver)
1380 struct GAS_PROPORTIONAL_Handle *s = solver;
1381 struct AddressWrapper *cur;
1382 struct AddressWrapper *next;
1384 GNUNET_assert (s != NULL);
1386 for (c = 0; c < s->networks; c++)
1388 if (s->network_entries[c].total_addresses > 0)
1390 LOG (GNUNET_ERROR_TYPE_ERROR,
1391 "Had %u addresses for network `%s' not deleted during shutdown\n",
1392 s->network_entries[c].total_addresses,
1393 s->network_entries[c].desc);
1397 if (s->network_entries[c].active_addresses > 0)
1399 LOG (GNUNET_ERROR_TYPE_ERROR,
1400 "Had %u active addresses for network `%s' not deleted during shutdown\n",
1401 s->network_entries[c].active_addresses,
1402 s->network_entries[c].desc);
1406 next = s->network_entries[c].head;
1407 while (NULL != (cur = next))
1410 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
1411 s->network_entries[c].tail,
1415 GNUNET_free (s->network_entries[c].stat_total);
1416 GNUNET_free (s->network_entries[c].stat_active);
1418 if (s->total_addresses > 0)
1420 LOG (GNUNET_ERROR_TYPE_ERROR,
1421 "Had %u addresses not deleted during shutdown\n",
1422 s->total_addresses);
1425 if (s->active_addresses > 0)
1427 LOG (GNUNET_ERROR_TYPE_ERROR,
1428 "Had %u active addresses not deleted during shutdown\n",
1429 s->active_addresses);
1432 GNUNET_free (s->network_entries);
1437 /* end of gnunet-service-ats-solver_proportional.c */