2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 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/plugin_ats_proportional.c
23 * @brief ATS proportional solver
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_ats_plugin.h"
30 #include "gnunet_ats_service.h"
31 #include "gnunet-service-ats_addresses.h"
33 #define PROP_STABILITY_FACTOR 1.25
36 #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
41 * NOTE: Do not change this documentation. This documentation is based
42 * on gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
43 * use build_txt.sh to generate plaintext output
45 * ATS addresses : proportional solver
47 * The proportional solver ("proportional") distributes the available
48 * bandwidth fair over all the addresses influenced by the
49 * preference values. For each available network type an in- and
50 * outbound quota is configured and the bandwidth available in
51 * these networks is distributed over the addresses. The solver
52 * first assigns every addresses the minimum amount of bandwidth
53 * #GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
54 * remaining bandwidth available according to the preference
55 * values. For each peer only a single address gets bandwidth
56 * assigned and only one address marked as active. The most
57 * important functionality for the solver is implemented in: *
58 * find_address_it is an hashmap iterator returning the prefered
59 * address for an peer * update_quota_per_network distributes
60 * available bandwidth for a network over active addresses
62 * Changes to addresses automatically have an impact on the the
63 * bandwidth assigned to other addresses in the same network since
64 * the solver distributes the remaining bandwidth over the
65 * addresses in the network. When changes to the addresses occur,
66 * the solver first performs the changes, like adding or deleting
67 * addresses, and then updates bandwidth assignment for the
68 * affected network. Bandwidth assignment is only recalculated on
69 * demand when an address is requested by a client for a peer or
70 * when the addresses available have changed or an address changed
71 * the network it is located in. When the bandwidth assignment has
72 * changed the callback is called with the new bandwidth
73 * assignments. The bandwidth distribution for a network is
74 * recalculated due to: * address suggestion requests * address
75 * deletions * address switching networks during address update *
78 * 3.1 Data structures used
80 * For each ATS network (e.g. WAN, LAN, loopback) a struct Network
81 * is used to specify network related information as total adresses
82 * and active addresses in this network and the configured in- and
83 * outbound quota. Each network also contains a list of addresses
84 * added to the solver located in this network. The proportional
85 * solver uses the addresses' solver_information field to store the
86 * proportional network it belongs to for each address.
90 * When the proportional solver is initialized the solver creates a
91 * new solver handle and initializes the network structures with
92 * the quotas passed from addresses and returns the handle solver.
94 * 3.3 Adding an address
96 * When a new address is added to the solver using s_add, a lookup
97 * for the network for this address is done and the address is
98 * enqueued in in the linked list of the network.
100 * 3.4 Updating an address
102 * The main purpose of address updates is to update the ATS
103 * information for addresse selection. Important for the proportional
104 * solver is when an address switches network it is located
105 * in. This is common because addresses added by transport's
106 * validation mechanism are commonly located in
107 * #GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
108 * in this network type and only if a connection is successful on
109 * return of payload data transport switches to the real network
110 * the address is located in. When an address changes networks it
111 * is first of all removed from the old network using the solver
112 * API function #GAS_proportional_address_delete() and the network in
113 * the address struct is updated. A lookup for the respective new
114 * proportional network is done and stored in the addresse's
115 * solver_information field. Next the address is re-added to the
116 * solver using the solver API function
117 * #GAS_proportional_address_add(). If the address was marked as in
118 * active, the solver checks if bandwidth is available in the
119 * network and if yes sets the address to active and updates the
120 * bandwidth distribution in this network. If no bandwidth is
121 * available it sets the bandwidth for this address to 0 and tries
122 * to suggest an alternative address. If an alternative address was
123 * found, addresses' callback is called for this address.
125 * 3.5 Deleting an address
127 * When an address is removed from the solver, it removes the
128 * respective address from the network and if the address was
129 * marked as active, it updates the bandwidth distribution for this
132 * 3.6 Requesting addresses
134 * When an address is requested for a peer the solver performs a
135 * lookup for the peer entry in addresses address hashmap and
136 * selects the best address. The selection of the most suitable
137 * address is done in the find_address_it hashmap iterator
138 * described in detail in section 3.7. If no address is returned,
139 * no address can be suggested at the moment. If the address
140 * returned is marked as active, the solver can return this
141 * address. If the address is not marked as active, the solver
142 * checks if another address belongign to this peer is marked as
143 * active and marks the address as inactive, updates the bandwidth
144 * for this address to 0, call the bandwidth changed callback for
145 * this address due to the change and updates quota assignment for
146 * the addresse's network. the now in-active address is belonging
147 * to. The solver marks the new address as active and updates the
148 * bandwidth assignment for this network.
150 * 3.7 Choosing addresses
152 * Choosing the best possible address for suggestion is done by
153 * iterating over all addresses of a peer stored in addresses'
154 * hashmap and using the hashmap iterator find_address_it to select
155 * the best available address. Several checks are done when an
156 * address is selected. First if this address is currently blocked
157 * by addresses from being suggested. An address is blocked for the
158 * duration of #ATS_BLOCKING_DELTA when it is suggested to
159 * transport. Next it is checked if at least
160 * #GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
161 * in the addresse's network, because suggesting an address without
162 * bandwidth does not make sense. This also ensures that all active
163 * addresses in this network get at least the minimum amount of
164 * bandwidth assigned. In the next step the solver ensures that for
165 * tcp connections inbound connections are prefered over outbound
166 * connections. In the next stet the solver ensures that
167 * connections are prefered in the following order: * connections
168 * are already established and have bandwidth assigned *
169 * connections with a shorter distance * connectes have a shorter
172 * 3.8 Changing preferences
176 * During shutdown all network entries and aging processes are
177 * destroyed and freed.
182 * This solver assigns in and outbound bandwidth equally for all
183 * addresses in specific network type (WAN, LAN) based on configured
184 * in and outbound quota for this network.
186 * The solver is notified by addresses about changes to the addresses
187 * and recalculates the bandwith assigned if required. The solver
188 * notifies addresses by calling the GAS_bandwidth_changed_cb
196 * For each peer only a single is selected and marked as "active" in the address
201 * You have the networks WAN and LAN and quotas
202 * WAN_TOTAL_IN, WAN_TOTAL_OUT
203 * LAN_TOTAL_IN, LAN_TOTAL_OUT
205 * If you have x addresses in the network segment LAN, the quotas are
206 * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
208 * Quotas are automatically recalculated and reported back when addresses are
214 * Default value to assume for the proportionality factor,
215 * if none is given in the configuration.
217 #define PROPORTIONALITY_FACTOR 2.0
221 * Address information stored for the proportional solver in the
222 * `solver_information` member of `struct GNUNET_ATS_Address`.
224 * They are also stored in the respective `struct Network`'s linked
227 struct AddressWrapper
232 struct AddressWrapper *next;
237 struct AddressWrapper *prev;
242 struct ATS_Address *addr;
245 * Network scope this address is in
247 struct Network *network;
252 uint32_t calculated_quota_in;
257 uint32_t calculated_quota_out;
260 * When was this address activated
262 struct GNUNET_TIME_Absolute activated;
268 * Representation of a network
273 * Network description
278 * String for statistics total addresses
283 * String for statistics active addresses
288 * Linked list of addresses in this network: head
290 struct AddressWrapper *head;
293 * Linked list of addresses in this network: tail
295 struct AddressWrapper *tail;
298 * Total inbound quota
300 unsigned long long total_quota_in;
303 * Total outbound quota
305 unsigned long long total_quota_out;
310 enum GNUNET_ATS_Network_Type type;
313 * Number of active addresses for this network
315 unsigned int active_addresses;
318 * Number of total addresses for this network
320 unsigned int total_addresses;
326 * A handle for the proportional solver
328 struct GAS_PROPORTIONAL_Handle
332 * Our execution environment.
334 struct GNUNET_ATS_PluginEnvironment *env;
339 struct Network *network_entries;
342 * Proportionality factor
349 double stability_factor;
352 * Bulk lock counter. If zero, we are not locked.
354 unsigned int bulk_lock;
357 * Number of changes made while solver was locked. We really only
358 * use 0/non-zero to check on unlock if we have to run the update.
360 unsigned int bulk_requests;
363 * Number of active addresses for solver
365 unsigned int active_addresses;
368 * Number of networks in @a network_entries
370 unsigned int network_count;
375 * Function used to unload the plugin.
377 * @param cls return value from #libgnunet_plugin_ats_proportional_init()
380 libgnunet_plugin_ats_proportional_done (void *cls)
382 struct GNUNET_ATS_SolverFunctions *sf = cls;
383 struct GAS_PROPORTIONAL_Handle *s = sf->cls;
384 struct AddressWrapper *cur;
385 struct AddressWrapper *next;
388 for (c = 0; c < s->network_count; c++)
390 if (s->network_entries[c].total_addresses > 0)
392 LOG (GNUNET_ERROR_TYPE_DEBUG,
393 "Had %u addresses for network `%s' not deleted during shutdown\n",
394 s->network_entries[c].total_addresses,
395 s->network_entries[c].desc);
399 if (s->network_entries[c].active_addresses > 0)
401 LOG (GNUNET_ERROR_TYPE_DEBUG,
402 "Had %u active addresses for network `%s' not deleted during shutdown\n",
403 s->network_entries[c].active_addresses,
404 s->network_entries[c].desc);
408 next = s->network_entries[c].head;
409 while (NULL != (cur = next))
412 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
413 s->network_entries[c].tail,
415 GNUNET_free_non_null (cur->addr->solver_information);
418 GNUNET_free (s->network_entries[c].stat_total);
419 GNUNET_free (s->network_entries[c].stat_active);
421 if (s->active_addresses > 0)
423 LOG (GNUNET_ERROR_TYPE_DEBUG,
424 "Had %u active addresses not deleted during shutdown\n",
425 s->active_addresses);
428 GNUNET_free (s->network_entries);
435 * Test if bandwidth is available in this network to add an additional address
437 * @param net the network type to update
438 * @return #GNUNET_YES or #GNUNET_NO
441 is_bandwidth_available_in_network (struct Network *net)
443 unsigned int na = net->active_addresses + 1;
444 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
446 if (((net->total_quota_in / na) > min_bw)
447 && ((net->total_quota_out / na) > min_bw))
449 LOG (GNUNET_ERROR_TYPE_DEBUG,
450 "Enough bandwidth available for %u active addresses in network `%s'\n",
456 LOG (GNUNET_ERROR_TYPE_DEBUG,
457 "Not enough bandwidth available for %u active addresses in network `%s'\n",
465 * Update bandwidth assigned to peers in this network
467 * @param s the solver handle
468 * @param net the network type to update
472 distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
475 struct AddressWrapper *aw;
476 unsigned long long remaining_quota_in = 0;
477 unsigned long long quota_out_used = 0;
478 unsigned long long remaining_quota_out = 0;
479 unsigned long long quota_in_used = 0;
481 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
482 double relative_peer_prefence;
483 double sum_relative_peer_prefences; /* Important: has to be double not float due to precision */
484 double cur_pref; /* Important: has to be double not float due to precision */
487 const double *peer_relative_prefs = NULL; /* Important: has to be double not float due to precision */
489 uint32_t assigned_quota_in = 0;
490 uint32_t assigned_quota_out = 0;
493 LOG (GNUNET_ERROR_TYPE_INFO,
494 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
496 net->active_addresses,
498 net->total_quota_in);
500 if (net->active_addresses == 0)
502 return; /* no addresses to update */
506 * Assign every peer in network minimum Bandwidth
507 * Distribute bandwidth left according to preference
510 if ((net->active_addresses * min_bw) > net->total_quota_in)
515 if ((net->active_addresses * min_bw) > net->total_quota_out)
521 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
522 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
523 LOG(GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
524 remaining_quota_in, remaining_quota_out);
525 sum_relative_peer_prefences = 0.0;
527 /* Calculate sum of relative preference for active addresses in this network */
529 for (aw = net->head; NULL != aw; aw = aw->next)
531 if (GNUNET_YES != aw->addr->active)
534 peer_relative_prefs = s->env->get_preferences (s->env->cls,
536 relative_peer_prefence = 0.0;
537 relative_peer_prefence += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
538 sum_relative_peer_prefences += relative_peer_prefence;
542 if (count_addresses != net->active_addresses)
545 LOG (GNUNET_ERROR_TYPE_WARNING,
546 "%s: Counted %u active addresses, but network says to have %u active addresses\n",
547 net->desc, count_addresses, net->active_addresses);
548 for (aw = net->head; NULL != aw; aw = aw->next)
550 if (GNUNET_YES != aw->addr->active)
553 LOG (GNUNET_ERROR_TYPE_WARNING,
554 "Active: `%s' `%s' length %u\n",
555 GNUNET_i2s (&aw->addr->peer),
561 LOG (GNUNET_ERROR_TYPE_INFO,
562 "Total relative preference %.3f for %u addresses in network %s\n",
563 sum_relative_peer_prefences, net->active_addresses, net->desc);
565 for (aw = net->head; NULL != aw; aw = aw->next)
567 if (GNUNET_YES == aw->addr->active)
569 peer_relative_prefs = s->env->get_preferences (s->env->cls,
572 cur_pref = peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
573 total_weight = net->active_addresses +
574 s->prop_factor * sum_relative_peer_prefences;
575 peer_weight = (1.0 + (s->prop_factor * cur_pref));
577 assigned_quota_in = min_bw
578 + ((peer_weight / total_weight) * remaining_quota_in);
579 assigned_quota_out = min_bw
580 + ((peer_weight / total_weight) * remaining_quota_out);
582 LOG (GNUNET_ERROR_TYPE_INFO,
583 "New quota for peer `%s' with weight (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
584 GNUNET_i2s (&aw->addr->peer), peer_weight, total_weight,
585 assigned_quota_in, assigned_quota_out);
589 assigned_quota_in = 0;
590 assigned_quota_out = 0;
593 quota_in_used += assigned_quota_in;
594 quota_out_used += assigned_quota_out;
595 /* Prevent overflow due to rounding errors */
596 if (assigned_quota_in > UINT32_MAX)
597 assigned_quota_in = UINT32_MAX;
598 if (assigned_quota_out > UINT32_MAX)
599 assigned_quota_out = UINT32_MAX;
601 /* Compare to current bandwidth assigned */
602 aw->calculated_quota_in = assigned_quota_in;
603 aw->calculated_quota_out = assigned_quota_out;
605 LOG(GNUNET_ERROR_TYPE_DEBUG,
606 "Total bandwidth assigned is (in/out): %llu /%llu\n", quota_in_used,
608 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
610 LOG(GNUNET_ERROR_TYPE_ERROR,
611 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
612 net->active_addresses, quota_out_used, net->total_quota_out);
614 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
616 LOG(GNUNET_ERROR_TYPE_ERROR,
617 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
618 net->active_addresses, quota_in_used, net->total_quota_in);
624 * Context for finding the best address* Linked list of addresses in this network: head
626 struct FindBestAddressCtx
631 struct GAS_PROPORTIONAL_Handle *s;
634 * The currently best address
636 struct ATS_Address *best;
641 * Find index of a ATS property type in the quality properties array.
643 * @param type ATS property type
644 * @return index in the quality array, #GNUNET_SYSERR if the type
645 * was not a quality property
648 find_quality_property_index (enum GNUNET_ATS_Property type)
650 enum GNUNET_ATS_Property existing_types[] = GNUNET_ATS_QualityProperties;
653 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
654 if (existing_types[c] == type)
657 return GNUNET_SYSERR;
662 * Find a "good" address to use for a peer by iterating over the addresses for this peer.
663 * If we already have an existing address, we stick to it.
664 * Otherwise, we pick by lowest distance and then by lowest latency.
666 * @param cls the `struct FindBestAddressCtx *' where we store the result
668 * @param value another `struct ATS_Address*` to consider using
669 * @return #GNUNET_OK (continue to iterate)
672 find_best_address_it (void *cls,
673 const struct GNUNET_PeerIdentity *key,
676 struct FindBestAddressCtx *ctx = cls;
677 struct ATS_Address *current = value;
678 struct ATS_Address *current_best = current;
679 struct AddressWrapper *asi;
680 struct GNUNET_TIME_Relative active_time;
681 struct GNUNET_TIME_Relative min_active_time;
683 double best_distance;
689 asi = current->solver_information;
696 if (GNUNET_NO == is_bandwidth_available_in_network (asi->network))
698 return GNUNET_OK; /* There's no bandwidth available in this network */
701 if (NULL != ctx->best)
703 /* Compare current addresses with denominated 'best' address */
704 current_best = ctx->best;
708 /* We do not have a 'best' address so take this address */
709 LOG (GNUNET_ERROR_TYPE_DEBUG,
710 "Setting initial address %p\n",
712 current_best = current;
716 if (GNUNET_YES == current->active)
718 GNUNET_assert (asi->activated.abs_value_us != GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us);
719 active_time = GNUNET_TIME_absolute_get_duration (asi->activated);
720 min_active_time.rel_value_us = ((double) GNUNET_TIME_UNIT_SECONDS.rel_value_us) *
721 ctx->s->stability_factor;
722 if (active_time.rel_value_us <= min_active_time.rel_value_us)
724 /* Keep active address for stability reasons */
730 /* Now compare ATS information */
731 index = find_quality_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
732 cur_distance = current->atsin[index].norm;
733 best_distance = ctx->best->atsin[index].norm;
734 index = find_quality_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
735 cur_delay = current->atsin[index].norm;
736 best_delay = ctx->best->atsin[index].norm;
738 /* user shorter distance */
739 if (cur_distance < best_distance)
741 if (GNUNET_NO == ctx->best->active)
743 current_best = current; /* Use current */
745 else if ((best_distance / cur_distance) > ctx->s->stability_factor)
747 /* Best and active address performs worse */
748 current_best = current;
753 /* Use current best */
754 current_best = ctx->best;
757 /* User connection with less delay */
758 if (cur_delay < best_delay)
761 if (GNUNET_NO == ctx->best->active)
763 current_best = current; /* Use current */
765 else if ((best_delay / cur_delay) > ctx->s->stability_factor)
767 /* Best and active address performs worse */
768 current_best = current;
773 /* Use current best */
774 current_best = ctx->best;
778 ctx->best = current_best;
784 * Find the currently best address for a peer from the set of addresses available
785 * or return NULL of no address is available
787 * @param s the proportional handle
788 * @param addresses the address hashmap
789 * @param id the peer id
790 * @return the address or NULL
793 get_best_address (struct GAS_PROPORTIONAL_Handle *s,
794 struct GNUNET_CONTAINER_MultiPeerMap *addresses,
795 const struct GNUNET_PeerIdentity *id)
797 struct FindBestAddressCtx fba_ctx;
801 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
803 &find_best_address_it,
810 * Address map iterator to find current active address for peer.
811 * Asserts that only one address is active per peer.
813 * @param cls last active address
814 * @param key peer's key
815 * @param value address to check
816 * @return #GNUNET_NO on double active address else #GNUNET_YES;
819 get_active_address_it (void *cls,
820 const struct GNUNET_PeerIdentity *key,
823 struct ATS_Address **dest = cls;
824 struct ATS_Address *aa = value;
826 if (GNUNET_YES != aa->active)
828 GNUNET_assert (NULL == (*dest));
835 * Find current active address for peer
837 * @param s the solver handle
838 * @param peer the peer
839 * @return active address or NULL
841 static struct ATS_Address *
842 get_active_address (struct GAS_PROPORTIONAL_Handle *s,
843 const struct GNUNET_PeerIdentity *peer)
845 struct ATS_Address *dest;
848 GNUNET_CONTAINER_multipeermap_get_multiple (s->env->addresses,
850 &get_active_address_it,
857 * Lookup network struct by type
859 * @param s the solver handle
860 * @param type the network type
861 * @return the network struct
863 static struct Network *
864 get_network (struct GAS_PROPORTIONAL_Handle *s,
865 enum GNUNET_ATS_Network_Type type)
867 if (type >= s->env->network_count)
872 return &s->network_entries[type];
877 * Decrease address count in network
879 * @param s the solver handle
880 * @param net the network type
883 address_decrement_active (struct GAS_PROPORTIONAL_Handle *s,
886 if (net->active_addresses < 1)
892 net->active_addresses--;
893 GNUNET_STATISTICS_update (s->env->stats,
894 net->stat_active, -1, GNUNET_NO);
896 if (s->active_addresses < 1)
902 s->active_addresses--;
903 GNUNET_STATISTICS_update (s->env->stats,
904 "# ATS addresses total",
916 * @return GNUNET_YES if equal, GNUNET_NO else
919 address_eq (struct ATS_Address *a, struct ATS_Address *b)
921 GNUNET_assert (NULL != a);
922 GNUNET_assert (NULL != b);
923 if (0 != strcmp(a->plugin, b->plugin))
925 if (a->addr_len != b->addr_len)
927 if (0 != memcmp (a->addr, b->addr, b->addr_len))
929 if (a->session_id != b->session_id)
936 * Notify bandwidth changes to addresses
938 * @param s solver handle
939 * @param net the network to propagate changes in
942 propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
945 struct AddressWrapper *cur;
947 for (cur = net->head; NULL != cur; cur = cur->next)
949 if ( (cur->addr->assigned_bw_in != cur->calculated_quota_in) ||
950 (cur->addr->assigned_bw_out != cur->calculated_quota_out) )
952 cur->addr->assigned_bw_in = cur->calculated_quota_in;
953 cur->addr->assigned_bw_out = cur->calculated_quota_out;
955 /* Reset for next iteration */
956 cur->calculated_quota_in = 0;
957 cur->calculated_quota_out = 0;
958 LOG (GNUNET_ERROR_TYPE_DEBUG,
959 "Bandwidth for %s address %p for peer `%s' changed to %u/%u\n",
960 (GNUNET_NO == cur->addr->active) ? "inactive" : "active",
962 GNUNET_i2s (&cur->addr->peer),
963 cur->addr->assigned_bw_in,
964 cur->addr->assigned_bw_out);
966 /* Notify on change */
967 if ((GNUNET_YES == cur->addr->active))
969 s->env->bandwidth_changed_cb (s->env->cls,
978 * Distribibute bandwidth
980 * @param s the solver handle
981 * @param n the network, can be NULL for all network
984 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
987 if (GNUNET_YES == s->bulk_lock)
995 LOG (GNUNET_ERROR_TYPE_INFO,
996 "Redistributing bandwidth in network %s with %u active and %u total addresses\n",
997 GNUNET_ATS_print_network_type(n->type),
1001 s->env->info_cb (s->env->cls,
1004 GAS_INFO_PROP_SINGLE);
1007 distribute_bandwidth(s, n);
1009 s->env->info_cb (s->env->cls,
1012 GAS_INFO_PROP_SINGLE);
1013 s->env->info_cb (s->env->cls,
1014 GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
1016 GAS_INFO_PROP_SINGLE);
1018 /* Do propagation */
1019 propagate_bandwidth (s, n);
1021 s->env->info_cb (s->env->cls,
1022 GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
1024 GAS_INFO_PROP_SINGLE);
1029 s->env->info_cb (s->env->cls,
1033 for (i = 0; i < s->network_count; i++)
1036 distribute_bandwidth(s, &s->network_entries[i]);
1039 s->env->info_cb (s->env->cls,
1043 s->env->info_cb (s->env->cls,
1044 GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
1047 for (i = 0; i < s->network_count; i++)
1049 /* Do propagation */
1050 propagate_bandwidth(s, &s->network_entries[i]);
1052 s->env->info_cb (s->env->cls,
1053 GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
1061 * Update active address for a peer:
1062 * Check if active address exists and what the best address is, if addresses
1063 * are different switch
1065 * @param s solver handle
1066 * @param peer the peer to check
1067 * return the new address or NULL if no update was performed
1069 static struct ATS_Address *
1070 update_active_address (struct GAS_PROPORTIONAL_Handle *s,
1071 const struct GNUNET_PeerIdentity *peer)
1073 struct ATS_Address *best_address;
1074 struct ATS_Address *current_address;
1075 struct AddressWrapper *asi;
1076 struct Network *net;
1078 LOG (GNUNET_ERROR_TYPE_INFO,
1079 "Updating active address for peer `%s'\n",
1082 /* Find active address */
1083 current_address = get_active_address (s,
1086 LOG (GNUNET_ERROR_TYPE_INFO,
1087 "Peer `%s' has active address %p\n",
1091 /* Find best address */
1092 best_address = get_best_address (s,
1095 LOG (GNUNET_ERROR_TYPE_INFO,
1096 "Peer `%s' has best address %p\n",
1100 if (NULL != current_address)
1102 GNUNET_assert (GNUNET_YES == current_address->active);
1103 if ( (NULL == best_address) ||
1104 ( (NULL != best_address) &&
1105 (GNUNET_NO == address_eq (current_address,
1108 /* We switch to a new address (or to none),
1109 mark old address as inactive */
1110 LOG (GNUNET_ERROR_TYPE_INFO,
1111 "Disabling previous %s address %p for peer `%s'\n",
1112 (GNUNET_NO == current_address->active) ? "inactive" : "active",
1116 asi = current_address->solver_information;
1118 asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
1119 current_address->active = GNUNET_NO; /* No active any longer */
1120 current_address->assigned_bw_in = 0; /* no bandwidth assigned */
1121 current_address->assigned_bw_out = 0; /* no bandwidth assigned */
1122 address_decrement_active (s, net);
1123 distribute_bandwidth_in_network (s, net);
1125 if (NULL == best_address)
1127 /* We previously had an active address, but now we cannot suggest one
1128 * Therefore we have to disconnect the peer */
1129 LOG (GNUNET_ERROR_TYPE_INFO,
1130 "Disconnecting peer `%s' with previous address %p\n",
1133 s->env->bandwidth_changed_cb (s->env->cls,
1137 if (NULL == best_address)
1139 LOG (GNUNET_ERROR_TYPE_INFO,
1140 "Cannot suggest address for peer `%s'\n",
1145 LOG (GNUNET_ERROR_TYPE_INFO,
1146 "Suggesting new address %p for peer `%s'\n",
1150 if ( (NULL != current_address) &&
1151 (GNUNET_YES == address_eq (best_address, current_address)) )
1153 GNUNET_break (GNUNET_NO != current_address->active);
1154 return best_address; /* Same same */
1157 asi = best_address->solver_information;
1158 GNUNET_assert (NULL != asi);
1161 /* Mark address as active */
1162 asi->activated = GNUNET_TIME_absolute_get ();
1163 best_address->active = GNUNET_YES;
1165 net->active_addresses++;
1166 s->active_addresses++;
1167 GNUNET_STATISTICS_update (s->env->stats,
1168 "# ATS active addresses total",
1171 GNUNET_STATISTICS_update (s->env->stats,
1175 LOG (GNUNET_ERROR_TYPE_INFO,
1176 "Address %p for peer `%s' is now active\n",
1179 /* Distribute bandwidth */
1180 distribute_bandwidth_in_network (s, net);
1181 return best_address;
1186 * Changes the preferences for a peer in the problem
1188 * @param solver the solver handle
1189 * @param peer the peer to change the preference for
1190 * @param kind the kind to change the preference
1191 * @param pref_rel the normalized preference value for this kind over all clients
1194 GAS_proportional_address_change_preference (void *solver,
1195 const struct GNUNET_PeerIdentity *peer,
1196 enum GNUNET_ATS_PreferenceKind kind,
1199 struct GAS_PROPORTIONAL_Handle *s = solver;
1200 struct ATS_Address *best_address;
1201 struct ATS_Address *active_address;
1202 struct AddressWrapper *asi;
1205 s->env->get_connectivity (s->env->cls,
1207 return; /* Peer is not requested */
1209 /* This peer is requested, find best address */
1210 active_address = get_active_address (s,
1212 best_address = update_active_address (s, peer);
1214 if ((NULL != best_address) && ((NULL != active_address) &&
1215 (GNUNET_YES == address_eq (active_address, best_address))))
1217 asi = best_address->solver_information;
1218 GNUNET_assert (NULL != asi);
1220 /* We sticked to the same address, therefore redistribute */
1221 distribute_bandwidth_in_network (s, asi->network);
1227 * Get application feedback for a peer
1229 * @param solver the solver handle
1230 * @param application the application
1231 * @param peer the peer to change the preference for
1232 * @param scope the time interval for this feedback: [now - scope .. now]
1233 * @param kind the kind to change the preference
1234 * @param score the score
1237 GAS_proportional_address_preference_feedback (void *solver,
1238 struct GNUNET_SERVER_Client *application,
1239 const struct GNUNET_PeerIdentity *peer,
1240 const struct GNUNET_TIME_Relative scope,
1241 enum GNUNET_ATS_PreferenceKind kind,
1244 struct GAS_PROPORTIONAL_Handle *s = solver;
1246 GNUNET_assert(NULL != peer);
1247 GNUNET_assert(NULL != s);
1252 * Get the preferred address for a specific peer
1254 * @param solver the solver handle
1255 * @param peer the identity of the peer
1256 * @return best address
1258 static const struct ATS_Address *
1259 GAS_proportional_get_preferred_address (void *solver,
1260 const struct GNUNET_PeerIdentity *peer)
1262 struct GAS_PROPORTIONAL_Handle *s = solver;
1263 const struct ATS_Address *best_address;
1265 best_address = update_active_address (s, peer);
1266 if (s->bulk_lock > 0)
1268 return best_address;
1273 * Stop notifying about address and bandwidth changes for this peer
1275 * @param solver the solver handle
1276 * @param peer the peer
1279 GAS_proportional_stop_get_preferred_address (void *solver,
1280 const struct GNUNET_PeerIdentity *peer)
1282 struct GAS_PROPORTIONAL_Handle *s = solver;
1283 struct ATS_Address *cur;
1284 struct AddressWrapper *asi;
1285 struct Network *cur_net;
1287 cur = get_active_address (s,
1291 LOG (GNUNET_ERROR_TYPE_INFO,
1292 "Disabling %s address %p for peer `%s'\n",
1293 (GNUNET_NO == cur->active) ? "inactive" : "active",
1295 GNUNET_i2s (&cur->peer));
1297 /* Disabling current address */
1298 asi = cur->solver_information;
1299 cur_net = asi->network;
1300 asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
1301 cur->active = GNUNET_NO; /* No active any longer */
1302 cur->assigned_bw_in = 0; /* no bandwidth assigned */
1303 cur->assigned_bw_out = 0; /* no bandwidth assigned */
1305 address_decrement_active (s, cur_net);
1307 distribute_bandwidth_in_network (s, cur_net);
1313 * Start a bulk operation
1315 * @param solver the solver
1318 GAS_proportional_bulk_start (void *solver)
1320 struct GAS_PROPORTIONAL_Handle *s = solver;
1322 LOG (GNUNET_ERROR_TYPE_DEBUG,
1323 "Locking solver for bulk operation ...\n");
1324 GNUNET_assert (NULL != solver);
1330 * Bulk operation done
1333 GAS_proportional_bulk_stop (void *solver)
1335 struct GAS_PROPORTIONAL_Handle *s = solver;
1337 LOG (GNUNET_ERROR_TYPE_DEBUG,
1338 "Unlocking solver from bulk operation ...\n");
1339 if (s->bulk_lock < 1)
1345 if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1347 LOG (GNUNET_ERROR_TYPE_INFO,
1348 "No lock pending, recalculating\n");
1349 distribute_bandwidth_in_network (s, NULL);
1350 s->bulk_requests = 0;
1356 * Transport properties for this address have changed
1358 * @param solver solver handle
1359 * @param address the address
1360 * @param type the ATSI type in HBO
1361 * @param abs_value the absolute value of the property
1362 * @param rel_value the normalized value
1365 GAS_proportional_address_property_changed (void *solver,
1366 struct ATS_Address *address,
1367 enum GNUNET_ATS_Property type,
1371 struct GAS_PROPORTIONAL_Handle *s = solver;
1372 struct AddressWrapper *asi;
1373 struct ATS_Address *best_address;
1374 struct ATS_Address *active_address;
1376 asi = address->solver_information;
1377 LOG (GNUNET_ERROR_TYPE_INFO,
1378 "Property `%s' for peer `%s' address %p changed to %.2f\n",
1379 GNUNET_ATS_print_property_type (type),
1380 GNUNET_i2s (&address->peer),
1385 s->env->get_connectivity (s->env->cls,
1387 return; /* Peer is not requested */
1389 /* This peer is requested, find active and best address */
1390 active_address = get_active_address(s,
1392 best_address = update_active_address (s,
1395 if ((NULL != best_address) && ((NULL != active_address) &&
1396 (GNUNET_YES == address_eq (active_address, best_address))))
1398 asi = best_address->solver_information;
1399 GNUNET_assert (NULL != asi);
1401 /* We sticked to the same address, therefore redistribute */
1402 distribute_bandwidth_in_network (s, asi->network);
1408 * Add a new single address to a network
1410 * @param solver the solver Handle
1411 * @param address the address to add
1412 * @param network network type of this address
1415 GAS_proportional_address_add (void *solver,
1416 struct ATS_Address *address,
1417 enum GNUNET_ATS_Network_Type network)
1419 struct GAS_PROPORTIONAL_Handle *s = solver;
1420 struct Network *net;
1421 struct AddressWrapper *aw;
1423 net = get_network (s,
1431 aw = GNUNET_new (struct AddressWrapper);
1433 GNUNET_CONTAINER_DLL_insert (net->head,
1436 net->total_addresses++;
1437 GNUNET_STATISTICS_update (s->env->stats,
1438 "# ATS addresses total",
1441 GNUNET_STATISTICS_update (s->env->stats,
1446 address->solver_information = aw;
1448 LOG (GNUNET_ERROR_TYPE_INFO,
1449 "Adding new address %p for peer `%s', now total %u and active %u addresses in network `%s'\n",
1451 GNUNET_i2s (&address->peer),
1452 net->total_addresses,
1453 net->active_addresses,
1457 s->env->get_connectivity (s->env->cls,
1459 return; /* Peer is not requested */
1461 /* This peer is requested, find best address */
1462 update_active_address (s,
1468 * Remove an address from the solver. To do so, we:
1469 * - Removed it from specific network
1470 * - Decrease the number of total addresses
1472 * - decrease number of active addreses
1475 * @param solver the solver handle
1476 * @param address the address to remove
1479 GAS_proportional_address_delete (void *solver,
1480 struct ATS_Address *address)
1482 struct GAS_PROPORTIONAL_Handle *s = solver;
1483 struct AddressWrapper *aw = address->solver_information;
1484 struct Network *net = aw->network;
1486 LOG (GNUNET_ERROR_TYPE_DEBUG,
1487 "Deleting %s address for peer `%s' from network `%s' (total: %u/active: %u)\n",
1488 (GNUNET_NO == address->active) ? "inactive" : "active",
1489 GNUNET_i2s (&address->peer),
1491 net->total_addresses,
1492 net->active_addresses);
1494 GNUNET_CONTAINER_DLL_remove (net->head,
1497 GNUNET_assert (net->total_addresses > 0);
1498 net->total_addresses--;
1499 GNUNET_STATISTICS_update (s->env->stats,
1503 if (GNUNET_YES == address->active)
1505 /* Address was active, remove from network and update quotas*/
1506 address->active = GNUNET_NO;
1507 address->assigned_bw_in = 0;
1508 address->assigned_bw_out = 0;
1509 address_decrement_active (s, net);
1510 /* FIXME: this may trigger the solver unnecessarily,
1511 especially if update_active_address() succeeds! */
1512 distribute_bandwidth_in_network (s, net);
1515 update_active_address (s,
1518 /* No alternative address found, disconnect peer */
1519 LOG (GNUNET_ERROR_TYPE_INFO,
1520 "Disconnecting peer `%s' after deleting previous address %p\n",
1521 GNUNET_i2s (&address->peer),
1523 s->env->bandwidth_changed_cb (s->env->cls,
1528 address->solver_information = NULL;
1529 LOG (GNUNET_ERROR_TYPE_DEBUG,
1530 "After deleting address now total %u and active %u addresses in network `%s'\n",
1531 net->total_addresses,
1532 net->active_addresses,
1538 * Function invoked when the plugin is loaded.
1540 * @param[in,out] cls the `struct GNUNET_ATS_PluginEnvironment *` to use;
1541 * modified to return the API functions (ugh).
1542 * @return the `struct GAS_PROPORTIONAL_Handle` to pass as a closure
1545 libgnunet_plugin_ats_proportional_init (void *cls)
1547 static struct GNUNET_ATS_SolverFunctions sf;
1548 struct GNUNET_ATS_PluginEnvironment *env = cls;
1549 struct GAS_PROPORTIONAL_Handle *s;
1550 struct Network * cur;
1554 s = GNUNET_new (struct GAS_PROPORTIONAL_Handle);
1557 sf.s_add = &GAS_proportional_address_add;
1558 sf.s_address_update_property = &GAS_proportional_address_property_changed;
1559 sf.s_get = &GAS_proportional_get_preferred_address;
1560 sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
1561 sf.s_pref = &GAS_proportional_address_change_preference;
1562 sf.s_feedback = &GAS_proportional_address_preference_feedback;
1563 sf.s_del = &GAS_proportional_address_delete;
1564 sf.s_bulk_start = &GAS_proportional_bulk_start;
1565 sf.s_bulk_stop = &GAS_proportional_bulk_stop;
1566 s->stability_factor = PROP_STABILITY_FACTOR;
1567 if (GNUNET_SYSERR !=
1568 GNUNET_CONFIGURATION_get_value_float (env->cfg,
1570 "PROP_STABILITY_FACTOR",
1573 if ((f_tmp < 1.0) || (f_tmp > 2.0))
1575 LOG (GNUNET_ERROR_TYPE_ERROR,
1576 _("Invalid %s configuration %f \n"),
1577 "PROP_STABILITY_FACTOR",
1582 s->stability_factor = f_tmp;
1583 LOG (GNUNET_ERROR_TYPE_INFO,
1584 "Using %s of %.3f\n",
1585 "PROP_STABILITY_FACTOR",
1589 s->prop_factor = PROPORTIONALITY_FACTOR;
1590 if (GNUNET_SYSERR !=
1591 GNUNET_CONFIGURATION_get_value_float (env->cfg,
1593 "PROP_PROPORTIONALITY_FACTOR",
1598 LOG (GNUNET_ERROR_TYPE_ERROR,
1599 _("Invalid %s configuration %f\n"),
1600 "PROP_PROPORTIONALITY_FACTOR",
1605 s->prop_factor = f_tmp;
1606 LOG (GNUNET_ERROR_TYPE_INFO,
1607 "Using %s of %.3f\n",
1608 "PROP_PROPORTIONALITY_FACTOR"
1613 s->network_entries = GNUNET_malloc (env->network_count *
1614 sizeof (struct Network));
1615 for (c = 0; c < env->network_count; c++)
1617 cur = &s->network_entries[c];
1619 cur->total_quota_in = env->in_quota[c];
1620 cur->total_quota_out = env->out_quota[c];
1621 cur->desc = GNUNET_ATS_print_network_type (c);
1622 GNUNET_asprintf (&cur->stat_total,
1623 "# ATS addresses %s total",
1625 GNUNET_asprintf (&cur->stat_active,
1626 "# ATS active addresses %s total",
1628 LOG (GNUNET_ERROR_TYPE_INFO,
1629 "Added network %u `%s' (%llu/%llu)\n",
1632 cur->total_quota_in,
1633 cur->total_quota_out);
1639 /* end of plugin_ats_proportional.c */