2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file ats/plugin_ats_proportional.c
20 * @brief ATS proportional solver
21 * @author Matthias Wachs
22 * @author Christian Grothoff
25 #include "gnunet_statistics_service.h"
26 #include "gnunet_ats_service.h"
27 #include "gnunet_ats_plugin.h"
28 #include "gnunet-service-ats_addresses.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
33 * How much do we value stability over adaptation by default. A low
34 * value (close to 1.0) means we adapt as soon as possible, a larger
35 * value means that we have to have the respective factor of an
36 * advantage (or delay) before we adapt and sacrifice stability.
38 #define PROP_STABILITY_FACTOR 1.25
42 * Default value to assume for the proportionality factor, if none is
43 * given in the configuration. This factor determines how strong the
44 * bandwidth allocation will orient itself on the application
45 * preferences. A lower factor means a more balanced bandwidth
46 * distribution while a larger number means a distribution more in
47 * line with application (bandwidth) preferences.
49 #define PROPORTIONALITY_FACTOR 2.0
53 * Address information stored for the proportional solver in the
54 * `solver_information` member of `struct GNUNET_ATS_Address`.
56 * They are also stored in the respective `struct Network`'s linked
64 struct AddressWrapper *next;
69 struct AddressWrapper *prev;
74 struct ATS_Address *addr;
77 * Network scope this address is in
79 struct Network *network;
84 uint32_t calculated_quota_in;
89 uint32_t calculated_quota_out;
92 * When was this address activated
94 struct GNUNET_TIME_Absolute activated;
100 * Representation of a network
105 * Network description
110 * String for statistics total addresses
115 * String for statistics active addresses
120 * Linked list of addresses in this network: head
122 struct AddressWrapper *head;
125 * Linked list of addresses in this network: tail
127 struct AddressWrapper *tail;
130 * Total inbound quota
132 unsigned long long total_quota_in;
135 * Total outbound quota
137 unsigned long long total_quota_out;
142 enum GNUNET_ATS_Network_Type type;
145 * Number of active addresses for this network
147 unsigned int active_addresses;
150 * Number of total addresses for this network
152 unsigned int total_addresses;
158 * A handle for the proportional solver
160 struct GAS_PROPORTIONAL_Handle
164 * Our execution environment.
166 struct GNUNET_ATS_PluginEnvironment *env;
171 struct Network *network_entries;
174 * Proportionality factor
181 double stability_factor;
184 * Bulk lock counter. If zero, we are not locked.
186 unsigned int bulk_lock;
189 * Number of changes made while solver was locked. We really only
190 * use 0/non-zero to check on unlock if we have to run the update.
192 unsigned int bulk_requests;
195 * Number of active addresses for solver
197 unsigned int active_addresses;
203 * Test if bandwidth is available in this network to add an additional address.
205 * @param net the network type to check
206 * @param extra for how many extra addresses do we check?
207 * @return #GNUNET_YES or #GNUNET_NO
210 is_bandwidth_available_in_network (struct Network *net,
214 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
216 GNUNET_assert (((int)net->active_addresses) + extra >= 0);
217 na = net->active_addresses + extra;
220 if ( ((net->total_quota_in / na) > min_bw) &&
221 ((net->total_quota_out / na) > min_bw) )
228 * Test if all peers in this network require connectivity at level at
231 * @param s the solver handle
232 * @param net the network type to check
233 * @param con connection return value threshold to check
234 * @return #GNUNET_YES or #GNUNET_NO
237 all_require_connectivity (struct GAS_PROPORTIONAL_Handle *s,
241 struct AddressWrapper *aw;
243 for (aw = net->head; NULL != aw; aw = aw->next)
245 s->env->get_connectivity (s->env->cls,
253 * Update bandwidth assigned to peers in this network. The basic idea
254 * is to assign every peer in the network the minimum bandwidth, and
255 * then distribute the remaining bandwidth proportional to application
258 * @param s the solver handle
259 * @param net the network type to update
262 distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
265 const uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
266 struct AddressWrapper *aw;
267 unsigned long long remaining_quota_in;
268 unsigned long long quota_out_used;
269 unsigned long long remaining_quota_out;
270 unsigned long long quota_in_used;
271 unsigned int count_addresses;
272 double sum_relative_peer_prefences;
275 const double *peer_relative_prefs;
277 LOG (GNUNET_ERROR_TYPE_INFO,
278 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
280 net->active_addresses,
282 net->total_quota_in);
284 if (0 == net->active_addresses)
285 return; /* no addresses to update */
288 if ((net->active_addresses * min_bw) > net->total_quota_in)
293 if ((net->active_addresses * min_bw) > net->total_quota_out)
299 /* Calculate sum of relative preference for active addresses in this
301 sum_relative_peer_prefences = 0.0;
303 for (aw = net->head; NULL != aw; aw = aw->next)
305 if (GNUNET_YES != aw->addr->active)
307 peer_relative_prefs = s->env->get_preferences (s->env->cls,
309 sum_relative_peer_prefences
310 += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
313 if (count_addresses != net->active_addresses)
316 LOG (GNUNET_ERROR_TYPE_WARNING,
317 "%s: Counted %u active addresses, expected %u active addresses\n",
320 net->active_addresses);
322 net->active_addresses = count_addresses;
324 LOG (GNUNET_ERROR_TYPE_INFO,
325 "Total relative preference %.3f for %u addresses in network %s\n",
326 sum_relative_peer_prefences,
327 net->active_addresses,
330 /* check how much we have to distribute */
331 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
332 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
333 LOG (GNUNET_ERROR_TYPE_DEBUG,
334 "Proportionally distributable bandwidth (in/out): %llu/%llu\n",
336 remaining_quota_out);
338 /* distribute remaining quota; we do not do it exactly proportional,
339 but balance "even" distribution ("net->active_addresses") with
340 the preference sum using the "prop_factor". */
341 total_weight = net->active_addresses +
342 s->prop_factor * sum_relative_peer_prefences;
345 for (aw = net->head; NULL != aw; aw = aw->next)
347 if (GNUNET_YES != aw->addr->active)
349 /* set to 0, just to be sure */
350 aw->calculated_quota_in = 0;
351 aw->calculated_quota_out = 0;
354 peer_relative_prefs = s->env->get_preferences (s->env->cls,
357 + s->prop_factor * peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
359 aw->calculated_quota_in = min_bw
360 + (peer_weight / total_weight) * remaining_quota_in;
361 aw->calculated_quota_out = min_bw
362 + (peer_weight / total_weight) * remaining_quota_out;
364 LOG (GNUNET_ERROR_TYPE_INFO,
365 "New quotas for peer `%s' with weight (cur/total) %.3f/%.3f (in/out) are: %u/%u\n",
366 GNUNET_i2s (&aw->addr->peer),
369 (unsigned int) aw->calculated_quota_in,
370 (unsigned int) aw->calculated_quota_out);
371 quota_in_used += aw->calculated_quota_in;
372 quota_out_used += aw->calculated_quota_out;
374 LOG (GNUNET_ERROR_TYPE_DEBUG,
375 "Total bandwidth assigned is (in/out): %llu /%llu\n",
378 /* +1 due to possible rounding errors */
379 GNUNET_break (quota_out_used <= net->total_quota_out + 1);
380 GNUNET_break (quota_in_used <= net->total_quota_in + 1);
385 * Notify ATS service of bandwidth changes to addresses.
387 * @param s solver handle
388 * @param net the network to propagate changes in
391 propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
394 struct AddressWrapper *cur;
396 for (cur = net->head; NULL != cur; cur = cur->next)
398 if ( (cur->addr->assigned_bw_in == cur->calculated_quota_in) &&
399 (cur->addr->assigned_bw_out == cur->calculated_quota_out) )
401 cur->addr->assigned_bw_in = cur->calculated_quota_in;
402 cur->addr->assigned_bw_out = cur->calculated_quota_out;
403 if (GNUNET_YES == cur->addr->active)
404 s->env->bandwidth_changed_cb (s->env->cls,
411 * Distribute bandwidth. The addresses have already been selected,
412 * this is merely distributed the bandwidth among the addresses.
414 * @param s the solver handle
415 * @param n the network, can be NULL for all networks
418 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
423 if (0 != s->bulk_lock)
430 LOG (GNUNET_ERROR_TYPE_DEBUG,
431 "Redistributing bandwidth in network %s with %u active and %u total addresses\n",
432 GNUNET_ATS_print_network_type(n->type),
435 s->env->info_cb (s->env->cls,
438 GAS_INFO_PROP_SINGLE);
439 distribute_bandwidth(s,
441 s->env->info_cb (s->env->cls,
444 GAS_INFO_PROP_SINGLE);
445 s->env->info_cb (s->env->cls,
446 GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
448 GAS_INFO_PROP_SINGLE);
449 propagate_bandwidth (s,
452 s->env->info_cb (s->env->cls,
453 GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
455 GAS_INFO_PROP_SINGLE);
459 LOG (GNUNET_ERROR_TYPE_DEBUG,
460 "Redistributing bandwidth in all %u networks\n",
461 s->env->network_count);
462 s->env->info_cb (s->env->cls,
466 for (i = 0; i < s->env->network_count; i++)
467 distribute_bandwidth (s,
468 &s->network_entries[i]);
469 s->env->info_cb (s->env->cls,
473 s->env->info_cb (s->env->cls,
474 GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
477 for (i = 0; i < s->env->network_count; i++)
478 propagate_bandwidth (s,
479 &s->network_entries[i]);
480 s->env->info_cb (s->env->cls,
481 GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
489 * Context for finding the best address* Linked list of addresses in this network: head
491 struct FindBestAddressCtx
496 struct GAS_PROPORTIONAL_Handle *s;
499 * The currently best address
501 struct ATS_Address *best;
506 * Find a "good" address to use for a peer by iterating over the
507 * addresses for this peer. If we already have an existing address,
508 * we stick to it. Otherwise, we pick by lowest distance and then by
511 * @param cls the `struct FindBestAddressCtx *' where we store the result
512 * @param key the peer we are trying to find the best address for
513 * @param value another `struct ATS_Address*` to consider using
514 * @return #GNUNET_OK (continue to iterate)
517 find_best_address_it (void *cls,
518 const struct GNUNET_PeerIdentity *key,
521 struct FindBestAddressCtx *ctx = cls;
522 struct ATS_Address *current = value;
523 struct AddressWrapper *asi = current->solver_information;
524 struct GNUNET_TIME_Relative active_time;
526 double best_distance;
533 /* we need +1 slot if 'current' is not yet active */
534 need = (GNUNET_YES == current->active) ? 0 : 1;
535 /* we save -1 slot if 'best' is active and belongs
536 to the same network (as we would replace it) */
537 if ( (NULL != ctx->best) &&
538 (GNUNET_YES == ctx->best->active) &&
539 (((struct AddressWrapper *) ctx->best->solver_information)->network ==
542 /* we can gain -1 slot if this peers connectivity
543 requirement is higher than that of another peer
544 in that network scope */
545 con = ctx->s->env->get_connectivity (ctx->s->env->cls,
548 all_require_connectivity (ctx->s,
552 /* test if minimum bandwidth for 'current' would be available */
554 = is_bandwidth_available_in_network (asi->network,
558 /* Bandwidth for this address is unavailable, so we cannot use
562 if (GNUNET_YES == current->active)
564 active_time = GNUNET_TIME_absolute_get_duration (asi->activated);
565 if (active_time.rel_value_us <=
566 ((double) GNUNET_TIME_UNIT_SECONDS.rel_value_us) * ctx->s->stability_factor)
568 /* Keep active address for stability reasons */
573 if (NULL == ctx->best)
575 /* We so far have nothing else, so go with it! */
580 /* Now compare ATS information */
581 cur_distance = current->norm_distance.norm;
582 best_distance = ctx->best->norm_distance.norm;
583 cur_delay = current->norm_delay.norm;
584 best_delay = ctx->best->norm_delay.norm;
586 /* user shorter distance */
587 if (cur_distance < best_distance)
589 if (GNUNET_NO == ctx->best->active)
591 /* Activity doesn't influence the equation, use current */
594 else if ((best_distance / cur_distance) > ctx->s->stability_factor)
596 /* Distance change is significant, switch active address! */
601 /* User connection with less delay */
602 if (cur_delay < best_delay)
604 if (GNUNET_NO == ctx->best->active)
606 /* Activity doesn't influence the equation, use current */
609 else if ((best_delay / cur_delay) > ctx->s->stability_factor)
611 /* Latency change is significant, switch active address! */
620 * Find the currently best address for a peer from the set of
621 * addresses available or return NULL of no address is available.
623 * @param s the proportional handle
624 * @param addresses the address hashmap
625 * @param id the peer id
626 * @return the address or NULL
629 get_best_address (struct GAS_PROPORTIONAL_Handle *s,
630 struct GNUNET_CONTAINER_MultiPeerMap *addresses,
631 const struct GNUNET_PeerIdentity *id)
633 struct FindBestAddressCtx fba_ctx;
637 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
639 &find_best_address_it,
646 * Decrease number of active addresses in network.
648 * @param s the solver handle
649 * @param net the network type
652 address_decrement_active (struct GAS_PROPORTIONAL_Handle *s,
655 GNUNET_assert (net->active_addresses > 0);
656 net->active_addresses--;
657 GNUNET_STATISTICS_update (s->env->stats,
661 GNUNET_assert (s->active_addresses > 0);
662 s->active_addresses--;
663 GNUNET_STATISTICS_update (s->env->stats,
664 "# ATS addresses total",
671 * Address map iterator to find current active address for peer.
672 * Asserts that only one address is active per peer.
674 * @param cls last active address
675 * @param key peer's key
676 * @param value address to check
677 * @return #GNUNET_NO on double active address else #GNUNET_YES;
680 get_active_address_it (void *cls,
681 const struct GNUNET_PeerIdentity *key,
684 struct ATS_Address **dest = cls;
685 struct ATS_Address *aa = value;
687 if (GNUNET_YES != aa->active)
689 GNUNET_assert (NULL == (*dest));
696 * Find current active address for peer
698 * @param s the solver handle
699 * @param peer the peer
700 * @return active address or NULL
702 static struct ATS_Address *
703 get_active_address (struct GAS_PROPORTIONAL_Handle *s,
704 const struct GNUNET_PeerIdentity *peer)
706 struct ATS_Address *dest;
709 GNUNET_CONTAINER_multipeermap_get_multiple (s->env->addresses,
711 &get_active_address_it,
718 * Update active address for a peer. Check if active address exists
719 * and what the best address is, if addresses are different switch.
720 * Then reallocate bandwidth within the affected network scopes.
722 * @param s solver handle
723 * @param current_address the address currently active for the peer,
725 * @param peer the peer to check
728 update_active_address (struct GAS_PROPORTIONAL_Handle *s,
729 struct ATS_Address *current_address,
730 const struct GNUNET_PeerIdentity *peer)
732 struct ATS_Address *best_address;
733 struct AddressWrapper *asi_cur;
734 struct AddressWrapper *asi_best;
735 struct AddressWrapper *aw;
736 struct AddressWrapper *aw_min;
738 unsigned int con_min;
740 best_address = get_best_address (s,
743 if (NULL != best_address)
744 asi_best = best_address->solver_information;
747 if (current_address == best_address)
748 return; /* no changes */
749 if (NULL != current_address)
751 /* We switch to a new address (or to none);
752 mark old address as inactive. */
753 asi_cur = current_address->solver_information;
754 GNUNET_assert (GNUNET_YES == current_address->active);
755 LOG (GNUNET_ERROR_TYPE_INFO,
756 "Disabling previous active address for peer `%s'\n",
758 asi_cur->activated = GNUNET_TIME_UNIT_ZERO_ABS;
759 current_address->active = GNUNET_NO;
760 current_address->assigned_bw_in = 0;
761 current_address->assigned_bw_out = 0;
762 address_decrement_active (s,
764 if ( (NULL == best_address) ||
765 (asi_best->network != asi_cur->network) )
766 distribute_bandwidth_in_network (s,
768 if (NULL == best_address)
770 /* We previously had an active address, but now we cannot
771 * suggest one. Therefore we have to disconnect the peer.
772 * The above call to "distribute_bandwidth_in_network()
773 * does not see 'current_address' so we need to trigger
774 * the update here. */
775 LOG (GNUNET_ERROR_TYPE_DEBUG,
776 "Disconnecting peer `%s'.\n",
778 s->env->bandwidth_changed_cb (s->env->cls,
783 if (NULL == best_address)
785 /* We do not have a new address, so we are done. */
786 LOG (GNUNET_ERROR_TYPE_DEBUG,
787 "Cannot suggest address for peer `%s'\n",
791 /* We do have a new address, activate it */
792 LOG (GNUNET_ERROR_TYPE_DEBUG,
793 "Selecting new address %p for peer `%s'\n",
796 /* Mark address as active */
797 best_address->active = GNUNET_YES;
798 asi_best->activated = GNUNET_TIME_absolute_get ();
799 asi_best->network->active_addresses++;
800 s->active_addresses++;
801 GNUNET_STATISTICS_update (s->env->stats,
802 "# ATS active addresses total",
805 GNUNET_STATISTICS_update (s->env->stats,
806 asi_best->network->stat_active,
809 LOG (GNUNET_ERROR_TYPE_INFO,
810 "Address %p for peer `%s' is now active\n",
815 is_bandwidth_available_in_network (asi_best->network,
818 /* we went over the maximum number of addresses for
819 this scope; remove the address with the smallest
820 connectivity requirement */
821 con_min = UINT32_MAX;
823 for (aw = asi_best->network->head; NULL != aw; aw = aw->next)
826 (a_con = s->env->get_connectivity (s->env->cls,
827 &aw->addr->peer))) &&
828 (GNUNET_YES == aw->addr->active) )
836 update_active_address (s,
838 &aw_min->addr->peer);
840 distribute_bandwidth_in_network (s,
846 * The preferences for a peer in the problem changed.
848 * @param solver the solver handle
849 * @param peer the peer to change the preference for
850 * @param kind the kind to change the preference
851 * @param pref_rel the normalized preference value for this kind over all clients
854 GAS_proportional_change_preference (void *solver,
855 const struct GNUNET_PeerIdentity *peer,
856 enum GNUNET_ATS_PreferenceKind kind,
859 struct GAS_PROPORTIONAL_Handle *s = solver;
861 if (GNUNET_ATS_PREFERENCE_BANDWIDTH != kind)
862 return; /* we do not care */
863 distribute_bandwidth_in_network (s,
869 * Get application feedback for a peer
871 * @param solver the solver handle
872 * @param application the application
873 * @param peer the peer to change the preference for
874 * @param scope the time interval for this feedback: [now - scope .. now]
875 * @param kind the kind to change the preference
876 * @param score the score
879 GAS_proportional_feedback (void *solver,
880 struct GNUNET_SERVICE_Client *application,
881 const struct GNUNET_PeerIdentity *peer,
882 const struct GNUNET_TIME_Relative scope,
883 enum GNUNET_ATS_PreferenceKind kind,
886 /* Proportional does not care about feedback */
891 * Get the preferred address for a specific peer
893 * @param solver the solver handle
894 * @param peer the identity of the peer
897 GAS_proportional_start_get_address (void *solver,
898 const struct GNUNET_PeerIdentity *peer)
900 struct GAS_PROPORTIONAL_Handle *s = solver;
902 update_active_address (s,
903 get_active_address (s,
910 * Stop notifying about address and bandwidth changes for this peer
912 * @param solver the solver handle
913 * @param peer the peer
916 GAS_proportional_stop_get_address (void *solver,
917 const struct GNUNET_PeerIdentity *peer)
919 struct GAS_PROPORTIONAL_Handle *s = solver;
920 struct ATS_Address *cur;
921 struct AddressWrapper *asi;
923 cur = get_active_address (s,
927 asi = cur->solver_information;
928 distribute_bandwidth_in_network (s,
934 * Start a bulk operation
936 * @param solver the solver
939 GAS_proportional_bulk_start (void *solver)
941 struct GAS_PROPORTIONAL_Handle *s = solver;
943 LOG (GNUNET_ERROR_TYPE_DEBUG,
944 "Locking solver for bulk operation ...\n");
945 GNUNET_assert (NULL != solver);
951 * Bulk operation done.
953 * @param solver our `struct GAS_PROPORTIONAL_Handle *`
956 GAS_proportional_bulk_stop (void *solver)
958 struct GAS_PROPORTIONAL_Handle *s = solver;
960 LOG (GNUNET_ERROR_TYPE_DEBUG,
961 "Unlocking solver from bulk operation ...\n");
962 if (s->bulk_lock < 1)
968 if ( (0 == s->bulk_lock) &&
969 (0 < s->bulk_requests) )
971 LOG (GNUNET_ERROR_TYPE_INFO,
972 "No lock pending, recalculating\n");
973 distribute_bandwidth_in_network (s,
975 s->bulk_requests = 0;
981 * Transport properties for this address have changed
983 * @param solver solver handle
984 * @param address the address
987 GAS_proportional_address_property_changed (void *solver,
988 struct ATS_Address *address)
990 struct GAS_PROPORTIONAL_Handle *s = solver;
991 struct AddressWrapper *asi = address->solver_information;
993 distribute_bandwidth_in_network (s,
999 * Add a new single address to a network
1001 * @param solver the solver Handle
1002 * @param address the address to add
1003 * @param network network type of this address
1006 GAS_proportional_address_add (void *solver,
1007 struct ATS_Address *address,
1010 struct GAS_PROPORTIONAL_Handle *s = solver;
1011 struct Network *net;
1012 struct AddressWrapper *aw;
1014 GNUNET_assert (network < s->env->network_count);
1015 net = &s->network_entries[network];
1016 net->total_addresses++;
1018 aw = GNUNET_new (struct AddressWrapper);
1021 address->solver_information = aw;
1022 GNUNET_CONTAINER_DLL_insert (net->head,
1025 GNUNET_STATISTICS_update (s->env->stats,
1026 "# ATS addresses total",
1029 GNUNET_STATISTICS_update (s->env->stats,
1033 update_active_address (s,
1034 get_active_address (s,
1037 LOG (GNUNET_ERROR_TYPE_INFO,
1038 "Added new address for `%s', now total %u and active %u addresses in network `%s'\n",
1039 GNUNET_i2s (&address->peer),
1040 net->total_addresses,
1041 net->active_addresses,
1047 * Remove an address from the solver. To do so, we:
1048 * - Removed it from specific network
1049 * - Decrease the number of total addresses
1051 * - decrease number of active addreses
1054 * @param solver the solver handle
1055 * @param address the address to remove
1058 GAS_proportional_address_delete (void *solver,
1059 struct ATS_Address *address)
1061 struct GAS_PROPORTIONAL_Handle *s = solver;
1062 struct AddressWrapper *aw = address->solver_information;
1063 struct Network *net = aw->network;
1065 LOG (GNUNET_ERROR_TYPE_DEBUG,
1066 "Deleting %s address for peer `%s' from network `%s' (total: %u/active: %u)\n",
1067 (GNUNET_NO == address->active) ? "inactive" : "active",
1068 GNUNET_i2s (&address->peer),
1070 net->total_addresses,
1071 net->active_addresses);
1073 GNUNET_CONTAINER_DLL_remove (net->head,
1076 GNUNET_assert (net->total_addresses > 0);
1077 net->total_addresses--;
1078 GNUNET_STATISTICS_update (s->env->stats,
1082 if (GNUNET_YES == address->active)
1084 /* Address was active, remove from network and update quotas */
1085 update_active_address (s,
1088 distribute_bandwidth_in_network (s, net);
1091 address->solver_information = NULL;
1092 LOG (GNUNET_ERROR_TYPE_DEBUG,
1093 "After deleting address now total %u and active %u addresses in network `%s'\n",
1094 net->total_addresses,
1095 net->active_addresses,
1101 * Function invoked when the plugin is loaded.
1103 * @param[in,out] cls the `struct GNUNET_ATS_PluginEnvironment *` to use;
1104 * modified to return the API functions (ugh).
1105 * @return the `struct GAS_PROPORTIONAL_Handle` to pass as a closure
1108 libgnunet_plugin_ats_proportional_init (void *cls)
1110 static struct GNUNET_ATS_SolverFunctions sf;
1111 struct GNUNET_ATS_PluginEnvironment *env = cls;
1112 struct GAS_PROPORTIONAL_Handle *s;
1113 struct Network * cur;
1117 s = GNUNET_new (struct GAS_PROPORTIONAL_Handle);
1120 sf.s_add = &GAS_proportional_address_add;
1121 sf.s_address_update_property = &GAS_proportional_address_property_changed;
1122 sf.s_get = &GAS_proportional_start_get_address;
1123 sf.s_get_stop = &GAS_proportional_stop_get_address;
1124 sf.s_pref = &GAS_proportional_change_preference;
1125 sf.s_feedback = &GAS_proportional_feedback;
1126 sf.s_del = &GAS_proportional_address_delete;
1127 sf.s_bulk_start = &GAS_proportional_bulk_start;
1128 sf.s_bulk_stop = &GAS_proportional_bulk_stop;
1129 s->stability_factor = PROP_STABILITY_FACTOR;
1130 if (GNUNET_SYSERR !=
1131 GNUNET_CONFIGURATION_get_value_float (env->cfg,
1133 "PROP_STABILITY_FACTOR",
1136 if ((f_tmp < 1.0) || (f_tmp > 2.0))
1138 LOG (GNUNET_ERROR_TYPE_ERROR,
1139 _("Invalid %s configuration %f \n"),
1140 "PROP_STABILITY_FACTOR",
1145 s->stability_factor = f_tmp;
1146 LOG (GNUNET_ERROR_TYPE_INFO,
1147 "Using %s of %.3f\n",
1148 "PROP_STABILITY_FACTOR",
1152 s->prop_factor = PROPORTIONALITY_FACTOR;
1153 if (GNUNET_SYSERR !=
1154 GNUNET_CONFIGURATION_get_value_float (env->cfg,
1156 "PROP_PROPORTIONALITY_FACTOR",
1161 LOG (GNUNET_ERROR_TYPE_ERROR,
1162 _("Invalid %s configuration %f\n"),
1163 "PROP_PROPORTIONALITY_FACTOR",
1168 s->prop_factor = f_tmp;
1169 LOG (GNUNET_ERROR_TYPE_INFO,
1170 "Using %s of %.3f\n",
1171 "PROP_PROPORTIONALITY_FACTOR",
1176 s->network_entries = GNUNET_malloc (env->network_count *
1177 sizeof (struct Network));
1178 for (c = 0; c < env->network_count; c++)
1180 cur = &s->network_entries[c];
1182 cur->total_quota_in = env->in_quota[c];
1183 cur->total_quota_out = env->out_quota[c];
1184 cur->desc = GNUNET_ATS_print_network_type (c);
1185 GNUNET_asprintf (&cur->stat_total,
1186 "# ATS addresses %s total",
1188 GNUNET_asprintf (&cur->stat_active,
1189 "# ATS active addresses %s total",
1191 LOG (GNUNET_ERROR_TYPE_INFO,
1192 "Added network %u `%s' (%llu/%llu)\n",
1195 cur->total_quota_in,
1196 cur->total_quota_out);
1203 * Function used to unload the plugin.
1205 * @param cls return value from #libgnunet_plugin_ats_proportional_init()
1208 libgnunet_plugin_ats_proportional_done (void *cls)
1210 struct GNUNET_ATS_SolverFunctions *sf = cls;
1211 struct GAS_PROPORTIONAL_Handle *s = sf->cls;
1212 struct AddressWrapper *cur;
1213 struct AddressWrapper *next;
1216 for (c = 0; c < s->env->network_count; c++)
1218 GNUNET_break (0 == s->network_entries[c].total_addresses);
1219 GNUNET_break (0 == s->network_entries[c].active_addresses);
1220 next = s->network_entries[c].head;
1221 while (NULL != (cur = next))
1224 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
1225 s->network_entries[c].tail,
1227 GNUNET_free_non_null (cur->addr->solver_information);
1230 GNUNET_free (s->network_entries[c].stat_total);
1231 GNUNET_free (s->network_entries[c].stat_active);
1233 GNUNET_break (0 == s->active_addresses);
1234 GNUNET_free (s->network_entries);
1240 /* end of plugin_ats_proportional.c */