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_MultiPeerMap *addresses;
231 * Pending address requests
233 struct GNUNET_CONTAINER_MultiPeerMap *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,
580 const struct GNUNET_PeerIdentity *key,
583 struct FindBestAddressCtx *fba_ctx = (struct FindBestAddressCtx *) cls;
584 struct ATS_Address *current = (struct ATS_Address *) value;
585 struct GNUNET_TIME_Absolute now;
586 struct Network *net = (struct Network *) current->solver_information;
587 const double *norm_prop_cur;
588 const double *norm_prop_prev;
591 now = GNUNET_TIME_absolute_get ();
593 if (current->blocked_until.abs_value_us
594 == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value_us)
596 /* This address is blocked for suggestion */
597 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address %p blocked for suggestion for %s \n",
599 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (now, current->blocked_until), GNUNET_YES));
602 if (GNUNET_NO == is_bandwidth_available_in_network (net))
603 return GNUNET_OK; /* There's no bandwidth available in this network */
604 if (NULL != fba_ctx->best)
606 GNUNET_assert(NULL != fba_ctx->best->plugin);
607 GNUNET_assert(NULL != current->plugin);
608 if (0 == strcmp (fba_ctx->best->plugin, current->plugin))
610 if ((0 != fba_ctx->best->addr_len) && (0 == current->addr_len))
612 /* saved address was an outbound address, but we have an inbound address */
613 fba_ctx->best = current;
616 if (0 == fba_ctx->best->addr_len)
618 /* saved address was an inbound address, so do not overwrite */
623 if (NULL == fba_ctx->best)
625 fba_ctx->best = current;
628 if ((ntohl (fba_ctx->best->assigned_bw_in.value__) == 0)
629 && (ntohl (current->assigned_bw_in.value__) > 0))
631 /* stick to existing connection */
632 fba_ctx->best = current;
636 norm_prop_cur = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
637 (const struct ATS_Address *) current);
638 norm_prop_prev = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
639 (const struct ATS_Address *) fba_ctx->best);
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s previous %.2f current %.2f\n",
642 "DISTANCE", norm_prop_cur[1], norm_prop_cur[1]);
644 index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
645 if (GNUNET_SYSERR == index)
650 if (norm_prop_cur[index] < norm_prop_prev[index])
652 /* user shorter distance */
653 fba_ctx->best = current;
657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s previous %.2f current %.2f\n",
658 "DELAY", norm_prop_cur[1], norm_prop_cur[1]);
660 index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
661 if (GNUNET_SYSERR == index)
666 if (norm_prop_cur[index] < norm_prop_prev[index])
668 /* user shorter delay */
669 fba_ctx->best = current;
679 * ---------------------------
683 * Update bandwidth assignment for all networks
685 * @param s the solver handle
688 distribute_bandwidth_in_all_networks (struct GAS_PROPORTIONAL_Handle *s)
691 for (i = 0; i < s->networks; i++)
692 distribute_bandwidth_in_network (s, &s->network_entries[i], NULL );
696 * Lookup network struct by type
698 * @param s the solver handle
699 * @param type the network type
700 * @return the network struct
702 static struct Network *
703 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
706 for (c = 0; c < s->networks; c++)
708 if (s->network_entries[c].type == type)
709 return &s->network_entries[c];
716 * Hashmap Iterator to find current active address for peer
718 * @param cls last active address
719 * @param key peer's key
720 * @param value address to check
721 * @return #GNUNET_NO on double active address else #GNUNET_YES;
724 get_active_address_it (void *cls,
725 const struct GNUNET_PeerIdentity *key,
728 struct ATS_Address **dest = cls;
729 struct ATS_Address *aa = (struct ATS_Address *) value;
731 if (GNUNET_YES == aa->active)
736 /* should never happen */
737 LOG(GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n",
738 GNUNET_i2s (&aa->peer));
748 * Find current active address for peer
750 * @param solver the solver handle
751 * @param addresses the address set
752 * @param peer the peer
753 * @return active address or NULL
755 static struct ATS_Address *
756 get_active_address (void *solver,
757 const struct GNUNET_CONTAINER_MultiPeerMap * addresses,
758 const struct GNUNET_PeerIdentity *peer)
760 struct ATS_Address * dest = NULL;
762 GNUNET_CONTAINER_multipeermap_get_multiple (addresses, peer,
763 &get_active_address_it, &dest);
769 addresse_increment (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
770 int total, int active)
772 if (GNUNET_YES == total)
774 s->total_addresses++;
775 net->total_addresses++;
776 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
777 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
779 if (GNUNET_YES == active)
781 net->active_addresses++;
782 s->active_addresses++;
783 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1,
785 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
792 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
793 int total, int active)
796 if (GNUNET_YES == total)
798 if (s->total_addresses < 1)
805 s->total_addresses--;
806 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
809 if (net->total_addresses < 1)
816 net->total_addresses--;
817 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
821 if (GNUNET_YES == active)
823 if (net->active_addresses < 1)
830 net->active_addresses--;
831 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
833 if (s->active_addresses < 1)
840 s->active_addresses--;
841 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
849 * Solver API functions
850 * ---------------------------
854 * Changes the preferences for a peer in the problem
856 * @param solver the solver handle
857 * @param peer the peer to change the preference for
858 * @param kind the kind to change the preference
859 * @param pref_rel the normalized preference value for this kind over all clients
862 GAS_proportional_address_change_preference (void *solver,
863 const struct GNUNET_PeerIdentity *peer,
864 enum GNUNET_ATS_PreferenceKind kind,
867 struct GAS_PROPORTIONAL_Handle *s = solver;
868 GNUNET_assert(NULL != solver);
869 GNUNET_assert(NULL != peer);
871 distribute_bandwidth_in_all_networks (s);
876 * Get application feedback for a peer
878 * @param solver the solver handle
879 * @param application the application
880 * @param peer the peer to change the preference for
881 * @param scope the time interval for this feedback: [now - scope .. now]
882 * @param kind the kind to change the preference
883 * @param score the score
886 GAS_proportional_address_preference_feedback (void *solver, void *application,
887 const struct GNUNET_PeerIdentity *peer,
888 const struct GNUNET_TIME_Relative scope,
889 enum GNUNET_ATS_PreferenceKind kind, double score)
891 struct GAS_PROPORTIONAL_Handle *s = solver;
892 GNUNET_assert(NULL != solver);
893 GNUNET_assert(NULL != peer);
895 GNUNET_assert(NULL != s);
900 * Get the preferred address for a specific peer
902 * @param solver the solver handle
903 * @param peer the identity of the peer
905 const struct ATS_Address *
906 GAS_proportional_get_preferred_address (void *solver,
907 const struct GNUNET_PeerIdentity *peer)
909 struct GAS_PROPORTIONAL_Handle *s = solver;
910 struct Network *net_prev;
911 struct Network *net_cur;
912 struct ATS_Address *prev;
913 struct FindBestAddressCtx fba_ctx;
915 GNUNET_assert(s != NULL);
916 GNUNET_assert(peer != NULL);
918 /* Add to list of pending requests */
919 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests,
922 GNUNET_assert (GNUNET_OK ==
923 GNUNET_CONTAINER_multipeermap_put (s->requests,
925 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
928 /* Get address with: stick to current address, lower distance, lower latency */
932 GNUNET_CONTAINER_multipeermap_get_multiple (s->addresses, peer,
933 &find_best_address_it, &fba_ctx);
934 if (NULL == fba_ctx.best)
936 LOG(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
941 LOG(GNUNET_ERROR_TYPE_INFO, "Suggesting %s address %p for peer `%s'\n",
942 (GNUNET_NO == fba_ctx.best->active) ? "inactive" : "active", fba_ctx.best,
944 net_cur = (struct Network *) fba_ctx.best->solver_information;
945 if (NULL == fba_ctx.best)
947 LOG(GNUNET_ERROR_TYPE_ERROR,
948 "Trying to suggesting unknown address peer `%s'\n", GNUNET_i2s (peer));
952 if (GNUNET_YES == fba_ctx.best->active)
954 /* This address was selected previously, so no need to update quotas */
958 /* This address was not active, so we have to:
960 * - mark previous active address as not active
961 * - update quota for previous address network
962 * - update quota for this address network
964 prev = get_active_address (s,
968 net_prev = (struct Network *) prev->solver_information;
969 prev->active = GNUNET_NO; /* No active any longer */
970 prev->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
971 prev->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
972 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
974 distribute_bandwidth_in_network (s, net_prev, NULL );
977 if (GNUNET_NO == (is_bandwidth_available_in_network (fba_ctx.best->solver_information)))
979 GNUNET_break(0); /* This should never happen*/
983 fba_ctx.best->active = GNUNET_YES;
984 addresse_increment (s, net_cur, GNUNET_NO, GNUNET_YES);
985 distribute_bandwidth_in_network (s, net_cur, fba_ctx.best );
990 * Stop notifying about address and bandwidth changes for this peer
992 * @param solver the solver handle
993 * @param peer the peer
996 GAS_proportional_stop_get_preferred_address (void *solver,
997 const struct GNUNET_PeerIdentity *peer)
999 struct GAS_PROPORTIONAL_Handle *s = solver;
1000 struct ATS_Address *cur;
1001 struct Network *cur_net;
1004 == GNUNET_CONTAINER_multipeermap_contains (s->requests,
1006 GNUNET_CONTAINER_multipeermap_remove (s->requests, peer,
1009 cur = get_active_address (s,
1010 s->addresses, peer);
1013 /* Disabling current address */
1014 cur_net = (struct Network *) cur->solver_information;
1015 cur->active = GNUNET_NO; /* No active any longer */
1016 cur->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1017 cur->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1018 if (GNUNET_SYSERR == addresse_decrement (s, cur_net, GNUNET_NO, GNUNET_YES))
1020 distribute_bandwidth_in_network (s, cur_net, NULL );
1026 * Remove an address from the solver
1028 * @param solver the solver handle
1029 * @param address the address to remove
1030 * @param session_only delete only session not whole address
1033 GAS_proportional_address_delete (void *solver, struct ATS_Address *address,
1036 struct GAS_PROPORTIONAL_Handle *s = solver;
1037 struct Network *net;
1038 struct AddressWrapper *aw;
1039 const struct ATS_Address *new_address;
1042 /* Remove an adress completely, we have to:
1043 * - Remove from specific network
1044 * - Decrease number of total addresses
1046 * - decrease number of active addreses
1050 net = (struct Network *) address->solver_information;
1052 if (GNUNET_NO == session_only)
1054 LOG(GNUNET_ERROR_TYPE_INFO,
1055 "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1056 (GNUNET_NO == address->active) ? "inactive" : "active", address,
1057 GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1058 net->active_addresses);
1060 /* Remove address */
1061 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1062 for (aw = net->head; NULL != aw; aw = aw->next)
1064 if (aw->addr == address)
1072 GNUNET_CONTAINER_DLL_remove(net->head, net->tail, aw);
1077 /* Remove session only: remove if active and update */
1078 LOG(GNUNET_ERROR_TYPE_INFO,
1079 "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1080 (GNUNET_NO == address->active) ? "inactive" : "active", address,
1081 GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1082 net->active_addresses);
1085 if (GNUNET_YES == address->active)
1087 /* Address was active, remove from network and update quotas*/
1088 address->active = GNUNET_NO;
1089 address->assigned_bw_in = BANDWIDTH_ZERO;
1090 address->assigned_bw_out = BANDWIDTH_ZERO;
1092 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1094 distribute_bandwidth_in_network (s, net, NULL );
1095 if (NULL == (new_address = GAS_proportional_get_preferred_address (s, &address->peer)))
1097 /* No alternative address found, disconnect peer */
1098 s->bw_changed (s->bw_changed_cls, address);
1102 s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) new_address);
1105 LOG(GNUNET_ERROR_TYPE_DEBUG,
1106 "After deleting address now total %u and active %u addresses in network `%s'\n",
1107 net->total_addresses, net->active_addresses, net->desc);
1112 * Start a bulk operation
1114 * @param solver the solver
1117 GAS_proportional_bulk_start (void *solver)
1119 LOG(GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1120 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1122 GNUNET_assert(NULL != solver);
1128 * Bulk operation done
1131 GAS_proportional_bulk_stop (void *solver)
1133 LOG(GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1135 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1136 GNUNET_assert(NULL != solver);
1138 if (s->bulk_lock < 1)
1144 if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1146 LOG(GNUNET_ERROR_TYPE_DEBUG, "No lock pending, recalculating\n");
1147 distribute_bandwidth_in_all_networks (s);
1148 s->bulk_requests = 0;
1154 * Add a new single address to a network
1156 * @param solver the solver Handle
1157 * @param address the address to add
1158 * @param network network type of this address
1161 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1165 * Transport properties for this address have changed
1167 * @param solver solver handle
1168 * @param address the address
1169 * @param type the ATSI type in HBO
1170 * @param abs_value the absolute value of the property
1171 * @param rel_value the normalized value
1174 GAS_proportional_address_property_changed (void *solver,
1175 struct ATS_Address *address, uint32_t type, uint32_t abs_value,
1178 struct GAS_PROPORTIONAL_Handle *s;
1181 GNUNET_assert(NULL != solver);
1182 GNUNET_assert(NULL != address);
1184 s = (struct GAS_PROPORTIONAL_Handle *) solver;
1185 n = (struct Network *) address->solver_information;
1193 LOG(GNUNET_ERROR_TYPE_DEBUG,
1194 "Property `%s' for peer `%s' address %p changed to %.2f \n",
1195 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1196 address, rel_value);
1199 case GNUNET_ATS_UTILIZATION_UP:
1200 case GNUNET_ATS_UTILIZATION_DOWN:
1201 case GNUNET_ATS_QUALITY_NET_DELAY:
1202 case GNUNET_ATS_QUALITY_NET_DISTANCE:
1203 case GNUNET_ATS_COST_WAN:
1204 case GNUNET_ATS_COST_LAN:
1205 case GNUNET_ATS_COST_WLAN:
1206 distribute_bandwidth_in_network (s, n, GNUNET_NO);
1212 * Transport session for this address has changed
1214 * NOTE: values in addresses are already updated
1216 * @param solver solver handle
1217 * @param address the address
1218 * @param cur_session the current session
1219 * @param new_session the new session
1222 GAS_proportional_address_session_changed (void *solver,
1223 struct ATS_Address *address, uint32_t cur_session, uint32_t new_session)
1225 if (cur_session != new_session)
1227 LOG(GNUNET_ERROR_TYPE_DEBUG, "Session changed from %u to %u\n", cur_session,
1233 * Usage for this address has changed
1235 * NOTE: values in addresses are already updated
1237 * @param solver solver handle
1238 * @param address the address
1239 * @param in_use usage state
1242 GAS_proportional_address_inuse_changed (void *solver,
1243 struct ATS_Address *address, int in_use)
1245 LOG(GNUNET_ERROR_TYPE_DEBUG, "Usage changed to %s\n",
1246 (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1250 * Network scope for this address has changed
1252 * NOTE: values in addresses are already updated
1254 * @param solver solver handle
1255 * @param address the address
1256 * @param current_network the current network
1257 * @param new_network the new network
1260 GAS_proportional_address_change_network (void *solver,
1261 struct ATS_Address *address, uint32_t current_network, uint32_t new_network)
1263 struct ATS_Address *new;
1264 struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1265 int save_active = GNUNET_NO;
1266 struct Network *new_net = NULL;
1268 if (current_network == new_network)
1274 /* Network changed */
1275 LOG(GNUNET_ERROR_TYPE_DEBUG,
1276 "Network type changed, moving %s address from `%s' to `%s'\n",
1277 (GNUNET_YES == address->active) ? "active" : "inactive",
1278 GNUNET_ATS_print_network_type (current_network),
1279 GNUNET_ATS_print_network_type (new_network));
1281 save_active = address->active;
1283 /* Disable and assign no bandwidth */
1284 address->active = GNUNET_NO;
1285 address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1286 address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1288 /* Remove from old network */
1289 GAS_proportional_address_delete (solver, address, GNUNET_NO);
1291 /* Set new network type */
1292 if (NULL == (new_net = get_network (solver, new_network)))
1294 /* Address changed to invalid network... */
1295 LOG(GNUNET_ERROR_TYPE_ERROR,
1296 _("Invalid network type `%u' `%s': Disconnect!\n"), new_network,
1297 GNUNET_ATS_print_network_type (new_network));
1299 /* Find new address to suggest since no bandwidth in network*/
1300 if (NULL == (new = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, &address->peer)))
1302 /* No alternative address found, disconnect peer */
1303 s->bw_changed (s->bw_changed_cls, address);
1308 /* Add to new network and update*/
1309 address->solver_information = new_net;
1310 GAS_proportional_address_add (solver, address, new_network);
1311 if (GNUNET_YES == save_active)
1313 /* check if bandwidth available in new network */
1314 if (GNUNET_YES == (is_bandwidth_available_in_network (new_net)))
1316 /* Assign bandwidth to updated address */
1317 address->active = GNUNET_YES;
1318 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
1319 distribute_bandwidth_in_network (solver, new_net, NULL );
1323 LOG(GNUNET_ERROR_TYPE_DEBUG,
1324 "Not enough bandwidth in new network, suggesting alternative address ..\n");
1325 /* Find new address to suggest since no bandwidth in network*/
1328 (struct ATS_Address *) GAS_proportional_get_preferred_address (s,
1331 /* No alternative address found, disconnect peer */
1332 s->bw_changed (s->bw_changed_cls, address);
1339 * Add a new single address to a network
1341 * @param solver the solver Handle
1342 * @param address the address to add
1343 * @param network network type of this address
1346 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1349 struct GAS_PROPORTIONAL_Handle *s = solver;
1350 struct Network *net = NULL;
1351 struct AddressWrapper *aw = NULL;
1352 const struct ATS_Address *new_address;
1354 GNUNET_assert(NULL != s);
1357 net = get_network (s, network);
1364 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
1366 GNUNET_CONTAINER_DLL_insert(net->head, net->tail, aw);
1367 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1368 aw->addr->solver_information = net;
1370 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1372 if (NULL == get_active_address (s, s->addresses, &address->peer))
1374 if (NULL != (new_address = GAS_proportional_get_preferred_address (s, &address->peer)))
1375 s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) address);
1378 LOG(GNUNET_ERROR_TYPE_DEBUG,
1379 "After adding address now total %u and active %u addresses in network `%s'\n",
1380 net->total_addresses, net->active_addresses, net->desc);
1384 * Init the proportional problem solver
1387 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1388 * out_quota[i] contains outbound quota for network type i
1389 * in_quota[i] contains inbound quota for network type i
1392 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1393 * network[2] == GNUNET_ATS_NET_LAN
1394 * out_quota[2] == 65353
1395 * in_quota[2] == 65353
1397 * @param cfg configuration handle
1398 * @param stats the GNUNET_STATISTICS handle
1399 * @param addresses hashmap containing all addresses
1400 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1401 * @param out_quota array of outbound quotas
1402 * @param in_quota array of outbound quota
1403 * @param dest_length array length for quota arrays
1404 * @param bw_changed_cb callback for changed bandwidth amounts
1405 * @param bw_changed_cb_cls cls for callback
1406 * @param get_preference callback to get relative preferences for a peer
1407 * @param get_preference_cls cls for callback to get relative preferences
1408 * @param get_properties for callback to get relative properties
1409 * @param get_properties_cls cls for callback to get relative properties
1410 * @return handle for the solver on success, NULL on fail
1413 GAS_proportional_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1414 const struct GNUNET_STATISTICS_Handle *stats,
1415 const struct GNUNET_CONTAINER_MultiPeerMap *addresses, int *network,
1416 unsigned long long *out_quota, unsigned long long *in_quota,
1417 int dest_length, GAS_bandwidth_changed_cb bw_changed_cb,
1418 void *bw_changed_cb_cls, GAS_get_preferences get_preference,
1419 void *get_preference_cls, GAS_get_properties get_properties,
1420 void *get_properties_cls)
1423 struct GAS_PROPORTIONAL_Handle *s =
1424 GNUNET_malloc (sizeof (struct GAS_PROPORTIONAL_Handle));
1425 struct Network * cur;
1426 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1428 GNUNET_assert(NULL != cfg);
1429 GNUNET_assert(NULL != stats);
1430 GNUNET_assert(NULL != network);
1431 GNUNET_assert(NULL != bw_changed_cb);
1432 GNUNET_assert(NULL != get_preference);
1433 GNUNET_assert(NULL != get_properties);
1435 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1436 s->bw_changed = bw_changed_cb;
1437 s->bw_changed_cls = bw_changed_cb_cls;
1438 s->get_preferences = get_preference;
1439 s->get_preferences_cls = get_preference_cls;
1440 s->get_properties = get_properties;
1441 s->get_properties_cls = get_properties_cls;
1442 s->networks = dest_length;
1443 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
1444 s->active_addresses = 0;
1445 s->total_addresses = 0;
1446 s->bulk_lock = GNUNET_NO;
1447 s->addresses = addresses;
1449 s->requests = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1451 for (c = 0; c < dest_length; c++)
1453 cur = &s->network_entries[c];
1454 cur->total_addresses = 0;
1455 cur->active_addresses = 0;
1456 cur->type = network[c];
1457 cur->total_quota_in = in_quota[c];
1458 cur->total_quota_out = out_quota[c];
1459 cur->desc = net_str[c];
1460 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
1461 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total",
1468 * Shutdown the proportional problem solver
1470 * @param solver the respective handle to shutdown
1473 GAS_proportional_done (void *solver)
1475 struct GAS_PROPORTIONAL_Handle *s = solver;
1476 struct AddressWrapper *cur;
1477 struct AddressWrapper *next;
1479 GNUNET_assert(s != NULL);
1481 for (c = 0; c < s->networks; c++)
1483 if (s->network_entries[c].total_addresses > 0)
1485 LOG(GNUNET_ERROR_TYPE_ERROR,
1486 "Had %u addresses for network `%s' not deleted during shutdown\n",
1487 s->network_entries[c].total_addresses, s->network_entries[c].desc);
1491 if (s->network_entries[c].active_addresses > 0)
1493 LOG(GNUNET_ERROR_TYPE_ERROR,
1494 "Had %u active addresses for network `%s' not deleted during shutdown\n",
1495 s->network_entries[c].active_addresses, s->network_entries[c].desc);
1499 next = s->network_entries[c].head;
1500 while (NULL != (cur = next))
1503 GNUNET_CONTAINER_DLL_remove(s->network_entries[c].head,
1504 s->network_entries[c].tail, cur);
1507 GNUNET_free(s->network_entries[c].stat_total);
1508 GNUNET_free(s->network_entries[c].stat_active);
1510 if (s->total_addresses > 0)
1512 LOG(GNUNET_ERROR_TYPE_ERROR,
1513 "Had %u addresses not deleted during shutdown\n", s->total_addresses);
1516 if (s->active_addresses > 0)
1518 LOG(GNUNET_ERROR_TYPE_ERROR,
1519 "Had %u active addresses not deleted during shutdown\n",
1520 s->active_addresses);
1523 GNUNET_free (s->network_entries);
1524 GNUNET_CONTAINER_multipeermap_destroy (s->requests);
1528 /* end of gnunet-service-ats-solver_proportional.c */