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,
727 const struct ATS_Address *address)
729 return GAS_normalization_get_properties (address);
734 * Extract an ATS performance info from an address
736 * @param address the address
737 * @param type the type to extract in HBO
738 * @return the value in HBO or #GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
741 get_performance_info (struct ATS_Address *address, uint32_t type)
744 GNUNET_assert(NULL != address);
746 if ((NULL == address->atsi) || (0 == address->atsi_count))
747 return GNUNET_ATS_VALUE_UNDEFINED;
749 for (c1 = 0; c1 < address->atsi_count; c1++)
751 if (ntohl (address->atsi[c1].type) == type)
752 return ntohl (address->atsi[c1].value);
754 return GNUNET_ATS_VALUE_UNDEFINED;
759 * Add a new address for a peer.
762 * @param plugin_name transport plugin name
763 * @param plugin_addr plugin address
764 * @param plugin_addr_len length of the plugin address in @a plugin_addr
765 * @param local_address_info the local address for the address
766 * @param session_id session id, can be 0
767 * @param atsi performance information for this address
768 * @param atsi_count number of performance information contained in @a atsi
771 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
772 const char *plugin_name,
773 const void *plugin_addr,
774 size_t plugin_addr_len,
775 uint32_t local_address_info,
777 const struct GNUNET_ATS_Information *atsi,
780 struct ATS_Address *new_address;
781 struct ATS_Address *existing_address;
782 struct GNUNET_ATS_Information *atsi_delta;
783 uint32_t atsi_delta_count;
785 uint32_t previous_session;
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "Received `%s' for peer `%s'\n",
792 if (GNUNET_NO == running)
797 new_address = create_address (peer,
804 disassemble_ats_information (new_address,
808 GNUNET_free_non_null (atsi_delta);
809 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
810 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
811 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
813 /* Get existing address or address with session == 0 */
814 existing_address = find_equivalent_address (peer, new_address);
815 if (NULL == existing_address)
817 /* Add a new address */
818 new_address->t_added = GNUNET_TIME_absolute_get();
819 new_address->t_last_activity = GNUNET_TIME_absolute_get();
820 GNUNET_assert(GNUNET_OK ==
821 GNUNET_CONTAINER_multipeermap_put (addresses,
824 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
826 GNUNET_STATISTICS_set (stats,
828 GNUNET_CONTAINER_multipeermap_size (addresses),
831 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
832 "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
837 GNUNET_ATS_print_network_type (addr_net));
839 /* Tell solver about new address */
840 env.sf.s_add (solver, new_address, addr_net);
842 env.sf.s_bulk_start (solver);
843 GAS_normalization_normalize_property (addresses,
847 env.sf.s_bulk_stop (solver);
849 /* Notify performance clients about new address */
850 GAS_performance_notify_all_clients (&new_address->peer, new_address->plugin,
851 new_address->addr, new_address->addr_len, new_address->active,
852 new_address->atsi, new_address->atsi_count,
853 GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_out),
854 GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_in));
858 /* FIXME: this case should probably not be allowed... */
859 /* We have an existing address we can use, clean up new */
860 GNUNET_free(new_address->plugin);
861 GNUNET_free_non_null(new_address->atsi);
862 GNUNET_free(new_address);
865 if (0 != existing_address->session_id)
867 /* Should not happen */
872 addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
873 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
874 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
876 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
877 "Found existing address for peer `%s' %p with new session %u in network %s\n",
878 GNUNET_i2s (peer), existing_address, session_id,
879 GNUNET_ATS_print_network_type (addr_net));
880 /* We have an address without an session, update this address */
881 existing_address->t_added = GNUNET_TIME_absolute_get();
882 existing_address->t_last_activity = GNUNET_TIME_absolute_get();
884 atsi_delta_count = 0;
886 == disassemble_ats_information (existing_address, atsi, atsi_count,
887 &atsi_delta, &atsi_delta_count))
889 /* Notify performance clients about properties */
890 GAS_performance_notify_all_clients (&existing_address->peer,
891 existing_address->plugin, existing_address->addr,
892 existing_address->addr_len, existing_address->active,
893 existing_address->atsi, existing_address->atsi_count,
894 GNUNET_BANDWIDTH_value_init (existing_address->assigned_bw_out),
895 GNUNET_BANDWIDTH_value_init (existing_address->assigned_bw_in));
897 for (c1 = 0; c1 < atsi_delta_count; c1++)
899 if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
900 && (addr_net != ntohl (atsi_delta[c1].value)))
902 /* Network type changed */
903 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
904 "Address for peer `%s' %p changed from network %s to %s\n",
905 GNUNET_i2s (peer), existing_address,
906 GNUNET_ATS_print_network_type (addr_net),
907 GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
908 env.sf.s_address_update_network (solver, existing_address,
909 ntohl (atsi_delta[c1].value),
910 get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
911 addr_net = get_performance_info (existing_address,
912 GNUNET_ATS_NETWORK_TYPE);
915 /* Notify solver about update with atsi information and session */
916 env.sf.s_bulk_start (solver);
917 GAS_normalization_normalize_property (addresses, existing_address,
919 env.sf.s_bulk_stop (solver);
921 GNUNET_free_non_null(atsi_delta);
923 /* Notify solver about new session */
924 if (existing_address->session_id == session_id)
925 return; /* possible, can both be 0 since address is revalidated */
927 previous_session = existing_address->session_id;
928 existing_address->session_id = session_id;
929 env.sf.s_address_update_session (solver, existing_address,
930 previous_session, session_id);
932 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
933 "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
934 GNUNET_i2s (peer), existing_address, existing_address->addr_len,
935 session_id, GNUNET_ATS_print_network_type (addr_net));
940 * Update an address with new performance information for a peer.
943 * @param session_id session id, never 0
944 * @param atsi performance information for this address
945 * @param atsi_count number of performance information contained in @a atsi
948 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
950 const struct GNUNET_ATS_Information *atsi,
953 struct ATS_Address *aa;
954 struct GNUNET_ATS_Information *atsi_delta;
955 uint32_t atsi_delta_count;
956 uint32_t prev_session;
959 if (GNUNET_NO == running)
961 GNUNET_assert (NULL != addresses);
963 /* Get existing address */
964 aa = find_exact_address (peer,
971 if (NULL == aa->solver_information)
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "Received `%s' for peer `%s' address \n",
984 aa->t_last_activity = GNUNET_TIME_absolute_get();
985 if (session_id != aa->session_id)
987 /* Session changed */
988 prev_session = aa->session_id;
989 aa->session_id = session_id;
990 env.sf.s_address_update_session (solver,
997 atsi_delta_count = 0;
999 disassemble_ats_information (aa, atsi,
1004 /* ATS properties changed */
1005 for (c1 = 0; c1 < atsi_delta_count; c1++)
1007 if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
1009 /* Network type changed */
1010 env.sf.s_address_update_network (solver, aa,
1011 ntohl (atsi_delta[c1].value),
1012 get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
1016 /* Notify performance clients about updated address */
1017 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr,
1018 aa->addr_len, aa->active, aa->atsi, aa->atsi_count,
1019 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
1020 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
1022 env.sf.s_bulk_start (solver);
1023 GAS_normalization_normalize_property (addresses,
1027 env.sf.s_bulk_stop (solver);
1029 GNUNET_free_non_null (atsi_delta);
1034 * Remove an address or just a session for a peer.
1037 * @param session_id session id, can never be 0
1040 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
1041 uint32_t session_id)
1043 struct ATS_Address *ea;
1045 if (GNUNET_NO == running)
1048 /* Get existing address */
1049 ea = find_exact_address (peer,
1054 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1055 "Tried to destroy unknown address for peer `%s' session id %u\n",
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1062 "Received `%s' for peer `%s' address %p session %u\n",
1063 "ADDRESS DESTROYED",
1067 GNUNET_CONTAINER_multipeermap_remove (addresses,
1071 env.sf.s_del (solver, ea, GNUNET_NO);
1072 GAS_performance_notify_all_clients (peer,
1081 GNUNET_STATISTICS_set (stats,
1083 GNUNET_CONTAINER_multipeermap_size (addresses),
1089 * Notification about active use of an address.
1090 * in_use == #GNUNET_YES:
1091 * This address is used to maintain an active connection with a peer.
1092 * in_use == #GNUNET_NO:
1093 * This address is no longer used to maintain an active connection with a peer.
1095 * Note: can only be called with in_use == #GNUNET_NO if called with #GNUNET_YES
1099 * @param session_id session id, can be 0
1100 * @param in_use #GNUNET_YES if #GNUNET_NO FIXME
1101 * @return #GNUNET_SYSERR on failure (address unknown ...)
1104 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
1105 uint32_t session_id,
1108 struct ATS_Address *ea;
1110 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1111 "Received `%s' for peer `%s'\n",
1114 if (GNUNET_NO == running)
1115 return GNUNET_SYSERR;
1116 ea = find_exact_address (peer,
1120 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1121 "Trying to set unknown address `%s' `%u' to %s \n",
1124 (GNUNET_NO == in_use) ? "NO" : "YES");
1126 return GNUNET_SYSERR;
1128 if (ea->used == in_use)
1131 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1132 "Address in use called multiple times for peer `%s': %s -> %s \n",
1134 (GNUNET_NO == ea->used) ? "NO" : "YES",
1135 (GNUNET_NO == in_use) ? "NO" : "YES");
1136 return GNUNET_SYSERR;
1138 /* Tell solver about update */
1140 ea->t_last_activity = GNUNET_TIME_absolute_get();
1141 env.sf.s_address_update_inuse (solver,
1149 * Cancel address suggestions for a peer
1151 * @param peer the peer id
1154 GAS_addresses_request_address_cancel (const struct GNUNET_PeerIdentity *peer)
1156 struct GAS_Addresses_Suggestion_Requests *cur = pending_requests_head;
1158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1159 "Received request: `%s' for peer %s\n",
1160 "request_address_cancel",
1165 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1173 "No address requests pending for peer `%s', cannot remove!\n",
1177 env.sf.s_get_stop (solver, peer);
1178 GAS_addresses_handle_backoff_reset (peer);
1179 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1180 "Removed request pending for peer `%s\n",
1182 GNUNET_CONTAINER_DLL_remove (pending_requests_head,
1183 pending_requests_tail,
1190 * Request address suggestions for a peer
1192 * @param peer the peer id
1195 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
1197 struct GAS_Addresses_Suggestion_Requests *cur = pending_requests_head;
1198 struct ATS_Address *aa;
1200 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1201 "Received `%s' for peer `%s'\n",
1205 if (GNUNET_NO == running)
1209 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1210 break; /* already suggesting */
1215 cur = GNUNET_new (struct GAS_Addresses_Suggestion_Requests);
1217 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1218 "Adding new address suggestion request for `%s'\n",
1220 GNUNET_CONTAINER_DLL_insert (pending_requests_head,
1221 pending_requests_tail,
1225 /* Get prefered address from solver */
1226 aa = (struct ATS_Address *) env.sf.s_get (solver, peer);
1229 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1234 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1235 aa, GNUNET_i2s (peer));
1237 GAS_scheduling_transmit_address_suggestion (peer,
1239 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
1240 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
1242 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1243 ATS_BLOCKING_DELTA);
1244 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1245 aa->block_interval);
1247 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1248 "Address %p ready for suggestion, block interval now %llu \n", aa,
1249 aa->block_interval);
1253 * Iterator to reset address blocking
1255 * @param cls not used
1256 * @param key the peer
1257 * @param value the address to reset
1258 * @return #GNUNET_OK to continue
1261 reset_address_it (void *cls,
1262 const struct GNUNET_PeerIdentity *key,
1265 struct ATS_Address *aa = value;
1267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1268 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1269 GNUNET_i2s (&aa->peer),
1271 aa->block_interval);
1272 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1273 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1279 * Reset suggestion backoff for a peer
1281 * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1282 * reset using this function
1284 * @param peer the peer id
1287 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
1289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1290 "Received `%s' for peer `%s'\n",
1294 GNUNET_break(GNUNET_SYSERR !=
1295 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
1303 * Solver information callback
1305 * @param cls the closure
1306 * @param op the operation
1307 * @param status operation status
1308 * @param add additional information
1311 solver_info_cb (void *cls,
1312 enum GAS_Solver_Operation op,
1313 enum GAS_Solver_Status status,
1314 enum GAS_Solver_Additional_Information add)
1320 add_info = "GAS_INFO_NONE";
1323 add_info = "GAS_INFO_MLP_FULL";
1325 case GAS_INFO_UPDATED:
1326 add_info = "GAS_INFO_MLP_UPDATED";
1328 case GAS_INFO_PROP_ALL:
1329 add_info = "GAS_INFO_PROP_ALL";
1331 case GAS_INFO_PROP_SINGLE:
1332 add_info = "GAS_INFO_PROP_SINGLE";
1335 add_info = "INVALID";
1340 case GAS_OP_SOLVE_START:
1341 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1342 "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
1343 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL", add_info);
1345 case GAS_OP_SOLVE_STOP:
1346 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1347 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
1348 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL", add_info);
1351 case GAS_OP_SOLVE_SETUP_START:
1352 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1353 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
1354 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1357 case GAS_OP_SOLVE_SETUP_STOP:
1358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1359 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
1360 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1363 case GAS_OP_SOLVE_MLP_LP_START:
1364 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1365 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
1366 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1368 case GAS_OP_SOLVE_MLP_LP_STOP:
1369 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1370 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
1371 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1374 case GAS_OP_SOLVE_MLP_MLP_START:
1375 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1376 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
1377 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1379 case GAS_OP_SOLVE_MLP_MLP_STOP:
1380 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1381 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
1382 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1384 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
1385 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1386 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
1387 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1389 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
1390 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1391 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
1392 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1401 * The preference changed for a peer
1404 * @param peer the peer
1405 * @param kind the ATS kind
1406 * @param pref_rel the new relative preference value
1409 normalized_preference_changed_cb (void *cls,
1410 const struct GNUNET_PeerIdentity *peer,
1411 enum GNUNET_ATS_PreferenceKind kind,
1414 /* Tell solver about update */
1415 env.sf.s_pref (solver, peer, kind, pref_rel);
1420 * The relative value for a property changed
1423 * @param address the peer
1424 * @param type the ATS type
1425 * @param prop_rel the new relative preference value
1428 normalized_property_changed_cb (void *cls,
1429 struct ATS_Address *address,
1433 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1434 "Normalized property %s for peer `%s' changed to %.3f \n",
1435 GNUNET_ATS_print_property_type (type),
1436 GNUNET_i2s (&address->peer),
1438 env.sf.s_address_update_property (solver,
1446 static struct GAS_Addresses_Preference_Clients *
1447 find_preference_client (void *client)
1449 struct GAS_Addresses_Preference_Clients *cur;
1451 for (cur = preference_clients_head; NULL != cur; cur = cur->next)
1452 if (cur->client == client)
1459 * A performance client disconnected
1461 * @param client the client
1464 GAS_addresses_preference_client_disconnect (void *client)
1466 struct GAS_Addresses_Preference_Clients *pc;
1468 if (NULL != (pc = find_preference_client (client)))
1470 GNUNET_CONTAINER_DLL_remove (preference_clients_head,
1471 preference_clients_tail,
1474 GNUNET_assert (pref_clients > 0);
1476 GNUNET_STATISTICS_set (stats,
1477 "# active performance clients",
1481 GAS_normalization_preference_client_disconnect (client);
1486 * Change the preference for a peer
1488 * @param client the client sending this request
1489 * @param peer the peer id
1490 * @param kind the preference kind to change
1491 * @param score_abs the new preference score
1494 GAS_addresses_preference_change (void *client,
1495 const struct GNUNET_PeerIdentity *peer,
1496 enum GNUNET_ATS_PreferenceKind kind,
1499 struct GAS_Addresses_Preference_Clients *pc;
1501 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1502 "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1503 GNUNET_i2s (peer), client);
1505 if (GNUNET_NO == running)
1509 GNUNET_CONTAINER_multipeermap_contains (addresses,
1512 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1513 "Received `%s' for unknown peer `%s' from client %p\n",
1514 "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1518 if (NULL == find_preference_client (client))
1520 pc = GNUNET_new (struct GAS_Addresses_Preference_Clients);
1521 pc->client = client;
1522 GNUNET_CONTAINER_DLL_insert (preference_clients_head,
1523 preference_clients_tail,
1526 GNUNET_STATISTICS_set (stats,
1527 "# active performance clients",
1532 env.sf.s_bulk_start (solver);
1533 /* Tell normalization about change, normalization will call callback if preference changed */
1534 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1535 env.sf.s_bulk_stop (solver);
1540 * Change the preference for a peer
1542 * @param application the client sending this request
1543 * @param peer the peer id
1544 * @param scope the time interval for this feedback: [now - scope .. now]
1545 * @param kind the preference kind to change
1546 * @param score_abs the new preference score
1549 GAS_addresses_preference_feedback (void *application,
1550 const struct GNUNET_PeerIdentity *peer,
1551 const struct GNUNET_TIME_Relative scope,
1552 enum GNUNET_ATS_PreferenceKind kind,
1555 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1556 "Received `%s' for peer `%s' for client %p\n",
1557 "PREFERENCE FEEDBACK",
1561 if (GNUNET_NO == running)
1565 GNUNET_CONTAINER_multipeermap_contains (addresses,
1568 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1569 "Received `%s' for unknown peer `%s' from client %p\n",
1570 "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1574 env.sf.s_feedback (solver, application, peer, scope, kind,
1580 * Load quotas for networks from configuration
1582 * @param cfg configuration handle
1583 * @param out_dest where to write outbound quotas
1584 * @param in_dest where to write inbound quotas
1585 * @param dest_length length of inbound and outbound arrays
1586 * @return number of networks loaded
1589 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1590 unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1592 char * entry_in = NULL;
1593 char * entry_out = NULL;
1594 char * quota_out_str;
1595 char * quota_in_str;
1599 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1603 GNUNET_asprintf (&entry_out,
1605 GNUNET_ATS_print_network_type (c));
1606 GNUNET_asprintf (&entry_in,
1608 GNUNET_ATS_print_network_type (c));
1612 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
1616 if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
1618 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1621 if ((GNUNET_NO == res)
1623 == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
1626 if ((GNUNET_NO == res)
1628 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
1632 if (GNUNET_NO == res)
1634 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1635 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1636 GNUNET_ATS_print_network_type (c),
1638 GNUNET_ATS_DefaultBandwidth);
1639 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1643 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1644 _("Outbound quota configure for network `%s' is %llu\n"),
1645 GNUNET_ATS_print_network_type (c),
1648 GNUNET_free(quota_out_str);
1652 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1653 _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1654 GNUNET_ATS_print_network_type (c),
1655 GNUNET_ATS_DefaultBandwidth);
1656 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1661 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
1665 if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
1667 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1670 if ((GNUNET_NO == res)
1672 == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1674 if ((GNUNET_NO == res)
1676 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
1680 if (GNUNET_NO == res)
1682 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1683 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1684 GNUNET_ATS_print_network_type (c),
1686 GNUNET_ATS_DefaultBandwidth);
1687 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1691 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1692 _("Inbound quota configured for network `%s' is %llu\n"),
1693 GNUNET_ATS_print_network_type (c),
1696 GNUNET_free(quota_in_str);
1700 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1701 _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1702 GNUNET_ATS_print_network_type (c),
1703 GNUNET_ATS_DefaultBandwidth);
1704 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1706 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1707 "Loaded quota for network `%s' (in/out): %llu %llu\n",
1708 GNUNET_ATS_print_network_type (c),
1711 GNUNET_free(entry_out);
1712 GNUNET_free(entry_in);
1714 return GNUNET_ATS_NetworkTypeCount;
1719 * Callback for solver to notify about assignment changes
1722 * @param address the address with changes
1725 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1727 struct GAS_Addresses_Suggestion_Requests *cur;
1731 GNUNET_assert(address != NULL);
1732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1733 "Bandwidth assignment changed for peer %s \n",
1734 GNUNET_i2s (&address->peer));
1736 /* Notify performance clients about changes to address */
1737 GAS_performance_notify_all_clients (&address->peer, address->plugin,
1738 address->addr, address->addr_len, address->active, address->atsi,
1739 address->atsi_count,
1740 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
1741 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
1743 for (cur = pending_requests_head;NULL != cur; cur = cur->next)
1744 if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
1745 break; /* we have an address request pending*/
1748 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1749 "Nobody is interested in peer `%s' :(\n",
1750 GNUNET_i2s (&address->peer));
1754 if ((0 == address->assigned_bw_in) && (0 == address->assigned_bw_out))
1756 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1757 "Telling transport to disconnect peer `%s'\n",
1758 GNUNET_i2s (&address->peer));
1760 /* Notify scheduling clients about suggestion */
1761 GAS_scheduling_transmit_address_suggestion (&address->peer,
1762 address->session_id,
1768 /* Do bandwidth stability check */
1769 diff_out = abs (address->assigned_bw_out - address->last_notified_bw_out);
1770 diff_in = abs (address->assigned_bw_in - address->last_notified_bw_in);
1772 if ( (diff_out < htonl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) &&
1773 (diff_in < htonl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) )
1776 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1777 "Sending bandwidth update for peer `%s': %u %u\n",
1778 GNUNET_i2s (&address->peer), address->assigned_bw_out,
1779 address->assigned_bw_out);
1781 /* *Notify scheduling clients about suggestion */
1782 GAS_scheduling_transmit_address_suggestion (&address->peer,
1783 address->session_id,
1784 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
1785 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
1787 address->last_notified_bw_out = address->assigned_bw_out;
1788 address->last_notified_bw_in = address->assigned_bw_in;
1793 * Initialize address subsystem. The addresses subsystem manages the addresses
1794 * known and current performance information. It has a solver component
1795 * responsible for the resource allocation. It tells the solver about changes
1796 * and receives updates when the solver changes the resource allocation.
1798 * @param cfg configuration to use
1799 * @param stats_ the statistics handle to use
1800 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (failed to load
1804 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1805 struct GNUNET_STATISTICS_Handle *stats_)
1807 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1808 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1813 running = GNUNET_NO;
1815 /* Initialize the addresses database */
1816 addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1819 /* Figure out configured solution method */
1820 if (GNUNET_SYSERR ==
1821 GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1823 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1824 "No resource assignment method configured, using proportional approach\n");
1825 ats_mode = MODE_PROPORTIONAL;
1829 for (c = 0; c < strlen (mode_str); c++)
1830 mode_str[c] = toupper (mode_str[c]);
1831 if (0 == strcmp (mode_str, "PROPORTIONAL"))
1832 ats_mode = MODE_PROPORTIONAL;
1833 else if (0 == strcmp (mode_str, "MLP"))
1835 ats_mode = MODE_MLP;
1837 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1838 "Assignment method `%s' configured, but GLPK is not available, please install \n",
1840 ats_mode = MODE_PROPORTIONAL;
1843 else if (0 == strcmp (mode_str, "RIL"))
1844 ats_mode = MODE_RIL;
1847 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1848 "Invalid resource assignment method `%s' configured, using proportional approach\n",
1850 ats_mode = MODE_PROPORTIONAL;
1852 GNUNET_free(mode_str);
1855 load_quotas (cfg, quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount);
1856 env.info_cb = &solver_info_cb;
1857 env.info_cb_cls = NULL;
1858 env.bandwidth_changed_cb = &bandwidth_changed_cb;
1859 env.bw_changed_cb_cls = NULL;
1860 env.get_preferences = &get_preferences_cb;
1861 env.get_preference_cls = NULL;
1862 env.get_property = &get_property_cb;
1863 env.get_property_cls = NULL;
1866 env.addresses = addresses;
1868 env.network_count = GNUNET_ATS_NetworkTypeCount;
1869 int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1870 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1872 env.networks[c] = networks[c];
1873 env.out_quota[c] = quotas_out[c];
1874 env.in_quota[c] = quotas_in[c];
1878 case MODE_PROPORTIONAL:
1879 plugin_short = "proportional";
1882 plugin_short = "mlp";
1885 plugin_short = "ril";
1888 plugin_short = NULL;
1891 GNUNET_asprintf (&plugin,
1892 "libgnunet_plugin_ats_%s",
1894 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1895 _("Initializing solver `%s '`%s'\n"),
1898 if (NULL == (solver = GNUNET_PLUGIN_load (plugin, &env)))
1900 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1901 _("Failed to initialize solver `%s'!\n"),
1903 return GNUNET_SYSERR;
1906 GNUNET_assert (NULL != env.sf.s_add);
1907 GNUNET_assert (NULL != env.sf.s_address_update_inuse);
1908 GNUNET_assert (NULL != env.sf.s_address_update_property);
1909 GNUNET_assert (NULL != env.sf.s_address_update_session);
1910 GNUNET_assert (NULL != env.sf.s_address_update_network);
1911 GNUNET_assert (NULL != env.sf.s_get);
1912 GNUNET_assert (NULL != env.sf.s_get_stop);
1913 GNUNET_assert (NULL != env.sf.s_pref);
1914 GNUNET_assert (NULL != env.sf.s_feedback);
1915 GNUNET_assert (NULL != env.sf.s_del);
1916 GNUNET_assert (NULL != env.sf.s_bulk_start);
1917 GNUNET_assert (NULL != env.sf.s_bulk_stop);
1920 GAS_normalization_start (&normalized_preference_changed_cb, NULL,
1921 &normalized_property_changed_cb, NULL);
1925 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1926 _("Failed to initialize solver!\n"));
1927 return GNUNET_SYSERR;
1929 /* up and running */
1930 running = GNUNET_YES;
1932 GNUNET_STATISTICS_set (stats,
1934 GNUNET_CONTAINER_multipeermap_size (addresses),
1942 * Destroy all addresses iterator
1945 * @param key peer identity (unused)
1946 * @param value the 'struct ATS_Address' to free
1947 * @return #GNUNET_OK (continue to iterate)
1950 destroy_all_address_it (void *cls,
1951 const struct GNUNET_PeerIdentity *key,
1954 struct ATS_Address *aa = value;
1957 GNUNET_assert(GNUNET_YES ==
1958 GNUNET_CONTAINER_multipeermap_remove (addresses,
1962 env.sf.s_del (solver, aa, GNUNET_NO);
1964 GAS_performance_notify_all_clients (&aa->peer,
1978 * Remove all addresses
1981 GAS_addresses_destroy_all ()
1983 if (GNUNET_NO == running)
1986 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1987 "Destroying all addresses\n");
1988 env.sf.s_bulk_start (solver);
1989 if (NULL != addresses)
1990 GNUNET_CONTAINER_multipeermap_iterate (addresses,
1991 &destroy_all_address_it,
1993 env.sf.s_bulk_start (solver);
1998 * Shutdown address subsystem.
2001 GAS_addresses_done ()
2003 struct GAS_Addresses_Suggestion_Requests *cur;
2004 struct GAS_Addresses_Preference_Clients *pcur;
2006 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2007 "Shutting down addresses\n");
2008 GAS_addresses_destroy_all ();
2009 running = GNUNET_NO;
2010 GNUNET_CONTAINER_multipeermap_destroy (addresses);
2012 while (NULL != (cur = pending_requests_head))
2014 GNUNET_CONTAINER_DLL_remove (pending_requests_head,
2015 pending_requests_tail,
2020 while (NULL != (pcur = preference_clients_head))
2022 GNUNET_CONTAINER_DLL_remove (preference_clients_head,
2023 preference_clients_tail,
2025 GNUNET_assert (pref_clients > 0);
2027 GNUNET_STATISTICS_set (stats,
2028 "# active performance clients",
2033 GNUNET_PLUGIN_unload (plugin,
2035 GNUNET_free (plugin);
2036 /* Stop configured solution method */
2037 GAS_normalization_stop ();
2042 * Closure for #peerinfo_it().
2044 struct PeerInfoIteratorContext
2047 * Function to call for each address.
2049 GNUNET_ATS_PeerInfo_Iterator it;
2052 * Closure for @e it.
2059 * Iterator to iterate over a peer's addresses
2061 * @param cls a `struct PeerInfoIteratorContext`
2062 * @param key the peer id
2063 * @param value the `struct ATS_address`
2064 * @return #GNUNET_OK to continue
2067 peerinfo_it (void *cls,
2068 const struct GNUNET_PeerIdentity *key,
2071 struct PeerInfoIteratorContext *pi_ctx = cls;
2072 struct ATS_Address *addr = value;
2074 pi_ctx->it (pi_ctx->it_cls,
2080 addr->atsi, addr->atsi_count,
2081 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_out),
2082 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_in));
2088 * Return information all peers currently known to ATS
2090 * @param peer the respective peer, NULL for 'all' peers
2091 * @param pi_it the iterator to call for every peer
2092 * @param pi_it_cls the closure for @a pi_it
2095 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer,
2096 GNUNET_ATS_PeerInfo_Iterator pi_it,
2099 struct PeerInfoIteratorContext pi_ctx;
2103 /* does not make sense without callback */
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2108 "Returning information for %s from a total of %u known addresses\n",
2111 : GNUNET_i2s (peer),
2112 (unsigned int) GNUNET_CONTAINER_multipeermap_size (addresses));
2114 pi_ctx.it_cls = pi_it_cls;
2116 GNUNET_CONTAINER_multipeermap_iterate (addresses,
2120 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
2122 &peerinfo_it, &pi_ctx);
2124 NULL, NULL, NULL, 0,
2131 /* end of gnunet-service-ats_addresses.c */