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
341 * Value we pass for zero bandwidth.
343 const static struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
347 * Disassemble ATS information and update performance information in address
349 * Updates existing information and adds new information
351 * @param dest destination address
352 * @param update source ATS information
353 * @param update_count number of ATS information in @a update
354 * @param delta_dest ats performance information which were updated
355 * including previous value
356 * @param delta_count number of ATS information in the @a delta_dest
357 * @return #GNUNET_YES if address was address updated, GNUNET_NO otherwise
360 disassemble_ats_information (struct ATS_Address *dest,
361 const struct GNUNET_ATS_Information *update,
362 uint32_t update_count,
363 struct GNUNET_ATS_Information **delta_dest,
364 uint32_t *delta_count)
370 struct GNUNET_ATS_Information add_atsi[update_count];
371 struct GNUNET_ATS_Information delta_atsi[update_count];
372 struct GNUNET_ATS_Information *tmp_atsi;
373 uint32_t add_atsi_count;
374 uint32_t delta_atsi_count;
378 delta_atsi_count = 0;
380 if (0 == update_count)
383 if (NULL == dest->atsi)
385 /* Create performance information */
387 GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
388 dest->atsi_count = update_count;
389 memcpy (dest->atsi, update,
390 update_count * sizeof(struct GNUNET_ATS_Information));
392 GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
393 for (c1 = 0; c1 < update_count; c1++)
395 (*delta_dest)[c1].type = update[c1].type;
396 (*delta_dest)[c1].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
398 (*delta_count) = update_count;
402 for (c1 = 0; c1 < update_count; c1++)
404 /* Update existing performance information */
406 for (c2 = 0; c2 < dest->atsi_count; c2++)
408 if (update[c1].type == dest->atsi[c2].type)
410 if (update[c1].value != dest->atsi[c2].value)
412 /* Save previous value in delta */
413 delta_atsi[delta_atsi_count] = dest->atsi[c2];
416 dest->atsi[c2].value = update[c1].value;
423 if (GNUNET_NO == found)
425 add_atsi[add_atsi_count] = update[c1];
427 delta_atsi[delta_atsi_count].type = update[c1].type;
428 delta_atsi[delta_atsi_count].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
433 if (add_atsi_count > 0)
435 /* Extend ats performance information */
437 tmp_atsi = GNUNET_malloc ((dest->atsi_count + add_atsi_count) *
438 (sizeof (struct GNUNET_ATS_Information)));
439 memcpy (tmp_atsi, dest->atsi,
440 dest->atsi_count * sizeof(struct GNUNET_ATS_Information));
441 memcpy (&tmp_atsi[dest->atsi_count], add_atsi,
442 add_atsi_count * sizeof(struct GNUNET_ATS_Information));
443 GNUNET_free(dest->atsi);
444 dest->atsi = tmp_atsi;
445 dest->atsi_count = dest->atsi_count + add_atsi_count;
449 if (delta_atsi_count > 0)
453 GNUNET_malloc (delta_atsi_count * sizeof (struct GNUNET_ATS_Information));
454 memcpy ((*delta_dest), delta_atsi,
455 delta_atsi_count * sizeof(struct GNUNET_ATS_Information));
456 (*delta_count) = delta_atsi_count;
464 * Free the given address
466 * @param addr address to destroy
469 free_address (struct ATS_Address *addr)
471 GNUNET_free(addr->plugin);
472 GNUNET_free_non_null(addr->atsi);
478 * Create a ATS_address with the given information
481 * @param plugin_name plugin
482 * @param plugin_addr address
483 * @param plugin_addr_len address length
484 * @param local_address_info additional local info for the address
485 * @param session_id session identifier, can be 0
486 * @return the ATS_Address
488 static struct ATS_Address *
489 create_address (const struct GNUNET_PeerIdentity *peer,
490 const char *plugin_name,
491 const void *plugin_addr,
492 size_t plugin_addr_len,
493 uint32_t local_address_info,
496 struct ATS_Address *aa = NULL;
500 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
502 aa->addr_len = plugin_addr_len;
504 memcpy (&aa[1], plugin_addr, plugin_addr_len);
505 aa->plugin = GNUNET_strdup (plugin_name);
506 aa->session_id = session_id;
507 aa->local_address_info = local_address_info;
509 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
511 aa->atsin[c1].avg_queue_index = 0;
512 for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
513 aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
520 * Closure for #compare_address_it()
522 struct CompareAddressContext
524 const struct ATS_Address *search;
526 /* exact_address != NULL if address and session is equal */
527 struct ATS_Address *exact_address;
528 /* exact_address != NULL if address and session is 0 */
529 struct ATS_Address *base_address;
536 * @param cls a CompareAddressContext containin the source address
538 * @param value the address to compare with
539 * @return #GNUNET_YES to continue, #GNUNET_NO if address is founce
542 compare_address_it (void *cls,
543 const struct GNUNET_PeerIdentity *key,
546 struct CompareAddressContext *cac = cls;
547 struct ATS_Address *aa = value;
549 /* Find an matching exact address:
552 * aa->addr_len == cac->search->addr_len
553 * aa->plugin == cac->search->plugin
554 * aa->addr == cac->search->addr
555 * aa->session == cac->search->session
557 * return as exact address
559 if ((aa->addr_len == cac->search->addr_len)
560 && (0 == strcmp (aa->plugin, cac->search->plugin)))
562 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
563 && (aa->session_id == cac->search->session_id))
564 cac->exact_address = aa;
567 /* Find an matching base address:
571 * aa->session_id == 0
574 * aa->addr_len == cac->search->addr_len
575 * aa->plugin == cac->search->plugin
576 * aa->addr == cac->search->addr
578 * return as base address
580 if ((aa->addr_len == cac->search->addr_len)
581 && (0 == strcmp (aa->plugin, cac->search->plugin)))
583 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
584 && (aa->session_id == 0))
585 cac->base_address = aa;
588 /* Find an matching exact address based on session:
592 * cac->search->addr_len == 0
595 * aa->plugin == cac->search->plugin
596 * aa->session_id == cac->search->session_id
598 * return as exact address
600 if (0 == cac->search->addr_len)
602 if ((0 == strcmp (aa->plugin, cac->search->plugin))
603 && (aa->session_id == cac->search->session_id))
604 cac->exact_address = aa;
607 if (cac->exact_address == NULL )
608 return GNUNET_YES; /* Continue iteration to find exact address */
610 return GNUNET_NO; /* Stop iteration since we have an exact address */
615 * Find an existing equivalent address record.
616 * Compares by peer identity and network address OR by session ID
617 * (one of the two must match).
619 * @param handle the address handle
620 * @param peer peer to lookup addresses for
621 * @param addr existing address record
622 * @return existing address record, NULL for none
625 find_equivalent_address (struct GAS_Addresses_Handle *handle,
626 const struct GNUNET_PeerIdentity *peer,
627 const struct ATS_Address *addr)
629 struct CompareAddressContext cac;
631 cac.exact_address = NULL;
632 cac.base_address = NULL;
634 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
636 &compare_address_it, &cac);
638 if (cac.exact_address == NULL)
639 return cac.base_address;
640 return cac.exact_address;
645 * Find the exact address
647 * @param handle the address handle to use
649 * @param plugin_name transport plugin name
650 * @param plugin_addr plugin address
651 * @param plugin_addr_len length of the plugin address
652 * @param local_address_info the local address for the address
653 * @param session_id session id, can be 0
654 * @return an ATS_address or NULL
657 static struct ATS_Address *
658 find_exact_address (struct GAS_Addresses_Handle *handle,
659 const struct GNUNET_PeerIdentity *peer,
660 const char *plugin_name,
661 const void *plugin_addr,
662 size_t plugin_addr_len,
663 uint32_t local_address_info,
666 struct ATS_Address *aa;
667 struct ATS_Address *ea;
669 aa = create_address (peer,
676 /* Get existing address or address with session == 0 */
677 ea = find_equivalent_address (handle, peer, aa);
681 else if (ea->session_id != session_id)
688 * Function allowing the solver to obtain normalized preference
692 * @param id the peer to return the normalized properties for
693 * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
696 get_preferences_cb (void *cls,
697 const struct GNUNET_PeerIdentity *id)
699 return GAS_normalization_get_preferences_by_peer (id);
704 * Function allowing the solver to obtain normalized property
705 * values for an address from solver
708 * @param address the address
709 * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
712 get_property_cb (void *cls, const struct ATS_Address *address)
714 return GAS_normalization_get_properties ((struct ATS_Address *) address);
719 * Extract an ATS performance info from an address
721 * @param address the address
722 * @param type the type to extract in HBO
723 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
726 get_performance_info (struct ATS_Address *address, uint32_t type)
729 GNUNET_assert(NULL != address);
731 if ((NULL == address->atsi) || (0 == address->atsi_count))
732 return GNUNET_ATS_VALUE_UNDEFINED;
734 for (c1 = 0; c1 < address->atsi_count; c1++)
736 if (ntohl (address->atsi[c1].type) == type)
737 return ntohl (address->atsi[c1].value);
739 return GNUNET_ATS_VALUE_UNDEFINED;
744 * Add a new address for a peer.
746 * @param handle the address handle to use
748 * @param plugin_name transport plugin name
749 * @param plugin_addr plugin address
750 * @param plugin_addr_len length of the plugin address in @a plugin_addr
751 * @param local_address_info the local address for the address
752 * @param session_id session id, can be 0
753 * @param atsi performance information for this address
754 * @param atsi_count number of performance information contained in @a atsi
757 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
758 const struct GNUNET_PeerIdentity *peer,
759 const char *plugin_name,
760 const void *plugin_addr,
761 size_t plugin_addr_len,
762 uint32_t local_address_info,
764 const struct GNUNET_ATS_Information *atsi,
767 struct ATS_Address *new_address;
768 struct ATS_Address *existing_address;
769 struct GNUNET_ATS_Information *atsi_delta;
770 uint32_t atsi_delta_count;
772 uint32_t previous_session;
775 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
776 "Received `%s' for peer `%s'\n",
780 if (GNUNET_NO == handle->running)
783 GNUNET_assert(NULL != handle->addresses);
785 new_address = create_address (peer, plugin_name,
786 plugin_addr, plugin_addr_len,
787 local_address_info, session_id);
789 disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
791 GNUNET_free_non_null(atsi_delta);
792 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
793 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
794 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
796 /* Get existing address or address with session == 0 */
797 existing_address = find_equivalent_address (handle, peer, new_address);
798 if (existing_address == NULL )
800 /* Add a new address */
801 new_address->t_added = GNUNET_TIME_absolute_get();
802 new_address->t_last_activity = GNUNET_TIME_absolute_get();
803 GNUNET_assert(GNUNET_OK ==
804 GNUNET_CONTAINER_multipeermap_put (handle->addresses,
807 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
809 GNUNET_STATISTICS_set (handle->stat,
811 GNUNET_CONTAINER_multipeermap_size (handle->addresses),
814 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
815 "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
820 GNUNET_ATS_print_network_type (addr_net));
822 /* Tell solver about new address */
823 handle->env.sf.s_add (handle->solver, new_address, addr_net);
825 handle->env.sf.s_bulk_start (handle->solver);
826 GAS_normalization_normalize_property (handle->addresses,
830 handle->env.sf.s_bulk_stop (handle->solver);
832 /* Notify performance clients about new address */
833 GAS_performance_notify_all_clients (&new_address->peer,
836 new_address->addr_len,
839 new_address->atsi_count,
840 new_address->assigned_bw_out,
841 new_address->assigned_bw_in);
845 /* We have an existing address we can use, clean up new */
846 GNUNET_free(new_address->plugin);
847 GNUNET_free_non_null(new_address->atsi);
848 GNUNET_free(new_address);
851 if (0 != existing_address->session_id)
853 /* Should not happen */
858 addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
859 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
860 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
862 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
863 "Found existing address for peer `%s' %p with new session %u in network %s\n",
864 GNUNET_i2s (peer), existing_address, session_id,
865 GNUNET_ATS_print_network_type (addr_net));
866 /* We have an address without an session, update this address */
867 existing_address->t_added = GNUNET_TIME_absolute_get();
868 existing_address->t_last_activity = GNUNET_TIME_absolute_get();
870 atsi_delta_count = 0;
872 == disassemble_ats_information (existing_address, atsi, atsi_count,
873 &atsi_delta, &atsi_delta_count))
875 /* Notify performance clients about properties */
876 GAS_performance_notify_all_clients (&existing_address->peer,
877 existing_address->plugin,
878 existing_address->addr,
879 existing_address->addr_len,
880 existing_address->active,
881 existing_address->atsi,
882 existing_address->atsi_count,
883 existing_address->assigned_bw_out,
884 existing_address->assigned_bw_in);
886 for (c1 = 0; c1 < atsi_delta_count; c1++)
888 if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
889 && (addr_net != ntohl (atsi_delta[c1].value)))
891 /* Network type changed */
892 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
893 "Address for peer `%s' %p changed from network %s to %s\n",
894 GNUNET_i2s (peer), existing_address,
895 GNUNET_ATS_print_network_type (addr_net),
896 GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
897 handle->env.sf.s_address_update_network (handle->solver, existing_address,
898 ntohl (atsi_delta[c1].value),
899 get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
900 addr_net = get_performance_info (existing_address,
901 GNUNET_ATS_NETWORK_TYPE);
904 /* Notify solver about update with atsi information and session */
905 handle->env.sf.s_bulk_start (handle->solver);
906 GAS_normalization_normalize_property (handle->addresses, existing_address,
908 handle->env.sf.s_bulk_stop (handle->solver);
910 GNUNET_free_non_null(atsi_delta);
912 /* Notify solver about new session */
913 if (existing_address->session_id == session_id)
914 return; /* possible, can both be 0 since address is revalidated */
916 previous_session = existing_address->session_id;
917 existing_address->session_id = session_id;
918 handle->env.sf.s_address_update_session (handle->solver, existing_address,
919 previous_session, session_id);
921 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
922 "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
923 GNUNET_i2s (peer), existing_address, existing_address->addr_len,
924 session_id, GNUNET_ATS_print_network_type (addr_net));
929 * Update an address with a session or performance information for a peer.
931 * If an address was added without a session it will be updated with the
934 * @param handle the address handle to use
936 * @param plugin_name transport plugin name
937 * @param plugin_addr plugin address
938 * @param plugin_addr_len length of the plugin address
939 * @param local_address_info the local address for the address
940 * @param session_id session id, can be 0
941 * @param atsi performance information for this address
942 * @param atsi_count number of performance information contained in @a atsi
945 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
946 const struct GNUNET_PeerIdentity *peer,
947 const char *plugin_name,
948 const void *plugin_addr,
949 size_t plugin_addr_len,
950 uint32_t local_address_info,
952 const struct GNUNET_ATS_Information *atsi,
955 struct ATS_Address *aa;
956 struct GNUNET_ATS_Information *atsi_delta;
957 uint32_t atsi_delta_count;
958 uint32_t prev_session;
961 if (GNUNET_NO == handle->running)
964 GNUNET_assert(NULL != handle->addresses);
966 /* Get existing address */
967 aa = find_exact_address (handle, peer, plugin_name, plugin_addr,
968 plugin_addr_len, local_address_info, session_id);
971 if (NULL == aa->solver_information)
974 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
975 "Received `%s' for peer `%s' address \n",
977 GNUNET_i2s (peer), aa);
980 aa->t_last_activity = GNUNET_TIME_absolute_get();
981 if (session_id != aa->session_id)
983 /* Session changed */
984 prev_session = aa->session_id;
985 aa->session_id = session_id;
986 handle->env.sf.s_address_update_session (handle->solver,
993 atsi_delta_count = 0;
995 disassemble_ats_information (aa, atsi,
1000 /* ATS properties changed */
1001 for (c1 = 0; c1 < atsi_delta_count; c1++)
1003 if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
1005 /* Network type changed */
1006 handle->env.sf.s_address_update_network (handle->solver, aa,
1007 ntohl (atsi_delta[c1].value),
1008 get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
1012 /* Notify performance clients about updated address */
1013 GAS_performance_notify_all_clients (&aa->peer,
1020 aa->assigned_bw_out,
1021 aa->assigned_bw_in);
1022 handle->env.sf.s_bulk_start (handle->solver);
1023 GAS_normalization_normalize_property (handle->addresses,
1027 handle->env.sf.s_bulk_stop (handle->solver);
1029 GNUNET_free_non_null (atsi_delta);
1034 * Closure for #destroy_by_session_id().
1036 struct DestroyContext
1041 struct ATS_Address *aa;
1046 struct GAS_Addresses_Handle *handle;
1049 * #GNUNET_NO : full address
1050 * #GNUNET_YES : just session
1059 * If session != 0, just the session is deleted, the address itself still exists
1060 * If session == 0, remove full address
1061 * If session == 0 and addrlen == 0, destroy inbound address
1065 * @param value the `struct ATS_Address *`
1066 * @return #GNUNET_OK (continue to iterate)
1069 destroy_by_session_id (void *cls,
1070 const struct GNUNET_PeerIdentity *key,
1073 struct DestroyContext *dc = cls;
1074 struct GAS_Addresses_Handle *handle = dc->handle;
1075 const struct ATS_Address *des = dc->aa;
1076 struct ATS_Address *aa = value;
1081 sizeof (struct GNUNET_PeerIdentity)));
1082 if (0 == des->session_id)
1084 /* Session == 0, remove full address */
1085 if ((0 == strcmp (des->plugin, aa->plugin))
1086 && (aa->addr_len == des->addr_len)
1087 && (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
1090 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1091 "Deleting full address for peer `%s' session %u %p\n",
1092 GNUNET_i2s (&aa->peer),
1095 /* Notify solver about deletion */
1096 GNUNET_assert (GNUNET_YES ==
1097 GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1100 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1101 GAS_performance_notify_all_clients (&aa->peer,
1110 dc->result = GNUNET_NO;
1111 return GNUNET_OK; /* Continue iteration */
1116 /* Session != 0, just remove session */
1117 if (aa->session_id != des->session_id)
1118 return GNUNET_OK; /* irrelevant */
1120 if ((aa->session_id != 0) && (0 != strcmp (des->plugin, aa->plugin)))
1122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1123 "Different plugins during removal: `%s' vs `%s' \n",
1129 if (GNUNET_HELLO_ADDRESS_INFO_INBOUND ==
1130 (aa->local_address_info && GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1132 /* Inbound connection died, delete full address */
1133 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1134 "Deleting inbound address for peer `%s': `%s' session %u\n",
1135 GNUNET_i2s (&aa->peer),
1139 /* Notify solver about deletion */
1140 GNUNET_assert(GNUNET_YES ==
1141 GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1143 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1144 GAS_performance_notify_all_clients (&aa->peer,
1153 dc->result = GNUNET_NO;
1154 return GNUNET_OK; /* Continue iteration */
1159 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1160 "Deleting session for peer `%s': `%s' %u\n",
1161 GNUNET_i2s (&aa->peer),
1162 aa->plugin, aa->session_id);
1163 /* Notify solver to delete session */
1164 handle->env.sf.s_del (handle->solver, aa, GNUNET_YES);
1166 aa->active = GNUNET_NO;
1167 GAS_performance_notify_all_clients (&aa->peer,
1183 * Remove an address or just a session for a peer.
1185 * @param handle the address handle to use
1187 * @param plugin_name transport plugin name
1188 * @param plugin_addr plugin address
1189 * @param plugin_addr_len length of the plugin address in @a plugin_addr
1190 * @param local_address_info the local address for the address
1191 * @param session_id session id, can be 0
1194 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
1195 const struct GNUNET_PeerIdentity *peer,
1196 const char *plugin_name,
1197 const void *plugin_addr,
1198 size_t plugin_addr_len,
1199 uint32_t local_address_info,
1200 uint32_t session_id)
1202 struct ATS_Address *ea;
1203 struct DestroyContext dc;
1205 if (GNUNET_NO == handle->running)
1208 /* Get existing address */
1209 ea = find_exact_address (handle,
1218 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1219 "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Received `%s' for peer `%s' address %p session %u\n", "ADDRESS DESTROY",
1231 GNUNET_break (0 < strlen (plugin_name));
1233 dc.aa = create_address (peer,
1237 local_address_info, session_id);
1238 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1240 &destroy_by_session_id, &dc);
1241 GNUNET_STATISTICS_set (handle->stat,
1243 GNUNET_CONTAINER_multipeermap_size (handle->addresses),
1245 free_address (dc.aa);
1250 * Notification about active use of an address.
1251 * in_use == #GNUNET_YES:
1252 * This address is used to maintain an active connection with a peer.
1253 * in_use == #GNUNET_NO:
1254 * This address is no longer used to maintain an active connection with a peer.
1256 * Note: can only be called with in_use == #GNUNET_NO if called with #GNUNET_YES
1259 * @param handle the address handle to use
1261 * @param plugin_name transport plugin name
1262 * @param plugin_addr plugin address
1263 * @param plugin_addr_len length of the plugin address
1264 * @param local_address_info the local address for the address
1265 * @param session_id session id, can be 0
1266 * @param in_use #GNUNET_YES if #GNUNET_NO FIXME
1267 * @return #GNUNET_SYSERR on failure (address unknown ...)
1270 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
1271 const struct GNUNET_PeerIdentity *peer,
1272 const char *plugin_name,
1273 const void *plugin_addr,
1274 size_t plugin_addr_len,
1275 uint32_t local_address_info,
1276 uint32_t session_id,
1279 struct ATS_Address *ea;
1281 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1282 "Received `%s' for peer `%s'\n",
1285 if (GNUNET_NO == handle->running)
1286 return GNUNET_SYSERR;
1287 ea = find_exact_address (handle,
1295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1296 "Trying to set unknown address `%s' `%s' `%u' to %s \n",
1300 (GNUNET_NO == in_use) ? "NO" : "YES");
1302 return GNUNET_SYSERR;
1304 if (ea->used == in_use)
1307 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1308 "Address in use called multiple times for peer `%s': %s -> %s \n",
1310 (GNUNET_NO == ea->used) ? "NO" : "YES",
1311 (GNUNET_NO == in_use) ? "NO" : "YES");
1312 return GNUNET_SYSERR;
1314 /* Tell solver about update */
1316 ea->t_last_activity = GNUNET_TIME_absolute_get();
1317 handle->env.sf.s_address_update_inuse (handle->solver,
1325 * Cancel address suggestions for a peer
1327 * @param handle the address handle
1328 * @param peer the peer id
1331 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
1332 const struct GNUNET_PeerIdentity *peer)
1334 struct GAS_Addresses_Suggestion_Requests *cur = handle->pending_requests_head;
1336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received request: `%s' for peer %s\n",
1337 "request_address_cancel", GNUNET_i2s (peer));
1341 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1348 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1349 "No address requests pending for peer `%s', cannot remove!\n",
1353 handle->env.sf.s_get_stop (handle->solver, peer);
1354 GAS_addresses_handle_backoff_reset (handle, peer);
1355 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Removed request pending for peer `%s\n",
1357 GNUNET_CONTAINER_DLL_remove(handle->pending_requests_head, handle->pending_requests_tail, cur);
1363 * Request address suggestions for a peer
1365 * @param handle the address handle
1366 * @param peer the peer id
1369 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
1370 const struct GNUNET_PeerIdentity *peer)
1372 struct GAS_Addresses_Suggestion_Requests *cur = handle->pending_requests_head;
1373 struct ATS_Address *aa;
1375 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1376 "REQUEST ADDRESS", GNUNET_i2s (peer));
1378 if (GNUNET_NO == handle->running)
1382 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1383 break; /* already suggesting */
1388 cur = GNUNET_new (struct GAS_Addresses_Suggestion_Requests);
1390 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1391 "Adding new address suggestion request for `%s'\n",
1393 GNUNET_CONTAINER_DLL_insert(handle->pending_requests_head, handle->pending_requests_tail, cur);
1396 /* Get prefered address from solver */
1397 aa = (struct ATS_Address *) handle->env.sf.s_get (handle->solver, peer);
1400 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1405 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1406 aa, GNUNET_i2s (peer));
1408 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
1409 aa->addr_len, aa->local_address_info, aa->session_id,
1410 aa->atsi, aa->atsi_count,
1411 aa->assigned_bw_out, aa->assigned_bw_in);
1413 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1414 ATS_BLOCKING_DELTA);
1415 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1416 aa->block_interval);
1418 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1419 "Address %p ready for suggestion, block interval now %llu \n", aa,
1420 aa->block_interval);
1424 * Iterator to reset address blocking
1426 * @param cls not used
1427 * @param key the peer
1428 * @param value the address to reset
1429 * @return #GNUNET_OK to continue
1432 reset_address_it (void *cls,
1433 const struct GNUNET_PeerIdentity *key,
1436 struct ATS_Address *aa = value;
1438 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1439 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1440 GNUNET_i2s (&aa->peer),
1442 aa->block_interval);
1443 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1444 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1450 * Reset suggestion backoff for a peer
1452 * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1453 * reset using this function
1455 * @param handle the address handle
1456 * @param peer the peer id
1459 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1460 const struct GNUNET_PeerIdentity *peer)
1462 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1463 "RESET BACKOFF", GNUNET_i2s (peer));
1466 GNUNET_SYSERR != GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1468 &reset_address_it, NULL));
1473 * Solver information callback
1475 * @param cls the closure
1476 * @param op the operation
1477 * @param stat operation status
1478 * @param add additional information
1482 solver_info_cb (void *cls,
1483 enum GAS_Solver_Operation op,
1484 enum GAS_Solver_Status stat,
1485 enum GAS_Solver_Additional_Information add)
1491 add_info = "GAS_INFO_NONE";
1494 add_info = "GAS_INFO_MLP_FULL";
1496 case GAS_INFO_UPDATED:
1497 add_info = "GAS_INFO_MLP_UPDATED";
1499 case GAS_INFO_PROP_ALL:
1500 add_info = "GAS_INFO_PROP_ALL";
1502 case GAS_INFO_PROP_SINGLE:
1503 add_info = "GAS_INFO_PROP_SINGLE";
1506 add_info = "INVALID";
1511 case GAS_OP_SOLVE_START:
1512 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1513 "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
1514 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
1516 case GAS_OP_SOLVE_STOP:
1517 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1518 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
1519 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
1522 case GAS_OP_SOLVE_SETUP_START:
1523 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1524 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
1525 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1528 case GAS_OP_SOLVE_SETUP_STOP:
1529 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1530 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
1531 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1534 case GAS_OP_SOLVE_MLP_LP_START:
1535 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1536 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
1537 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1539 case GAS_OP_SOLVE_MLP_LP_STOP:
1540 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1541 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
1542 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1545 case GAS_OP_SOLVE_MLP_MLP_START:
1546 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1547 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
1548 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1550 case GAS_OP_SOLVE_MLP_MLP_STOP:
1551 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1552 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
1553 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1555 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
1556 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1557 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
1558 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1560 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
1561 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1562 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
1563 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1572 * The preference changed for a peer
1574 * @param cls the address handle
1575 * @param peer the peer
1576 * @param kind the ATS kind
1577 * @param pref_rel the new relative preference value
1580 normalized_preference_changed_cb (void *cls,
1581 const struct GNUNET_PeerIdentity *peer, enum GNUNET_ATS_PreferenceKind kind,
1584 GNUNET_assert(NULL != cls);
1585 struct GAS_Addresses_Handle *handle = cls;
1587 /* Tell solver about update */
1588 handle->env.sf.s_pref (handle->solver, peer, kind, pref_rel);
1592 * The relative value for a property changed
1594 * @param cls the address handle
1595 * @param address the peer
1596 * @param type the ATS type
1597 * @param prop_rel the new relative preference value
1600 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
1601 uint32_t type, double prop_rel)
1603 struct GAS_Addresses_Handle *ah = (struct GAS_Addresses_Handle *) cls;
1604 GNUNET_assert(NULL != ah);
1606 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1607 "Normalized property %s for peer `%s' changed to %.3f \n",
1608 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1611 ah->env.sf.s_address_update_property (ah->solver, address, type, 0, prop_rel);
1614 static struct GAS_Addresses_Preference_Clients *
1615 find_preference_client (struct GAS_Addresses_Handle *handle, void *client)
1617 struct GAS_Addresses_Preference_Clients *cur;
1619 for (cur = handle->preference_clients_head; NULL != cur; cur = cur->next)
1621 if (cur->client == client)
1628 * A performance client disconnected
1630 * @param handle address handle
1631 * @param client the client
1635 GAS_addresses_preference_client_disconnect (struct GAS_Addresses_Handle *handle,
1638 struct GAS_Addresses_Preference_Clients * pc;
1639 if (NULL != (pc = find_preference_client (handle, client)))
1641 GNUNET_CONTAINER_DLL_remove (handle->preference_clients_head,
1642 handle->preference_clients_tail, pc);
1644 GNUNET_assert (handle->pref_clients > 0);
1645 handle->pref_clients --;
1646 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
1648 GAS_normalization_preference_client_disconnect (client);
1652 * Change the preference for a peer
1654 * @param handle the address handle
1655 * @param client the client sending this request
1656 * @param peer the peer id
1657 * @param kind the preference kind to change
1658 * @param score_abs the new preference score
1661 GAS_addresses_preference_change (struct GAS_Addresses_Handle *handle,
1662 void *client, const struct GNUNET_PeerIdentity *peer,
1663 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1665 struct GAS_Addresses_Preference_Clients * pc;
1666 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1667 "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1668 GNUNET_i2s (peer), client);
1670 if (GNUNET_NO == handle->running)
1674 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1677 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1678 "Received `%s' for unknown peer `%s' from client %p\n",
1679 "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1683 if (NULL == find_preference_client (handle, client))
1685 pc = GNUNET_new (struct GAS_Addresses_Preference_Clients);
1686 pc->client = client;
1687 GNUNET_CONTAINER_DLL_insert (handle->preference_clients_head,
1688 handle->preference_clients_tail, pc);
1689 handle->pref_clients ++;
1690 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
1693 handle->env.sf.s_bulk_start (handle->solver);
1694 /* Tell normalization about change, normalization will call callback if preference changed */
1695 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1696 handle->env.sf.s_bulk_stop (handle->solver);
1700 * Change the preference for a peer
1702 * @param handle the address handle
1703 * @param application the client sending this request
1704 * @param peer the peer id
1705 * @param scope the time interval for this feedback: [now - scope .. now]
1706 * @param kind the preference kind to change
1707 * @param score_abs the new preference score
1710 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1711 void *application, const struct GNUNET_PeerIdentity *peer,
1712 const struct GNUNET_TIME_Relative scope,
1713 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1715 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1716 "Received `%s' for peer `%s' for client %p\n", "PREFERENCE FEEDBACK",
1717 GNUNET_i2s (peer), application);
1719 if (GNUNET_NO == handle->running)
1723 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1726 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1727 "Received `%s' for unknown peer `%s' from client %p\n",
1728 "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1732 handle->env.sf.s_feedback (handle->solver, application, peer, scope, kind,
1737 * Load quotas for networks from configuration
1739 * @param cfg configuration handle
1740 * @param out_dest where to write outbound quotas
1741 * @param in_dest where to write inbound quotas
1742 * @param dest_length length of inbound and outbound arrays
1743 * @return number of networks loaded
1746 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1747 unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1749 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1750 char * entry_in = NULL;
1751 char * entry_out = NULL;
1752 char * quota_out_str;
1753 char * quota_in_str;
1757 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1761 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1762 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1766 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
1770 if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
1772 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1775 if ((GNUNET_NO == res)
1777 == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
1780 if ((GNUNET_NO == res)
1782 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
1786 if (GNUNET_NO == res)
1788 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1789 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1790 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1791 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1795 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1796 _("Outbound quota configure for network `%s' is %llu\n"),
1797 network_str[c], out_dest[c]);
1799 GNUNET_free(quota_out_str);
1803 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1804 _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1805 network_str[c], GNUNET_ATS_DefaultBandwidth);
1806 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1811 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
1815 if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
1817 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1820 if ((GNUNET_NO == res)
1822 == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1824 if ((GNUNET_NO == res)
1826 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
1830 if (GNUNET_NO == res)
1832 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1833 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1834 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1835 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1839 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1840 _("Inbound quota configured for network `%s' is %llu\n"),
1841 network_str[c], in_dest[c]);
1843 GNUNET_free(quota_in_str);
1847 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1848 _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1849 network_str[c], GNUNET_ATS_DefaultBandwidth);
1850 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1852 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1853 "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c],
1854 in_dest[c], out_dest[c]);
1855 GNUNET_free(entry_out);
1856 GNUNET_free(entry_in);
1858 return GNUNET_ATS_NetworkTypeCount;
1863 * Callback for solver to notify about assignment changes
1865 * @param cls the GAS_Addresses_Handle
1866 * @param address the address with changes
1869 bandwidth_changed_cb (void *cls,
1870 struct ATS_Address *address)
1872 struct GAS_Addresses_Handle *handle = cls;
1873 struct GAS_Addresses_Suggestion_Requests *cur;
1875 GNUNET_assert(handle != NULL);
1876 GNUNET_assert(address != NULL);
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 "Bandwidth assignment changed for peer %s \n",
1879 GNUNET_i2s (&address->peer));
1881 /* Notify performance clients about changes to address */
1882 GAS_performance_notify_all_clients (&address->peer,
1888 address->atsi_count,
1889 address->assigned_bw_out,
1890 address->assigned_bw_in);
1891 cur = handle->pending_requests_head;
1894 if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
1895 break; /* we have an address request pending*/
1900 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1901 "Nobody is interested in peer `%s' :(\n",
1902 GNUNET_i2s (&address->peer));
1906 if ((0 == ntohl (address->assigned_bw_in.value__))
1907 && (0 == ntohl (address->assigned_bw_out.value__)))
1909 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1910 "Telling transport to disconnect peer `%s'\n",
1911 GNUNET_i2s (&address->peer));
1915 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1916 "Sending bandwidth update for peer `%s': %u %u\n",
1917 GNUNET_i2s (&address->peer),
1918 (unsigned int) ntohl (address->assigned_bw_out.value__),
1919 (unsigned int) ntohl (address->assigned_bw_out.value__));
1922 /* *Notify scheduling clients about suggestion */
1923 GAS_scheduling_transmit_address_suggestion (&address->peer, address->plugin,
1924 address->addr, address->addr_len, address->local_address_info,
1925 address->session_id, address->atsi,
1926 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1931 * Initialize address subsystem. The addresses subsystem manages the addresses
1932 * known and current performance information. It has a solver component
1933 * responsible for the resource allocation. It tells the solver about changes
1934 * and receives updates when the solver changes the resource allocation.
1936 * @param cfg configuration to use
1937 * @param stats the statistics handle to use
1938 * @return an address handle
1940 struct GAS_Addresses_Handle *
1941 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1942 const struct GNUNET_STATISTICS_Handle *stats)
1944 struct GAS_Addresses_Handle *ah;
1945 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1946 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1951 ah = GNUNET_new (struct GAS_Addresses_Handle);
1952 ah->running = GNUNET_NO;
1954 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1955 /* Initialize the addresses database */
1956 ah->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1957 ah->pref_clients = 0;
1958 GNUNET_assert(NULL != ah->addresses);
1960 /* Figure out configured solution method */
1962 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1964 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1965 "No resource assignment method configured, using proportional approach\n");
1966 ah->ats_mode = MODE_PROPORTIONAL;
1970 for (c = 0; c < strlen (mode_str); c++)
1971 mode_str[c] = toupper (mode_str[c]);
1972 if (0 == strcmp (mode_str, "PROPORTIONAL"))
1973 ah->ats_mode = MODE_PROPORTIONAL;
1974 else if (0 == strcmp (mode_str, "MLP"))
1976 ah->ats_mode = MODE_MLP;
1978 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1979 "Assignment method `%s' configured, but GLPK is not available, please install \n",
1981 ah->ats_mode = MODE_PROPORTIONAL;
1984 else if (0 == strcmp (mode_str, "RIL"))
1985 ah->ats_mode = MODE_RIL;
1988 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1989 "Invalid resource assignment method `%s' configured, using proportional approach\n",
1991 ah->ats_mode = MODE_PROPORTIONAL;
1993 GNUNET_free(mode_str);
1996 load_quotas (cfg, quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount);
1997 ah->env.info_cb = &solver_info_cb;
1998 ah->env.info_cb_cls = ah;
1999 ah->env.bandwidth_changed_cb = &bandwidth_changed_cb;
2000 ah->env.bw_changed_cb_cls = ah;
2001 ah->env.get_preferences = &get_preferences_cb;
2002 ah->env.get_preference_cls = ah;
2003 ah->env.get_property = &get_property_cb;
2004 ah->env.get_property_cls = ah;
2006 ah->env.stats = stats;
2007 ah->env.addresses = ah->addresses;
2009 ah->env.network_count = GNUNET_ATS_NetworkTypeCount;
2010 int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
2011 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
2013 ah->env.networks[c] = networks[c];
2014 ah->env.out_quota[c] = quotas_out[c];
2015 ah->env.in_quota[c] = quotas_in[c];
2018 switch (ah->ats_mode) {
2019 case MODE_PROPORTIONAL:
2020 plugin_short = "proportional";
2023 plugin_short = "mlp";
2026 plugin_short = "ril";
2029 plugin_short = NULL;
2032 GNUNET_asprintf (&ah->plugin, "libgnunet_plugin_ats_%s", plugin_short);
2033 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s '`%s'\n"), plugin_short, ah->plugin);
2034 if (NULL == (ah->solver = GNUNET_PLUGIN_load (ah->plugin, &ah->env)))
2036 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver `%s'!\n"), ah->plugin);
2040 GNUNET_assert (NULL != ah->env.sf.s_add);
2041 GNUNET_assert (NULL != ah->env.sf.s_address_update_inuse);
2042 GNUNET_assert (NULL != ah->env.sf.s_address_update_property);
2043 GNUNET_assert (NULL != ah->env.sf.s_address_update_session);
2044 GNUNET_assert (NULL != ah->env.sf.s_address_update_network);
2045 GNUNET_assert (NULL != ah->env.sf.s_get);
2046 GNUNET_assert (NULL != ah->env.sf.s_get_stop);
2047 GNUNET_assert (NULL != ah->env.sf.s_pref);
2048 GNUNET_assert (NULL != ah->env.sf.s_feedback);
2049 GNUNET_assert (NULL != ah->env.sf.s_del);
2050 GNUNET_assert (NULL != ah->env.sf.s_bulk_start);
2051 GNUNET_assert (NULL != ah->env.sf.s_bulk_stop);
2054 GAS_normalization_start (&normalized_preference_changed_cb, ah,
2055 &normalized_property_changed_cb, ah);
2057 if (NULL == ah->solver)
2059 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver!\n"));
2063 /* up and running */
2064 ah->running = GNUNET_YES;
2066 GNUNET_STATISTICS_set (ah->stat, "# addresses",
2067 GNUNET_CONTAINER_multipeermap_size (ah->addresses), GNUNET_NO);
2073 * Destroy all addresses iterator
2076 * @param key peer identity (unused)
2077 * @param value the 'struct ATS_Address' to free
2078 * @return #GNUNET_OK (continue to iterate)
2081 destroy_all_address_it (void *cls,
2082 const struct GNUNET_PeerIdentity *key,
2085 struct GAS_Addresses_Handle *handle = cls;
2086 struct ATS_Address *aa = value;
2089 GNUNET_assert(GNUNET_YES ==
2090 GNUNET_CONTAINER_multipeermap_remove (handle->addresses, key, value));
2092 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
2094 GAS_performance_notify_all_clients (&aa->peer,
2108 * Remove all addresses
2110 * @param handle the address handle to use
2113 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
2115 if (GNUNET_NO == handle->running)
2118 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2119 "Destroying all addresses\n");
2120 handle->env.sf.s_bulk_start (handle->solver);
2121 if (NULL != handle->addresses)
2122 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2123 &destroy_all_address_it,
2125 handle->env.sf.s_bulk_start (handle->solver);
2130 * Shutdown address subsystem.
2132 * @param handle the address handle to shutdown
2135 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
2137 struct GAS_Addresses_Suggestion_Requests *cur;
2138 struct GAS_Addresses_Preference_Clients *pcur;
2140 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Shutting down addresses\n");
2141 GNUNET_assert(NULL != handle);
2142 GAS_addresses_destroy_all (handle);
2143 handle->running = GNUNET_NO;
2144 GNUNET_CONTAINER_multipeermap_destroy (handle->addresses);
2145 handle->addresses = NULL;
2146 while (NULL != (cur = handle->pending_requests_head))
2148 GNUNET_CONTAINER_DLL_remove(handle->pending_requests_head, handle->pending_requests_tail, cur);
2152 while (NULL != (pcur = handle->preference_clients_head))
2154 GNUNET_CONTAINER_DLL_remove (handle->preference_clients_head,
2155 handle->preference_clients_tail, pcur);
2156 GNUNET_assert (handle->pref_clients > 0);
2157 handle->pref_clients --;
2158 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
2162 GNUNET_PLUGIN_unload (handle->plugin, handle->solver);
2163 GNUNET_free (handle->plugin);
2164 GNUNET_free(handle);
2165 /* Stop configured solution method */
2166 GAS_normalization_stop ();
2170 struct PeerIteratorContext
2172 GNUNET_ATS_Peer_Iterator it;
2174 struct GNUNET_CONTAINER_MultiPeerMap *peers_returned;
2179 * Iterator to iterate over all peers
2181 * @param cls a PeerIteratorContext
2182 * @param key the peer id
2183 * @param value the ATS_address
2184 * @return #GNUNET_OK to continue
2188 const struct GNUNET_PeerIdentity *key,
2191 struct PeerIteratorContext *ip_ctx = cls;
2194 GNUNET_CONTAINER_multipeermap_contains (ip_ctx->peers_returned, key))
2196 GNUNET_CONTAINER_multipeermap_put (ip_ctx->peers_returned, key, NULL,
2197 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2198 ip_ctx->it (ip_ctx->it_cls, key);
2205 * Return information all peers currently known to ATS
2207 * @param handle the address handle to use
2208 * @param p_it the iterator to call for every peer
2209 * @param p_it_cls the closure for the iterator
2212 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
2213 GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
2215 struct PeerIteratorContext ip_ctx;
2220 GNUNET_assert(NULL != handle->addresses);
2222 size = GNUNET_CONTAINER_multipeermap_size (handle->addresses);
2226 ip_ctx.it_cls = p_it_cls;
2227 ip_ctx.peers_returned = GNUNET_CONTAINER_multipeermap_create (size,
2229 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2232 GNUNET_CONTAINER_multipeermap_destroy (ip_ctx.peers_returned);
2234 p_it (p_it_cls, NULL );
2237 struct PeerInfoIteratorContext
2239 GNUNET_ATS_PeerInfo_Iterator it;
2245 * Iterator to iterate over a peer's addresses
2247 * @param cls a `struct PeerInfoIteratorContext`
2248 * @param key the peer id
2249 * @param value the `struct ATS_address`
2250 * @return #GNUNET_OK to continue
2253 peerinfo_it (void *cls,
2254 const struct GNUNET_PeerIdentity *key,
2257 struct PeerInfoIteratorContext *pi_ctx = cls;
2258 struct ATS_Address *addr = value;
2260 if (NULL != pi_ctx->it)
2262 pi_ctx->it (pi_ctx->it_cls, &addr->peer, addr->plugin, addr->addr,
2263 addr->addr_len, addr->active, addr->atsi, addr->atsi_count,
2264 addr->assigned_bw_out, addr->assigned_bw_in);
2271 * Return information all peers currently known to ATS
2273 * @param handle the address handle to use
2274 * @param peer the respective peer
2275 * @param pi_it the iterator to call for every peer
2276 * @param pi_it_cls the closure for the iterator
2279 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
2280 const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it,
2283 struct PeerInfoIteratorContext pi_ctx;
2285 GNUNET_assert(NULL != peer);
2286 GNUNET_assert(NULL != handle->addresses);
2288 return; /* does not make sense without callback */
2291 pi_ctx.it_cls = pi_it_cls;
2292 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
2294 &peerinfo_it, &pi_ctx);
2298 NULL, NULL, NULL, 0,
2306 /* end of gnunet-service-ats_addresses.c */