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;
277 static struct GNUNET_STATISTICS_Handle *stats;
280 * A multihashmap to store all addresses
282 static struct GNUNET_CONTAINER_MultiPeerMap *addresses;
285 * Is ATS addresses running
290 * Preferences clients
292 static int pref_clients;
295 * Configured ATS solver
305 * Address suggestion requests DLL head
307 static struct GAS_Addresses_Suggestion_Requests *pending_requests_head;
310 * Address suggestion requests DLL tail
312 static struct GAS_Addresses_Suggestion_Requests *pending_requests_tail;
315 * Preference requests DLL head
317 static struct GAS_Addresses_Preference_Clients *preference_clients_head;
320 * Preference requests DLL head
322 static struct GAS_Addresses_Preference_Clients *preference_clients_tail;
327 static struct GNUNET_ATS_PluginEnvironment env;
330 * Solver plugin name as string
335 * Value we pass for zero bandwidth.
337 static const struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
341 * Disassemble ATS information and update performance information in address
343 * Updates existing information and adds new information
345 * @param dest destination address
346 * @param update source ATS information
347 * @param update_count number of ATS information in @a update
348 * @param delta_dest ats performance information which were updated
349 * including previous value
350 * @param delta_count number of ATS information in the @a delta_dest
351 * @return #GNUNET_YES if address was address updated, GNUNET_NO otherwise
354 disassemble_ats_information (struct ATS_Address *dest,
355 const struct GNUNET_ATS_Information *update,
356 uint32_t update_count,
357 struct GNUNET_ATS_Information **delta_dest,
358 uint32_t *delta_count)
364 struct GNUNET_ATS_Information add_atsi[update_count];
365 struct GNUNET_ATS_Information delta_atsi[update_count];
366 struct GNUNET_ATS_Information *tmp_atsi;
367 uint32_t add_atsi_count;
368 uint32_t delta_atsi_count;
372 delta_atsi_count = 0;
374 if (0 == update_count)
377 if (NULL == dest->atsi)
379 /* Create performance information */
381 GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
382 dest->atsi_count = update_count;
383 memcpy (dest->atsi, update,
384 update_count * sizeof(struct GNUNET_ATS_Information));
386 GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
387 for (c1 = 0; c1 < update_count; c1++)
389 (*delta_dest)[c1].type = update[c1].type;
390 (*delta_dest)[c1].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
392 (*delta_count) = update_count;
396 for (c1 = 0; c1 < update_count; c1++)
398 /* Update existing performance information */
400 for (c2 = 0; c2 < dest->atsi_count; c2++)
402 if (update[c1].type == dest->atsi[c2].type)
404 if (update[c1].value != dest->atsi[c2].value)
406 /* Save previous value in delta */
407 delta_atsi[delta_atsi_count] = dest->atsi[c2];
410 dest->atsi[c2].value = update[c1].value;
417 if (GNUNET_NO == found)
419 add_atsi[add_atsi_count] = update[c1];
421 delta_atsi[delta_atsi_count].type = update[c1].type;
422 delta_atsi[delta_atsi_count].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
427 if (add_atsi_count > 0)
429 /* Extend ats performance information */
431 tmp_atsi = GNUNET_malloc ((dest->atsi_count + add_atsi_count) *
432 (sizeof (struct GNUNET_ATS_Information)));
433 memcpy (tmp_atsi, dest->atsi,
434 dest->atsi_count * sizeof(struct GNUNET_ATS_Information));
435 memcpy (&tmp_atsi[dest->atsi_count], add_atsi,
436 add_atsi_count * sizeof(struct GNUNET_ATS_Information));
437 GNUNET_free(dest->atsi);
438 dest->atsi = tmp_atsi;
439 dest->atsi_count = dest->atsi_count + add_atsi_count;
443 if (delta_atsi_count > 0)
447 GNUNET_malloc (delta_atsi_count * sizeof (struct GNUNET_ATS_Information));
448 memcpy ((*delta_dest), delta_atsi,
449 delta_atsi_count * sizeof(struct GNUNET_ATS_Information));
450 (*delta_count) = delta_atsi_count;
458 * Free the given address
460 * @param addr address to destroy
463 free_address (struct ATS_Address *addr)
465 GNUNET_free (addr->plugin);
466 GNUNET_free_non_null (addr->atsi);
472 * Create a ATS_address with the given information
475 * @param plugin_name plugin
476 * @param plugin_addr address
477 * @param plugin_addr_len address length
478 * @param local_address_info additional local info for the address
479 * @param session_id session identifier, can never be 0
480 * @return the ATS_Address
482 static struct ATS_Address *
483 create_address (const struct GNUNET_PeerIdentity *peer,
484 const char *plugin_name,
485 const void *plugin_addr,
486 size_t plugin_addr_len,
487 uint32_t local_address_info,
490 struct ATS_Address *aa;
494 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
496 aa->addr_len = plugin_addr_len;
498 memcpy (&aa[1], plugin_addr, plugin_addr_len);
499 aa->plugin = GNUNET_strdup (plugin_name);
500 aa->session_id = session_id;
501 aa->local_address_info = local_address_info;
503 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
505 aa->atsin[c1].avg_queue_index = 0;
506 for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
507 aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
514 * Closure for #compare_address_it()
516 struct CompareAddressContext
518 const struct ATS_Address *search;
520 /* exact_address != NULL if address and session is equal */
521 struct ATS_Address *exact_address;
522 /* exact_address != NULL if address and session is 0 */
523 struct ATS_Address *base_address;
530 * @param cls a CompareAddressContext containin the source address
532 * @param value the address to compare with
533 * @return #GNUNET_YES to continue, #GNUNET_NO if address is founce
536 compare_address_it (void *cls,
537 const struct GNUNET_PeerIdentity *key,
540 struct CompareAddressContext *cac = cls;
541 struct ATS_Address *aa = value;
543 /* Find an matching exact address:
546 * aa->addr_len == cac->search->addr_len
547 * aa->plugin == cac->search->plugin
548 * aa->addr == cac->search->addr
549 * aa->session == cac->search->session
551 * return as exact address
553 if ((aa->addr_len == cac->search->addr_len)
554 && (0 == strcmp (aa->plugin, cac->search->plugin)))
556 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
557 && (aa->session_id == cac->search->session_id))
558 cac->exact_address = aa;
561 /* Find an matching base address:
565 * aa->session_id == 0
568 * aa->addr_len == cac->search->addr_len
569 * aa->plugin == cac->search->plugin
570 * aa->addr == cac->search->addr
572 * return as base address
574 if ((aa->addr_len == cac->search->addr_len)
575 && (0 == strcmp (aa->plugin, cac->search->plugin)))
577 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
578 && (aa->session_id == 0))
579 cac->base_address = aa;
582 /* Find an matching exact address based on session:
586 * cac->search->addr_len == 0
589 * aa->plugin == cac->search->plugin
590 * aa->session_id == cac->search->session_id
592 * return as exact address
594 if (0 == cac->search->addr_len)
596 if ((0 == strcmp (aa->plugin, cac->search->plugin))
597 && (aa->session_id == cac->search->session_id))
598 cac->exact_address = aa;
601 if (cac->exact_address == NULL )
602 return GNUNET_YES; /* Continue iteration to find exact address */
604 return GNUNET_NO; /* Stop iteration since we have an exact address */
609 * Find an existing equivalent address record.
610 * Compares by peer identity and network address OR by session ID
611 * (one of the two must match).
613 * @param peer peer to lookup addresses for
614 * @param addr existing address record
615 * @return existing address record, NULL for none
618 find_equivalent_address (const struct GNUNET_PeerIdentity *peer,
619 const struct ATS_Address *addr)
621 struct CompareAddressContext cac;
623 cac.exact_address = NULL;
624 cac.base_address = NULL;
626 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
628 &compare_address_it, &cac);
630 if (NULL == cac.exact_address)
631 return cac.base_address;
632 return cac.exact_address;
637 * Closure for #find_address_cb()
639 struct FindAddressContext
642 * Session Id to look for.
647 * Where to store matching address result.
649 struct ATS_Address *exact_address;
655 * Find session matching given session ID.
657 * @param cls a `struct FindAddressContext`
659 * @param value the address to compare with
660 * @return #GNUNET_YES to continue, #GNUNET_NO if address is found
663 find_address_cb (void *cls,
664 const struct GNUNET_PeerIdentity *key,
667 struct FindAddressContext *fac = cls;
668 struct ATS_Address *aa = value;
670 if (aa->session_id == fac->session_id)
672 fac->exact_address = aa;
680 * Find the exact address
683 * @param session_id session id, can never be 0
684 * @return an ATS_address or NULL
686 static struct ATS_Address *
687 find_exact_address (const struct GNUNET_PeerIdentity *peer,
690 struct FindAddressContext fac;
692 fac.exact_address = NULL;
693 fac.session_id = session_id;
694 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
696 &find_address_cb, &fac);
697 return fac.exact_address;
702 * Function allowing the solver to obtain normalized preference
706 * @param id the peer to return the normalized properties for
707 * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
710 get_preferences_cb (void *cls,
711 const struct GNUNET_PeerIdentity *id)
713 return GAS_normalization_get_preferences_by_peer (id);
718 * Function allowing the solver to obtain normalized property
719 * values for an address from solver
722 * @param address the address
723 * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
726 get_property_cb (void *cls, const struct ATS_Address *address)
728 return GAS_normalization_get_properties ((struct ATS_Address *) address);
733 * Extract an ATS performance info from an address
735 * @param address the address
736 * @param type the type to extract in HBO
737 * @return the value in HBO or #GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
740 get_performance_info (struct ATS_Address *address, uint32_t type)
743 GNUNET_assert(NULL != address);
745 if ((NULL == address->atsi) || (0 == address->atsi_count))
746 return GNUNET_ATS_VALUE_UNDEFINED;
748 for (c1 = 0; c1 < address->atsi_count; c1++)
750 if (ntohl (address->atsi[c1].type) == type)
751 return ntohl (address->atsi[c1].value);
753 return GNUNET_ATS_VALUE_UNDEFINED;
758 * Add a new address for a peer.
761 * @param plugin_name transport plugin name
762 * @param plugin_addr plugin address
763 * @param plugin_addr_len length of the plugin address in @a plugin_addr
764 * @param local_address_info the local address for the address
765 * @param session_id session id, can be 0
766 * @param atsi performance information for this address
767 * @param atsi_count number of performance information contained in @a atsi
770 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
771 const char *plugin_name,
772 const void *plugin_addr,
773 size_t plugin_addr_len,
774 uint32_t local_address_info,
776 const struct GNUNET_ATS_Information *atsi,
779 struct ATS_Address *new_address;
780 struct ATS_Address *existing_address;
781 struct GNUNET_ATS_Information *atsi_delta;
782 uint32_t atsi_delta_count;
784 uint32_t previous_session;
787 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
788 "Received `%s' for peer `%s'\n",
792 if (GNUNET_NO == running)
795 GNUNET_assert(NULL != addresses);
797 new_address = create_address (peer, plugin_name,
798 plugin_addr, plugin_addr_len,
799 local_address_info, session_id);
801 disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
803 GNUNET_free_non_null (atsi_delta);
804 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
805 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
806 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
808 /* Get existing address or address with session == 0 */
809 existing_address = find_equivalent_address (peer, new_address);
810 if (NULL == existing_address)
812 /* Add a new address */
813 new_address->t_added = GNUNET_TIME_absolute_get();
814 new_address->t_last_activity = GNUNET_TIME_absolute_get();
815 GNUNET_assert(GNUNET_OK ==
816 GNUNET_CONTAINER_multipeermap_put (addresses,
819 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
821 GNUNET_STATISTICS_set (stats,
823 GNUNET_CONTAINER_multipeermap_size (addresses),
826 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
827 "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
832 GNUNET_ATS_print_network_type (addr_net));
834 /* Tell solver about new address */
835 env.sf.s_add (solver, new_address, addr_net);
837 env.sf.s_bulk_start (solver);
838 GAS_normalization_normalize_property (addresses,
842 env.sf.s_bulk_stop (solver);
844 /* Notify performance clients about new address */
845 GAS_performance_notify_all_clients (&new_address->peer, new_address->plugin,
846 new_address->addr, new_address->addr_len, new_address->active,
847 new_address->atsi, new_address->atsi_count,
848 GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_out),
849 GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_in));
853 /* FIXME: this case should probably not be allowed... */
854 /* We have an existing address we can use, clean up new */
855 GNUNET_free(new_address->plugin);
856 GNUNET_free_non_null(new_address->atsi);
857 GNUNET_free(new_address);
860 if (0 != existing_address->session_id)
862 /* Should not happen */
867 addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
868 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
869 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
871 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
872 "Found existing address for peer `%s' %p with new session %u in network %s\n",
873 GNUNET_i2s (peer), existing_address, session_id,
874 GNUNET_ATS_print_network_type (addr_net));
875 /* We have an address without an session, update this address */
876 existing_address->t_added = GNUNET_TIME_absolute_get();
877 existing_address->t_last_activity = GNUNET_TIME_absolute_get();
879 atsi_delta_count = 0;
881 == disassemble_ats_information (existing_address, atsi, atsi_count,
882 &atsi_delta, &atsi_delta_count))
884 /* Notify performance clients about properties */
885 GAS_performance_notify_all_clients (&existing_address->peer,
886 existing_address->plugin, existing_address->addr,
887 existing_address->addr_len, existing_address->active,
888 existing_address->atsi, existing_address->atsi_count,
889 GNUNET_BANDWIDTH_value_init (existing_address->assigned_bw_out),
890 GNUNET_BANDWIDTH_value_init (existing_address->assigned_bw_in));
892 for (c1 = 0; c1 < atsi_delta_count; c1++)
894 if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
895 && (addr_net != ntohl (atsi_delta[c1].value)))
897 /* Network type changed */
898 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
899 "Address for peer `%s' %p changed from network %s to %s\n",
900 GNUNET_i2s (peer), existing_address,
901 GNUNET_ATS_print_network_type (addr_net),
902 GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
903 env.sf.s_address_update_network (solver, existing_address,
904 ntohl (atsi_delta[c1].value),
905 get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
906 addr_net = get_performance_info (existing_address,
907 GNUNET_ATS_NETWORK_TYPE);
910 /* Notify solver about update with atsi information and session */
911 env.sf.s_bulk_start (solver);
912 GAS_normalization_normalize_property (addresses, existing_address,
914 env.sf.s_bulk_stop (solver);
916 GNUNET_free_non_null(atsi_delta);
918 /* Notify solver about new session */
919 if (existing_address->session_id == session_id)
920 return; /* possible, can both be 0 since address is revalidated */
922 previous_session = existing_address->session_id;
923 existing_address->session_id = session_id;
924 env.sf.s_address_update_session (solver, existing_address,
925 previous_session, session_id);
927 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
928 "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
929 GNUNET_i2s (peer), existing_address, existing_address->addr_len,
930 session_id, GNUNET_ATS_print_network_type (addr_net));
935 * Update an address with new performance information for a peer.
938 * @param session_id session id, never 0
939 * @param atsi performance information for this address
940 * @param atsi_count number of performance information contained in @a atsi
943 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
945 const struct GNUNET_ATS_Information *atsi,
948 struct ATS_Address *aa;
949 struct GNUNET_ATS_Information *atsi_delta;
950 uint32_t atsi_delta_count;
951 uint32_t prev_session;
954 if (GNUNET_NO == running)
956 GNUNET_assert (NULL != addresses);
958 /* Get existing address */
959 aa = find_exact_address (peer,
966 if (NULL == aa->solver_information)
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "Received `%s' for peer `%s' address \n",
979 aa->t_last_activity = GNUNET_TIME_absolute_get();
980 if (session_id != aa->session_id)
982 /* Session changed */
983 prev_session = aa->session_id;
984 aa->session_id = session_id;
985 env.sf.s_address_update_session (solver,
992 atsi_delta_count = 0;
994 disassemble_ats_information (aa, atsi,
999 /* ATS properties changed */
1000 for (c1 = 0; c1 < atsi_delta_count; c1++)
1002 if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
1004 /* Network type changed */
1005 env.sf.s_address_update_network (solver, aa,
1006 ntohl (atsi_delta[c1].value),
1007 get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
1011 /* Notify performance clients about updated address */
1012 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr,
1013 aa->addr_len, aa->active, aa->atsi, aa->atsi_count,
1014 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
1015 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
1017 env.sf.s_bulk_start (solver);
1018 GAS_normalization_normalize_property (addresses,
1022 env.sf.s_bulk_stop (solver);
1024 GNUNET_free_non_null (atsi_delta);
1029 * Remove an address or just a session for a peer.
1032 * @param session_id session id, can never be 0
1035 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
1036 uint32_t session_id)
1038 struct ATS_Address *ea;
1040 if (GNUNET_NO == running)
1043 /* Get existing address */
1044 ea = find_exact_address (peer,
1049 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1050 "Tried to destroy unknown address for peer `%s' session id %u\n",
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057 "Received `%s' for peer `%s' address %p session %u\n",
1058 "ADDRESS DESTROYED",
1062 GNUNET_CONTAINER_multipeermap_remove (addresses,
1066 env.sf.s_del (solver, ea, GNUNET_NO);
1067 GAS_performance_notify_all_clients (peer,
1076 GNUNET_STATISTICS_set (stats,
1078 GNUNET_CONTAINER_multipeermap_size (addresses),
1084 * Notification about active use of an address.
1085 * in_use == #GNUNET_YES:
1086 * This address is used to maintain an active connection with a peer.
1087 * in_use == #GNUNET_NO:
1088 * This address is no longer used to maintain an active connection with a peer.
1090 * Note: can only be called with in_use == #GNUNET_NO if called with #GNUNET_YES
1094 * @param session_id session id, can be 0
1095 * @param in_use #GNUNET_YES if #GNUNET_NO FIXME
1096 * @return #GNUNET_SYSERR on failure (address unknown ...)
1099 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
1100 uint32_t session_id,
1103 struct ATS_Address *ea;
1105 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1106 "Received `%s' for peer `%s'\n",
1109 if (GNUNET_NO == running)
1110 return GNUNET_SYSERR;
1111 ea = find_exact_address (peer,
1115 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1116 "Trying to set unknown address `%s' `%u' to %s \n",
1119 (GNUNET_NO == in_use) ? "NO" : "YES");
1121 return GNUNET_SYSERR;
1123 if (ea->used == in_use)
1126 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1127 "Address in use called multiple times for peer `%s': %s -> %s \n",
1129 (GNUNET_NO == ea->used) ? "NO" : "YES",
1130 (GNUNET_NO == in_use) ? "NO" : "YES");
1131 return GNUNET_SYSERR;
1133 /* Tell solver about update */
1135 ea->t_last_activity = GNUNET_TIME_absolute_get();
1136 env.sf.s_address_update_inuse (solver,
1144 * Cancel address suggestions for a peer
1146 * @param peer the peer id
1149 GAS_addresses_request_address_cancel (const struct GNUNET_PeerIdentity *peer)
1151 struct GAS_Addresses_Suggestion_Requests *cur = pending_requests_head;
1153 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1154 "Received request: `%s' for peer %s\n",
1155 "request_address_cancel",
1160 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1167 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1168 "No address requests pending for peer `%s', cannot remove!\n",
1172 env.sf.s_get_stop (solver, peer);
1173 GAS_addresses_handle_backoff_reset (peer);
1174 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1175 "Removed request pending for peer `%s\n",
1177 GNUNET_CONTAINER_DLL_remove (pending_requests_head,
1178 pending_requests_tail,
1185 * Request address suggestions for a peer
1187 * @param peer the peer id
1190 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
1192 struct GAS_Addresses_Suggestion_Requests *cur = pending_requests_head;
1193 struct ATS_Address *aa;
1195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1196 "Received `%s' for peer `%s'\n",
1200 if (GNUNET_NO == running)
1204 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1205 break; /* already suggesting */
1210 cur = GNUNET_new (struct GAS_Addresses_Suggestion_Requests);
1212 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1213 "Adding new address suggestion request for `%s'\n",
1215 GNUNET_CONTAINER_DLL_insert (pending_requests_head,
1216 pending_requests_tail,
1220 /* Get prefered address from solver */
1221 aa = (struct ATS_Address *) env.sf.s_get (solver, peer);
1224 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1229 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1230 aa, GNUNET_i2s (peer));
1232 GAS_scheduling_transmit_address_suggestion (peer,
1234 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
1235 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
1237 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1238 ATS_BLOCKING_DELTA);
1239 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1240 aa->block_interval);
1242 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1243 "Address %p ready for suggestion, block interval now %llu \n", aa,
1244 aa->block_interval);
1248 * Iterator to reset address blocking
1250 * @param cls not used
1251 * @param key the peer
1252 * @param value the address to reset
1253 * @return #GNUNET_OK to continue
1256 reset_address_it (void *cls,
1257 const struct GNUNET_PeerIdentity *key,
1260 struct ATS_Address *aa = value;
1262 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1263 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1264 GNUNET_i2s (&aa->peer),
1266 aa->block_interval);
1267 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1268 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1274 * Reset suggestion backoff for a peer
1276 * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1277 * reset using this function
1279 * @param peer the peer id
1282 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
1284 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1285 "Received `%s' for peer `%s'\n",
1289 GNUNET_break(GNUNET_SYSERR !=
1290 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
1298 * Solver information callback
1300 * @param cls the closure
1301 * @param op the operation
1302 * @param status operation status
1303 * @param add additional information
1306 solver_info_cb (void *cls,
1307 enum GAS_Solver_Operation op,
1308 enum GAS_Solver_Status status,
1309 enum GAS_Solver_Additional_Information add)
1315 add_info = "GAS_INFO_NONE";
1318 add_info = "GAS_INFO_MLP_FULL";
1320 case GAS_INFO_UPDATED:
1321 add_info = "GAS_INFO_MLP_UPDATED";
1323 case GAS_INFO_PROP_ALL:
1324 add_info = "GAS_INFO_PROP_ALL";
1326 case GAS_INFO_PROP_SINGLE:
1327 add_info = "GAS_INFO_PROP_SINGLE";
1330 add_info = "INVALID";
1335 case GAS_OP_SOLVE_START:
1336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1337 "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
1338 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL", add_info);
1340 case GAS_OP_SOLVE_STOP:
1341 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1342 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
1343 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL", add_info);
1346 case GAS_OP_SOLVE_SETUP_START:
1347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1348 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
1349 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1352 case GAS_OP_SOLVE_SETUP_STOP:
1353 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1354 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
1355 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1358 case GAS_OP_SOLVE_MLP_LP_START:
1359 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1360 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
1361 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1363 case GAS_OP_SOLVE_MLP_LP_STOP:
1364 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1365 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
1366 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1369 case GAS_OP_SOLVE_MLP_MLP_START:
1370 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1371 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
1372 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1374 case GAS_OP_SOLVE_MLP_MLP_STOP:
1375 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1376 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
1377 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1379 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
1380 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1381 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
1382 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1384 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
1385 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1386 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
1387 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1396 * The preference changed for a peer
1399 * @param peer the peer
1400 * @param kind the ATS kind
1401 * @param pref_rel the new relative preference value
1404 normalized_preference_changed_cb (void *cls,
1405 const struct GNUNET_PeerIdentity *peer,
1406 enum GNUNET_ATS_PreferenceKind kind,
1409 /* Tell solver about update */
1410 env.sf.s_pref (solver, peer, kind, pref_rel);
1415 * The relative value for a property changed
1418 * @param address the peer
1419 * @param type the ATS type
1420 * @param prop_rel the new relative preference value
1423 normalized_property_changed_cb (void *cls,
1424 struct ATS_Address *address,
1428 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1429 "Normalized property %s for peer `%s' changed to %.3f \n",
1430 GNUNET_ATS_print_property_type (type),
1431 GNUNET_i2s (&address->peer),
1433 env.sf.s_address_update_property (solver,
1441 static struct GAS_Addresses_Preference_Clients *
1442 find_preference_client (void *client)
1444 struct GAS_Addresses_Preference_Clients *cur;
1446 for (cur = preference_clients_head; NULL != cur; cur = cur->next)
1447 if (cur->client == client)
1454 * A performance client disconnected
1456 * @param client the client
1459 GAS_addresses_preference_client_disconnect (void *client)
1461 struct GAS_Addresses_Preference_Clients *pc;
1463 if (NULL != (pc = find_preference_client (client)))
1465 GNUNET_CONTAINER_DLL_remove (preference_clients_head,
1466 preference_clients_tail,
1469 GNUNET_assert (pref_clients > 0);
1471 GNUNET_STATISTICS_set (stats,
1472 "# active performance clients",
1476 GAS_normalization_preference_client_disconnect (client);
1481 * Change the preference for a peer
1483 * @param client the client sending this request
1484 * @param peer the peer id
1485 * @param kind the preference kind to change
1486 * @param score_abs the new preference score
1489 GAS_addresses_preference_change (void *client,
1490 const struct GNUNET_PeerIdentity *peer,
1491 enum GNUNET_ATS_PreferenceKind kind,
1494 struct GAS_Addresses_Preference_Clients *pc;
1496 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1497 "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1498 GNUNET_i2s (peer), client);
1500 if (GNUNET_NO == running)
1504 GNUNET_CONTAINER_multipeermap_contains (addresses,
1507 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1508 "Received `%s' for unknown peer `%s' from client %p\n",
1509 "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1513 if (NULL == find_preference_client (client))
1515 pc = GNUNET_new (struct GAS_Addresses_Preference_Clients);
1516 pc->client = client;
1517 GNUNET_CONTAINER_DLL_insert (preference_clients_head,
1518 preference_clients_tail,
1521 GNUNET_STATISTICS_set (stats,
1522 "# active performance clients",
1527 env.sf.s_bulk_start (solver);
1528 /* Tell normalization about change, normalization will call callback if preference changed */
1529 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1530 env.sf.s_bulk_stop (solver);
1535 * Change the preference for a peer
1537 * @param application the client sending this request
1538 * @param peer the peer id
1539 * @param scope the time interval for this feedback: [now - scope .. now]
1540 * @param kind the preference kind to change
1541 * @param score_abs the new preference score
1544 GAS_addresses_preference_feedback (void *application,
1545 const struct GNUNET_PeerIdentity *peer,
1546 const struct GNUNET_TIME_Relative scope,
1547 enum GNUNET_ATS_PreferenceKind kind,
1550 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1551 "Received `%s' for peer `%s' for client %p\n",
1552 "PREFERENCE FEEDBACK",
1556 if (GNUNET_NO == running)
1560 GNUNET_CONTAINER_multipeermap_contains (addresses,
1563 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1564 "Received `%s' for unknown peer `%s' from client %p\n",
1565 "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1569 env.sf.s_feedback (solver, application, peer, scope, kind,
1575 * Load quotas for networks from configuration
1577 * @param cfg configuration handle
1578 * @param out_dest where to write outbound quotas
1579 * @param in_dest where to write inbound quotas
1580 * @param dest_length length of inbound and outbound arrays
1581 * @return number of networks loaded
1584 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1585 unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1587 char * entry_in = NULL;
1588 char * entry_out = NULL;
1589 char * quota_out_str;
1590 char * quota_in_str;
1594 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1598 GNUNET_asprintf (&entry_out,
1600 GNUNET_ATS_print_network_type (c));
1601 GNUNET_asprintf (&entry_in,
1603 GNUNET_ATS_print_network_type (c));
1607 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
1611 if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
1613 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1616 if ((GNUNET_NO == res)
1618 == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
1621 if ((GNUNET_NO == res)
1623 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
1627 if (GNUNET_NO == res)
1629 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1630 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1631 GNUNET_ATS_print_network_type (c),
1633 GNUNET_ATS_DefaultBandwidth);
1634 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1638 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1639 _("Outbound quota configure for network `%s' is %llu\n"),
1640 GNUNET_ATS_print_network_type (c),
1643 GNUNET_free(quota_out_str);
1647 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1648 _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1649 GNUNET_ATS_print_network_type (c),
1650 GNUNET_ATS_DefaultBandwidth);
1651 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1656 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
1660 if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
1662 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1665 if ((GNUNET_NO == res)
1667 == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1669 if ((GNUNET_NO == res)
1671 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
1675 if (GNUNET_NO == res)
1677 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1678 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1679 GNUNET_ATS_print_network_type (c),
1681 GNUNET_ATS_DefaultBandwidth);
1682 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1686 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1687 _("Inbound quota configured for network `%s' is %llu\n"),
1688 GNUNET_ATS_print_network_type (c),
1691 GNUNET_free(quota_in_str);
1695 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1696 _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1697 GNUNET_ATS_print_network_type (c),
1698 GNUNET_ATS_DefaultBandwidth);
1699 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1701 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1702 "Loaded quota for network `%s' (in/out): %llu %llu\n",
1703 GNUNET_ATS_print_network_type (c),
1706 GNUNET_free(entry_out);
1707 GNUNET_free(entry_in);
1709 return GNUNET_ATS_NetworkTypeCount;
1714 * Callback for solver to notify about assignment changes
1717 * @param address the address with changes
1720 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1722 struct GAS_Addresses_Suggestion_Requests *cur;
1726 GNUNET_assert(address != NULL);
1727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1728 "Bandwidth assignment changed for peer %s \n",
1729 GNUNET_i2s (&address->peer));
1731 /* Notify performance clients about changes to address */
1732 GAS_performance_notify_all_clients (&address->peer, address->plugin,
1733 address->addr, address->addr_len, address->active, address->atsi,
1734 address->atsi_count,
1735 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
1736 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
1738 for (cur = pending_requests_head;NULL != cur; cur = cur->next)
1739 if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
1740 break; /* we have an address request pending*/
1743 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1744 "Nobody is interested in peer `%s' :(\n",
1745 GNUNET_i2s (&address->peer));
1749 if ((0 == address->assigned_bw_in) && (0 == address->assigned_bw_out))
1751 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1752 "Telling transport to disconnect peer `%s'\n",
1753 GNUNET_i2s (&address->peer));
1755 /* Notify scheduling clients about suggestion */
1756 GAS_scheduling_transmit_address_suggestion (&address->peer,
1757 address->session_id,
1758 GNUNET_BANDWIDTH_value_init (0),
1759 GNUNET_BANDWIDTH_value_init (0));
1763 /* Do bandwidth stability check */
1764 diff_out = abs (address->assigned_bw_out - address->last_notified_bw_out);
1765 diff_in = abs (address->assigned_bw_in - address->last_notified_bw_in);
1767 if ( (diff_out < htonl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) &&
1768 (diff_in < htonl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) )
1771 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1772 "Sending bandwidth update for peer `%s': %u %u\n",
1773 GNUNET_i2s (&address->peer), address->assigned_bw_out,
1774 address->assigned_bw_out);
1776 /* *Notify scheduling clients about suggestion */
1777 GAS_scheduling_transmit_address_suggestion (&address->peer,
1778 address->session_id,
1779 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
1780 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
1782 address->last_notified_bw_out = address->assigned_bw_out;
1783 address->last_notified_bw_in = address->assigned_bw_in;
1788 * Initialize address subsystem. The addresses subsystem manages the addresses
1789 * known and current performance information. It has a solver component
1790 * responsible for the resource allocation. It tells the solver about changes
1791 * and receives updates when the solver changes the resource allocation.
1793 * @param cfg configuration to use
1794 * @param stats_ the statistics handle to use
1795 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (failed to load
1799 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1800 struct GNUNET_STATISTICS_Handle *stats_)
1802 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1803 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1808 running = GNUNET_NO;
1810 /* Initialize the addresses database */
1811 addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1814 /* Figure out configured solution method */
1815 if (GNUNET_SYSERR ==
1816 GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1818 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1819 "No resource assignment method configured, using proportional approach\n");
1820 ats_mode = MODE_PROPORTIONAL;
1824 for (c = 0; c < strlen (mode_str); c++)
1825 mode_str[c] = toupper (mode_str[c]);
1826 if (0 == strcmp (mode_str, "PROPORTIONAL"))
1827 ats_mode = MODE_PROPORTIONAL;
1828 else if (0 == strcmp (mode_str, "MLP"))
1830 ats_mode = MODE_MLP;
1832 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1833 "Assignment method `%s' configured, but GLPK is not available, please install \n",
1835 ats_mode = MODE_PROPORTIONAL;
1838 else if (0 == strcmp (mode_str, "RIL"))
1839 ats_mode = MODE_RIL;
1842 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1843 "Invalid resource assignment method `%s' configured, using proportional approach\n",
1845 ats_mode = MODE_PROPORTIONAL;
1847 GNUNET_free(mode_str);
1850 load_quotas (cfg, quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount);
1851 env.info_cb = &solver_info_cb;
1852 env.info_cb_cls = NULL;
1853 env.bandwidth_changed_cb = &bandwidth_changed_cb;
1854 env.bw_changed_cb_cls = NULL;
1855 env.get_preferences = &get_preferences_cb;
1856 env.get_preference_cls = NULL;
1857 env.get_property = &get_property_cb;
1858 env.get_property_cls = NULL;
1861 env.addresses = addresses;
1863 env.network_count = GNUNET_ATS_NetworkTypeCount;
1864 int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1865 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1867 env.networks[c] = networks[c];
1868 env.out_quota[c] = quotas_out[c];
1869 env.in_quota[c] = quotas_in[c];
1873 case MODE_PROPORTIONAL:
1874 plugin_short = "proportional";
1877 plugin_short = "mlp";
1880 plugin_short = "ril";
1883 plugin_short = NULL;
1886 GNUNET_asprintf (&plugin,
1887 "libgnunet_plugin_ats_%s",
1889 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1890 _("Initializing solver `%s '`%s'\n"),
1893 if (NULL == (solver = GNUNET_PLUGIN_load (plugin, &env)))
1895 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1896 _("Failed to initialize solver `%s'!\n"),
1898 return GNUNET_SYSERR;
1901 GNUNET_assert (NULL != env.sf.s_add);
1902 GNUNET_assert (NULL != env.sf.s_address_update_inuse);
1903 GNUNET_assert (NULL != env.sf.s_address_update_property);
1904 GNUNET_assert (NULL != env.sf.s_address_update_session);
1905 GNUNET_assert (NULL != env.sf.s_address_update_network);
1906 GNUNET_assert (NULL != env.sf.s_get);
1907 GNUNET_assert (NULL != env.sf.s_get_stop);
1908 GNUNET_assert (NULL != env.sf.s_pref);
1909 GNUNET_assert (NULL != env.sf.s_feedback);
1910 GNUNET_assert (NULL != env.sf.s_del);
1911 GNUNET_assert (NULL != env.sf.s_bulk_start);
1912 GNUNET_assert (NULL != env.sf.s_bulk_stop);
1915 GAS_normalization_start (&normalized_preference_changed_cb, NULL,
1916 &normalized_property_changed_cb, NULL);
1920 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1921 _("Failed to initialize solver!\n"));
1922 return GNUNET_SYSERR;
1924 /* up and running */
1925 running = GNUNET_YES;
1927 GNUNET_STATISTICS_set (stats,
1929 GNUNET_CONTAINER_multipeermap_size (addresses),
1937 * Destroy all addresses iterator
1940 * @param key peer identity (unused)
1941 * @param value the 'struct ATS_Address' to free
1942 * @return #GNUNET_OK (continue to iterate)
1945 destroy_all_address_it (void *cls,
1946 const struct GNUNET_PeerIdentity *key,
1949 struct ATS_Address *aa = value;
1952 GNUNET_assert(GNUNET_YES ==
1953 GNUNET_CONTAINER_multipeermap_remove (addresses,
1957 env.sf.s_del (solver, aa, GNUNET_NO);
1959 GAS_performance_notify_all_clients (&aa->peer,
1973 * Remove all addresses
1976 GAS_addresses_destroy_all ()
1978 if (GNUNET_NO == running)
1981 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1982 "Destroying all addresses\n");
1983 env.sf.s_bulk_start (solver);
1984 if (NULL != addresses)
1985 GNUNET_CONTAINER_multipeermap_iterate (addresses,
1986 &destroy_all_address_it,
1988 env.sf.s_bulk_start (solver);
1993 * Shutdown address subsystem.
1996 GAS_addresses_done ()
1998 struct GAS_Addresses_Suggestion_Requests *cur;
1999 struct GAS_Addresses_Preference_Clients *pcur;
2001 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2002 "Shutting down addresses\n");
2003 GAS_addresses_destroy_all ();
2004 running = GNUNET_NO;
2005 GNUNET_CONTAINER_multipeermap_destroy (addresses);
2007 while (NULL != (cur = pending_requests_head))
2009 GNUNET_CONTAINER_DLL_remove (pending_requests_head,
2010 pending_requests_tail,
2015 while (NULL != (pcur = preference_clients_head))
2017 GNUNET_CONTAINER_DLL_remove (preference_clients_head,
2018 preference_clients_tail,
2020 GNUNET_assert (pref_clients > 0);
2022 GNUNET_STATISTICS_set (stats,
2023 "# active performance clients",
2028 GNUNET_PLUGIN_unload (plugin,
2030 GNUNET_free (plugin);
2031 /* Stop configured solution method */
2032 GAS_normalization_stop ();
2037 * Closure for #peerinfo_it().
2039 struct PeerInfoIteratorContext
2042 * Function to call for each address.
2044 GNUNET_ATS_PeerInfo_Iterator it;
2047 * Closure for @e it.
2054 * Iterator to iterate over a peer's addresses
2056 * @param cls a `struct PeerInfoIteratorContext`
2057 * @param key the peer id
2058 * @param value the `struct ATS_address`
2059 * @return #GNUNET_OK to continue
2062 peerinfo_it (void *cls,
2063 const struct GNUNET_PeerIdentity *key,
2066 struct PeerInfoIteratorContext *pi_ctx = cls;
2067 struct ATS_Address *addr = value;
2069 pi_ctx->it (pi_ctx->it_cls,
2075 addr->atsi, addr->atsi_count,
2076 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_out),
2077 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_in));
2083 * Return information all peers currently known to ATS
2085 * @param peer the respective peer, NULL for 'all' peers
2086 * @param pi_it the iterator to call for every peer
2087 * @param pi_it_cls the closure for @a pi_it
2090 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer,
2091 GNUNET_ATS_PeerInfo_Iterator pi_it,
2094 struct PeerInfoIteratorContext pi_ctx;
2098 /* does not make sense without callback */
2102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2103 "Returning information for %s from a total of %u known addresses\n",
2106 : GNUNET_i2s (peer),
2107 (unsigned int) GNUNET_CONTAINER_multipeermap_size (addresses));
2109 pi_ctx.it_cls = pi_it_cls;
2111 GNUNET_CONTAINER_multipeermap_iterate (addresses,
2115 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
2117 &peerinfo_it, &pi_ctx);
2119 NULL, NULL, NULL, 0,
2126 /* end of gnunet-service-ats_addresses.c */