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-service-ats.h"
30 #include "gnunet-service-ats_addresses.h"
31 #include "gnunet-service-ats_normalization.h"
32 #include "gnunet-service-ats_performance.h"
33 #include "gnunet-service-ats_scheduling.h"
34 #include "gnunet-service-ats_reservations.h"
36 #include "gnunet-service-ats-solver_mlp.h"
38 #include "gnunet-service-ats-solver_proportional.h"
39 #include "gnunet-service-ats-solver_ril.h"
42 * NOTE: Do not change this documentation. This documentation is based on
43 * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
44 * use build_txt.sh to generate plaintext output
46 * 1 ATS addresses : ATS address management
48 * This ATS addresses ("addresses") component manages the addresses known to
49 * ATS service and suggests addresses to transport service when it is
50 * interested in address suggestion for a peer. ATS addresses also
51 * instantiates the bandwidth assignment mechanism (solver), notifies it
52 * about changes to addresses and forwards changes to bandwidth assignments
53 * to transport, depending if transport is interested in this change.
59 * Addresses are added by specifying peer ID, plugin, address, address length
60 * and session, if available. ATS information can be specified if available.
64 * ATS specifies a fix set of networks an address can belong to. For each
65 * network an inbound and outbound quota will be specified. The available
66 * networks and addtional helper varaibles are defined in
67 * gnunet_ats_service.h. At the moment 5 networks are defined:
68 * * GNUNET_ATS_NET_UNSPECIFIED
69 * * GNUNET_ATS_NET_LOOPBACK
70 * * GNUNET_ATS_NET_LAN
71 * * GNUNET_ATS_NET_WAN
72 * * GNUNET_ATS_NET_WLAN
74 * The total number of networks defined is stored in
75 * GNUNET_ATS_NetworkTypeCount GNUNET_ATS_NetworkType can be used array
76 * initializer for an int array, while GNUNET_ATS_NetworkType is an
77 * initializer for a char array containing a string description of all
82 * An inbound and outbound quota for each of the networks mentioned in 1.1.2
83 * is loaded from ats configuration during initialization. This quota defines
84 * to total amount of inbound and outbound traffic allowed for a specific
85 * network. The configuration values used are in section ats:
86 * * "NETWORK"_QUOTA_IN = <value>
87 * * "NETWORK"_QUOTA_IN = <value>
89 * You can specify quotas by setting the <value> to a:
90 * * unrestricted: unlimited
91 * * number of bytes: e.g. 10240
92 * * fancy value: e.g. 64 Kib
94 * unlimited is defined as GNUNET_ATS_MaxBandwidthString and equivalent to
95 * the value GNUNET_ATS_MaxBandwidth Important predefined values for quotas
97 * * GNUNET_ATS_DefaultBandwidth: 65536
98 * * GNUNET_ATS_MaxBandwidth: UINT32_MAX
99 * * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT: 1024
101 * Details of loading quotas and default values will be described on
103 * 1.1.4 Preference values
105 * 1.2 Data structures used
107 * Addresse uses struct ATS_Address for each address. The structs are stored
108 * in a linked list and provides a pointer void *solver_information for the
109 * solver to store address specific information. It provides the int values
110 * active which is set to GNUNET_YES if the address is select for transport
111 * use and used, representing that transport service is actively using this
112 * address. Address information are stored in peer, addr, addr_len, plugin.
116 * During initialization a hashmap to store addresses is created. The quotas
117 * for all networks defined for ATS are loaded from configuration. For each
118 * network first the logic will check if the string
119 * GNUNET_ATS_MaxBandwidthString is configured, if not it will try to convert
120 * the configured value as a fancy size and if this fails it will try to use
121 * it as a value_number. If no configuration value is found it will assign
122 * GNUNET_ATS_DefaultBandwidth. The most important step is to load the
123 * configured solver using configuration "[ats]:MODE". Current solvers are
124 * MODE_PROPORTIONAL, MODE_MLP. Interaction is done using a solver API
129 * * s_init: init the solver with required information
130 * * s_add: add a new address
131 * * s_update: update ATS values or session for an address
132 * * s_get: get prefered address for a peer
133 * * s_del: delete an address
134 * * s_pref: change preference value for a peer
135 * * s_done: shutdown solver
137 * Callbacks: addresses provides a bandwidth_changed_cb callback to the
138 * solver which is called when bandwidth assigned to peer has changed
142 * During shutdown all addresses are freed and the solver told to shutdown
144 * 1.6 Addresses and sessions
146 * Addresses consist of the address itself and a numerical session. When a
147 * new address without a session is added it has no session, so it gets
148 * session 0 assigned. When an address with a session is added and an address
149 * object with session 0 is found, this object is updated with the session
150 * otherwise a new address object with this session assigned is created.
154 * Addresses a1,a2 with session s1, s2 are "exact" if:
155 * (a1 == a2)&&(s1 == s2)
156 * Addresses a1,a2 with session s1, s2 are "equivalent" if:
157 * (a1 == a2)&&((s1 == s2)||(s1 == 0)||(s2 == 0)
159 * 1.7 Address management
161 * Transport service notifies ATS about changes to the addresses known to
164 * 1.7.1 Adding an address
166 * When transport learns a new address it tells ATS and ATS is telling
167 * addresses about it using GAS_address_add. If not known to addresses it
168 * creates a new address object and calls solver's s_add. ATS information are
169 * deserialized and solver is notified about the session and ATS information
172 * 1.7.2 Updating an address
174 * Addresses does an lookup up for the existing address with the given
175 * session. If disassembles included ATS information and notifies the solver
176 * using s_update about the update.
178 * 1.7.3 Deleting an address
180 * Addresses does an lookup for the exact address and session and if removes
181 * this address. If session != 0 the session is set to 0 and the address is
182 * kept. If session == 0, the addresses is removed.
184 * 1.7.4 Requesting an address suggestion
186 * The address client issues a request address message to be notified about
187 * address suggestions for a specific peer. Addresses asks the solver with
188 * s_get. If no address is available, it will not send a response, otherwise
189 * it will respond with the choosen address.
191 * 1.7.5 Address suggestions
193 * Addresses will notify the client automatically on any bandwidth_changed_cb
194 * by the solver if a address suggestion request is pending. If no address is
195 * available it will not respond at all If the client is not interested
196 * anymore, it has to cancel the address suggestion request.
198 * 1.7.6 Suggestions blocks and reset
200 * After suggesting an address it is blocked for ATS_BLOCKING_DELTA sec. to
201 * prevent the client from being thrashed. If the client requires immediately
202 * it can reset this block using GAS_addresses_handle_backoff_reset.
204 * 1.7.7 Marking address in use
206 * The client can notify addresses that it successfully uses an address and
207 * wants this address to be kept by calling GSA_address_in_use. Adresses will
208 * mark the address as used an notify the solver about the use.
210 * 1.7.8 Address lifecycle
213 * * (updated address) || (address in use)
216 * 1.8 Bandwidth assignment
218 * The addresses are used to perform resource allocation operations. ATS
219 * addresses takes care of instantiating the solver configured and notifies
220 * the respective solver about address changes and receives changes to the
221 * bandwidth assignment from the solver. The current bandwidth assignment is
222 * sent to transport. The specific solvers will be described in the specific
225 * 1.9 Changing peer preferences
227 * The bandwidth assigned to a peer can be influenced by setting a preference
228 * for a peer. The prefernce will be given to to the solver with s_pref which
229 * has to take care of the preference value
234 * Available ressource assignment modes
241 * Assign each peer an equal amount of bandwidth (bw)
243 * bw_per_peer = bw_total / #active addresses
250 * Solve ressource assignment as an optimization problem
251 * Uses an mixed integer programming solver
256 * Reinforcement Learning mode:
258 * Solve resource assignment using a learning agent
264 * Pending Address suggestion requests
266 struct GAS_Addresses_Suggestion_Requests
271 struct GAS_Addresses_Suggestion_Requests *next;
276 struct GAS_Addresses_Suggestion_Requests *prev;
281 struct GNUNET_PeerIdentity id;
285 * Handle for ATS address component
287 struct GAS_Addresses_Handle
292 struct GNUNET_STATISTICS_Handle *stat;
295 * A multihashmap to store all addresses
297 struct GNUNET_CONTAINER_MultiHashMap *addresses;
300 * Configure WAN quota in
302 unsigned long long wan_quota_in;
305 * Configure WAN quota out
307 unsigned long long wan_quota_out;
310 * Is ATS addresses running
315 * Configured ATS solver
325 * Address suggestion requests DLL head
327 struct GAS_Addresses_Suggestion_Requests *r_head;
330 * Address suggestion requests DLL tail
332 struct GAS_Addresses_Suggestion_Requests *r_tail;
334 /* Solver functions */
339 GAS_solver_init s_init;
342 * Add an address to the solver
344 GAS_solver_address_add s_add;
346 GAS_solver_address_property_changed s_address_update_property;
348 GAS_solver_address_session_changed s_address_update_session;
350 GAS_solver_address_inuse_changed s_address_update_inuse;
352 GAS_solver_address_network_changed s_address_update_network;
355 * Get address from solver
357 GAS_solver_get_preferred_address s_get;
360 * Get address from solver
362 GAS_solver_stop_get_preferred_address s_get_stop;
365 * Delete address in solver
367 GAS_solver_address_delete s_del;
370 * Change relative preference for quality in solver
372 GAS_solver_address_change_preference s_pref;
375 * Give feedback about the current assignment
377 GAS_solver_address_feedback_preference s_feedback;
380 * Start a bulk operation
382 GAS_solver_bulk_start s_bulk_start;
385 * Bulk operation done
387 GAS_solver_bulk_stop s_bulk_stop;
392 GAS_solver_done s_done;
396 * Disassemble ATS information and update performance information in address
398 * Updates existing information and adds new information
400 * @param dest destination address
401 * @param update source ATS information
402 * @param update_count number of ATS information
403 * @param delta_dest ats performance information which were updated
404 * including previous value
405 * @param delta_count number of ATS information in the delta
406 * @return GNUNET_YES if address was address updated, GNUNET_NO otherwise
409 disassemble_ats_information (struct ATS_Address *dest,
410 const struct GNUNET_ATS_Information *update, uint32_t update_count,
411 struct GNUNET_ATS_Information **delta_dest, uint32_t *delta_count)
419 struct GNUNET_ATS_Information add_atsi[update_count];
420 struct GNUNET_ATS_Information delta_atsi[update_count];
421 struct GNUNET_ATS_Information *tmp_atsi;
422 uint32_t add_atsi_count;
423 uint32_t delta_atsi_count;
427 delta_atsi_count = 0;
429 if (0 == update_count)
432 if (NULL == dest->atsi)
434 /* Create performance information */
436 GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
437 dest->atsi_count = update_count;
438 memcpy (dest->atsi, update,
439 update_count * sizeof(struct GNUNET_ATS_Information));
441 GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
442 for (c1 = 0; c1 < update_count; c1++)
444 (*delta_dest)[c1].type = update[c1].type;
445 (*delta_dest)[c1].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
447 (*delta_count) = update_count;
451 for (c1 = 0; c1 < update_count; c1++)
453 /* Update existing performance information */
455 for (c2 = 0; c2 < dest->atsi_count; c2++)
457 if (update[c1].type == dest->atsi[c2].type)
459 if (update[c1].value != dest->atsi[c2].value)
461 /* Save previous value in delta */
462 delta_atsi[delta_atsi_count] = dest->atsi[c2];
465 dest->atsi[c2].value = update[c1].value;
472 if (GNUNET_NO == found)
474 add_atsi[add_atsi_count] = update[c1];
476 delta_atsi[delta_atsi_count].type = update[c1].type;
477 delta_atsi[delta_atsi_count].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
482 if (add_atsi_count > 0)
484 /* Extend ats performance information */
486 tmp_atsi = GNUNET_malloc ((dest->atsi_count + add_atsi_count) *
487 (sizeof (struct GNUNET_ATS_Information)));
488 memcpy (tmp_atsi, dest->atsi,
489 dest->atsi_count * sizeof(struct GNUNET_ATS_Information));
490 memcpy (&tmp_atsi[dest->atsi_count], add_atsi,
491 add_atsi_count * sizeof(struct GNUNET_ATS_Information));
492 GNUNET_free(dest->atsi);
493 dest->atsi = tmp_atsi;
494 dest->atsi_count = dest->atsi_count + add_atsi_count;
498 if (delta_atsi_count > 0)
502 GNUNET_malloc (delta_atsi_count * sizeof (struct GNUNET_ATS_Information));
503 memcpy ((*delta_dest), delta_atsi,
504 delta_atsi_count * sizeof(struct GNUNET_ATS_Information));
505 (*delta_count) = delta_atsi_count;
512 * Free the given address
514 * @param addr address to destroy
517 free_address (struct ATS_Address *addr)
519 GNUNET_free(addr->plugin);
520 GNUNET_free_non_null(addr->atsi);
525 * Create a ATS_address with the given information
528 * @param plugin_name plugin
529 * @param plugin_addr address
530 * @param plugin_addr_len address length
531 * @param session_id session
532 * @return the ATS_Address
534 static struct ATS_Address *
535 create_address (const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
536 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
538 struct ATS_Address *aa = NULL;
542 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
544 aa->addr_len = plugin_addr_len;
546 memcpy (&aa[1], plugin_addr, plugin_addr_len);
547 aa->plugin = GNUNET_strdup (plugin_name);
548 aa->session_id = session_id;
549 aa->active = GNUNET_NO;
550 aa->used = GNUNET_NO;
551 aa->solver_information = NULL;
554 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
555 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
557 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
559 aa->atsin[c1].avg_queue_index = 0;
560 for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
561 aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
567 struct CompareAddressContext
569 const struct ATS_Address *search;
571 /* exact_address != NULL if address and session is equal */
572 struct ATS_Address *exact_address;
573 /* exact_address != NULL if address and session is 0 */
574 struct ATS_Address *base_address;
580 * @param cls a CompareAddressContext containin the source address
582 * @param value the address to compare with
583 * @return GNUNET_YES to continue, GNUNET_NO if address is founce
587 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
589 struct CompareAddressContext *cac = cls;
590 struct ATS_Address *aa = value;
592 /* Find an matching exact address:
595 * aa->addr_len == cac->search->addr_len
596 * aa->plugin == cac->search->plugin
597 * aa->addr == cac->search->addr
598 * aa->session == cac->search->session
600 * return as exact address
602 if ((aa->addr_len == cac->search->addr_len)
603 && (0 == strcmp (aa->plugin, cac->search->plugin)))
605 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
606 && (aa->session_id == cac->search->session_id))
607 cac->exact_address = aa;
610 /* Find an matching base address:
614 * aa->session_id == 0
617 * aa->addr_len == cac->search->addr_len
618 * aa->plugin == cac->search->plugin
619 * aa->addr == cac->search->addr
621 * return as base address
623 if ((aa->addr_len == cac->search->addr_len)
624 && (0 == strcmp (aa->plugin, cac->search->plugin)))
626 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
627 && (aa->session_id == 0))
628 cac->base_address = aa;
631 /* Find an matching exact address based on session:
635 * cac->search->addr_len == 0
638 * aa->plugin == cac->search->plugin
639 * aa->session_id == cac->search->session_id
641 * return as exact address
643 if (0 == cac->search->addr_len)
645 if ((0 == strcmp (aa->plugin, cac->search->plugin))
646 && (aa->session_id == cac->search->session_id))
647 cac->exact_address = aa;
650 if (cac->exact_address == NULL )
651 return GNUNET_YES; /* Continue iteration to find exact address */
653 return GNUNET_NO; /* Stop iteration since we have an exact address */
657 * Find an existing equivalent address record.
658 * Compares by peer identity and network address OR by session ID
659 * (one of the two must match).
661 * @param handle the address handle
662 * @param peer peer to lookup addresses for
663 * @param addr existing address record
664 * @return existing address record, NULL for none
667 find_equivalent_address (struct GAS_Addresses_Handle *handle,
668 const struct GNUNET_PeerIdentity *peer, const struct ATS_Address *addr)
670 struct CompareAddressContext cac;
672 cac.exact_address = NULL;
673 cac.base_address = NULL;
675 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
676 &peer->hashPubKey, &compare_address_it, &cac);
678 if (cac.exact_address == NULL )
679 return cac.base_address;
680 return cac.exact_address;
684 * Find the exact address
686 * @param handle the address handle to use
688 * @param plugin_name transport plugin name
689 * @param plugin_addr plugin address
690 * @param plugin_addr_len length of the plugin address
691 * @param session_id session id, can be 0
692 * @return an ATS_address or NULL
695 static struct ATS_Address *
696 find_exact_address (struct GAS_Addresses_Handle *handle,
697 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
698 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
700 struct ATS_Address *aa;
701 struct ATS_Address *ea;
703 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
706 /* Get existing address or address with session == 0 */
707 ea = find_equivalent_address (handle, peer, aa);
711 else if (ea->session_id != session_id)
717 * Extract an ATS performance info from an address
719 * @param address the address
720 * @param type the type to extract in HBO
721 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
724 get_performance_info (struct ATS_Address *address, uint32_t type)
727 GNUNET_assert(NULL != address);
729 if ((NULL == address->atsi) || (0 == address->atsi_count))
730 return GNUNET_ATS_VALUE_UNDEFINED;
732 for (c1 = 0; c1 < address->atsi_count; c1++)
734 if (ntohl (address->atsi[c1].type) == type)
735 return ntohl (address->atsi[c1].value);
737 return GNUNET_ATS_VALUE_UNDEFINED;
741 * Add a new address for a peer.
743 * @param handle the address handle to use
745 * @param plugin_name transport plugin name
746 * @param plugin_addr plugin address
747 * @param plugin_addr_len length of the plugin address
748 * @param session_id session id, can be 0
749 * @param atsi performance information for this address
750 * @param atsi_count number of performance information contained
753 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
754 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
755 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
756 const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
758 struct ATS_Address *new_address;
759 struct ATS_Address *existing_address;
760 struct GNUNET_ATS_Information *atsi_delta;
761 uint32_t atsi_delta_count;
763 uint32_t previous_session;
766 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
767 "ADDRESS ADD", GNUNET_i2s (peer));
769 if (GNUNET_NO == handle->running)
772 GNUNET_assert(NULL != handle->addresses);
774 new_address = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
777 disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
779 GNUNET_free_non_null(atsi_delta);
780 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
781 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
782 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
784 /* Get existing address or address with session == 0 */
785 existing_address = find_equivalent_address (handle, peer, new_address);
786 if (existing_address == NULL )
788 /* Add a new address */
790 GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (handle->addresses,
793 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
795 GNUNET_STATISTICS_set (handle->stat, "# addresses",
796 GNUNET_CONTAINER_multihashmap_size (handle->addresses), GNUNET_NO);
798 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
799 "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
800 new_address, GNUNET_i2s (peer), plugin_addr_len, session_id,
801 GNUNET_ATS_print_network_type (addr_net));
803 /* Tell solver about new address */
804 handle->s_add (handle->solver, new_address, addr_net);
806 handle->s_bulk_start (handle->solver);
807 GAS_normalization_normalize_property (handle->addresses, new_address, atsi,
809 handle->s_bulk_stop (handle->solver);
811 /* Notify performance clients about new address */
812 GAS_performance_notify_all_clients (&new_address->peer, new_address->plugin,
813 new_address->addr, new_address->addr_len, new_address->session_id,
814 new_address->atsi, new_address->atsi_count,
815 new_address->assigned_bw_out, new_address->assigned_bw_in);
819 /* We have an existing address we can use, clean up new */
820 GNUNET_free(new_address->plugin);
821 GNUNET_free_non_null(new_address->atsi);
822 GNUNET_free(new_address);
825 if (0 != existing_address->session_id)
827 /* Should not happen */
832 addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
833 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
834 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
836 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
837 "Found existing address for peer `%s' %p with new session %u in network %s\n",
838 GNUNET_i2s (peer), existing_address, session_id,
839 GNUNET_ATS_print_network_type (addr_net));
840 /* We have an address without an session, update this address */
842 atsi_delta_count = 0;
844 == disassemble_ats_information (existing_address, atsi, atsi_count,
845 &atsi_delta, &atsi_delta_count))
847 /* Notify performance clients about properties */
848 GAS_performance_notify_all_clients (&existing_address->peer,
849 existing_address->plugin, existing_address->addr,
850 existing_address->addr_len, existing_address->session_id,
851 existing_address->atsi, existing_address->atsi_count,
852 existing_address->assigned_bw_out, existing_address->assigned_bw_in);
854 for (c1 = 0; c1 < atsi_delta_count; c1++)
856 if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
857 && (addr_net != ntohl (atsi_delta[c1].value)))
859 /* Network type changed */
860 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
861 "Address for peer `%s' %p changed from network %s to %s\n",
862 GNUNET_i2s (peer), existing_address,
863 GNUNET_ATS_print_network_type (addr_net),
864 GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
865 handle->s_address_update_network (handle->solver, existing_address,
866 ntohl (atsi_delta[c1].value),
867 get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
868 addr_net = get_performance_info (existing_address,
869 GNUNET_ATS_NETWORK_TYPE);
872 /* Notify solver about update with atsi information and session */
873 handle->s_bulk_start (handle->solver);
874 GAS_normalization_normalize_property (handle->addresses, existing_address,
876 handle->s_bulk_stop (handle->solver);
878 GNUNET_free_non_null(atsi_delta);
880 /* Notify solver about new session */
881 if (existing_address->session_id == session_id)
882 return; /* possible, can both be 0 since address is revalidated */
884 previous_session = existing_address->session_id;
885 existing_address->session_id = session_id;
886 handle->s_address_update_session (handle->solver, existing_address,
887 previous_session, session_id);
889 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
890 "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
891 GNUNET_i2s (peer), existing_address, existing_address->addr_len,
892 session_id, GNUNET_ATS_print_network_type (addr_net));
896 * Update an address with a session or performance information for a peer.
898 * If an address was added without a session it will be updated with the
901 * @param handle the address handle to use
903 * @param plugin_name transport plugin name
904 * @param plugin_addr plugin address
905 * @param plugin_addr_len length of the plugin address
906 * @param session_id session id, can be 0
907 * @param atsi performance information for this address
908 * @param atsi_count number of performance information contained
911 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
912 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
913 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
914 const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
916 struct ATS_Address *aa;
917 struct GNUNET_ATS_Information *atsi_delta;
918 uint32_t atsi_delta_count;
919 uint32_t prev_session;
922 if (GNUNET_NO == handle->running)
925 GNUNET_assert(NULL != handle->addresses);
927 /* Get existing address */
928 aa = find_exact_address (handle, peer, plugin_name, plugin_addr,
929 plugin_addr_len, session_id);
932 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n", */
933 /* GNUNET_i2s (peer), plugin_name, session_id); */
934 /* GNUNET_break (0); */
938 if (NULL == aa->solver_information)
940 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
941 "Tried to update unknown address for peer `%s' `%s' session id %u\n",
942 GNUNET_i2s (peer), plugin_name, session_id);
946 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s' address \n",
947 "ADDRESS UPDATE", GNUNET_i2s (peer), aa);
950 if (session_id != aa->session_id)
952 /* Session changed */
953 prev_session = aa->session_id;
954 aa->session_id = session_id;
955 handle->s_address_update_session (handle->solver, aa, prev_session,
960 atsi_delta_count = 0;
962 == disassemble_ats_information (aa, atsi, atsi_count, &atsi_delta,
965 /* ATS properties changed */
966 for (c1 = 0; c1 < atsi_delta_count; c1++)
968 if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
970 /* Network type changed */
971 handle->s_address_update_network (handle->solver, aa,
972 ntohl (atsi_delta[c1].value),
973 get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
977 /* Notify performance clients about updated address */
978 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr,
979 aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
980 aa->assigned_bw_out, aa->assigned_bw_in);
982 handle->s_bulk_start (handle->solver);
983 GAS_normalization_normalize_property (handle->addresses, aa, atsi,
985 handle->s_bulk_stop (handle->solver);
987 GNUNET_free_non_null(atsi_delta);
990 struct DestroyContext
992 struct ATS_Address *aa;
994 struct GAS_Addresses_Handle *handle;
997 * GNUNET_NO : full address
998 * GNUNET_YES : just session
1006 * If session != 0, just the session is deleted, the address itself still exists
1007 * If session == 0, remove full address
1008 * If session == 0 and addrlen == 0, destroy inbound address
1012 * @param value the 'struct ATS_Address'
1013 * @return GNUNET_OK (continue to iterate)
1016 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key,
1019 struct DestroyContext *dc = cls;
1020 struct GAS_Addresses_Handle *handle = dc->handle;
1021 const struct ATS_Address *des = dc->aa;
1022 struct ATS_Address *aa = value;
1025 0 == memcmp (&aa->peer, &des->peer, sizeof(struct GNUNET_PeerIdentity)));
1027 if (des->session_id == 0)
1029 /* Session == 0, remove full address */
1030 if ((0 == strcmp (des->plugin, aa->plugin))
1031 && (aa->addr_len == des->addr_len)
1032 && (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
1035 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1036 "Deleting full address for peer `%s' session %u %p\n",
1037 GNUNET_i2s (&aa->peer), aa->session_id, aa);
1039 /* Notify solver about deletion */
1041 GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (handle->addresses, &aa->peer.hashPubKey, aa));
1042 handle->s_del (handle->solver, aa, GNUNET_NO);
1044 dc->result = GNUNET_NO;
1045 return GNUNET_OK; /* Continue iteration */
1050 /* Session != 0, just remove session */
1051 if (aa->session_id != des->session_id)
1052 return GNUNET_OK; /* irrelevant */
1054 if ((aa->session_id != 0) && (0 != strcmp (des->plugin, aa->plugin)))
1056 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1057 "Different plugins during removal: `%s' vs `%s' \n", des->plugin,
1063 if (aa->addr_len == 0)
1065 /* Inbound connection died, delete full address */
1066 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1067 "Deleting inbound address for peer `%s': `%s' session %u\n",
1068 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
1070 /* Notify solver about deletion */
1072 GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (handle->addresses, &aa->peer.hashPubKey, aa));
1073 handle->s_del (handle->solver, aa, GNUNET_NO);
1075 dc->result = GNUNET_NO;
1076 return GNUNET_OK; /* Continue iteration */
1081 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1082 "Deleting session for peer `%s': `%s' %u\n", GNUNET_i2s (&aa->peer),
1083 aa->plugin, aa->session_id);
1084 /* Notify solver to delete session */
1085 handle->s_del (handle->solver, aa, GNUNET_YES);
1087 aa->active = GNUNET_NO;
1095 * Remove an address or just a session for a peer.
1097 * @param handle the address handle to use
1099 * @param plugin_name transport plugin name
1100 * @param plugin_addr plugin address
1101 * @param plugin_addr_len length of the plugin address
1102 * @param session_id session id, can be 0
1105 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
1106 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1107 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
1109 struct ATS_Address *ea;
1110 struct DestroyContext dc;
1111 if (GNUNET_NO == handle->running)
1114 /* Get existing address */
1115 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1116 plugin_addr_len, session_id);
1119 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1120 "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
1121 GNUNET_i2s (peer), plugin_name, session_id);
1125 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1126 "Received `%s' for peer `%s' address %p session %u\n", "ADDRESS DESTROY",
1127 GNUNET_i2s (peer), ea, session_id);
1129 GNUNET_break(0 < strlen (plugin_name));
1131 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
1134 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
1135 &peer->hashPubKey, &destroy_by_session_id, &dc);
1136 GNUNET_STATISTICS_set (handle->stat, "# addresses",
1137 GNUNET_CONTAINER_multihashmap_size (handle->addresses), GNUNET_NO);
1138 free_address (dc.aa);
1142 * Notification about active use of an address.
1143 * in_use == GNUNET_YES:
1144 * This address is used to maintain an active connection with a peer.
1145 * in_use == GNUNET_NO:
1146 * This address is no longer used to maintain an active connection with a peer.
1148 * Note: can only be called with in_use == GNUNET_NO if called with GNUNET_YES
1151 * @param handle the address handle to use
1153 * @param plugin_name transport plugin name
1154 * @param plugin_addr plugin address
1155 * @param plugin_addr_len length of the plugin address
1156 * @param session_id session id, can be 0
1157 * @param in_use GNUNET_YES if GNUNET_NO
1158 * @return GNUNET_SYSERR on failure (address unknown ...)
1161 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
1162 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1163 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
1166 struct ATS_Address *ea;
1167 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1168 "ADDRESS IN USE", GNUNET_i2s (peer));
1170 if (GNUNET_NO == handle->running)
1171 return GNUNET_SYSERR;
1173 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1174 plugin_addr_len, session_id);
1177 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1178 "Trying to set unknown address `%s' `%s' `%u' to %s \n",
1179 GNUNET_i2s (peer), plugin_name, session_id,
1180 (GNUNET_NO == in_use) ? "NO" : "YES");
1182 return GNUNET_SYSERR;
1184 if (ea->used == in_use)
1187 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1188 "Address in use called multiple times for peer `%s': %s -> %s \n",
1189 GNUNET_i2s (peer), (GNUNET_NO == ea->used) ? "NO" : "YES",
1190 (GNUNET_NO == in_use) ? "NO" : "YES");
1191 return GNUNET_SYSERR;
1194 /* Tell solver about update */
1196 handle->s_address_update_inuse (handle->solver, ea, ea->used);
1201 * Cancel address suggestions for a peer
1203 * @param handle the address handle
1204 * @param peer the peer id
1207 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
1208 const struct GNUNET_PeerIdentity *peer)
1210 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1212 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received request: `%s' for peer %s\n",
1213 "request_address_cancel", GNUNET_i2s (peer));
1217 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1224 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1225 "No address requests pending for peer `%s', cannot remove!\n",
1229 handle->s_get_stop (handle->solver, peer);
1230 GAS_addresses_handle_backoff_reset (handle, peer);
1231 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Removed request pending for peer `%s\n",
1233 GNUNET_CONTAINER_DLL_remove(handle->r_head, handle->r_tail, cur);
1239 * Request address suggestions for a peer
1241 * @param handle the address handle
1242 * @param peer the peer id
1245 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
1246 const struct GNUNET_PeerIdentity *peer)
1248 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1249 struct ATS_Address *aa;
1251 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received `%s' for peer `%s'\n",
1252 "REQUEST ADDRESS", GNUNET_i2s (peer));
1254 if (GNUNET_NO == handle->running)
1258 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1259 break; /* already suggesting */
1264 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
1266 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1267 "Adding new address suggestion request for `%s'\n",
1269 GNUNET_CONTAINER_DLL_insert(handle->r_head, handle->r_tail, cur);
1273 * Debuging information about addresses
1275 * GNUNET_CONTAINER_multihashmap_get_multiple(handle->addresses,
1276 * &peer->hashPubKey, &addrinfo_it, (void *) peer);
1279 /* Get prefered address from solver */
1280 aa = (struct ATS_Address *) handle->s_get (handle->solver, peer);
1283 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1288 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1289 aa, GNUNET_i2s (peer));
1291 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
1292 aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
1293 aa->assigned_bw_out, aa->assigned_bw_in);
1295 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1296 ATS_BLOCKING_DELTA);
1297 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1298 aa->block_interval);
1300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1301 "Address %p ready for suggestion, block interval now %llu \n", aa,
1302 aa->block_interval);
1306 * Iterator to reset address blocking
1308 * @param cls not used
1309 * @param key the peer
1310 * @param value the address to reset
1311 * @return GNUNET_OK to continue
1314 reset_address_it (void *cls, const struct GNUNET_HashCode *key, void *value)
1316 struct ATS_Address *aa = value;
1318 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1319 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1320 GNUNET_i2s (&aa->peer), aa, aa->block_interval);
1322 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1323 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1328 * Reset suggestion backoff for a peer
1330 * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1331 * reset using this function
1333 * @param handle the address handle
1334 * @param peer the peer id
1337 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1338 const struct GNUNET_PeerIdentity *peer)
1340 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1341 "RESET BACKOFF", GNUNET_i2s (peer));
1344 GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &reset_address_it, NULL));
1348 * The preference changed for a peer
1350 * @param cls the address handle
1351 * @param peer the peer
1352 * @param kind the ATS kind
1353 * @param pref_rel the new relative preference value
1356 normalized_preference_changed_cb (void *cls,
1357 const struct GNUNET_PeerIdentity *peer, enum GNUNET_ATS_PreferenceKind kind,
1360 GNUNET_assert(NULL != cls);
1361 struct GAS_Addresses_Handle *handle = cls;
1363 /* Tell solver about update */
1364 handle->s_pref (handle->solver, peer, kind, pref_rel);
1368 * The relative value for a property changed
1370 * @param cls the address handle
1371 * @param address the peer
1372 * @param type the ATS type
1373 * @param prop_rel the new relative preference value
1376 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
1377 uint32_t type, double prop_rel)
1379 struct GAS_Addresses_Handle *ah = (struct GAS_Addresses_Handle *) cls;
1380 GNUNET_assert(NULL != ah);
1382 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1383 "Normalized property %s for peer `%s' changed to %.3f \n",
1384 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1387 ah->s_address_update_property (ah->solver, address, type, 0, prop_rel);
1391 * Function allowing the solver to obtain normalized preference
1392 * values from solver
1395 * @param id the peer to return the normalized properties for
1396 * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
1399 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
1401 return GAS_normalization_get_preferences (id);
1405 * Function allowing the solver to obtain normalized property
1406 * values for an address from solver
1409 * @param address the address
1410 * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
1413 get_property_cb (void *cls, const struct ATS_Address *address)
1415 return GAS_normalization_get_properties ((struct ATS_Address *) address);
1419 * Change the preference for a peer
1421 * @param handle the address handle
1422 * @param client the client sending this request
1423 * @param peer the peer id
1424 * @param kind the preference kind to change
1425 * @param score_abs the new preference score
1428 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
1429 void *client, const struct GNUNET_PeerIdentity *peer,
1430 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1433 "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1434 GNUNET_i2s (peer), client);
1436 if (GNUNET_NO == handle->running)
1440 == GNUNET_CONTAINER_multihashmap_contains (handle->addresses,
1443 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1444 "Received `%s' for unknown peer `%s' from client %p\n",
1445 "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1449 handle->s_bulk_start (handle->solver);
1450 /* Tell normalization about change, normalization will call callback if preference changed */
1451 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1452 handle->s_bulk_stop (handle->solver);
1456 * Change the preference for a peer
1458 * @param handle the address handle
1459 * @param application the client sending this request
1460 * @param peer the peer id
1461 * @param scope the time interval for this feedback: [now - scope .. now]
1462 * @param kind the preference kind to change
1463 * @param score_abs the new preference score
1466 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1467 void *application, const struct GNUNET_PeerIdentity *peer,
1468 const struct GNUNET_TIME_Relative scope,
1469 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1471 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1472 "Received `%s' for peer `%s' for client %p\n", "PREFERENCE FEEDBACK",
1473 GNUNET_i2s (peer), application);
1475 if (GNUNET_NO == handle->running)
1479 == GNUNET_CONTAINER_multihashmap_contains (handle->addresses,
1482 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1483 "Received `%s' for unknown peer `%s' from client %p\n",
1484 "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1488 handle->s_feedback (handle->solver, application, peer, scope, kind,
1493 * Load quotas for networks from configuration
1495 * @param cfg configuration handle
1496 * @param out_dest where to write outbound quotas
1497 * @param in_dest where to write inbound quotas
1498 * @param dest_length length of inbound and outbound arrays
1499 * @return number of networks loaded
1502 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1503 unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1505 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1506 char * entry_in = NULL;
1507 char * entry_out = NULL;
1508 char * quota_out_str;
1509 char * quota_in_str;
1513 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1517 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1518 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1522 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
1526 if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
1528 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1531 if ((GNUNET_NO == res)
1533 == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
1536 if ((GNUNET_NO == res)
1538 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
1542 if (GNUNET_NO == res)
1544 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1545 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1546 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1547 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1551 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1552 _("Outbound quota configure for network `%s' is %llu\n"),
1553 network_str[c], out_dest[c]);
1555 GNUNET_free(quota_out_str);
1559 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1560 _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1561 network_str[c], GNUNET_ATS_DefaultBandwidth);
1562 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1567 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
1571 if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
1573 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1576 if ((GNUNET_NO == res)
1578 == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1580 if ((GNUNET_NO == res)
1582 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
1586 if (GNUNET_NO == res)
1588 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1589 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1590 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1591 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1595 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1596 _("Inbound quota configured for network `%s' is %llu\n"),
1597 network_str[c], in_dest[c]);
1599 GNUNET_free(quota_in_str);
1603 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1604 _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1605 network_str[c], GNUNET_ATS_DefaultBandwidth);
1606 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1608 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1609 "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c],
1610 in_dest[c], out_dest[c]);
1611 GNUNET_free(entry_out);
1612 GNUNET_free(entry_in);
1614 return GNUNET_ATS_NetworkTypeCount;
1618 * Callback for solver to notify about assignment changes
1620 * @param cls the GAS_Addresses_Handle
1621 * @param address the address with changes
1624 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1626 struct GAS_Addresses_Handle *handle = cls;
1627 struct GAS_Addresses_Suggestion_Requests *cur;
1629 GNUNET_assert(handle != NULL);
1630 GNUNET_assert(address != NULL);
1632 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1633 "Bandwidth assignment changed for peer %s \n",
1634 GNUNET_i2s (&address->peer));
1636 /* Notify performance clients about changes to address */
1637 GAS_performance_notify_all_clients (&address->peer, address->plugin,
1638 address->addr, address->addr_len, address->session_id, address->atsi,
1639 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1640 cur = handle->r_head;
1643 if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
1644 break; /* we have an address request pending*/
1649 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Nobody is interested in peer `%s' :(\n",
1650 GNUNET_i2s (&address->peer));
1654 if ((0 == ntohl (address->assigned_bw_in.value__))
1655 && (0 == ntohl (address->assigned_bw_out.value__)))
1657 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1658 "Telling transport to disconnect peer `%s'\n",
1659 GNUNET_i2s (&address->peer));
1663 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1664 "Sending bandwidth update for peer `%s': %llu %llu\n",
1665 GNUNET_i2s (&address->peer), address->assigned_bw_out,
1666 address->assigned_bw_out);
1669 /* *Notify scheduling clients about suggestion */
1670 GAS_scheduling_transmit_address_suggestion (&address->peer, address->plugin,
1671 address->addr, address->addr_len, address->session_id, address->atsi,
1672 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1676 * Initialize address subsystem. The addresses subsystem manages the addresses
1677 * known and current performance information. It has a solver component
1678 * responsible for the resource allocation. It tells the solver about changes
1679 * and receives updates when the solver changes the resource allocation.
1681 * @param cfg configuration to use
1682 * @param stats the statistics handle to use
1683 * @return an address handle
1685 struct GAS_Addresses_Handle *
1686 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1687 const struct GNUNET_STATISTICS_Handle *stats)
1689 struct GAS_Addresses_Handle *ah;
1690 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1691 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1692 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1697 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1698 ah->running = GNUNET_NO;
1700 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1701 /* Initialize the addresses database */
1702 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1703 GNUNET_assert(NULL != ah->addresses);
1705 /* Figure out configured solution method */
1707 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1709 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1710 "No resource assignment method configured, using proportional approach\n");
1711 ah->ats_mode = MODE_PROPORTIONAL;
1715 for (c = 0; c < strlen (mode_str); c++)
1716 mode_str[c] = toupper (mode_str[c]);
1717 if (0 == strcmp (mode_str, "PROPORTIONAL"))
1719 ah->ats_mode = MODE_PROPORTIONAL;
1721 else if (0 == strcmp (mode_str, "MLP"))
1723 ah->ats_mode = MODE_MLP;
1725 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1726 "Assignment method `%s' configured, but GLPK is not available, please install \n",
1728 ah->ats_mode = MODE_PROPORTIONAL;
1731 else if (0 == strcmp (mode_str, "RIL"))
1733 ah->ats_mode = MODE_RIL;
1737 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1738 "Invalid resource assignment method `%s' configured, using proportional approach\n",
1740 ah->ats_mode = MODE_PROPORTIONAL;
1742 GNUNET_free(mode_str);
1744 /* Start configured solution method */
1745 switch (ah->ats_mode)
1748 /* Init the MLP solver with default values */
1750 ah->s_init = &GAS_mlp_init;
1751 ah->s_add = &GAS_mlp_address_add;
1752 ah->s_address_update_property = &GAS_mlp_address_property_changed;
1753 ah->s_address_update_session = &GAS_mlp_address_session_changed;
1754 ah->s_address_update_inuse = &GAS_mlp_address_inuse_changed;
1755 ah->s_address_update_network = &GAS_mlp_address_change_network;
1756 ah->s_get = &GAS_mlp_get_preferred_address;
1757 ah->s_get_stop = &GAS_mlp_stop_get_preferred_address;
1758 ah->s_pref = &GAS_mlp_address_change_preference;
1759 ah->s_feedback = &GAS_mlp_address_preference_feedback;
1760 ah->s_del = &GAS_mlp_address_delete;
1761 ah->s_bulk_start = &GAS_mlp_bulk_start;
1762 ah->s_bulk_stop = &GAS_mlp_bulk_stop;
1763 ah->s_done = &GAS_mlp_done;
1769 case MODE_PROPORTIONAL:
1770 /* Init the proportional solver with default values */
1771 ah->s_init = &GAS_proportional_init;
1772 ah->s_add = &GAS_proportional_address_add;
1773 ah->s_address_update_property = &GAS_proportional_address_property_changed;
1774 ah->s_address_update_session = &GAS_proportional_address_session_changed;
1775 ah->s_address_update_inuse = &GAS_proportional_address_inuse_changed;
1776 ah->s_address_update_network = &GAS_proportional_address_change_network;
1777 ah->s_get = &GAS_proportional_get_preferred_address;
1778 ah->s_get_stop = &GAS_proportional_stop_get_preferred_address;
1779 ah->s_pref = &GAS_proportional_address_change_preference;
1780 ah->s_feedback = &GAS_proportional_address_preference_feedback;
1781 ah->s_del = &GAS_proportional_address_delete;
1782 ah->s_bulk_start = &GAS_proportional_bulk_start;
1783 ah->s_bulk_stop = &GAS_proportional_bulk_stop;
1784 ah->s_done = &GAS_proportional_done;
1785 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n",
1789 /* Init the ril solver with default values */
1790 ah->s_init = &GAS_ril_init;
1791 ah->s_add = &GAS_ril_address_add;
1792 ah->s_address_update_property = &GAS_ril_address_property_changed;
1793 ah->s_address_update_session = &GAS_ril_address_session_changed;
1794 ah->s_address_update_inuse = &GAS_ril_address_inuse_changed;
1795 ah->s_address_update_network = &GAS_ril_address_change_network;
1796 ah->s_get = &GAS_ril_get_preferred_address;
1797 ah->s_get_stop = &GAS_ril_stop_get_preferred_address;
1798 ah->s_pref = &GAS_ril_address_change_preference;
1799 ah->s_feedback = &GAS_ril_address_preference_feedback;
1800 ah->s_del = &GAS_ril_address_delete;
1801 ah->s_bulk_start = &GAS_ril_bulk_start;
1802 ah->s_bulk_stop = &GAS_ril_bulk_stop;
1803 ah->s_done = &GAS_ril_done;
1804 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "RIL");
1811 GNUNET_assert(NULL != ah->s_init);
1812 GNUNET_assert(NULL != ah->s_add);
1813 GNUNET_assert(NULL != ah->s_address_update_inuse);
1814 GNUNET_assert(NULL != ah->s_address_update_property);
1815 GNUNET_assert(NULL != ah->s_address_update_session);
1816 GNUNET_assert(NULL != ah->s_address_update_network);
1817 GNUNET_assert(NULL != ah->s_get);
1818 GNUNET_assert(NULL != ah->s_get_stop);
1819 GNUNET_assert(NULL != ah->s_pref);
1820 GNUNET_assert(NULL != ah->s_feedback);
1821 GNUNET_assert(NULL != ah->s_del);
1822 GNUNET_assert(NULL != ah->s_done);
1823 GNUNET_assert(NULL != ah->s_bulk_start);
1824 GNUNET_assert(NULL != ah->s_bulk_stop);
1826 GAS_normalization_start (&normalized_preference_changed_cb, ah,
1827 &normalized_property_changed_cb, ah);
1828 quota_count = load_quotas (cfg, quotas_in, quotas_out,
1829 GNUNET_ATS_NetworkTypeCount);
1831 ah->solver = ah->s_init (cfg, stats, ah->addresses, quotas, quotas_in,
1832 quotas_out, quota_count, &bandwidth_changed_cb, ah, &get_preferences_cb,
1833 NULL, &get_property_cb, NULL );
1834 if (NULL == ah->solver)
1836 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver!\n"));
1840 /* up and running */
1841 ah->running = GNUNET_YES;
1843 GNUNET_STATISTICS_set (ah->stat, "# addresses",
1844 GNUNET_CONTAINER_multihashmap_size (ah->addresses), GNUNET_NO);
1850 * Destroy all addresses iterator
1853 * @param key peer identity (unused)
1854 * @param value the 'struct ATS_Address' to free
1855 * @return GNUNET_OK (continue to iterate)
1858 destroy_all_address_it (void *cls, const struct GNUNET_HashCode * key,
1861 struct GAS_Addresses_Handle *handle = cls;
1862 struct ATS_Address *aa = value;
1866 GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (handle->addresses, key, value));
1868 handle->s_del (handle->solver, aa, GNUNET_NO);
1876 * Remove all addresses
1878 * @param handle the address handle to use
1881 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1883 if (GNUNET_NO == handle->running)
1886 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Destroying all addresses\n");
1887 handle->s_bulk_start (handle->solver);
1888 if (handle->addresses != NULL )
1889 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses,
1890 &destroy_all_address_it, handle);
1891 handle->s_bulk_start (handle->solver);
1895 * Shutdown address subsystem.
1897 * @param handle the address handle to shutdown
1900 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1902 struct GAS_Addresses_Suggestion_Requests *cur;
1904 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Shutting down addresses\n");
1905 GNUNET_assert(NULL != handle);
1906 GAS_addresses_destroy_all (handle);
1907 handle->running = GNUNET_NO;
1908 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1909 handle->addresses = NULL;
1910 while (NULL != (cur = handle->r_head))
1912 GNUNET_CONTAINER_DLL_remove(handle->r_head, handle->r_tail, cur);
1915 handle->s_done (handle->solver);
1916 GNUNET_free(handle);
1917 /* Stop configured solution method */
1918 GAS_normalization_stop ();
1921 struct PeerIteratorContext
1923 GNUNET_ATS_Peer_Iterator it;
1925 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1929 * Iterator to iterate over all peers
1931 * @param cls a PeerIteratorContext
1932 * @param key the peer id
1933 * @param value the ATS_address
1934 * @return GNUNET_OK to continue
1937 peer_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1939 struct PeerIteratorContext *ip_ctx = cls;
1940 struct GNUNET_PeerIdentity tmp;
1943 == GNUNET_CONTAINER_multihashmap_contains (ip_ctx->peers_returned, key))
1945 GNUNET_CONTAINER_multihashmap_put (ip_ctx->peers_returned, key, NULL,
1946 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1947 tmp.hashPubKey = (*key);
1948 ip_ctx->it (ip_ctx->it_cls, &tmp);
1955 * Return information all peers currently known to ATS
1957 * @param handle the address handle to use
1958 * @param p_it the iterator to call for every peer
1959 * @param p_it_cls the closure for the iterator
1962 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
1963 GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1965 struct PeerIteratorContext ip_ctx;
1970 GNUNET_assert(NULL != handle->addresses);
1972 size = GNUNET_CONTAINER_multihashmap_size (handle->addresses);
1976 ip_ctx.it_cls = p_it_cls;
1977 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size,
1979 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it,
1981 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1983 p_it (p_it_cls, NULL );
1986 struct PeerInfoIteratorContext
1988 GNUNET_ATS_PeerInfo_Iterator it;
1993 * Iterator to iterate over a peer's addresses
1995 * @param cls a PeerInfoIteratorContext
1996 * @param key the peer id
1997 * @param value the ATS_address
1998 * @return GNUNET_OK to continue
2001 peerinfo_it (void *cls, const struct GNUNET_HashCode * key, void *value)
2003 struct PeerInfoIteratorContext *pi_ctx = cls;
2004 struct ATS_Address *addr = (struct ATS_Address *) value;
2006 if (NULL != pi_ctx->it)
2008 pi_ctx->it (pi_ctx->it_cls, &addr->peer, addr->plugin, addr->addr,
2009 addr->addr_len, addr->active, addr->atsi, addr->atsi_count,
2010 addr->assigned_bw_out, addr->assigned_bw_in);
2016 * Return information all peers currently known to ATS
2018 * @param handle the address handle to use
2019 * @param peer the respective peer
2020 * @param pi_it the iterator to call for every peer
2021 * @param pi_it_cls the closure for the iterator
2024 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
2025 const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it,
2028 struct PeerInfoIteratorContext pi_ctx;
2029 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
2030 GNUNET_assert(NULL != peer);
2031 GNUNET_assert(NULL != handle->addresses);
2033 return; /* does not make sense without callback */
2035 zero_bw = GNUNET_BANDWIDTH_value_init (0);
2037 pi_ctx.it_cls = pi_it_cls;
2039 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
2040 &peer->hashPubKey, &peerinfo_it, &pi_ctx);
2043 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw,
2048 /* end of gnunet-service-ats_addresses.c */