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 * Bandwidth changed callback
229 GAS_bandwidth_changed_cb bw_changed;
232 * Bandwidth changed callback cls
234 void *bw_changed_cls;
237 * ATS function to get preferences
239 GAS_get_preferences get_preferences;
242 * Closure for ATS function to get preferences
244 void *get_preferences_cls;
252 * Number of changes while solver was locked
258 * Total number of addresses for solver
260 unsigned int total_addresses;
263 * Number of active addresses for solver
265 unsigned int active_addresses;
270 struct Network *network_entries;
275 unsigned int networks;
281 * Representation of a network
291 * Network description
296 * Total inbound quota
299 unsigned long long total_quota_in;
302 * Total outbound quota
305 unsigned long long total_quota_out;
308 * Number of active addresses for this network
310 unsigned int active_addresses;
313 * Number of total addresses for this network
315 unsigned int total_addresses;
318 * String for statistics total addresses
323 * String for statistics active addresses
327 struct AddressWrapper *head;
328 struct AddressWrapper *tail;
333 * Wrapper for addresses to store them in network's linked list
335 struct AddressWrapper
340 struct AddressWrapper *next;
345 struct AddressWrapper *prev;
350 struct ATS_Address *addr;
356 * Important solver functions
357 * ---------------------------
361 * Test if bandwidth is available in this network to add an additional address
363 * @param net the network type to update
364 * @return GNUNET_YES or GNUNET_NO
367 is_bandwidth_available_in_network (struct Network *net)
369 GNUNET_assert (NULL != net);
370 unsigned int na = net->active_addresses + 1;
371 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
372 if (((net->total_quota_in / na) > min_bw) &&
373 ((net->total_quota_out / na) > min_bw))
375 LOG (GNUNET_ERROR_TYPE_DEBUG,
376 "Enough bandwidth available for %u active addresses in network `%s'\n",
382 LOG (GNUNET_ERROR_TYPE_DEBUG,
383 "Not enough bandwidth available for %u active addresses in network `%s'\n",
391 * Update bandwidth assigned to peers in this network
393 * @param s the solver handle
394 * @param net the network type to update
395 * @param address_except address excluded from notification, since we suggest
399 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
401 struct ATS_Address *address_except)
403 unsigned long long remaining_quota_in = 0;
404 unsigned long long quota_out_used = 0;
406 unsigned long long remaining_quota_out = 0;
407 unsigned long long quota_in_used = 0;
408 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
410 double total_prefs; /* Important: has to be double not float due to precision */
411 double cur_pref; /* Important: has to be double not float due to precision */
412 const double *t = NULL; /* Important: has to be double not float due to precision */
415 unsigned long long assigned_quota_in = 0;
416 unsigned long long assigned_quota_out = 0;
417 struct AddressWrapper *cur;
420 if (GNUNET_YES == s->bulk_lock)
426 LOG (GNUNET_ERROR_TYPE_DEBUG,
427 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
428 net->desc, net->active_addresses, net->total_quota_in, net->total_quota_in);
430 if (net->active_addresses == 0)
431 return; /* no addresses to update */
434 * Assign every peer in network minimum Bandwidth
435 * Distribute bandwidth left according to preference
438 if ((net->active_addresses * min_bw) > net->total_quota_in)
443 if ((net->active_addresses * min_bw) > net->total_quota_out)
449 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
450 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
451 LOG (GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
452 remaining_quota_in, remaining_quota_out);
454 for (cur = net->head; NULL != cur; cur = cur->next)
456 if (GNUNET_YES == cur->addr->active)
458 GNUNET_assert (NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
461 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
463 if (c != GNUNET_ATS_PREFERENCE_END)
465 //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
469 total_prefs += (peer_prefs / (GNUNET_ATS_PreferenceCount -1));
472 for (cur = net->head; NULL != cur; cur = cur->next)
474 if (GNUNET_YES == cur->addr->active)
477 GNUNET_assert (NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
479 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
481 if (c != GNUNET_ATS_PREFERENCE_END)
486 assigned_quota_in = min_bw + ((cur_pref / total_prefs) * remaining_quota_in);
487 assigned_quota_out = min_bw + ((cur_pref / total_prefs) * remaining_quota_out);
489 LOG (GNUNET_ERROR_TYPE_DEBUG,
490 "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
491 GNUNET_i2s (&cur->addr->peer),
492 cur_pref, total_prefs,
493 assigned_quota_in, assigned_quota_out);
497 assigned_quota_in = 0;
498 assigned_quota_out = 0;
501 quota_in_used += assigned_quota_in;
502 quota_out_used += assigned_quota_out;
503 /* Prevent overflow due to rounding errors */
504 if (assigned_quota_in > UINT32_MAX)
505 assigned_quota_in = UINT32_MAX;
506 if (assigned_quota_out > UINT32_MAX)
507 assigned_quota_out = UINT32_MAX;
509 /* Compare to current bandwidth assigned */
510 if ((assigned_quota_in != ntohl(cur->addr->assigned_bw_in.value__)) ||
511 (assigned_quota_out != ntohl(cur->addr->assigned_bw_out.value__)))
513 cur->addr->assigned_bw_in.value__ = htonl (assigned_quota_in);
514 cur->addr->assigned_bw_out.value__ = htonl (assigned_quota_out);
515 /* Notify on change */
516 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
517 s->bw_changed (s->bw_changed_cls, cur->addr);
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "Total bandwidth assigned is (in/out): %llu /%llu\n",
525 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
527 LOG (GNUNET_ERROR_TYPE_ERROR,
528 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
529 net->active_addresses,
531 net->total_quota_out);
533 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
535 LOG (GNUNET_ERROR_TYPE_ERROR,
536 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
537 net->active_addresses,
539 net->total_quota_in);
545 * Extract an ATS performance info from an address
547 * @param address the address
548 * @param type the type to extract in HBO
549 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
552 get_performance_info (struct ATS_Address *address, uint32_t type);
555 * Find a "good" address to use for a peer by iterating over the addresses for this peer.
556 * If we already have an existing address, we stick to it.
557 * Otherwise, we pick by lowest distance and then by lowest latency.
559 * @param cls the 'struct ATS_Address**' where we store the result
561 * @param value another 'struct ATS_Address*' to consider using
562 * @return GNUNET_OK (continue to iterate)
565 find_best_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
567 struct ATS_Address **previous_p = cls;
568 struct ATS_Address *current = (struct ATS_Address *) value;
569 struct ATS_Address *previous = *previous_p;
570 struct GNUNET_TIME_Absolute now;
571 struct Network *net = (struct Network *) current->solver_information;
572 uint32_t p_distance_cur;
573 uint32_t p_distance_prev;
574 uint32_t p_delay_cur;
575 uint32_t p_delay_prev;
577 now = GNUNET_TIME_absolute_get();
579 if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
581 /* This address is blocked for suggestion */
582 LOG (GNUNET_ERROR_TYPE_DEBUG,
583 "Address %p blocked for suggestion for %llu ms \n",
585 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
589 if (GNUNET_NO == is_bandwidth_available_in_network (net))
590 return GNUNET_OK; /* There's no bandwidth available in this network */
592 if (NULL != previous)
594 GNUNET_assert (NULL != previous->plugin);
595 GNUNET_assert (NULL != current->plugin);
596 if (0 == strcmp (previous->plugin, current->plugin))
598 if ((0 != previous->addr_len) &&
599 (0 == current->addr_len))
601 /* saved address was an outbound address, but we have an inbound address */
602 *previous_p = current;
605 if (0 == previous->addr_len)
607 /* saved address was an inbound address, so do not overwrite */
613 if (NULL == previous)
615 *previous_p = current;
618 if ((ntohl (previous->assigned_bw_in.value__) == 0) &&
619 (ntohl (current->assigned_bw_in.value__) > 0))
621 /* stick to existing connection */
622 *previous_p = current;
626 p_distance_prev = get_performance_info (previous, GNUNET_ATS_QUALITY_NET_DISTANCE);
627 p_distance_cur = get_performance_info (current, GNUNET_ATS_QUALITY_NET_DISTANCE);
628 if ((p_distance_prev != GNUNET_ATS_VALUE_UNDEFINED) && (p_distance_cur != GNUNET_ATS_VALUE_UNDEFINED) &&
629 (p_distance_prev > p_distance_cur))
631 /* user shorter distance */
632 *previous_p = current;
636 p_delay_prev = get_performance_info (previous, GNUNET_ATS_QUALITY_NET_DELAY);
637 p_delay_cur = get_performance_info (current, GNUNET_ATS_QUALITY_NET_DELAY);
638 if ((p_delay_prev != GNUNET_ATS_VALUE_UNDEFINED) && (p_delay_cur != GNUNET_ATS_VALUE_UNDEFINED) &&
639 (p_delay_prev > p_delay_cur))
641 /* user lower latency */
642 *previous_p = current;
652 * ---------------------------
656 * Update bandwidth assignment for all networks
658 * @param s the solver handle
661 distribute_bandwidth_in_all_networks (struct GAS_PROPORTIONAL_Handle *s)
665 for (i = 0; i < s->networks; i++)
666 distribute_bandwidth_in_network (s, &s->network_entries[i], NULL);
672 * Lookup network struct by type
674 * @param s the solver handle
675 * @param type the network type
676 * @return the network struct
678 static struct Network *
679 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
682 for (c = 0 ; c < s->networks; c++)
684 if (s->network_entries[c].type == type)
685 return &s->network_entries[c];
693 * Hashmap Iterator to find current active address for peer
695 * @param cls last active address
696 * @param key peer's key
697 * @param value address to check
698 * @return GNUNET_NO on double active address else GNUNET_YES
701 get_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
703 struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
704 struct ATS_Address * aa = (struct ATS_Address *) value;
706 if (GNUNET_YES == aa->active)
710 /* should never happen */
711 LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
722 * Find current active address for peer
724 * @param solver the solver handle
725 * @param addresses the address set
726 * @param peer the peer
727 * @return active address or NULL
729 static struct ATS_Address *
730 get_active_address (void *solver,
731 struct GNUNET_CONTAINER_MultiHashMap * addresses,
732 const struct GNUNET_PeerIdentity *peer)
734 struct ATS_Address * dest = NULL;
736 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
738 &get_active_address_it, &dest);
745 addresse_increment (struct GAS_PROPORTIONAL_Handle *s,
750 if (GNUNET_YES == total)
752 s->total_addresses ++;
753 net->total_addresses ++;
754 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
755 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
757 if (GNUNET_YES == active)
759 net->active_addresses ++;
760 s->active_addresses ++;
761 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
762 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
769 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s,
775 if (GNUNET_YES == total)
777 if (s->total_addresses < 1)
784 s->total_addresses --;
785 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
787 if (net->total_addresses < 1)
794 net->total_addresses --;
795 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
799 if (GNUNET_YES == active)
801 if (net->active_addresses < 1)
808 net->active_addresses --;
809 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
811 if (s->active_addresses < 1)
818 s->active_addresses --;
819 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
827 * Extract an ATS performance info from an address
829 * @param address the address
830 * @param type the type to extract in HBO
831 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
834 get_performance_info (struct ATS_Address *address, uint32_t type)
837 GNUNET_assert (NULL != address);
839 if ((NULL == address->atsi) || (0 == address->atsi_count))
840 return GNUNET_ATS_VALUE_UNDEFINED;
842 for (c1 = 0; c1 < address->atsi_count; c1++)
844 if (ntohl(address->atsi[c1].type) == type)
845 return ntohl(address->atsi[c1].value);
847 return GNUNET_ATS_VALUE_UNDEFINED;
852 * Solver API functions
853 * ---------------------------
857 * Changes the preferences for a peer in the problem
859 * @param solver the solver handle
860 * @param addresses the address hashmap
861 * @param peer the peer to change the preference for
862 * @param kind the kind to change the preference
863 * @param pref_rel the normalized preference value for this kind over all clients
866 GAS_proportional_address_change_preference (void *solver,
867 struct GNUNET_CONTAINER_MultiHashMap *addresses,
868 const struct GNUNET_PeerIdentity *peer,
869 enum GNUNET_ATS_PreferenceKind kind,
872 struct GAS_PROPORTIONAL_Handle *s = solver;
873 GNUNET_assert (NULL != solver);
874 GNUNET_assert (NULL != peer);
876 distribute_bandwidth_in_all_networks (s);
880 * Get the preferred address for a specific peer
882 * @param solver the solver handle
883 * @param addresses the address hashmap containing all addresses
884 * @param peer the identity of the peer
886 const struct ATS_Address *
887 GAS_proportional_get_preferred_address (void *solver,
888 struct GNUNET_CONTAINER_MultiHashMap * addresses,
889 const struct GNUNET_PeerIdentity *peer)
891 struct GAS_PROPORTIONAL_Handle *s = solver;
892 struct Network *net_prev;
893 struct Network *net_cur;
894 struct ATS_Address *cur;
895 struct ATS_Address *prev;
897 GNUNET_assert (s != NULL);
899 /* Get address with: stick to current address, lower distance, lower latency */
900 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
901 &find_best_address_it, &cur);
904 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
908 LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
909 (GNUNET_NO == cur->active) ? "inactive" : "active",
910 cur, GNUNET_i2s (peer));
911 net_cur = (struct Network *) cur->solver_information;
914 LOG (GNUNET_ERROR_TYPE_ERROR, "Trying to suggesting unknown address peer `%s'\n",
919 if (GNUNET_YES == cur->active)
921 /* This address was selected previously, so no need to update quotas */
925 /* This address was not active, so we have to:
927 * - mark previous active address as not active
928 * - update quota for previous address network
929 * - update quota for this address network
932 prev = get_active_address (s, addresses, peer);
935 net_prev = (struct Network *) prev->solver_information;
936 prev->active = GNUNET_NO; /* No active any longer */
937 prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
938 prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
939 s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
940 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
942 distribute_bandwidth_in_network (s, net_prev, NULL);
945 if (GNUNET_NO == (is_bandwidth_available_in_network (cur->solver_information)))
947 GNUNET_break (0); /* This should never happen*/
951 cur->active = GNUNET_YES;
952 addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
953 distribute_bandwidth_in_network (s, net_cur, cur);
959 * Stop notifying about address and bandwidth changes for this peer
961 * @param solver the solver handle
962 * @param addresses address hashmap
963 * @param peer the peer
966 GAS_proportional_stop_get_preferred_address (void *solver,
967 struct GNUNET_CONTAINER_MultiHashMap *addresses,
968 const struct GNUNET_PeerIdentity *peer)
975 * Remove an address from the solver
977 * @param solver the solver handle
978 * @param addresses the address hashmap containing all addresses
979 * @param address the address to remove
980 * @param session_only delete only session not whole address
983 GAS_proportional_address_delete (void *solver,
984 struct GNUNET_CONTAINER_MultiHashMap * addresses,
985 struct ATS_Address *address, int session_only)
987 struct GAS_PROPORTIONAL_Handle *s = solver;
989 struct AddressWrapper *aw;
991 /* Remove an adress completely, we have to:
992 * - Remove from specific network
993 * - Decrease number of total addresses
995 * - decrease number of active addreses
999 net = (struct Network *) address->solver_information;
1001 if (GNUNET_NO == session_only)
1003 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1004 (GNUNET_NO == address->active) ? "inactive" : "active",
1005 address, GNUNET_i2s (&address->peer),
1006 net->desc, net->total_addresses, net->active_addresses);
1008 /* Remove address */
1009 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1010 for (aw = net->head; NULL != aw; aw = aw->next)
1012 if (aw->addr == address)
1020 GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
1025 /* Remove session only: remove if active and update */
1026 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1027 (GNUNET_NO == address->active) ? "inactive" : "active",
1028 address, GNUNET_i2s (&address->peer),
1029 net->desc, net->total_addresses, net->active_addresses);
1032 if (GNUNET_YES == address->active)
1034 /* Address was active, remove from network and update quotas*/
1035 address->active = GNUNET_NO;
1036 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1038 distribute_bandwidth_in_network (s, net, NULL);
1040 LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
1041 net->total_addresses,
1042 net->active_addresses,
1049 * Start a bulk operation
1051 * @param solver the solver
1054 GAS_proportional_bulk_start (void *solver)
1056 LOG (GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1057 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1059 GNUNET_assert (NULL != solver);
1064 * Bulk operation done
1067 GAS_proportional_bulk_stop (void *solver)
1069 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1071 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1072 GNUNET_assert (NULL != solver);
1074 if (s->bulk_lock < 1)
1080 if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1082 LOG (GNUNET_ERROR_TYPE_ERROR, "No lock pending, recalculating\n");
1083 distribute_bandwidth_in_all_networks (s);
1084 s->bulk_requests = 0;
1090 * Add a new single address to a network
1092 * @param solver the solver Handle
1093 * @param addresses the address hashmap containing all addresses
1094 * @param address the address to add
1095 * @param network network type of this address
1098 GAS_proportional_address_add (void *solver,
1099 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1100 struct ATS_Address *address,
1104 * Updates a single address in the solver and checks previous values
1106 * @param solver the solver Handle
1107 * @param addresses the address hashmap containing all addresses
1108 * @param address the update address
1109 * @param session the previous session
1110 * @param in_use the previous address in use state
1111 * @param prev_ats previous ATS information
1112 * @param prev_atsi_count the previous atsi count
1115 GAS_proportional_address_update (void *solver,
1116 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1117 struct ATS_Address *address,
1120 const struct GNUNET_ATS_Information *prev_ats,
1121 uint32_t prev_atsi_count)
1123 struct ATS_Address *new;
1124 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1126 uint32_t prev_value;
1129 int save_active = GNUNET_NO;
1130 struct Network *new_net = NULL;
1132 /* Check updates to performance information */
1133 for (i = 0; i < prev_atsi_count; i++)
1135 prev_type = ntohl (prev_ats[i].type);
1136 prev_value = ntohl (prev_ats[i].value);
1139 case GNUNET_ATS_UTILIZATION_UP:
1140 case GNUNET_ATS_UTILIZATION_DOWN:
1141 case GNUNET_ATS_QUALITY_NET_DELAY:
1142 case GNUNET_ATS_QUALITY_NET_DISTANCE:
1143 case GNUNET_ATS_COST_WAN:
1144 case GNUNET_ATS_COST_LAN:
1145 case GNUNET_ATS_COST_WLAN:
1146 /* No actions required here*/
1148 case GNUNET_ATS_NETWORK_TYPE:
1150 addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
1151 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
1154 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
1156 if (addr_net != prev_value)
1158 /* Network changed */
1159 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
1160 (GNUNET_YES == address->active) ? "active" : "inactive",
1161 GNUNET_ATS_print_network_type(prev_value),
1162 GNUNET_ATS_print_network_type(addr_net));
1164 save_active = address->active;
1165 /* remove from old network */
1166 GAS_proportional_address_delete (solver, addresses, address, GNUNET_NO);
1168 /* set new network type */
1169 new_net = get_network (solver, addr_net);
1170 if (NULL == new_net)
1172 /* Address changed to invalid network... */
1173 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot find network of type `%u' %s\n"),
1174 addr_net, GNUNET_ATS_print_network_type (addr_net));
1175 address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
1176 address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
1177 s->bw_changed (s->bw_changed_cls, address);
1180 address->solver_information = new_net;
1182 /* Add to new network and update*/
1183 GAS_proportional_address_add (solver, addresses, address, addr_net);
1184 if (GNUNET_YES == save_active)
1186 /* check if bandwidth available in new network */
1187 if (GNUNET_YES == (is_bandwidth_available_in_network (new_net)))
1189 /* Suggest updated address */
1190 address->active = GNUNET_YES;
1191 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
1192 distribute_bandwidth_in_network (solver, new_net, NULL);
1196 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not enough bandwidth in new network, suggesting alternative address ..\n");
1198 /* Set old address to zero bw */
1199 address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
1200 address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
1201 s->bw_changed (s->bw_changed_cls, address);
1203 /* Find new address to suggest since no bandwidth in network*/
1204 new = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, addresses, &address->peer);
1207 /* Have an alternative address to suggest */
1208 s->bw_changed (s->bw_changed_cls, new);
1216 case GNUNET_ATS_ARRAY_TERMINATOR:
1219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1220 "Received unsupported ATS type %u\n", prev_type);
1227 if (address->session_id != session)
1229 LOG (GNUNET_ERROR_TYPE_DEBUG,
1230 "Session changed from %u to %u\n", session, address->session_id);
1232 if (address->used != in_use)
1234 LOG (GNUNET_ERROR_TYPE_DEBUG,
1235 "Usage changed from %u to %u\n", in_use, address->used);
1242 * Add a new single address to a network
1244 * @param solver the solver Handle
1245 * @param addresses the address hashmap containing all addresses
1246 * @param address the address to add
1247 * @param network network type of this address
1250 GAS_proportional_address_add (void *solver,
1251 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1252 struct ATS_Address *address,
1255 struct GAS_PROPORTIONAL_Handle *s = solver;
1256 struct Network *net = NULL;
1257 struct AddressWrapper *aw = NULL;
1258 GNUNET_assert (NULL != s);
1260 net = get_network (s, network);
1267 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
1269 GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
1270 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1271 aw->addr->solver_information = net;
1273 LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
1274 net->total_addresses,
1275 net->active_addresses,
1281 * Init the proportional problem solver
1284 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1285 * out_quota[i] contains outbound quota for network type i
1286 * in_quota[i] contains inbound quota for network type i
1289 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1290 * network[2] == GNUNET_ATS_NET_LAN
1291 * out_quota[2] == 65353
1292 * in_quota[2] == 65353
1294 * @param cfg configuration handle
1295 * @param stats the GNUNET_STATISTICS handle
1296 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1297 * @param out_quota array of outbound quotas
1298 * @param in_quota array of outbound quota
1299 * @param dest_length array length for quota arrays
1300 * @param bw_changed_cb callback for changed bandwidth amounts
1301 * @param bw_changed_cb_cls cls for callback
1302 * @param get_preference callback to get relative preferences for a peer
1303 * @param get_preference_cls cls for callback to get relative preferences
1304 * @return handle for the solver on success, NULL on fail
1307 GAS_proportional_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1308 const struct GNUNET_STATISTICS_Handle *stats,
1310 unsigned long long *out_quota,
1311 unsigned long long *in_quota,
1313 GAS_bandwidth_changed_cb bw_changed_cb,
1314 void *bw_changed_cb_cls,
1315 GAS_get_preferences get_preference,
1316 void *get_preference_cls)
1319 struct GAS_PROPORTIONAL_Handle *s = GNUNET_malloc (sizeof (struct GAS_PROPORTIONAL_Handle));
1320 struct Network * cur;
1321 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1324 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1325 s->bw_changed = bw_changed_cb;
1326 s->bw_changed_cls = bw_changed_cb_cls;
1327 s->get_preferences = get_preference;
1328 s->get_preferences_cls = get_preference_cls;
1329 s->networks = dest_length;
1330 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
1331 s->active_addresses = 0;
1332 s->total_addresses = 0;
1333 s->bulk_lock = GNUNET_NO;
1335 for (c = 0; c < dest_length; c++)
1337 cur = &s->network_entries[c];
1338 cur->total_addresses = 0;
1339 cur->active_addresses = 0;
1340 cur->type = network[c];
1341 cur->total_quota_in = in_quota[c];
1342 cur->total_quota_out = out_quota[c];
1343 cur->desc = net_str[c];
1344 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
1345 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
1352 * Shutdown the proportional problem solver
1354 * @param solver the respective handle to shutdown
1357 GAS_proportional_done (void *solver)
1359 struct GAS_PROPORTIONAL_Handle *s = solver;
1360 struct AddressWrapper *cur;
1361 struct AddressWrapper *next;
1363 GNUNET_assert (s != NULL);
1365 for (c = 0; c < s->networks; c++)
1367 if (s->network_entries[c].total_addresses > 0)
1369 LOG (GNUNET_ERROR_TYPE_ERROR,
1370 "Had %u addresses for network `%s' not deleted during shutdown\n",
1371 s->network_entries[c].total_addresses,
1372 s->network_entries[c].desc);
1376 if (s->network_entries[c].active_addresses > 0)
1378 LOG (GNUNET_ERROR_TYPE_ERROR,
1379 "Had %u active addresses for network `%s' not deleted during shutdown\n",
1380 s->network_entries[c].active_addresses,
1381 s->network_entries[c].desc);
1385 next = s->network_entries[c].head;
1386 while (NULL != (cur = next))
1389 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
1390 s->network_entries[c].tail,
1394 GNUNET_free (s->network_entries[c].stat_total);
1395 GNUNET_free (s->network_entries[c].stat_active);
1397 if (s->total_addresses > 0)
1399 LOG (GNUNET_ERROR_TYPE_ERROR,
1400 "Had %u addresses not deleted during shutdown\n",
1401 s->total_addresses);
1404 if (s->active_addresses > 0)
1406 LOG (GNUNET_ERROR_TYPE_ERROR,
1407 "Had %u active addresses not deleted during shutdown\n",
1408 s->active_addresses);
1411 GNUNET_free (s->network_entries);
1416 /* end of gnunet-service-ats-solver_proportional.c */