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",
793 if (GNUNET_NO == running)
796 GNUNET_assert(NULL != addresses);
798 new_address = create_address (peer, plugin_name,
799 plugin_addr, plugin_addr_len,
800 local_address_info, session_id);
802 disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
804 GNUNET_free_non_null (atsi_delta);
805 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
806 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
807 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
809 /* Get existing address or address with session == 0 */
810 existing_address = find_equivalent_address (peer, new_address);
811 if (NULL == existing_address)
813 /* Add a new address */
814 new_address->t_added = GNUNET_TIME_absolute_get();
815 new_address->t_last_activity = GNUNET_TIME_absolute_get();
816 GNUNET_assert(GNUNET_OK ==
817 GNUNET_CONTAINER_multipeermap_put (addresses,
820 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
822 GNUNET_STATISTICS_set (stats,
824 GNUNET_CONTAINER_multipeermap_size (addresses),
827 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
828 "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
833 GNUNET_ATS_print_network_type (addr_net));
835 /* Tell solver about new address */
836 env.sf.s_add (solver, new_address, addr_net);
838 env.sf.s_bulk_start (solver);
839 GAS_normalization_normalize_property (addresses,
843 env.sf.s_bulk_stop (solver);
845 /* Notify performance clients about new address */
846 GAS_performance_notify_all_clients (&new_address->peer, new_address->plugin,
847 new_address->addr, new_address->addr_len, new_address->active,
848 new_address->atsi, new_address->atsi_count,
849 GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_out),
850 GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_in));
854 /* FIXME: this case should probably not be allowed... */
855 /* We have an existing address we can use, clean up new */
856 GNUNET_free(new_address->plugin);
857 GNUNET_free_non_null(new_address->atsi);
858 GNUNET_free(new_address);
861 if (0 != existing_address->session_id)
863 /* Should not happen */
868 addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
869 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
870 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
872 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
873 "Found existing address for peer `%s' %p with new session %u in network %s\n",
874 GNUNET_i2s (peer), existing_address, session_id,
875 GNUNET_ATS_print_network_type (addr_net));
876 /* We have an address without an session, update this address */
877 existing_address->t_added = GNUNET_TIME_absolute_get();
878 existing_address->t_last_activity = GNUNET_TIME_absolute_get();
880 atsi_delta_count = 0;
882 == disassemble_ats_information (existing_address, atsi, atsi_count,
883 &atsi_delta, &atsi_delta_count))
885 /* Notify performance clients about properties */
886 GAS_performance_notify_all_clients (&existing_address->peer,
887 existing_address->plugin, existing_address->addr,
888 existing_address->addr_len, existing_address->active,
889 existing_address->atsi, existing_address->atsi_count,
890 GNUNET_BANDWIDTH_value_init (existing_address->assigned_bw_out),
891 GNUNET_BANDWIDTH_value_init (existing_address->assigned_bw_in));
893 for (c1 = 0; c1 < atsi_delta_count; c1++)
895 if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
896 && (addr_net != ntohl (atsi_delta[c1].value)))
898 /* Network type changed */
899 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
900 "Address for peer `%s' %p changed from network %s to %s\n",
901 GNUNET_i2s (peer), existing_address,
902 GNUNET_ATS_print_network_type (addr_net),
903 GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
904 env.sf.s_address_update_network (solver, existing_address,
905 ntohl (atsi_delta[c1].value),
906 get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
907 addr_net = get_performance_info (existing_address,
908 GNUNET_ATS_NETWORK_TYPE);
911 /* Notify solver about update with atsi information and session */
912 env.sf.s_bulk_start (solver);
913 GAS_normalization_normalize_property (addresses, existing_address,
915 env.sf.s_bulk_stop (solver);
917 GNUNET_free_non_null(atsi_delta);
919 /* Notify solver about new session */
920 if (existing_address->session_id == session_id)
921 return; /* possible, can both be 0 since address is revalidated */
923 previous_session = existing_address->session_id;
924 existing_address->session_id = session_id;
925 env.sf.s_address_update_session (solver, existing_address,
926 previous_session, session_id);
928 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
929 "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
930 GNUNET_i2s (peer), existing_address, existing_address->addr_len,
931 session_id, GNUNET_ATS_print_network_type (addr_net));
936 * Update an address with new performance information for a peer.
939 * @param session_id session id, never 0
940 * @param atsi performance information for this address
941 * @param atsi_count number of performance information contained in @a atsi
944 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
946 const struct GNUNET_ATS_Information *atsi,
949 struct ATS_Address *aa;
950 struct GNUNET_ATS_Information *atsi_delta;
951 uint32_t atsi_delta_count;
952 uint32_t prev_session;
955 if (GNUNET_NO == running)
957 GNUNET_assert (NULL != addresses);
959 /* Get existing address */
960 aa = find_exact_address (peer,
967 if (NULL == aa->solver_information)
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 "Received `%s' for peer `%s' address \n",
980 aa->t_last_activity = GNUNET_TIME_absolute_get();
981 if (session_id != aa->session_id)
983 /* Session changed */
984 prev_session = aa->session_id;
985 aa->session_id = session_id;
986 env.sf.s_address_update_session (solver,
993 atsi_delta_count = 0;
995 disassemble_ats_information (aa, atsi,
1000 /* ATS properties changed */
1001 for (c1 = 0; c1 < atsi_delta_count; c1++)
1003 if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
1005 /* Network type changed */
1006 env.sf.s_address_update_network (solver, aa,
1007 ntohl (atsi_delta[c1].value),
1008 get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
1012 /* Notify performance clients about updated address */
1013 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr,
1014 aa->addr_len, aa->active, aa->atsi, aa->atsi_count,
1015 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
1016 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
1018 env.sf.s_bulk_start (solver);
1019 GAS_normalization_normalize_property (addresses,
1023 env.sf.s_bulk_stop (solver);
1025 GNUNET_free_non_null (atsi_delta);
1030 * Remove an address or just a session for a peer.
1033 * @param session_id session id, can never be 0
1036 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
1037 uint32_t session_id)
1039 struct ATS_Address *ea;
1041 if (GNUNET_NO == running)
1044 /* Get existing address */
1045 ea = find_exact_address (peer,
1050 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1051 "Tried to destroy unknown address for peer `%s' session id %u\n",
1057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058 "Received `%s' for peer `%s' address %p session %u\n",
1059 "ADDRESS DESTROYED",
1063 GNUNET_CONTAINER_multipeermap_remove (addresses,
1067 env.sf.s_del (solver, ea, GNUNET_NO);
1068 GAS_performance_notify_all_clients (peer,
1077 GNUNET_STATISTICS_set (stats,
1079 GNUNET_CONTAINER_multipeermap_size (addresses),
1085 * Notification about active use of an address.
1086 * in_use == #GNUNET_YES:
1087 * This address is used to maintain an active connection with a peer.
1088 * in_use == #GNUNET_NO:
1089 * This address is no longer used to maintain an active connection with a peer.
1091 * Note: can only be called with in_use == #GNUNET_NO if called with #GNUNET_YES
1095 * @param session_id session id, can be 0
1096 * @param in_use #GNUNET_YES if #GNUNET_NO FIXME
1097 * @return #GNUNET_SYSERR on failure (address unknown ...)
1100 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
1101 uint32_t session_id,
1104 struct ATS_Address *ea;
1106 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1107 "Received `%s' for peer `%s'\n",
1110 if (GNUNET_NO == running)
1111 return GNUNET_SYSERR;
1112 ea = find_exact_address (peer,
1116 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1117 "Trying to set unknown address `%s' `%u' to %s \n",
1120 (GNUNET_NO == in_use) ? "NO" : "YES");
1122 return GNUNET_SYSERR;
1124 if (ea->used == in_use)
1127 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1128 "Address in use called multiple times for peer `%s': %s -> %s \n",
1130 (GNUNET_NO == ea->used) ? "NO" : "YES",
1131 (GNUNET_NO == in_use) ? "NO" : "YES");
1132 return GNUNET_SYSERR;
1134 /* Tell solver about update */
1136 ea->t_last_activity = GNUNET_TIME_absolute_get();
1137 env.sf.s_address_update_inuse (solver,
1145 * Cancel address suggestions for a peer
1147 * @param peer the peer id
1150 GAS_addresses_request_address_cancel (const struct GNUNET_PeerIdentity *peer)
1152 struct GAS_Addresses_Suggestion_Requests *cur = pending_requests_head;
1154 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1155 "Received request: `%s' for peer %s\n",
1156 "request_address_cancel",
1161 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1168 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1169 "No address requests pending for peer `%s', cannot remove!\n",
1173 env.sf.s_get_stop (solver, peer);
1174 GAS_addresses_handle_backoff_reset (peer);
1175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1176 "Removed request pending for peer `%s\n",
1178 GNUNET_CONTAINER_DLL_remove (pending_requests_head,
1179 pending_requests_tail,
1186 * Request address suggestions for a peer
1188 * @param peer the peer id
1191 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
1193 struct GAS_Addresses_Suggestion_Requests *cur = pending_requests_head;
1194 struct ATS_Address *aa;
1196 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1197 "Received `%s' for peer `%s'\n",
1201 if (GNUNET_NO == running)
1205 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1206 break; /* already suggesting */
1211 cur = GNUNET_new (struct GAS_Addresses_Suggestion_Requests);
1213 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1214 "Adding new address suggestion request for `%s'\n",
1216 GNUNET_CONTAINER_DLL_insert (pending_requests_head,
1217 pending_requests_tail,
1221 /* Get prefered address from solver */
1222 aa = (struct ATS_Address *) env.sf.s_get (solver, peer);
1225 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1230 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1231 aa, GNUNET_i2s (peer));
1233 GAS_scheduling_transmit_address_suggestion (peer,
1235 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
1236 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
1238 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1239 ATS_BLOCKING_DELTA);
1240 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1241 aa->block_interval);
1243 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1244 "Address %p ready for suggestion, block interval now %llu \n", aa,
1245 aa->block_interval);
1249 * Iterator to reset address blocking
1251 * @param cls not used
1252 * @param key the peer
1253 * @param value the address to reset
1254 * @return #GNUNET_OK to continue
1257 reset_address_it (void *cls,
1258 const struct GNUNET_PeerIdentity *key,
1261 struct ATS_Address *aa = value;
1263 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1264 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1265 GNUNET_i2s (&aa->peer),
1267 aa->block_interval);
1268 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1269 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1275 * Reset suggestion backoff for a peer
1277 * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1278 * reset using this function
1280 * @param peer the peer id
1283 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
1285 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1286 "Received `%s' for peer `%s'\n",
1290 GNUNET_break(GNUNET_SYSERR !=
1291 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
1299 * Solver information callback
1301 * @param cls the closure
1302 * @param op the operation
1303 * @param status operation status
1304 * @param add additional information
1307 solver_info_cb (void *cls,
1308 enum GAS_Solver_Operation op,
1309 enum GAS_Solver_Status status,
1310 enum GAS_Solver_Additional_Information add)
1316 add_info = "GAS_INFO_NONE";
1319 add_info = "GAS_INFO_MLP_FULL";
1321 case GAS_INFO_UPDATED:
1322 add_info = "GAS_INFO_MLP_UPDATED";
1324 case GAS_INFO_PROP_ALL:
1325 add_info = "GAS_INFO_PROP_ALL";
1327 case GAS_INFO_PROP_SINGLE:
1328 add_info = "GAS_INFO_PROP_SINGLE";
1331 add_info = "INVALID";
1336 case GAS_OP_SOLVE_START:
1337 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1338 "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
1339 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL", add_info);
1341 case GAS_OP_SOLVE_STOP:
1342 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1343 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
1344 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL", add_info);
1347 case GAS_OP_SOLVE_SETUP_START:
1348 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1349 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
1350 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1353 case GAS_OP_SOLVE_SETUP_STOP:
1354 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1355 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
1356 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1359 case GAS_OP_SOLVE_MLP_LP_START:
1360 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1361 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
1362 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1364 case GAS_OP_SOLVE_MLP_LP_STOP:
1365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1366 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
1367 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1370 case GAS_OP_SOLVE_MLP_MLP_START:
1371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1372 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
1373 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1375 case GAS_OP_SOLVE_MLP_MLP_STOP:
1376 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1377 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
1378 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1380 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
1381 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1382 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
1383 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1385 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
1386 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1387 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
1388 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
1397 * The preference changed for a peer
1400 * @param peer the peer
1401 * @param kind the ATS kind
1402 * @param pref_rel the new relative preference value
1405 normalized_preference_changed_cb (void *cls,
1406 const struct GNUNET_PeerIdentity *peer,
1407 enum GNUNET_ATS_PreferenceKind kind,
1410 /* Tell solver about update */
1411 env.sf.s_pref (solver, peer, kind, pref_rel);
1416 * The relative value for a property changed
1419 * @param address the peer
1420 * @param type the ATS type
1421 * @param prop_rel the new relative preference value
1424 normalized_property_changed_cb (void *cls,
1425 struct ATS_Address *address,
1429 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1430 "Normalized property %s for peer `%s' changed to %.3f \n",
1431 GNUNET_ATS_print_property_type (type),
1432 GNUNET_i2s (&address->peer),
1434 env.sf.s_address_update_property (solver,
1442 static struct GAS_Addresses_Preference_Clients *
1443 find_preference_client (void *client)
1445 struct GAS_Addresses_Preference_Clients *cur;
1447 for (cur = preference_clients_head; NULL != cur; cur = cur->next)
1448 if (cur->client == client)
1455 * A performance client disconnected
1457 * @param client the client
1460 GAS_addresses_preference_client_disconnect (void *client)
1462 struct GAS_Addresses_Preference_Clients *pc;
1464 if (NULL != (pc = find_preference_client (client)))
1466 GNUNET_CONTAINER_DLL_remove (preference_clients_head,
1467 preference_clients_tail,
1470 GNUNET_assert (pref_clients > 0);
1472 GNUNET_STATISTICS_set (stats,
1473 "# active performance clients",
1477 GAS_normalization_preference_client_disconnect (client);
1482 * Change the preference for a peer
1484 * @param client the client sending this request
1485 * @param peer the peer id
1486 * @param kind the preference kind to change
1487 * @param score_abs the new preference score
1490 GAS_addresses_preference_change (void *client,
1491 const struct GNUNET_PeerIdentity *peer,
1492 enum GNUNET_ATS_PreferenceKind kind,
1495 struct GAS_Addresses_Preference_Clients *pc;
1497 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1498 "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1499 GNUNET_i2s (peer), client);
1501 if (GNUNET_NO == running)
1505 GNUNET_CONTAINER_multipeermap_contains (addresses,
1508 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1509 "Received `%s' for unknown peer `%s' from client %p\n",
1510 "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1514 if (NULL == find_preference_client (client))
1516 pc = GNUNET_new (struct GAS_Addresses_Preference_Clients);
1517 pc->client = client;
1518 GNUNET_CONTAINER_DLL_insert (preference_clients_head,
1519 preference_clients_tail,
1522 GNUNET_STATISTICS_set (stats,
1523 "# active performance clients",
1528 env.sf.s_bulk_start (solver);
1529 /* Tell normalization about change, normalization will call callback if preference changed */
1530 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1531 env.sf.s_bulk_stop (solver);
1536 * Change the preference for a peer
1538 * @param application the client sending this request
1539 * @param peer the peer id
1540 * @param scope the time interval for this feedback: [now - scope .. now]
1541 * @param kind the preference kind to change
1542 * @param score_abs the new preference score
1545 GAS_addresses_preference_feedback (void *application,
1546 const struct GNUNET_PeerIdentity *peer,
1547 const struct GNUNET_TIME_Relative scope,
1548 enum GNUNET_ATS_PreferenceKind kind,
1551 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1552 "Received `%s' for peer `%s' for client %p\n",
1553 "PREFERENCE FEEDBACK",
1557 if (GNUNET_NO == running)
1561 GNUNET_CONTAINER_multipeermap_contains (addresses,
1564 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1565 "Received `%s' for unknown peer `%s' from client %p\n",
1566 "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1570 env.sf.s_feedback (solver, application, peer, scope, kind,
1576 * Load quotas for networks from configuration
1578 * @param cfg configuration handle
1579 * @param out_dest where to write outbound quotas
1580 * @param in_dest where to write inbound quotas
1581 * @param dest_length length of inbound and outbound arrays
1582 * @return number of networks loaded
1585 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1586 unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1588 char * entry_in = NULL;
1589 char * entry_out = NULL;
1590 char * quota_out_str;
1591 char * quota_in_str;
1595 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1599 GNUNET_asprintf (&entry_out,
1601 GNUNET_ATS_print_network_type (c));
1602 GNUNET_asprintf (&entry_in,
1604 GNUNET_ATS_print_network_type (c));
1608 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
1612 if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
1614 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1617 if ((GNUNET_NO == res)
1619 == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
1622 if ((GNUNET_NO == res)
1624 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
1628 if (GNUNET_NO == res)
1630 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1631 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1632 GNUNET_ATS_print_network_type (c),
1634 GNUNET_ATS_DefaultBandwidth);
1635 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1639 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1640 _("Outbound quota configure for network `%s' is %llu\n"),
1641 GNUNET_ATS_print_network_type (c),
1644 GNUNET_free(quota_out_str);
1648 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1649 _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1650 GNUNET_ATS_print_network_type (c),
1651 GNUNET_ATS_DefaultBandwidth);
1652 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1657 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
1661 if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
1663 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1666 if ((GNUNET_NO == res)
1668 == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1670 if ((GNUNET_NO == res)
1672 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
1676 if (GNUNET_NO == res)
1678 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1679 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1680 GNUNET_ATS_print_network_type (c),
1682 GNUNET_ATS_DefaultBandwidth);
1683 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1687 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1688 _("Inbound quota configured for network `%s' is %llu\n"),
1689 GNUNET_ATS_print_network_type (c),
1692 GNUNET_free(quota_in_str);
1696 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1697 _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1698 GNUNET_ATS_print_network_type (c),
1699 GNUNET_ATS_DefaultBandwidth);
1700 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1702 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1703 "Loaded quota for network `%s' (in/out): %llu %llu\n",
1704 GNUNET_ATS_print_network_type (c),
1707 GNUNET_free(entry_out);
1708 GNUNET_free(entry_in);
1710 return GNUNET_ATS_NetworkTypeCount;
1715 * Callback for solver to notify about assignment changes
1718 * @param address the address with changes
1721 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1723 struct GAS_Addresses_Suggestion_Requests *cur;
1727 GNUNET_assert(address != NULL);
1728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1729 "Bandwidth assignment changed for peer %s \n",
1730 GNUNET_i2s (&address->peer));
1732 /* Notify performance clients about changes to address */
1733 GAS_performance_notify_all_clients (&address->peer, address->plugin,
1734 address->addr, address->addr_len, address->active, address->atsi,
1735 address->atsi_count,
1736 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
1737 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
1739 for (cur = pending_requests_head;NULL != cur; cur = cur->next)
1740 if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
1741 break; /* we have an address request pending*/
1744 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1745 "Nobody is interested in peer `%s' :(\n",
1746 GNUNET_i2s (&address->peer));
1750 if ((0 == address->assigned_bw_in) && (0 == address->assigned_bw_out))
1752 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1753 "Telling transport to disconnect peer `%s'\n",
1754 GNUNET_i2s (&address->peer));
1756 /* Notify scheduling clients about suggestion */
1757 GAS_scheduling_transmit_address_suggestion (&address->peer,
1758 address->session_id,
1759 GNUNET_BANDWIDTH_value_init (0),
1760 GNUNET_BANDWIDTH_value_init (0));
1764 /* Do bandwidth stability check */
1765 diff_out = abs (address->assigned_bw_out - address->last_notified_bw_out);
1766 diff_in = abs (address->assigned_bw_in - address->last_notified_bw_in);
1768 if ( (diff_out < htonl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) &&
1769 (diff_in < htonl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) )
1772 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1773 "Sending bandwidth update for peer `%s': %u %u\n",
1774 GNUNET_i2s (&address->peer), address->assigned_bw_out,
1775 address->assigned_bw_out);
1777 /* *Notify scheduling clients about suggestion */
1778 GAS_scheduling_transmit_address_suggestion (&address->peer,
1779 address->session_id,
1780 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
1781 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
1783 address->last_notified_bw_out = address->assigned_bw_out;
1784 address->last_notified_bw_in = address->assigned_bw_in;
1789 * Initialize address subsystem. The addresses subsystem manages the addresses
1790 * known and current performance information. It has a solver component
1791 * responsible for the resource allocation. It tells the solver about changes
1792 * and receives updates when the solver changes the resource allocation.
1794 * @param cfg configuration to use
1795 * @param stats_ the statistics handle to use
1796 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (failed to load
1800 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1801 struct GNUNET_STATISTICS_Handle *stats_)
1803 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1804 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1809 running = GNUNET_NO;
1811 /* Initialize the addresses database */
1812 addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1815 /* Figure out configured solution method */
1816 if (GNUNET_SYSERR ==
1817 GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1819 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1820 "No resource assignment method configured, using proportional approach\n");
1821 ats_mode = MODE_PROPORTIONAL;
1825 for (c = 0; c < strlen (mode_str); c++)
1826 mode_str[c] = toupper (mode_str[c]);
1827 if (0 == strcmp (mode_str, "PROPORTIONAL"))
1828 ats_mode = MODE_PROPORTIONAL;
1829 else if (0 == strcmp (mode_str, "MLP"))
1831 ats_mode = MODE_MLP;
1833 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1834 "Assignment method `%s' configured, but GLPK is not available, please install \n",
1836 ats_mode = MODE_PROPORTIONAL;
1839 else if (0 == strcmp (mode_str, "RIL"))
1840 ats_mode = MODE_RIL;
1843 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1844 "Invalid resource assignment method `%s' configured, using proportional approach\n",
1846 ats_mode = MODE_PROPORTIONAL;
1848 GNUNET_free(mode_str);
1851 load_quotas (cfg, quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount);
1852 env.info_cb = &solver_info_cb;
1853 env.info_cb_cls = NULL;
1854 env.bandwidth_changed_cb = &bandwidth_changed_cb;
1855 env.bw_changed_cb_cls = NULL;
1856 env.get_preferences = &get_preferences_cb;
1857 env.get_preference_cls = NULL;
1858 env.get_property = &get_property_cb;
1859 env.get_property_cls = NULL;
1862 env.addresses = addresses;
1864 env.network_count = GNUNET_ATS_NetworkTypeCount;
1865 int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1866 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1868 env.networks[c] = networks[c];
1869 env.out_quota[c] = quotas_out[c];
1870 env.in_quota[c] = quotas_in[c];
1874 case MODE_PROPORTIONAL:
1875 plugin_short = "proportional";
1878 plugin_short = "mlp";
1881 plugin_short = "ril";
1884 plugin_short = NULL;
1887 GNUNET_asprintf (&plugin,
1888 "libgnunet_plugin_ats_%s",
1890 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1891 _("Initializing solver `%s '`%s'\n"),
1894 if (NULL == (solver = GNUNET_PLUGIN_load (plugin, &env)))
1896 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1897 _("Failed to initialize solver `%s'!\n"),
1899 return GNUNET_SYSERR;
1902 GNUNET_assert (NULL != env.sf.s_add);
1903 GNUNET_assert (NULL != env.sf.s_address_update_inuse);
1904 GNUNET_assert (NULL != env.sf.s_address_update_property);
1905 GNUNET_assert (NULL != env.sf.s_address_update_session);
1906 GNUNET_assert (NULL != env.sf.s_address_update_network);
1907 GNUNET_assert (NULL != env.sf.s_get);
1908 GNUNET_assert (NULL != env.sf.s_get_stop);
1909 GNUNET_assert (NULL != env.sf.s_pref);
1910 GNUNET_assert (NULL != env.sf.s_feedback);
1911 GNUNET_assert (NULL != env.sf.s_del);
1912 GNUNET_assert (NULL != env.sf.s_bulk_start);
1913 GNUNET_assert (NULL != env.sf.s_bulk_stop);
1916 GAS_normalization_start (&normalized_preference_changed_cb, NULL,
1917 &normalized_property_changed_cb, NULL);
1921 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1922 _("Failed to initialize solver!\n"));
1923 return GNUNET_SYSERR;
1925 /* up and running */
1926 running = GNUNET_YES;
1928 GNUNET_STATISTICS_set (stats,
1930 GNUNET_CONTAINER_multipeermap_size (addresses),
1938 * Destroy all addresses iterator
1941 * @param key peer identity (unused)
1942 * @param value the 'struct ATS_Address' to free
1943 * @return #GNUNET_OK (continue to iterate)
1946 destroy_all_address_it (void *cls,
1947 const struct GNUNET_PeerIdentity *key,
1950 struct ATS_Address *aa = value;
1953 GNUNET_assert(GNUNET_YES ==
1954 GNUNET_CONTAINER_multipeermap_remove (addresses,
1958 env.sf.s_del (solver, aa, GNUNET_NO);
1960 GAS_performance_notify_all_clients (&aa->peer,
1974 * Remove all addresses
1977 GAS_addresses_destroy_all ()
1979 if (GNUNET_NO == running)
1982 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1983 "Destroying all addresses\n");
1984 env.sf.s_bulk_start (solver);
1985 if (NULL != addresses)
1986 GNUNET_CONTAINER_multipeermap_iterate (addresses,
1987 &destroy_all_address_it,
1989 env.sf.s_bulk_start (solver);
1994 * Shutdown address subsystem.
1997 GAS_addresses_done ()
1999 struct GAS_Addresses_Suggestion_Requests *cur;
2000 struct GAS_Addresses_Preference_Clients *pcur;
2002 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2003 "Shutting down addresses\n");
2004 GAS_addresses_destroy_all ();
2005 running = GNUNET_NO;
2006 GNUNET_CONTAINER_multipeermap_destroy (addresses);
2008 while (NULL != (cur = pending_requests_head))
2010 GNUNET_CONTAINER_DLL_remove (pending_requests_head,
2011 pending_requests_tail,
2016 while (NULL != (pcur = preference_clients_head))
2018 GNUNET_CONTAINER_DLL_remove (preference_clients_head,
2019 preference_clients_tail,
2021 GNUNET_assert (pref_clients > 0);
2023 GNUNET_STATISTICS_set (stats,
2024 "# active performance clients",
2029 GNUNET_PLUGIN_unload (plugin,
2031 GNUNET_free (plugin);
2032 /* Stop configured solution method */
2033 GAS_normalization_stop ();
2038 * Closure for #peerinfo_it().
2040 struct PeerInfoIteratorContext
2043 * Function to call for each address.
2045 GNUNET_ATS_PeerInfo_Iterator it;
2048 * Closure for @e it.
2055 * Iterator to iterate over a peer's addresses
2057 * @param cls a `struct PeerInfoIteratorContext`
2058 * @param key the peer id
2059 * @param value the `struct ATS_address`
2060 * @return #GNUNET_OK to continue
2063 peerinfo_it (void *cls,
2064 const struct GNUNET_PeerIdentity *key,
2067 struct PeerInfoIteratorContext *pi_ctx = cls;
2068 struct ATS_Address *addr = value;
2070 pi_ctx->it (pi_ctx->it_cls,
2076 addr->atsi, addr->atsi_count,
2077 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_out),
2078 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_in));
2084 * Return information all peers currently known to ATS
2086 * @param peer the respective peer, NULL for 'all' peers
2087 * @param pi_it the iterator to call for every peer
2088 * @param pi_it_cls the closure for @a pi_it
2091 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer,
2092 GNUNET_ATS_PeerInfo_Iterator pi_it,
2095 struct PeerInfoIteratorContext pi_ctx;
2099 /* does not make sense without callback */
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2104 "Returning information for %s from a total of %u known addresses\n",
2107 : GNUNET_i2s (peer),
2108 (unsigned int) GNUNET_CONTAINER_multipeermap_size (addresses));
2110 pi_ctx.it_cls = pi_it_cls;
2112 GNUNET_CONTAINER_multipeermap_iterate (addresses,
2116 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
2118 &peerinfo_it, &pi_ctx);
2120 NULL, NULL, NULL, 0,
2127 /* end of gnunet-service-ats_addresses.c */