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;
508 aa->assigned_bw_out = 0;
509 aa->assigned_bw_in = 0;
510 aa->last_notified_bw_out = 0;
511 aa->last_notified_bw_in = 0;
513 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
515 aa->atsin[c1].avg_queue_index = 0;
516 for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
517 aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
524 * Closure for #compare_address_it()
526 struct CompareAddressContext
528 const struct ATS_Address *search;
530 /* exact_address != NULL if address and session is equal */
531 struct ATS_Address *exact_address;
532 /* exact_address != NULL if address and session is 0 */
533 struct ATS_Address *base_address;
540 * @param cls a CompareAddressContext containin the source address
542 * @param value the address to compare with
543 * @return #GNUNET_YES to continue, #GNUNET_NO if address is founce
546 compare_address_it (void *cls,
547 const struct GNUNET_PeerIdentity *key,
550 struct CompareAddressContext *cac = cls;
551 struct ATS_Address *aa = value;
553 /* Find an matching exact address:
556 * aa->addr_len == cac->search->addr_len
557 * aa->plugin == cac->search->plugin
558 * aa->addr == cac->search->addr
559 * aa->session == cac->search->session
561 * return as exact address
563 if ((aa->addr_len == cac->search->addr_len)
564 && (0 == strcmp (aa->plugin, cac->search->plugin)))
566 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
567 && (aa->session_id == cac->search->session_id))
568 cac->exact_address = aa;
571 /* Find an matching base address:
575 * aa->session_id == 0
578 * aa->addr_len == cac->search->addr_len
579 * aa->plugin == cac->search->plugin
580 * aa->addr == cac->search->addr
582 * return as base address
584 if ((aa->addr_len == cac->search->addr_len)
585 && (0 == strcmp (aa->plugin, cac->search->plugin)))
587 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
588 && (aa->session_id == 0))
589 cac->base_address = aa;
592 /* Find an matching exact address based on session:
596 * cac->search->addr_len == 0
599 * aa->plugin == cac->search->plugin
600 * aa->session_id == cac->search->session_id
602 * return as exact address
604 if (0 == cac->search->addr_len)
606 if ((0 == strcmp (aa->plugin, cac->search->plugin))
607 && (aa->session_id == cac->search->session_id))
608 cac->exact_address = aa;
611 if (cac->exact_address == NULL )
612 return GNUNET_YES; /* Continue iteration to find exact address */
614 return GNUNET_NO; /* Stop iteration since we have an exact address */
619 * Find an existing equivalent address record.
620 * Compares by peer identity and network address OR by session ID
621 * (one of the two must match).
623 * @param handle the address handle
624 * @param peer peer to lookup addresses for
625 * @param addr existing address record
626 * @return existing address record, NULL for none
629 find_equivalent_address (struct GAS_Addresses_Handle *handle,
630 const struct GNUNET_PeerIdentity *peer,
631 const struct ATS_Address *addr)
633 struct CompareAddressContext cac;
635 cac.exact_address = NULL;
636 cac.base_address = NULL;
638 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
640 &compare_address_it, &cac);
642 if (cac.exact_address == NULL)
643 return cac.base_address;
644 return cac.exact_address;
649 * Find the exact address
651 * @param handle the address handle to use
653 * @param plugin_name transport plugin name
654 * @param plugin_addr plugin address
655 * @param plugin_addr_len length of the plugin address
656 * @param local_address_info the local address for the address
657 * @param session_id session id, can be 0
658 * @return an ATS_address or NULL
661 static struct ATS_Address *
662 find_exact_address (struct GAS_Addresses_Handle *handle,
663 const struct GNUNET_PeerIdentity *peer,
664 const char *plugin_name,
665 const void *plugin_addr,
666 size_t plugin_addr_len,
667 uint32_t local_address_info,
670 struct ATS_Address *aa;
671 struct ATS_Address *ea;
673 aa = create_address (peer,
680 /* Get existing address or address with session == 0 */
681 ea = find_equivalent_address (handle, peer, aa);
685 else if (ea->session_id != session_id)
692 * Function allowing the solver to obtain normalized preference
696 * @param id the peer to return the normalized properties for
697 * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
700 get_preferences_cb (void *cls,
701 const struct GNUNET_PeerIdentity *id)
703 return GAS_normalization_get_preferences_by_peer (id);
708 * Function allowing the solver to obtain normalized property
709 * values for an address from solver
712 * @param address the address
713 * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
716 get_property_cb (void *cls, const struct ATS_Address *address)
718 return GAS_normalization_get_properties ((struct ATS_Address *) address);
723 * Extract an ATS performance info from an address
725 * @param address the address
726 * @param type the type to extract in HBO
727 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
730 get_performance_info (struct ATS_Address *address, uint32_t type)
733 GNUNET_assert(NULL != address);
735 if ((NULL == address->atsi) || (0 == address->atsi_count))
736 return GNUNET_ATS_VALUE_UNDEFINED;
738 for (c1 = 0; c1 < address->atsi_count; c1++)
740 if (ntohl (address->atsi[c1].type) == type)
741 return ntohl (address->atsi[c1].value);
743 return GNUNET_ATS_VALUE_UNDEFINED;
748 * Add a new address for a peer.
750 * @param handle the address handle to use
752 * @param plugin_name transport plugin name
753 * @param plugin_addr plugin address
754 * @param plugin_addr_len length of the plugin address in @a plugin_addr
755 * @param local_address_info the local address for the address
756 * @param session_id session id, can be 0
757 * @param atsi performance information for this address
758 * @param atsi_count number of performance information contained in @a atsi
761 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
762 const struct GNUNET_PeerIdentity *peer,
763 const char *plugin_name,
764 const void *plugin_addr,
765 size_t plugin_addr_len,
766 uint32_t local_address_info,
768 const struct GNUNET_ATS_Information *atsi,
771 struct ATS_Address *new_address;
772 struct ATS_Address *existing_address;
773 struct GNUNET_ATS_Information *atsi_delta;
774 uint32_t atsi_delta_count;
776 uint32_t previous_session;
779 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
780 "Received `%s' for peer `%s'\n",
784 if (GNUNET_NO == handle->running)
787 GNUNET_assert(NULL != handle->addresses);
789 new_address = create_address (peer, plugin_name,
790 plugin_addr, plugin_addr_len,
791 local_address_info, session_id);
793 disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
795 GNUNET_free_non_null(atsi_delta);
796 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
797 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
798 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
800 /* Get existing address or address with session == 0 */
801 existing_address = find_equivalent_address (handle, peer, new_address);
802 if (existing_address == NULL )
804 /* Add a new address */
805 new_address->t_added = GNUNET_TIME_absolute_get();
806 new_address->t_last_activity = GNUNET_TIME_absolute_get();
807 GNUNET_assert(GNUNET_OK ==
808 GNUNET_CONTAINER_multipeermap_put (handle->addresses,
811 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
813 GNUNET_STATISTICS_set (handle->stat,
815 GNUNET_CONTAINER_multipeermap_size (handle->addresses),
818 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
819 "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
824 GNUNET_ATS_print_network_type (addr_net));
826 /* Tell solver about new address */
827 handle->env.sf.s_add (handle->solver, new_address, addr_net);
829 handle->env.sf.s_bulk_start (handle->solver);
830 GAS_normalization_normalize_property (handle->addresses,
834 handle->env.sf.s_bulk_stop (handle->solver);
836 /* Notify performance clients about new address */
837 GAS_performance_notify_all_clients (&new_address->peer, new_address->plugin,
838 new_address->addr, new_address->addr_len, new_address->active,
839 new_address->atsi, new_address->atsi_count,
840 GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_out),
841 GNUNET_BANDWIDTH_value_init (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, existing_address->addr,
878 existing_address->addr_len, existing_address->active,
879 existing_address->atsi, existing_address->atsi_count,
880 GNUNET_BANDWIDTH_value_init (existing_address->assigned_bw_out),
881 GNUNET_BANDWIDTH_value_init (existing_address->assigned_bw_in));
883 for (c1 = 0; c1 < atsi_delta_count; c1++)
885 if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
886 && (addr_net != ntohl (atsi_delta[c1].value)))
888 /* Network type changed */
889 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
890 "Address for peer `%s' %p changed from network %s to %s\n",
891 GNUNET_i2s (peer), existing_address,
892 GNUNET_ATS_print_network_type (addr_net),
893 GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
894 handle->env.sf.s_address_update_network (handle->solver, existing_address,
895 ntohl (atsi_delta[c1].value),
896 get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
897 addr_net = get_performance_info (existing_address,
898 GNUNET_ATS_NETWORK_TYPE);
901 /* Notify solver about update with atsi information and session */
902 handle->env.sf.s_bulk_start (handle->solver);
903 GAS_normalization_normalize_property (handle->addresses, existing_address,
905 handle->env.sf.s_bulk_stop (handle->solver);
907 GNUNET_free_non_null(atsi_delta);
909 /* Notify solver about new session */
910 if (existing_address->session_id == session_id)
911 return; /* possible, can both be 0 since address is revalidated */
913 previous_session = existing_address->session_id;
914 existing_address->session_id = session_id;
915 handle->env.sf.s_address_update_session (handle->solver, existing_address,
916 previous_session, session_id);
918 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
919 "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
920 GNUNET_i2s (peer), existing_address, existing_address->addr_len,
921 session_id, GNUNET_ATS_print_network_type (addr_net));
926 * Update an address with a session or performance information for a peer.
928 * If an address was added without a session it will be updated with the
931 * @param handle the address handle to use
933 * @param plugin_name transport plugin name
934 * @param plugin_addr plugin address
935 * @param plugin_addr_len length of the plugin address
936 * @param local_address_info the local address for the address
937 * @param session_id session id, can be 0
938 * @param atsi performance information for this address
939 * @param atsi_count number of performance information contained in @a atsi
942 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
943 const struct GNUNET_PeerIdentity *peer,
944 const char *plugin_name,
945 const void *plugin_addr,
946 size_t plugin_addr_len,
947 uint32_t local_address_info,
949 const struct GNUNET_ATS_Information *atsi,
952 struct ATS_Address *aa;
953 struct GNUNET_ATS_Information *atsi_delta;
954 uint32_t atsi_delta_count;
955 uint32_t prev_session;
958 if (GNUNET_NO == handle->running)
961 GNUNET_assert(NULL != handle->addresses);
963 /* Get existing address */
964 aa = find_exact_address (handle, peer, plugin_name, plugin_addr,
965 plugin_addr_len, local_address_info, session_id);
968 if (NULL == aa->solver_information)
971 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
972 "Received `%s' for peer `%s' address \n",
974 GNUNET_i2s (peer), aa);
977 aa->t_last_activity = GNUNET_TIME_absolute_get();
978 if (session_id != aa->session_id)
980 /* Session changed */
981 prev_session = aa->session_id;
982 aa->session_id = session_id;
983 handle->env.sf.s_address_update_session (handle->solver,
990 atsi_delta_count = 0;
992 disassemble_ats_information (aa, atsi,
997 /* ATS properties changed */
998 for (c1 = 0; c1 < atsi_delta_count; c1++)
1000 if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
1002 /* Network type changed */
1003 handle->env.sf.s_address_update_network (handle->solver, aa,
1004 ntohl (atsi_delta[c1].value),
1005 get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
1009 /* Notify performance clients about updated address */
1010 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr,
1011 aa->addr_len, aa->active, aa->atsi, aa->atsi_count,
1012 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
1013 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
1015 handle->env.sf.s_bulk_start (handle->solver);
1016 GAS_normalization_normalize_property (handle->addresses,
1020 handle->env.sf.s_bulk_stop (handle->solver);
1022 GNUNET_free_non_null (atsi_delta);
1027 * Closure for #destroy_by_session_id().
1029 struct DestroyContext
1034 struct ATS_Address *aa;
1039 struct GAS_Addresses_Handle *handle;
1042 * #GNUNET_NO : full address
1043 * #GNUNET_YES : just session
1052 * If session != 0, just the session is deleted, the address itself still exists
1053 * If session == 0, remove full address
1054 * If session == 0 and addrlen == 0, destroy inbound address
1058 * @param value the `struct ATS_Address *`
1059 * @return #GNUNET_OK (continue to iterate)
1062 destroy_by_session_id (void *cls,
1063 const struct GNUNET_PeerIdentity *key,
1066 struct DestroyContext *dc = cls;
1067 struct GAS_Addresses_Handle *handle = dc->handle;
1068 const struct ATS_Address *des = dc->aa;
1069 struct ATS_Address *aa = value;
1074 sizeof (struct GNUNET_PeerIdentity)));
1075 if (0 == des->session_id)
1077 /* Session == 0, remove full address */
1078 if ((0 == strcmp (des->plugin, aa->plugin))
1079 && (aa->addr_len == des->addr_len)
1080 && (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
1083 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1084 "Deleting full address for peer `%s' session %u %p\n",
1085 GNUNET_i2s (&aa->peer),
1088 /* Notify solver about deletion */
1089 GNUNET_assert (GNUNET_YES ==
1090 GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1093 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1094 GAS_performance_notify_all_clients (&aa->peer,
1103 dc->result = GNUNET_NO;
1104 return GNUNET_OK; /* Continue iteration */
1109 /* Session != 0, just remove session */
1110 if (aa->session_id != des->session_id)
1111 return GNUNET_OK; /* irrelevant */
1113 if ((aa->session_id != 0) && (0 != strcmp (des->plugin, aa->plugin)))
1115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1116 "Different plugins during removal: `%s' vs `%s' \n",
1122 if (GNUNET_HELLO_ADDRESS_INFO_INBOUND ==
1123 (aa->local_address_info && GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1125 /* Inbound connection died, delete full address */
1126 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1127 "Deleting inbound address for peer `%s': `%s' session %u\n",
1128 GNUNET_i2s (&aa->peer),
1132 /* Notify solver about deletion */
1133 GNUNET_assert(GNUNET_YES ==
1134 GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1136 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1137 GAS_performance_notify_all_clients (&aa->peer,
1146 dc->result = GNUNET_NO;
1147 return GNUNET_OK; /* Continue iteration */
1152 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1153 "Deleting session for peer `%s': `%s' %u\n",
1154 GNUNET_i2s (&aa->peer),
1155 aa->plugin, aa->session_id);
1156 /* Notify solver to delete session */
1157 handle->env.sf.s_del (handle->solver, aa, GNUNET_YES);
1159 GAS_performance_notify_all_clients (&aa->peer,
1175 * Remove an address or just a session for a peer.
1177 * @param handle the address handle to use
1179 * @param plugin_name transport plugin name
1180 * @param plugin_addr plugin address
1181 * @param plugin_addr_len length of the plugin address in @a plugin_addr
1182 * @param local_address_info the local address for the address
1183 * @param session_id session id, can be 0
1186 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
1187 const struct GNUNET_PeerIdentity *peer,
1188 const char *plugin_name,
1189 const void *plugin_addr,
1190 size_t plugin_addr_len,
1191 uint32_t local_address_info,
1192 uint32_t session_id)
1194 struct ATS_Address *ea;
1195 struct DestroyContext dc;
1197 if (GNUNET_NO == handle->running)
1200 /* Get existing address */
1201 ea = find_exact_address (handle,
1210 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1211 "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 "Received `%s' for peer `%s' address %p session %u\n", "ADDRESS DESTROY",
1223 GNUNET_break (0 < strlen (plugin_name));
1225 dc.aa = create_address (peer,
1229 local_address_info, session_id);
1230 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1232 &destroy_by_session_id, &dc);
1233 GNUNET_STATISTICS_set (handle->stat,
1235 GNUNET_CONTAINER_multipeermap_size (handle->addresses),
1237 free_address (dc.aa);
1242 * Notification about active use of an address.
1243 * in_use == #GNUNET_YES:
1244 * This address is used to maintain an active connection with a peer.
1245 * in_use == #GNUNET_NO:
1246 * This address is no longer used to maintain an active connection with a peer.
1248 * Note: can only be called with in_use == #GNUNET_NO if called with #GNUNET_YES
1251 * @param handle the address handle to use
1253 * @param plugin_name transport plugin name
1254 * @param plugin_addr plugin address
1255 * @param plugin_addr_len length of the plugin address
1256 * @param local_address_info the local address for the address
1257 * @param session_id session id, can be 0
1258 * @param in_use #GNUNET_YES if #GNUNET_NO FIXME
1259 * @return #GNUNET_SYSERR on failure (address unknown ...)
1262 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
1263 const struct GNUNET_PeerIdentity *peer,
1264 const char *plugin_name,
1265 const void *plugin_addr,
1266 size_t plugin_addr_len,
1267 uint32_t local_address_info,
1268 uint32_t session_id,
1271 struct ATS_Address *ea;
1273 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1274 "Received `%s' for peer `%s'\n",
1277 if (GNUNET_NO == handle->running)
1278 return GNUNET_SYSERR;
1279 ea = find_exact_address (handle,
1287 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1288 "Trying to set unknown address `%s' `%s' `%u' to %s \n",
1292 (GNUNET_NO == in_use) ? "NO" : "YES");
1294 return GNUNET_SYSERR;
1296 if (ea->used == in_use)
1299 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1300 "Address in use called multiple times for peer `%s': %s -> %s \n",
1302 (GNUNET_NO == ea->used) ? "NO" : "YES",
1303 (GNUNET_NO == in_use) ? "NO" : "YES");
1304 return GNUNET_SYSERR;
1306 /* Tell solver about update */
1308 ea->t_last_activity = GNUNET_TIME_absolute_get();
1309 handle->env.sf.s_address_update_inuse (handle->solver,
1317 * Cancel address suggestions for a peer
1319 * @param handle the address handle
1320 * @param peer the peer id
1323 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
1324 const struct GNUNET_PeerIdentity *peer)
1326 struct GAS_Addresses_Suggestion_Requests *cur = handle->pending_requests_head;
1328 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received request: `%s' for peer %s\n",
1329 "request_address_cancel", GNUNET_i2s (peer));
1333 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1340 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1341 "No address requests pending for peer `%s', cannot remove!\n",
1345 handle->env.sf.s_get_stop (handle->solver, peer);
1346 GAS_addresses_handle_backoff_reset (handle, peer);
1347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Removed request pending for peer `%s\n",
1349 GNUNET_CONTAINER_DLL_remove(handle->pending_requests_head, handle->pending_requests_tail, cur);
1355 * Request address suggestions for a peer
1357 * @param handle the address handle
1358 * @param peer the peer id
1361 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
1362 const struct GNUNET_PeerIdentity *peer)
1364 struct GAS_Addresses_Suggestion_Requests *cur = handle->pending_requests_head;
1365 struct ATS_Address *aa;
1367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1368 "REQUEST ADDRESS", GNUNET_i2s (peer));
1370 if (GNUNET_NO == handle->running)
1374 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1375 break; /* already suggesting */
1380 cur = GNUNET_new (struct GAS_Addresses_Suggestion_Requests);
1382 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1383 "Adding new address suggestion request for `%s'\n",
1385 GNUNET_CONTAINER_DLL_insert(handle->pending_requests_head, handle->pending_requests_tail, cur);
1388 /* Get prefered address from solver */
1389 aa = (struct ATS_Address *) handle->env.sf.s_get (handle->solver, peer);
1392 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1397 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1398 aa, GNUNET_i2s (peer));
1400 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
1401 aa->addr_len, aa->local_address_info, aa->session_id, aa->atsi,
1402 aa->atsi_count, GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
1403 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
1405 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1406 ATS_BLOCKING_DELTA);
1407 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1408 aa->block_interval);
1410 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1411 "Address %p ready for suggestion, block interval now %llu \n", aa,
1412 aa->block_interval);
1416 * Iterator to reset address blocking
1418 * @param cls not used
1419 * @param key the peer
1420 * @param value the address to reset
1421 * @return #GNUNET_OK to continue
1424 reset_address_it (void *cls,
1425 const struct GNUNET_PeerIdentity *key,
1428 struct ATS_Address *aa = value;
1430 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1431 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1432 GNUNET_i2s (&aa->peer),
1434 aa->block_interval);
1435 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1436 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1442 * Reset suggestion backoff for a peer
1444 * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1445 * reset using this function
1447 * @param handle the address handle
1448 * @param peer the peer id
1451 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1452 const struct GNUNET_PeerIdentity *peer)
1454 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1455 "RESET BACKOFF", GNUNET_i2s (peer));
1458 GNUNET_SYSERR != GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1460 &reset_address_it, NULL));
1465 * Solver information callback
1467 * @param cls the closure
1468 * @param op the operation
1469 * @param stat operation status
1470 * @param add additional information
1474 solver_info_cb (void *cls,
1475 enum GAS_Solver_Operation op,
1476 enum GAS_Solver_Status stat,
1477 enum GAS_Solver_Additional_Information add)
1483 add_info = "GAS_INFO_NONE";
1486 add_info = "GAS_INFO_MLP_FULL";
1488 case GAS_INFO_UPDATED:
1489 add_info = "GAS_INFO_MLP_UPDATED";
1491 case GAS_INFO_PROP_ALL:
1492 add_info = "GAS_INFO_PROP_ALL";
1494 case GAS_INFO_PROP_SINGLE:
1495 add_info = "GAS_INFO_PROP_SINGLE";
1498 add_info = "INVALID";
1503 case GAS_OP_SOLVE_START:
1504 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1505 "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
1506 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
1508 case GAS_OP_SOLVE_STOP:
1509 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1510 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
1511 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
1514 case GAS_OP_SOLVE_SETUP_START:
1515 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1516 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
1517 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1520 case GAS_OP_SOLVE_SETUP_STOP:
1521 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1522 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
1523 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1526 case GAS_OP_SOLVE_MLP_LP_START:
1527 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1528 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
1529 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1531 case GAS_OP_SOLVE_MLP_LP_STOP:
1532 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1533 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
1534 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1537 case GAS_OP_SOLVE_MLP_MLP_START:
1538 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1539 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
1540 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1542 case GAS_OP_SOLVE_MLP_MLP_STOP:
1543 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1544 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
1545 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1547 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
1548 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1549 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
1550 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1552 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
1553 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1554 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
1555 (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
1564 * The preference changed for a peer
1566 * @param cls the address handle
1567 * @param peer the peer
1568 * @param kind the ATS kind
1569 * @param pref_rel the new relative preference value
1572 normalized_preference_changed_cb (void *cls,
1573 const struct GNUNET_PeerIdentity *peer, enum GNUNET_ATS_PreferenceKind kind,
1576 GNUNET_assert(NULL != cls);
1577 struct GAS_Addresses_Handle *handle = cls;
1579 /* Tell solver about update */
1580 handle->env.sf.s_pref (handle->solver, peer, kind, pref_rel);
1584 * The relative value for a property changed
1586 * @param cls the address handle
1587 * @param address the peer
1588 * @param type the ATS type
1589 * @param prop_rel the new relative preference value
1592 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
1593 uint32_t type, double prop_rel)
1595 struct GAS_Addresses_Handle *ah = (struct GAS_Addresses_Handle *) cls;
1596 GNUNET_assert(NULL != ah);
1598 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1599 "Normalized property %s for peer `%s' changed to %.3f \n",
1600 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1603 ah->env.sf.s_address_update_property (ah->solver, address, type, 0, prop_rel);
1606 static struct GAS_Addresses_Preference_Clients *
1607 find_preference_client (struct GAS_Addresses_Handle *handle, void *client)
1609 struct GAS_Addresses_Preference_Clients *cur;
1611 for (cur = handle->preference_clients_head; NULL != cur; cur = cur->next)
1613 if (cur->client == client)
1621 * A performance client disconnected
1623 * @param handle address handle
1624 * @param client the client
1627 GAS_addresses_preference_client_disconnect (struct GAS_Addresses_Handle *handle,
1630 struct GAS_Addresses_Preference_Clients * pc;
1631 if (NULL != (pc = find_preference_client (handle, client)))
1633 GNUNET_CONTAINER_DLL_remove (handle->preference_clients_head,
1634 handle->preference_clients_tail, pc);
1636 GNUNET_assert (handle->pref_clients > 0);
1637 handle->pref_clients --;
1638 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO);
1640 GAS_normalization_preference_client_disconnect (client);
1645 * Change the preference for a peer
1647 * @param handle the address handle
1648 * @param client the client sending this request
1649 * @param peer the peer id
1650 * @param kind the preference kind to change
1651 * @param score_abs the new preference score
1654 GAS_addresses_preference_change (struct GAS_Addresses_Handle *handle,
1655 void *client, const struct GNUNET_PeerIdentity *peer,
1656 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1658 struct GAS_Addresses_Preference_Clients * pc;
1659 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1660 "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1661 GNUNET_i2s (peer), client);
1663 if (GNUNET_NO == handle->running)
1667 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1670 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1671 "Received `%s' for unknown peer `%s' from client %p\n",
1672 "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1676 if (NULL == find_preference_client (handle, client))
1678 pc = GNUNET_new (struct GAS_Addresses_Preference_Clients);
1679 pc->client = client;
1680 GNUNET_CONTAINER_DLL_insert (handle->preference_clients_head,
1681 handle->preference_clients_tail, pc);
1682 handle->pref_clients ++;
1683 GNUNET_STATISTICS_set (handle->stat,
1684 "# active performance clients",
1685 handle->pref_clients,
1689 handle->env.sf.s_bulk_start (handle->solver);
1690 /* Tell normalization about change, normalization will call callback if preference changed */
1691 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1692 handle->env.sf.s_bulk_stop (handle->solver);
1697 * Change the preference for a peer
1699 * @param handle the address handle
1700 * @param application the client sending this request
1701 * @param peer the peer id
1702 * @param scope the time interval for this feedback: [now - scope .. now]
1703 * @param kind the preference kind to change
1704 * @param score_abs the new preference score
1707 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1709 const struct GNUNET_PeerIdentity *peer,
1710 const struct GNUNET_TIME_Relative scope,
1711 enum GNUNET_ATS_PreferenceKind kind,
1714 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1715 "Received `%s' for peer `%s' for client %p\n", "PREFERENCE FEEDBACK",
1716 GNUNET_i2s (peer), application);
1718 if (GNUNET_NO == handle->running)
1722 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1725 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1726 "Received `%s' for unknown peer `%s' from client %p\n",
1727 "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1731 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, struct ATS_Address *address)
1871 struct GAS_Addresses_Handle *handle = cls;
1872 struct GAS_Addresses_Suggestion_Requests *cur;
1876 GNUNET_assert(handle != NULL);
1877 GNUNET_assert(address != NULL);
1878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1879 "Bandwidth assignment changed for peer %s \n",
1880 GNUNET_i2s (&address->peer));
1882 /* Notify performance clients about changes to address */
1883 GAS_performance_notify_all_clients (&address->peer, address->plugin,
1884 address->addr, address->addr_len, address->active, address->atsi,
1885 address->atsi_count,
1886 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
1887 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
1889 cur = handle->pending_requests_head;
1892 if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
1893 break; /* we have an address request pending*/
1898 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1899 "Nobody is interested in peer `%s' :(\n",
1900 GNUNET_i2s (&address->peer));
1904 if ((0 == address->assigned_bw_in) && (0 == address->assigned_bw_out))
1906 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1907 "Telling transport to disconnect peer `%s'\n",
1908 GNUNET_i2s (&address->peer));
1910 /* *Notify scheduling clients about suggestion */
1911 GAS_scheduling_transmit_address_suggestion (&address->peer, address->plugin,
1912 address->addr, address->addr_len, address->local_address_info,
1913 address->session_id, address->atsi, address->atsi_count,
1914 GNUNET_BANDWIDTH_value_init (0),
1915 GNUNET_BANDWIDTH_value_init (0));
1920 /* Do bandwidth stability check */
1921 diff_out = abs (address->assigned_bw_out - address->last_notified_bw_out);
1922 diff_in = abs (address->assigned_bw_in - address->last_notified_bw_in);
1924 if ( (diff_out < htonl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) &&
1925 (diff_in < htonl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) )
1928 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1929 "Sending bandwidth update for peer `%s': %u %u\n",
1930 GNUNET_i2s (&address->peer), address->assigned_bw_out,
1931 address->assigned_bw_out);
1933 /* *Notify scheduling clients about suggestion */
1934 GAS_scheduling_transmit_address_suggestion (&address->peer, address->plugin,
1935 address->addr, address->addr_len, address->local_address_info,
1936 address->session_id, address->atsi, address->atsi_count,
1937 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
1938 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
1940 address->last_notified_bw_out = address->assigned_bw_out;
1941 address->last_notified_bw_in = address->assigned_bw_in;
1946 * Initialize address subsystem. The addresses subsystem manages the addresses
1947 * known and current performance information. It has a solver component
1948 * responsible for the resource allocation. It tells the solver about changes
1949 * and receives updates when the solver changes the resource allocation.
1951 * @param cfg configuration to use
1952 * @param stats the statistics handle to use
1953 * @return an address handle
1955 struct GAS_Addresses_Handle *
1956 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1957 const struct GNUNET_STATISTICS_Handle *stats)
1959 struct GAS_Addresses_Handle *ah;
1960 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1961 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1966 ah = GNUNET_new (struct GAS_Addresses_Handle);
1967 ah->running = GNUNET_NO;
1969 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1970 /* Initialize the addresses database */
1971 ah->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1972 ah->pref_clients = 0;
1973 GNUNET_assert(NULL != ah->addresses);
1975 /* Figure out configured solution method */
1976 if (GNUNET_SYSERR ==
1977 GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1979 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1980 "No resource assignment method configured, using proportional approach\n");
1981 ah->ats_mode = MODE_PROPORTIONAL;
1985 for (c = 0; c < strlen (mode_str); c++)
1986 mode_str[c] = toupper (mode_str[c]);
1987 if (0 == strcmp (mode_str, "PROPORTIONAL"))
1988 ah->ats_mode = MODE_PROPORTIONAL;
1989 else if (0 == strcmp (mode_str, "MLP"))
1991 ah->ats_mode = MODE_MLP;
1993 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1994 "Assignment method `%s' configured, but GLPK is not available, please install \n",
1996 ah->ats_mode = MODE_PROPORTIONAL;
1999 else if (0 == strcmp (mode_str, "RIL"))
2000 ah->ats_mode = MODE_RIL;
2003 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2004 "Invalid resource assignment method `%s' configured, using proportional approach\n",
2006 ah->ats_mode = MODE_PROPORTIONAL;
2008 GNUNET_free(mode_str);
2011 load_quotas (cfg, quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount);
2012 ah->env.info_cb = &solver_info_cb;
2013 ah->env.info_cb_cls = ah;
2014 ah->env.bandwidth_changed_cb = &bandwidth_changed_cb;
2015 ah->env.bw_changed_cb_cls = ah;
2016 ah->env.get_preferences = &get_preferences_cb;
2017 ah->env.get_preference_cls = ah;
2018 ah->env.get_property = &get_property_cb;
2019 ah->env.get_property_cls = ah;
2021 ah->env.stats = stats;
2022 ah->env.addresses = ah->addresses;
2024 ah->env.network_count = GNUNET_ATS_NetworkTypeCount;
2025 int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
2026 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
2028 ah->env.networks[c] = networks[c];
2029 ah->env.out_quota[c] = quotas_out[c];
2030 ah->env.in_quota[c] = quotas_in[c];
2033 switch (ah->ats_mode) {
2034 case MODE_PROPORTIONAL:
2035 plugin_short = "proportional";
2038 plugin_short = "mlp";
2041 plugin_short = "ril";
2044 plugin_short = NULL;
2047 GNUNET_asprintf (&ah->plugin,
2048 "libgnunet_plugin_ats_%s",
2050 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2051 _("Initializing solver `%s '`%s'\n"),
2054 if (NULL == (ah->solver = GNUNET_PLUGIN_load (ah->plugin, &ah->env)))
2056 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2057 _("Failed to initialize solver `%s'!\n"),
2062 GNUNET_assert (NULL != ah->env.sf.s_add);
2063 GNUNET_assert (NULL != ah->env.sf.s_address_update_inuse);
2064 GNUNET_assert (NULL != ah->env.sf.s_address_update_property);
2065 GNUNET_assert (NULL != ah->env.sf.s_address_update_session);
2066 GNUNET_assert (NULL != ah->env.sf.s_address_update_network);
2067 GNUNET_assert (NULL != ah->env.sf.s_get);
2068 GNUNET_assert (NULL != ah->env.sf.s_get_stop);
2069 GNUNET_assert (NULL != ah->env.sf.s_pref);
2070 GNUNET_assert (NULL != ah->env.sf.s_feedback);
2071 GNUNET_assert (NULL != ah->env.sf.s_del);
2072 GNUNET_assert (NULL != ah->env.sf.s_bulk_start);
2073 GNUNET_assert (NULL != ah->env.sf.s_bulk_stop);
2076 GAS_normalization_start (&normalized_preference_changed_cb, ah,
2077 &normalized_property_changed_cb, ah);
2079 if (NULL == ah->solver)
2081 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2082 _("Failed to initialize solver!\n"));
2086 /* up and running */
2087 ah->running = GNUNET_YES;
2089 GNUNET_STATISTICS_set (ah->stat, "# addresses",
2090 GNUNET_CONTAINER_multipeermap_size (ah->addresses), GNUNET_NO);
2097 * Destroy all addresses iterator
2100 * @param key peer identity (unused)
2101 * @param value the 'struct ATS_Address' to free
2102 * @return #GNUNET_OK (continue to iterate)
2105 destroy_all_address_it (void *cls,
2106 const struct GNUNET_PeerIdentity *key,
2109 struct GAS_Addresses_Handle *handle = cls;
2110 struct ATS_Address *aa = value;
2113 GNUNET_assert(GNUNET_YES ==
2114 GNUNET_CONTAINER_multipeermap_remove (handle->addresses, key, value));
2116 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
2118 GAS_performance_notify_all_clients (&aa->peer,
2132 * Remove all addresses
2134 * @param handle the address handle to use
2137 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
2139 if (GNUNET_NO == handle->running)
2142 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2143 "Destroying all addresses\n");
2144 handle->env.sf.s_bulk_start (handle->solver);
2145 if (NULL != handle->addresses)
2146 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2147 &destroy_all_address_it,
2149 handle->env.sf.s_bulk_start (handle->solver);
2154 * Shutdown address subsystem.
2156 * @param handle the address handle to shutdown
2159 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
2161 struct GAS_Addresses_Suggestion_Requests *cur;
2162 struct GAS_Addresses_Preference_Clients *pcur;
2164 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Shutting down addresses\n");
2165 GNUNET_assert(NULL != handle);
2166 GAS_addresses_destroy_all (handle);
2167 handle->running = GNUNET_NO;
2168 GNUNET_CONTAINER_multipeermap_destroy (handle->addresses);
2169 handle->addresses = NULL;
2170 while (NULL != (cur = handle->pending_requests_head))
2172 GNUNET_CONTAINER_DLL_remove (handle->pending_requests_head,
2173 handle->pending_requests_tail,
2178 while (NULL != (pcur = handle->preference_clients_head))
2180 GNUNET_CONTAINER_DLL_remove (handle->preference_clients_head,
2181 handle->preference_clients_tail,
2183 GNUNET_assert (handle->pref_clients > 0);
2184 handle->pref_clients --;
2185 GNUNET_STATISTICS_set (handle->stat,
2186 "# active performance clients",
2187 handle->pref_clients,
2191 GNUNET_PLUGIN_unload (handle->plugin,
2193 GNUNET_free (handle->plugin);
2194 GNUNET_free(handle);
2195 /* Stop configured solution method */
2196 GAS_normalization_stop ();
2201 * Closure for #peerinfo_it().
2203 struct PeerInfoIteratorContext
2206 * Function to call for each address.
2208 GNUNET_ATS_PeerInfo_Iterator it;
2211 * Closure for @e it.
2218 * Iterator to iterate over a peer's addresses
2220 * @param cls a `struct PeerInfoIteratorContext`
2221 * @param key the peer id
2222 * @param value the `struct ATS_address`
2223 * @return #GNUNET_OK to continue
2226 peerinfo_it (void *cls,
2227 const struct GNUNET_PeerIdentity *key,
2230 struct PeerInfoIteratorContext *pi_ctx = cls;
2231 struct ATS_Address *addr = value;
2233 pi_ctx->it (pi_ctx->it_cls,
2239 addr->atsi, addr->atsi_count,
2240 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_out),
2241 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_in));
2247 * Return information all peers currently known to ATS
2249 * @param handle the address handle to use
2250 * @param peer the respective peer, NULL for 'all' peers
2251 * @param pi_it the iterator to call for every peer
2252 * @param pi_it_cls the closure for @a pi_it
2255 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
2256 const struct GNUNET_PeerIdentity *peer,
2257 GNUNET_ATS_PeerInfo_Iterator pi_it,
2260 struct PeerInfoIteratorContext pi_ctx;
2264 /* does not make sense without callback */
2268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2269 "Returning information for %s from a total of %u known addresses\n",
2272 : GNUNET_i2s (peer),
2273 (unsigned int) GNUNET_CONTAINER_multipeermap_size (handle->addresses));
2275 pi_ctx.it_cls = pi_it_cls;
2277 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2281 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
2283 &peerinfo_it, &pi_ctx);
2285 NULL, NULL, NULL, 0,
2292 /* end of gnunet-service-ats_addresses.c */