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-simplistic",__VA_ARGS__)
36 * NOTE: Do not change this documentation. This documentation is based
37 * on gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
38 * use build_txt.sh to generate plaintext output
40 * ATS addresses : simplistic solver
42 * The simplistic solver ("simplistic") distributes the available
43 * bandwidth fair over all the addresses influenced by the
44 * preference values. For each available network type an in- and
45 * outbound quota is configured and the bandwidth available in
46 * these networks is distributed over the addresses. The solver
47 * first assigns every addresses the minimum amount of bandwidth
48 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
49 * remaining bandwidth available according to the preference
50 * values. For each peer only a single address gets bandwidth
51 * assigned and only one address marked as active. The most
52 * important functionality for the solver is implemented in: *
53 * find_address_it is an hashmap iterator returning the prefered
54 * address for an peer * update_quota_per_network distributes
55 * available bandwidth for a network over active addresses
57 * Changes to addresses automatically have an impact on the the
58 * bandwidth assigned to other addresses in the same network since
59 * the solver distributes the remaining bandwidth over the
60 * addresses in the network. When changes to the addresses occur,
61 * the solver first performs the changes, like adding or deleting
62 * addresses, and then updates bandwidth assignment for the
63 * affected network. Bandwidth assignment is only recalculated on
64 * demand when an address is requested by a client for a peer or
65 * when the addresses available have changed or an address changed
66 * the network it is located in. When the bandwidth assignment has
67 * changed the callback is called with the new bandwidth
68 * assignments. The bandwidth distribution for a network is
69 * recalculated due to: * address suggestion requests * address
70 * deletions * address switching networks during address update *
73 * 3.1 Data structures used
75 * For each ATS network (e.g. WAN, LAN, loopback) a struct Network
76 * is used to specify network related information as total adresses
77 * and active addresses in this network and the configured in- and
78 * outbound quota. Each network also contains a list of addresses
79 * added to the solver located in this network. The simplistic
80 * solver uses the addresses' solver_information field to store the
81 * simplistic network it belongs to for each address.
85 * When the simplistic solver is initialized the solver creates a
86 * new solver handle and initializes the network structures with
87 * the quotas passed from addresses and returns the handle solver.
89 * 3.3 Adding an address
91 * When a new address is added to the solver using s_add, a lookup
92 * for the network for this address is done and the address is
93 * enqueued in in the linked list of the network.
95 * 3.4 Updating an address
97 * The main purpose of address updates is to update the ATS
98 * information for addresse selection. Important for the simplistic
99 * solver is when an address switches network it is located
100 * in. This is common because addresses added by transport's
101 * validation mechanism are commonly located in
102 * GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
103 * in this network type and only if a connection is successful on
104 * return of payload data transport switches to the real network
105 * the address is located in. When an address changes networks it
106 * is first of all removed from the old network using the solver
107 * API function GAS_simplistic_address_delete and the network in
108 * the address struct is updated. A lookup for the respective new
109 * simplistic network is done and stored in the addresse's
110 * solver_information field. Next the address is re-added to the
111 * solver using the solver API function
112 * GAS_simplistic_address_add. If the address was marked as in
113 * active, the solver checks if bandwidth is available in the
114 * network and if yes sets the address to active and updates the
115 * bandwidth distribution in this network. If no bandwidth is
116 * available it sets the bandwidth for this address to 0 and tries
117 * to suggest an alternative address. If an alternative address was
118 * found, addresses' callback is called for this address.
120 * 3.5 Deleting an address
122 * When an address is removed from the solver, it removes the
123 * respective address from the network and if the address was
124 * marked as active, it updates the bandwidth distribution for this
127 * 3.6 Requesting addresses
129 * When an address is requested for a peer the solver performs a
130 * lookup for the peer entry in addresses address hashmap and
131 * selects the best address. The selection of the most suitable
132 * address is done in the find_address_it hashmap iterator
133 * described in detail in section 3.7. If no address is returned,
134 * no address can be suggested at the moment. If the address
135 * returned is marked as active, the solver can return this
136 * address. If the address is not marked as active, the solver
137 * checks if another address belongign to this peer is marked as
138 * active and marks the address as inactive, updates the bandwidth
139 * for this address to 0, call the bandwidth changed callback for
140 * this address due to the change and updates quota assignment for
141 * the addresse's network. the now in-active address is belonging
142 * to. The solver marks the new address as active and updates the
143 * bandwidth assignment for this network.
145 * 3.7 Choosing addresses
147 * Choosing the best possible address for suggestion is done by
148 * iterating over all addresses of a peer stored in addresses'
149 * hashmap and using the hashmap iterator find_address_it to select
150 * the best available address. Several checks are done when an
151 * address is selected. First if this address is currently blocked
152 * by addresses from being suggested. An address is blocked for the
153 * duration of ATS_BLOCKING_DELTA when it is suggested to
154 * transport. Next it is checked if at least
155 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
156 * in the addresse's network, because suggesting an address without
157 * bandwidth does not make sense. This also ensures that all active
158 * addresses in this network get at least the minimum amount of
159 * bandwidth assigned. In the next step the solver ensures that for
160 * tcp connections inbound connections are prefered over outbound
161 * connections. In the next stet the solver ensures that
162 * connections are prefered in the following order: * connections
163 * are already established and have bandwidth assigned *
164 * connections with a shorter distance * connectes have a shorter
167 * 3.8 Changing preferences
171 * During shutdown all network entries and aging processes are
172 * destroyed and freed.
177 * This solver assigns in and outbound bandwidth equally for all
178 * addresses in specific network type (WAN, LAN) based on configured
179 * in and outbound quota for this network.
181 * The solver is notified by addresses about changes to the addresses
182 * and recalculates the bandwith assigned if required. The solver
183 * notifies addresses by calling the GAS_bandwidth_changed_cb
191 * For each peer only a single is selected and marked as "active" in the address
196 * You have the networks WAN and LAN and quotas
197 * WAN_TOTAL_IN, WAN_TOTAL_OUT
198 * LAN_TOTAL_IN, LAN_TOTAL_OUT
200 * If you have x addresses in the network segment LAN, the quotas are
201 * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
203 * Quotas are automatically recalculated and reported back when addresses are
208 #define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
209 #define PREF_AGING_FACTOR 0.95
211 #define DEFAULT_PREFERENCE 1.0
212 #define MIN_UPDATE_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
215 * A handle for the simplistic solver
217 struct GAS_SIMPLISTIC_Handle
223 struct GNUNET_STATISTICS_Handle *stats;
226 * Total number of addresses for solver
228 unsigned int total_addresses;
231 * Number of active addresses for solver
233 unsigned int active_addresses;
238 struct Network *network_entries;
243 unsigned int networks;
248 GAS_bandwidth_changed_cb bw_changed;
253 void *bw_changed_cls;
255 struct GNUNET_CONTAINER_MultiHashMap *prefs;
257 struct PreferenceClient *pc_head;
258 struct PreferenceClient *pc_tail;
269 * Network description
274 * Total inbound quota
277 unsigned long long total_quota_in;
280 * Total outbound quota
283 unsigned long long total_quota_out;
286 * Number of active addresses for this network
288 unsigned int active_addresses;
291 * Number of total addresses for this network
293 unsigned int total_addresses;
296 * String for statistics total addresses
301 * String for statistics active addresses
305 struct AddressWrapper *head;
306 struct AddressWrapper *tail;
309 struct AddressWrapper
311 struct AddressWrapper *next;
312 struct AddressWrapper *prev;
314 struct ATS_Address *addr;
318 struct PreferenceClient
320 struct PreferenceClient *prev;
321 struct PreferenceClient *next;
324 double f_total[GNUNET_ATS_PreferenceCount];
326 struct PreferencePeer *p_head;
327 struct PreferencePeer *p_tail;
331 struct PreferencePeer
333 struct PreferencePeer *next;
334 struct PreferencePeer *prev;
335 struct PreferenceClient *client;
336 struct GAS_SIMPLISTIC_Handle *s;
337 struct GNUNET_PeerIdentity id;
339 double f[GNUNET_ATS_PreferenceCount];
340 double f_rel[GNUNET_ATS_PreferenceCount];
343 GNUNET_SCHEDULER_TaskIdentifier aging_task;
347 * Get the prefered address for a specific peer
349 * @param solver the solver handle
350 * @param addresses the address hashmap containing all addresses
351 * @param peer the identity of the peer
353 const struct ATS_Address *
354 GAS_simplistic_get_preferred_address (void *solver,
355 struct GNUNET_CONTAINER_MultiHashMap * addresses,
356 const struct GNUNET_PeerIdentity *peer);
361 * Important solver functions
362 * ---------------------------
370 * ---------------------------
374 free_pref (void *cls,
375 const struct GNUNET_HashCode * key,
387 * Test if bandwidth is available in this network
389 * @param net the network type to update
390 * @return GNUNET_YES or GNUNET_NO
394 bw_available_in_network (struct Network *net)
396 unsigned int na = net->active_addresses + 1;
397 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
398 if (((net->total_quota_in / na) > min_bw) &&
399 ((net->total_quota_out / na) > min_bw))
401 LOG (GNUNET_ERROR_TYPE_DEBUG,
402 "Enough bandwidth available for %u active addresses in network `%s'\n",
408 LOG (GNUNET_ERROR_TYPE_DEBUG,
409 "Not enough bandwidth available for %u active addresses in network `%s'\n",
417 * Update the quotas for a network type
419 * @param s the solver handle
420 * @param net the network type to update
421 * @param address_except address excluded from notifcation, since we suggest
425 update_quota_per_network (struct GAS_SIMPLISTIC_Handle *s,
427 struct ATS_Address *address_except)
429 unsigned long long remaining_quota_in = 0;
430 unsigned long long quota_out_used = 0;
432 unsigned long long remaining_quota_out = 0;
433 unsigned long long quota_in_used = 0;
434 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
435 double total_prefs; /* Important: has to be double not float due to precision */
436 double cur_pref; /* Important: has to be double not float due to precision */
437 double *t = NULL; /* Important: has to be double not float due to precision */
439 unsigned long long assigned_quota_in = 0;
440 unsigned long long assigned_quota_out = 0;
441 struct AddressWrapper *cur;
443 LOG (GNUNET_ERROR_TYPE_DEBUG,
444 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
445 net->desc, net->active_addresses, net->total_quota_in, net->total_quota_in);
447 if (net->active_addresses == 0)
448 return; /* no addresses to update */
451 * Assign every peer in network minimum Bandwidth
452 * Distribute bandwidth left according to preference
455 if ((net->active_addresses * min_bw) > net->total_quota_in)
460 if ((net->active_addresses * min_bw) > net->total_quota_out)
466 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
467 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
468 LOG (GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
469 remaining_quota_in, remaining_quota_out);
471 for (cur = net->head; NULL != cur; cur = cur->next)
473 if (GNUNET_YES == cur->addr->active)
475 t = GNUNET_CONTAINER_multihashmap_get (s->prefs, &cur->addr->peer.hashPubKey);
477 total_prefs += DEFAULT_PREFERENCE;
484 for (cur = net->head; NULL != cur; cur = cur->next)
486 if (GNUNET_YES == cur->addr->active)
489 t = GNUNET_CONTAINER_multihashmap_get (s->prefs, &cur->addr->peer.hashPubKey);
491 cur_pref = DEFAULT_PREFERENCE;
494 assigned_quota_in = min_bw + ((cur_pref / total_prefs) * remaining_quota_in);
495 assigned_quota_out = min_bw + ((cur_pref / total_prefs) * remaining_quota_out);
497 LOG (GNUNET_ERROR_TYPE_DEBUG,
498 "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
499 GNUNET_i2s (&cur->addr->peer),
500 cur_pref, total_prefs,
501 assigned_quota_in, assigned_quota_out);
505 assigned_quota_in = 0;
506 assigned_quota_out = 0;
509 quota_in_used += assigned_quota_in;
510 quota_out_used += assigned_quota_out;
511 /* Prevent overflow due to rounding errors */
512 if (assigned_quota_in > UINT32_MAX)
513 assigned_quota_in = UINT32_MAX;
514 if (assigned_quota_out > UINT32_MAX)
515 assigned_quota_out = UINT32_MAX;
517 /* Compare to current bandwidth assigned */
518 if ((assigned_quota_in != ntohl(cur->addr->assigned_bw_in.value__)) ||
519 (assigned_quota_out != ntohl(cur->addr->assigned_bw_out.value__)))
521 cur->addr->assigned_bw_in.value__ = htonl (assigned_quota_in);
522 cur->addr->assigned_bw_out.value__ = htonl (assigned_quota_out);
523 /* Notify on change */
524 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
525 s->bw_changed (s->bw_changed_cls, cur->addr);
529 LOG (GNUNET_ERROR_TYPE_DEBUG,
530 "Total bandwidth assigned is (in/out): %llu /%llu\n",
533 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
535 LOG (GNUNET_ERROR_TYPE_ERROR,
536 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
537 net->active_addresses,
539 net->total_quota_out);
541 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
543 LOG (GNUNET_ERROR_TYPE_ERROR,
544 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
545 net->active_addresses,
547 net->total_quota_in);
552 update_all_networks (struct GAS_SIMPLISTIC_Handle *s)
555 for (i = 0; i < s->networks; i++)
556 update_quota_per_network (s, &s->network_entries[i], NULL);
561 addresse_increment (struct GAS_SIMPLISTIC_Handle *s,
566 if (GNUNET_YES == total)
568 s->total_addresses ++;
569 net->total_addresses ++;
570 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
571 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
573 if (GNUNET_YES == active)
575 net->active_addresses ++;
576 s->active_addresses ++;
577 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
578 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
584 addresse_decrement (struct GAS_SIMPLISTIC_Handle *s,
590 if (GNUNET_YES == total)
592 if (s->total_addresses < 1)
599 s->total_addresses --;
600 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
602 if (net->total_addresses < 1)
609 net->total_addresses --;
610 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
614 if (GNUNET_YES == active)
616 if (net->active_addresses < 1)
623 net->active_addresses --;
624 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
626 if (s->active_addresses < 1)
633 s->active_addresses --;
634 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
641 * Extract an ATS performance info from an address
643 * @param address the address
644 * @param type the type to extract in HBO
645 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
648 get_performance_info (struct ATS_Address *address, uint32_t type)
651 GNUNET_assert (NULL != address);
653 if ((NULL == address->atsi) || (0 == address->atsi_count))
654 return GNUNET_ATS_VALUE_UNDEFINED;
656 for (c1 = 0; c1 < address->atsi_count; c1++)
658 if (ntohl(address->atsi[c1].type) == type)
659 return ntohl(address->atsi[c1].value);
661 return GNUNET_ATS_VALUE_UNDEFINED;
666 * Add a single address within a network to the solver
668 * @param solver the solver Handle
669 * @param addresses the address hashmap containing all addresses
670 * @param address the address to add
671 * @param network network type of this address
674 GAS_simplistic_address_add (void *solver,
675 struct GNUNET_CONTAINER_MultiHashMap *addresses,
676 struct ATS_Address *address,
679 struct GAS_SIMPLISTIC_Handle *s = solver;
680 struct Network *net = NULL;
681 struct AddressWrapper *aw = NULL;
684 GNUNET_assert (NULL != s);
685 for (c = 0; c < s->networks; c++)
687 net = &s->network_entries[c];
688 if (network == net->type)
697 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
699 GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
700 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
701 aw->addr->solver_information = net;
703 LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
704 net->total_addresses,
705 net->active_addresses,
710 * Remove an address from the solver
712 * @param solver the solver handle
713 * @param addresses the address hashmap containing all addresses
714 * @param address the address to remove
715 * @param session_only delete only session not whole address
718 GAS_simplistic_address_delete (void *solver,
719 struct GNUNET_CONTAINER_MultiHashMap * addresses,
720 struct ATS_Address *address, int session_only)
722 struct GAS_SIMPLISTIC_Handle *s = solver;
724 struct AddressWrapper *aw;
726 /* Remove an adress completely, we have to:
727 * - Remove from specific network
728 * - Decrease number of total addresses
730 * - decrease number of active addreses
734 net = (struct Network *) address->solver_information;
736 if (GNUNET_NO == session_only)
738 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
739 (GNUNET_NO == address->active) ? "inactive" : "active",
740 address, GNUNET_i2s (&address->peer),
741 net->desc, net->total_addresses, net->active_addresses);
744 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
745 for (aw = net->head; NULL != aw; aw = aw->next)
747 if (aw->addr == address)
755 GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
760 /* Remove session only: remove if active and update */
761 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
762 (GNUNET_NO == address->active) ? "inactive" : "active",
763 address, GNUNET_i2s (&address->peer),
764 net->desc, net->total_addresses, net->active_addresses);
767 if (GNUNET_YES == address->active)
769 /* Address was active, remove from network and update quotas*/
770 address->active = GNUNET_NO;
771 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
773 update_quota_per_network (s, net, NULL);
775 LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
776 net->total_addresses,
777 net->active_addresses,
782 static struct Network *
783 find_network (struct GAS_SIMPLISTIC_Handle *s, uint32_t type)
786 for (c = 0 ; c < s->networks; c++)
788 if (s->network_entries[c].type == type)
789 return &s->network_entries[c];
795 * Updates a single address in the solver
797 * If ATS information was updated, the previous values are passed
799 * @param solver the solver Handle
800 * @param addresses the address hashmap containing all addresses
801 * @param address the update address
802 * @param session the new session (if changed otherwise current)
803 * @param in_use the new address in use state (if changed otherwise current)
804 * @param prev_ats ATS information
805 * @param prev_atsi_count the atsi count
808 GAS_simplistic_address_update (void *solver,
809 struct GNUNET_CONTAINER_MultiHashMap *addresses,
810 struct ATS_Address *address,
813 const struct GNUNET_ATS_Information *prev_ats,
814 uint32_t prev_atsi_count)
816 struct ATS_Address *new;
817 struct GAS_SIMPLISTIC_Handle *s = (struct GAS_SIMPLISTIC_Handle *) solver;
822 int save_active = GNUNET_NO;
823 struct Network *new_net = NULL;
824 for (i = 0; i < prev_atsi_count; i++)
826 prev_type = ntohl (prev_ats[i].type);
827 prev_value = ntohl (prev_ats[i].value);
830 case GNUNET_ATS_UTILIZATION_UP:
831 case GNUNET_ATS_UTILIZATION_DOWN:
832 case GNUNET_ATS_QUALITY_NET_DELAY:
833 case GNUNET_ATS_QUALITY_NET_DISTANCE:
834 case GNUNET_ATS_COST_WAN:
835 case GNUNET_ATS_COST_LAN:
836 case GNUNET_ATS_COST_WLAN:
837 /* No actions required here*/
839 case GNUNET_ATS_NETWORK_TYPE:
841 addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
842 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
845 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
847 if (addr_net != prev_value)
849 /* Network changed */
850 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
851 (GNUNET_YES == address->active) ? "active" : "inactive",
852 GNUNET_ATS_print_network_type(prev_value),
853 GNUNET_ATS_print_network_type(addr_net));
855 save_active = address->active;
856 /* remove from old network */
857 GAS_simplistic_address_delete (solver, addresses, address, GNUNET_NO);
859 /* set new network type */
860 new_net = find_network (solver, addr_net);
861 address->solver_information = new_net;
863 /* Add to new network and update*/
864 GAS_simplistic_address_add (solver, addresses, address, addr_net);
865 if (GNUNET_YES == save_active)
867 /* check if bandwidth available in new network */
868 if (GNUNET_YES == (bw_available_in_network (new_net)))
870 /* Suggest updated address */
871 address->active = GNUNET_YES;
872 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
873 update_quota_per_network (solver, new_net, NULL);
877 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not enough bandwidth in new network, suggesting alternative address ..\n");
879 /* Set old address to zero bw */
880 address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
881 address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
882 s->bw_changed (s->bw_changed_cls, address);
884 /* Find new address to suggest since no bandwidth in network*/
885 new = (struct ATS_Address *) GAS_simplistic_get_preferred_address (s, addresses, &address->peer);
888 /* Have an alternative address to suggest */
889 s->bw_changed (s->bw_changed_cls, new);
897 case GNUNET_ATS_ARRAY_TERMINATOR:
900 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
901 "Received unsupported ATS type %u\n", prev_type);
908 if (address->session_id != session)
910 LOG (GNUNET_ERROR_TYPE_DEBUG,
911 "Session changed from %u to %u\n", address->session_id, session);
912 address->session_id = session;
914 if (address->used != in_use)
916 LOG (GNUNET_ERROR_TYPE_DEBUG,
917 "Usage changed from %u to %u\n", address->used, in_use);
918 address->used = in_use;
926 * Find a "good" address to use for a peer. If we already have an existing
927 * address, we stick to it. Otherwise, we pick by lowest distance and then
930 * @param cls the 'struct ATS_Address**' where we store the result
932 * @param value another 'struct ATS_Address*' to consider using
933 * @return GNUNET_OK (continue to iterate)
936 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
938 struct ATS_Address **previous_p = cls;
939 struct ATS_Address *current = (struct ATS_Address *) value;
940 struct ATS_Address *previous = *previous_p;
941 struct GNUNET_TIME_Absolute now;
942 struct Network *net = (struct Network *) current->solver_information;
943 uint32_t p_distance_cur;
944 uint32_t p_distance_prev;
945 uint32_t p_delay_cur;
946 uint32_t p_delay_prev;
948 now = GNUNET_TIME_absolute_get();
950 if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
952 /* This address is blocked for suggestion */
953 LOG (GNUNET_ERROR_TYPE_DEBUG,
954 "Address %p blocked for suggestion for %llu ms \n",
956 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
960 if (GNUNET_NO == bw_available_in_network (net))
961 return GNUNET_OK; /* There's no bandwidth available in this network */
963 if (NULL != previous)
965 if ((0 == strcmp (previous->plugin, "tcp")) &&
966 (0 == strcmp (current->plugin, "tcp")))
968 if ((0 != previous->addr_len) &&
969 (0 == current->addr_len))
971 /* saved address was an outbound address, but we have an inbound address */
972 *previous_p = current;
975 if (0 == previous->addr_len)
977 /* saved address was an inbound address, so do not overwrite */
983 if (NULL == previous)
985 *previous_p = current;
988 if ((ntohl (previous->assigned_bw_in.value__) == 0) &&
989 (ntohl (current->assigned_bw_in.value__) > 0))
991 /* stick to existing connection */
992 *previous_p = current;
996 p_distance_prev = get_performance_info (previous, GNUNET_ATS_QUALITY_NET_DISTANCE);
997 p_distance_cur = get_performance_info (current, GNUNET_ATS_QUALITY_NET_DISTANCE);
998 if ((p_distance_prev != GNUNET_ATS_VALUE_UNDEFINED) && (p_distance_cur != GNUNET_ATS_VALUE_UNDEFINED) &&
999 (p_distance_prev > p_distance_cur))
1001 /* user shorter distance */
1002 *previous_p = current;
1006 p_delay_prev = get_performance_info (previous, GNUNET_ATS_QUALITY_NET_DELAY);
1007 p_delay_cur = get_performance_info (current, GNUNET_ATS_QUALITY_NET_DELAY);
1008 if ((p_delay_prev != GNUNET_ATS_VALUE_UNDEFINED) && (p_delay_cur != GNUNET_ATS_VALUE_UNDEFINED) &&
1009 (p_delay_prev > p_delay_cur))
1011 /* user lower latency */
1012 *previous_p = current;
1021 find_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1023 struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
1024 struct ATS_Address * aa = (struct ATS_Address *) value;
1026 if (GNUNET_YES == aa->active)
1030 /* should never happen */
1031 LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
1040 static struct ATS_Address *
1041 find_active_address (void *solver,
1042 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1043 const struct GNUNET_PeerIdentity *peer)
1045 struct ATS_Address * dest = NULL;
1047 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
1049 &find_active_address_it, &dest);
1054 * Get the prefered address for a specific peer
1056 * @param solver the solver handle
1057 * @param addresses the address hashmap containing all addresses
1058 * @param peer the identity of the peer
1060 const struct ATS_Address *
1061 GAS_simplistic_get_preferred_address (void *solver,
1062 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1063 const struct GNUNET_PeerIdentity *peer)
1065 struct GAS_SIMPLISTIC_Handle *s = solver;
1066 struct Network *net_prev;
1067 struct Network *net_cur;
1068 struct ATS_Address *cur;
1069 struct ATS_Address *prev;
1071 GNUNET_assert (s != NULL);
1073 /* Get address with: stick to current address, lower distance, lower latency */
1074 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
1075 &find_address_it, &cur);
1078 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
1082 LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
1083 (GNUNET_NO == cur->active) ? "inactive" : "active",
1084 cur, GNUNET_i2s (peer));
1085 net_cur = (struct Network *) cur->solver_information;
1086 if (GNUNET_YES == cur->active)
1088 /* This address was selected previously, so no need to update quotas */
1092 /* This address was not active, so we have to:
1094 * - mark previous active address as not active
1095 * - update quota for previous address network
1096 * - update quota for this address network
1099 prev = find_active_address (s, addresses, peer);
1102 net_prev = (struct Network *) prev->solver_information;
1103 prev->active = GNUNET_NO; /* No active any longer */
1104 prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
1105 prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
1106 s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
1107 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
1109 update_quota_per_network (s, net_prev, NULL);
1112 if (GNUNET_NO == (bw_available_in_network (cur->solver_information)))
1114 GNUNET_break (0); /* This should never happen*/
1118 cur->active = GNUNET_YES;
1119 addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
1120 update_quota_per_network (s, net_cur, cur);
1127 * Stop notifying about address and bandwidth changes for this peer
1129 * @param solver the MLP handle
1130 * @param addresses address hashmap
1131 * @param peer the peer
1134 GAS_simplistic_stop_get_preferred_address (void *solver,
1135 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1136 const struct GNUNET_PeerIdentity *peer)
1143 * Preference calculation
1144 * ---------------------------
1149 recalculate_preferences (struct PreferencePeer *p)
1151 struct GAS_SIMPLISTIC_Handle *s = p->s;
1152 struct PreferencePeer *p_cur;
1153 struct PreferenceClient *c_cur = p->client;
1154 double p_rel_global;
1165 * Set of peers p_i in P
1166 * Set of preference kinds k
1167 * A preference value f_k_p_i with an unknown range
1170 * A client specific relative preference f_p_i_rel [1..2] for all peers
1172 * For every client c
1174 * For every preference kind k:
1176 * We remember for the preference f_p_i for each peer p_i.
1177 * We have a default preference value f_p_i = 0
1178 * We have a sum of all preferences f_t = sum (f_p_i)
1179 * So we can calculate a relative preference value fr_p_i:
1182 * f_k_p_i_rel = [1..2], default 1.0
1184 * f_p_i_rel = sum (f_k_p_i_rel) / count(k)
1189 /* For this client: for all preferences, except TERMINATOR */
1190 for (kind = GNUNET_ATS_PREFERENCE_END + 1 ; kind < GNUNET_ATS_PreferenceCount; kind ++)
1192 /* Recalcalculate total preference for this quality kind over all peers*/
1193 c_cur->f_total[kind] = 0;
1194 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
1195 c_cur->f_total[kind] += p_cur->f[kind];
1197 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p has total preference for %s of %.3f\n",
1199 GNUNET_ATS_print_preference_type (kind),
1200 c_cur->f_total[kind]);
1202 /* Recalcalculate relative preference for all peers */
1203 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
1205 /* Calculate relative preference for specific kind */
1206 if (0.0 == c_cur->f_total[kind])
1208 /* No one has preference, so set default preference */
1209 p_cur->f_rel[kind] = DEFAULT_PREFERENCE;
1213 p_cur->f_rel[kind] = (c_cur->f_total[kind] + p_cur->f[kind]) / c_cur->f_total[kind];
1215 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has relative preference for %s of %.3f\n",
1217 GNUNET_i2s (&p_cur->id),
1218 GNUNET_ATS_print_preference_type (kind),
1219 p_cur->f_rel[kind]);
1221 /* Calculate peer relative preference */
1222 /* Start with kind = 1 to exclude terminator */
1223 p_cur->f_rel_total = 0;
1224 for (rkind = GNUNET_ATS_PREFERENCE_END + 1; rkind < GNUNET_ATS_PreferenceCount; rkind ++)
1226 p_cur->f_rel_total += p_cur->f_rel[rkind];
1228 p_cur->f_rel_total /= (GNUNET_ATS_PreferenceCount - 1.0); /* -1 due to terminator */
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has total relative preference of %.3f\n",
1231 GNUNET_i2s (&p_cur->id),
1232 p_cur->f_rel_total);
1236 /* Calculcate global total relative peer preference over all clients */
1239 for (c_cur = s->pc_head; NULL != c_cur; c_cur = c_cur->next)
1241 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
1242 if (0 == memcmp (&p_cur->id, &p->id, sizeof (p_cur->id)))
1247 p_rel_global += p_cur->f_rel_total;
1250 p_rel_global /= clients;
1251 LOG (GNUNET_ERROR_TYPE_DEBUG, "Global preference value for peer `%s': %.3f\n",
1252 GNUNET_i2s (&p->id), p_rel_global);
1254 /* Update global map */
1255 if (NULL != (dest = GNUNET_CONTAINER_multihashmap_get(s->prefs, &p->id.hashPubKey)))
1256 (*dest) = p_rel_global;
1259 dest = GNUNET_malloc (sizeof (double));
1260 (*dest) = p_rel_global;
1261 GNUNET_CONTAINER_multihashmap_put(s->prefs,
1264 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1269 update_preference (struct PreferencePeer *p,
1270 enum GNUNET_ATS_PreferenceKind kind,
1273 double score = score_f;
1275 /* Update preference value according to type */
1277 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
1278 case GNUNET_ATS_PREFERENCE_LATENCY:
1279 p->f[kind] = (p->f[kind] + score) / 2;
1281 case GNUNET_ATS_PREFERENCE_END:
1286 recalculate_preferences(p);
1287 update_all_networks (p->s);
1292 preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1297 struct PreferencePeer *p = cls;
1298 GNUNET_assert (NULL != p);
1301 p->aging_task = GNUNET_SCHEDULER_NO_TASK;
1303 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aging preferences for peer `%s'\n",
1304 GNUNET_i2s (&p->id));
1306 /* Issue for aging :
1308 * Not for every peer preference values are set by default, so reducing the
1309 * absolute preference value does not help for aging because it does not have
1310 * influence on the relative values.
1312 * So we have to reduce the relative value to have an immediate impact on
1313 * quota calculation. In addition we cannot call recalculate_preferences here
1314 * but instead reduce the absolute value to have an aging impact on future
1315 * calls to change_preference where recalculate_preferences is called
1318 /* Aging absolute values: */
1319 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
1324 p->f[i] *= PREF_AGING_FACTOR;
1325 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aged preference for peer `%s' from %.3f to %.3f\n",
1326 GNUNET_i2s (&p->id), backup, p->f[i]);
1329 /* Updating relative value */
1330 t = GNUNET_CONTAINER_multihashmap_get (p->s->prefs, &p->id.hashPubKey);
1338 (*t) = (*t) * PREF_AGING_FACTOR;
1341 update_all_networks (p->s);
1343 p->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
1344 &preference_aging, p);
1349 * Changes the preferences for a peer in the problem
1351 * @param solver the solver handle
1352 * @param client the client with this preference
1353 * @param peer the peer to change the preference for
1354 * @param kind the kind to change the preference
1355 * @param score the score
1358 GAS_simplistic_address_change_preference (void *solver,
1360 const struct GNUNET_PeerIdentity *peer,
1361 enum GNUNET_ATS_PreferenceKind kind,
1364 static struct GNUNET_TIME_Absolute next_update;
1365 struct GAS_SIMPLISTIC_Handle *s = solver;
1366 struct PreferenceClient *c_cur;
1367 struct PreferencePeer *p_cur;
1370 GNUNET_assert (NULL != solver);
1371 GNUNET_assert (NULL != client);
1372 GNUNET_assert (NULL != peer);
1374 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p changes preference for peer `%s' %s %f\n",
1377 GNUNET_ATS_print_preference_type (kind),
1380 if (kind >= GNUNET_ATS_PreferenceCount)
1386 /* Find preference client */
1387 for (c_cur = s->pc_head; NULL != c_cur; c_cur = c_cur->next)
1389 if (client == c_cur->client)
1392 /* Not found: create new preference client */
1395 c_cur = GNUNET_malloc (sizeof (struct PreferenceClient));
1396 c_cur->client = client;
1397 GNUNET_CONTAINER_DLL_insert (s->pc_head, s->pc_tail, c_cur);
1400 /* Find entry for peer */
1401 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
1402 if (0 == memcmp (&p_cur->id, peer, sizeof (p_cur->id)))
1405 /* Not found: create new peer entry */
1408 p_cur = GNUNET_malloc (sizeof (struct PreferencePeer));
1410 p_cur->client = c_cur;
1411 p_cur->id = (*peer);
1412 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
1414 /* Default value per peer absolut preference for a quality:
1415 * No value set, so absolute preference 0 */
1417 /* Default value per peer relative preference for a quality: 1.0 */
1418 p_cur->f_rel[i] = DEFAULT_PREFERENCE;
1420 p_cur->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL, &preference_aging, p_cur);
1421 GNUNET_CONTAINER_DLL_insert (c_cur->p_head, c_cur->p_tail, p_cur);
1424 update_preference (p_cur, kind, score);
1426 /* FIXME: We should update quotas if UPDATE_INTERVAL is reached */
1427 if (GNUNET_TIME_absolute_get().abs_value > next_update.abs_value)
1430 next_update = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
1431 MIN_UPDATE_INTERVAL);
1437 * Solver API functions
1438 * ---------------------------
1443 * Shutdown the proportional problem solver
1445 * @param solver the respective handle to shutdown
1448 GAS_proportional_done (void *solver)
1450 struct GAS_SIMPLISTIC_Handle *s = solver;
1451 struct PreferenceClient *pc;
1452 struct PreferenceClient *next_pc;
1453 struct PreferencePeer *p;
1454 struct PreferencePeer *next_p;
1455 struct AddressWrapper *cur;
1456 struct AddressWrapper *next;
1458 GNUNET_assert (s != NULL);
1460 for (c = 0; c < s->networks; c++)
1462 if (s->network_entries[c].total_addresses > 0)
1464 LOG (GNUNET_ERROR_TYPE_ERROR,
1465 "Had %u addresses for network `%s' not deleted during shutdown\n",
1466 s->network_entries[c].total_addresses,
1467 s->network_entries[c].desc);
1471 if (s->network_entries[c].active_addresses > 0)
1473 LOG (GNUNET_ERROR_TYPE_ERROR,
1474 "Had %u active addresses for network `%s' not deleted during shutdown\n",
1475 s->network_entries[c].active_addresses,
1476 s->network_entries[c].desc);
1480 next = s->network_entries[c].head;
1481 while (NULL != (cur = next))
1484 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
1485 s->network_entries[c].tail,
1489 GNUNET_free (s->network_entries[c].stat_total);
1490 GNUNET_free (s->network_entries[c].stat_active);
1492 if (s->total_addresses > 0)
1494 LOG (GNUNET_ERROR_TYPE_ERROR,
1495 "Had %u addresses not deleted during shutdown\n",
1496 s->total_addresses);
1499 if (s->active_addresses > 0)
1501 LOG (GNUNET_ERROR_TYPE_ERROR,
1502 "Had %u active addresses not deleted during shutdown\n",
1503 s->active_addresses);
1506 GNUNET_free (s->network_entries);
1508 next_pc = s->pc_head;
1509 while (NULL != (pc = next_pc))
1512 GNUNET_CONTAINER_DLL_remove (s->pc_head, s->pc_tail, pc);
1513 next_p = pc->p_head;
1514 while (NULL != (p = next_p))
1517 if (GNUNET_SCHEDULER_NO_TASK != p->aging_task)
1519 GNUNET_SCHEDULER_cancel(p->aging_task);
1520 p->aging_task = GNUNET_SCHEDULER_NO_TASK;
1522 GNUNET_CONTAINER_DLL_remove (pc->p_head, pc->p_tail, p);
1528 GNUNET_CONTAINER_multihashmap_iterate (s->prefs, &free_pref, NULL);
1529 GNUNET_CONTAINER_multihashmap_destroy (s->prefs);
1535 * Init the proportional problem solver
1538 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1539 * out_quota[i] contains outbound quota for network type i
1540 * in_quota[i] contains inbound quota for network type i
1543 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1544 * network[2] == GNUNET_ATS_NET_LAN
1545 * out_quota[2] == 65353
1546 * in_quota[2] == 65353
1548 * @param cfg configuration handle
1549 * @param stats the GNUNET_STATISTICS handle
1550 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1551 * @param out_quota array of outbound quotas
1552 * @param in_quota array of outbound quota
1553 * @param dest_length array length for quota arrays
1554 * @param bw_changed_cb callback for changed bandwidth amounts
1555 * @param bw_changed_cb_cls cls for callback
1556 * @return handle for the solver on success, NULL on fail
1559 GAS_proportional_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1560 const struct GNUNET_STATISTICS_Handle *stats,
1562 unsigned long long *out_quota,
1563 unsigned long long *in_quota,
1565 GAS_bandwidth_changed_cb bw_changed_cb,
1566 void *bw_changed_cb_cls)
1569 struct GAS_SIMPLISTIC_Handle *s = GNUNET_malloc (sizeof (struct GAS_SIMPLISTIC_Handle));
1570 struct Network * cur;
1571 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1574 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1575 s->bw_changed = bw_changed_cb;
1576 s->bw_changed_cls = bw_changed_cb_cls;
1577 s->networks = dest_length;
1578 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
1579 s->active_addresses = 0;
1580 s->total_addresses = 0;
1581 s->prefs = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1583 for (c = 0; c < dest_length; c++)
1585 cur = &s->network_entries[c];
1586 cur->total_addresses = 0;
1587 cur->active_addresses = 0;
1588 cur->type = network[c];
1589 cur->total_quota_in = in_quota[c];
1590 cur->total_quota_out = out_quota[c];
1591 cur->desc = net_str[c];
1592 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
1593 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
1598 /* end of gnunet-service-ats-solver_proportional.c */