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;
313 struct AddressWrapper
315 struct AddressWrapper *next;
316 struct AddressWrapper *prev;
318 struct ATS_Address *addr;
322 struct PreferenceClient
324 struct PreferenceClient *prev;
325 struct PreferenceClient *next;
328 double f_total[GNUNET_ATS_PreferenceCount];
330 struct PreferencePeer *p_head;
331 struct PreferencePeer *p_tail;
335 struct PreferencePeer
337 struct PreferencePeer *next;
338 struct PreferencePeer *prev;
339 struct PreferenceClient *client;
340 struct GAS_PROPORTIONAL_Handle *s;
341 struct GNUNET_PeerIdentity id;
343 double f[GNUNET_ATS_PreferenceCount];
344 double f_rel[GNUNET_ATS_PreferenceCount];
347 GNUNET_SCHEDULER_TaskIdentifier aging_task;
352 * Important solver functions
353 * ---------------------------
357 * Test if bandwidth is available in this network to add an additional address
359 * @param net the network type to update
360 * @return GNUNET_YES or GNUNET_NO
363 is_bandwidth_available_in_network (struct Network *net)
365 unsigned int na = net->active_addresses + 1;
366 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
367 if (((net->total_quota_in / na) > min_bw) &&
368 ((net->total_quota_out / na) > min_bw))
370 LOG (GNUNET_ERROR_TYPE_DEBUG,
371 "Enough bandwidth available for %u active addresses in network `%s'\n",
377 LOG (GNUNET_ERROR_TYPE_DEBUG,
378 "Not enough bandwidth available for %u active addresses in network `%s'\n",
386 * Update bandwidth assigned to peers in this network
388 * @param s the solver handle
389 * @param net the network type to update
390 * @param address_except address excluded from notifcation, since we suggest
394 update_quota_per_network (struct GAS_PROPORTIONAL_Handle *s,
396 struct ATS_Address *address_except)
398 unsigned long long remaining_quota_in = 0;
399 unsigned long long quota_out_used = 0;
401 unsigned long long remaining_quota_out = 0;
402 unsigned long long quota_in_used = 0;
403 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
404 double total_prefs; /* Important: has to be double not float due to precision */
405 double cur_pref; /* Important: has to be double not float due to precision */
406 double *t = NULL; /* Important: has to be double not float due to precision */
408 unsigned long long assigned_quota_in = 0;
409 unsigned long long assigned_quota_out = 0;
410 struct AddressWrapper *cur;
412 LOG (GNUNET_ERROR_TYPE_DEBUG,
413 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
414 net->desc, net->active_addresses, net->total_quota_in, net->total_quota_in);
416 if (net->active_addresses == 0)
417 return; /* no addresses to update */
420 * Assign every peer in network minimum Bandwidth
421 * Distribute bandwidth left according to preference
424 if ((net->active_addresses * min_bw) > net->total_quota_in)
429 if ((net->active_addresses * min_bw) > net->total_quota_out)
435 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
436 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
437 LOG (GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
438 remaining_quota_in, remaining_quota_out);
440 for (cur = net->head; NULL != cur; cur = cur->next)
442 if (GNUNET_YES == cur->addr->active)
444 t = GNUNET_CONTAINER_multihashmap_get (s->prefs, &cur->addr->peer.hashPubKey);
446 total_prefs += DEFAULT_PREFERENCE;
453 for (cur = net->head; NULL != cur; cur = cur->next)
455 if (GNUNET_YES == cur->addr->active)
458 t = GNUNET_CONTAINER_multihashmap_get (s->prefs, &cur->addr->peer.hashPubKey);
460 cur_pref = DEFAULT_PREFERENCE;
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);
524 * ---------------------------
529 * Update bandwidth assignment for all networks
531 * @param s the solver handle
534 update_all_networks (struct GAS_PROPORTIONAL_Handle *s)
537 for (i = 0; i < s->networks; i++)
538 update_quota_per_network (s, &s->network_entries[i], NULL);
544 * Lookup network struct by type
546 * @param s the solver handle
547 * @param type the network type
548 * @return the network struct
550 static struct Network *
551 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
554 for (c = 0 ; c < s->networks; c++)
556 if (s->network_entries[c].type == type)
557 return &s->network_entries[c];
564 * Hashmap Iterator to find current active address for peer
566 * @param cls last active address
567 * @param key peer's key
568 * @param value address to check
569 * @return GNUNET_NO on double active address else GNUNET_YES
572 get_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
574 struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
575 struct ATS_Address * aa = (struct ATS_Address *) value;
577 if (GNUNET_YES == aa->active)
581 /* should never happen */
582 LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
592 * Find current active address for peer
594 * @param solver the solver handle
595 * @param addresses the address set
596 * @param peer the peer
597 * @return active address or NULL
599 static struct ATS_Address *
600 get_active_address (void *solver,
601 struct GNUNET_CONTAINER_MultiHashMap * addresses,
602 const struct GNUNET_PeerIdentity *peer)
604 struct ATS_Address * dest = NULL;
606 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
608 &get_active_address_it, &dest);
613 free_pref (void *cls,
614 const struct GNUNET_HashCode * key,
631 addresse_increment (struct GAS_PROPORTIONAL_Handle *s,
636 if (GNUNET_YES == total)
638 s->total_addresses ++;
639 net->total_addresses ++;
640 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
641 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
643 if (GNUNET_YES == active)
645 net->active_addresses ++;
646 s->active_addresses ++;
647 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
648 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
654 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s,
660 if (GNUNET_YES == total)
662 if (s->total_addresses < 1)
669 s->total_addresses --;
670 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
672 if (net->total_addresses < 1)
679 net->total_addresses --;
680 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
684 if (GNUNET_YES == active)
686 if (net->active_addresses < 1)
693 net->active_addresses --;
694 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
696 if (s->active_addresses < 1)
703 s->active_addresses --;
704 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
711 * Extract an ATS performance info from an address
713 * @param address the address
714 * @param type the type to extract in HBO
715 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
718 get_performance_info (struct ATS_Address *address, uint32_t type)
721 GNUNET_assert (NULL != address);
723 if ((NULL == address->atsi) || (0 == address->atsi_count))
724 return GNUNET_ATS_VALUE_UNDEFINED;
726 for (c1 = 0; c1 < address->atsi_count; c1++)
728 if (ntohl(address->atsi[c1].type) == type)
729 return ntohl(address->atsi[c1].value);
731 return GNUNET_ATS_VALUE_UNDEFINED;
743 * Find a "good" address to use for a peer. If we already have an existing
744 * address, we stick to it. Otherwise, we pick by lowest distance and then
747 * @param cls the 'struct ATS_Address**' where we store the result
749 * @param value another 'struct ATS_Address*' to consider using
750 * @return GNUNET_OK (continue to iterate)
753 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
755 struct ATS_Address **previous_p = cls;
756 struct ATS_Address *current = (struct ATS_Address *) value;
757 struct ATS_Address *previous = *previous_p;
758 struct GNUNET_TIME_Absolute now;
759 struct Network *net = (struct Network *) current->solver_information;
760 uint32_t p_distance_cur;
761 uint32_t p_distance_prev;
762 uint32_t p_delay_cur;
763 uint32_t p_delay_prev;
765 now = GNUNET_TIME_absolute_get();
767 if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
769 /* This address is blocked for suggestion */
770 LOG (GNUNET_ERROR_TYPE_DEBUG,
771 "Address %p blocked for suggestion for %llu ms \n",
773 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
777 if (GNUNET_NO == is_bandwidth_available_in_network (net))
778 return GNUNET_OK; /* There's no bandwidth available in this network */
780 if (NULL != previous)
782 if ((0 == strcmp (previous->plugin, "tcp")) &&
783 (0 == strcmp (current->plugin, "tcp")))
785 if ((0 != previous->addr_len) &&
786 (0 == current->addr_len))
788 /* saved address was an outbound address, but we have an inbound address */
789 *previous_p = current;
792 if (0 == previous->addr_len)
794 /* saved address was an inbound address, so do not overwrite */
800 if (NULL == previous)
802 *previous_p = current;
805 if ((ntohl (previous->assigned_bw_in.value__) == 0) &&
806 (ntohl (current->assigned_bw_in.value__) > 0))
808 /* stick to existing connection */
809 *previous_p = current;
813 p_distance_prev = get_performance_info (previous, GNUNET_ATS_QUALITY_NET_DISTANCE);
814 p_distance_cur = get_performance_info (current, GNUNET_ATS_QUALITY_NET_DISTANCE);
815 if ((p_distance_prev != GNUNET_ATS_VALUE_UNDEFINED) && (p_distance_cur != GNUNET_ATS_VALUE_UNDEFINED) &&
816 (p_distance_prev > p_distance_cur))
818 /* user shorter distance */
819 *previous_p = current;
823 p_delay_prev = get_performance_info (previous, GNUNET_ATS_QUALITY_NET_DELAY);
824 p_delay_cur = get_performance_info (current, GNUNET_ATS_QUALITY_NET_DELAY);
825 if ((p_delay_prev != GNUNET_ATS_VALUE_UNDEFINED) && (p_delay_cur != GNUNET_ATS_VALUE_UNDEFINED) &&
826 (p_delay_prev > p_delay_cur))
828 /* user lower latency */
829 *previous_p = current;
842 * Preference calculation
843 * ---------------------------
848 recalculate_preferences (struct PreferencePeer *p)
850 struct GAS_PROPORTIONAL_Handle *s = p->s;
851 struct PreferencePeer *p_cur;
852 struct PreferenceClient *c_cur = p->client;
864 * Set of peers p_i in P
865 * Set of preference kinds k
866 * A preference value f_k_p_i with an unknown range
869 * A client specific relative preference f_p_i_rel [1..2] for all peers
873 * For every preference kind k:
875 * We remember for the preference f_p_i for each peer p_i.
876 * We have a default preference value f_p_i = 0
877 * We have a sum of all preferences f_t = sum (f_p_i)
878 * So we can calculate a relative preference value fr_p_i:
881 * f_k_p_i_rel = [1..2], default 1.0
883 * f_p_i_rel = sum (f_k_p_i_rel) / count(k)
888 /* For this client: for all preferences, except TERMINATOR */
889 for (kind = GNUNET_ATS_PREFERENCE_END + 1 ; kind < GNUNET_ATS_PreferenceCount; kind ++)
891 /* Recalcalculate total preference for this quality kind over all peers*/
892 c_cur->f_total[kind] = 0;
893 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
894 c_cur->f_total[kind] += p_cur->f[kind];
896 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p has total preference for %s of %.3f\n",
898 GNUNET_ATS_print_preference_type (kind),
899 c_cur->f_total[kind]);
901 /* Recalcalculate relative preference for all peers */
902 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
904 /* Calculate relative preference for specific kind */
905 if (0.0 == c_cur->f_total[kind])
907 /* No one has preference, so set default preference */
908 p_cur->f_rel[kind] = DEFAULT_PREFERENCE;
912 p_cur->f_rel[kind] = (c_cur->f_total[kind] + p_cur->f[kind]) / c_cur->f_total[kind];
914 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has relative preference for %s of %.3f\n",
916 GNUNET_i2s (&p_cur->id),
917 GNUNET_ATS_print_preference_type (kind),
920 /* Calculate peer relative preference */
921 /* Start with kind = 1 to exclude terminator */
922 p_cur->f_rel_total = 0;
923 for (rkind = GNUNET_ATS_PREFERENCE_END + 1; rkind < GNUNET_ATS_PreferenceCount; rkind ++)
925 p_cur->f_rel_total += p_cur->f_rel[rkind];
927 p_cur->f_rel_total /= (GNUNET_ATS_PreferenceCount - 1.0); /* -1 due to terminator */
928 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has total relative preference of %.3f\n",
930 GNUNET_i2s (&p_cur->id),
935 /* Calculcate global total relative peer preference over all clients */
938 for (c_cur = s->pc_head; NULL != c_cur; c_cur = c_cur->next)
940 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
941 if (0 == memcmp (&p_cur->id, &p->id, sizeof (p_cur->id)))
946 p_rel_global += p_cur->f_rel_total;
949 p_rel_global /= clients;
950 LOG (GNUNET_ERROR_TYPE_DEBUG, "Global preference value for peer `%s': %.3f\n",
951 GNUNET_i2s (&p->id), p_rel_global);
953 /* Update global map */
954 if (NULL != (dest = GNUNET_CONTAINER_multihashmap_get(s->prefs, &p->id.hashPubKey)))
955 (*dest) = p_rel_global;
958 dest = GNUNET_malloc (sizeof (double));
959 (*dest) = p_rel_global;
960 GNUNET_CONTAINER_multihashmap_put(s->prefs,
963 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
968 update_preference (struct PreferencePeer *p,
969 enum GNUNET_ATS_PreferenceKind kind,
972 double score = score_f;
974 /* Update preference value according to type */
976 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
977 case GNUNET_ATS_PREFERENCE_LATENCY:
978 p->f[kind] = (p->f[kind] + score) / 2;
980 case GNUNET_ATS_PREFERENCE_END:
985 recalculate_preferences(p);
986 update_all_networks (p->s);
991 preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
996 struct PreferencePeer *p = cls;
997 GNUNET_assert (NULL != p);
1000 p->aging_task = GNUNET_SCHEDULER_NO_TASK;
1002 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aging preferences for peer `%s'\n",
1003 GNUNET_i2s (&p->id));
1005 /* Issue for aging :
1007 * Not for every peer preference values are set by default, so reducing the
1008 * absolute preference value does not help for aging because it does not have
1009 * influence on the relative values.
1011 * So we have to reduce the relative value to have an immediate impact on
1012 * quota calculation. In addition we cannot call recalculate_preferences here
1013 * but instead reduce the absolute value to have an aging impact on future
1014 * calls to change_preference where recalculate_preferences is called
1017 /* Aging absolute values: */
1018 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
1023 p->f[i] *= PREF_AGING_FACTOR;
1024 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aged preference for peer `%s' from %.3f to %.3f\n",
1025 GNUNET_i2s (&p->id), backup, p->f[i]);
1028 /* Updating relative value */
1029 t = GNUNET_CONTAINER_multihashmap_get (p->s->prefs, &p->id.hashPubKey);
1037 (*t) = (*t) * PREF_AGING_FACTOR;
1040 update_all_networks (p->s);
1042 p->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
1043 &preference_aging, p);
1048 * Changes the preferences for a peer in the problem
1050 * @param solver the solver handle
1051 * @param client the client with this preference
1052 * @param peer the peer to change the preference for
1053 * @param kind the kind to change the preference
1054 * @param score the score
1057 GAS_simplistic_address_change_preference (void *solver,
1059 const struct GNUNET_PeerIdentity *peer,
1060 enum GNUNET_ATS_PreferenceKind kind,
1063 static struct GNUNET_TIME_Absolute next_update;
1064 struct GAS_PROPORTIONAL_Handle *s = solver;
1065 struct PreferenceClient *c_cur;
1066 struct PreferencePeer *p_cur;
1069 GNUNET_assert (NULL != solver);
1070 GNUNET_assert (NULL != client);
1071 GNUNET_assert (NULL != peer);
1073 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p changes preference for peer `%s' %s %f\n",
1076 GNUNET_ATS_print_preference_type (kind),
1079 if (kind >= GNUNET_ATS_PreferenceCount)
1085 /* Find preference client */
1086 for (c_cur = s->pc_head; NULL != c_cur; c_cur = c_cur->next)
1088 if (client == c_cur->client)
1091 /* Not found: create new preference client */
1094 c_cur = GNUNET_malloc (sizeof (struct PreferenceClient));
1095 c_cur->client = client;
1096 GNUNET_CONTAINER_DLL_insert (s->pc_head, s->pc_tail, c_cur);
1099 /* Find entry for peer */
1100 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
1101 if (0 == memcmp (&p_cur->id, peer, sizeof (p_cur->id)))
1104 /* Not found: create new peer entry */
1107 p_cur = GNUNET_malloc (sizeof (struct PreferencePeer));
1109 p_cur->client = c_cur;
1110 p_cur->id = (*peer);
1111 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
1113 /* Default value per peer absolut preference for a quality:
1114 * No value set, so absolute preference 0 */
1116 /* Default value per peer relative preference for a quality: 1.0 */
1117 p_cur->f_rel[i] = DEFAULT_PREFERENCE;
1119 p_cur->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL, &preference_aging, p_cur);
1120 GNUNET_CONTAINER_DLL_insert (c_cur->p_head, c_cur->p_tail, p_cur);
1123 update_preference (p_cur, kind, score);
1125 /* FIXME: We should update quotas if UPDATE_INTERVAL is reached */
1126 if (GNUNET_TIME_absolute_get().abs_value > next_update.abs_value)
1129 next_update = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
1130 MIN_UPDATE_INTERVAL);
1136 * Solver API functions
1137 * ---------------------------
1141 * Get the preferred address for a specific peer
1143 * @param solver the solver handle
1144 * @param addresses the address hashmap containing all addresses
1145 * @param peer the identity of the peer
1147 const struct ATS_Address *
1148 GAS_proportional_get_preferred_address (void *solver,
1149 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1150 const struct GNUNET_PeerIdentity *peer)
1152 struct GAS_PROPORTIONAL_Handle *s = solver;
1153 struct Network *net_prev;
1154 struct Network *net_cur;
1155 struct ATS_Address *cur;
1156 struct ATS_Address *prev;
1158 GNUNET_assert (s != NULL);
1160 /* Get address with: stick to current address, lower distance, lower latency */
1161 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
1162 &find_address_it, &cur);
1165 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
1169 LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
1170 (GNUNET_NO == cur->active) ? "inactive" : "active",
1171 cur, GNUNET_i2s (peer));
1172 net_cur = (struct Network *) cur->solver_information;
1173 if (GNUNET_YES == cur->active)
1175 /* This address was selected previously, so no need to update quotas */
1179 /* This address was not active, so we have to:
1181 * - mark previous active address as not active
1182 * - update quota for previous address network
1183 * - update quota for this address network
1186 prev = get_active_address (s, addresses, peer);
1189 net_prev = (struct Network *) prev->solver_information;
1190 prev->active = GNUNET_NO; /* No active any longer */
1191 prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
1192 prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
1193 s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
1194 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
1196 update_quota_per_network (s, net_prev, NULL);
1199 if (GNUNET_NO == (is_bandwidth_available_in_network (cur->solver_information)))
1201 GNUNET_break (0); /* This should never happen*/
1205 cur->active = GNUNET_YES;
1206 addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
1207 update_quota_per_network (s, net_cur, cur);
1214 * Stop notifying about address and bandwidth changes for this peer
1216 * @param solver the solver handle
1217 * @param addresses address hashmap
1218 * @param peer the peer
1221 GAS_proportional_stop_get_preferred_address (void *solver,
1222 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1223 const struct GNUNET_PeerIdentity *peer)
1230 * Remove an address from the solver
1232 * @param solver the solver handle
1233 * @param addresses the address hashmap containing all addresses
1234 * @param address the address to remove
1235 * @param session_only delete only session not whole address
1238 GAS_proportional_address_delete (void *solver,
1239 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1240 struct ATS_Address *address, int session_only)
1242 struct GAS_PROPORTIONAL_Handle *s = solver;
1243 struct Network *net;
1244 struct AddressWrapper *aw;
1246 /* Remove an adress completely, we have to:
1247 * - Remove from specific network
1248 * - Decrease number of total addresses
1250 * - decrease number of active addreses
1254 net = (struct Network *) address->solver_information;
1256 if (GNUNET_NO == session_only)
1258 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1259 (GNUNET_NO == address->active) ? "inactive" : "active",
1260 address, GNUNET_i2s (&address->peer),
1261 net->desc, net->total_addresses, net->active_addresses);
1263 /* Remove address */
1264 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1265 for (aw = net->head; NULL != aw; aw = aw->next)
1267 if (aw->addr == address)
1275 GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
1280 /* Remove session only: remove if active and update */
1281 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1282 (GNUNET_NO == address->active) ? "inactive" : "active",
1283 address, GNUNET_i2s (&address->peer),
1284 net->desc, net->total_addresses, net->active_addresses);
1287 if (GNUNET_YES == address->active)
1289 /* Address was active, remove from network and update quotas*/
1290 address->active = GNUNET_NO;
1291 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1293 update_quota_per_network (s, net, NULL);
1295 LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
1296 net->total_addresses,
1297 net->active_addresses,
1304 * Add a new single address to a network
1306 * @param solver the solver Handle
1307 * @param addresses the address hashmap containing all addresses
1308 * @param address the address to add
1309 * @param network network type of this address
1312 GAS_proportional_address_add (void *solver,
1313 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1314 struct ATS_Address *address,
1318 * Updates a single address in the solver
1320 * If ATS information was updated, the previous values are passed
1322 * @param solver the solver Handle
1323 * @param addresses the address hashmap containing all addresses
1324 * @param address the update address
1325 * @param session the new session (if changed otherwise current)
1326 * @param in_use the new address in use state (if changed otherwise current)
1327 * @param prev_ats ATS information
1328 * @param prev_atsi_count the atsi count
1331 GAS_proportional_address_update (void *solver,
1332 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1333 struct ATS_Address *address,
1336 const struct GNUNET_ATS_Information *prev_ats,
1337 uint32_t prev_atsi_count)
1339 struct ATS_Address *new;
1340 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1342 uint32_t prev_value;
1345 int save_active = GNUNET_NO;
1346 struct Network *new_net = NULL;
1347 for (i = 0; i < prev_atsi_count; i++)
1349 prev_type = ntohl (prev_ats[i].type);
1350 prev_value = ntohl (prev_ats[i].value);
1353 case GNUNET_ATS_UTILIZATION_UP:
1354 case GNUNET_ATS_UTILIZATION_DOWN:
1355 case GNUNET_ATS_QUALITY_NET_DELAY:
1356 case GNUNET_ATS_QUALITY_NET_DISTANCE:
1357 case GNUNET_ATS_COST_WAN:
1358 case GNUNET_ATS_COST_LAN:
1359 case GNUNET_ATS_COST_WLAN:
1360 /* No actions required here*/
1362 case GNUNET_ATS_NETWORK_TYPE:
1364 addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
1365 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
1368 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
1370 if (addr_net != prev_value)
1372 /* Network changed */
1373 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
1374 (GNUNET_YES == address->active) ? "active" : "inactive",
1375 GNUNET_ATS_print_network_type(prev_value),
1376 GNUNET_ATS_print_network_type(addr_net));
1378 save_active = address->active;
1379 /* remove from old network */
1380 GAS_proportional_address_delete (solver, addresses, address, GNUNET_NO);
1382 /* set new network type */
1383 new_net = get_network (solver, addr_net);
1384 address->solver_information = new_net;
1386 /* Add to new network and update*/
1387 GAS_proportional_address_add (solver, addresses, address, addr_net);
1388 if (GNUNET_YES == save_active)
1390 /* check if bandwidth available in new network */
1391 if (GNUNET_YES == (is_bandwidth_available_in_network (new_net)))
1393 /* Suggest updated address */
1394 address->active = GNUNET_YES;
1395 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
1396 update_quota_per_network (solver, new_net, NULL);
1400 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not enough bandwidth in new network, suggesting alternative address ..\n");
1402 /* Set old address to zero bw */
1403 address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
1404 address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
1405 s->bw_changed (s->bw_changed_cls, address);
1407 /* Find new address to suggest since no bandwidth in network*/
1408 new = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, addresses, &address->peer);
1411 /* Have an alternative address to suggest */
1412 s->bw_changed (s->bw_changed_cls, new);
1420 case GNUNET_ATS_ARRAY_TERMINATOR:
1423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1424 "Received unsupported ATS type %u\n", prev_type);
1431 if (address->session_id != session)
1433 LOG (GNUNET_ERROR_TYPE_DEBUG,
1434 "Session changed from %u to %u\n", address->session_id, session);
1435 address->session_id = session;
1437 if (address->used != in_use)
1439 LOG (GNUNET_ERROR_TYPE_DEBUG,
1440 "Usage changed from %u to %u\n", address->used, in_use);
1441 address->used = in_use;
1448 * Add a new single address to a network
1450 * @param solver the solver Handle
1451 * @param addresses the address hashmap containing all addresses
1452 * @param address the address to add
1453 * @param network network type of this address
1456 GAS_proportional_address_add (void *solver,
1457 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1458 struct ATS_Address *address,
1461 struct GAS_PROPORTIONAL_Handle *s = solver;
1462 struct Network *net = NULL;
1463 struct AddressWrapper *aw = NULL;
1466 GNUNET_assert (NULL != s);
1467 for (c = 0; c < s->networks; c++)
1469 net = &s->network_entries[c];
1470 if (network == net->type)
1479 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
1481 GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
1482 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1483 aw->addr->solver_information = net;
1485 LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
1486 net->total_addresses,
1487 net->active_addresses,
1493 * Init the proportional problem solver
1496 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1497 * out_quota[i] contains outbound quota for network type i
1498 * in_quota[i] contains inbound quota for network type i
1501 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1502 * network[2] == GNUNET_ATS_NET_LAN
1503 * out_quota[2] == 65353
1504 * in_quota[2] == 65353
1506 * @param cfg configuration handle
1507 * @param stats the GNUNET_STATISTICS handle
1508 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1509 * @param out_quota array of outbound quotas
1510 * @param in_quota array of outbound quota
1511 * @param dest_length array length for quota arrays
1512 * @param bw_changed_cb callback for changed bandwidth amounts
1513 * @param bw_changed_cb_cls cls for callback
1514 * @return handle for the solver on success, NULL on fail
1517 GAS_proportional_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1518 const struct GNUNET_STATISTICS_Handle *stats,
1520 unsigned long long *out_quota,
1521 unsigned long long *in_quota,
1523 GAS_bandwidth_changed_cb bw_changed_cb,
1524 void *bw_changed_cb_cls)
1527 struct GAS_PROPORTIONAL_Handle *s = GNUNET_malloc (sizeof (struct GAS_PROPORTIONAL_Handle));
1528 struct Network * cur;
1529 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1532 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1533 s->bw_changed = bw_changed_cb;
1534 s->bw_changed_cls = bw_changed_cb_cls;
1535 s->networks = dest_length;
1536 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
1537 s->active_addresses = 0;
1538 s->total_addresses = 0;
1539 s->prefs = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1541 for (c = 0; c < dest_length; c++)
1543 cur = &s->network_entries[c];
1544 cur->total_addresses = 0;
1545 cur->active_addresses = 0;
1546 cur->type = network[c];
1547 cur->total_quota_in = in_quota[c];
1548 cur->total_quota_out = out_quota[c];
1549 cur->desc = net_str[c];
1550 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
1551 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
1558 * Shutdown the proportional problem solver
1560 * @param solver the respective handle to shutdown
1563 GAS_proportional_done (void *solver)
1565 struct GAS_PROPORTIONAL_Handle *s = solver;
1566 struct PreferenceClient *pc;
1567 struct PreferenceClient *next_pc;
1568 struct PreferencePeer *p;
1569 struct PreferencePeer *next_p;
1570 struct AddressWrapper *cur;
1571 struct AddressWrapper *next;
1573 GNUNET_assert (s != NULL);
1575 for (c = 0; c < s->networks; c++)
1577 if (s->network_entries[c].total_addresses > 0)
1579 LOG (GNUNET_ERROR_TYPE_ERROR,
1580 "Had %u addresses for network `%s' not deleted during shutdown\n",
1581 s->network_entries[c].total_addresses,
1582 s->network_entries[c].desc);
1586 if (s->network_entries[c].active_addresses > 0)
1588 LOG (GNUNET_ERROR_TYPE_ERROR,
1589 "Had %u active addresses for network `%s' not deleted during shutdown\n",
1590 s->network_entries[c].active_addresses,
1591 s->network_entries[c].desc);
1595 next = s->network_entries[c].head;
1596 while (NULL != (cur = next))
1599 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
1600 s->network_entries[c].tail,
1604 GNUNET_free (s->network_entries[c].stat_total);
1605 GNUNET_free (s->network_entries[c].stat_active);
1607 if (s->total_addresses > 0)
1609 LOG (GNUNET_ERROR_TYPE_ERROR,
1610 "Had %u addresses not deleted during shutdown\n",
1611 s->total_addresses);
1614 if (s->active_addresses > 0)
1616 LOG (GNUNET_ERROR_TYPE_ERROR,
1617 "Had %u active addresses not deleted during shutdown\n",
1618 s->active_addresses);
1621 GNUNET_free (s->network_entries);
1623 next_pc = s->pc_head;
1624 while (NULL != (pc = next_pc))
1627 GNUNET_CONTAINER_DLL_remove (s->pc_head, s->pc_tail, pc);
1628 next_p = pc->p_head;
1629 while (NULL != (p = next_p))
1632 if (GNUNET_SCHEDULER_NO_TASK != p->aging_task)
1634 GNUNET_SCHEDULER_cancel(p->aging_task);
1635 p->aging_task = GNUNET_SCHEDULER_NO_TASK;
1637 GNUNET_CONTAINER_DLL_remove (pc->p_head, pc->p_tail, p);
1643 GNUNET_CONTAINER_multihashmap_iterate (s->prefs, &free_pref, NULL);
1644 GNUNET_CONTAINER_multihashmap_destroy (s->prefs);
1649 /* end of gnunet-service-ats-solver_proportional.c */