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_PREFERENCE 1.0
212 #define MIN_UPDATE_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
215 * A handle for the proportional solver
217 struct GAS_PROPORTIONAL_Handle
223 struct GNUNET_STATISTICS_Handle *stats;
226 * Total number of addresses for solver
228 unsigned int total_addresses;
231 * Number of active addresses for solver
233 unsigned int active_addresses;
238 struct Network *network_entries;
243 unsigned int networks;
248 GAS_bandwidth_changed_cb bw_changed;
253 void *bw_changed_cls;
255 struct GNUNET_CONTAINER_MultiHashMap *prefs;
257 struct PreferenceClient *pc_head;
258 struct PreferenceClient *pc_tail;
262 * Representation of a network
272 * Network description
277 * Total inbound quota
280 unsigned long long total_quota_in;
283 * Total outbound quota
286 unsigned long long total_quota_out;
289 * Number of active addresses for this network
291 unsigned int active_addresses;
294 * Number of total addresses for this network
296 unsigned int total_addresses;
299 * String for statistics total addresses
304 * String for statistics active addresses
308 struct AddressWrapper *head;
309 struct AddressWrapper *tail;
314 * Wrapper for addresses to store them in network's linked list
316 struct AddressWrapper
321 struct AddressWrapper *next;
326 struct AddressWrapper *prev;
331 struct ATS_Address *addr;
338 struct PreferenceClient
343 struct PreferenceClient *prev;
349 struct PreferenceClient *next;
357 * Total preference for this peer
359 double f_total[GNUNET_ATS_PreferenceCount];
362 * List of peer preferences for this client
368 struct PreferencePeer *p_head;
373 struct PreferencePeer *p_tail;
379 struct PreferencePeer
384 struct PreferencePeer *next;
389 struct PreferencePeer *prev;
394 struct PreferenceClient *client;
399 struct GAS_PROPORTIONAL_Handle *s;
404 struct GNUNET_PeerIdentity id;
409 double f[GNUNET_ATS_PreferenceCount];
412 * Relative Preference Values
414 double f_rel[GNUNET_ATS_PreferenceCount];
417 * Relative Total Preference Value
421 GNUNET_SCHEDULER_TaskIdentifier aging_task;
426 * Important solver functions
427 * ---------------------------
431 * Test if bandwidth is available in this network to add an additional address
433 * @param net the network type to update
434 * @return GNUNET_YES or GNUNET_NO
437 is_bandwidth_available_in_network (struct Network *net)
439 unsigned int na = net->active_addresses + 1;
440 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
441 if (((net->total_quota_in / na) > min_bw) &&
442 ((net->total_quota_out / na) > min_bw))
444 LOG (GNUNET_ERROR_TYPE_DEBUG,
445 "Enough bandwidth available for %u active addresses in network `%s'\n",
451 LOG (GNUNET_ERROR_TYPE_DEBUG,
452 "Not enough bandwidth available for %u active addresses in network `%s'\n",
460 * Update bandwidth assigned to peers in this network
462 * @param s the solver handle
463 * @param net the network type to update
464 * @param address_except address excluded from notifcation, since we suggest
468 update_quota_per_network (struct GAS_PROPORTIONAL_Handle *s,
470 struct ATS_Address *address_except)
472 unsigned long long remaining_quota_in = 0;
473 unsigned long long quota_out_used = 0;
475 unsigned long long remaining_quota_out = 0;
476 unsigned long long quota_in_used = 0;
477 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
478 double total_prefs; /* Important: has to be double not float due to precision */
479 double cur_pref; /* Important: has to be double not float due to precision */
480 double *t = NULL; /* Important: has to be double not float due to precision */
482 unsigned long long assigned_quota_in = 0;
483 unsigned long long assigned_quota_out = 0;
484 struct AddressWrapper *cur;
486 LOG (GNUNET_ERROR_TYPE_DEBUG,
487 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
488 net->desc, net->active_addresses, net->total_quota_in, net->total_quota_in);
490 if (net->active_addresses == 0)
491 return; /* no addresses to update */
494 * Assign every peer in network minimum Bandwidth
495 * Distribute bandwidth left according to preference
498 if ((net->active_addresses * min_bw) > net->total_quota_in)
503 if ((net->active_addresses * min_bw) > net->total_quota_out)
509 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
510 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
511 LOG (GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
512 remaining_quota_in, remaining_quota_out);
514 for (cur = net->head; NULL != cur; cur = cur->next)
516 if (GNUNET_YES == cur->addr->active)
518 t = GNUNET_CONTAINER_multihashmap_get (s->prefs, &cur->addr->peer.hashPubKey);
520 total_prefs += DEFAULT_PREFERENCE;
527 for (cur = net->head; NULL != cur; cur = cur->next)
529 if (GNUNET_YES == cur->addr->active)
532 t = GNUNET_CONTAINER_multihashmap_get (s->prefs, &cur->addr->peer.hashPubKey);
534 cur_pref = DEFAULT_PREFERENCE;
537 assigned_quota_in = min_bw + ((cur_pref / total_prefs) * remaining_quota_in);
538 assigned_quota_out = min_bw + ((cur_pref / total_prefs) * remaining_quota_out);
540 LOG (GNUNET_ERROR_TYPE_DEBUG,
541 "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
542 GNUNET_i2s (&cur->addr->peer),
543 cur_pref, total_prefs,
544 assigned_quota_in, assigned_quota_out);
548 assigned_quota_in = 0;
549 assigned_quota_out = 0;
552 quota_in_used += assigned_quota_in;
553 quota_out_used += assigned_quota_out;
554 /* Prevent overflow due to rounding errors */
555 if (assigned_quota_in > UINT32_MAX)
556 assigned_quota_in = UINT32_MAX;
557 if (assigned_quota_out > UINT32_MAX)
558 assigned_quota_out = UINT32_MAX;
560 /* Compare to current bandwidth assigned */
561 if ((assigned_quota_in != ntohl(cur->addr->assigned_bw_in.value__)) ||
562 (assigned_quota_out != ntohl(cur->addr->assigned_bw_out.value__)))
564 cur->addr->assigned_bw_in.value__ = htonl (assigned_quota_in);
565 cur->addr->assigned_bw_out.value__ = htonl (assigned_quota_out);
566 /* Notify on change */
567 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
568 s->bw_changed (s->bw_changed_cls, cur->addr);
572 LOG (GNUNET_ERROR_TYPE_DEBUG,
573 "Total bandwidth assigned is (in/out): %llu /%llu\n",
576 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
578 LOG (GNUNET_ERROR_TYPE_ERROR,
579 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
580 net->active_addresses,
582 net->total_quota_out);
584 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
586 LOG (GNUNET_ERROR_TYPE_ERROR,
587 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
588 net->active_addresses,
590 net->total_quota_in);
598 * ---------------------------
603 * Update bandwidth assignment for all networks
605 * @param s the solver handle
608 update_all_networks (struct GAS_PROPORTIONAL_Handle *s)
611 for (i = 0; i < s->networks; i++)
612 update_quota_per_network (s, &s->network_entries[i], NULL);
618 * Lookup network struct by type
620 * @param s the solver handle
621 * @param type the network type
622 * @return the network struct
624 static struct Network *
625 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
628 for (c = 0 ; c < s->networks; c++)
630 if (s->network_entries[c].type == type)
631 return &s->network_entries[c];
638 * Hashmap Iterator to find current active address for peer
640 * @param cls last active address
641 * @param key peer's key
642 * @param value address to check
643 * @return GNUNET_NO on double active address else GNUNET_YES
646 get_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
648 struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
649 struct ATS_Address * aa = (struct ATS_Address *) value;
651 if (GNUNET_YES == aa->active)
655 /* should never happen */
656 LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
666 * Find current active address for peer
668 * @param solver the solver handle
669 * @param addresses the address set
670 * @param peer the peer
671 * @return active address or NULL
673 static struct ATS_Address *
674 get_active_address (void *solver,
675 struct GNUNET_CONTAINER_MultiHashMap * addresses,
676 const struct GNUNET_PeerIdentity *peer)
678 struct ATS_Address * dest = NULL;
680 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
682 &get_active_address_it, &dest);
687 free_pref (void *cls,
688 const struct GNUNET_HashCode * key,
705 addresse_increment (struct GAS_PROPORTIONAL_Handle *s,
710 if (GNUNET_YES == total)
712 s->total_addresses ++;
713 net->total_addresses ++;
714 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
715 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
717 if (GNUNET_YES == active)
719 net->active_addresses ++;
720 s->active_addresses ++;
721 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
722 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
728 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s,
734 if (GNUNET_YES == total)
736 if (s->total_addresses < 1)
743 s->total_addresses --;
744 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
746 if (net->total_addresses < 1)
753 net->total_addresses --;
754 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
758 if (GNUNET_YES == active)
760 if (net->active_addresses < 1)
767 net->active_addresses --;
768 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
770 if (s->active_addresses < 1)
777 s->active_addresses --;
778 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
785 * Extract an ATS performance info from an address
787 * @param address the address
788 * @param type the type to extract in HBO
789 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
792 get_performance_info (struct ATS_Address *address, uint32_t type)
795 GNUNET_assert (NULL != address);
797 if ((NULL == address->atsi) || (0 == address->atsi_count))
798 return GNUNET_ATS_VALUE_UNDEFINED;
800 for (c1 = 0; c1 < address->atsi_count; c1++)
802 if (ntohl(address->atsi[c1].type) == type)
803 return ntohl(address->atsi[c1].value);
805 return GNUNET_ATS_VALUE_UNDEFINED;
817 * Find a "good" address to use for a peer. If we already have an existing
818 * address, we stick to it. Otherwise, we pick by lowest distance and then
821 * @param cls the 'struct ATS_Address**' where we store the result
823 * @param value another 'struct ATS_Address*' to consider using
824 * @return GNUNET_OK (continue to iterate)
827 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
829 struct ATS_Address **previous_p = cls;
830 struct ATS_Address *current = (struct ATS_Address *) value;
831 struct ATS_Address *previous = *previous_p;
832 struct GNUNET_TIME_Absolute now;
833 struct Network *net = (struct Network *) current->solver_information;
834 uint32_t p_distance_cur;
835 uint32_t p_distance_prev;
836 uint32_t p_delay_cur;
837 uint32_t p_delay_prev;
839 now = GNUNET_TIME_absolute_get();
841 if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
843 /* This address is blocked for suggestion */
844 LOG (GNUNET_ERROR_TYPE_DEBUG,
845 "Address %p blocked for suggestion for %llu ms \n",
847 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
851 if (GNUNET_NO == is_bandwidth_available_in_network (net))
852 return GNUNET_OK; /* There's no bandwidth available in this network */
854 if (NULL != previous)
856 if ((0 == strcmp (previous->plugin, "tcp")) &&
857 (0 == strcmp (current->plugin, "tcp")))
859 if ((0 != previous->addr_len) &&
860 (0 == current->addr_len))
862 /* saved address was an outbound address, but we have an inbound address */
863 *previous_p = current;
866 if (0 == previous->addr_len)
868 /* saved address was an inbound address, so do not overwrite */
874 if (NULL == previous)
876 *previous_p = current;
879 if ((ntohl (previous->assigned_bw_in.value__) == 0) &&
880 (ntohl (current->assigned_bw_in.value__) > 0))
882 /* stick to existing connection */
883 *previous_p = current;
887 p_distance_prev = get_performance_info (previous, GNUNET_ATS_QUALITY_NET_DISTANCE);
888 p_distance_cur = get_performance_info (current, GNUNET_ATS_QUALITY_NET_DISTANCE);
889 if ((p_distance_prev != GNUNET_ATS_VALUE_UNDEFINED) && (p_distance_cur != GNUNET_ATS_VALUE_UNDEFINED) &&
890 (p_distance_prev > p_distance_cur))
892 /* user shorter distance */
893 *previous_p = current;
897 p_delay_prev = get_performance_info (previous, GNUNET_ATS_QUALITY_NET_DELAY);
898 p_delay_cur = get_performance_info (current, GNUNET_ATS_QUALITY_NET_DELAY);
899 if ((p_delay_prev != GNUNET_ATS_VALUE_UNDEFINED) && (p_delay_cur != GNUNET_ATS_VALUE_UNDEFINED) &&
900 (p_delay_prev > p_delay_cur))
902 /* user lower latency */
903 *previous_p = current;
916 * Preference calculation
917 * ---------------------------
922 recalculate_preferences (struct PreferencePeer *p)
924 struct GAS_PROPORTIONAL_Handle *s = p->s;
925 struct PreferencePeer *p_cur;
926 struct PreferenceClient *c_cur = p->client;
938 * Set of peers p_i in P
939 * Set of preference kinds k
940 * A preference value f_k_p_i with an unknown range
943 * A client specific relative preference f_p_i_rel [1..2] for all peers
947 * For every preference kind k:
949 * We remember for the preference f_p_i for each peer p_i.
950 * We have a default preference value f_p_i = 0
951 * We have a sum of all preferences f_t = sum (f_p_i)
952 * So we can calculate a relative preference value fr_p_i:
955 * f_k_p_i_rel = [1..2], default 1.0
957 * f_p_i_rel = sum (f_k_p_i_rel) / count(k)
962 /* For this client: for all preferences, except TERMINATOR */
963 for (kind = GNUNET_ATS_PREFERENCE_END + 1 ; kind < GNUNET_ATS_PreferenceCount; kind ++)
965 /* Recalcalculate total preference for this quality kind over all peers*/
966 c_cur->f_total[kind] = 0;
967 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
968 c_cur->f_total[kind] += p_cur->f[kind];
970 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p has total preference for %s of %.3f\n",
972 GNUNET_ATS_print_preference_type (kind),
973 c_cur->f_total[kind]);
975 /* Recalcalculate relative preference for all peers */
976 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
978 /* Calculate relative preference for specific kind */
979 if (0.0 == c_cur->f_total[kind])
981 /* No one has preference, so set default preference */
982 p_cur->f_rel[kind] = DEFAULT_PREFERENCE;
986 p_cur->f_rel[kind] = (c_cur->f_total[kind] + p_cur->f[kind]) / c_cur->f_total[kind];
988 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has relative preference for %s of %.3f\n",
990 GNUNET_i2s (&p_cur->id),
991 GNUNET_ATS_print_preference_type (kind),
994 /* Calculate peer relative preference */
995 /* Start with kind = 1 to exclude terminator */
996 p_cur->f_rel_total = 0;
997 for (rkind = GNUNET_ATS_PREFERENCE_END + 1; rkind < GNUNET_ATS_PreferenceCount; rkind ++)
999 p_cur->f_rel_total += p_cur->f_rel[rkind];
1001 p_cur->f_rel_total /= (GNUNET_ATS_PreferenceCount - 1.0); /* -1 due to terminator */
1002 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has total relative preference of %.3f\n",
1004 GNUNET_i2s (&p_cur->id),
1005 p_cur->f_rel_total);
1009 /* Calculcate global total relative peer preference over all clients */
1012 for (c_cur = s->pc_head; NULL != c_cur; c_cur = c_cur->next)
1014 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
1015 if (0 == memcmp (&p_cur->id, &p->id, sizeof (p_cur->id)))
1020 p_rel_global += p_cur->f_rel_total;
1023 p_rel_global /= clients;
1024 LOG (GNUNET_ERROR_TYPE_DEBUG, "Global preference value for peer `%s': %.3f\n",
1025 GNUNET_i2s (&p->id), p_rel_global);
1027 /* Update global map */
1028 if (NULL != (dest = GNUNET_CONTAINER_multihashmap_get(s->prefs, &p->id.hashPubKey)))
1029 (*dest) = p_rel_global;
1032 dest = GNUNET_malloc (sizeof (double));
1033 (*dest) = p_rel_global;
1034 GNUNET_CONTAINER_multihashmap_put(s->prefs,
1037 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1042 update_preference (struct PreferencePeer *p,
1043 enum GNUNET_ATS_PreferenceKind kind,
1046 double score = score_f;
1048 /* Update preference value according to type */
1050 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
1051 case GNUNET_ATS_PREFERENCE_LATENCY:
1052 p->f[kind] = (p->f[kind] + score) / 2;
1054 case GNUNET_ATS_PREFERENCE_END:
1059 recalculate_preferences(p);
1060 update_all_networks (p->s);
1065 preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1070 struct PreferencePeer *p = cls;
1071 GNUNET_assert (NULL != p);
1074 p->aging_task = GNUNET_SCHEDULER_NO_TASK;
1076 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aging preferences for peer `%s'\n",
1077 GNUNET_i2s (&p->id));
1079 /* Issue for aging :
1081 * Not for every peer preference values are set by default, so reducing the
1082 * absolute preference value does not help for aging because it does not have
1083 * influence on the relative values.
1085 * So we have to reduce the relative value to have an immediate impact on
1086 * quota calculation. In addition we cannot call recalculate_preferences here
1087 * but instead reduce the absolute value to have an aging impact on future
1088 * calls to change_preference where recalculate_preferences is called
1091 /* Aging absolute values: */
1092 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
1097 p->f[i] *= PREF_AGING_FACTOR;
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aged preference for peer `%s' from %.3f to %.3f\n",
1099 GNUNET_i2s (&p->id), backup, p->f[i]);
1102 /* Updating relative value */
1103 t = GNUNET_CONTAINER_multihashmap_get (p->s->prefs, &p->id.hashPubKey);
1111 (*t) = (*t) * PREF_AGING_FACTOR;
1114 update_all_networks (p->s);
1116 p->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
1117 &preference_aging, p);
1122 * Changes the preferences for a peer in the problem
1124 * @param solver the solver handle
1125 * @param client the client with this preference
1126 * @param peer the peer to change the preference for
1127 * @param kind the kind to change the preference
1128 * @param score the score
1131 GAS_simplistic_address_change_preference (void *solver,
1133 const struct GNUNET_PeerIdentity *peer,
1134 enum GNUNET_ATS_PreferenceKind kind,
1137 static struct GNUNET_TIME_Absolute next_update;
1138 struct GAS_PROPORTIONAL_Handle *s = solver;
1139 struct PreferenceClient *c_cur;
1140 struct PreferencePeer *p_cur;
1143 GNUNET_assert (NULL != solver);
1144 GNUNET_assert (NULL != client);
1145 GNUNET_assert (NULL != peer);
1147 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p changes preference for peer `%s' %s %f\n",
1150 GNUNET_ATS_print_preference_type (kind),
1153 if (kind >= GNUNET_ATS_PreferenceCount)
1159 /* Find preference client */
1160 for (c_cur = s->pc_head; NULL != c_cur; c_cur = c_cur->next)
1162 if (client == c_cur->client)
1165 /* Not found: create new preference client */
1168 c_cur = GNUNET_malloc (sizeof (struct PreferenceClient));
1169 c_cur->client = client;
1170 GNUNET_CONTAINER_DLL_insert (s->pc_head, s->pc_tail, c_cur);
1173 /* Find entry for peer */
1174 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
1175 if (0 == memcmp (&p_cur->id, peer, sizeof (p_cur->id)))
1178 /* Not found: create new peer entry */
1181 p_cur = GNUNET_malloc (sizeof (struct PreferencePeer));
1183 p_cur->client = c_cur;
1184 p_cur->id = (*peer);
1185 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
1187 /* Default value per peer absolut preference for a quality:
1188 * No value set, so absolute preference 0 */
1190 /* Default value per peer relative preference for a quality: 1.0 */
1191 p_cur->f_rel[i] = DEFAULT_PREFERENCE;
1193 p_cur->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL, &preference_aging, p_cur);
1194 GNUNET_CONTAINER_DLL_insert (c_cur->p_head, c_cur->p_tail, p_cur);
1197 update_preference (p_cur, kind, score);
1199 /* FIXME: We should update quotas if UPDATE_INTERVAL is reached */
1200 if (GNUNET_TIME_absolute_get().abs_value > next_update.abs_value)
1203 next_update = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
1204 MIN_UPDATE_INTERVAL);
1210 * Solver API functions
1211 * ---------------------------
1215 * Get the preferred address for a specific peer
1217 * @param solver the solver handle
1218 * @param addresses the address hashmap containing all addresses
1219 * @param peer the identity of the peer
1221 const struct ATS_Address *
1222 GAS_proportional_get_preferred_address (void *solver,
1223 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1224 const struct GNUNET_PeerIdentity *peer)
1226 struct GAS_PROPORTIONAL_Handle *s = solver;
1227 struct Network *net_prev;
1228 struct Network *net_cur;
1229 struct ATS_Address *cur;
1230 struct ATS_Address *prev;
1232 GNUNET_assert (s != NULL);
1234 /* Get address with: stick to current address, lower distance, lower latency */
1235 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
1236 &find_address_it, &cur);
1239 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
1243 LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
1244 (GNUNET_NO == cur->active) ? "inactive" : "active",
1245 cur, GNUNET_i2s (peer));
1246 net_cur = (struct Network *) cur->solver_information;
1247 if (GNUNET_YES == cur->active)
1249 /* This address was selected previously, so no need to update quotas */
1253 /* This address was not active, so we have to:
1255 * - mark previous active address as not active
1256 * - update quota for previous address network
1257 * - update quota for this address network
1260 prev = get_active_address (s, addresses, peer);
1263 net_prev = (struct Network *) prev->solver_information;
1264 prev->active = GNUNET_NO; /* No active any longer */
1265 prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
1266 prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
1267 s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
1268 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
1270 update_quota_per_network (s, net_prev, NULL);
1273 if (GNUNET_NO == (is_bandwidth_available_in_network (cur->solver_information)))
1275 GNUNET_break (0); /* This should never happen*/
1279 cur->active = GNUNET_YES;
1280 addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
1281 update_quota_per_network (s, net_cur, cur);
1288 * Stop notifying about address and bandwidth changes for this peer
1290 * @param solver the solver handle
1291 * @param addresses address hashmap
1292 * @param peer the peer
1295 GAS_proportional_stop_get_preferred_address (void *solver,
1296 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1297 const struct GNUNET_PeerIdentity *peer)
1304 * Remove an address from the solver
1306 * @param solver the solver handle
1307 * @param addresses the address hashmap containing all addresses
1308 * @param address the address to remove
1309 * @param session_only delete only session not whole address
1312 GAS_proportional_address_delete (void *solver,
1313 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1314 struct ATS_Address *address, int session_only)
1316 struct GAS_PROPORTIONAL_Handle *s = solver;
1317 struct Network *net;
1318 struct AddressWrapper *aw;
1320 /* Remove an adress completely, we have to:
1321 * - Remove from specific network
1322 * - Decrease number of total addresses
1324 * - decrease number of active addreses
1328 net = (struct Network *) address->solver_information;
1330 if (GNUNET_NO == session_only)
1332 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1333 (GNUNET_NO == address->active) ? "inactive" : "active",
1334 address, GNUNET_i2s (&address->peer),
1335 net->desc, net->total_addresses, net->active_addresses);
1337 /* Remove address */
1338 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1339 for (aw = net->head; NULL != aw; aw = aw->next)
1341 if (aw->addr == address)
1349 GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
1354 /* Remove session only: remove if active and update */
1355 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1356 (GNUNET_NO == address->active) ? "inactive" : "active",
1357 address, GNUNET_i2s (&address->peer),
1358 net->desc, net->total_addresses, net->active_addresses);
1361 if (GNUNET_YES == address->active)
1363 /* Address was active, remove from network and update quotas*/
1364 address->active = GNUNET_NO;
1365 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1367 update_quota_per_network (s, net, NULL);
1369 LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
1370 net->total_addresses,
1371 net->active_addresses,
1378 * Add a new single address to a network
1380 * @param solver the solver Handle
1381 * @param addresses the address hashmap containing all addresses
1382 * @param address the address to add
1383 * @param network network type of this address
1386 GAS_proportional_address_add (void *solver,
1387 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1388 struct ATS_Address *address,
1392 * Updates a single address in the solver and checks previous values
1394 * @param solver the solver Handle
1395 * @param addresses the address hashmap containing all addresses
1396 * @param address the update address
1397 * @param session the previous session
1398 * @param in_use the previous address in use state
1399 * @param prev_ats previous ATS information
1400 * @param prev_atsi_count the previous atsi count
1403 GAS_proportional_address_update (void *solver,
1404 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1405 struct ATS_Address *address,
1408 const struct GNUNET_ATS_Information *prev_ats,
1409 uint32_t prev_atsi_count)
1411 struct ATS_Address *new;
1412 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1414 uint32_t prev_value;
1417 int save_active = GNUNET_NO;
1418 struct Network *new_net = NULL;
1420 /* Check updates to performance information */
1421 for (i = 0; i < prev_atsi_count; i++)
1423 prev_type = ntohl (prev_ats[i].type);
1424 prev_value = ntohl (prev_ats[i].value);
1427 case GNUNET_ATS_UTILIZATION_UP:
1428 case GNUNET_ATS_UTILIZATION_DOWN:
1429 case GNUNET_ATS_QUALITY_NET_DELAY:
1430 case GNUNET_ATS_QUALITY_NET_DISTANCE:
1431 case GNUNET_ATS_COST_WAN:
1432 case GNUNET_ATS_COST_LAN:
1433 case GNUNET_ATS_COST_WLAN:
1434 /* No actions required here*/
1436 case GNUNET_ATS_NETWORK_TYPE:
1438 addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
1439 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
1442 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
1444 if (addr_net != prev_value)
1446 /* Network changed */
1447 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
1448 (GNUNET_YES == address->active) ? "active" : "inactive",
1449 GNUNET_ATS_print_network_type(prev_value),
1450 GNUNET_ATS_print_network_type(addr_net));
1452 save_active = address->active;
1453 /* remove from old network */
1454 GAS_proportional_address_delete (solver, addresses, address, GNUNET_NO);
1456 /* set new network type */
1457 new_net = get_network (solver, addr_net);
1458 address->solver_information = new_net;
1460 /* Add to new network and update*/
1461 GAS_proportional_address_add (solver, addresses, address, addr_net);
1462 if (GNUNET_YES == save_active)
1464 /* check if bandwidth available in new network */
1465 if (GNUNET_YES == (is_bandwidth_available_in_network (new_net)))
1467 /* Suggest updated address */
1468 address->active = GNUNET_YES;
1469 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
1470 update_quota_per_network (solver, new_net, NULL);
1474 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not enough bandwidth in new network, suggesting alternative address ..\n");
1476 /* Set old address to zero bw */
1477 address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
1478 address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
1479 s->bw_changed (s->bw_changed_cls, address);
1481 /* Find new address to suggest since no bandwidth in network*/
1482 new = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, addresses, &address->peer);
1485 /* Have an alternative address to suggest */
1486 s->bw_changed (s->bw_changed_cls, new);
1494 case GNUNET_ATS_ARRAY_TERMINATOR:
1497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1498 "Received unsupported ATS type %u\n", prev_type);
1505 if (address->session_id != session)
1507 LOG (GNUNET_ERROR_TYPE_DEBUG,
1508 "Session changed from %u to %u\n", session, address->session_id);
1510 if (address->used != in_use)
1512 LOG (GNUNET_ERROR_TYPE_DEBUG,
1513 "Usage changed from %u to %u\n", in_use, address->used);
1520 * Add a new single address to a network
1522 * @param solver the solver Handle
1523 * @param addresses the address hashmap containing all addresses
1524 * @param address the address to add
1525 * @param network network type of this address
1528 GAS_proportional_address_add (void *solver,
1529 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1530 struct ATS_Address *address,
1533 struct GAS_PROPORTIONAL_Handle *s = solver;
1534 struct Network *net = NULL;
1535 struct AddressWrapper *aw = NULL;
1536 GNUNET_assert (NULL != s);
1538 net = get_network (s, network);
1545 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
1547 GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
1548 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1549 aw->addr->solver_information = net;
1551 LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
1552 net->total_addresses,
1553 net->active_addresses,
1559 * Init the proportional problem solver
1562 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1563 * out_quota[i] contains outbound quota for network type i
1564 * in_quota[i] contains inbound quota for network type i
1567 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1568 * network[2] == GNUNET_ATS_NET_LAN
1569 * out_quota[2] == 65353
1570 * in_quota[2] == 65353
1572 * @param cfg configuration handle
1573 * @param stats the GNUNET_STATISTICS handle
1574 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1575 * @param out_quota array of outbound quotas
1576 * @param in_quota array of outbound quota
1577 * @param dest_length array length for quota arrays
1578 * @param bw_changed_cb callback for changed bandwidth amounts
1579 * @param bw_changed_cb_cls cls for callback
1580 * @return handle for the solver on success, NULL on fail
1583 GAS_proportional_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1584 const struct GNUNET_STATISTICS_Handle *stats,
1586 unsigned long long *out_quota,
1587 unsigned long long *in_quota,
1589 GAS_bandwidth_changed_cb bw_changed_cb,
1590 void *bw_changed_cb_cls)
1593 struct GAS_PROPORTIONAL_Handle *s = GNUNET_malloc (sizeof (struct GAS_PROPORTIONAL_Handle));
1594 struct Network * cur;
1595 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1598 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1599 s->bw_changed = bw_changed_cb;
1600 s->bw_changed_cls = bw_changed_cb_cls;
1601 s->networks = dest_length;
1602 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
1603 s->active_addresses = 0;
1604 s->total_addresses = 0;
1605 s->prefs = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1607 for (c = 0; c < dest_length; c++)
1609 cur = &s->network_entries[c];
1610 cur->total_addresses = 0;
1611 cur->active_addresses = 0;
1612 cur->type = network[c];
1613 cur->total_quota_in = in_quota[c];
1614 cur->total_quota_out = out_quota[c];
1615 cur->desc = net_str[c];
1616 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
1617 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
1624 * Shutdown the proportional problem solver
1626 * @param solver the respective handle to shutdown
1629 GAS_proportional_done (void *solver)
1631 struct GAS_PROPORTIONAL_Handle *s = solver;
1632 struct PreferenceClient *pc;
1633 struct PreferenceClient *next_pc;
1634 struct PreferencePeer *p;
1635 struct PreferencePeer *next_p;
1636 struct AddressWrapper *cur;
1637 struct AddressWrapper *next;
1639 GNUNET_assert (s != NULL);
1641 for (c = 0; c < s->networks; c++)
1643 if (s->network_entries[c].total_addresses > 0)
1645 LOG (GNUNET_ERROR_TYPE_ERROR,
1646 "Had %u addresses for network `%s' not deleted during shutdown\n",
1647 s->network_entries[c].total_addresses,
1648 s->network_entries[c].desc);
1652 if (s->network_entries[c].active_addresses > 0)
1654 LOG (GNUNET_ERROR_TYPE_ERROR,
1655 "Had %u active addresses for network `%s' not deleted during shutdown\n",
1656 s->network_entries[c].active_addresses,
1657 s->network_entries[c].desc);
1661 next = s->network_entries[c].head;
1662 while (NULL != (cur = next))
1665 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
1666 s->network_entries[c].tail,
1670 GNUNET_free (s->network_entries[c].stat_total);
1671 GNUNET_free (s->network_entries[c].stat_active);
1673 if (s->total_addresses > 0)
1675 LOG (GNUNET_ERROR_TYPE_ERROR,
1676 "Had %u addresses not deleted during shutdown\n",
1677 s->total_addresses);
1680 if (s->active_addresses > 0)
1682 LOG (GNUNET_ERROR_TYPE_ERROR,
1683 "Had %u active addresses not deleted during shutdown\n",
1684 s->active_addresses);
1687 GNUNET_free (s->network_entries);
1689 next_pc = s->pc_head;
1690 while (NULL != (pc = next_pc))
1693 GNUNET_CONTAINER_DLL_remove (s->pc_head, s->pc_tail, pc);
1694 next_p = pc->p_head;
1695 while (NULL != (p = next_p))
1698 if (GNUNET_SCHEDULER_NO_TASK != p->aging_task)
1700 GNUNET_SCHEDULER_cancel(p->aging_task);
1701 p->aging_task = GNUNET_SCHEDULER_NO_TASK;
1703 GNUNET_CONTAINER_DLL_remove (pc->p_head, pc->p_tail, p);
1709 GNUNET_CONTAINER_multihashmap_iterate (s->prefs, &free_pref, NULL);
1710 GNUNET_CONTAINER_multihashmap_destroy (s->prefs);
1715 /* end of gnunet-service-ats-solver_proportional.c */