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-service-ats_normalization.h"
31 #include "gnunet_statistics_service.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
37 * NOTE: Do not change this documentation. This documentation is based
38 * on gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
39 * use build_txt.sh to generate plaintext output
41 * ATS addresses : simplistic solver
43 * The simplistic solver ("simplistic") distributes the available
44 * bandwidth fair over all the addresses influenced by the
45 * preference values. For each available network type an in- and
46 * outbound quota is configured and the bandwidth available in
47 * these networks is distributed over the addresses. The solver
48 * first assigns every addresses the minimum amount of bandwidth
49 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
50 * remaining bandwidth available according to the preference
51 * values. For each peer only a single address gets bandwidth
52 * assigned and only one address marked as active. The most
53 * important functionality for the solver is implemented in: *
54 * find_address_it is an hashmap iterator returning the prefered
55 * address for an peer * update_quota_per_network distributes
56 * available bandwidth for a network over active addresses
58 * Changes to addresses automatically have an impact on the the
59 * bandwidth assigned to other addresses in the same network since
60 * the solver distributes the remaining bandwidth over the
61 * addresses in the network. When changes to the addresses occur,
62 * the solver first performs the changes, like adding or deleting
63 * addresses, and then updates bandwidth assignment for the
64 * affected network. Bandwidth assignment is only recalculated on
65 * demand when an address is requested by a client for a peer or
66 * when the addresses available have changed or an address changed
67 * the network it is located in. When the bandwidth assignment has
68 * changed the callback is called with the new bandwidth
69 * assignments. The bandwidth distribution for a network is
70 * recalculated due to: * address suggestion requests * address
71 * deletions * address switching networks during address update *
74 * 3.1 Data structures used
76 * For each ATS network (e.g. WAN, LAN, loopback) a struct Network
77 * is used to specify network related information as total adresses
78 * and active addresses in this network and the configured in- and
79 * outbound quota. Each network also contains a list of addresses
80 * added to the solver located in this network. The simplistic
81 * solver uses the addresses' solver_information field to store the
82 * simplistic network it belongs to for each address.
86 * When the simplistic solver is initialized the solver creates a
87 * new solver handle and initializes the network structures with
88 * the quotas passed from addresses and returns the handle solver.
90 * 3.3 Adding an address
92 * When a new address is added to the solver using s_add, a lookup
93 * for the network for this address is done and the address is
94 * enqueued in in the linked list of the network.
96 * 3.4 Updating an address
98 * The main purpose of address updates is to update the ATS
99 * information for addresse selection. Important for the simplistic
100 * solver is when an address switches network it is located
101 * in. This is common because addresses added by transport's
102 * validation mechanism are commonly located in
103 * GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
104 * in this network type and only if a connection is successful on
105 * return of payload data transport switches to the real network
106 * the address is located in. When an address changes networks it
107 * is first of all removed from the old network using the solver
108 * API function GAS_simplistic_address_delete and the network in
109 * the address struct is updated. A lookup for the respective new
110 * simplistic network is done and stored in the addresse's
111 * solver_information field. Next the address is re-added to the
112 * solver using the solver API function
113 * GAS_simplistic_address_add. If the address was marked as in
114 * active, the solver checks if bandwidth is available in the
115 * network and if yes sets the address to active and updates the
116 * bandwidth distribution in this network. If no bandwidth is
117 * available it sets the bandwidth for this address to 0 and tries
118 * to suggest an alternative address. If an alternative address was
119 * found, addresses' callback is called for this address.
121 * 3.5 Deleting an address
123 * When an address is removed from the solver, it removes the
124 * respective address from the network and if the address was
125 * marked as active, it updates the bandwidth distribution for this
128 * 3.6 Requesting addresses
130 * When an address is requested for a peer the solver performs a
131 * lookup for the peer entry in addresses address hashmap and
132 * selects the best address. The selection of the most suitable
133 * address is done in the find_address_it hashmap iterator
134 * described in detail in section 3.7. If no address is returned,
135 * no address can be suggested at the moment. If the address
136 * returned is marked as active, the solver can return this
137 * address. If the address is not marked as active, the solver
138 * checks if another address belongign to this peer is marked as
139 * active and marks the address as inactive, updates the bandwidth
140 * for this address to 0, call the bandwidth changed callback for
141 * this address due to the change and updates quota assignment for
142 * the addresse's network. the now in-active address is belonging
143 * to. The solver marks the new address as active and updates the
144 * bandwidth assignment for this network.
146 * 3.7 Choosing addresses
148 * Choosing the best possible address for suggestion is done by
149 * iterating over all addresses of a peer stored in addresses'
150 * hashmap and using the hashmap iterator find_address_it to select
151 * the best available address. Several checks are done when an
152 * address is selected. First if this address is currently blocked
153 * by addresses from being suggested. An address is blocked for the
154 * duration of ATS_BLOCKING_DELTA when it is suggested to
155 * transport. Next it is checked if at least
156 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
157 * in the addresse's network, because suggesting an address without
158 * bandwidth does not make sense. This also ensures that all active
159 * addresses in this network get at least the minimum amount of
160 * bandwidth assigned. In the next step the solver ensures that for
161 * tcp connections inbound connections are prefered over outbound
162 * connections. In the next stet the solver ensures that
163 * connections are prefered in the following order: * connections
164 * are already established and have bandwidth assigned *
165 * connections with a shorter distance * connectes have a shorter
168 * 3.8 Changing preferences
172 * During shutdown all network entries and aging processes are
173 * destroyed and freed.
178 * This solver assigns in and outbound bandwidth equally for all
179 * addresses in specific network type (WAN, LAN) based on configured
180 * in and outbound quota for this network.
182 * The solver is notified by addresses about changes to the addresses
183 * and recalculates the bandwith assigned if required. The solver
184 * notifies addresses by calling the GAS_bandwidth_changed_cb
192 * For each peer only a single is selected and marked as "active" in the address
197 * You have the networks WAN and LAN and quotas
198 * WAN_TOTAL_IN, WAN_TOTAL_OUT
199 * LAN_TOTAL_IN, LAN_TOTAL_OUT
201 * If you have x addresses in the network segment LAN, the quotas are
202 * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
204 * Quotas are automatically recalculated and reported back when addresses are
209 #define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
210 #define PREF_AGING_FACTOR 0.95
212 #define DEFAULT_REL_PREFERENCE 1.0
213 #define DEFAULT_ABS_PREFERENCE 0.0
214 #define MIN_UPDATE_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
218 * A handle for the proportional solver
220 struct GAS_PROPORTIONAL_Handle
226 struct GNUNET_STATISTICS_Handle *stats;
229 * Total number of addresses for solver
231 unsigned int total_addresses;
234 * Number of active addresses for solver
236 unsigned int active_addresses;
241 struct Network *network_entries;
246 unsigned int networks;
251 GAS_bandwidth_changed_cb bw_changed;
256 void *bw_changed_cls;
258 struct GNUNET_CONTAINER_MultiHashMap *prefs;
260 struct PreferenceClient *pc_head;
261 struct PreferenceClient *pc_tail;
266 * Representation of a network
276 * Network description
281 * Total inbound quota
284 unsigned long long total_quota_in;
287 * Total outbound quota
290 unsigned long long total_quota_out;
293 * Number of active addresses for this network
295 unsigned int active_addresses;
298 * Number of total addresses for this network
300 unsigned int total_addresses;
303 * String for statistics total addresses
308 * String for statistics active addresses
312 struct AddressWrapper *head;
313 struct AddressWrapper *tail;
318 * Wrapper for addresses to store them in network's linked list
320 struct AddressWrapper
325 struct AddressWrapper *next;
330 struct AddressWrapper *prev;
335 struct ATS_Address *addr;
341 * Important solver functions
342 * ---------------------------
346 * Test if bandwidth is available in this network to add an additional address
348 * @param net the network type to update
349 * @return GNUNET_YES or GNUNET_NO
352 is_bandwidth_available_in_network (struct Network *net)
354 unsigned int na = net->active_addresses + 1;
355 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
356 if (((net->total_quota_in / na) > min_bw) &&
357 ((net->total_quota_out / na) > min_bw))
359 LOG (GNUNET_ERROR_TYPE_DEBUG,
360 "Enough bandwidth available for %u active addresses in network `%s'\n",
366 LOG (GNUNET_ERROR_TYPE_DEBUG,
367 "Not enough bandwidth available for %u active addresses in network `%s'\n",
375 * Update bandwidth assigned to peers in this network
377 * @param s the solver handle
378 * @param net the network type to update
379 * @param address_except address excluded from notification, since we suggest
383 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
385 struct ATS_Address *address_except)
387 unsigned long long remaining_quota_in = 0;
388 unsigned long long quota_out_used = 0;
390 unsigned long long remaining_quota_out = 0;
391 unsigned long long quota_in_used = 0;
392 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
394 double total_prefs; /* Important: has to be double not float due to precision */
395 double cur_pref; /* Important: has to be double not float due to precision */
396 const double *t = NULL; /* Important: has to be double not float due to precision */
399 unsigned long long assigned_quota_in = 0;
400 unsigned long long assigned_quota_out = 0;
401 struct AddressWrapper *cur;
403 LOG (GNUNET_ERROR_TYPE_DEBUG,
404 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
405 net->desc, net->active_addresses, net->total_quota_in, net->total_quota_in);
407 if (net->active_addresses == 0)
408 return; /* no addresses to update */
411 * Assign every peer in network minimum Bandwidth
412 * Distribute bandwidth left according to preference
415 if ((net->active_addresses * min_bw) > net->total_quota_in)
420 if ((net->active_addresses * min_bw) > net->total_quota_out)
426 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
427 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
428 LOG (GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
429 remaining_quota_in, remaining_quota_out);
431 for (cur = net->head; NULL != cur; cur = cur->next)
433 if (GNUNET_YES == cur->addr->active)
435 GNUNET_assert (NULL != (t = GAS_normalization_get_preferences (&cur->addr->peer)));
438 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
440 if (c != GNUNET_ATS_PREFERENCE_END)
442 //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
446 total_prefs += (peer_prefs / (GNUNET_ATS_PreferenceCount -1));
449 for (cur = net->head; NULL != cur; cur = cur->next)
451 if (GNUNET_YES == cur->addr->active)
454 GNUNET_assert (NULL != (t = GAS_normalization_get_preferences (&cur->addr->peer)));
456 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
458 if (c != GNUNET_ATS_PREFERENCE_END)
463 assigned_quota_in = min_bw + ((cur_pref / total_prefs) * remaining_quota_in);
464 assigned_quota_out = min_bw + ((cur_pref / total_prefs) * remaining_quota_out);
466 LOG (GNUNET_ERROR_TYPE_DEBUG,
467 "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
468 GNUNET_i2s (&cur->addr->peer),
469 cur_pref, total_prefs,
470 assigned_quota_in, assigned_quota_out);
474 assigned_quota_in = 0;
475 assigned_quota_out = 0;
478 quota_in_used += assigned_quota_in;
479 quota_out_used += assigned_quota_out;
480 /* Prevent overflow due to rounding errors */
481 if (assigned_quota_in > UINT32_MAX)
482 assigned_quota_in = UINT32_MAX;
483 if (assigned_quota_out > UINT32_MAX)
484 assigned_quota_out = UINT32_MAX;
486 /* Compare to current bandwidth assigned */
487 if ((assigned_quota_in != ntohl(cur->addr->assigned_bw_in.value__)) ||
488 (assigned_quota_out != ntohl(cur->addr->assigned_bw_out.value__)))
490 cur->addr->assigned_bw_in.value__ = htonl (assigned_quota_in);
491 cur->addr->assigned_bw_out.value__ = htonl (assigned_quota_out);
492 /* Notify on change */
493 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
494 s->bw_changed (s->bw_changed_cls, cur->addr);
498 LOG (GNUNET_ERROR_TYPE_DEBUG,
499 "Total bandwidth assigned is (in/out): %llu /%llu\n",
502 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
504 LOG (GNUNET_ERROR_TYPE_ERROR,
505 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
506 net->active_addresses,
508 net->total_quota_out);
510 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
512 LOG (GNUNET_ERROR_TYPE_ERROR,
513 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
514 net->active_addresses,
516 net->total_quota_in);
522 * Extract an ATS performance info from an address
524 * @param address the address
525 * @param type the type to extract in HBO
526 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
529 get_performance_info (struct ATS_Address *address, uint32_t type);
532 * Find a "good" address to use for a peer by iterating over the addresses for this peer.
533 * If we already have an existing address, we stick to it.
534 * Otherwise, we pick by lowest distance and then by lowest latency.
536 * @param cls the 'struct ATS_Address**' where we store the result
538 * @param value another 'struct ATS_Address*' to consider using
539 * @return GNUNET_OK (continue to iterate)
542 find_best_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
544 struct ATS_Address **previous_p = cls;
545 struct ATS_Address *current = (struct ATS_Address *) value;
546 struct ATS_Address *previous = *previous_p;
547 struct GNUNET_TIME_Absolute now;
548 struct Network *net = (struct Network *) current->solver_information;
549 uint32_t p_distance_cur;
550 uint32_t p_distance_prev;
551 uint32_t p_delay_cur;
552 uint32_t p_delay_prev;
554 now = GNUNET_TIME_absolute_get();
556 if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
558 /* This address is blocked for suggestion */
559 LOG (GNUNET_ERROR_TYPE_DEBUG,
560 "Address %p blocked for suggestion for %llu ms \n",
562 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
566 if (GNUNET_NO == is_bandwidth_available_in_network (net))
567 return GNUNET_OK; /* There's no bandwidth available in this network */
569 if (NULL != previous)
571 GNUNET_assert (NULL != previous->plugin);
572 GNUNET_assert (NULL != current->plugin);
573 if (0 == strcmp (previous->plugin, current->plugin))
575 if ((0 != previous->addr_len) &&
576 (0 == current->addr_len))
578 /* saved address was an outbound address, but we have an inbound address */
579 *previous_p = current;
582 if (0 == previous->addr_len)
584 /* saved address was an inbound address, so do not overwrite */
590 if (NULL == previous)
592 *previous_p = current;
595 if ((ntohl (previous->assigned_bw_in.value__) == 0) &&
596 (ntohl (current->assigned_bw_in.value__) > 0))
598 /* stick to existing connection */
599 *previous_p = current;
603 p_distance_prev = get_performance_info (previous, GNUNET_ATS_QUALITY_NET_DISTANCE);
604 p_distance_cur = get_performance_info (current, GNUNET_ATS_QUALITY_NET_DISTANCE);
605 if ((p_distance_prev != GNUNET_ATS_VALUE_UNDEFINED) && (p_distance_cur != GNUNET_ATS_VALUE_UNDEFINED) &&
606 (p_distance_prev > p_distance_cur))
608 /* user shorter distance */
609 *previous_p = current;
613 p_delay_prev = get_performance_info (previous, GNUNET_ATS_QUALITY_NET_DELAY);
614 p_delay_cur = get_performance_info (current, GNUNET_ATS_QUALITY_NET_DELAY);
615 if ((p_delay_prev != GNUNET_ATS_VALUE_UNDEFINED) && (p_delay_cur != GNUNET_ATS_VALUE_UNDEFINED) &&
616 (p_delay_prev > p_delay_cur))
618 /* user lower latency */
619 *previous_p = current;
629 * ---------------------------
633 * Update bandwidth assignment for all networks
635 * @param s the solver handle
638 distribute_bandwidth_in_all_networks (struct GAS_PROPORTIONAL_Handle *s)
641 for (i = 0; i < s->networks; i++)
642 distribute_bandwidth_in_network (s, &s->network_entries[i], NULL);
648 * Lookup network struct by type
650 * @param s the solver handle
651 * @param type the network type
652 * @return the network struct
654 static struct Network *
655 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
658 for (c = 0 ; c < s->networks; c++)
660 if (s->network_entries[c].type == type)
661 return &s->network_entries[c];
668 * Hashmap Iterator to find current active address for peer
670 * @param cls last active address
671 * @param key peer's key
672 * @param value address to check
673 * @return GNUNET_NO on double active address else GNUNET_YES
676 get_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
678 struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
679 struct ATS_Address * aa = (struct ATS_Address *) value;
681 if (GNUNET_YES == aa->active)
685 /* should never happen */
686 LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
697 * Find current active address for peer
699 * @param solver the solver handle
700 * @param addresses the address set
701 * @param peer the peer
702 * @return active address or NULL
704 static struct ATS_Address *
705 get_active_address (void *solver,
706 struct GNUNET_CONTAINER_MultiHashMap * addresses,
707 const struct GNUNET_PeerIdentity *peer)
709 struct ATS_Address * dest = NULL;
711 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
713 &get_active_address_it, &dest);
720 addresse_increment (struct GAS_PROPORTIONAL_Handle *s,
725 if (GNUNET_YES == total)
727 s->total_addresses ++;
728 net->total_addresses ++;
729 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
730 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
732 if (GNUNET_YES == active)
734 net->active_addresses ++;
735 s->active_addresses ++;
736 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
737 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
744 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s,
750 if (GNUNET_YES == total)
752 if (s->total_addresses < 1)
759 s->total_addresses --;
760 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
762 if (net->total_addresses < 1)
769 net->total_addresses --;
770 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
774 if (GNUNET_YES == active)
776 if (net->active_addresses < 1)
783 net->active_addresses --;
784 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
786 if (s->active_addresses < 1)
793 s->active_addresses --;
794 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
802 * Extract an ATS performance info from an address
804 * @param address the address
805 * @param type the type to extract in HBO
806 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
809 get_performance_info (struct ATS_Address *address, uint32_t type)
812 GNUNET_assert (NULL != address);
814 if ((NULL == address->atsi) || (0 == address->atsi_count))
815 return GNUNET_ATS_VALUE_UNDEFINED;
817 for (c1 = 0; c1 < address->atsi_count; c1++)
819 if (ntohl(address->atsi[c1].type) == type)
820 return ntohl(address->atsi[c1].value);
822 return GNUNET_ATS_VALUE_UNDEFINED;
827 * Solver API functions
828 * ---------------------------
832 * Changes the preferences for a peer in the problem
834 * @param solver the solver handle
835 * @param addresses the address hashmap
836 * @param peer the peer to change the preference for
837 * @param kind the kind to change the preference
838 * @param pref_rel the normalized preference value for this kind over all clients
841 GAS_proportional_address_change_preference (void *solver,
842 struct GNUNET_CONTAINER_MultiHashMap *addresses,
843 const struct GNUNET_PeerIdentity *peer,
844 enum GNUNET_ATS_PreferenceKind kind,
847 struct GAS_PROPORTIONAL_Handle *s = solver;
848 GNUNET_assert (NULL != solver);
849 GNUNET_assert (NULL != peer);
850 distribute_bandwidth_in_all_networks (s);
854 * Get the preferred address for a specific peer
856 * @param solver the solver handle
857 * @param addresses the address hashmap containing all addresses
858 * @param peer the identity of the peer
860 const struct ATS_Address *
861 GAS_proportional_get_preferred_address (void *solver,
862 struct GNUNET_CONTAINER_MultiHashMap * addresses,
863 const struct GNUNET_PeerIdentity *peer)
865 struct GAS_PROPORTIONAL_Handle *s = solver;
866 struct Network *net_prev;
867 struct Network *net_cur;
868 struct ATS_Address *cur;
869 struct ATS_Address *prev;
871 GNUNET_assert (s != NULL);
873 /* Get address with: stick to current address, lower distance, lower latency */
874 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
875 &find_best_address_it, &cur);
878 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
882 LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
883 (GNUNET_NO == cur->active) ? "inactive" : "active",
884 cur, GNUNET_i2s (peer));
885 net_cur = (struct Network *) cur->solver_information;
886 if (GNUNET_YES == cur->active)
888 /* This address was selected previously, so no need to update quotas */
892 /* This address was not active, so we have to:
894 * - mark previous active address as not active
895 * - update quota for previous address network
896 * - update quota for this address network
899 prev = get_active_address (s, addresses, peer);
902 net_prev = (struct Network *) prev->solver_information;
903 prev->active = GNUNET_NO; /* No active any longer */
904 prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
905 prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
906 s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
907 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
909 distribute_bandwidth_in_network (s, net_prev, NULL);
912 if (GNUNET_NO == (is_bandwidth_available_in_network (cur->solver_information)))
914 GNUNET_break (0); /* This should never happen*/
918 cur->active = GNUNET_YES;
919 addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
920 distribute_bandwidth_in_network (s, net_cur, cur);
927 * Stop notifying about address and bandwidth changes for this peer
929 * @param solver the solver handle
930 * @param addresses address hashmap
931 * @param peer the peer
934 GAS_proportional_stop_get_preferred_address (void *solver,
935 struct GNUNET_CONTAINER_MultiHashMap *addresses,
936 const struct GNUNET_PeerIdentity *peer)
943 * Remove an address from the solver
945 * @param solver the solver handle
946 * @param addresses the address hashmap containing all addresses
947 * @param address the address to remove
948 * @param session_only delete only session not whole address
951 GAS_proportional_address_delete (void *solver,
952 struct GNUNET_CONTAINER_MultiHashMap * addresses,
953 struct ATS_Address *address, int session_only)
955 struct GAS_PROPORTIONAL_Handle *s = solver;
957 struct AddressWrapper *aw;
959 /* Remove an adress completely, we have to:
960 * - Remove from specific network
961 * - Decrease number of total addresses
963 * - decrease number of active addreses
967 net = (struct Network *) address->solver_information;
969 if (GNUNET_NO == session_only)
971 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
972 (GNUNET_NO == address->active) ? "inactive" : "active",
973 address, GNUNET_i2s (&address->peer),
974 net->desc, net->total_addresses, net->active_addresses);
977 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
978 for (aw = net->head; NULL != aw; aw = aw->next)
980 if (aw->addr == address)
988 GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
993 /* Remove session only: remove if active and update */
994 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
995 (GNUNET_NO == address->active) ? "inactive" : "active",
996 address, GNUNET_i2s (&address->peer),
997 net->desc, net->total_addresses, net->active_addresses);
1000 if (GNUNET_YES == address->active)
1002 /* Address was active, remove from network and update quotas*/
1003 address->active = GNUNET_NO;
1004 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1006 distribute_bandwidth_in_network (s, net, NULL);
1008 LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
1009 net->total_addresses,
1010 net->active_addresses,
1017 * Add a new single address to a network
1019 * @param solver the solver Handle
1020 * @param addresses the address hashmap containing all addresses
1021 * @param address the address to add
1022 * @param network network type of this address
1025 GAS_proportional_address_add (void *solver,
1026 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1027 struct ATS_Address *address,
1031 * Updates a single address in the solver and checks previous values
1033 * @param solver the solver Handle
1034 * @param addresses the address hashmap containing all addresses
1035 * @param address the update address
1036 * @param session the previous session
1037 * @param in_use the previous address in use state
1038 * @param prev_ats previous ATS information
1039 * @param prev_atsi_count the previous atsi count
1042 GAS_proportional_address_update (void *solver,
1043 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1044 struct ATS_Address *address,
1047 const struct GNUNET_ATS_Information *prev_ats,
1048 uint32_t prev_atsi_count)
1050 struct ATS_Address *new;
1051 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1053 uint32_t prev_value;
1056 int save_active = GNUNET_NO;
1057 struct Network *new_net = NULL;
1059 /* Check updates to performance information */
1060 for (i = 0; i < prev_atsi_count; i++)
1062 prev_type = ntohl (prev_ats[i].type);
1063 prev_value = ntohl (prev_ats[i].value);
1066 case GNUNET_ATS_UTILIZATION_UP:
1067 case GNUNET_ATS_UTILIZATION_DOWN:
1068 case GNUNET_ATS_QUALITY_NET_DELAY:
1069 case GNUNET_ATS_QUALITY_NET_DISTANCE:
1070 case GNUNET_ATS_COST_WAN:
1071 case GNUNET_ATS_COST_LAN:
1072 case GNUNET_ATS_COST_WLAN:
1073 /* No actions required here*/
1075 case GNUNET_ATS_NETWORK_TYPE:
1077 addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
1078 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
1081 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
1083 if (addr_net != prev_value)
1085 /* Network changed */
1086 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
1087 (GNUNET_YES == address->active) ? "active" : "inactive",
1088 GNUNET_ATS_print_network_type(prev_value),
1089 GNUNET_ATS_print_network_type(addr_net));
1091 save_active = address->active;
1092 /* remove from old network */
1093 GAS_proportional_address_delete (solver, addresses, address, GNUNET_NO);
1095 /* set new network type */
1096 new_net = get_network (solver, addr_net);
1097 address->solver_information = new_net;
1099 /* Add to new network and update*/
1100 GAS_proportional_address_add (solver, addresses, address, addr_net);
1101 if (GNUNET_YES == save_active)
1103 /* check if bandwidth available in new network */
1104 if (GNUNET_YES == (is_bandwidth_available_in_network (new_net)))
1106 /* Suggest updated address */
1107 address->active = GNUNET_YES;
1108 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
1109 distribute_bandwidth_in_network (solver, new_net, NULL);
1113 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not enough bandwidth in new network, suggesting alternative address ..\n");
1115 /* Set old address to zero bw */
1116 address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
1117 address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
1118 s->bw_changed (s->bw_changed_cls, address);
1120 /* Find new address to suggest since no bandwidth in network*/
1121 new = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, addresses, &address->peer);
1124 /* Have an alternative address to suggest */
1125 s->bw_changed (s->bw_changed_cls, new);
1133 case GNUNET_ATS_ARRAY_TERMINATOR:
1136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1137 "Received unsupported ATS type %u\n", prev_type);
1144 if (address->session_id != session)
1146 LOG (GNUNET_ERROR_TYPE_DEBUG,
1147 "Session changed from %u to %u\n", session, address->session_id);
1149 if (address->used != in_use)
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 "Usage changed from %u to %u\n", in_use, address->used);
1159 * Add a new single address to a network
1161 * @param solver the solver Handle
1162 * @param addresses the address hashmap containing all addresses
1163 * @param address the address to add
1164 * @param network network type of this address
1167 GAS_proportional_address_add (void *solver,
1168 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1169 struct ATS_Address *address,
1172 struct GAS_PROPORTIONAL_Handle *s = solver;
1173 struct Network *net = NULL;
1174 struct AddressWrapper *aw = NULL;
1175 GNUNET_assert (NULL != s);
1177 net = get_network (s, network);
1184 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
1186 GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
1187 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1188 aw->addr->solver_information = net;
1190 LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
1191 net->total_addresses,
1192 net->active_addresses,
1198 * Init the proportional problem solver
1201 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1202 * out_quota[i] contains outbound quota for network type i
1203 * in_quota[i] contains inbound quota for network type i
1206 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1207 * network[2] == GNUNET_ATS_NET_LAN
1208 * out_quota[2] == 65353
1209 * in_quota[2] == 65353
1211 * @param cfg configuration handle
1212 * @param stats the GNUNET_STATISTICS handle
1213 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1214 * @param out_quota array of outbound quotas
1215 * @param in_quota array of outbound quota
1216 * @param dest_length array length for quota arrays
1217 * @param bw_changed_cb callback for changed bandwidth amounts
1218 * @param bw_changed_cb_cls cls for callback
1219 * @return handle for the solver on success, NULL on fail
1222 GAS_proportional_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1223 const struct GNUNET_STATISTICS_Handle *stats,
1225 unsigned long long *out_quota,
1226 unsigned long long *in_quota,
1228 GAS_bandwidth_changed_cb bw_changed_cb,
1229 void *bw_changed_cb_cls)
1232 struct GAS_PROPORTIONAL_Handle *s = GNUNET_malloc (sizeof (struct GAS_PROPORTIONAL_Handle));
1233 struct Network * cur;
1234 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1237 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1238 s->bw_changed = bw_changed_cb;
1239 s->bw_changed_cls = bw_changed_cb_cls;
1240 s->networks = dest_length;
1241 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
1242 s->active_addresses = 0;
1243 s->total_addresses = 0;
1244 s->prefs = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1246 for (c = 0; c < dest_length; c++)
1248 cur = &s->network_entries[c];
1249 cur->total_addresses = 0;
1250 cur->active_addresses = 0;
1251 cur->type = network[c];
1252 cur->total_quota_in = in_quota[c];
1253 cur->total_quota_out = out_quota[c];
1254 cur->desc = net_str[c];
1255 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
1256 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
1263 * Shutdown the proportional problem solver
1265 * @param solver the respective handle to shutdown
1268 GAS_proportional_done (void *solver)
1270 struct GAS_PROPORTIONAL_Handle *s = solver;
1271 struct AddressWrapper *cur;
1272 struct AddressWrapper *next;
1274 GNUNET_assert (s != NULL);
1276 for (c = 0; c < s->networks; c++)
1278 if (s->network_entries[c].total_addresses > 0)
1280 LOG (GNUNET_ERROR_TYPE_ERROR,
1281 "Had %u addresses for network `%s' not deleted during shutdown\n",
1282 s->network_entries[c].total_addresses,
1283 s->network_entries[c].desc);
1287 if (s->network_entries[c].active_addresses > 0)
1289 LOG (GNUNET_ERROR_TYPE_ERROR,
1290 "Had %u active addresses for network `%s' not deleted during shutdown\n",
1291 s->network_entries[c].active_addresses,
1292 s->network_entries[c].desc);
1296 next = s->network_entries[c].head;
1297 while (NULL != (cur = next))
1300 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
1301 s->network_entries[c].tail,
1305 GNUNET_free (s->network_entries[c].stat_total);
1306 GNUNET_free (s->network_entries[c].stat_active);
1308 if (s->total_addresses > 0)
1310 LOG (GNUNET_ERROR_TYPE_ERROR,
1311 "Had %u addresses not deleted during shutdown\n",
1312 s->total_addresses);
1315 if (s->active_addresses > 0)
1317 LOG (GNUNET_ERROR_TYPE_ERROR,
1318 "Had %u active addresses not deleted during shutdown\n",
1319 s->active_addresses);
1322 GNUNET_free (s->network_entries);
1327 /* end of gnunet-service-ats-solver_proportional.c */