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.c
23 * @brief ats service address management
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_ats_service.h"
29 #include "gnunet_ats_plugin.h"
30 #include "gnunet-service-ats.h"
31 #include "gnunet-service-ats_addresses.h"
32 #include "gnunet-service-ats_normalization.h"
33 #include "gnunet-service-ats_performance.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/ats-paper.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 Suggestions blocks and reset
197 * After suggesting an address it is blocked for ATS_BLOCKING_DELTA sec. to
198 * prevent the client from being thrashed. If the client requires immediately
199 * it can reset this block using GAS_addresses_handle_backoff_reset.
201 * 1.7.7 Marking address in use
203 * The client can notify addresses that it successfully uses an address and
204 * wants this address to be kept by calling GSA_address_in_use. Adresses will
205 * mark the address as used an notify the solver about the use.
207 * 1.7.8 Address lifecycle
210 * * (updated address) || (address in use)
213 * 1.8 Bandwidth assignment
215 * The addresses are used to perform resource allocation operations. ATS
216 * addresses takes care of instantiating the solver configured and notifies
217 * the respective solver about address changes and receives changes to the
218 * bandwidth assignment from the solver. The current bandwidth assignment is
219 * sent to transport. The specific solvers will be described in the specific
222 * 1.9 Changing peer preferences
224 * The bandwidth assigned to a peer can be influenced by setting a preference
225 * for a peer. The prefernce will be given to to the solver with s_pref which
226 * has to take care of the preference value
232 * Pending Address suggestion requests
234 struct GAS_Addresses_Suggestion_Requests
239 struct GAS_Addresses_Suggestion_Requests *next;
244 struct GAS_Addresses_Suggestion_Requests *prev;
249 struct GNUNET_PeerIdentity id;
253 * Pending Address suggestion requests
255 struct GAS_Addresses_Preference_Clients
260 struct GAS_Addresses_Preference_Clients *next;
265 struct GAS_Addresses_Preference_Clients *prev;
274 * Handle for ATS address component
276 struct GAS_Addresses_Handle
281 struct GNUNET_STATISTICS_Handle *stat;
284 * A multihashmap to store all addresses
286 struct GNUNET_CONTAINER_MultiPeerMap *addresses;
289 * Is ATS addresses running
294 * Preferences clients
299 * Configured ATS solver
309 * Address suggestion requests DLL head
311 struct GAS_Addresses_Suggestion_Requests *pending_requests_head;
314 * Address suggestion requests DLL tail
316 struct GAS_Addresses_Suggestion_Requests *pending_requests_tail;
319 * Address suggestion requests DLL head
321 struct GAS_Addresses_Preference_Clients *preference_clients_head;
324 * Address suggestion requests DLL head
326 struct GAS_Addresses_Preference_Clients *preference_clients_tail;
331 struct GNUNET_ATS_PluginEnvironment env;
334 * Solver plugin name as string
340 * Disassemble ATS information and update performance information in address
342 * Updates existing information and adds new information
344 * @param dest destination address
345 * @param update source ATS information
346 * @param update_count number of ATS information
347 * @param delta_dest ats performance information which were updated
348 * including previous value
349 * @param delta_count number of ATS information in the delta
350 * @return GNUNET_YES if address was address updated, GNUNET_NO otherwise
353 disassemble_ats_information (struct ATS_Address *dest,
354 const struct GNUNET_ATS_Information *update, uint32_t update_count,
355 struct GNUNET_ATS_Information **delta_dest, uint32_t *delta_count)
363 struct GNUNET_ATS_Information add_atsi[update_count];
364 struct GNUNET_ATS_Information delta_atsi[update_count];
365 struct GNUNET_ATS_Information *tmp_atsi;
366 uint32_t add_atsi_count;
367 uint32_t delta_atsi_count;
371 delta_atsi_count = 0;
373 if (0 == update_count)
376 if (NULL == dest->atsi)
378 /* Create performance information */
380 GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
381 dest->atsi_count = update_count;
382 memcpy (dest->atsi, update,
383 update_count * sizeof(struct GNUNET_ATS_Information));
385 GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
386 for (c1 = 0; c1 < update_count; c1++)
388 (*delta_dest)[c1].type = update[c1].type;
389 (*delta_dest)[c1].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
391 (*delta_count) = update_count;
395 for (c1 = 0; c1 < update_count; c1++)
397 /* Update existing performance information */
399 for (c2 = 0; c2 < dest->atsi_count; c2++)
401 if (update[c1].type == dest->atsi[c2].type)
403 if (update[c1].value != dest->atsi[c2].value)
405 /* Save previous value in delta */
406 delta_atsi[delta_atsi_count] = dest->atsi[c2];
409 dest->atsi[c2].value = update[c1].value;
416 if (GNUNET_NO == found)
418 add_atsi[add_atsi_count] = update[c1];
420 delta_atsi[delta_atsi_count].type = update[c1].type;
421 delta_atsi[delta_atsi_count].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
426 if (add_atsi_count > 0)
428 /* Extend ats performance information */
430 tmp_atsi = GNUNET_malloc ((dest->atsi_count + add_atsi_count) *
431 (sizeof (struct GNUNET_ATS_Information)));
432 memcpy (tmp_atsi, dest->atsi,
433 dest->atsi_count * sizeof(struct GNUNET_ATS_Information));
434 memcpy (&tmp_atsi[dest->atsi_count], add_atsi,
435 add_atsi_count * sizeof(struct GNUNET_ATS_Information));
436 GNUNET_free(dest->atsi);
437 dest->atsi = tmp_atsi;
438 dest->atsi_count = dest->atsi_count + add_atsi_count;
442 if (delta_atsi_count > 0)
446 GNUNET_malloc (delta_atsi_count * sizeof (struct GNUNET_ATS_Information));
447 memcpy ((*delta_dest), delta_atsi,
448 delta_atsi_count * sizeof(struct GNUNET_ATS_Information));
449 (*delta_count) = delta_atsi_count;
456 * Free the given address
458 * @param addr address to destroy
461 free_address (struct ATS_Address *addr)
463 GNUNET_free(addr->plugin);
464 GNUNET_free_non_null(addr->atsi);
469 * Create a ATS_address with the given information
472 * @param plugin_name plugin
473 * @param plugin_addr address
474 * @param plugin_addr_len address length
475 * @param session_id session
476 * @return the ATS_Address
478 static struct ATS_Address *
479 create_address (const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
480 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
482 struct ATS_Address *aa = NULL;
486 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
488 aa->addr_len = plugin_addr_len;
490 memcpy (&aa[1], plugin_addr, plugin_addr_len);
491 aa->plugin = GNUNET_strdup (plugin_name);
492 aa->session_id = session_id;
493 aa->active = GNUNET_NO;
494 aa->used = GNUNET_NO;
495 aa->solver_information = NULL;
498 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
499 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
501 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
503 aa->atsin[c1].avg_queue_index = 0;
504 for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
505 aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
511 struct CompareAddressContext
513 const struct ATS_Address *search;
515 /* exact_address != NULL if address and session is equal */
516 struct ATS_Address *exact_address;
517 /* exact_address != NULL if address and session is 0 */
518 struct ATS_Address *base_address;
525 * @param cls a CompareAddressContext containin the source address
527 * @param value the address to compare with
528 * @return #GNUNET_YES to continue, #GNUNET_NO if address is founce
531 compare_address_it (void *cls,
532 const struct GNUNET_PeerIdentity *key,
535 struct CompareAddressContext *cac = cls;
536 struct ATS_Address *aa = value;
538 /* Find an matching exact address:
541 * aa->addr_len == cac->search->addr_len
542 * aa->plugin == cac->search->plugin
543 * aa->addr == cac->search->addr
544 * aa->session == cac->search->session
546 * return as exact address
548 if ((aa->addr_len == cac->search->addr_len)
549 && (0 == strcmp (aa->plugin, cac->search->plugin)))
551 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
552 && (aa->session_id == cac->search->session_id))
553 cac->exact_address = aa;
556 /* Find an matching base address:
560 * aa->session_id == 0
563 * aa->addr_len == cac->search->addr_len
564 * aa->plugin == cac->search->plugin
565 * aa->addr == cac->search->addr
567 * return as base address
569 if ((aa->addr_len == cac->search->addr_len)
570 && (0 == strcmp (aa->plugin, cac->search->plugin)))
572 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
573 && (aa->session_id == 0))
574 cac->base_address = aa;
577 /* Find an matching exact address based on session:
581 * cac->search->addr_len == 0
584 * aa->plugin == cac->search->plugin
585 * aa->session_id == cac->search->session_id
587 * return as exact address
589 if (0 == cac->search->addr_len)
591 if ((0 == strcmp (aa->plugin, cac->search->plugin))
592 && (aa->session_id == cac->search->session_id))
593 cac->exact_address = aa;
596 if (cac->exact_address == NULL )
597 return GNUNET_YES; /* Continue iteration to find exact address */
599 return GNUNET_NO; /* Stop iteration since we have an exact address */
603 * Find an existing equivalent address record.
604 * Compares by peer identity and network address OR by session ID
605 * (one of the two must match).
607 * @param handle the address handle
608 * @param peer peer to lookup addresses for
609 * @param addr existing address record
610 * @return existing address record, NULL for none
613 find_equivalent_address (struct GAS_Addresses_Handle *handle,
614 const struct GNUNET_PeerIdentity *peer, const struct ATS_Address *addr)
616 struct CompareAddressContext cac;
618 cac.exact_address = NULL;
619 cac.base_address = NULL;
621 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
623 &compare_address_it, &cac);
625 if (cac.exact_address == NULL)
626 return cac.base_address;
627 return cac.exact_address;
632 * Find the exact address
634 * @param handle the address handle to use
636 * @param plugin_name transport plugin name
637 * @param plugin_addr plugin address
638 * @param plugin_addr_len length of the plugin address
639 * @param session_id session id, can be 0
640 * @return an ATS_address or NULL
643 static struct ATS_Address *
644 find_exact_address (struct GAS_Addresses_Handle *handle,
645 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
646 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
648 struct ATS_Address *aa;
649 struct ATS_Address *ea;
651 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
654 /* Get existing address or address with session == 0 */
655 ea = find_equivalent_address (handle, peer, aa);
659 else if (ea->session_id != session_id)
665 * Function allowing the solver to obtain normalized preference
669 * @param id the peer to return the normalized properties for
670 * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
673 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
675 return GAS_normalization_get_preferences_by_peer (id);
679 * Function allowing the solver to obtain normalized property
680 * values for an address from solver
683 * @param address the address
684 * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
687 get_property_cb (void *cls, const struct ATS_Address *address)
689 return GAS_normalization_get_properties ((struct ATS_Address *) address);
693 * Extract an ATS performance info from an address
695 * @param address the address
696 * @param type the type to extract in HBO
697 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
700 get_performance_info (struct ATS_Address *address, uint32_t type)
703 GNUNET_assert(NULL != address);
705 if ((NULL == address->atsi) || (0 == address->atsi_count))
706 return GNUNET_ATS_VALUE_UNDEFINED;
708 for (c1 = 0; c1 < address->atsi_count; c1++)
710 if (ntohl (address->atsi[c1].type) == type)
711 return ntohl (address->atsi[c1].value);
713 return GNUNET_ATS_VALUE_UNDEFINED;
717 * Add a new address for a peer.
719 * @param handle the address handle to use
721 * @param plugin_name transport plugin name
722 * @param plugin_addr plugin address
723 * @param plugin_addr_len length of the plugin address
724 * @param session_id session id, can be 0
725 * @param atsi performance information for this address
726 * @param atsi_count number of performance information contained
729 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
730 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
731 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
732 const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
734 struct ATS_Address *new_address;
735 struct ATS_Address *existing_address;
736 struct GNUNET_ATS_Information *atsi_delta;
737 uint32_t atsi_delta_count;
739 uint32_t previous_session;
742 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
743 "ADDRESS ADD", GNUNET_i2s (peer));
745 if (GNUNET_NO == handle->running)
748 GNUNET_assert(NULL != handle->addresses);
750 new_address = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
753 disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
755 GNUNET_free_non_null(atsi_delta);
756 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
757 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
758 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
760 /* Get existing address or address with session == 0 */
761 existing_address = find_equivalent_address (handle, peer, new_address);
762 if (existing_address == NULL )
764 /* Add a new address */
766 GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (handle->addresses,
769 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
771 GNUNET_STATISTICS_set (handle->stat, "# addresses",
772 GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
774 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
775 "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
778 plugin_addr_len, session_id,
779 GNUNET_ATS_print_network_type (addr_net));
781 /* Tell solver about new address */
782 handle->env.sf.s_add (handle->solver, new_address, addr_net);
784 handle->env.sf.s_bulk_start (handle->solver);
785 GAS_normalization_normalize_property (handle->addresses, new_address, atsi,
787 handle->env.sf.s_bulk_stop (handle->solver);
789 /* Notify performance clients about new address */
790 GAS_performance_notify_all_clients (&new_address->peer, new_address->plugin,
791 new_address->addr, new_address->addr_len, new_address->session_id,
792 new_address->atsi, new_address->atsi_count,
793 new_address->assigned_bw_out, new_address->assigned_bw_in);
797 /* We have an existing address we can use, clean up new */
798 GNUNET_free(new_address->plugin);
799 GNUNET_free_non_null(new_address->atsi);
800 GNUNET_free(new_address);
803 if (0 != existing_address->session_id)
805 /* Should not happen */
810 addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
811 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
812 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
814 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
815 "Found existing address for peer `%s' %p with new session %u in network %s\n",
816 GNUNET_i2s (peer), existing_address, session_id,
817 GNUNET_ATS_print_network_type (addr_net));
818 /* We have an address without an session, update this address */
820 atsi_delta_count = 0;
822 == disassemble_ats_information (existing_address, atsi, atsi_count,
823 &atsi_delta, &atsi_delta_count))
825 /* Notify performance clients about properties */
826 GAS_performance_notify_all_clients (&existing_address->peer,
827 existing_address->plugin, existing_address->addr,
828 existing_address->addr_len, existing_address->session_id,
829 existing_address->atsi, existing_address->atsi_count,
830 existing_address->assigned_bw_out, existing_address->assigned_bw_in);
832 for (c1 = 0; c1 < atsi_delta_count; c1++)
834 if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
835 && (addr_net != ntohl (atsi_delta[c1].value)))
837 /* Network type changed */
838 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
839 "Address for peer `%s' %p changed from network %s to %s\n",
840 GNUNET_i2s (peer), existing_address,
841 GNUNET_ATS_print_network_type (addr_net),
842 GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
843 handle->env.sf.s_address_update_network (handle->solver, existing_address,
844 ntohl (atsi_delta[c1].value),
845 get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
846 addr_net = get_performance_info (existing_address,
847 GNUNET_ATS_NETWORK_TYPE);
850 /* Notify solver about update with atsi information and session */
851 handle->env.sf.s_bulk_start (handle->solver);
852 GAS_normalization_normalize_property (handle->addresses, existing_address,
854 handle->env.sf.s_bulk_stop (handle->solver);
856 GNUNET_free_non_null(atsi_delta);
858 /* Notify solver about new session */
859 if (existing_address->session_id == session_id)
860 return; /* possible, can both be 0 since address is revalidated */
862 previous_session = existing_address->session_id;
863 existing_address->session_id = session_id;
864 handle->env.sf.s_address_update_session (handle->solver, existing_address,
865 previous_session, session_id);
867 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
868 "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
869 GNUNET_i2s (peer), existing_address, existing_address->addr_len,
870 session_id, GNUNET_ATS_print_network_type (addr_net));
874 * Update an address with a session or performance information for a peer.
876 * If an address was added without a session it will be updated with the
879 * @param handle the address handle to use
881 * @param plugin_name transport plugin name
882 * @param plugin_addr plugin address
883 * @param plugin_addr_len length of the plugin address
884 * @param session_id session id, can be 0
885 * @param atsi performance information for this address
886 * @param atsi_count number of performance information contained
889 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
890 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
891 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
892 const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
894 struct ATS_Address *aa;
895 struct GNUNET_ATS_Information *atsi_delta;
896 uint32_t atsi_delta_count;
897 uint32_t prev_session;
900 if (GNUNET_NO == handle->running)
903 GNUNET_assert(NULL != handle->addresses);
905 /* Get existing address */
906 aa = find_exact_address (handle, peer, plugin_name, plugin_addr,
907 plugin_addr_len, session_id);
910 if (NULL == aa->solver_information)
913 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s' address \n",
914 "ADDRESS UPDATE", GNUNET_i2s (peer), aa);
917 if (session_id != aa->session_id)
919 /* Session changed */
920 prev_session = aa->session_id;
921 aa->session_id = session_id;
922 handle->env.sf.s_address_update_session (handle->solver, aa, prev_session,
927 atsi_delta_count = 0;
929 == disassemble_ats_information (aa, atsi, atsi_count, &atsi_delta,
932 /* ATS properties changed */
933 for (c1 = 0; c1 < atsi_delta_count; c1++)
935 if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
937 /* Network type changed */
938 handle->env.sf.s_address_update_network (handle->solver, aa,
939 ntohl (atsi_delta[c1].value),
940 get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
944 /* Notify performance clients about updated address */
945 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr,
946 aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
947 aa->assigned_bw_out, aa->assigned_bw_in);
949 handle->env.sf.s_bulk_start (handle->solver);
950 GAS_normalization_normalize_property (handle->addresses, aa, atsi,
952 handle->env.sf.s_bulk_stop (handle->solver);
954 GNUNET_free_non_null(atsi_delta);
957 struct DestroyContext
959 struct ATS_Address *aa;
961 struct GAS_Addresses_Handle *handle;
964 * GNUNET_NO : full address
965 * GNUNET_YES : just session
973 * If session != 0, just the session is deleted, the address itself still exists
974 * If session == 0, remove full address
975 * If session == 0 and addrlen == 0, destroy inbound address
979 * @param value the 'struct ATS_Address'
980 * @return GNUNET_OK (continue to iterate)
983 destroy_by_session_id (void *cls,
984 const struct GNUNET_PeerIdentity *key,
987 struct DestroyContext *dc = cls;
988 struct GAS_Addresses_Handle *handle = dc->handle;
989 const struct ATS_Address *des = dc->aa;
990 struct ATS_Address *aa = value;
993 0 == memcmp (&aa->peer, &des->peer, sizeof(struct GNUNET_PeerIdentity)));
995 if (des->session_id == 0)
997 /* Session == 0, remove full address */
998 if ((0 == strcmp (des->plugin, aa->plugin))
999 && (aa->addr_len == des->addr_len)
1000 && (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
1003 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1004 "Deleting full address for peer `%s' session %u %p\n",
1005 GNUNET_i2s (&aa->peer), aa->session_id, aa);
1007 /* Notify solver about deletion */
1009 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1012 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1014 dc->result = GNUNET_NO;
1015 return GNUNET_OK; /* Continue iteration */
1020 /* Session != 0, just remove session */
1021 if (aa->session_id != des->session_id)
1022 return GNUNET_OK; /* irrelevant */
1024 if ((aa->session_id != 0) && (0 != strcmp (des->plugin, aa->plugin)))
1026 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1027 "Different plugins during removal: `%s' vs `%s' \n", des->plugin,
1033 if (aa->addr_len == 0)
1035 /* Inbound connection died, delete full address */
1036 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1037 "Deleting inbound address for peer `%s': `%s' session %u\n",
1038 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
1040 /* Notify solver about deletion */
1042 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1044 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1046 dc->result = GNUNET_NO;
1047 return GNUNET_OK; /* Continue iteration */
1052 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1053 "Deleting session for peer `%s': `%s' %u\n", GNUNET_i2s (&aa->peer),
1054 aa->plugin, aa->session_id);
1055 /* Notify solver to delete session */
1056 handle->env.sf.s_del (handle->solver, aa, GNUNET_YES);
1058 aa->active = GNUNET_NO;
1067 * Remove an address or just a session for a peer.
1069 * @param handle the address handle to use
1071 * @param plugin_name transport plugin name
1072 * @param plugin_addr plugin address
1073 * @param plugin_addr_len length of the plugin address
1074 * @param session_id session id, can be 0
1077 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
1078 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1079 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
1081 struct ATS_Address *ea;
1082 struct DestroyContext dc;
1083 if (GNUNET_NO == handle->running)
1086 /* Get existing address */
1087 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1088 plugin_addr_len, session_id);
1091 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1092 "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
1093 GNUNET_i2s (peer), plugin_name, session_id);
1097 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1098 "Received `%s' for peer `%s' address %p session %u\n", "ADDRESS DESTROY",
1099 GNUNET_i2s (peer), ea, session_id);
1101 GNUNET_break(0 < strlen (plugin_name));
1103 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
1106 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1108 &destroy_by_session_id, &dc);
1109 GNUNET_STATISTICS_set (handle->stat, "# addresses",
1110 GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
1111 free_address (dc.aa);
1115 * Notification about active use of an address.
1116 * in_use == GNUNET_YES:
1117 * This address is used to maintain an active connection with a peer.
1118 * in_use == GNUNET_NO:
1119 * This address is no longer used to maintain an active connection with a peer.
1121 * Note: can only be called with in_use == GNUNET_NO if called with GNUNET_YES
1124 * @param handle the address handle to use
1126 * @param plugin_name transport plugin name
1127 * @param plugin_addr plugin address
1128 * @param plugin_addr_len length of the plugin address
1129 * @param session_id session id, can be 0
1130 * @param in_use GNUNET_YES if GNUNET_NO
1131 * @return GNUNET_SYSERR on failure (address unknown ...)
1134 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
1135 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1136 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
1139 struct ATS_Address *ea;
1140 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1141 "ADDRESS IN USE", GNUNET_i2s (peer));
1143 if (GNUNET_NO == handle->running)
1144 return GNUNET_SYSERR;
1146 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1147 plugin_addr_len, session_id);
1150 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1151 "Trying to set unknown address `%s' `%s' `%u' to %s \n",
1152 GNUNET_i2s (peer), plugin_name, session_id,
1153 (GNUNET_NO == in_use) ? "NO" : "YES");
1155 return GNUNET_SYSERR;
1157 if (ea->used == in_use)
1160 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1161 "Address in use called multiple times for peer `%s': %s -> %s \n",
1162 GNUNET_i2s (peer), (GNUNET_NO == ea->used) ? "NO" : "YES",
1163 (GNUNET_NO == in_use) ? "NO" : "YES");
1164 return GNUNET_SYSERR;
1167 /* Tell solver about update */
1169 handle->env.sf.s_address_update_inuse (handle->solver, ea, ea->used);
1174 * Cancel address suggestions for a peer
1176 * @param handle the address handle
1177 * @param peer the peer id
1180 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
1181 const struct GNUNET_PeerIdentity *peer)
1183 struct GAS_Addresses_Suggestion_Requests *cur = handle->pending_requests_head;
1185 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received request: `%s' for peer %s\n",
1186 "request_address_cancel", GNUNET_i2s (peer));
1190 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1197 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1198 "No address requests pending for peer `%s', cannot remove!\n",
1202 handle->env.sf.s_get_stop (handle->solver, peer);
1203 GAS_addresses_handle_backoff_reset (handle, peer);
1204 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Removed request pending for peer `%s\n",
1206 GNUNET_CONTAINER_DLL_remove(handle->pending_requests_head, handle->pending_requests_tail, cur);
1212 * Request address suggestions for a peer
1214 * @param handle the address handle
1215 * @param peer the peer id
1218 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
1219 const struct GNUNET_PeerIdentity *peer)
1221 struct GAS_Addresses_Suggestion_Requests *cur = handle->pending_requests_head;
1222 struct ATS_Address *aa;
1224 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1225 "REQUEST ADDRESS", GNUNET_i2s (peer));
1227 if (GNUNET_NO == handle->running)
1231 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1232 break; /* already suggesting */
1237 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
1239 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1240 "Adding new address suggestion request for `%s'\n",
1242 GNUNET_CONTAINER_DLL_insert(handle->pending_requests_head, handle->pending_requests_tail, cur);
1245 /* Get prefered address from solver */
1246 aa = (struct ATS_Address *) handle->env.sf.s_get (handle->solver, peer);
1249 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1254 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1255 aa, GNUNET_i2s (peer));
1257 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
1258 aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
1259 aa->assigned_bw_out, aa->assigned_bw_in);
1261 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1262 ATS_BLOCKING_DELTA);
1263 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1264 aa->block_interval);
1266 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1267 "Address %p ready for suggestion, block interval now %llu \n", aa,
1268 aa->block_interval);
1272 * Iterator to reset address blocking
1274 * @param cls not used
1275 * @param key the peer
1276 * @param value the address to reset
1277 * @return #GNUNET_OK to continue
1280 reset_address_it (void *cls,
1281 const struct GNUNET_PeerIdentity *key,
1284 struct ATS_Address *aa = value;
1286 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1287 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1288 GNUNET_i2s (&aa->peer),
1290 aa->block_interval);
1291 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1292 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1298 * Reset suggestion backoff for a peer
1300 * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1301 * reset using this function
1303 * @param handle the address handle
1304 * @param peer the peer id
1307 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1308 const struct GNUNET_PeerIdentity *peer)
1310 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1311 "RESET BACKOFF", GNUNET_i2s (peer));
1314 GNUNET_SYSERR != GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1316 &reset_address_it, NULL));
1322 eval_count_active_it (void *cls, const struct GNUNET_PeerIdentity *id, void *obj)
1324 int *request_fulfilled = cls;
1325 struct ATS_Address *addr = obj;
1327 if (GNUNET_YES == addr->active)
1328 (*request_fulfilled) = GNUNET_YES;
1330 if (*request_fulfilled == GNUNET_YES)
1340 struct SummaryContext {
1342 * Sum of the utilized inbound bandwidth per network
1344 unsigned long long bandwidth_in_assigned[GNUNET_ATS_NetworkTypeCount];
1347 * Sum of the utilized outbound bandwidth per network
1349 unsigned long long bandwidth_out_assigned[GNUNET_ATS_NetworkTypeCount];
1352 * Sum addresses within a network
1354 unsigned int addresses_in_network[GNUNET_ATS_NetworkTypeCount];
1359 eval_sum_bw_used (void *cls, const struct GNUNET_PeerIdentity *id, void *obj)
1361 struct ATS_Address *addr = obj;
1362 int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1364 struct SummaryContext *ctx = cls;
1368 if (GNUNET_YES == addr->active)
1370 net = get_performance_info (addr, GNUNET_ATS_NETWORK_TYPE);
1371 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1373 if (net == networks[c])
1375 ctx->addresses_in_network[c] ++;
1376 ctx->bandwidth_in_assigned[c] += ntohl (addr->assigned_bw_in.value__);
1377 ctx->bandwidth_out_assigned[c] += ntohl (addr->assigned_bw_out.value__);
1380 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Active address in %s with (in/out) %llu/%llu Bps\n",
1381 GNUNET_ATS_print_network_type(net),
1382 ntohl (addr->assigned_bw_in.value__),
1383 ntohl (addr->assigned_bw_out.value__));
1391 struct RelativityContext {
1393 struct GAS_Addresses_Handle *ah;
1397 find_active_address (void *cls, const struct GNUNET_PeerIdentity *id, void *obj)
1399 struct ATS_Address **res = cls;
1400 struct ATS_Address *addr = obj;
1402 if (GNUNET_YES == addr->active)
1412 * Evaluate current bandwidth assignment
1414 * @param ah address handle
1417 GAS_addresses_evaluate_assignment (struct GAS_Addresses_Handle *ah)
1419 struct GAS_Addresses_Suggestion_Requests *cur;
1420 struct GAS_Addresses_Preference_Clients *pcur;
1423 float quality_requests_fulfilled = 0.0;
1424 float quality_bandwidth_utilization[GNUNET_ATS_NetworkTypeCount];
1425 float quality_bandwidth_utilization_total = 0.0;
1426 float quality_application_requirements = 0.0;
1429 int include_requests;
1430 int include_utilization;
1431 int include_requirements;
1433 /* Variable related to requests */
1434 unsigned int requests_pending;
1435 unsigned int requests_fulfilled;
1436 unsigned int request_active;
1438 /* Variable related to utilization */
1439 struct SummaryContext sum;
1440 struct ATS_Address *active_address;
1443 /* Variables for preferences */
1444 int prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceType;
1447 const double *norm_values;
1448 double prefs_fulfill[GNUNET_ATS_PreferenceCount];
1449 int prefs_clients[GNUNET_ATS_PreferenceCount];
1452 GNUNET_assert (NULL != ah);
1453 GNUNET_assert (NULL != ah->addresses);
1455 requests_pending = 0;
1456 requests_fulfilled = 0;
1457 /* 1) How many requests could be fulfilled? */
1458 for (cur = ah->pending_requests_head; NULL != cur; cur = cur->next)
1460 request_active = GNUNET_NO;
1461 GNUNET_CONTAINER_multipeermap_get_multiple (ah->addresses,
1462 &cur->id, &eval_count_active_it, &request_active);
1463 if (GNUNET_YES == request_active)
1464 requests_fulfilled ++;
1465 requests_pending ++;
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': %u pending requests, %s\n",
1467 GNUNET_i2s (&cur->id),
1469 (GNUNET_YES == request_active) ? "active adress" : "no active address");
1472 if (requests_pending > 0)
1474 quality_requests_fulfilled = (float) requests_fulfilled / requests_pending;
1475 include_requests = GNUNET_YES;
1479 quality_requests_fulfilled = 0.0;
1480 include_requests = GNUNET_NO;
1482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u pending requests, %u requests fullfilled\n",
1483 requests_pending, requests_fulfilled);
1485 /* 2) How well is bandwidth utilized? */
1487 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1489 quality_bandwidth_utilization[c] = 0.0;
1490 sum.addresses_in_network[c] = 0;
1491 sum.bandwidth_in_assigned[c] = 0;
1492 sum.bandwidth_out_assigned[c] = 0;
1494 GNUNET_CONTAINER_multipeermap_iterate(ah->addresses,
1495 &eval_sum_bw_used, &sum);
1496 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1498 quality_bandwidth_utilization[c] = (((float)sum.bandwidth_out_assigned[c] / ah->env.out_quota[c]) +
1499 ((float)sum.bandwidth_in_assigned[c] / ah->env.in_quota[c])) / 2;
1501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Utilization for network `%s': %f\n",
1502 GNUNET_ATS_print_network_type(ah->env.networks[c]),
1503 quality_bandwidth_utilization[c]);
1504 if (sum.addresses_in_network[c] > 0)
1506 quality_bandwidth_utilization_total += quality_bandwidth_utilization[c];
1510 if (0 < network_count)
1512 quality_bandwidth_utilization_total /= network_count;
1513 include_utilization = GNUNET_YES;
1517 quality_bandwidth_utilization_total = 0.0;
1518 include_utilization = GNUNET_NO;
1521 /* 3) How well does selection match application requirements */
1522 if (0 == ah->pref_clients)
1524 include_requirements = 0;
1528 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1530 prefs_fulfill[c] = 0.0;
1531 prefs_clients[c] = 0;
1534 for (cur = ah->pending_requests_head; NULL != cur; cur = cur->next)
1536 active_address = NULL;
1537 GNUNET_CONTAINER_multipeermap_get_multiple (ah->addresses,
1538 &cur->id, &find_active_address, &active_address);
1540 for (pcur = ah->preference_clients_head; NULL != pcur; pcur = pcur->next)
1542 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1544 if (prefs[c] == GNUNET_ATS_PREFERENCE_END)
1546 pref_val = GAS_normalization_get_preferences_by_client (pcur->client, &cur->id, prefs[c]);
1547 if (-1.0 == pref_val)
1553 if (DEFAULT_REL_PREFERENCE == pref_val)
1555 /* Default preference value */
1559 if (NULL != active_address)
1561 norm_values = GAS_normalization_get_properties (active_address);
1562 prop_val = norm_values[c];
1563 if ((norm_values[c] <= 1.0) || (norm_values[c] >= 2.0))
1564 prop_val = DEFAULT_REL_QUALITY;
1568 prop_val = DEFAULT_REL_QUALITY;
1571 /* We now have preference values [1..2] and properties [1..2] */
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u Client %p, Peer %s Property %s: pref: %.3f prop %.3f \n",
1576 GNUNET_i2s (&cur->id),
1577 GNUNET_ATS_print_preference_type(prefs[c]),
1581 prefs_fulfill[c] += (pref_val * prop_val) / 2;
1582 prefs_clients[c] ++;
1587 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1589 if (0 < prefs_clients[c])
1591 prefs_fulfill[c] /= prefs_clients[c];
1593 quality_application_requirements += prefs_fulfill[c];
1597 quality_application_requirements /= rels;
1599 quality_application_requirements = 0.0;
1601 include_requirements = 1;
1605 if (include_requests + include_utilization + include_requirements > 0)
1606 guq = (quality_requests_fulfilled + quality_bandwidth_utilization_total + quality_application_requirements) /
1607 (include_requests + include_utilization + include_requirements);
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1612 "Requests fulfilled %.3f bandwidth utilized %.3f application preferences met %.3f => %.3f\n",
1613 quality_requests_fulfilled,
1614 quality_bandwidth_utilization_total,
1615 quality_application_requirements,
1620 * Solver information callback
1622 * @param cls the closure
1623 * @param op the operation
1624 * @param stat operation status
1625 * @param add additional information
1629 solver_info_cb (void *cls,
1630 enum GAS_Solver_Operation op,
1631 enum GAS_Solver_Status stat,
1632 enum GAS_Solver_Additional_Information add)
1638 add_info = "GAS_INFO_NONE";
1641 add_info = "GAS_INFO_MLP_FULL";
1643 case GAS_INFO_UPDATED:
1644 add_info = "GAS_INFO_MLP_UPDATED";
1646 case GAS_INFO_PROP_ALL:
1647 add_info = "GAS_INFO_PROP_ALL";
1649 case GAS_INFO_PROP_SINGLE:
1650 add_info = "GAS_INFO_PROP_SINGLE";
1653 add_info = "INVALID";
1658 case GAS_OP_SOLVE_START:
1659 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1660 "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
1661 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
1663 case GAS_OP_SOLVE_STOP:
1664 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1665 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
1666 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
1669 case GAS_OP_SOLVE_SETUP_START:
1670 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1671 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
1672 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1675 case GAS_OP_SOLVE_SETUP_STOP:
1676 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1677 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
1678 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1681 case GAS_OP_SOLVE_MLP_LP_START:
1682 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1683 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
1684 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1686 case GAS_OP_SOLVE_MLP_LP_STOP:
1687 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1688 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
1689 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1692 case GAS_OP_SOLVE_MLP_MLP_START:
1693 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1694 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
1695 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1697 case GAS_OP_SOLVE_MLP_MLP_STOP:
1698 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1699 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
1700 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1702 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
1703 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1704 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
1705 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1707 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
1708 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1709 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
1710 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1711 GAS_addresses_evaluate_assignment (cls);
1720 * The preference changed for a peer
1722 * @param cls the address handle
1723 * @param peer the peer
1724 * @param kind the ATS kind
1725 * @param pref_rel the new relative preference value
1728 normalized_preference_changed_cb (void *cls,
1729 const struct GNUNET_PeerIdentity *peer, enum GNUNET_ATS_PreferenceKind kind,
1732 GNUNET_assert(NULL != cls);
1733 struct GAS_Addresses_Handle *handle = cls;
1735 /* Tell solver about update */
1736 handle->env.sf.s_pref (handle->solver, peer, kind, pref_rel);
1740 * The relative value for a property changed
1742 * @param cls the address handle
1743 * @param address the peer
1744 * @param type the ATS type
1745 * @param prop_rel the new relative preference value
1748 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
1749 uint32_t type, double prop_rel)
1751 struct GAS_Addresses_Handle *ah = (struct GAS_Addresses_Handle *) cls;
1752 GNUNET_assert(NULL != ah);
1754 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1755 "Normalized property %s for peer `%s' changed to %.3f \n",
1756 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1759 ah->env.sf.s_address_update_property (ah->solver, address, type, 0, prop_rel);
1762 static struct GAS_Addresses_Preference_Clients *
1763 find_preference_client (struct GAS_Addresses_Handle *handle, void *client)
1765 struct GAS_Addresses_Preference_Clients *cur;
1767 for (cur = handle->preference_clients_head; NULL != cur; cur = cur->next)
1769 if (cur->client == client)
1776 * A performance client disconnected
1778 * @param handle address handle
1779 * @param client the client
1783 GAS_addresses_preference_client_disconnect (struct GAS_Addresses_Handle *handle,
1786 struct GAS_Addresses_Preference_Clients * pc;
1787 if (NULL != (pc = find_preference_client (handle, client)))
1789 GNUNET_CONTAINER_DLL_remove (handle->preference_clients_head,
1790 handle->preference_clients_tail, pc);
1792 GNUNET_assert (handle->pref_clients > 0);
1793 handle->pref_clients --;
1794 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
1796 GAS_normalization_preference_client_disconnect (client);
1800 * Change the preference for a peer
1802 * @param handle the address handle
1803 * @param client the client sending this request
1804 * @param peer the peer id
1805 * @param kind the preference kind to change
1806 * @param score_abs the new preference score
1809 GAS_addresses_preference_change (struct GAS_Addresses_Handle *handle,
1810 void *client, const struct GNUNET_PeerIdentity *peer,
1811 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1813 struct GAS_Addresses_Preference_Clients * pc;
1814 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1815 "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1816 GNUNET_i2s (peer), client);
1818 if (GNUNET_NO == handle->running)
1822 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1825 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1826 "Received `%s' for unknown peer `%s' from client %p\n",
1827 "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1831 if (NULL == find_preference_client (handle, client))
1833 pc = GNUNET_malloc (sizeof (struct GAS_Addresses_Preference_Clients));
1834 pc->client = client;
1835 GNUNET_CONTAINER_DLL_insert (handle->preference_clients_head,
1836 handle->preference_clients_tail, pc);
1837 handle->pref_clients ++;
1838 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
1841 handle->env.sf.s_bulk_start (handle->solver);
1842 /* Tell normalization about change, normalization will call callback if preference changed */
1843 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1844 handle->env.sf.s_bulk_stop (handle->solver);
1848 * Change the preference for a peer
1850 * @param handle the address handle
1851 * @param application the client sending this request
1852 * @param peer the peer id
1853 * @param scope the time interval for this feedback: [now - scope .. now]
1854 * @param kind the preference kind to change
1855 * @param score_abs the new preference score
1858 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1859 void *application, const struct GNUNET_PeerIdentity *peer,
1860 const struct GNUNET_TIME_Relative scope,
1861 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1863 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1864 "Received `%s' for peer `%s' for client %p\n", "PREFERENCE FEEDBACK",
1865 GNUNET_i2s (peer), application);
1867 if (GNUNET_NO == handle->running)
1871 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1874 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1875 "Received `%s' for unknown peer `%s' from client %p\n",
1876 "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1880 handle->env.sf.s_feedback (handle->solver, application, peer, scope, kind,
1885 * Load quotas for networks from configuration
1887 * @param cfg configuration handle
1888 * @param out_dest where to write outbound quotas
1889 * @param in_dest where to write inbound quotas
1890 * @param dest_length length of inbound and outbound arrays
1891 * @return number of networks loaded
1894 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1895 unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1897 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1898 char * entry_in = NULL;
1899 char * entry_out = NULL;
1900 char * quota_out_str;
1901 char * quota_in_str;
1905 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1909 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1910 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1914 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
1918 if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
1920 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1923 if ((GNUNET_NO == res)
1925 == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
1928 if ((GNUNET_NO == res)
1930 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
1934 if (GNUNET_NO == res)
1936 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1937 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1938 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1939 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1943 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1944 _("Outbound quota configure for network `%s' is %llu\n"),
1945 network_str[c], out_dest[c]);
1947 GNUNET_free(quota_out_str);
1951 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1952 _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1953 network_str[c], GNUNET_ATS_DefaultBandwidth);
1954 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1959 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
1963 if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
1965 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1968 if ((GNUNET_NO == res)
1970 == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1972 if ((GNUNET_NO == res)
1974 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
1978 if (GNUNET_NO == res)
1980 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1981 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1982 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1983 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1987 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1988 _("Inbound quota configured for network `%s' is %llu\n"),
1989 network_str[c], in_dest[c]);
1991 GNUNET_free(quota_in_str);
1995 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1996 _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1997 network_str[c], GNUNET_ATS_DefaultBandwidth);
1998 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2000 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2001 "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c],
2002 in_dest[c], out_dest[c]);
2003 GNUNET_free(entry_out);
2004 GNUNET_free(entry_in);
2006 return GNUNET_ATS_NetworkTypeCount;
2010 * Callback for solver to notify about assignment changes
2012 * @param cls the GAS_Addresses_Handle
2013 * @param address the address with changes
2016 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
2018 struct GAS_Addresses_Handle *handle = cls;
2019 struct GAS_Addresses_Suggestion_Requests *cur;
2021 GNUNET_assert(handle != NULL);
2022 GNUNET_assert(address != NULL);
2024 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2025 "Bandwidth assignment changed for peer %s \n",
2026 GNUNET_i2s (&address->peer));
2028 /* Notify performance clients about changes to address */
2029 GAS_performance_notify_all_clients (&address->peer, address->plugin,
2030 address->addr, address->addr_len, address->session_id, address->atsi,
2031 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
2032 cur = handle->pending_requests_head;
2035 if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
2036 break; /* we have an address request pending*/
2041 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Nobody is interested in peer `%s' :(\n",
2042 GNUNET_i2s (&address->peer));
2046 if ((0 == ntohl (address->assigned_bw_in.value__))
2047 && (0 == ntohl (address->assigned_bw_out.value__)))
2049 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2050 "Telling transport to disconnect peer `%s'\n",
2051 GNUNET_i2s (&address->peer));
2055 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2056 "Sending bandwidth update for peer `%s': %llu %llu\n",
2057 GNUNET_i2s (&address->peer), address->assigned_bw_out,
2058 address->assigned_bw_out);
2061 /* *Notify scheduling clients about suggestion */
2062 GAS_scheduling_transmit_address_suggestion (&address->peer, address->plugin,
2063 address->addr, address->addr_len, address->session_id, address->atsi,
2064 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
2068 * Initialize address subsystem. The addresses subsystem manages the addresses
2069 * known and current performance information. It has a solver component
2070 * responsible for the resource allocation. It tells the solver about changes
2071 * and receives updates when the solver changes the resource allocation.
2073 * @param cfg configuration to use
2074 * @param stats the statistics handle to use
2075 * @return an address handle
2077 struct GAS_Addresses_Handle *
2078 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
2079 const struct GNUNET_STATISTICS_Handle *stats)
2081 struct GAS_Addresses_Handle *ah;
2082 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
2083 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
2088 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
2089 ah->running = GNUNET_NO;
2091 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
2092 /* Initialize the addresses database */
2093 ah->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
2094 ah->pref_clients = 0;
2095 GNUNET_assert(NULL != ah->addresses);
2097 /* Figure out configured solution method */
2098 plugin_short = NULL;
2100 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
2102 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2103 "No resource assignment method configured, using proportional approach\n");
2104 ah->ats_mode = MODE_PROPORTIONAL;
2108 for (c = 0; c < strlen (mode_str); c++)
2109 mode_str[c] = toupper (mode_str[c]);
2110 if (0 == strcmp (mode_str, "PROPORTIONAL"))
2112 ah->ats_mode = MODE_PROPORTIONAL;
2113 plugin_short = "proportional";
2115 else if (0 == strcmp (mode_str, "MLP"))
2117 ah->ats_mode = MODE_MLP;
2118 plugin_short = "mlp";
2120 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2121 "Assignment method `%s' configured, but GLPK is not available, please install \n",
2123 ah->ats_mode = MODE_PROPORTIONAL;
2124 plugin_short = "proportional";
2127 else if (0 == strcmp (mode_str, "RIL"))
2129 ah->ats_mode = MODE_RIL;
2130 plugin_short = "ril";
2134 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2135 "Invalid resource assignment method `%s' configured, using proportional approach\n",
2137 ah->ats_mode = MODE_PROPORTIONAL;
2138 plugin_short = "proportional";
2140 GNUNET_free(mode_str);
2143 load_quotas (cfg, quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount);
2144 ah->env.info_cb = &solver_info_cb;
2145 ah->env.info_cb_cls = ah;
2146 ah->env.bandwidth_changed_cb = &bandwidth_changed_cb;
2147 ah->env.bw_changed_cb_cls = ah;
2148 ah->env.get_preferences = &get_preferences_cb;
2149 ah->env.get_preference_cls = ah;
2150 ah->env.get_property = &get_property_cb;
2151 ah->env.get_property_cls = ah;
2153 ah->env.stats = stats;
2154 ah->env.addresses = ah->addresses;
2156 ah->env.network_count = GNUNET_ATS_NetworkTypeCount;
2157 int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
2158 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
2160 ah->env.networks[c] = networks[c];
2161 ah->env.out_quota[c] = quotas_out[c];
2162 ah->env.in_quota[c] = quotas_in[c];
2165 GNUNET_asprintf (&ah->plugin, "libgnunet_plugin_ats_%s", plugin_short);
2166 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s '`%s'\n"), plugin_short, ah->plugin);
2167 if (NULL == (ah->solver = GNUNET_PLUGIN_load (ah->plugin, &ah->env)))
2169 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver `%s'!\n"), ah->plugin);
2173 GNUNET_assert (NULL != ah->env.sf.s_add);
2174 GNUNET_assert (NULL != ah->env.sf.s_address_update_inuse);
2175 GNUNET_assert (NULL != ah->env.sf.s_address_update_property);
2176 GNUNET_assert (NULL != ah->env.sf.s_address_update_session);
2177 GNUNET_assert (NULL != ah->env.sf.s_address_update_network);
2178 GNUNET_assert (NULL != ah->env.sf.s_get);
2179 GNUNET_assert (NULL != ah->env.sf.s_get_stop);
2180 GNUNET_assert (NULL != ah->env.sf.s_pref);
2181 GNUNET_assert (NULL != ah->env.sf.s_feedback);
2182 GNUNET_assert (NULL != ah->env.sf.s_del);
2183 GNUNET_assert (NULL != ah->env.sf.s_bulk_start);
2184 GNUNET_assert (NULL != ah->env.sf.s_bulk_stop);
2187 GAS_normalization_start (&normalized_preference_changed_cb, ah,
2188 &normalized_property_changed_cb, ah);
2190 if (NULL == ah->solver)
2192 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver!\n"));
2196 /* up and running */
2197 ah->running = GNUNET_YES;
2199 GNUNET_STATISTICS_set (ah->stat, "# addresses",
2200 GNUNET_CONTAINER_multipeermap_size (ah->addresses), GNUNET_NO);
2206 * Destroy all addresses iterator
2209 * @param key peer identity (unused)
2210 * @param value the 'struct ATS_Address' to free
2211 * @return #GNUNET_OK (continue to iterate)
2214 destroy_all_address_it (void *cls,
2215 const struct GNUNET_PeerIdentity *key,
2218 struct GAS_Addresses_Handle *handle = cls;
2219 struct ATS_Address *aa = value;
2222 GNUNET_assert(GNUNET_YES ==
2223 GNUNET_CONTAINER_multipeermap_remove (handle->addresses, key, value));
2225 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
2234 * Remove all addresses
2236 * @param handle the address handle to use
2239 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
2241 if (GNUNET_NO == handle->running)
2244 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Destroying all addresses\n");
2245 handle->env.sf.s_bulk_start (handle->solver);
2246 if (handle->addresses != NULL )
2247 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2248 &destroy_all_address_it,
2250 handle->env.sf.s_bulk_start (handle->solver);
2255 * Shutdown address subsystem.
2257 * @param handle the address handle to shutdown
2260 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
2262 struct GAS_Addresses_Suggestion_Requests *cur;
2263 struct GAS_Addresses_Preference_Clients *pcur;
2265 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Shutting down addresses\n");
2266 GNUNET_assert(NULL != handle);
2267 GAS_addresses_destroy_all (handle);
2268 handle->running = GNUNET_NO;
2269 GNUNET_CONTAINER_multipeermap_destroy (handle->addresses);
2270 handle->addresses = NULL;
2271 while (NULL != (cur = handle->pending_requests_head))
2273 GNUNET_CONTAINER_DLL_remove(handle->pending_requests_head, handle->pending_requests_tail, cur);
2277 while (NULL != (pcur = handle->preference_clients_head))
2279 GNUNET_CONTAINER_DLL_remove (handle->preference_clients_head,
2280 handle->preference_clients_tail, pcur);
2281 GNUNET_assert (handle->pref_clients > 0);
2282 handle->pref_clients --;
2283 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
2287 GNUNET_PLUGIN_unload (handle->plugin, handle->solver);
2288 GNUNET_free (handle->plugin);
2289 GNUNET_free(handle);
2290 /* Stop configured solution method */
2291 GAS_normalization_stop ();
2295 struct PeerIteratorContext
2297 GNUNET_ATS_Peer_Iterator it;
2299 struct GNUNET_CONTAINER_MultiPeerMap *peers_returned;
2304 * Iterator to iterate over all peers
2306 * @param cls a PeerIteratorContext
2307 * @param key the peer id
2308 * @param value the ATS_address
2309 * @return #GNUNET_OK to continue
2313 const struct GNUNET_PeerIdentity *key,
2316 struct PeerIteratorContext *ip_ctx = cls;
2319 GNUNET_CONTAINER_multipeermap_contains (ip_ctx->peers_returned, key))
2321 GNUNET_CONTAINER_multipeermap_put (ip_ctx->peers_returned, key, NULL,
2322 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2323 ip_ctx->it (ip_ctx->it_cls, key);
2330 * Return information all peers currently known to ATS
2332 * @param handle the address handle to use
2333 * @param p_it the iterator to call for every peer
2334 * @param p_it_cls the closure for the iterator
2337 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
2338 GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
2340 struct PeerIteratorContext ip_ctx;
2345 GNUNET_assert(NULL != handle->addresses);
2347 size = GNUNET_CONTAINER_multipeermap_size (handle->addresses);
2351 ip_ctx.it_cls = p_it_cls;
2352 ip_ctx.peers_returned = GNUNET_CONTAINER_multipeermap_create (size,
2354 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2357 GNUNET_CONTAINER_multipeermap_destroy (ip_ctx.peers_returned);
2359 p_it (p_it_cls, NULL );
2362 struct PeerInfoIteratorContext
2364 GNUNET_ATS_PeerInfo_Iterator it;
2370 * Iterator to iterate over a peer's addresses
2372 * @param cls a `struct PeerInfoIteratorContext`
2373 * @param key the peer id
2374 * @param value the `struct ATS_address`
2375 * @return #GNUNET_OK to continue
2378 peerinfo_it (void *cls,
2379 const struct GNUNET_PeerIdentity *key,
2382 struct PeerInfoIteratorContext *pi_ctx = cls;
2383 struct ATS_Address *addr = value;
2385 if (NULL != pi_ctx->it)
2387 pi_ctx->it (pi_ctx->it_cls, &addr->peer, addr->plugin, addr->addr,
2388 addr->addr_len, addr->active, addr->atsi, addr->atsi_count,
2389 addr->assigned_bw_out, addr->assigned_bw_in);
2396 * Return information all peers currently known to ATS
2398 * @param handle the address handle to use
2399 * @param peer the respective peer
2400 * @param pi_it the iterator to call for every peer
2401 * @param pi_it_cls the closure for the iterator
2404 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
2405 const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it,
2408 struct PeerInfoIteratorContext pi_ctx;
2409 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
2411 GNUNET_assert(NULL != peer);
2412 GNUNET_assert(NULL != handle->addresses);
2414 return; /* does not make sense without callback */
2416 zero_bw = GNUNET_BANDWIDTH_value_init (0);
2418 pi_ctx.it_cls = pi_it_cls;
2420 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
2422 &peerinfo_it, &pi_ctx);
2425 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw,
2430 /* end of gnunet-service-ats_addresses.c */