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 local_address_info additional local info for the address
476 * @param session_id session
477 * @return the ATS_Address
479 static struct ATS_Address *
480 create_address (const struct GNUNET_PeerIdentity *peer,
481 const char *plugin_name,
482 const void *plugin_addr,
483 size_t plugin_addr_len,
484 uint32_t local_address_info,
487 struct ATS_Address *aa = NULL;
491 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
493 aa->addr_len = plugin_addr_len;
495 memcpy (&aa[1], plugin_addr, plugin_addr_len);
496 aa->plugin = GNUNET_strdup (plugin_name);
497 aa->session_id = session_id;
498 aa->local_address_info = local_address_info;
499 aa->active = GNUNET_NO;
500 aa->used = GNUNET_NO;
501 aa->solver_information = NULL;
504 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
505 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
507 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
509 aa->atsin[c1].avg_queue_index = 0;
510 for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
511 aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
517 struct CompareAddressContext
519 const struct ATS_Address *search;
521 /* exact_address != NULL if address and session is equal */
522 struct ATS_Address *exact_address;
523 /* exact_address != NULL if address and session is 0 */
524 struct ATS_Address *base_address;
531 * @param cls a CompareAddressContext containin the source address
533 * @param value the address to compare with
534 * @return #GNUNET_YES to continue, #GNUNET_NO if address is founce
537 compare_address_it (void *cls,
538 const struct GNUNET_PeerIdentity *key,
541 struct CompareAddressContext *cac = cls;
542 struct ATS_Address *aa = value;
544 /* Find an matching exact address:
547 * aa->addr_len == cac->search->addr_len
548 * aa->plugin == cac->search->plugin
549 * aa->addr == cac->search->addr
550 * aa->session == cac->search->session
552 * return as exact address
554 if ((aa->addr_len == cac->search->addr_len)
555 && (0 == strcmp (aa->plugin, cac->search->plugin)))
557 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
558 && (aa->session_id == cac->search->session_id))
559 cac->exact_address = aa;
562 /* Find an matching base address:
566 * aa->session_id == 0
569 * aa->addr_len == cac->search->addr_len
570 * aa->plugin == cac->search->plugin
571 * aa->addr == cac->search->addr
573 * return as base address
575 if ((aa->addr_len == cac->search->addr_len)
576 && (0 == strcmp (aa->plugin, cac->search->plugin)))
578 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
579 && (aa->session_id == 0))
580 cac->base_address = aa;
583 /* Find an matching exact address based on session:
587 * cac->search->addr_len == 0
590 * aa->plugin == cac->search->plugin
591 * aa->session_id == cac->search->session_id
593 * return as exact address
595 if (0 == cac->search->addr_len)
597 if ((0 == strcmp (aa->plugin, cac->search->plugin))
598 && (aa->session_id == cac->search->session_id))
599 cac->exact_address = aa;
602 if (cac->exact_address == NULL )
603 return GNUNET_YES; /* Continue iteration to find exact address */
605 return GNUNET_NO; /* Stop iteration since we have an exact address */
609 * Find an existing equivalent address record.
610 * Compares by peer identity and network address OR by session ID
611 * (one of the two must match).
613 * @param handle the address handle
614 * @param peer peer to lookup addresses for
615 * @param addr existing address record
616 * @return existing address record, NULL for none
619 find_equivalent_address (struct GAS_Addresses_Handle *handle,
620 const struct GNUNET_PeerIdentity *peer, const struct ATS_Address *addr)
622 struct CompareAddressContext cac;
624 cac.exact_address = NULL;
625 cac.base_address = NULL;
627 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
629 &compare_address_it, &cac);
631 if (cac.exact_address == NULL)
632 return cac.base_address;
633 return cac.exact_address;
638 * Find the exact address
640 * @param handle the address handle to use
642 * @param plugin_name transport plugin name
643 * @param plugin_addr plugin address
644 * @param plugin_addr_len length of the plugin address
645 * @param local_address_info the local address for the address
646 * @param session_id session id, can be 0
647 * @return an ATS_address or NULL
650 static struct ATS_Address *
651 find_exact_address (struct GAS_Addresses_Handle *handle,
652 const struct GNUNET_PeerIdentity *peer,
653 const char *plugin_name,
654 const void *plugin_addr,
655 size_t plugin_addr_len,
656 uint32_t local_address_info,
659 struct ATS_Address *aa;
660 struct ATS_Address *ea;
662 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
663 local_address_info, session_id);
665 /* Get existing address or address with session == 0 */
666 ea = find_equivalent_address (handle, peer, aa);
670 else if (ea->session_id != session_id)
676 * Function allowing the solver to obtain normalized preference
680 * @param id the peer to return the normalized properties for
681 * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
684 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
686 return GAS_normalization_get_preferences_by_peer (id);
690 * Function allowing the solver to obtain normalized property
691 * values for an address from solver
694 * @param address the address
695 * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
698 get_property_cb (void *cls, const struct ATS_Address *address)
700 return GAS_normalization_get_properties ((struct ATS_Address *) address);
704 * Extract an ATS performance info from an address
706 * @param address the address
707 * @param type the type to extract in HBO
708 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
711 get_performance_info (struct ATS_Address *address, uint32_t type)
714 GNUNET_assert(NULL != address);
716 if ((NULL == address->atsi) || (0 == address->atsi_count))
717 return GNUNET_ATS_VALUE_UNDEFINED;
719 for (c1 = 0; c1 < address->atsi_count; c1++)
721 if (ntohl (address->atsi[c1].type) == type)
722 return ntohl (address->atsi[c1].value);
724 return GNUNET_ATS_VALUE_UNDEFINED;
728 * Add a new address for a peer.
730 * @param handle the address handle to use
732 * @param plugin_name transport plugin name
733 * @param plugin_addr plugin address
734 * @param plugin_addr_len length of the plugin address
735 * @param local_address_info the local address for the address
736 * @param session_id session id, can be 0
737 * @param atsi performance information for this address
738 * @param atsi_count number of performance information contained
741 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
742 const struct GNUNET_PeerIdentity *peer,
743 const char *plugin_name,
744 const void *plugin_addr,
745 size_t plugin_addr_len,
746 uint32_t local_address_info,
748 const struct GNUNET_ATS_Information *atsi,
751 struct ATS_Address *new_address;
752 struct ATS_Address *existing_address;
753 struct GNUNET_ATS_Information *atsi_delta;
754 uint32_t atsi_delta_count;
756 uint32_t previous_session;
759 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
760 "ADDRESS ADD", GNUNET_i2s (peer));
762 if (GNUNET_NO == handle->running)
765 GNUNET_assert(NULL != handle->addresses);
767 new_address = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
768 local_address_info, session_id);
770 disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
772 GNUNET_free_non_null(atsi_delta);
773 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
774 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
775 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
777 /* Get existing address or address with session == 0 */
778 existing_address = find_equivalent_address (handle, peer, new_address);
779 if (existing_address == NULL )
781 /* Add a new address */
782 new_address->t_added = GNUNET_TIME_absolute_get();
783 new_address->t_last_activity = GNUNET_TIME_absolute_get();
785 GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (handle->addresses,
788 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
790 GNUNET_STATISTICS_set (handle->stat, "# addresses",
791 GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
793 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
794 "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
797 plugin_addr_len, session_id,
798 GNUNET_ATS_print_network_type (addr_net));
800 /* Tell solver about new address */
801 handle->env.sf.s_add (handle->solver, new_address, addr_net);
803 handle->env.sf.s_bulk_start (handle->solver);
804 GAS_normalization_normalize_property (handle->addresses, new_address, atsi,
806 handle->env.sf.s_bulk_stop (handle->solver);
808 /* Notify performance clients about new address */
809 GAS_performance_notify_all_clients (&new_address->peer, new_address->plugin,
810 new_address->addr, new_address->addr_len, new_address->session_id,
811 new_address->atsi, new_address->atsi_count,
812 new_address->assigned_bw_out, new_address->assigned_bw_in);
816 /* We have an existing address we can use, clean up new */
817 GNUNET_free(new_address->plugin);
818 GNUNET_free_non_null(new_address->atsi);
819 GNUNET_free(new_address);
822 if (0 != existing_address->session_id)
824 /* Should not happen */
829 addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
830 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
831 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
833 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
834 "Found existing address for peer `%s' %p with new session %u in network %s\n",
835 GNUNET_i2s (peer), existing_address, session_id,
836 GNUNET_ATS_print_network_type (addr_net));
837 /* We have an address without an session, update this address */
838 existing_address->t_added = GNUNET_TIME_absolute_get();
839 existing_address->t_last_activity = GNUNET_TIME_absolute_get();
841 atsi_delta_count = 0;
843 == disassemble_ats_information (existing_address, atsi, atsi_count,
844 &atsi_delta, &atsi_delta_count))
846 /* Notify performance clients about properties */
847 GAS_performance_notify_all_clients (&existing_address->peer,
848 existing_address->plugin, existing_address->addr,
849 existing_address->addr_len, existing_address->session_id,
850 existing_address->atsi, existing_address->atsi_count,
851 existing_address->assigned_bw_out, existing_address->assigned_bw_in);
853 for (c1 = 0; c1 < atsi_delta_count; c1++)
855 if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
856 && (addr_net != ntohl (atsi_delta[c1].value)))
858 /* Network type changed */
859 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
860 "Address for peer `%s' %p changed from network %s to %s\n",
861 GNUNET_i2s (peer), existing_address,
862 GNUNET_ATS_print_network_type (addr_net),
863 GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
864 handle->env.sf.s_address_update_network (handle->solver, existing_address,
865 ntohl (atsi_delta[c1].value),
866 get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
867 addr_net = get_performance_info (existing_address,
868 GNUNET_ATS_NETWORK_TYPE);
871 /* Notify solver about update with atsi information and session */
872 handle->env.sf.s_bulk_start (handle->solver);
873 GAS_normalization_normalize_property (handle->addresses, existing_address,
875 handle->env.sf.s_bulk_stop (handle->solver);
877 GNUNET_free_non_null(atsi_delta);
879 /* Notify solver about new session */
880 if (existing_address->session_id == session_id)
881 return; /* possible, can both be 0 since address is revalidated */
883 previous_session = existing_address->session_id;
884 existing_address->session_id = session_id;
885 handle->env.sf.s_address_update_session (handle->solver, existing_address,
886 previous_session, session_id);
888 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
889 "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
890 GNUNET_i2s (peer), existing_address, existing_address->addr_len,
891 session_id, GNUNET_ATS_print_network_type (addr_net));
895 * Update an address with a session or performance information for a peer.
897 * If an address was added without a session it will be updated with the
900 * @param handle the address handle to use
902 * @param plugin_name transport plugin name
903 * @param plugin_addr plugin address
904 * @param plugin_addr_len length of the plugin address
905 * @param local_address_info the local address for the address
906 * @param session_id session id, can be 0
907 * @param atsi performance information for this address
908 * @param atsi_count number of performance information contained
911 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
912 const struct GNUNET_PeerIdentity *peer,
913 const char *plugin_name,
914 const void *plugin_addr,
915 size_t plugin_addr_len,
916 uint32_t local_address_info,
918 const struct GNUNET_ATS_Information *atsi,
921 struct ATS_Address *aa;
922 struct GNUNET_ATS_Information *atsi_delta;
923 uint32_t atsi_delta_count;
924 uint32_t prev_session;
927 if (GNUNET_NO == handle->running)
930 GNUNET_assert(NULL != handle->addresses);
932 /* Get existing address */
933 aa = find_exact_address (handle, peer, plugin_name, plugin_addr,
934 plugin_addr_len, local_address_info, session_id);
937 if (NULL == aa->solver_information)
940 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s' address \n",
941 "ADDRESS UPDATE", GNUNET_i2s (peer), aa);
944 aa->t_last_activity = GNUNET_TIME_absolute_get();
945 if (session_id != aa->session_id)
947 /* Session changed */
948 prev_session = aa->session_id;
949 aa->session_id = session_id;
950 handle->env.sf.s_address_update_session (handle->solver, aa, prev_session,
955 atsi_delta_count = 0;
957 == disassemble_ats_information (aa, atsi, atsi_count, &atsi_delta,
960 /* ATS properties changed */
961 for (c1 = 0; c1 < atsi_delta_count; c1++)
963 if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
965 /* Network type changed */
966 handle->env.sf.s_address_update_network (handle->solver, aa,
967 ntohl (atsi_delta[c1].value),
968 get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
972 /* Notify performance clients about updated address */
973 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr,
974 aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
975 aa->assigned_bw_out, aa->assigned_bw_in);
977 handle->env.sf.s_bulk_start (handle->solver);
978 GAS_normalization_normalize_property (handle->addresses, aa, atsi,
980 handle->env.sf.s_bulk_stop (handle->solver);
982 GNUNET_free_non_null(atsi_delta);
985 struct DestroyContext
987 struct ATS_Address *aa;
989 struct GAS_Addresses_Handle *handle;
992 * GNUNET_NO : full address
993 * GNUNET_YES : just session
1001 * If session != 0, just the session is deleted, the address itself still exists
1002 * If session == 0, remove full address
1003 * If session == 0 and addrlen == 0, destroy inbound address
1007 * @param value the 'struct ATS_Address'
1008 * @return GNUNET_OK (continue to iterate)
1011 destroy_by_session_id (void *cls,
1012 const struct GNUNET_PeerIdentity *key,
1015 struct DestroyContext *dc = cls;
1016 struct GAS_Addresses_Handle *handle = dc->handle;
1017 const struct ATS_Address *des = dc->aa;
1018 struct ATS_Address *aa = value;
1021 0 == memcmp (&aa->peer, &des->peer, sizeof(struct GNUNET_PeerIdentity)));
1023 if (des->session_id == 0)
1025 /* Session == 0, remove full address */
1026 if ((0 == strcmp (des->plugin, aa->plugin))
1027 && (aa->addr_len == des->addr_len)
1028 && (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
1031 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1032 "Deleting full address for peer `%s' session %u %p\n",
1033 GNUNET_i2s (&aa->peer), aa->session_id, aa);
1035 /* Notify solver about deletion */
1037 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1040 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1042 dc->result = GNUNET_NO;
1043 return GNUNET_OK; /* Continue iteration */
1048 /* Session != 0, just remove session */
1049 if (aa->session_id != des->session_id)
1050 return GNUNET_OK; /* irrelevant */
1052 if ((aa->session_id != 0) && (0 != strcmp (des->plugin, aa->plugin)))
1054 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1055 "Different plugins during removal: `%s' vs `%s' \n", des->plugin,
1060 if (GNUNET_HELLO_ADDRESS_INFO_INBOUND ==
1061 (aa->local_address_info && GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1063 /* Inbound connection died, delete full address */
1064 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1065 "Deleting inbound address for peer `%s': `%s' session %u\n",
1066 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
1068 /* Notify solver about deletion */
1070 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1072 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1074 dc->result = GNUNET_NO;
1075 return GNUNET_OK; /* Continue iteration */
1080 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1081 "Deleting session for peer `%s': `%s' %u\n", GNUNET_i2s (&aa->peer),
1082 aa->plugin, aa->session_id);
1083 /* Notify solver to delete session */
1084 handle->env.sf.s_del (handle->solver, aa, GNUNET_YES);
1086 aa->active = GNUNET_NO;
1095 * Remove an address or just a session for a peer.
1097 * @param handle the address handle to use
1099 * @param plugin_name transport plugin name
1100 * @param plugin_addr plugin address
1101 * @param plugin_addr_len length of the plugin address
1102 * @param local_address_info the local address for the address
1103 * @param session_id session id, can be 0
1106 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
1107 const struct GNUNET_PeerIdentity *peer,
1108 const char *plugin_name,
1109 const void *plugin_addr,
1110 size_t plugin_addr_len,
1111 uint32_t local_address_info,
1112 uint32_t session_id)
1114 struct ATS_Address *ea;
1115 struct DestroyContext dc;
1116 if (GNUNET_NO == handle->running)
1119 /* Get existing address */
1120 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1121 plugin_addr_len, local_address_info, session_id);
1124 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1125 "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
1126 GNUNET_i2s (peer), plugin_name, session_id);
1130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1131 "Received `%s' for peer `%s' address %p session %u\n", "ADDRESS DESTROY",
1132 GNUNET_i2s (peer), ea, session_id);
1134 GNUNET_break(0 < strlen (plugin_name));
1136 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
1137 local_address_info, session_id);
1139 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1141 &destroy_by_session_id, &dc);
1142 GNUNET_STATISTICS_set (handle->stat, "# addresses",
1143 GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
1144 free_address (dc.aa);
1148 * Notification about active use of an address.
1149 * in_use == GNUNET_YES:
1150 * This address is used to maintain an active connection with a peer.
1151 * in_use == GNUNET_NO:
1152 * This address is no longer used to maintain an active connection with a peer.
1154 * Note: can only be called with in_use == GNUNET_NO if called with GNUNET_YES
1157 * @param handle the address handle to use
1159 * @param plugin_name transport plugin name
1160 * @param plugin_addr plugin address
1161 * @param plugin_addr_len length of the plugin address
1162 * @param local_address_info the local address for the address
1163 * @param session_id session id, can be 0
1164 * @param in_use GNUNET_YES if GNUNET_NO
1165 * @return GNUNET_SYSERR on failure (address unknown ...)
1168 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
1169 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1170 const void *plugin_addr, size_t plugin_addr_len,
1171 uint32_t local_address_info,
1172 uint32_t session_id,
1175 struct ATS_Address *ea;
1176 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1177 "ADDRESS IN USE", GNUNET_i2s (peer));
1179 if (GNUNET_NO == handle->running)
1180 return GNUNET_SYSERR;
1182 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1183 plugin_addr_len, local_address_info, session_id);
1186 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1187 "Trying to set unknown address `%s' `%s' `%u' to %s \n",
1188 GNUNET_i2s (peer), plugin_name, session_id,
1189 (GNUNET_NO == in_use) ? "NO" : "YES");
1191 return GNUNET_SYSERR;
1193 if (ea->used == in_use)
1196 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1197 "Address in use called multiple times for peer `%s': %s -> %s \n",
1198 GNUNET_i2s (peer), (GNUNET_NO == ea->used) ? "NO" : "YES",
1199 (GNUNET_NO == in_use) ? "NO" : "YES");
1200 return GNUNET_SYSERR;
1203 /* Tell solver about update */
1205 ea->t_last_activity = GNUNET_TIME_absolute_get();
1206 handle->env.sf.s_address_update_inuse (handle->solver, ea, ea->used);
1211 * Cancel address suggestions for a peer
1213 * @param handle the address handle
1214 * @param peer the peer id
1217 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
1218 const struct GNUNET_PeerIdentity *peer)
1220 struct GAS_Addresses_Suggestion_Requests *cur = handle->pending_requests_head;
1222 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received request: `%s' for peer %s\n",
1223 "request_address_cancel", GNUNET_i2s (peer));
1227 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1234 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1235 "No address requests pending for peer `%s', cannot remove!\n",
1239 handle->env.sf.s_get_stop (handle->solver, peer);
1240 GAS_addresses_handle_backoff_reset (handle, peer);
1241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Removed request pending for peer `%s\n",
1243 GNUNET_CONTAINER_DLL_remove(handle->pending_requests_head, handle->pending_requests_tail, cur);
1249 * Request address suggestions for a peer
1251 * @param handle the address handle
1252 * @param peer the peer id
1255 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
1256 const struct GNUNET_PeerIdentity *peer)
1258 struct GAS_Addresses_Suggestion_Requests *cur = handle->pending_requests_head;
1259 struct ATS_Address *aa;
1261 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1262 "REQUEST ADDRESS", GNUNET_i2s (peer));
1264 if (GNUNET_NO == handle->running)
1268 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1269 break; /* already suggesting */
1274 cur = GNUNET_new (struct GAS_Addresses_Suggestion_Requests);
1276 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1277 "Adding new address suggestion request for `%s'\n",
1279 GNUNET_CONTAINER_DLL_insert(handle->pending_requests_head, handle->pending_requests_tail, cur);
1282 /* Get prefered address from solver */
1283 aa = (struct ATS_Address *) handle->env.sf.s_get (handle->solver, peer);
1286 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1291 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1292 aa, GNUNET_i2s (peer));
1294 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
1295 aa->addr_len, aa->local_address_info, aa->session_id,
1296 aa->atsi, aa->atsi_count,
1297 aa->assigned_bw_out, aa->assigned_bw_in);
1299 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1300 ATS_BLOCKING_DELTA);
1301 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1302 aa->block_interval);
1304 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1305 "Address %p ready for suggestion, block interval now %llu \n", aa,
1306 aa->block_interval);
1310 * Iterator to reset address blocking
1312 * @param cls not used
1313 * @param key the peer
1314 * @param value the address to reset
1315 * @return #GNUNET_OK to continue
1318 reset_address_it (void *cls,
1319 const struct GNUNET_PeerIdentity *key,
1322 struct ATS_Address *aa = value;
1324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1325 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1326 GNUNET_i2s (&aa->peer),
1328 aa->block_interval);
1329 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1330 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1336 * Reset suggestion backoff for a peer
1338 * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1339 * reset using this function
1341 * @param handle the address handle
1342 * @param peer the peer id
1345 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1346 const struct GNUNET_PeerIdentity *peer)
1348 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1349 "RESET BACKOFF", GNUNET_i2s (peer));
1352 GNUNET_SYSERR != GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1354 &reset_address_it, NULL));
1359 * Solver information callback
1361 * @param cls the closure
1362 * @param op the operation
1363 * @param stat operation status
1364 * @param add additional information
1368 solver_info_cb (void *cls,
1369 enum GAS_Solver_Operation op,
1370 enum GAS_Solver_Status stat,
1371 enum GAS_Solver_Additional_Information add)
1377 add_info = "GAS_INFO_NONE";
1380 add_info = "GAS_INFO_MLP_FULL";
1382 case GAS_INFO_UPDATED:
1383 add_info = "GAS_INFO_MLP_UPDATED";
1385 case GAS_INFO_PROP_ALL:
1386 add_info = "GAS_INFO_PROP_ALL";
1388 case GAS_INFO_PROP_SINGLE:
1389 add_info = "GAS_INFO_PROP_SINGLE";
1392 add_info = "INVALID";
1397 case GAS_OP_SOLVE_START:
1398 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1399 "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
1400 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
1402 case GAS_OP_SOLVE_STOP:
1403 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1404 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
1405 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
1408 case GAS_OP_SOLVE_SETUP_START:
1409 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1410 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
1411 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1414 case GAS_OP_SOLVE_SETUP_STOP:
1415 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1416 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
1417 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1420 case GAS_OP_SOLVE_MLP_LP_START:
1421 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1422 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
1423 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1425 case GAS_OP_SOLVE_MLP_LP_STOP:
1426 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1427 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
1428 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1431 case GAS_OP_SOLVE_MLP_MLP_START:
1432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1433 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
1434 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1436 case GAS_OP_SOLVE_MLP_MLP_STOP:
1437 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1438 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
1439 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1441 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
1442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1443 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
1444 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1446 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
1447 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1448 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
1449 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1458 * The preference changed for a peer
1460 * @param cls the address handle
1461 * @param peer the peer
1462 * @param kind the ATS kind
1463 * @param pref_rel the new relative preference value
1466 normalized_preference_changed_cb (void *cls,
1467 const struct GNUNET_PeerIdentity *peer, enum GNUNET_ATS_PreferenceKind kind,
1470 GNUNET_assert(NULL != cls);
1471 struct GAS_Addresses_Handle *handle = cls;
1473 /* Tell solver about update */
1474 handle->env.sf.s_pref (handle->solver, peer, kind, pref_rel);
1478 * The relative value for a property changed
1480 * @param cls the address handle
1481 * @param address the peer
1482 * @param type the ATS type
1483 * @param prop_rel the new relative preference value
1486 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
1487 uint32_t type, double prop_rel)
1489 struct GAS_Addresses_Handle *ah = (struct GAS_Addresses_Handle *) cls;
1490 GNUNET_assert(NULL != ah);
1492 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1493 "Normalized property %s for peer `%s' changed to %.3f \n",
1494 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1497 ah->env.sf.s_address_update_property (ah->solver, address, type, 0, prop_rel);
1500 static struct GAS_Addresses_Preference_Clients *
1501 find_preference_client (struct GAS_Addresses_Handle *handle, void *client)
1503 struct GAS_Addresses_Preference_Clients *cur;
1505 for (cur = handle->preference_clients_head; NULL != cur; cur = cur->next)
1507 if (cur->client == client)
1514 * A performance client disconnected
1516 * @param handle address handle
1517 * @param client the client
1521 GAS_addresses_preference_client_disconnect (struct GAS_Addresses_Handle *handle,
1524 struct GAS_Addresses_Preference_Clients * pc;
1525 if (NULL != (pc = find_preference_client (handle, client)))
1527 GNUNET_CONTAINER_DLL_remove (handle->preference_clients_head,
1528 handle->preference_clients_tail, pc);
1530 GNUNET_assert (handle->pref_clients > 0);
1531 handle->pref_clients --;
1532 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
1534 GAS_normalization_preference_client_disconnect (client);
1538 * Change the preference for a peer
1540 * @param handle the address handle
1541 * @param client the client sending this request
1542 * @param peer the peer id
1543 * @param kind the preference kind to change
1544 * @param score_abs the new preference score
1547 GAS_addresses_preference_change (struct GAS_Addresses_Handle *handle,
1548 void *client, const struct GNUNET_PeerIdentity *peer,
1549 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1551 struct GAS_Addresses_Preference_Clients * pc;
1552 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1553 "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1554 GNUNET_i2s (peer), client);
1556 if (GNUNET_NO == handle->running)
1560 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1563 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1564 "Received `%s' for unknown peer `%s' from client %p\n",
1565 "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1569 if (NULL == find_preference_client (handle, client))
1571 pc = GNUNET_new (struct GAS_Addresses_Preference_Clients);
1572 pc->client = client;
1573 GNUNET_CONTAINER_DLL_insert (handle->preference_clients_head,
1574 handle->preference_clients_tail, pc);
1575 handle->pref_clients ++;
1576 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
1579 handle->env.sf.s_bulk_start (handle->solver);
1580 /* Tell normalization about change, normalization will call callback if preference changed */
1581 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1582 handle->env.sf.s_bulk_stop (handle->solver);
1586 * Change the preference for a peer
1588 * @param handle the address handle
1589 * @param application the client sending this request
1590 * @param peer the peer id
1591 * @param scope the time interval for this feedback: [now - scope .. now]
1592 * @param kind the preference kind to change
1593 * @param score_abs the new preference score
1596 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1597 void *application, const struct GNUNET_PeerIdentity *peer,
1598 const struct GNUNET_TIME_Relative scope,
1599 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1601 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1602 "Received `%s' for peer `%s' for client %p\n", "PREFERENCE FEEDBACK",
1603 GNUNET_i2s (peer), application);
1605 if (GNUNET_NO == handle->running)
1609 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1612 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1613 "Received `%s' for unknown peer `%s' from client %p\n",
1614 "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1618 handle->env.sf.s_feedback (handle->solver, application, peer, scope, kind,
1623 * Load quotas for networks from configuration
1625 * @param cfg configuration handle
1626 * @param out_dest where to write outbound quotas
1627 * @param in_dest where to write inbound quotas
1628 * @param dest_length length of inbound and outbound arrays
1629 * @return number of networks loaded
1632 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1633 unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1635 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1636 char * entry_in = NULL;
1637 char * entry_out = NULL;
1638 char * quota_out_str;
1639 char * quota_in_str;
1643 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1647 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1648 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1652 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
1656 if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
1658 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1661 if ((GNUNET_NO == res)
1663 == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
1666 if ((GNUNET_NO == res)
1668 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
1672 if (GNUNET_NO == res)
1674 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1675 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1676 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1677 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1681 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1682 _("Outbound quota configure for network `%s' is %llu\n"),
1683 network_str[c], out_dest[c]);
1685 GNUNET_free(quota_out_str);
1689 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1690 _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1691 network_str[c], GNUNET_ATS_DefaultBandwidth);
1692 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1697 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
1701 if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
1703 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1706 if ((GNUNET_NO == res)
1708 == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1710 if ((GNUNET_NO == res)
1712 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
1716 if (GNUNET_NO == res)
1718 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1719 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1720 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1721 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1725 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1726 _("Inbound quota configured for network `%s' is %llu\n"),
1727 network_str[c], in_dest[c]);
1729 GNUNET_free(quota_in_str);
1733 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1734 _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1735 network_str[c], GNUNET_ATS_DefaultBandwidth);
1736 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1738 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1739 "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c],
1740 in_dest[c], out_dest[c]);
1741 GNUNET_free(entry_out);
1742 GNUNET_free(entry_in);
1744 return GNUNET_ATS_NetworkTypeCount;
1748 * Callback for solver to notify about assignment changes
1750 * @param cls the GAS_Addresses_Handle
1751 * @param address the address with changes
1754 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1756 struct GAS_Addresses_Handle *handle = cls;
1757 struct GAS_Addresses_Suggestion_Requests *cur;
1759 GNUNET_assert(handle != NULL);
1760 GNUNET_assert(address != NULL);
1762 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1763 "Bandwidth assignment changed for peer %s \n",
1764 GNUNET_i2s (&address->peer));
1766 /* Notify performance clients about changes to address */
1767 GAS_performance_notify_all_clients (&address->peer, address->plugin,
1768 address->addr, address->addr_len, address->session_id, address->atsi,
1769 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1770 cur = handle->pending_requests_head;
1773 if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
1774 break; /* we have an address request pending*/
1779 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1780 "Nobody is interested in peer `%s' :(\n",
1781 GNUNET_i2s (&address->peer));
1785 if ((0 == ntohl (address->assigned_bw_in.value__))
1786 && (0 == ntohl (address->assigned_bw_out.value__)))
1788 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1789 "Telling transport to disconnect peer `%s'\n",
1790 GNUNET_i2s (&address->peer));
1794 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1795 "Sending bandwidth update for peer `%s': %u %u\n",
1796 GNUNET_i2s (&address->peer),
1797 (unsigned int) ntohl (address->assigned_bw_out.value__),
1798 (unsigned int) ntohl (address->assigned_bw_out.value__));
1801 /* *Notify scheduling clients about suggestion */
1802 GAS_scheduling_transmit_address_suggestion (&address->peer, address->plugin,
1803 address->addr, address->addr_len, address->local_address_info,
1804 address->session_id, address->atsi,
1805 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1810 * Initialize address subsystem. The addresses subsystem manages the addresses
1811 * known and current performance information. It has a solver component
1812 * responsible for the resource allocation. It tells the solver about changes
1813 * and receives updates when the solver changes the resource allocation.
1815 * @param cfg configuration to use
1816 * @param stats the statistics handle to use
1817 * @return an address handle
1819 struct GAS_Addresses_Handle *
1820 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1821 const struct GNUNET_STATISTICS_Handle *stats)
1823 struct GAS_Addresses_Handle *ah;
1824 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1825 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1830 ah = GNUNET_new (struct GAS_Addresses_Handle);
1831 ah->running = GNUNET_NO;
1833 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1834 /* Initialize the addresses database */
1835 ah->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1836 ah->pref_clients = 0;
1837 GNUNET_assert(NULL != ah->addresses);
1839 /* Figure out configured solution method */
1841 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1843 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1844 "No resource assignment method configured, using proportional approach\n");
1845 ah->ats_mode = MODE_PROPORTIONAL;
1849 for (c = 0; c < strlen (mode_str); c++)
1850 mode_str[c] = toupper (mode_str[c]);
1851 if (0 == strcmp (mode_str, "PROPORTIONAL"))
1852 ah->ats_mode = MODE_PROPORTIONAL;
1853 else if (0 == strcmp (mode_str, "MLP"))
1855 ah->ats_mode = MODE_MLP;
1857 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1858 "Assignment method `%s' configured, but GLPK is not available, please install \n",
1860 ah->ats_mode = MODE_PROPORTIONAL;
1863 else if (0 == strcmp (mode_str, "RIL"))
1864 ah->ats_mode = MODE_RIL;
1867 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1868 "Invalid resource assignment method `%s' configured, using proportional approach\n",
1870 ah->ats_mode = MODE_PROPORTIONAL;
1872 GNUNET_free(mode_str);
1875 load_quotas (cfg, quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount);
1876 ah->env.info_cb = &solver_info_cb;
1877 ah->env.info_cb_cls = ah;
1878 ah->env.bandwidth_changed_cb = &bandwidth_changed_cb;
1879 ah->env.bw_changed_cb_cls = ah;
1880 ah->env.get_preferences = &get_preferences_cb;
1881 ah->env.get_preference_cls = ah;
1882 ah->env.get_property = &get_property_cb;
1883 ah->env.get_property_cls = ah;
1885 ah->env.stats = stats;
1886 ah->env.addresses = ah->addresses;
1888 ah->env.network_count = GNUNET_ATS_NetworkTypeCount;
1889 int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1890 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1892 ah->env.networks[c] = networks[c];
1893 ah->env.out_quota[c] = quotas_out[c];
1894 ah->env.in_quota[c] = quotas_in[c];
1897 switch (ah->ats_mode) {
1898 case MODE_PROPORTIONAL:
1899 plugin_short = "proportional";
1902 plugin_short = "mlp";
1905 plugin_short = "ril";
1908 plugin_short = NULL;
1911 GNUNET_asprintf (&ah->plugin, "libgnunet_plugin_ats_%s", plugin_short);
1912 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s '`%s'\n"), plugin_short, ah->plugin);
1913 if (NULL == (ah->solver = GNUNET_PLUGIN_load (ah->plugin, &ah->env)))
1915 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver `%s'!\n"), ah->plugin);
1919 GNUNET_assert (NULL != ah->env.sf.s_add);
1920 GNUNET_assert (NULL != ah->env.sf.s_address_update_inuse);
1921 GNUNET_assert (NULL != ah->env.sf.s_address_update_property);
1922 GNUNET_assert (NULL != ah->env.sf.s_address_update_session);
1923 GNUNET_assert (NULL != ah->env.sf.s_address_update_network);
1924 GNUNET_assert (NULL != ah->env.sf.s_get);
1925 GNUNET_assert (NULL != ah->env.sf.s_get_stop);
1926 GNUNET_assert (NULL != ah->env.sf.s_pref);
1927 GNUNET_assert (NULL != ah->env.sf.s_feedback);
1928 GNUNET_assert (NULL != ah->env.sf.s_del);
1929 GNUNET_assert (NULL != ah->env.sf.s_bulk_start);
1930 GNUNET_assert (NULL != ah->env.sf.s_bulk_stop);
1933 GAS_normalization_start (&normalized_preference_changed_cb, ah,
1934 &normalized_property_changed_cb, ah);
1936 if (NULL == ah->solver)
1938 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver!\n"));
1942 /* up and running */
1943 ah->running = GNUNET_YES;
1945 GNUNET_STATISTICS_set (ah->stat, "# addresses",
1946 GNUNET_CONTAINER_multipeermap_size (ah->addresses), GNUNET_NO);
1952 * Destroy all addresses iterator
1955 * @param key peer identity (unused)
1956 * @param value the 'struct ATS_Address' to free
1957 * @return #GNUNET_OK (continue to iterate)
1960 destroy_all_address_it (void *cls,
1961 const struct GNUNET_PeerIdentity *key,
1964 struct GAS_Addresses_Handle *handle = cls;
1965 struct ATS_Address *aa = value;
1968 GNUNET_assert(GNUNET_YES ==
1969 GNUNET_CONTAINER_multipeermap_remove (handle->addresses, key, value));
1971 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1980 * Remove all addresses
1982 * @param handle the address handle to use
1985 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1987 if (GNUNET_NO == handle->running)
1990 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Destroying all addresses\n");
1991 handle->env.sf.s_bulk_start (handle->solver);
1992 if (handle->addresses != NULL )
1993 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
1994 &destroy_all_address_it,
1996 handle->env.sf.s_bulk_start (handle->solver);
2001 * Shutdown address subsystem.
2003 * @param handle the address handle to shutdown
2006 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
2008 struct GAS_Addresses_Suggestion_Requests *cur;
2009 struct GAS_Addresses_Preference_Clients *pcur;
2011 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Shutting down addresses\n");
2012 GNUNET_assert(NULL != handle);
2013 GAS_addresses_destroy_all (handle);
2014 handle->running = GNUNET_NO;
2015 GNUNET_CONTAINER_multipeermap_destroy (handle->addresses);
2016 handle->addresses = NULL;
2017 while (NULL != (cur = handle->pending_requests_head))
2019 GNUNET_CONTAINER_DLL_remove(handle->pending_requests_head, handle->pending_requests_tail, cur);
2023 while (NULL != (pcur = handle->preference_clients_head))
2025 GNUNET_CONTAINER_DLL_remove (handle->preference_clients_head,
2026 handle->preference_clients_tail, pcur);
2027 GNUNET_assert (handle->pref_clients > 0);
2028 handle->pref_clients --;
2029 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
2033 GNUNET_PLUGIN_unload (handle->plugin, handle->solver);
2034 GNUNET_free (handle->plugin);
2035 GNUNET_free(handle);
2036 /* Stop configured solution method */
2037 GAS_normalization_stop ();
2041 struct PeerIteratorContext
2043 GNUNET_ATS_Peer_Iterator it;
2045 struct GNUNET_CONTAINER_MultiPeerMap *peers_returned;
2050 * Iterator to iterate over all peers
2052 * @param cls a PeerIteratorContext
2053 * @param key the peer id
2054 * @param value the ATS_address
2055 * @return #GNUNET_OK to continue
2059 const struct GNUNET_PeerIdentity *key,
2062 struct PeerIteratorContext *ip_ctx = cls;
2065 GNUNET_CONTAINER_multipeermap_contains (ip_ctx->peers_returned, key))
2067 GNUNET_CONTAINER_multipeermap_put (ip_ctx->peers_returned, key, NULL,
2068 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2069 ip_ctx->it (ip_ctx->it_cls, key);
2076 * Return information all peers currently known to ATS
2078 * @param handle the address handle to use
2079 * @param p_it the iterator to call for every peer
2080 * @param p_it_cls the closure for the iterator
2083 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
2084 GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
2086 struct PeerIteratorContext ip_ctx;
2091 GNUNET_assert(NULL != handle->addresses);
2093 size = GNUNET_CONTAINER_multipeermap_size (handle->addresses);
2097 ip_ctx.it_cls = p_it_cls;
2098 ip_ctx.peers_returned = GNUNET_CONTAINER_multipeermap_create (size,
2100 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2103 GNUNET_CONTAINER_multipeermap_destroy (ip_ctx.peers_returned);
2105 p_it (p_it_cls, NULL );
2108 struct PeerInfoIteratorContext
2110 GNUNET_ATS_PeerInfo_Iterator it;
2116 * Iterator to iterate over a peer's addresses
2118 * @param cls a `struct PeerInfoIteratorContext`
2119 * @param key the peer id
2120 * @param value the `struct ATS_address`
2121 * @return #GNUNET_OK to continue
2124 peerinfo_it (void *cls,
2125 const struct GNUNET_PeerIdentity *key,
2128 struct PeerInfoIteratorContext *pi_ctx = cls;
2129 struct ATS_Address *addr = value;
2131 if (NULL != pi_ctx->it)
2133 pi_ctx->it (pi_ctx->it_cls, &addr->peer, addr->plugin, addr->addr,
2134 addr->addr_len, addr->active, addr->atsi, addr->atsi_count,
2135 addr->assigned_bw_out, addr->assigned_bw_in);
2142 * Return information all peers currently known to ATS
2144 * @param handle the address handle to use
2145 * @param peer the respective peer
2146 * @param pi_it the iterator to call for every peer
2147 * @param pi_it_cls the closure for the iterator
2150 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
2151 const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it,
2154 struct PeerInfoIteratorContext pi_ctx;
2155 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
2157 GNUNET_assert(NULL != peer);
2158 GNUNET_assert(NULL != handle->addresses);
2160 return; /* does not make sense without callback */
2162 zero_bw = GNUNET_BANDWIDTH_value_init (0);
2164 pi_ctx.it_cls = pi_it_cls;
2166 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
2168 &peerinfo_it, &pi_ctx);
2171 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw,
2176 /* end of gnunet-service-ats_addresses.c */