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_addresses_simplistic.h
23 * @brief ats simplistic ressource assignment
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 on
37 * 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 bandwidth
43 * fair over all the addresses influenced by the preference values. For each
44 * available network type an in- and outbound quota is configured and the
45 * bandwidth available in these networks is distributed over the addresses.
46 * The solver first assigns every addresses the minimum amount of bandwidth
47 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the remaining
48 * bandwidth available according to the preference values. For each peer only
49 * a single address gets bandwidth assigned and only one address marked as
51 * The most important functionality for the solver is implemented in:
53 * is an hashmap iterator returning the prefered address for an peer
54 * * update_quota_per_network
55 * distributes available bandwidth for a network over active addresses
57 * Changes to addresses automatically have an impact on the the bandwidth
58 * assigned to other addresses in the same network since the solver
59 * distributes the remaining bandwidth over the addresses in the network.
60 * When changes to the addresses occur, the solver first performs the
61 * changes, like adding or deleting addresses, and then updates bandwidth
62 * assignment for the affected network. Bandwidth assignment is only
63 * recalculated on demand when an address is requested by a client for a peer
64 * or when the addresses available have changed or an address changed the
65 * network it is located in. When the bandwidth assignment has changed the
66 * callback is called with the new bandwidth assignments. The bandwidth
67 * distribution for a network is recalculated due to:
68 * * address suggestion requests
70 * * address switching networks during address update
71 * * preference changes
73 * 3.1 Data structures used
75 * For each ATS network (e.g. WAN, LAN, loopback) a struct Network is used to
76 * specify network related information as total adresses and active addresses
77 * in this network and the configured in- and outbound quota. Each network
78 * also contains a list of addresses added to the solver located in this
79 * network. The simplistic solver uses the addresses' solver_information
80 * field to store the simplistic network it belongs to for each address.
84 * When the simplistic solver is initialized the solver creates a new solver
85 * handle and initializes the network structures with the quotas passed from
86 * addresses and returns the handle solver.
88 * 3.3 Adding an address
90 * When a new address is added to the solver using s_add, a lookup for the
91 * network for this address is done and the address is enqueued in in the
92 * linked list of the network.
94 * 3.4 Updating an address
96 * The main purpose of address updates is to update the ATS information for
97 * addresse selection. Important for the simplistic solver is when an address
98 * switches network it is located in. This is common because addresses added
99 * by transport's validation mechanism are commonly located in
100 * GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located in this
101 * network type and only if a connection is successful on return of payload
102 * data transport switches to the real network the address is located in.
103 * When an address changes networks it is first of all removed from the old
104 * network using the solver API function GAS_simplistic_address_delete and
105 * the network in the address struct is updated. A lookup for the respective
106 * new simplistic network is done and stored in the addresse's
107 * solver_information field. Next the address is re-added to the solver using
108 * the solver API function GAS_simplistic_address_add. If the address was
109 * marked as in active, the solver checks if bandwidth is available in the
110 * network and if yes sets the address to active and updates the bandwidth
111 * distribution in this network. If no bandwidth is available it sets the
112 * bandwidth for this address to 0 and tries to suggest an alternative
113 * address. If an alternative address was found, addresses' callback is
114 * called for this address.
116 * 3.5 Deleting an address
118 * When an address is removed from the solver, it removes the respective
119 * address from the network and if the address was marked as active, it
120 * updates the bandwidth distribution for this network.
122 * 3.6 Requesting addresses
124 * When an address is requested for a peer the solver performs a lookup for
125 * the peer entry in addresses address hashmap and selects the best address.
126 * The selection of the most suitable address is done in the find_address_it
127 * hashmap iterator described in detail in section 3.7. If no address is
128 * returned, no address can be suggested at the moment. If the address
129 * returned is marked as active, the solver can return this address. If the
130 * address is not marked as active, the solver checks if another address
131 * belongign to this peer is marked as active and marks the address as
132 * inactive, updates the bandwidth for this address to 0, call the bandwidth
133 * changed callback for this address due to the change and updates quota
134 * assignment for the addresse's network. the now in-active address is
135 * belonging to. The solver marks the new address as active and updates the
136 * bandwidth assignment for this network.
138 * 3.7 Choosing addresses
140 * Choosing the best possible address for suggestion is done by iterating
141 * over all addresses of a peer stored in addresses' hashmap and using the
142 * hashmap iterator find_address_it to select the best available address.
143 * Several checks are done when an address is selected. First if this address
144 * is currently blocked by addresses from being suggested. An address is
145 * blocked for the duration of ATS_BLOCKING_DELTA when it is suggested to
146 * transport. Next it is checked if at least
147 * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available in the
148 * addresse's network, because suggesting an address without bandwidth does
149 * not make sense. This also ensures that all active addresses in this
150 * network get at least the minimum amount of bandwidth assigned. In the next
151 * step the solver ensures that for tcp connections inbound connections are
152 * prefered over outbound connections. In the next stet the solver ensures
153 * that connections are prefered in the following order:
154 * * connections are already established and have bandwidth assigned
155 * * connections with a shorter distance
156 * * connectes have a shorter latency
158 * 3.8 Changing preferences
162 * During shutdown all network entries and aging processes are destroyed and
168 * This solver ssigns in and outbound bandwidth equally for all addresses in
169 * specific network type (WAN, LAN) based on configured in and outbound quota
172 * The solver is notified by addresses about changes to the addresses and
173 * recalculates the bandwith assigned if required. The solver notifies addresses
174 * by calling the GAS_bandwidth_changed_cb callback.
181 * For each peer only a single is selected and marked as "active" in the address
186 * You have the networks WAN and LAN and quotas
187 * WAN_TOTAL_IN, WAN_TOTAL_OUT
188 * LAN_TOTAL_IN, LAN_TOTAL_OUT
190 * If you have x addresses in the network segment LAN, the quotas are
191 * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
193 * Quotas are automatically recalculated and reported back when addresses are
198 #define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
199 #define PREF_AGING_FACTOR 0.95
201 #define DEFAULT_PREFERENCE 1.0
202 #define MIN_UPDATE_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
205 * A handle for the simplistic solver
207 struct GAS_SIMPLISTIC_Handle
213 struct GNUNET_STATISTICS_Handle *stats;
216 * Total number of addresses for solver
218 unsigned int total_addresses;
221 * Number of active addresses for solver
223 unsigned int active_addresses;
228 struct Network *network_entries;
233 unsigned int networks;
238 GAS_bandwidth_changed_cb bw_changed;
243 void *bw_changed_cls;
245 struct GNUNET_CONTAINER_MultiHashMap *prefs;
247 struct PreferenceClient *pc_head;
248 struct PreferenceClient *pc_tail;
259 * Network description
264 * Total inbound quota
267 unsigned long long total_quota_in;
270 * Total outbound quota
273 unsigned long long total_quota_out;
276 * Number of active addresses for this network
278 unsigned int active_addresses;
281 * Number of total addresses for this network
283 unsigned int total_addresses;
286 * String for statistics total addresses
291 * String for statistics active addresses
295 struct AddressWrapper *head;
296 struct AddressWrapper *tail;
299 struct AddressWrapper
301 struct AddressWrapper *next;
302 struct AddressWrapper *prev;
304 struct ATS_Address *addr;
308 struct PreferenceClient
310 struct PreferenceClient *prev;
311 struct PreferenceClient *next;
314 double f_total[GNUNET_ATS_PreferenceCount];
316 struct PreferencePeer *p_head;
317 struct PreferencePeer *p_tail;
321 struct PreferencePeer
323 struct PreferencePeer *next;
324 struct PreferencePeer *prev;
325 struct PreferenceClient *client;
326 struct GAS_SIMPLISTIC_Handle *s;
327 struct GNUNET_PeerIdentity id;
329 double f[GNUNET_ATS_PreferenceCount];
330 double f_rel[GNUNET_ATS_PreferenceCount];
333 GNUNET_SCHEDULER_TaskIdentifier aging_task;
337 * Get the prefered address for a specific peer
339 * @param solver the solver handle
340 * @param addresses the address hashmap containing all addresses
341 * @param peer the identity of the peer
343 const struct ATS_Address *
344 GAS_simplistic_get_preferred_address (void *solver,
345 struct GNUNET_CONTAINER_MultiHashMap * addresses,
346 const struct GNUNET_PeerIdentity *peer);
349 * Init the simplistic problem solving component
352 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
353 * out_quota[i] contains outbound quota for network type i
354 * in_quota[i] contains inbound quota for network type i
357 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
358 * network[2] == GNUNET_ATS_NET_LAN
359 * out_quota[2] == 65353
360 * in_quota[2] == 65353
362 * @param cfg configuration handle
363 * @param stats the GNUNET_STATISTICS handle
364 * @param network array of GNUNET_ATS_NetworkType with length dest_length
365 * @param out_quota array of outbound quotas
366 * @param in_quota array of outbound quota
367 * @param dest_length array length for quota arrays
368 * @param bw_changed_cb callback for changed bandwidth amounts
369 * @param bw_changed_cb_cls cls for callback
370 * @return handle for the solver on success, NULL on fail
373 GAS_simplistic_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
374 const struct GNUNET_STATISTICS_Handle *stats,
376 unsigned long long *out_quota,
377 unsigned long long *in_quota,
379 GAS_bandwidth_changed_cb bw_changed_cb,
380 void *bw_changed_cb_cls)
383 struct GAS_SIMPLISTIC_Handle *s = GNUNET_malloc (sizeof (struct GAS_SIMPLISTIC_Handle));
384 struct Network * cur;
385 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
388 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
389 s->bw_changed = bw_changed_cb;
390 s->bw_changed_cls = bw_changed_cb_cls;
391 s->networks = dest_length;
392 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
393 s->active_addresses = 0;
394 s->total_addresses = 0;
395 s->prefs = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
397 for (c = 0; c < dest_length; c++)
399 cur = &s->network_entries[c];
400 cur->total_addresses = 0;
401 cur->active_addresses = 0;
402 cur->type = network[c];
403 cur->total_quota_in = in_quota[c];
404 cur->total_quota_out = out_quota[c];
405 cur->desc = net_str[c];
406 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
407 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
413 free_pref (void *cls,
414 const struct GNUNET_HashCode * key,
423 * Shutdown the simplistic problem solving component
425 * @param solver the respective handle to shutdown
428 GAS_simplistic_done (void *solver)
430 struct GAS_SIMPLISTIC_Handle *s = solver;
431 struct PreferenceClient *pc;
432 struct PreferenceClient *next_pc;
433 struct PreferencePeer *p;
434 struct PreferencePeer *next_p;
435 struct AddressWrapper *cur;
436 struct AddressWrapper *next;
438 GNUNET_assert (s != NULL);
440 for (c = 0; c < s->networks; c++)
442 if (s->network_entries[c].total_addresses > 0)
444 LOG (GNUNET_ERROR_TYPE_ERROR,
445 "Had %u addresses for network `%s' not deleted during shutdown\n",
446 s->network_entries[c].total_addresses,
447 s->network_entries[c].desc);
451 if (s->network_entries[c].active_addresses > 0)
453 LOG (GNUNET_ERROR_TYPE_ERROR,
454 "Had %u active addresses for network `%s' not deleted during shutdown\n",
455 s->network_entries[c].active_addresses,
456 s->network_entries[c].desc);
460 next = s->network_entries[c].head;
461 while (NULL != (cur = next))
464 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
465 s->network_entries[c].tail,
469 GNUNET_free (s->network_entries[c].stat_total);
470 GNUNET_free (s->network_entries[c].stat_active);
472 if (s->total_addresses > 0)
474 LOG (GNUNET_ERROR_TYPE_ERROR,
475 "Had %u addresses not deleted during shutdown\n",
479 if (s->active_addresses > 0)
481 LOG (GNUNET_ERROR_TYPE_ERROR,
482 "Had %u active addresses not deleted during shutdown\n",
483 s->active_addresses);
486 GNUNET_free (s->network_entries);
488 next_pc = s->pc_head;
489 while (NULL != (pc = next_pc))
492 GNUNET_CONTAINER_DLL_remove (s->pc_head, s->pc_tail, pc);
494 while (NULL != (p = next_p))
497 if (GNUNET_SCHEDULER_NO_TASK != p->aging_task)
499 GNUNET_SCHEDULER_cancel(p->aging_task);
500 p->aging_task = GNUNET_SCHEDULER_NO_TASK;
502 GNUNET_CONTAINER_DLL_remove (pc->p_head, pc->p_tail, p);
508 GNUNET_CONTAINER_multihashmap_iterate (s->prefs, &free_pref, NULL);
509 GNUNET_CONTAINER_multihashmap_destroy (s->prefs);
515 * Test if bandwidth is available in this network
517 * @param s the solver handle
518 * @param net the network type to update
519 * @return GNUNET_YES or GNUNET_NO
523 bw_available_in_network (struct Network *net)
525 unsigned int na = net->active_addresses + 1;
526 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
527 if (((net->total_quota_in / na) > min_bw) &&
528 ((net->total_quota_out / na) > min_bw))
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 "Enough bandwidth available for %u active addresses in network `%s'\n",
537 LOG (GNUNET_ERROR_TYPE_DEBUG,
538 "Not enough bandwidth available for %u active addresses in network `%s'\n",
546 * Update the quotas for a network type
548 * @param s the solver handle
549 * @param net the network type to update
550 * @param address_except address excluded from notifcation, since we suggest
554 update_quota_per_network (struct GAS_SIMPLISTIC_Handle *s,
556 struct ATS_Address *address_except)
558 unsigned long long remaining_quota_in = 0;
559 unsigned long long quota_out_used = 0;
561 unsigned long long remaining_quota_out = 0;
562 unsigned long long quota_in_used = 0;
563 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
564 double total_prefs; /* Important: has to be double not float due to precision */
565 double cur_pref; /* Important: has to be double not float due to precision */
566 double *t = NULL; /* Important: has to be double not float due to precision */
568 unsigned long long assigned_quota_in = 0;
569 unsigned long long assigned_quota_out = 0;
570 struct AddressWrapper *cur;
572 LOG (GNUNET_ERROR_TYPE_DEBUG,
573 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
574 net->desc, net->active_addresses, net->total_quota_in, net->total_quota_in);
576 if (net->active_addresses == 0)
577 return; /* no addresses to update */
580 * Assign every peer in network minimum Bandwidth
581 * Distribute bandwidth left according to preference
584 if ((net->active_addresses * min_bw) > net->total_quota_in)
589 if ((net->active_addresses * min_bw) > net->total_quota_out)
595 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
596 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
597 LOG (GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
598 remaining_quota_in, remaining_quota_out);
600 for (cur = net->head; NULL != cur; cur = cur->next)
602 if (GNUNET_YES == cur->addr->active)
604 t = GNUNET_CONTAINER_multihashmap_get (s->prefs, &cur->addr->peer.hashPubKey);
606 total_prefs += DEFAULT_PREFERENCE;
613 for (cur = net->head; NULL != cur; cur = cur->next)
615 if (GNUNET_YES == cur->addr->active)
618 t = GNUNET_CONTAINER_multihashmap_get (s->prefs, &cur->addr->peer.hashPubKey);
620 cur_pref = DEFAULT_PREFERENCE;
623 assigned_quota_in = min_bw + ((cur_pref / total_prefs) * remaining_quota_in);
624 assigned_quota_out = min_bw + ((cur_pref / total_prefs) * remaining_quota_out);
626 LOG (GNUNET_ERROR_TYPE_DEBUG,
627 "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
628 GNUNET_i2s (&cur->addr->peer),
629 cur_pref, total_prefs,
630 assigned_quota_in, assigned_quota_out);
634 assigned_quota_in = 0;
635 assigned_quota_out = 0;
638 quota_in_used += assigned_quota_in;
639 quota_out_used += assigned_quota_out;
640 /* Prevent overflow due to rounding errors */
641 if (assigned_quota_in > UINT32_MAX)
642 assigned_quota_in = UINT32_MAX;
643 if (assigned_quota_out > UINT32_MAX)
644 assigned_quota_out = UINT32_MAX;
646 /* Compare to current bandwidth assigned */
647 if ((assigned_quota_in != ntohl(cur->addr->assigned_bw_in.value__)) ||
648 (assigned_quota_out != ntohl(cur->addr->assigned_bw_out.value__)))
650 cur->addr->assigned_bw_in.value__ = htonl (assigned_quota_in);
651 cur->addr->assigned_bw_out.value__ = htonl (assigned_quota_out);
652 /* Notify on change */
653 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
654 s->bw_changed (s->bw_changed_cls, cur->addr);
658 LOG (GNUNET_ERROR_TYPE_DEBUG,
659 "Total bandwidth assigned is (in/out): %llu /%llu\n",
662 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
664 LOG (GNUNET_ERROR_TYPE_ERROR,
665 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
666 net->active_addresses,
668 net->total_quota_out);
670 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
672 LOG (GNUNET_ERROR_TYPE_ERROR,
673 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
674 net->active_addresses,
676 net->total_quota_in);
681 update_all_networks (struct GAS_SIMPLISTIC_Handle *s)
684 for (i = 0; i < s->networks; i++)
685 update_quota_per_network (s, &s->network_entries[i], NULL);
690 addresse_increment (struct GAS_SIMPLISTIC_Handle *s,
695 if (GNUNET_YES == total)
697 s->total_addresses ++;
698 net->total_addresses ++;
699 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
700 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
702 if (GNUNET_YES == active)
704 net->active_addresses ++;
705 s->active_addresses ++;
706 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
707 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
713 addresse_decrement (struct GAS_SIMPLISTIC_Handle *s,
719 if (GNUNET_YES == total)
721 if (s->total_addresses < 1)
728 s->total_addresses --;
729 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
731 if (net->total_addresses < 1)
738 net->total_addresses --;
739 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
743 if (GNUNET_YES == active)
745 if (net->active_addresses < 1)
752 net->active_addresses --;
753 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
755 if (s->active_addresses < 1)
762 s->active_addresses --;
763 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
771 * Add a single address to the solve
773 * @param solver the solver Handle
774 * @param addresses the address hashmap containing all addresses
775 * @param address the address to add
778 GAS_simplistic_address_add (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
780 struct GAS_SIMPLISTIC_Handle *s = solver;
781 struct Network *net = NULL;
782 struct AddressWrapper *aw = NULL;
784 GNUNET_assert (NULL != s);
786 for (c = 0; c < s->networks; c++)
788 net = &s->network_entries[c];
789 if (address->atsp_network_type == net->type)
798 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
800 GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
801 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
802 aw->addr->solver_information = net;
805 LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
806 net->total_addresses,
807 net->active_addresses,
812 * Remove an address from the solver
814 * @param solver the solver handle
815 * @param addresses the address hashmap containing all addresses
816 * @param address the address to remove
817 * @param session_only delete only session not whole address
820 GAS_simplistic_address_delete (void *solver,
821 struct GNUNET_CONTAINER_MultiHashMap * addresses,
822 struct ATS_Address *address, int session_only)
824 struct GAS_SIMPLISTIC_Handle *s = solver;
826 struct AddressWrapper *aw;
828 /* Remove an adress completely, we have to:
829 * - Remove from specific network
830 * - Decrease number of total addresses
832 * - decrease number of active addreses
836 net = (struct Network *) address->solver_information;
838 if (GNUNET_NO == session_only)
840 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
841 (GNUNET_NO == address->active) ? "inactive" : "active",
842 address, GNUNET_i2s (&address->peer),
843 net->desc, net->total_addresses, net->active_addresses);
846 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
847 for (aw = net->head; NULL != aw; aw = aw->next)
849 if (aw->addr == address)
857 GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
862 /* Remove session only: remove if active and update */
863 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
864 (GNUNET_NO == address->active) ? "inactive" : "active",
865 address, GNUNET_i2s (&address->peer),
866 net->desc, net->total_addresses, net->active_addresses);
869 if (GNUNET_YES == address->active)
871 /* Address was active, remove from network and update quotas*/
872 address->active = GNUNET_NO;
873 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
875 update_quota_per_network (s, net, NULL);
877 LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
878 net->total_addresses,
879 net->active_addresses,
884 static struct Network *
885 find_network (struct GAS_SIMPLISTIC_Handle *s, uint32_t type)
888 for (c = 0 ; c < s->networks; c++)
890 if (s->network_entries[c].type == type)
891 return &s->network_entries[c];
897 * Updates a single address in the solve
899 * @param solver the solver Handle
900 * @param addresses the address hashmap containing all addresses
901 * @param address the update address
902 * @param session the new session (if changed otherwise current)
903 * @param in_use the new address in use state (if changed otherwise current)
904 * @param atsi the latest ATS information
905 * @param atsi_count the atsi count
908 GAS_simplistic_address_update (void *solver,
909 struct GNUNET_CONTAINER_MultiHashMap *addresses,
910 struct ATS_Address *address,
913 const struct GNUNET_ATS_Information *atsi,
916 struct ATS_Address *new;
917 struct GAS_SIMPLISTIC_Handle *s = (struct GAS_SIMPLISTIC_Handle *) solver;
921 int save_active = GNUNET_NO;
922 struct Network *new_net = NULL;
923 for (i = 0; i < atsi_count; i++)
925 type = ntohl (atsi[i].type);
926 value = ntohl (atsi[i].value);
929 case GNUNET_ATS_UTILIZATION_UP:
930 //if (address->atsp_utilization_out.value__ != atsi[i].value)
933 case GNUNET_ATS_UTILIZATION_DOWN:
934 //if (address->atsp_utilization_in.value__ != atsi[i].value)
937 case GNUNET_ATS_QUALITY_NET_DELAY:
938 //if (address->atsp_latency.rel_value != value)
941 case GNUNET_ATS_QUALITY_NET_DISTANCE:
942 //if (address->atsp_distance != value)
945 case GNUNET_ATS_COST_WAN:
946 //if (address->atsp_cost_wan != value)
949 case GNUNET_ATS_COST_LAN:
950 //if (address->atsp_cost_lan != value)
953 case GNUNET_ATS_COST_WLAN:
954 //if (address->atsp_cost_wlan != value)
957 case GNUNET_ATS_NETWORK_TYPE:
958 if (address->atsp_network_type != value)
961 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
962 (GNUNET_YES == address->active) ? "active" : "inactive",
963 GNUNET_ATS_print_network_type(address->atsp_network_type),
964 GNUNET_ATS_print_network_type(value));
966 save_active = address->active;
967 /* remove from old network */
968 GAS_simplistic_address_delete (solver, addresses, address, GNUNET_NO);
970 /* set new network type */
971 address->atsp_network_type = value;
972 new_net = find_network (solver, value);
973 address->solver_information = new_net;
974 if (address->solver_information == NULL)
977 address->atsp_network_type = GNUNET_ATS_NET_UNSPECIFIED;
981 /* Add to new network and update*/
982 GAS_simplistic_address_add (solver, addresses, address);
983 if (GNUNET_YES == save_active)
985 /* check if bandwidth available in new network */
986 if (GNUNET_YES == (bw_available_in_network (new_net)))
988 /* Suggest updated address */
989 address->active = GNUNET_YES;
990 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
991 update_quota_per_network (solver, new_net, NULL);
995 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not enough bandwidth in new network, suggesting alternative address ..\n");
997 /* Set old address to zero bw */
998 address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
999 address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
1000 s->bw_changed (s->bw_changed_cls, address);
1002 /* Find new address to suggest since no bandwidth in network*/
1003 new = (struct ATS_Address *) GAS_simplistic_get_preferred_address (s, addresses, &address->peer);
1006 /* Have an alternative address to suggest */
1007 s->bw_changed (s->bw_changed_cls, new);
1014 case GNUNET_ATS_ARRAY_TERMINATOR:
1017 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1018 "Received unsupported ATS type %u\n", type);
1025 if (address->session_id != session)
1027 LOG (GNUNET_ERROR_TYPE_DEBUG,
1028 "Session changed from %u to %u\n", address->session_id, session);
1029 address->session_id = session;
1031 if (address->used != in_use)
1033 LOG (GNUNET_ERROR_TYPE_DEBUG,
1034 "Usage changed from %u to %u\n", address->used, in_use);
1035 address->used = in_use;
1043 * Find a "good" address to use for a peer. If we already have an existing
1044 * address, we stick to it. Otherwise, we pick by lowest distance and then
1045 * by lowest latency.
1047 * @param cls the 'struct ATS_Address**' where we store the result
1049 * @param value another 'struct ATS_Address*' to consider using
1050 * @return GNUNET_OK (continue to iterate)
1053 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1055 struct ATS_Address **previous_p = cls;
1056 struct ATS_Address *current = (struct ATS_Address *) value;
1057 struct ATS_Address *previous = *previous_p;
1058 struct GNUNET_TIME_Absolute now;
1059 struct Network *net = (struct Network *) current->solver_information;
1061 now = GNUNET_TIME_absolute_get();
1063 if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
1065 /* This address is blocked for suggestion */
1066 LOG (GNUNET_ERROR_TYPE_DEBUG,
1067 "Address %p blocked for suggestion for %llu ms \n",
1069 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
1073 if (GNUNET_NO == bw_available_in_network (net))
1074 return GNUNET_OK; /* There's no bandwidth available in this network */
1076 if (NULL != previous)
1078 if ((0 == strcmp (previous->plugin, "tcp")) &&
1079 (0 == strcmp (current->plugin, "tcp")))
1081 if ((0 != previous->addr_len) &&
1082 (0 == current->addr_len))
1084 /* saved address was an outbound address, but we have an inbound address */
1085 *previous_p = current;
1088 if (0 == previous->addr_len)
1090 /* saved address was an inbound address, so do not overwrite */
1096 if (NULL == previous)
1098 *previous_p = current;
1101 if ((ntohl (previous->assigned_bw_in.value__) == 0) &&
1102 (ntohl (current->assigned_bw_in.value__) > 0))
1104 /* stick to existing connection */
1105 *previous_p = current;
1108 if (previous->atsp_distance > current->atsp_distance)
1110 /* user shorter distance */
1111 *previous_p = current;
1114 if (previous->atsp_latency.rel_value > current->atsp_latency.rel_value)
1116 /* user lower latency */
1117 *previous_p = current;
1125 find_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1127 struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
1128 struct ATS_Address * aa = (struct ATS_Address *) value;
1130 if (GNUNET_YES == aa->active)
1134 /* should never happen */
1135 LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
1144 static struct ATS_Address *
1145 find_active_address (void *solver,
1146 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1147 const struct GNUNET_PeerIdentity *peer)
1149 struct ATS_Address * dest = NULL;
1151 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
1153 &find_active_address_it, &dest);
1158 * Get the prefered address for a specific peer
1160 * @param solver the solver handle
1161 * @param addresses the address hashmap containing all addresses
1162 * @param peer the identity of the peer
1164 const struct ATS_Address *
1165 GAS_simplistic_get_preferred_address (void *solver,
1166 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1167 const struct GNUNET_PeerIdentity *peer)
1169 struct GAS_SIMPLISTIC_Handle *s = solver;
1170 struct Network *net_prev;
1171 struct Network *net_cur;
1172 struct ATS_Address *cur;
1173 struct ATS_Address *prev;
1175 GNUNET_assert (s != NULL);
1177 /* Get address with: stick to current address, lower distance, lower latency */
1178 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
1179 &find_address_it, &cur);
1182 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
1186 LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
1187 (GNUNET_NO == cur->active) ? "inactive" : "active",
1188 cur, GNUNET_i2s (peer));
1189 net_cur = (struct Network *) cur->solver_information;
1190 if (GNUNET_YES == cur->active)
1192 /* This address was selected previously, so no need to update quotas */
1196 /* This address was not active, so we have to:
1198 * - mark previous active address as not active
1199 * - update quota for previous address network
1200 * - update quota for this address network
1203 prev = find_active_address (s, addresses, peer);
1206 net_prev = (struct Network *) prev->solver_information;
1207 prev->active = GNUNET_NO; /* No active any longer */
1208 prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
1209 prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
1210 s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
1211 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
1213 update_quota_per_network (s, net_prev, NULL);
1216 if (GNUNET_NO == (bw_available_in_network (cur->solver_information)))
1218 GNUNET_break (0); /* This should never happen*/
1222 cur->active = GNUNET_YES;
1223 addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
1224 update_quota_per_network (s, net_cur, cur);
1231 * Stop notifying about address and bandwidth changes for this peer
1233 * @param solver the MLP handle
1234 * @param addresses address hashmap
1235 * @param peer the peer
1238 GAS_simplistic_stop_get_preferred_address (void *solver,
1239 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1240 const struct GNUNET_PeerIdentity *peer)
1246 recalculate_preferences (struct PreferencePeer *p)
1248 struct GAS_SIMPLISTIC_Handle *s = p->s;
1249 struct PreferencePeer *p_cur;
1250 struct PreferenceClient *c_cur = p->client;
1251 double p_rel_global;
1262 * Set of peers p_i in P
1263 * Set of preference kinds k
1264 * A preference value f_k_p_i with an unknown range
1267 * A client specific relative preference f_p_i_rel [1..2] for all peers
1269 * For every client c
1271 * For every preference kind k:
1273 * We remember for the preference f_p_i for each peer p_i.
1274 * We have a default preference value f_p_i = 0
1275 * We have a sum of all preferences f_t = sum (f_p_i)
1276 * So we can calculate a relative preference value fr_p_i:
1279 * f_k_p_i_rel = [1..2], default 1.0
1281 * f_p_i_rel = sum (f_k_p_i_rel) / #k
1286 /* For this client: for all preferences, except TERMINATOR */
1287 for (kind = GNUNET_ATS_PREFERENCE_END + 1 ; kind < GNUNET_ATS_PreferenceCount; kind ++)
1289 /* Recalcalculate total preference for this quality kind over all peers*/
1290 c_cur->f_total[kind] = 0;
1291 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
1292 c_cur->f_total[kind] += p_cur->f[kind];
1294 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p has total preference for %s of %.3f\n",
1296 GNUNET_ATS_print_preference_type (kind),
1297 c_cur->f_total[kind]);
1299 /* Recalcalculate relative preference for all peers */
1300 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
1302 /* Calculate relative preference for specific kind */
1303 if (0.0 == c_cur->f_total[kind])
1305 /* No one has preference, so set default preference */
1306 p_cur->f_rel[kind] = DEFAULT_PREFERENCE;
1310 p_cur->f_rel[kind] = (c_cur->f_total[kind] + p_cur->f[kind]) / c_cur->f_total[kind];
1312 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has relative preference for %s of %.3f\n",
1314 GNUNET_i2s (&p_cur->id),
1315 GNUNET_ATS_print_preference_type (kind),
1316 p_cur->f_rel[kind]);
1318 /* Calculate peer relative preference */
1319 /* Start with kind = 1 to exclude terminator */
1320 p_cur->f_rel_total = 0;
1321 for (rkind = GNUNET_ATS_PREFERENCE_END + 1; rkind < GNUNET_ATS_PreferenceCount; rkind ++)
1323 p_cur->f_rel_total += p_cur->f_rel[rkind];
1325 p_cur->f_rel_total /= (GNUNET_ATS_PreferenceCount - 1.0); /* -1 due to terminator */
1326 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has total relative preference of %.3f\n",
1328 GNUNET_i2s (&p_cur->id),
1329 p_cur->f_rel_total);
1333 /* Calculcate global total relative peer preference over all clients */
1336 for (c_cur = s->pc_head; NULL != c_cur; c_cur = c_cur->next)
1338 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
1339 if (0 == memcmp (&p_cur->id, &p->id, sizeof (p_cur->id)))
1344 p_rel_global += p_cur->f_rel_total;
1347 p_rel_global /= clients;
1348 LOG (GNUNET_ERROR_TYPE_DEBUG, "Global preference value for peer `%s': %.3f\n",
1349 GNUNET_i2s (&p->id), p_rel_global);
1351 /* Update global map */
1352 if (NULL != (dest = GNUNET_CONTAINER_multihashmap_get(s->prefs, &p->id.hashPubKey)))
1353 (*dest) = p_rel_global;
1356 dest = GNUNET_malloc (sizeof (double));
1357 (*dest) = p_rel_global;
1358 GNUNET_CONTAINER_multihashmap_put(s->prefs,
1361 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1366 update_preference (struct PreferencePeer *p,
1367 enum GNUNET_ATS_PreferenceKind kind,
1370 double score = score_f;
1372 /* Update preference value according to type */
1374 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
1375 case GNUNET_ATS_PREFERENCE_LATENCY:
1376 p->f[kind] = (p->f[kind] + score) / 2;
1378 case GNUNET_ATS_PREFERENCE_END:
1383 recalculate_preferences(p);
1384 update_all_networks (p->s);
1389 preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1394 struct PreferencePeer *p = cls;
1395 GNUNET_assert (NULL != p);
1398 p->aging_task = GNUNET_SCHEDULER_NO_TASK;
1400 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aging preferences for peer `%s'\n",
1401 GNUNET_i2s (&p->id));
1403 /* Issue for aging :
1405 * Not for every peer preference values are set by default, so reducing the
1406 * absolute preference value does not help for aging because it does not have
1407 * influence on the relative values.
1409 * So we have to reduce the relative value to have an immediate impact on
1410 * quota calculation. In addition we cannot call recalculate_preferences here
1411 * but instead reduce the absolute value to have an aging impact on future
1412 * calls to change_preference where recalculate_preferences is called
1415 /* Aging absolute values: */
1416 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
1421 p->f[i] *= PREF_AGING_FACTOR;
1422 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aged preference for peer `%s' from %.3f to %.3f\n",
1423 GNUNET_i2s (&p->id), backup, p->f[i]);
1426 /* Updating relative value */
1427 t = GNUNET_CONTAINER_multihashmap_get (p->s->prefs, &p->id.hashPubKey);
1435 (*t) = (*t) * PREF_AGING_FACTOR;
1438 update_all_networks (p->s);
1440 p->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
1441 &preference_aging, p);
1446 * Changes the preferences for a peer in the problem
1448 * @param solver the solver handle
1449 * @param client the client with this preference
1450 * @param peer the peer to change the preference for
1451 * @param kind the kind to change the preference
1452 * @param score the score
1455 GAS_simplistic_address_change_preference (void *solver,
1457 const struct GNUNET_PeerIdentity *peer,
1458 enum GNUNET_ATS_PreferenceKind kind,
1461 static struct GNUNET_TIME_Absolute next_update;
1462 struct GAS_SIMPLISTIC_Handle *s = solver;
1463 struct PreferenceClient *c_cur;
1464 struct PreferencePeer *p_cur;
1467 GNUNET_assert (NULL != solver);
1468 GNUNET_assert (NULL != client);
1469 GNUNET_assert (NULL != peer);
1471 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p changes preference for peer `%s' %s %f\n",
1474 GNUNET_ATS_print_preference_type (kind),
1477 if (kind >= GNUNET_ATS_PreferenceCount)
1483 /* Find preference client */
1484 for (c_cur = s->pc_head; NULL != c_cur; c_cur = c_cur->next)
1486 if (client == c_cur->client)
1489 /* Not found: create new preference client */
1492 c_cur = GNUNET_malloc (sizeof (struct PreferenceClient));
1493 c_cur->client = client;
1494 GNUNET_CONTAINER_DLL_insert (s->pc_head, s->pc_tail, c_cur);
1497 /* Find entry for peer */
1498 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
1499 if (0 == memcmp (&p_cur->id, peer, sizeof (p_cur->id)))
1502 /* Not found: create new peer entry */
1505 p_cur = GNUNET_malloc (sizeof (struct PreferencePeer));
1507 p_cur->client = c_cur;
1508 p_cur->id = (*peer);
1509 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
1511 /* Default value per peer absolut preference for a quality:
1512 * No value set, so absolute preference 0 */
1514 /* Default value per peer relative preference for a quality: 1.0 */
1515 p_cur->f_rel[i] = DEFAULT_PREFERENCE;
1517 p_cur->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL, &preference_aging, p_cur);
1518 GNUNET_CONTAINER_DLL_insert (c_cur->p_head, c_cur->p_tail, p_cur);
1521 update_preference (p_cur, kind, score_f);
1523 /* FIXME: We should update quotas if UPDATE_INTERVAL is reached */
1524 if (GNUNET_TIME_absolute_get().abs_value > next_update.abs_value)
1527 next_update = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
1528 MIN_UPDATE_INTERVAL);
1532 /* end of gnunet-service-ats_addresses_simplistic.c */