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,
480 const char *plugin_name,
481 const void *plugin_addr,
482 size_t plugin_addr_len,
483 uint32_t local_address_info,
486 struct ATS_Address *aa = NULL;
490 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
492 aa->addr_len = plugin_addr_len;
494 memcpy (&aa[1], plugin_addr, plugin_addr_len);
495 aa->plugin = GNUNET_strdup (plugin_name);
496 aa->session_id = session_id;
497 aa->local_address_info = local_address_info;
498 aa->active = GNUNET_NO;
499 aa->used = GNUNET_NO;
500 aa->solver_information = NULL;
503 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
504 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
506 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
508 aa->atsin[c1].avg_queue_index = 0;
509 for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
510 aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
516 struct CompareAddressContext
518 const struct ATS_Address *search;
520 /* exact_address != NULL if address and session is equal */
521 struct ATS_Address *exact_address;
522 /* exact_address != NULL if address and session is 0 */
523 struct ATS_Address *base_address;
530 * @param cls a CompareAddressContext containin the source address
532 * @param value the address to compare with
533 * @return #GNUNET_YES to continue, #GNUNET_NO if address is founce
536 compare_address_it (void *cls,
537 const struct GNUNET_PeerIdentity *key,
540 struct CompareAddressContext *cac = cls;
541 struct ATS_Address *aa = value;
543 /* Find an matching exact address:
546 * aa->addr_len == cac->search->addr_len
547 * aa->plugin == cac->search->plugin
548 * aa->addr == cac->search->addr
549 * aa->session == cac->search->session
551 * return as exact address
553 if ((aa->addr_len == cac->search->addr_len)
554 && (0 == strcmp (aa->plugin, cac->search->plugin)))
556 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
557 && (aa->session_id == cac->search->session_id))
558 cac->exact_address = aa;
561 /* Find an matching base address:
565 * aa->session_id == 0
568 * aa->addr_len == cac->search->addr_len
569 * aa->plugin == cac->search->plugin
570 * aa->addr == cac->search->addr
572 * return as base address
574 if ((aa->addr_len == cac->search->addr_len)
575 && (0 == strcmp (aa->plugin, cac->search->plugin)))
577 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
578 && (aa->session_id == 0))
579 cac->base_address = aa;
582 /* Find an matching exact address based on session:
586 * cac->search->addr_len == 0
589 * aa->plugin == cac->search->plugin
590 * aa->session_id == cac->search->session_id
592 * return as exact address
594 if (0 == cac->search->addr_len)
596 if ((0 == strcmp (aa->plugin, cac->search->plugin))
597 && (aa->session_id == cac->search->session_id))
598 cac->exact_address = aa;
601 if (cac->exact_address == NULL )
602 return GNUNET_YES; /* Continue iteration to find exact address */
604 return GNUNET_NO; /* Stop iteration since we have an exact address */
608 * Find an existing equivalent address record.
609 * Compares by peer identity and network address OR by session ID
610 * (one of the two must match).
612 * @param handle the address handle
613 * @param peer peer to lookup addresses for
614 * @param addr existing address record
615 * @return existing address record, NULL for none
618 find_equivalent_address (struct GAS_Addresses_Handle *handle,
619 const struct GNUNET_PeerIdentity *peer, const struct ATS_Address *addr)
621 struct CompareAddressContext cac;
623 cac.exact_address = NULL;
624 cac.base_address = NULL;
626 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
628 &compare_address_it, &cac);
630 if (cac.exact_address == NULL)
631 return cac.base_address;
632 return cac.exact_address;
637 * Find the exact address
639 * @param handle the address handle to use
641 * @param plugin_name transport plugin name
642 * @param plugin_addr plugin address
643 * @param plugin_addr_len length of the plugin address
644 * @param local_address_info the local address for the address
645 * @param session_id session id, can be 0
646 * @return an ATS_address or NULL
649 static struct ATS_Address *
650 find_exact_address (struct GAS_Addresses_Handle *handle,
651 const struct GNUNET_PeerIdentity *peer,
652 const char *plugin_name,
653 const void *plugin_addr,
654 size_t plugin_addr_len,
655 uint32_t local_address_info,
658 struct ATS_Address *aa;
659 struct ATS_Address *ea;
661 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
662 local_address_info, session_id);
664 /* Get existing address or address with session == 0 */
665 ea = find_equivalent_address (handle, peer, aa);
669 else if (ea->session_id != session_id)
675 * Function allowing the solver to obtain normalized preference
679 * @param id the peer to return the normalized properties for
680 * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
683 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
685 return GAS_normalization_get_preferences_by_peer (id);
689 * Function allowing the solver to obtain normalized property
690 * values for an address from solver
693 * @param address the address
694 * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
697 get_property_cb (void *cls, const struct ATS_Address *address)
699 return GAS_normalization_get_properties ((struct ATS_Address *) address);
703 * Extract an ATS performance info from an address
705 * @param address the address
706 * @param type the type to extract in HBO
707 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
710 get_performance_info (struct ATS_Address *address, uint32_t type)
713 GNUNET_assert(NULL != address);
715 if ((NULL == address->atsi) || (0 == address->atsi_count))
716 return GNUNET_ATS_VALUE_UNDEFINED;
718 for (c1 = 0; c1 < address->atsi_count; c1++)
720 if (ntohl (address->atsi[c1].type) == type)
721 return ntohl (address->atsi[c1].value);
723 return GNUNET_ATS_VALUE_UNDEFINED;
727 * Add a new address for a peer.
729 * @param handle the address handle to use
731 * @param plugin_name transport plugin name
732 * @param plugin_addr plugin address
733 * @param plugin_addr_len length of the plugin address
734 * @param local_address_info the local address for the address
735 * @param session_id session id, can be 0
736 * @param atsi performance information for this address
737 * @param atsi_count number of performance information contained
740 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
741 const struct GNUNET_PeerIdentity *peer,
742 const char *plugin_name,
743 const void *plugin_addr,
744 size_t plugin_addr_len,
745 uint32_t local_address_info,
747 const struct GNUNET_ATS_Information *atsi,
750 struct ATS_Address *new_address;
751 struct ATS_Address *existing_address;
752 struct GNUNET_ATS_Information *atsi_delta;
753 uint32_t atsi_delta_count;
755 uint32_t previous_session;
758 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
759 "ADDRESS ADD", GNUNET_i2s (peer));
761 if (GNUNET_NO == handle->running)
764 GNUNET_assert(NULL != handle->addresses);
766 new_address = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
767 local_address_info, session_id);
769 disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
771 GNUNET_free_non_null(atsi_delta);
772 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
773 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
774 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
776 /* Get existing address or address with session == 0 */
777 existing_address = find_equivalent_address (handle, peer, new_address);
778 if (existing_address == NULL )
780 /* Add a new address */
781 new_address->t_added = GNUNET_TIME_absolute_get();
782 new_address->t_last_activity = GNUNET_TIME_absolute_get();
784 GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (handle->addresses,
787 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
789 GNUNET_STATISTICS_set (handle->stat, "# addresses",
790 GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
792 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
793 "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
796 plugin_addr_len, session_id,
797 GNUNET_ATS_print_network_type (addr_net));
799 /* Tell solver about new address */
800 handle->env.sf.s_add (handle->solver, new_address, addr_net);
802 handle->env.sf.s_bulk_start (handle->solver);
803 GAS_normalization_normalize_property (handle->addresses, new_address, atsi,
805 handle->env.sf.s_bulk_stop (handle->solver);
807 /* Notify performance clients about new address */
808 GAS_performance_notify_all_clients (&new_address->peer, new_address->plugin,
809 new_address->addr, new_address->addr_len, new_address->session_id,
810 new_address->atsi, new_address->atsi_count,
811 new_address->assigned_bw_out, new_address->assigned_bw_in);
815 /* We have an existing address we can use, clean up new */
816 GNUNET_free(new_address->plugin);
817 GNUNET_free_non_null(new_address->atsi);
818 GNUNET_free(new_address);
821 if (0 != existing_address->session_id)
823 /* Should not happen */
828 addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
829 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
830 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
832 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
833 "Found existing address for peer `%s' %p with new session %u in network %s\n",
834 GNUNET_i2s (peer), existing_address, session_id,
835 GNUNET_ATS_print_network_type (addr_net));
836 /* We have an address without an session, update this address */
837 existing_address->t_added = GNUNET_TIME_absolute_get();
838 existing_address->t_last_activity = GNUNET_TIME_absolute_get();
840 atsi_delta_count = 0;
842 == disassemble_ats_information (existing_address, atsi, atsi_count,
843 &atsi_delta, &atsi_delta_count))
845 /* Notify performance clients about properties */
846 GAS_performance_notify_all_clients (&existing_address->peer,
847 existing_address->plugin, existing_address->addr,
848 existing_address->addr_len, existing_address->session_id,
849 existing_address->atsi, existing_address->atsi_count,
850 existing_address->assigned_bw_out, existing_address->assigned_bw_in);
852 for (c1 = 0; c1 < atsi_delta_count; c1++)
854 if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
855 && (addr_net != ntohl (atsi_delta[c1].value)))
857 /* Network type changed */
858 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
859 "Address for peer `%s' %p changed from network %s to %s\n",
860 GNUNET_i2s (peer), existing_address,
861 GNUNET_ATS_print_network_type (addr_net),
862 GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
863 handle->env.sf.s_address_update_network (handle->solver, existing_address,
864 ntohl (atsi_delta[c1].value),
865 get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
866 addr_net = get_performance_info (existing_address,
867 GNUNET_ATS_NETWORK_TYPE);
870 /* Notify solver about update with atsi information and session */
871 handle->env.sf.s_bulk_start (handle->solver);
872 GAS_normalization_normalize_property (handle->addresses, existing_address,
874 handle->env.sf.s_bulk_stop (handle->solver);
876 GNUNET_free_non_null(atsi_delta);
878 /* Notify solver about new session */
879 if (existing_address->session_id == session_id)
880 return; /* possible, can both be 0 since address is revalidated */
882 previous_session = existing_address->session_id;
883 existing_address->session_id = session_id;
884 handle->env.sf.s_address_update_session (handle->solver, existing_address,
885 previous_session, session_id);
887 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
888 "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
889 GNUNET_i2s (peer), existing_address, existing_address->addr_len,
890 session_id, GNUNET_ATS_print_network_type (addr_net));
894 * Update an address with a session or performance information for a peer.
896 * If an address was added without a session it will be updated with the
899 * @param handle the address handle to use
901 * @param plugin_name transport plugin name
902 * @param plugin_addr plugin address
903 * @param plugin_addr_len length of the plugin address
904 * @param local_address_info the local address for the address
905 * @param session_id session id, can be 0
906 * @param atsi performance information for this address
907 * @param atsi_count number of performance information contained
910 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
911 const struct GNUNET_PeerIdentity *peer,
912 const char *plugin_name,
913 const void *plugin_addr,
914 size_t plugin_addr_len,
915 uint32_t local_address_info,
917 const struct GNUNET_ATS_Information *atsi,
920 struct ATS_Address *aa;
921 struct GNUNET_ATS_Information *atsi_delta;
922 uint32_t atsi_delta_count;
923 uint32_t prev_session;
926 if (GNUNET_NO == handle->running)
929 GNUNET_assert(NULL != handle->addresses);
931 /* Get existing address */
932 aa = find_exact_address (handle, peer, plugin_name, plugin_addr,
933 plugin_addr_len, local_address_info, session_id);
936 if (NULL == aa->solver_information)
939 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s' address \n",
940 "ADDRESS UPDATE", GNUNET_i2s (peer), aa);
943 aa->t_last_activity = GNUNET_TIME_absolute_get();
944 if (session_id != aa->session_id)
946 /* Session changed */
947 prev_session = aa->session_id;
948 aa->session_id = session_id;
949 handle->env.sf.s_address_update_session (handle->solver, aa, prev_session,
954 atsi_delta_count = 0;
956 == disassemble_ats_information (aa, atsi, atsi_count, &atsi_delta,
959 /* ATS properties changed */
960 for (c1 = 0; c1 < atsi_delta_count; c1++)
962 if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
964 /* Network type changed */
965 handle->env.sf.s_address_update_network (handle->solver, aa,
966 ntohl (atsi_delta[c1].value),
967 get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
971 /* Notify performance clients about updated address */
972 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr,
973 aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
974 aa->assigned_bw_out, aa->assigned_bw_in);
976 handle->env.sf.s_bulk_start (handle->solver);
977 GAS_normalization_normalize_property (handle->addresses, aa, atsi,
979 handle->env.sf.s_bulk_stop (handle->solver);
981 GNUNET_free_non_null(atsi_delta);
984 struct DestroyContext
986 struct ATS_Address *aa;
988 struct GAS_Addresses_Handle *handle;
991 * GNUNET_NO : full address
992 * GNUNET_YES : just session
1000 * If session != 0, just the session is deleted, the address itself still exists
1001 * If session == 0, remove full address
1002 * If session == 0 and addrlen == 0, destroy inbound address
1006 * @param value the 'struct ATS_Address'
1007 * @return GNUNET_OK (continue to iterate)
1010 destroy_by_session_id (void *cls,
1011 const struct GNUNET_PeerIdentity *key,
1014 struct DestroyContext *dc = cls;
1015 struct GAS_Addresses_Handle *handle = dc->handle;
1016 const struct ATS_Address *des = dc->aa;
1017 struct ATS_Address *aa = value;
1020 0 == memcmp (&aa->peer, &des->peer, sizeof(struct GNUNET_PeerIdentity)));
1022 if (des->session_id == 0)
1024 /* Session == 0, remove full address */
1025 if ((0 == strcmp (des->plugin, aa->plugin))
1026 && (aa->addr_len == des->addr_len)
1027 && (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
1030 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1031 "Deleting full address for peer `%s' session %u %p\n",
1032 GNUNET_i2s (&aa->peer), aa->session_id, aa);
1034 /* Notify solver about deletion */
1036 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1039 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1041 dc->result = GNUNET_NO;
1042 return GNUNET_OK; /* Continue iteration */
1047 /* Session != 0, just remove session */
1048 if (aa->session_id != des->session_id)
1049 return GNUNET_OK; /* irrelevant */
1051 if ((aa->session_id != 0) && (0 != strcmp (des->plugin, aa->plugin)))
1053 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1054 "Different plugins during removal: `%s' vs `%s' \n", des->plugin,
1060 if (aa->addr_len == 0)
1062 /* Inbound connection died, delete full address */
1063 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1064 "Deleting inbound address for peer `%s': `%s' session %u\n",
1065 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
1067 /* Notify solver about deletion */
1069 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1071 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1073 dc->result = GNUNET_NO;
1074 return GNUNET_OK; /* Continue iteration */
1079 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1080 "Deleting session for peer `%s': `%s' %u\n", GNUNET_i2s (&aa->peer),
1081 aa->plugin, aa->session_id);
1082 /* Notify solver to delete session */
1083 handle->env.sf.s_del (handle->solver, aa, GNUNET_YES);
1085 aa->active = GNUNET_NO;
1094 * Remove an address or just a session for a peer.
1096 * @param handle the address handle to use
1098 * @param plugin_name transport plugin name
1099 * @param plugin_addr plugin address
1100 * @param plugin_addr_len length of the plugin address
1101 * @param local_address_info the local address for the address
1102 * @param session_id session id, can be 0
1105 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
1106 const struct GNUNET_PeerIdentity *peer,
1107 const char *plugin_name,
1108 const void *plugin_addr,
1109 size_t plugin_addr_len,
1110 uint32_t local_address_info,
1111 uint32_t session_id)
1113 struct ATS_Address *ea;
1114 struct DestroyContext dc;
1115 if (GNUNET_NO == handle->running)
1118 /* Get existing address */
1119 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1120 plugin_addr_len, local_address_info, session_id);
1123 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1124 "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
1125 GNUNET_i2s (peer), plugin_name, session_id);
1129 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1130 "Received `%s' for peer `%s' address %p session %u\n", "ADDRESS DESTROY",
1131 GNUNET_i2s (peer), ea, session_id);
1133 GNUNET_break(0 < strlen (plugin_name));
1135 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
1136 local_address_info, session_id);
1138 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1140 &destroy_by_session_id, &dc);
1141 GNUNET_STATISTICS_set (handle->stat, "# addresses",
1142 GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
1143 free_address (dc.aa);
1147 * Notification about active use of an address.
1148 * in_use == GNUNET_YES:
1149 * This address is used to maintain an active connection with a peer.
1150 * in_use == GNUNET_NO:
1151 * This address is no longer used to maintain an active connection with a peer.
1153 * Note: can only be called with in_use == GNUNET_NO if called with GNUNET_YES
1156 * @param handle the address handle to use
1158 * @param plugin_name transport plugin name
1159 * @param plugin_addr plugin address
1160 * @param plugin_addr_len length of the plugin address
1161 * @param local_address_info the local address for the address
1162 * @param session_id session id, can be 0
1163 * @param in_use GNUNET_YES if GNUNET_NO
1164 * @return GNUNET_SYSERR on failure (address unknown ...)
1167 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
1168 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1169 const void *plugin_addr, size_t plugin_addr_len,
1170 uint32_t local_address_info,
1171 uint32_t session_id,
1174 struct ATS_Address *ea;
1175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1176 "ADDRESS IN USE", GNUNET_i2s (peer));
1178 if (GNUNET_NO == handle->running)
1179 return GNUNET_SYSERR;
1181 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1182 plugin_addr_len, local_address_info, session_id);
1185 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1186 "Trying to set unknown address `%s' `%s' `%u' to %s \n",
1187 GNUNET_i2s (peer), plugin_name, session_id,
1188 (GNUNET_NO == in_use) ? "NO" : "YES");
1190 return GNUNET_SYSERR;
1192 if (ea->used == in_use)
1195 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1196 "Address in use called multiple times for peer `%s': %s -> %s \n",
1197 GNUNET_i2s (peer), (GNUNET_NO == ea->used) ? "NO" : "YES",
1198 (GNUNET_NO == in_use) ? "NO" : "YES");
1199 return GNUNET_SYSERR;
1202 /* Tell solver about update */
1204 ea->t_last_activity = GNUNET_TIME_absolute_get();
1205 handle->env.sf.s_address_update_inuse (handle->solver, ea, ea->used);
1210 * Cancel address suggestions for a peer
1212 * @param handle the address handle
1213 * @param peer the peer id
1216 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
1217 const struct GNUNET_PeerIdentity *peer)
1219 struct GAS_Addresses_Suggestion_Requests *cur = handle->pending_requests_head;
1221 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received request: `%s' for peer %s\n",
1222 "request_address_cancel", GNUNET_i2s (peer));
1226 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1233 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1234 "No address requests pending for peer `%s', cannot remove!\n",
1238 handle->env.sf.s_get_stop (handle->solver, peer);
1239 GAS_addresses_handle_backoff_reset (handle, peer);
1240 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Removed request pending for peer `%s\n",
1242 GNUNET_CONTAINER_DLL_remove(handle->pending_requests_head, handle->pending_requests_tail, cur);
1248 * Request address suggestions for a peer
1250 * @param handle the address handle
1251 * @param peer the peer id
1254 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
1255 const struct GNUNET_PeerIdentity *peer)
1257 struct GAS_Addresses_Suggestion_Requests *cur = handle->pending_requests_head;
1258 struct ATS_Address *aa;
1260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1261 "REQUEST ADDRESS", GNUNET_i2s (peer));
1263 if (GNUNET_NO == handle->running)
1267 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1268 break; /* already suggesting */
1273 cur = GNUNET_new (struct GAS_Addresses_Suggestion_Requests);
1275 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1276 "Adding new address suggestion request for `%s'\n",
1278 GNUNET_CONTAINER_DLL_insert(handle->pending_requests_head, handle->pending_requests_tail, cur);
1281 /* Get prefered address from solver */
1282 aa = (struct ATS_Address *) handle->env.sf.s_get (handle->solver, peer);
1285 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1290 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1291 aa, GNUNET_i2s (peer));
1293 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
1294 aa->addr_len, aa->local_address_info, aa->session_id,
1295 aa->atsi, aa->atsi_count,
1296 aa->assigned_bw_out, aa->assigned_bw_in);
1298 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1299 ATS_BLOCKING_DELTA);
1300 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1301 aa->block_interval);
1303 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1304 "Address %p ready for suggestion, block interval now %llu \n", aa,
1305 aa->block_interval);
1309 * Iterator to reset address blocking
1311 * @param cls not used
1312 * @param key the peer
1313 * @param value the address to reset
1314 * @return #GNUNET_OK to continue
1317 reset_address_it (void *cls,
1318 const struct GNUNET_PeerIdentity *key,
1321 struct ATS_Address *aa = value;
1323 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1324 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1325 GNUNET_i2s (&aa->peer),
1327 aa->block_interval);
1328 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1329 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1335 * Reset suggestion backoff for a peer
1337 * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1338 * reset using this function
1340 * @param handle the address handle
1341 * @param peer the peer id
1344 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1345 const struct GNUNET_PeerIdentity *peer)
1347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1348 "RESET BACKOFF", GNUNET_i2s (peer));
1351 GNUNET_SYSERR != GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1353 &reset_address_it, NULL));
1358 eval_count_active_it (void *cls,
1359 const struct GNUNET_PeerIdentity *id,
1362 int *request_fulfilled = cls;
1363 struct ATS_Address *addr = obj;
1365 if (GNUNET_YES == addr->active)
1366 (*request_fulfilled) = GNUNET_YES;
1368 if (*request_fulfilled == GNUNET_YES)
1378 struct SummaryContext
1381 * Sum of the utilized inbound bandwidth per network
1383 unsigned long long bandwidth_in_assigned[GNUNET_ATS_NetworkTypeCount];
1386 * Sum of the utilized outbound bandwidth per network
1388 unsigned long long bandwidth_out_assigned[GNUNET_ATS_NetworkTypeCount];
1391 * Sum addresses within a network
1393 unsigned int addresses_in_network[GNUNET_ATS_NetworkTypeCount];
1398 eval_sum_bw_used (void *cls, const struct GNUNET_PeerIdentity *id, void *obj)
1400 struct SummaryContext *ctx = cls;
1401 struct ATS_Address *addr = obj;
1402 int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1406 if (GNUNET_YES == addr->active)
1408 net = get_performance_info (addr, GNUNET_ATS_NETWORK_TYPE);
1409 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1411 if (net == networks[c])
1413 ctx->addresses_in_network[c] ++;
1414 ctx->bandwidth_in_assigned[c] += ntohl (addr->assigned_bw_in.value__);
1415 ctx->bandwidth_out_assigned[c] += ntohl (addr->assigned_bw_out.value__);
1418 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1419 "Active address in %s with (in/out) %u/%u Bps\n",
1420 GNUNET_ATS_print_network_type (net),
1421 (unsigned int) ntohl (addr->assigned_bw_in.value__),
1422 (unsigned int) ntohl (addr->assigned_bw_out.value__));
1431 struct RelativityContext
1434 struct GAS_Addresses_Handle *ah;
1439 find_active_address (void *cls, const struct GNUNET_PeerIdentity *id, void *obj)
1441 struct ATS_Address **res = cls;
1442 struct ATS_Address *addr = obj;
1444 if (GNUNET_YES == addr->active)
1454 * Evaluate current bandwidth assignment
1456 * @param ah address handle
1459 GAS_addresses_evaluate_assignment (struct GAS_Addresses_Handle *ah)
1461 struct GAS_Addresses_Suggestion_Requests *cur;
1462 struct GAS_Addresses_Preference_Clients *pcur;
1465 float quality_requests_fulfilled = 0.0;
1466 float quality_bandwidth_utilization[GNUNET_ATS_NetworkTypeCount];
1467 float quality_bandwidth_utilization_total = 0.0;
1468 float quality_application_requirements = 0.0;
1471 int include_requests;
1472 int include_utilization;
1473 int include_requirements;
1475 /* Variable related to requests */
1476 unsigned int requests_pending;
1477 unsigned int requests_fulfilled;
1478 unsigned int request_active;
1480 /* Variable related to utilization */
1481 struct SummaryContext sum;
1482 struct ATS_Address *active_address;
1485 /* Variables for preferences */
1486 int prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceType;
1489 const double *norm_values;
1490 double prefs_fulfill[GNUNET_ATS_PreferenceCount];
1491 int prefs_clients[GNUNET_ATS_PreferenceCount];
1494 GNUNET_assert (NULL != ah);
1495 GNUNET_assert (NULL != ah->addresses);
1497 requests_pending = 0;
1498 requests_fulfilled = 0;
1499 /* 1) How many requests could be fulfilled? */
1500 for (cur = ah->pending_requests_head; NULL != cur; cur = cur->next)
1502 request_active = GNUNET_NO;
1503 GNUNET_CONTAINER_multipeermap_get_multiple (ah->addresses,
1504 &cur->id, &eval_count_active_it, &request_active);
1505 if (GNUNET_YES == request_active)
1506 requests_fulfilled ++;
1507 requests_pending ++;
1508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': %u pending requests, %s\n",
1509 GNUNET_i2s (&cur->id),
1511 (GNUNET_YES == request_active) ? "active adress" : "no active address");
1514 if (requests_pending > 0)
1516 quality_requests_fulfilled = (float) requests_fulfilled / requests_pending;
1517 include_requests = GNUNET_YES;
1521 quality_requests_fulfilled = 0.0;
1522 include_requests = GNUNET_NO;
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u pending requests, %u requests fullfilled\n",
1525 requests_pending, requests_fulfilled);
1527 /* 2) How well is bandwidth utilized? */
1529 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1531 quality_bandwidth_utilization[c] = 0.0;
1532 sum.addresses_in_network[c] = 0;
1533 sum.bandwidth_in_assigned[c] = 0;
1534 sum.bandwidth_out_assigned[c] = 0;
1536 GNUNET_CONTAINER_multipeermap_iterate(ah->addresses,
1537 &eval_sum_bw_used, &sum);
1538 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1540 quality_bandwidth_utilization[c] = (((float)sum.bandwidth_out_assigned[c] / ah->env.out_quota[c]) +
1541 ((float)sum.bandwidth_in_assigned[c] / ah->env.in_quota[c])) / 2;
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Utilization for network `%s': %f\n",
1544 GNUNET_ATS_print_network_type(ah->env.networks[c]),
1545 quality_bandwidth_utilization[c]);
1546 if (sum.addresses_in_network[c] > 0)
1548 quality_bandwidth_utilization_total += quality_bandwidth_utilization[c];
1552 if (0 < network_count)
1554 quality_bandwidth_utilization_total /= network_count;
1555 include_utilization = GNUNET_YES;
1559 quality_bandwidth_utilization_total = 0.0;
1560 include_utilization = GNUNET_NO;
1563 /* 3) How well does selection match application requirements */
1564 if (0 == ah->pref_clients)
1566 include_requirements = 0;
1570 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1572 prefs_fulfill[c] = 0.0;
1573 prefs_clients[c] = 0;
1576 for (cur = ah->pending_requests_head; NULL != cur; cur = cur->next)
1578 active_address = NULL;
1579 GNUNET_CONTAINER_multipeermap_get_multiple (ah->addresses,
1580 &cur->id, &find_active_address, &active_address);
1582 for (pcur = ah->preference_clients_head; NULL != pcur; pcur = pcur->next)
1584 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1586 if (prefs[c] == GNUNET_ATS_PREFERENCE_END)
1588 pref_val = GAS_normalization_get_preferences_by_client (pcur->client, &cur->id, prefs[c]);
1589 if (-1.0 == pref_val)
1595 if (DEFAULT_REL_PREFERENCE == pref_val)
1597 /* Default preference value */
1601 if (NULL != active_address)
1603 norm_values = GAS_normalization_get_properties (active_address);
1604 prop_val = norm_values[c];
1605 if ((norm_values[c] <= 1.0) || (norm_values[c] >= 2.0))
1606 prop_val = DEFAULT_REL_QUALITY;
1610 prop_val = DEFAULT_REL_QUALITY;
1613 /* We now have preference values [1..2] and properties [1..2] */
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u Client %p, Peer %s Property %s: pref: %.3f prop %.3f \n",
1618 GNUNET_i2s (&cur->id),
1619 GNUNET_ATS_print_preference_type(prefs[c]),
1623 prefs_fulfill[c] += (pref_val * prop_val) / 2;
1624 prefs_clients[c] ++;
1629 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1631 if (0 < prefs_clients[c])
1633 prefs_fulfill[c] /= prefs_clients[c];
1635 quality_application_requirements += prefs_fulfill[c];
1639 quality_application_requirements /= rels;
1641 quality_application_requirements = 0.0;
1643 include_requirements = 1;
1647 if (include_requests + include_utilization + include_requirements > 0)
1648 guq = (quality_requests_fulfilled + quality_bandwidth_utilization_total + quality_application_requirements) /
1649 (include_requests + include_utilization + include_requirements);
1653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1654 "Requests fulfilled %.3f bandwidth utilized %.3f application preferences met %.3f => %.3f\n",
1655 quality_requests_fulfilled,
1656 quality_bandwidth_utilization_total,
1657 quality_application_requirements,
1662 * Solver information callback
1664 * @param cls the closure
1665 * @param op the operation
1666 * @param stat operation status
1667 * @param add additional information
1671 solver_info_cb (void *cls,
1672 enum GAS_Solver_Operation op,
1673 enum GAS_Solver_Status stat,
1674 enum GAS_Solver_Additional_Information add)
1680 add_info = "GAS_INFO_NONE";
1683 add_info = "GAS_INFO_MLP_FULL";
1685 case GAS_INFO_UPDATED:
1686 add_info = "GAS_INFO_MLP_UPDATED";
1688 case GAS_INFO_PROP_ALL:
1689 add_info = "GAS_INFO_PROP_ALL";
1691 case GAS_INFO_PROP_SINGLE:
1692 add_info = "GAS_INFO_PROP_SINGLE";
1695 add_info = "INVALID";
1700 case GAS_OP_SOLVE_START:
1701 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1702 "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
1703 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
1705 case GAS_OP_SOLVE_STOP:
1706 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1707 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
1708 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
1711 case GAS_OP_SOLVE_SETUP_START:
1712 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1713 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
1714 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1717 case GAS_OP_SOLVE_SETUP_STOP:
1718 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1719 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
1720 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1723 case GAS_OP_SOLVE_MLP_LP_START:
1724 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1725 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
1726 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1728 case GAS_OP_SOLVE_MLP_LP_STOP:
1729 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1730 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
1731 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1734 case GAS_OP_SOLVE_MLP_MLP_START:
1735 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1736 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
1737 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1739 case GAS_OP_SOLVE_MLP_MLP_STOP:
1740 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1741 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
1742 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1744 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
1745 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1746 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
1747 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1749 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
1750 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1751 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
1752 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1753 GAS_addresses_evaluate_assignment (cls);
1762 * The preference changed for a peer
1764 * @param cls the address handle
1765 * @param peer the peer
1766 * @param kind the ATS kind
1767 * @param pref_rel the new relative preference value
1770 normalized_preference_changed_cb (void *cls,
1771 const struct GNUNET_PeerIdentity *peer, enum GNUNET_ATS_PreferenceKind kind,
1774 GNUNET_assert(NULL != cls);
1775 struct GAS_Addresses_Handle *handle = cls;
1777 /* Tell solver about update */
1778 handle->env.sf.s_pref (handle->solver, peer, kind, pref_rel);
1782 * The relative value for a property changed
1784 * @param cls the address handle
1785 * @param address the peer
1786 * @param type the ATS type
1787 * @param prop_rel the new relative preference value
1790 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
1791 uint32_t type, double prop_rel)
1793 struct GAS_Addresses_Handle *ah = (struct GAS_Addresses_Handle *) cls;
1794 GNUNET_assert(NULL != ah);
1796 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1797 "Normalized property %s for peer `%s' changed to %.3f \n",
1798 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1801 ah->env.sf.s_address_update_property (ah->solver, address, type, 0, prop_rel);
1804 static struct GAS_Addresses_Preference_Clients *
1805 find_preference_client (struct GAS_Addresses_Handle *handle, void *client)
1807 struct GAS_Addresses_Preference_Clients *cur;
1809 for (cur = handle->preference_clients_head; NULL != cur; cur = cur->next)
1811 if (cur->client == client)
1818 * A performance client disconnected
1820 * @param handle address handle
1821 * @param client the client
1825 GAS_addresses_preference_client_disconnect (struct GAS_Addresses_Handle *handle,
1828 struct GAS_Addresses_Preference_Clients * pc;
1829 if (NULL != (pc = find_preference_client (handle, client)))
1831 GNUNET_CONTAINER_DLL_remove (handle->preference_clients_head,
1832 handle->preference_clients_tail, pc);
1834 GNUNET_assert (handle->pref_clients > 0);
1835 handle->pref_clients --;
1836 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
1838 GAS_normalization_preference_client_disconnect (client);
1842 * Change the preference for a peer
1844 * @param handle the address handle
1845 * @param client the client sending this request
1846 * @param peer the peer id
1847 * @param kind the preference kind to change
1848 * @param score_abs the new preference score
1851 GAS_addresses_preference_change (struct GAS_Addresses_Handle *handle,
1852 void *client, const struct GNUNET_PeerIdentity *peer,
1853 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1855 struct GAS_Addresses_Preference_Clients * pc;
1856 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1857 "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1858 GNUNET_i2s (peer), client);
1860 if (GNUNET_NO == handle->running)
1864 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1867 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1868 "Received `%s' for unknown peer `%s' from client %p\n",
1869 "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1873 if (NULL == find_preference_client (handle, client))
1875 pc = GNUNET_new (struct GAS_Addresses_Preference_Clients);
1876 pc->client = client;
1877 GNUNET_CONTAINER_DLL_insert (handle->preference_clients_head,
1878 handle->preference_clients_tail, pc);
1879 handle->pref_clients ++;
1880 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
1883 handle->env.sf.s_bulk_start (handle->solver);
1884 /* Tell normalization about change, normalization will call callback if preference changed */
1885 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1886 handle->env.sf.s_bulk_stop (handle->solver);
1890 * Change the preference for a peer
1892 * @param handle the address handle
1893 * @param application the client sending this request
1894 * @param peer the peer id
1895 * @param scope the time interval for this feedback: [now - scope .. now]
1896 * @param kind the preference kind to change
1897 * @param score_abs the new preference score
1900 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1901 void *application, const struct GNUNET_PeerIdentity *peer,
1902 const struct GNUNET_TIME_Relative scope,
1903 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1905 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1906 "Received `%s' for peer `%s' for client %p\n", "PREFERENCE FEEDBACK",
1907 GNUNET_i2s (peer), application);
1909 if (GNUNET_NO == handle->running)
1913 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1916 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1917 "Received `%s' for unknown peer `%s' from client %p\n",
1918 "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1922 handle->env.sf.s_feedback (handle->solver, application, peer, scope, kind,
1927 * Load quotas for networks from configuration
1929 * @param cfg configuration handle
1930 * @param out_dest where to write outbound quotas
1931 * @param in_dest where to write inbound quotas
1932 * @param dest_length length of inbound and outbound arrays
1933 * @return number of networks loaded
1936 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1937 unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1939 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1940 char * entry_in = NULL;
1941 char * entry_out = NULL;
1942 char * quota_out_str;
1943 char * quota_in_str;
1947 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1951 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1952 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1956 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
1960 if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
1962 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1965 if ((GNUNET_NO == res)
1967 == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
1970 if ((GNUNET_NO == res)
1972 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
1976 if (GNUNET_NO == res)
1978 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1979 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1980 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1981 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1985 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1986 _("Outbound quota configure for network `%s' is %llu\n"),
1987 network_str[c], out_dest[c]);
1989 GNUNET_free(quota_out_str);
1993 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1994 _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1995 network_str[c], GNUNET_ATS_DefaultBandwidth);
1996 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2001 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
2005 if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
2007 in_dest[c] = GNUNET_ATS_MaxBandwidth;
2010 if ((GNUNET_NO == res)
2012 == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
2014 if ((GNUNET_NO == res)
2016 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
2020 if (GNUNET_NO == res)
2022 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2023 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
2024 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
2025 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
2029 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2030 _("Inbound quota configured for network `%s' is %llu\n"),
2031 network_str[c], in_dest[c]);
2033 GNUNET_free(quota_in_str);
2037 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2038 _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
2039 network_str[c], GNUNET_ATS_DefaultBandwidth);
2040 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
2042 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2043 "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c],
2044 in_dest[c], out_dest[c]);
2045 GNUNET_free(entry_out);
2046 GNUNET_free(entry_in);
2048 return GNUNET_ATS_NetworkTypeCount;
2052 * Callback for solver to notify about assignment changes
2054 * @param cls the GAS_Addresses_Handle
2055 * @param address the address with changes
2058 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
2060 struct GAS_Addresses_Handle *handle = cls;
2061 struct GAS_Addresses_Suggestion_Requests *cur;
2063 GNUNET_assert(handle != NULL);
2064 GNUNET_assert(address != NULL);
2066 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2067 "Bandwidth assignment changed for peer %s \n",
2068 GNUNET_i2s (&address->peer));
2070 /* Notify performance clients about changes to address */
2071 GAS_performance_notify_all_clients (&address->peer, address->plugin,
2072 address->addr, address->addr_len, address->session_id, address->atsi,
2073 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
2074 cur = handle->pending_requests_head;
2077 if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
2078 break; /* we have an address request pending*/
2083 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2084 "Nobody is interested in peer `%s' :(\n",
2085 GNUNET_i2s (&address->peer));
2089 if ((0 == ntohl (address->assigned_bw_in.value__))
2090 && (0 == ntohl (address->assigned_bw_out.value__)))
2092 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2093 "Telling transport to disconnect peer `%s'\n",
2094 GNUNET_i2s (&address->peer));
2098 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2099 "Sending bandwidth update for peer `%s': %u %u\n",
2100 GNUNET_i2s (&address->peer),
2101 (unsigned int) ntohl (address->assigned_bw_out.value__),
2102 (unsigned int) ntohl (address->assigned_bw_out.value__));
2105 /* *Notify scheduling clients about suggestion */
2106 GAS_scheduling_transmit_address_suggestion (&address->peer, address->plugin,
2107 address->addr, address->addr_len, address->local_address_info,
2108 address->session_id, address->atsi,
2109 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
2114 * Initialize address subsystem. The addresses subsystem manages the addresses
2115 * known and current performance information. It has a solver component
2116 * responsible for the resource allocation. It tells the solver about changes
2117 * and receives updates when the solver changes the resource allocation.
2119 * @param cfg configuration to use
2120 * @param stats the statistics handle to use
2121 * @return an address handle
2123 struct GAS_Addresses_Handle *
2124 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
2125 const struct GNUNET_STATISTICS_Handle *stats)
2127 struct GAS_Addresses_Handle *ah;
2128 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
2129 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
2134 ah = GNUNET_new (struct GAS_Addresses_Handle);
2135 ah->running = GNUNET_NO;
2137 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
2138 /* Initialize the addresses database */
2139 ah->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
2140 ah->pref_clients = 0;
2141 GNUNET_assert(NULL != ah->addresses);
2143 /* Figure out configured solution method */
2145 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
2147 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2148 "No resource assignment method configured, using proportional approach\n");
2149 ah->ats_mode = MODE_PROPORTIONAL;
2153 for (c = 0; c < strlen (mode_str); c++)
2154 mode_str[c] = toupper (mode_str[c]);
2155 if (0 == strcmp (mode_str, "PROPORTIONAL"))
2156 ah->ats_mode = MODE_PROPORTIONAL;
2157 else if (0 == strcmp (mode_str, "MLP"))
2159 ah->ats_mode = MODE_MLP;
2161 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2162 "Assignment method `%s' configured, but GLPK is not available, please install \n",
2164 ah->ats_mode = MODE_PROPORTIONAL;
2167 else if (0 == strcmp (mode_str, "RIL"))
2168 ah->ats_mode = MODE_RIL;
2171 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2172 "Invalid resource assignment method `%s' configured, using proportional approach\n",
2174 ah->ats_mode = MODE_PROPORTIONAL;
2176 GNUNET_free(mode_str);
2179 load_quotas (cfg, quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount);
2180 ah->env.info_cb = &solver_info_cb;
2181 ah->env.info_cb_cls = ah;
2182 ah->env.bandwidth_changed_cb = &bandwidth_changed_cb;
2183 ah->env.bw_changed_cb_cls = ah;
2184 ah->env.get_preferences = &get_preferences_cb;
2185 ah->env.get_preference_cls = ah;
2186 ah->env.get_property = &get_property_cb;
2187 ah->env.get_property_cls = ah;
2189 ah->env.stats = stats;
2190 ah->env.addresses = ah->addresses;
2192 ah->env.network_count = GNUNET_ATS_NetworkTypeCount;
2193 int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
2194 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
2196 ah->env.networks[c] = networks[c];
2197 ah->env.out_quota[c] = quotas_out[c];
2198 ah->env.in_quota[c] = quotas_in[c];
2201 switch (ah->ats_mode) {
2202 case MODE_PROPORTIONAL:
2203 plugin_short = "proportional";
2206 plugin_short = "mlp";
2209 plugin_short = "ril";
2212 plugin_short = NULL;
2215 GNUNET_asprintf (&ah->plugin, "libgnunet_plugin_ats_%s", plugin_short);
2216 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s '`%s'\n"), plugin_short, ah->plugin);
2217 if (NULL == (ah->solver = GNUNET_PLUGIN_load (ah->plugin, &ah->env)))
2219 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver `%s'!\n"), ah->plugin);
2223 GNUNET_assert (NULL != ah->env.sf.s_add);
2224 GNUNET_assert (NULL != ah->env.sf.s_address_update_inuse);
2225 GNUNET_assert (NULL != ah->env.sf.s_address_update_property);
2226 GNUNET_assert (NULL != ah->env.sf.s_address_update_session);
2227 GNUNET_assert (NULL != ah->env.sf.s_address_update_network);
2228 GNUNET_assert (NULL != ah->env.sf.s_get);
2229 GNUNET_assert (NULL != ah->env.sf.s_get_stop);
2230 GNUNET_assert (NULL != ah->env.sf.s_pref);
2231 GNUNET_assert (NULL != ah->env.sf.s_feedback);
2232 GNUNET_assert (NULL != ah->env.sf.s_del);
2233 GNUNET_assert (NULL != ah->env.sf.s_bulk_start);
2234 GNUNET_assert (NULL != ah->env.sf.s_bulk_stop);
2237 GAS_normalization_start (&normalized_preference_changed_cb, ah,
2238 &normalized_property_changed_cb, ah);
2240 if (NULL == ah->solver)
2242 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver!\n"));
2246 /* up and running */
2247 ah->running = GNUNET_YES;
2249 GNUNET_STATISTICS_set (ah->stat, "# addresses",
2250 GNUNET_CONTAINER_multipeermap_size (ah->addresses), GNUNET_NO);
2256 * Destroy all addresses iterator
2259 * @param key peer identity (unused)
2260 * @param value the 'struct ATS_Address' to free
2261 * @return #GNUNET_OK (continue to iterate)
2264 destroy_all_address_it (void *cls,
2265 const struct GNUNET_PeerIdentity *key,
2268 struct GAS_Addresses_Handle *handle = cls;
2269 struct ATS_Address *aa = value;
2272 GNUNET_assert(GNUNET_YES ==
2273 GNUNET_CONTAINER_multipeermap_remove (handle->addresses, key, value));
2275 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
2284 * Remove all addresses
2286 * @param handle the address handle to use
2289 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
2291 if (GNUNET_NO == handle->running)
2294 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Destroying all addresses\n");
2295 handle->env.sf.s_bulk_start (handle->solver);
2296 if (handle->addresses != NULL )
2297 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2298 &destroy_all_address_it,
2300 handle->env.sf.s_bulk_start (handle->solver);
2305 * Shutdown address subsystem.
2307 * @param handle the address handle to shutdown
2310 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
2312 struct GAS_Addresses_Suggestion_Requests *cur;
2313 struct GAS_Addresses_Preference_Clients *pcur;
2315 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Shutting down addresses\n");
2316 GNUNET_assert(NULL != handle);
2317 GAS_addresses_destroy_all (handle);
2318 handle->running = GNUNET_NO;
2319 GNUNET_CONTAINER_multipeermap_destroy (handle->addresses);
2320 handle->addresses = NULL;
2321 while (NULL != (cur = handle->pending_requests_head))
2323 GNUNET_CONTAINER_DLL_remove(handle->pending_requests_head, handle->pending_requests_tail, cur);
2327 while (NULL != (pcur = handle->preference_clients_head))
2329 GNUNET_CONTAINER_DLL_remove (handle->preference_clients_head,
2330 handle->preference_clients_tail, pcur);
2331 GNUNET_assert (handle->pref_clients > 0);
2332 handle->pref_clients --;
2333 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
2337 GNUNET_PLUGIN_unload (handle->plugin, handle->solver);
2338 GNUNET_free (handle->plugin);
2339 GNUNET_free(handle);
2340 /* Stop configured solution method */
2341 GAS_normalization_stop ();
2345 struct PeerIteratorContext
2347 GNUNET_ATS_Peer_Iterator it;
2349 struct GNUNET_CONTAINER_MultiPeerMap *peers_returned;
2354 * Iterator to iterate over all peers
2356 * @param cls a PeerIteratorContext
2357 * @param key the peer id
2358 * @param value the ATS_address
2359 * @return #GNUNET_OK to continue
2363 const struct GNUNET_PeerIdentity *key,
2366 struct PeerIteratorContext *ip_ctx = cls;
2369 GNUNET_CONTAINER_multipeermap_contains (ip_ctx->peers_returned, key))
2371 GNUNET_CONTAINER_multipeermap_put (ip_ctx->peers_returned, key, NULL,
2372 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2373 ip_ctx->it (ip_ctx->it_cls, key);
2380 * Return information all peers currently known to ATS
2382 * @param handle the address handle to use
2383 * @param p_it the iterator to call for every peer
2384 * @param p_it_cls the closure for the iterator
2387 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
2388 GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
2390 struct PeerIteratorContext ip_ctx;
2395 GNUNET_assert(NULL != handle->addresses);
2397 size = GNUNET_CONTAINER_multipeermap_size (handle->addresses);
2401 ip_ctx.it_cls = p_it_cls;
2402 ip_ctx.peers_returned = GNUNET_CONTAINER_multipeermap_create (size,
2404 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2407 GNUNET_CONTAINER_multipeermap_destroy (ip_ctx.peers_returned);
2409 p_it (p_it_cls, NULL );
2412 struct PeerInfoIteratorContext
2414 GNUNET_ATS_PeerInfo_Iterator it;
2420 * Iterator to iterate over a peer's addresses
2422 * @param cls a `struct PeerInfoIteratorContext`
2423 * @param key the peer id
2424 * @param value the `struct ATS_address`
2425 * @return #GNUNET_OK to continue
2428 peerinfo_it (void *cls,
2429 const struct GNUNET_PeerIdentity *key,
2432 struct PeerInfoIteratorContext *pi_ctx = cls;
2433 struct ATS_Address *addr = value;
2435 if (NULL != pi_ctx->it)
2437 pi_ctx->it (pi_ctx->it_cls, &addr->peer, addr->plugin, addr->addr,
2438 addr->addr_len, addr->active, addr->atsi, addr->atsi_count,
2439 addr->assigned_bw_out, addr->assigned_bw_in);
2446 * Return information all peers currently known to ATS
2448 * @param handle the address handle to use
2449 * @param peer the respective peer
2450 * @param pi_it the iterator to call for every peer
2451 * @param pi_it_cls the closure for the iterator
2454 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
2455 const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it,
2458 struct PeerInfoIteratorContext pi_ctx;
2459 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
2461 GNUNET_assert(NULL != peer);
2462 GNUNET_assert(NULL != handle->addresses);
2464 return; /* does not make sense without callback */
2466 zero_bw = GNUNET_BANDWIDTH_value_init (0);
2468 pi_ctx.it_cls = pi_it_cls;
2470 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
2472 &peerinfo_it, &pi_ctx);
2475 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw,
2480 /* end of gnunet-service-ats_addresses.c */