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 : proportional solver
42 * The proportional solver ("proportional") 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 proportional
80 * solver uses the addresses' solver_information field to store the
81 * proportional network it belongs to for each address.
85 * When the proportional 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 proportional
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_proportional_address_delete and the network in
108 * the address struct is updated. A lookup for the respective new
109 * proportional 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_proportional_address_add. If the address was marked as in
113 * active, the solver checks if bandwidth is available in the
114 * network and if yes sets the address to active and updates the
115 * bandwidth distribution in this network. If no bandwidth is
116 * available it sets the bandwidth for this address to 0 and tries
117 * to suggest an alternative address. If an alternative address was
118 * found, addresses' callback is called for this address.
120 * 3.5 Deleting an address
122 * When an address is removed from the solver, it removes the
123 * respective address from the network and if the address was
124 * marked as active, it updates the bandwidth distribution for this
127 * 3.6 Requesting addresses
129 * When an address is requested for a peer the solver performs a
130 * lookup for the peer entry in addresses address hashmap and
131 * selects the best address. The selection of the most suitable
132 * address is done in the find_address_it hashmap iterator
133 * described in detail in section 3.7. If no address is returned,
134 * no address can be suggested at the moment. If the address
135 * returned is marked as active, the solver can return this
136 * address. If the address is not marked as active, the solver
137 * checks if another address belongign to this peer is marked as
138 * active and marks the address as inactive, updates the bandwidth
139 * for this address to 0, call the bandwidth changed callback for
140 * this address due to the change and updates quota assignment for
141 * the addresse's network. the now in-active address is belonging
142 * to. The solver marks the new address as active and updates the
143 * bandwidth assignment for this network.
145 * 3.7 Choosing addresses
147 * Choosing the best possible address for suggestion is done by
148 * iterating over all addresses of a peer stored in addresses'
149 * hashmap and using the hashmap iterator find_address_it to select
150 * the best available address. Several checks are done when an
151 * address is selected. First if this address is currently blocked
152 * by addresses from being suggested. An address is blocked for the
153 * duration of ATS_BLOCKING_DELTA when it is suggested to
154 * transport. Next it is checked if at least
155 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
156 * in the addresse's network, because suggesting an address without
157 * bandwidth does not make sense. This also ensures that all active
158 * addresses in this network get at least the minimum amount of
159 * bandwidth assigned. In the next step the solver ensures that for
160 * tcp connections inbound connections are prefered over outbound
161 * connections. In the next stet the solver ensures that
162 * connections are prefered in the following order: * connections
163 * are already established and have bandwidth assigned *
164 * connections with a shorter distance * connectes have a shorter
167 * 3.8 Changing preferences
171 * During shutdown all network entries and aging processes are
172 * destroyed and freed.
177 * This solver assigns in and outbound bandwidth equally for all
178 * addresses in specific network type (WAN, LAN) based on configured
179 * in and outbound quota for this network.
181 * The solver is notified by addresses about changes to the addresses
182 * and recalculates the bandwith assigned if required. The solver
183 * notifies addresses by calling the GAS_bandwidth_changed_cb
191 * For each peer only a single is selected and marked as "active" in the address
196 * You have the networks WAN and LAN and quotas
197 * WAN_TOTAL_IN, WAN_TOTAL_OUT
198 * LAN_TOTAL_IN, LAN_TOTAL_OUT
200 * If you have x addresses in the network segment LAN, the quotas are
201 * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
203 * Quotas are automatically recalculated and reported back when addresses are
208 #define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
209 #define PREF_AGING_FACTOR 0.95
211 #define DEFAULT_REL_PREFERENCE 1.0
212 #define DEFAULT_ABS_PREFERENCE 0.0
213 #define MIN_UPDATE_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
216 * A handle for the proportional solver
218 struct GAS_PROPORTIONAL_Handle
223 struct GNUNET_STATISTICS_Handle *stats;
226 * Hashmap containing all valid addresses
228 const struct GNUNET_CONTAINER_MultiHashMap *addresses;
231 * Pending address requests
233 struct GNUNET_CONTAINER_MultiHashMap *requests;
236 * Bandwidth changed callback
238 GAS_bandwidth_changed_cb bw_changed;
241 * Bandwidth changed callback cls
243 void *bw_changed_cls;
246 * ATS function to get preferences
248 GAS_get_preferences get_preferences;
251 * Closure for ATS function to get preferences
253 void *get_preferences_cls;
256 * ATS function to get properties
258 GAS_get_properties get_properties;
261 * Closure for ATS function to get properties
263 void *get_properties_cls;
271 * Number of changes while solver was locked
276 * Total number of addresses for solver
278 unsigned int total_addresses;
281 * Number of active addresses for solver
283 unsigned int active_addresses;
288 struct Network *network_entries;
293 unsigned int networks;
298 * Representation of a network
308 * Network description
313 * Total inbound quota
316 unsigned long long total_quota_in;
319 * Total outbound quota
322 unsigned long long total_quota_out;
325 * Number of active addresses for this network
327 unsigned int active_addresses;
330 * Number of total addresses for this network
332 unsigned int total_addresses;
335 * String for statistics total addresses
340 * String for statistics active addresses
344 struct AddressWrapper *head;
345 struct AddressWrapper *tail;
349 * Wrapper for addresses to store them in network's linked list
351 struct AddressWrapper
356 struct AddressWrapper *next;
361 struct AddressWrapper *prev;
366 struct ATS_Address *addr;
370 * Important solver functions
371 * ---------------------------
375 * Test if bandwidth is available in this network to add an additional address
377 * @param net the network type to update
378 * @return GNUNET_YES or GNUNET_NO
381 is_bandwidth_available_in_network (struct Network *net)
383 GNUNET_assert(NULL != net);
384 unsigned int na = net->active_addresses + 1;
385 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
386 if (((net->total_quota_in / na) > min_bw)
387 && ((net->total_quota_out / na) > min_bw))
389 LOG(GNUNET_ERROR_TYPE_DEBUG,
390 "Enough bandwidth available for %u active addresses in network `%s'\n",
395 LOG(GNUNET_ERROR_TYPE_DEBUG,
396 "Not enough bandwidth available for %u active addresses in network `%s'\n",
402 * Update bandwidth assigned to peers in this network
404 * @param s the solver handle
405 * @param net the network type to update
406 * @param address_except address excluded from notification, since we suggest
410 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
411 struct Network *net, struct ATS_Address *address_except)
413 unsigned long long remaining_quota_in = 0;
414 unsigned long long quota_out_used = 0;
416 unsigned long long remaining_quota_out = 0;
417 unsigned long long quota_in_used = 0;
418 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
420 double total_prefs; /* Important: has to be double not float due to precision */
421 double cur_pref; /* Important: has to be double not float due to precision */
422 const double *t = NULL; /* Important: has to be double not float due to precision */
425 unsigned long long assigned_quota_in = 0;
426 unsigned long long assigned_quota_out = 0;
427 struct AddressWrapper *cur;
429 if (GNUNET_YES == s->bulk_lock)
435 LOG(GNUNET_ERROR_TYPE_DEBUG,
436 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
437 net->desc, net->active_addresses, net->total_quota_in,
438 net->total_quota_in);
440 if (net->active_addresses == 0)
441 return; /* no addresses to update */
444 * Assign every peer in network minimum Bandwidth
445 * Distribute bandwidth left according to preference
448 if ((net->active_addresses * min_bw) > net->total_quota_in)
453 if ((net->active_addresses * min_bw) > net->total_quota_out)
459 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
460 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
461 LOG(GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
462 remaining_quota_in, remaining_quota_out);
464 for (cur = net->head; NULL != cur; cur = cur->next)
466 if (GNUNET_YES == cur->addr->active)
469 NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
472 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
474 if (c != GNUNET_ATS_PREFERENCE_END)
476 //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
480 total_prefs += (peer_prefs / (GNUNET_ATS_PreferenceCount - 1));
483 for (cur = net->head; NULL != cur; cur = cur->next)
485 if (GNUNET_YES == cur->addr->active)
489 NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
491 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
493 if (c != GNUNET_ATS_PREFERENCE_END)
498 assigned_quota_in = min_bw
499 + ((cur_pref / total_prefs) * remaining_quota_in);
500 assigned_quota_out = min_bw
501 + ((cur_pref / total_prefs) * remaining_quota_out);
503 LOG(GNUNET_ERROR_TYPE_DEBUG,
504 "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
505 GNUNET_i2s (&cur->addr->peer), cur_pref, total_prefs,
506 assigned_quota_in, assigned_quota_out);
510 assigned_quota_in = 0;
511 assigned_quota_out = 0;
514 quota_in_used += assigned_quota_in;
515 quota_out_used += assigned_quota_out;
516 /* Prevent overflow due to rounding errors */
517 if (assigned_quota_in > UINT32_MAX)
518 assigned_quota_in = UINT32_MAX;
519 if (assigned_quota_out > UINT32_MAX)
520 assigned_quota_out = UINT32_MAX;
522 /* Compare to current bandwidth assigned */
523 if ((assigned_quota_in != ntohl (cur->addr->assigned_bw_in.value__))
524 || (assigned_quota_out != ntohl (cur->addr->assigned_bw_out.value__)))
526 cur->addr->assigned_bw_in.value__ = htonl (assigned_quota_in);
527 cur->addr->assigned_bw_out.value__ = htonl (assigned_quota_out);
528 /* Notify on change */
529 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
530 s->bw_changed (s->bw_changed_cls, cur->addr);
534 LOG(GNUNET_ERROR_TYPE_DEBUG,
535 "Total bandwidth assigned is (in/out): %llu /%llu\n", quota_in_used,
537 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
539 LOG(GNUNET_ERROR_TYPE_ERROR,
540 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
541 net->active_addresses, quota_out_used, net->total_quota_out);
543 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
545 LOG(GNUNET_ERROR_TYPE_ERROR,
546 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
547 net->active_addresses, quota_in_used, net->total_quota_in);
551 struct FindBestAddressCtx
553 struct GAS_PROPORTIONAL_Handle *s;
554 struct ATS_Address *best;
558 find_property_index (uint32_t type)
560 int existing_types[] = GNUNET_ATS_QualityProperties;
562 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
563 if (existing_types[c] == type)
565 return GNUNET_SYSERR;
569 * Find a "good" address to use for a peer by iterating over the addresses for this peer.
570 * If we already have an existing address, we stick to it.
571 * Otherwise, we pick by lowest distance and then by lowest latency.
573 * @param cls the 'struct ATS_Address**' where we store the result
575 * @param value another 'struct ATS_Address*' to consider using
576 * @return GNUNET_OK (continue to iterate)
579 find_best_address_it (void *cls, const struct GNUNET_HashCode * key,
582 struct FindBestAddressCtx *fba_ctx = (struct FindBestAddressCtx *) cls;
583 struct ATS_Address *current = (struct ATS_Address *) value;
584 struct GNUNET_TIME_Absolute now;
585 struct Network *net = (struct Network *) current->solver_information;
586 const double *norm_prop_cur;
587 const double *norm_prop_prev;
590 now = GNUNET_TIME_absolute_get ();
592 if (current->blocked_until.abs_value_us
593 == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value_us)
595 /* This address is blocked for suggestion */
596 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address %p blocked for suggestion for %s \n",
598 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (now, current->blocked_until), GNUNET_YES));
601 if (GNUNET_NO == is_bandwidth_available_in_network (net))
602 return GNUNET_OK; /* There's no bandwidth available in this network */
603 if (NULL != fba_ctx->best)
605 GNUNET_assert(NULL != fba_ctx->best->plugin);
606 GNUNET_assert(NULL != current->plugin);
607 if (0 == strcmp (fba_ctx->best->plugin, current->plugin))
609 if ((0 != fba_ctx->best->addr_len) && (0 == current->addr_len))
611 /* saved address was an outbound address, but we have an inbound address */
612 fba_ctx->best = current;
615 if (0 == fba_ctx->best->addr_len)
617 /* saved address was an inbound address, so do not overwrite */
622 if (NULL == fba_ctx->best)
624 fba_ctx->best = current;
627 if ((ntohl (fba_ctx->best->assigned_bw_in.value__) == 0)
628 && (ntohl (current->assigned_bw_in.value__) > 0))
630 /* stick to existing connection */
631 fba_ctx->best = current;
635 norm_prop_cur = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
636 (const struct ATS_Address *) current);
637 norm_prop_prev = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
638 (const struct ATS_Address *) fba_ctx->best);
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s previous %.2f current %.2f\n",
641 "DISTANCE", norm_prop_cur[1], norm_prop_cur[1]);
643 index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
644 if (GNUNET_SYSERR == index)
649 if (norm_prop_cur[index] < norm_prop_prev[index])
651 /* user shorter distance */
652 fba_ctx->best = current;
656 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s previous %.2f current %.2f\n",
657 "DELAY", norm_prop_cur[1], norm_prop_cur[1]);
659 index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
660 if (GNUNET_SYSERR == index)
665 if (norm_prop_cur[index] < norm_prop_prev[index])
667 /* user shorter delay */
668 fba_ctx->best = current;
678 * ---------------------------
682 * Update bandwidth assignment for all networks
684 * @param s the solver handle
687 distribute_bandwidth_in_all_networks (struct GAS_PROPORTIONAL_Handle *s)
690 for (i = 0; i < s->networks; i++)
691 distribute_bandwidth_in_network (s, &s->network_entries[i], NULL );
695 * Lookup network struct by type
697 * @param s the solver handle
698 * @param type the network type
699 * @return the network struct
701 static struct Network *
702 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
705 for (c = 0; c < s->networks; c++)
707 if (s->network_entries[c].type == type)
708 return &s->network_entries[c];
715 * Hashmap Iterator to find current active address for peer
717 * @param cls last active address
718 * @param key peer's key
719 * @param value address to check
720 * @return GNUNET_NO on double active address else GNUNET_YES;
723 get_active_address_it (void *cls, const struct GNUNET_HashCode * key,
726 struct ATS_Address **dest = cls;
727 struct ATS_Address *aa = (struct ATS_Address *) value;
729 if (GNUNET_YES == aa->active)
734 /* should never happen */
735 LOG(GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n",
736 GNUNET_i2s (&aa->peer));
746 * Find current active address for peer
748 * @param solver the solver handle
749 * @param addresses the address set
750 * @param peer the peer
751 * @return active address or NULL
753 static struct ATS_Address *
754 get_active_address (void *solver,
755 struct GNUNET_CONTAINER_MultiHashMap * addresses,
756 const struct GNUNET_PeerIdentity *peer)
758 struct ATS_Address * dest = NULL;
760 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
761 &get_active_address_it, &dest);
766 addresse_increment (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
767 int total, int active)
769 if (GNUNET_YES == total)
771 s->total_addresses++;
772 net->total_addresses++;
773 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
774 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
776 if (GNUNET_YES == active)
778 net->active_addresses++;
779 s->active_addresses++;
780 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1,
782 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
788 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
789 int total, int active)
792 if (GNUNET_YES == total)
794 if (s->total_addresses < 1)
801 s->total_addresses--;
802 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
805 if (net->total_addresses < 1)
812 net->total_addresses--;
813 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
817 if (GNUNET_YES == active)
819 if (net->active_addresses < 1)
826 net->active_addresses--;
827 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
829 if (s->active_addresses < 1)
836 s->active_addresses--;
837 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
845 * Solver API functions
846 * ---------------------------
850 * Changes the preferences for a peer in the problem
852 * @param solver the solver handle
853 * @param peer the peer to change the preference for
854 * @param kind the kind to change the preference
855 * @param pref_rel the normalized preference value for this kind over all clients
858 GAS_proportional_address_change_preference (void *solver,
859 const struct GNUNET_PeerIdentity *peer, enum GNUNET_ATS_PreferenceKind kind,
862 struct GAS_PROPORTIONAL_Handle *s = solver;
863 GNUNET_assert(NULL != solver);
864 GNUNET_assert(NULL != peer);
866 distribute_bandwidth_in_all_networks (s);
870 * Get application feedback for a peer
872 * @param solver the solver handle
873 * @param application the application
874 * @param peer the peer to change the preference for
875 * @param scope the time interval for this feedback: [now - scope .. now]
876 * @param kind the kind to change the preference
877 * @param score the score
880 GAS_proportional_address_preference_feedback (void *solver, void *application,
881 const struct GNUNET_PeerIdentity *peer,
882 const struct GNUNET_TIME_Relative scope,
883 enum GNUNET_ATS_PreferenceKind kind, double score)
885 struct GAS_PROPORTIONAL_Handle *s = solver;
886 GNUNET_assert(NULL != solver);
887 GNUNET_assert(NULL != peer);
889 GNUNET_assert(NULL != s);
894 * Get the preferred address for a specific peer
896 * @param solver the solver handle
897 * @param peer the identity of the peer
899 const struct ATS_Address *
900 GAS_proportional_get_preferred_address (void *solver,
901 const struct GNUNET_PeerIdentity *peer)
903 struct GAS_PROPORTIONAL_Handle *s = solver;
904 struct Network *net_prev;
905 struct Network *net_cur;
906 struct ATS_Address *prev;
907 struct FindBestAddressCtx fba_ctx;
909 GNUNET_assert(s != NULL);
910 GNUNET_assert(peer != NULL);
912 /* Add to list of pending requests */
913 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (s->requests,
916 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (s->requests, &peer->hashPubKey, NULL,
917 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
920 /* Get address with: stick to current address, lower distance, lower latency */
924 GNUNET_CONTAINER_multihashmap_get_multiple (s->addresses, &peer->hashPubKey,
925 &find_best_address_it, &fba_ctx);
926 if (NULL == fba_ctx.best)
928 LOG(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
933 LOG(GNUNET_ERROR_TYPE_INFO, "Suggesting %s address %p for peer `%s'\n",
934 (GNUNET_NO == fba_ctx.best->active) ? "inactive" : "active", fba_ctx.best,
936 net_cur = (struct Network *) fba_ctx.best->solver_information;
937 if (NULL == fba_ctx.best)
939 LOG(GNUNET_ERROR_TYPE_ERROR,
940 "Trying to suggesting unknown address peer `%s'\n", GNUNET_i2s (peer));
944 if (GNUNET_YES == fba_ctx.best->active)
946 /* This address was selected previously, so no need to update quotas */
950 /* This address was not active, so we have to:
952 * - mark previous active address as not active
953 * - update quota for previous address network
954 * - update quota for this address network
956 prev = get_active_address (s,
957 (struct GNUNET_CONTAINER_MultiHashMap *) s->addresses, peer);
960 net_prev = (struct Network *) prev->solver_information;
961 prev->active = GNUNET_NO; /* No active any longer */
962 prev->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
963 prev->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
964 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
966 distribute_bandwidth_in_network (s, net_prev, NULL );
969 if (GNUNET_NO == (is_bandwidth_available_in_network (fba_ctx.best->solver_information)))
971 GNUNET_break(0); /* This should never happen*/
975 fba_ctx.best->active = GNUNET_YES;
976 addresse_increment (s, net_cur, GNUNET_NO, GNUNET_YES);
977 distribute_bandwidth_in_network (s, net_cur, fba_ctx.best );
982 * Stop notifying about address and bandwidth changes for this peer
984 * @param solver the solver handle
985 * @param peer the peer
988 GAS_proportional_stop_get_preferred_address (void *solver,
989 const struct GNUNET_PeerIdentity *peer)
991 struct GAS_PROPORTIONAL_Handle *s = solver;
992 struct ATS_Address *cur;
993 struct Network *cur_net;
996 == GNUNET_CONTAINER_multihashmap_contains (s->requests,
998 GNUNET_CONTAINER_multihashmap_remove (s->requests, &peer->hashPubKey,
1001 cur = get_active_address (s,
1002 (struct GNUNET_CONTAINER_MultiHashMap *) s->addresses, peer);
1005 /* Disabling current address */
1006 cur_net = (struct Network *) cur->solver_information;
1007 cur->active = GNUNET_NO; /* No active any longer */
1008 cur->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1009 cur->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1010 if (GNUNET_SYSERR == addresse_decrement (s, cur_net, GNUNET_NO, GNUNET_YES))
1012 distribute_bandwidth_in_network (s, cur_net, NULL );
1018 * Remove an address from the solver
1020 * @param solver the solver handle
1021 * @param address the address to remove
1022 * @param session_only delete only session not whole address
1025 GAS_proportional_address_delete (void *solver, struct ATS_Address *address,
1028 struct GAS_PROPORTIONAL_Handle *s = solver;
1029 struct Network *net;
1030 struct AddressWrapper *aw;
1031 const struct ATS_Address *new_address;
1034 /* Remove an adress completely, we have to:
1035 * - Remove from specific network
1036 * - Decrease number of total addresses
1038 * - decrease number of active addreses
1042 net = (struct Network *) address->solver_information;
1044 if (GNUNET_NO == session_only)
1046 LOG(GNUNET_ERROR_TYPE_INFO,
1047 "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1048 (GNUNET_NO == address->active) ? "inactive" : "active", address,
1049 GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1050 net->active_addresses);
1052 /* Remove address */
1053 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1054 for (aw = net->head; NULL != aw; aw = aw->next)
1056 if (aw->addr == address)
1064 GNUNET_CONTAINER_DLL_remove(net->head, net->tail, aw);
1069 /* Remove session only: remove if active and update */
1070 LOG(GNUNET_ERROR_TYPE_INFO,
1071 "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1072 (GNUNET_NO == address->active) ? "inactive" : "active", address,
1073 GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1074 net->active_addresses);
1077 if (GNUNET_YES == address->active)
1079 /* Address was active, remove from network and update quotas*/
1080 address->active = GNUNET_NO;
1081 address->assigned_bw_in = BANDWIDTH_ZERO;
1082 address->assigned_bw_out = BANDWIDTH_ZERO;
1084 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1086 distribute_bandwidth_in_network (s, net, NULL );
1087 if (NULL == (new_address = GAS_proportional_get_preferred_address (s, &address->peer)))
1089 /* No alternative address found, disconnect peer */
1090 s->bw_changed (s->bw_changed_cls, address);
1094 s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) new_address);
1097 LOG(GNUNET_ERROR_TYPE_DEBUG,
1098 "After deleting address now total %u and active %u addresses in network `%s'\n",
1099 net->total_addresses, net->active_addresses, net->desc);
1104 * Start a bulk operation
1106 * @param solver the solver
1109 GAS_proportional_bulk_start (void *solver)
1111 LOG(GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1112 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1114 GNUNET_assert(NULL != solver);
1119 * Bulk operation done
1122 GAS_proportional_bulk_stop (void *solver)
1124 LOG(GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1126 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1127 GNUNET_assert(NULL != solver);
1129 if (s->bulk_lock < 1)
1135 if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1137 LOG(GNUNET_ERROR_TYPE_DEBUG, "No lock pending, recalculating\n");
1138 distribute_bandwidth_in_all_networks (s);
1139 s->bulk_requests = 0;
1144 * Add a new single address to a network
1146 * @param solver the solver Handle
1147 * @param address the address to add
1148 * @param network network type of this address
1151 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1155 * Transport properties for this address have changed
1157 * @param solver solver handle
1158 * @param address the address
1159 * @param type the ATSI type in HBO
1160 * @param abs_value the absolute value of the property
1161 * @param rel_value the normalized value
1164 GAS_proportional_address_property_changed (void *solver,
1165 struct ATS_Address *address, uint32_t type, uint32_t abs_value,
1168 struct GAS_PROPORTIONAL_Handle *s;
1171 GNUNET_assert(NULL != solver);
1172 GNUNET_assert(NULL != address);
1174 s = (struct GAS_PROPORTIONAL_Handle *) solver;
1175 n = (struct Network *) address->solver_information;
1183 LOG(GNUNET_ERROR_TYPE_DEBUG,
1184 "Property `%s' for peer `%s' address %p changed to %.2f \n",
1185 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1186 address, rel_value);
1189 case GNUNET_ATS_UTILIZATION_UP:
1190 case GNUNET_ATS_UTILIZATION_DOWN:
1191 case GNUNET_ATS_QUALITY_NET_DELAY:
1192 case GNUNET_ATS_QUALITY_NET_DISTANCE:
1193 case GNUNET_ATS_COST_WAN:
1194 case GNUNET_ATS_COST_LAN:
1195 case GNUNET_ATS_COST_WLAN:
1196 distribute_bandwidth_in_network (s, n, GNUNET_NO);
1202 * Transport session for this address has changed
1204 * NOTE: values in addresses are already updated
1206 * @param solver solver handle
1207 * @param address the address
1208 * @param cur_session the current session
1209 * @param new_session the new session
1212 GAS_proportional_address_session_changed (void *solver,
1213 struct ATS_Address *address, uint32_t cur_session, uint32_t new_session)
1215 if (cur_session != new_session)
1217 LOG(GNUNET_ERROR_TYPE_DEBUG, "Session changed from %u to %u\n", cur_session,
1223 * Usage for this address has changed
1225 * NOTE: values in addresses are already updated
1227 * @param solver solver handle
1228 * @param address the address
1229 * @param in_use usage state
1232 GAS_proportional_address_inuse_changed (void *solver,
1233 struct ATS_Address *address, int in_use)
1235 LOG(GNUNET_ERROR_TYPE_DEBUG, "Usage changed to %s\n",
1236 (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1240 * Network scope for this address has changed
1242 * NOTE: values in addresses are already updated
1244 * @param solver solver handle
1245 * @param address the address
1246 * @param current_network the current network
1247 * @param new_network the new network
1250 GAS_proportional_address_change_network (void *solver,
1251 struct ATS_Address *address, uint32_t current_network, uint32_t new_network)
1253 struct ATS_Address *new;
1254 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1255 int save_active = GNUNET_NO;
1256 struct Network *new_net = NULL;
1258 if (current_network == new_network)
1264 /* Network changed */
1265 LOG(GNUNET_ERROR_TYPE_DEBUG,
1266 "Network type changed, moving %s address from `%s' to `%s'\n",
1267 (GNUNET_YES == address->active) ? "active" : "inactive",
1268 GNUNET_ATS_print_network_type (current_network),
1269 GNUNET_ATS_print_network_type (new_network));
1271 save_active = address->active;
1273 /* Disable and assign no bandwidth */
1274 address->active = GNUNET_NO;
1275 address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1276 address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1278 /* Remove from old network */
1279 GAS_proportional_address_delete (solver, address, GNUNET_NO);
1281 /* Set new network type */
1282 if (NULL == (new_net = get_network (solver, new_network)))
1284 /* Address changed to invalid network... */
1285 LOG(GNUNET_ERROR_TYPE_ERROR,
1286 _("Invalid network type `%u' `%s': Disconnect!\n"), new_network,
1287 GNUNET_ATS_print_network_type (new_network));
1289 /* Find new address to suggest since no bandwidth in network*/
1290 if (NULL == (new = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, &address->peer)))
1292 /* No alternative address found, disconnect peer */
1293 s->bw_changed (s->bw_changed_cls, address);
1298 /* Add to new network and update*/
1299 address->solver_information = new_net;
1300 GAS_proportional_address_add (solver, address, new_network);
1301 if (GNUNET_YES == save_active)
1303 /* check if bandwidth available in new network */
1304 if (GNUNET_YES == (is_bandwidth_available_in_network (new_net)))
1306 /* Assign bandwidth to updated address */
1307 address->active = GNUNET_YES;
1308 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
1309 distribute_bandwidth_in_network (solver, new_net, NULL );
1313 LOG(GNUNET_ERROR_TYPE_DEBUG,
1314 "Not enough bandwidth in new network, suggesting alternative address ..\n");
1315 /* Find new address to suggest since no bandwidth in network*/
1318 (struct ATS_Address *) GAS_proportional_get_preferred_address (s,
1321 /* No alternative address found, disconnect peer */
1322 s->bw_changed (s->bw_changed_cls, address);
1329 * Add a new single address to a network
1331 * @param solver the solver Handle
1332 * @param address the address to add
1333 * @param network network type of this address
1336 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1339 struct GAS_PROPORTIONAL_Handle *s = solver;
1340 struct Network *net = NULL;
1341 struct AddressWrapper *aw = NULL;
1342 const struct ATS_Address *new_address;
1344 GNUNET_assert(NULL != s);
1347 net = get_network (s, network);
1354 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
1356 GNUNET_CONTAINER_DLL_insert(net->head, net->tail, aw);
1357 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1358 aw->addr->solver_information = net;
1360 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (s->requests, &address->peer.hashPubKey))
1362 if (NULL == get_active_address (s, (struct GNUNET_CONTAINER_MultiHashMap *) s->addresses, &address->peer))
1364 if (NULL != (new_address = GAS_proportional_get_preferred_address (s, &address->peer)))
1365 s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) address);
1368 LOG(GNUNET_ERROR_TYPE_DEBUG,
1369 "After adding address now total %u and active %u addresses in network `%s'\n",
1370 net->total_addresses, net->active_addresses, net->desc);
1374 * Init the proportional problem solver
1377 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1378 * out_quota[i] contains outbound quota for network type i
1379 * in_quota[i] contains inbound quota for network type i
1382 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1383 * network[2] == GNUNET_ATS_NET_LAN
1384 * out_quota[2] == 65353
1385 * in_quota[2] == 65353
1387 * @param cfg configuration handle
1388 * @param stats the GNUNET_STATISTICS handle
1389 * @param addresses hashmap containing all addresses
1390 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1391 * @param out_quota array of outbound quotas
1392 * @param in_quota array of outbound quota
1393 * @param dest_length array length for quota arrays
1394 * @param bw_changed_cb callback for changed bandwidth amounts
1395 * @param bw_changed_cb_cls cls for callback
1396 * @param get_preference callback to get relative preferences for a peer
1397 * @param get_preference_cls cls for callback to get relative preferences
1398 * @param get_properties for callback to get relative properties
1399 * @param get_properties_cls cls for callback to get relative properties
1400 * @return handle for the solver on success, NULL on fail
1403 GAS_proportional_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1404 const struct GNUNET_STATISTICS_Handle *stats,
1405 const struct GNUNET_CONTAINER_MultiHashMap *addresses, int *network,
1406 unsigned long long *out_quota, unsigned long long *in_quota,
1407 int dest_length, GAS_bandwidth_changed_cb bw_changed_cb,
1408 void *bw_changed_cb_cls, GAS_get_preferences get_preference,
1409 void *get_preference_cls, GAS_get_properties get_properties,
1410 void *get_properties_cls)
1413 struct GAS_PROPORTIONAL_Handle *s =
1414 GNUNET_malloc (sizeof (struct GAS_PROPORTIONAL_Handle));
1415 struct Network * cur;
1416 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1418 GNUNET_assert(NULL != cfg);
1419 GNUNET_assert(NULL != stats);
1420 GNUNET_assert(NULL != network);
1421 GNUNET_assert(NULL != bw_changed_cb);
1422 GNUNET_assert(NULL != get_preference);
1423 GNUNET_assert(NULL != get_properties);
1425 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1426 s->bw_changed = bw_changed_cb;
1427 s->bw_changed_cls = bw_changed_cb_cls;
1428 s->get_preferences = get_preference;
1429 s->get_preferences_cls = get_preference_cls;
1430 s->get_properties = get_properties;
1431 s->get_properties_cls = get_properties_cls;
1432 s->networks = dest_length;
1433 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
1434 s->active_addresses = 0;
1435 s->total_addresses = 0;
1436 s->bulk_lock = GNUNET_NO;
1437 s->addresses = addresses;
1439 s->requests = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1441 for (c = 0; c < dest_length; c++)
1443 cur = &s->network_entries[c];
1444 cur->total_addresses = 0;
1445 cur->active_addresses = 0;
1446 cur->type = network[c];
1447 cur->total_quota_in = in_quota[c];
1448 cur->total_quota_out = out_quota[c];
1449 cur->desc = net_str[c];
1450 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
1451 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total",
1458 * Shutdown the proportional problem solver
1460 * @param solver the respective handle to shutdown
1463 GAS_proportional_done (void *solver)
1465 struct GAS_PROPORTIONAL_Handle *s = solver;
1466 struct AddressWrapper *cur;
1467 struct AddressWrapper *next;
1469 GNUNET_assert(s != NULL);
1471 for (c = 0; c < s->networks; c++)
1473 if (s->network_entries[c].total_addresses > 0)
1475 LOG(GNUNET_ERROR_TYPE_ERROR,
1476 "Had %u addresses for network `%s' not deleted during shutdown\n",
1477 s->network_entries[c].total_addresses, s->network_entries[c].desc);
1481 if (s->network_entries[c].active_addresses > 0)
1483 LOG(GNUNET_ERROR_TYPE_ERROR,
1484 "Had %u active addresses for network `%s' not deleted during shutdown\n",
1485 s->network_entries[c].active_addresses, s->network_entries[c].desc);
1489 next = s->network_entries[c].head;
1490 while (NULL != (cur = next))
1493 GNUNET_CONTAINER_DLL_remove(s->network_entries[c].head,
1494 s->network_entries[c].tail, cur);
1497 GNUNET_free(s->network_entries[c].stat_total);
1498 GNUNET_free(s->network_entries[c].stat_active);
1500 if (s->total_addresses > 0)
1502 LOG(GNUNET_ERROR_TYPE_ERROR,
1503 "Had %u addresses not deleted during shutdown\n", s->total_addresses);
1506 if (s->active_addresses > 0)
1508 LOG(GNUNET_ERROR_TYPE_ERROR,
1509 "Had %u active addresses not deleted during shutdown\n",
1510 s->active_addresses);
1513 GNUNET_free(s->network_entries);
1514 GNUNET_CONTAINER_multihashmap_destroy (s->requests);
1518 /* end of gnunet-service-ats-solver_proportional.c */