2 This file is part of GNUnet.
3 (C) 2011-2015 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file ats/gnunet-service-ats_addresses.c
23 * @brief ats service address management
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_ats_service.h"
29 #include "gnunet-service-ats.h"
30 #include "gnunet-service-ats_addresses.h"
31 #include "gnunet-service-ats_normalization.h"
32 #include "gnunet-service-ats_performance.h"
33 #include "gnunet-service-ats_plugins.h"
34 #include "gnunet-service-ats_scheduling.h"
35 #include "gnunet-service-ats_reservations.h"
39 * NOTE: Do not change this documentation. This documentation is based on
40 * gnunet.org:/vcs/fsnsg/2014-p2p-ats.git/tech-doku/ats-tech-guide.tex
41 * use build_txt.sh to generate plaintext output
43 * 1 ATS addresses : ATS address management
45 * This ATS addresses ("addresses") component manages the addresses known to
46 * ATS service and suggests addresses to transport service when it is
47 * interested in address suggestion for a peer. ATS addresses also
48 * instantiates the bandwidth assignment mechanism (solver), notifies it
49 * about changes to addresses and forwards changes to bandwidth assignments
50 * to transport, depending if transport is interested in this change.
56 * Addresses are added by specifying peer ID, plugin, address, address length
57 * and session, if available. ATS information can be specified if available.
61 * ATS specifies a fix set of networks an address can belong to. For each
62 * network an inbound and outbound quota will be specified. The available
63 * networks and addtional helper varaibles are defined in
64 * gnunet_ats_service.h. At the moment 5 networks are defined:
65 * * GNUNET_ATS_NET_UNSPECIFIED
66 * * GNUNET_ATS_NET_LOOPBACK
67 * * GNUNET_ATS_NET_LAN
68 * * GNUNET_ATS_NET_WAN
69 * * GNUNET_ATS_NET_WLAN
71 * The total number of networks defined is stored in
72 * GNUNET_ATS_NetworkTypeCount GNUNET_ATS_NetworkType can be used array
73 * initializer for an int array, while GNUNET_ATS_NetworkType is an
74 * initializer for a char array containing a string description of all
79 * An inbound and outbound quota for each of the networks mentioned in 1.1.2
80 * is loaded from ats configuration during initialization. This quota defines
81 * to total amount of inbound and outbound traffic allowed for a specific
82 * network. The configuration values used are in section ats:
83 * * "NETWORK"_QUOTA_IN = <value>
84 * * "NETWORK"_QUOTA_IN = <value>
86 * You can specify quotas by setting the <value> to a:
87 * * unrestricted: unlimited
88 * * number of bytes: e.g. 10240
89 * * fancy value: e.g. 64 Kib
91 * unlimited is defined as GNUNET_ATS_MaxBandwidthString and equivalent to
92 * the value GNUNET_ATS_MaxBandwidth Important predefined values for quotas
94 * * GNUNET_ATS_DefaultBandwidth: 65536
95 * * GNUNET_ATS_MaxBandwidth: UINT32_MAX
96 * * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT: 1024
98 * Details of loading quotas and default values will be described on
100 * 1.1.4 Preference values
102 * 1.2 Data structures used
104 * Addresse uses struct ATS_Address for each address. The structs are stored
105 * in a linked list and provides a pointer void *solver_information for the
106 * solver to store address specific information. It provides the int values
107 * active which is set to GNUNET_YES if the address is select for transport
108 * use and used, representing that transport service is actively using this
109 * address. Address information are stored in peer, addr, addr_len, plugin.
113 * During initialization a hashmap to store addresses is created. The quotas
114 * for all networks defined for ATS are loaded from configuration. For each
115 * network first the logic will check if the string
116 * GNUNET_ATS_MaxBandwidthString is configured, if not it will try to convert
117 * the configured value as a fancy size and if this fails it will try to use
118 * it as a value_number. If no configuration value is found it will assign
119 * GNUNET_ATS_DefaultBandwidth. The most important step is to load the
120 * configured solver using configuration "[ats]:MODE". Current solvers are
121 * MODE_PROPORTIONAL, MODE_MLP. Interaction is done using a solver API
126 * * s_init: init the solver with required information
127 * * s_add: add a new address
128 * * s_update: update ATS values or session for an address
129 * * s_get: get prefered address for a peer
130 * * s_del: delete an address
131 * * s_pref: change preference value for a peer
132 * * s_done: shutdown solver
134 * Callbacks: addresses provides a bandwidth_changed_cb callback to the
135 * solver which is called when bandwidth assigned to peer has changed
139 * During shutdown all addresses are freed and the solver told to shutdown
141 * 1.6 Addresses and sessions
143 * Addresses consist of the address itself and a numerical session. When a
144 * new address without a session is added it has no session, so it gets
145 * session 0 assigned. When an address with a session is added and an address
146 * object with session 0 is found, this object is updated with the session
147 * otherwise a new address object with this session assigned is created.
151 * Addresses a1,a2 with session s1, s2 are "exact" if:
152 * (a1 == a2)&&(s1 == s2)
153 * Addresses a1,a2 with session s1, s2 are "equivalent" if:
154 * (a1 == a2)&&((s1 == s2)||(s1 == 0)||(s2 == 0)
156 * 1.7 Address management
158 * Transport service notifies ATS about changes to the addresses known to
161 * 1.7.1 Adding an address
163 * When transport learns a new address it tells ATS and ATS is telling
164 * addresses about it using GAS_address_add. If not known to addresses it
165 * creates a new address object and calls solver's s_add. ATS information are
166 * deserialized and solver is notified about the session and ATS information
169 * 1.7.2 Updating an address
171 * Addresses does an lookup up for the existing address with the given
172 * session. If disassembles included ATS information and notifies the solver
173 * using s_update about the update.
175 * 1.7.3 Deleting an address
177 * Addresses does an lookup for the exact address and session and if removes
178 * this address. If session != 0 the session is set to 0 and the address is
179 * kept. If session == 0, the addresses is removed.
181 * 1.7.4 Requesting an address suggestion
183 * The address client issues a request address message to be notified about
184 * address suggestions for a specific peer. Addresses asks the solver with
185 * s_get. If no address is available, it will not send a response, otherwise
186 * it will respond with the choosen address.
188 * 1.7.5 Address suggestions
190 * Addresses will notify the client automatically on any bandwidth_changed_cb
191 * by the solver if a address suggestion request is pending. If no address is
192 * available it will not respond at all If the client is not interested
193 * anymore, it has to cancel the address suggestion request.
195 * 1.7.6 Address lifecycle
198 * * (updated address)
201 * 1.8 Bandwidth assignment
203 * The addresses are used to perform resource allocation operations. ATS
204 * addresses takes care of instantiating the solver configured and notifies
205 * the respective solver about address changes and receives changes to the
206 * bandwidth assignment from the solver. The current bandwidth assignment is
207 * sent to transport. The specific solvers will be described in the specific
210 * 1.9 Changing peer preferences
212 * The bandwidth assigned to a peer can be influenced by setting a preference
213 * for a peer. The prefernce will be given to to the solver with s_pref which
214 * has to take care of the preference value
219 * A multihashmap to store all addresses
221 struct GNUNET_CONTAINER_MultiPeerMap *GSA_addresses;
225 * Update statistic on number of addresses.
228 update_addresses_stat ()
230 GNUNET_STATISTICS_set (GSA_stats,
232 GNUNET_CONTAINER_multipeermap_size (GSA_addresses),
238 * Disassemble ATS information and update performance information in address
240 * Updates existing information and adds new information
242 * @param dest destination address
243 * @param update source ATS information
244 * @param update_count number of ATS information in @a update
245 * @param delta_dest ats performance information which were updated
246 * including previous value
247 * @param delta_count number of ATS information in the @a delta_dest
248 * @return #GNUNET_YES if address was address updated, GNUNET_NO otherwise
251 disassemble_ats_information (struct ATS_Address *dest,
252 const struct GNUNET_ATS_Information *update,
253 uint32_t update_count,
254 struct GNUNET_ATS_Information **delta_dest,
255 uint32_t *delta_count)
261 struct GNUNET_ATS_Information add_atsi[update_count];
262 struct GNUNET_ATS_Information delta_atsi[update_count];
263 struct GNUNET_ATS_Information *tmp_atsi;
264 uint32_t add_atsi_count;
265 uint32_t delta_atsi_count;
269 delta_atsi_count = 0;
271 if (0 == update_count)
274 if (NULL == dest->atsi)
276 /* Create performance information */
278 GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
279 dest->atsi_count = update_count;
282 update_count * sizeof(struct GNUNET_ATS_Information));
284 GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
285 for (c1 = 0; c1 < update_count; c1++)
287 (*delta_dest)[c1].type = update[c1].type;
288 (*delta_dest)[c1].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
290 (*delta_count) = update_count;
294 for (c1 = 0; c1 < update_count; c1++)
296 /* Update existing performance information */
298 for (c2 = 0; c2 < dest->atsi_count; c2++)
300 if (update[c1].type == dest->atsi[c2].type)
302 if (update[c1].value != dest->atsi[c2].value)
304 /* Save previous value in delta */
305 delta_atsi[delta_atsi_count] = dest->atsi[c2];
308 dest->atsi[c2].value = update[c1].value;
315 if (GNUNET_NO == found)
317 add_atsi[add_atsi_count] = update[c1];
319 delta_atsi[delta_atsi_count].type = update[c1].type;
320 delta_atsi[delta_atsi_count].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
325 if (add_atsi_count > 0)
327 /* Extend ats performance information */
329 tmp_atsi = GNUNET_malloc ((dest->atsi_count + add_atsi_count) *
330 (sizeof (struct GNUNET_ATS_Information)));
331 memcpy (tmp_atsi, dest->atsi,
332 dest->atsi_count * sizeof(struct GNUNET_ATS_Information));
333 memcpy (&tmp_atsi[dest->atsi_count], add_atsi,
334 add_atsi_count * sizeof(struct GNUNET_ATS_Information));
335 GNUNET_free (dest->atsi);
336 dest->atsi = tmp_atsi;
337 dest->atsi_count = dest->atsi_count + add_atsi_count;
341 if (delta_atsi_count > 0)
345 GNUNET_malloc (delta_atsi_count * sizeof (struct GNUNET_ATS_Information));
346 memcpy ((*delta_dest), delta_atsi,
347 delta_atsi_count * sizeof(struct GNUNET_ATS_Information));
348 (*delta_count) = delta_atsi_count;
356 * Free the given address
358 * @param addr address to destroy
361 free_address (struct ATS_Address *addr)
363 GNUNET_CONTAINER_multipeermap_remove (GSA_addresses,
366 update_addresses_stat ();
367 GAS_plugin_delete_address (addr);
368 GAS_performance_notify_all_clients (&addr->peer,
374 GNUNET_BANDWIDTH_ZERO,
375 GNUNET_BANDWIDTH_ZERO);
376 GNUNET_free (addr->plugin);
377 GNUNET_free_non_null (addr->atsi);
383 * Create a ATS_address with the given information
386 * @param plugin_name plugin
387 * @param plugin_addr address
388 * @param plugin_addr_len address length
389 * @param local_address_info additional local info for the address
390 * @param session_id session identifier, can never be 0
391 * @return the ATS_Address
393 static struct ATS_Address *
394 create_address (const struct GNUNET_PeerIdentity *peer,
395 const char *plugin_name,
396 const void *plugin_addr,
397 size_t plugin_addr_len,
398 uint32_t local_address_info,
401 struct ATS_Address *aa;
405 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
407 aa->addr_len = plugin_addr_len;
412 aa->plugin = GNUNET_strdup (plugin_name);
413 aa->session_id = session_id;
414 aa->local_address_info = local_address_info;
416 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
418 aa->atsin[c1].avg_queue_index = 0;
419 for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
420 aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
427 * Closure for #find_address_cb()
429 struct FindAddressContext
432 * Session Id to look for.
437 * Where to store matching address result.
439 struct ATS_Address *exact_address;
445 * Find session matching given session ID.
447 * @param cls a `struct FindAddressContext`
449 * @param value the address to compare with
450 * @return #GNUNET_YES to continue, #GNUNET_NO if address is found
453 find_address_cb (void *cls,
454 const struct GNUNET_PeerIdentity *key,
457 struct FindAddressContext *fac = cls;
458 struct ATS_Address *aa = value;
460 if (aa->session_id == fac->session_id)
462 fac->exact_address = aa;
470 * Find the exact address
473 * @param session_id session id, can never be 0
474 * @return an ATS_address or NULL
476 static struct ATS_Address *
477 find_exact_address (const struct GNUNET_PeerIdentity *peer,
480 struct FindAddressContext fac;
482 fac.exact_address = NULL;
483 fac.session_id = session_id;
484 GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
486 &find_address_cb, &fac);
487 return fac.exact_address;
492 * Extract an ATS performance info from an address
494 * @param address the address
495 * @param type the type to extract in HBO
496 * @return the value in HBO or #GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
499 get_performance_info (struct ATS_Address *address, uint32_t type)
502 GNUNET_assert(NULL != address);
504 if ((NULL == address->atsi) || (0 == address->atsi_count))
505 return GNUNET_ATS_VALUE_UNDEFINED;
507 for (c1 = 0; c1 < address->atsi_count; c1++)
509 if (ntohl (address->atsi[c1].type) == type)
510 return ntohl (address->atsi[c1].value);
512 return GNUNET_ATS_VALUE_UNDEFINED;
517 * Add a new address for a peer.
520 * @param plugin_name transport plugin name
521 * @param plugin_addr plugin address
522 * @param plugin_addr_len length of the plugin address in @a plugin_addr
523 * @param local_address_info the local address for the address
524 * @param session_id session id, can be 0
525 * @param atsi performance information for this address
526 * @param atsi_count number of performance information contained in @a atsi
529 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
530 const char *plugin_name,
531 const void *plugin_addr,
532 size_t plugin_addr_len,
533 uint32_t local_address_info,
535 const struct GNUNET_ATS_Information *atsi,
538 struct ATS_Address *new_address;
539 struct GNUNET_ATS_Information *atsi_delta;
540 uint32_t atsi_delta_count;
543 if (NULL != find_exact_address (peer, session_id))
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550 "Received `%s' for peer `%s'\n",
553 new_address = create_address (peer,
560 disassemble_ats_information (new_address,
564 GNUNET_free_non_null (atsi_delta);
565 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
566 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
567 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
569 /* Add a new address */
570 new_address->t_added = GNUNET_TIME_absolute_get();
571 new_address->t_last_activity = GNUNET_TIME_absolute_get();
572 GNUNET_assert(GNUNET_OK ==
573 GNUNET_CONTAINER_multipeermap_put (GSA_addresses,
576 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
577 update_addresses_stat ();
578 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
579 "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
584 GNUNET_ATS_print_network_type (addr_net));
586 /* Tell solver about new address */
587 GAS_plugin_new_address (new_address,
591 /* Notify performance clients about new address */
592 GAS_performance_notify_all_clients (&new_address->peer,
595 new_address->addr_len,
598 new_address->atsi_count,
599 GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_out),
600 GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_in));
605 * Update an address with new performance information for a peer.
608 * @param session_id session id, never 0
609 * @param atsi performance information for this address
610 * @param atsi_count number of performance information contained in @a atsi
613 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
615 const struct GNUNET_ATS_Information *atsi,
618 struct ATS_Address *aa;
619 struct GNUNET_ATS_Information *atsi_delta;
620 uint32_t atsi_delta_count;
622 /* Get existing address */
623 aa = find_exact_address (peer,
630 if (NULL == aa->solver_information)
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Received `%s' for peer `%s' address \n",
642 aa->t_last_activity = GNUNET_TIME_absolute_get();
644 atsi_delta_count = 0;
646 disassemble_ats_information (aa, atsi,
651 /* Notify performance clients about updated address */
652 GAS_performance_notify_all_clients (&aa->peer,
659 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
660 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
662 GAS_plugin_update_address (aa,
666 GNUNET_free_non_null (atsi_delta);
671 * Remove an address or just a session for a peer.
674 * @param session_id session id, can never be 0
677 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
680 struct ATS_Address *ea;
682 /* Get existing address */
683 ea = find_exact_address (peer,
690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
691 "Received ADDRESS_DESTROYED for peer `%s' address %p session %u\n",
700 * Initialize address subsystem. The addresses subsystem manages the addresses
701 * known and current performance information. It has a solver component
702 * responsible for the resource allocation. It tells the solver about changes
703 * and receives updates when the solver changes the resource allocation.
706 GAS_addresses_init ()
708 GSA_addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
709 update_addresses_stat ();
714 * Destroy all addresses iterator
717 * @param key peer identity (unused)
718 * @param value the 'struct ATS_Address' to free
719 * @return #GNUNET_OK (continue to iterate)
722 destroy_all_address_it (void *cls,
723 const struct GNUNET_PeerIdentity *key,
726 struct ATS_Address *aa = value;
734 * Remove all addresses
737 GAS_addresses_destroy_all ()
739 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
740 "Destroying all addresses\n");
741 GAS_plugin_solver_lock ();
742 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
743 &destroy_all_address_it,
745 GAS_plugin_solver_unlock ();
750 * Shutdown address subsystem.
753 GAS_addresses_done ()
755 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
756 "Shutting down addresses\n");
757 GAS_addresses_destroy_all ();
758 GNUNET_CONTAINER_multipeermap_destroy (GSA_addresses);
759 GSA_addresses = NULL;
764 * Closure for #peerinfo_it().
766 struct PeerInfoIteratorContext
769 * Function to call for each address.
771 GNUNET_ATS_PeerInfo_Iterator it;
781 * Iterator to iterate over a peer's addresses
783 * @param cls a `struct PeerInfoIteratorContext`
784 * @param key the peer id
785 * @param value the `struct ATS_address`
786 * @return #GNUNET_OK to continue
789 peerinfo_it (void *cls,
790 const struct GNUNET_PeerIdentity *key,
793 struct PeerInfoIteratorContext *pi_ctx = cls;
794 struct ATS_Address *addr = value;
796 pi_ctx->it (pi_ctx->it_cls,
802 addr->atsi, addr->atsi_count,
803 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_out),
804 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_in));
810 * Return information all peers currently known to ATS
812 * @param peer the respective peer, NULL for 'all' peers
813 * @param pi_it the iterator to call for every peer
814 * @param pi_it_cls the closure for @a pi_it
817 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer,
818 GNUNET_ATS_PeerInfo_Iterator pi_it,
821 struct PeerInfoIteratorContext pi_ctx;
825 /* does not make sense without callback */
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
830 "Returning information for %s from a total of %u known addresses\n",
834 (unsigned int) GNUNET_CONTAINER_multipeermap_size (GSA_addresses));
836 pi_ctx.it_cls = pi_it_cls;
838 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
842 GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
844 &peerinfo_it, &pi_ctx);
849 GNUNET_BANDWIDTH_ZERO,
850 GNUNET_BANDWIDTH_ZERO);
853 /* end of gnunet-service-ats_addresses.c */