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
235 * Available ressource assignment modes
242 * Assign each peer an equal amount of bandwidth (bw)
244 * bw_per_peer = bw_total / #active addresses
251 * Solve ressource assignment as an optimization problem
252 * Uses an mixed integer programming solver
257 * Reinforcement Learning mode:
259 * Solve resource assignment using a learning agent
266 * Pending Address suggestion requests
268 struct GAS_Addresses_Suggestion_Requests
273 struct GAS_Addresses_Suggestion_Requests *next;
278 struct GAS_Addresses_Suggestion_Requests *prev;
283 struct GNUNET_PeerIdentity id;
288 * Handle for ATS address component
290 struct GAS_Addresses_Handle
295 struct GNUNET_STATISTICS_Handle *stat;
298 * A multihashmap to store all addresses
300 struct GNUNET_CONTAINER_MultiHashMap *addresses;
303 * Configure WAN quota in
305 unsigned long long wan_quota_in;
308 * Configure WAN quota out
310 unsigned long long wan_quota_out;
313 * Is ATS addresses running
318 * Configured ATS solver
328 * Address suggestion requests DLL head
330 struct GAS_Addresses_Suggestion_Requests *r_head;
333 * Address suggestion requests DLL tail
335 struct GAS_Addresses_Suggestion_Requests *r_tail;
337 /* Solver functions */
342 GAS_solver_init s_init;
345 * Add an address to the solver
347 GAS_solver_address_add s_add;
350 GAS_solver_address_property_changed s_address_update_property;
352 GAS_solver_address_session_changed s_address_update_session;
354 GAS_solver_address_inuse_changed s_address_update_inuse;
356 GAS_solver_address_network_changed s_address_update_network;
359 * Get address from solver
361 GAS_solver_get_preferred_address s_get;
364 * Get address from solver
366 GAS_solver_stop_get_preferred_address s_get_stop;
369 * Delete address in solver
371 GAS_solver_address_delete s_del;
374 * Change relative preference for quality in solver
376 GAS_solver_address_change_preference s_pref;
379 * Give feedback about the current assignment
381 GAS_solver_address_feedback_preference s_feedback;
384 * Start a bulk operation
386 GAS_solver_bulk_start s_bulk_start;
389 * Bulk operation done
391 GAS_solver_bulk_stop s_bulk_stop;
396 GAS_solver_done s_done;
401 * Disassemble ATS information and update performance information in address
403 * Updates existing information and adds new information
405 * @param dest destination address
406 * @param update source ATS information
407 * @param update_count number of ATS information
408 * @param delta_dest ats performance information which were updated
409 * including previous value
410 * @param delta_count number of ATS information in the delta
411 * @return GNUNET_YES if address was address updated, GNUNET_NO otherwise
414 disassemble_ats_information (struct ATS_Address *dest,
415 const struct GNUNET_ATS_Information *update,
416 uint32_t update_count,
417 struct GNUNET_ATS_Information **delta_dest,
418 uint32_t *delta_count)
426 struct GNUNET_ATS_Information add_atsi[update_count];
427 struct GNUNET_ATS_Information delta_atsi[update_count];
428 struct GNUNET_ATS_Information *tmp_atsi;
429 uint32_t add_atsi_count;
430 uint32_t delta_atsi_count;
434 delta_atsi_count = 0;
436 if (0 == update_count)
439 if (NULL == dest->atsi)
441 /* Create performance information */
442 dest->atsi = GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
443 dest->atsi_count = update_count;
444 memcpy (dest->atsi, update, update_count * sizeof (struct GNUNET_ATS_Information));
445 (*delta_dest) = GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
446 for (c1 = 0; c1 < update_count; c1 ++)
448 (*delta_dest)[c1].type = update[c1].type;
449 (*delta_dest)[c1].value = htonl(GNUNET_ATS_VALUE_UNDEFINED);
451 (*delta_count) = update_count;
455 for (c1 = 0; c1 < update_count; c1++)
457 /* Update existing performance information */
459 for (c2 = 0; c2 < dest->atsi_count; c2++)
461 if (update[c1].type == dest->atsi[c2].type)
463 if (update[c1].value != dest->atsi[c2].value)
465 /* Save previous value in delta */
466 delta_atsi[delta_atsi_count] = dest->atsi[c2];
469 dest->atsi[c2].value = update[c1].value;
476 if (GNUNET_NO == found)
478 add_atsi[add_atsi_count] = update[c1];
480 delta_atsi[delta_atsi_count].type = update[c1].type;
481 delta_atsi[delta_atsi_count].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
486 if (add_atsi_count > 0)
488 /* Extend ats performance information */
490 tmp_atsi = GNUNET_malloc ((dest->atsi_count + add_atsi_count) *
491 (sizeof (struct GNUNET_ATS_Information)));
492 memcpy (tmp_atsi, dest->atsi, dest->atsi_count * sizeof (struct GNUNET_ATS_Information));
493 memcpy (&tmp_atsi[dest->atsi_count], add_atsi, add_atsi_count * sizeof (struct GNUNET_ATS_Information));
494 GNUNET_free (dest->atsi);
495 dest->atsi = tmp_atsi;
496 dest->atsi_count = dest->atsi_count + add_atsi_count;
500 if (delta_atsi_count > 0)
503 (*delta_dest) = GNUNET_malloc (delta_atsi_count * sizeof (struct GNUNET_ATS_Information));
504 memcpy ((*delta_dest), delta_atsi, 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,
536 const char *plugin_name,
537 const void *plugin_addr, size_t plugin_addr_len,
540 struct ATS_Address *aa = NULL;
544 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
546 aa->addr_len = plugin_addr_len;
548 memcpy (&aa[1], plugin_addr, plugin_addr_len);
549 aa->plugin = GNUNET_strdup (plugin_name);
550 aa->session_id = session_id;
551 aa->active = GNUNET_NO;
552 aa->used = GNUNET_NO;
553 aa->solver_information = NULL;
556 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
557 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
559 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1 ++)
561 aa->atsin[c1].avg_queue_index = 0;
562 for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
563 aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
570 struct CompareAddressContext
572 const struct ATS_Address *search;
574 /* exact_address != NULL if address and session is equal */
575 struct ATS_Address *exact_address;
576 /* exact_address != NULL if address and session is 0 */
577 struct ATS_Address *base_address;
583 * @param cls a CompareAddressContext containin the source address
585 * @param value the address to compare with
586 * @return GNUNET_YES to continue, GNUNET_NO if address is founce
590 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
592 struct CompareAddressContext *cac = cls;
593 struct ATS_Address *aa = value;
595 /* Find an matching exact address:
598 * aa->addr_len == cac->search->addr_len
599 * aa->plugin == cac->search->plugin
600 * aa->addr == cac->search->addr
601 * aa->session == cac->search->session
603 * return as exact address
605 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
607 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
608 cac->exact_address = aa;
611 /* Find an matching base address:
615 * aa->session_id == 0
618 * aa->addr_len == cac->search->addr_len
619 * aa->plugin == cac->search->plugin
620 * aa->addr == cac->search->addr
622 * return as base address
624 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
626 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
627 cac->base_address = aa;
630 /* Find an matching exact address based on session:
634 * cac->search->addr_len == 0
637 * aa->plugin == cac->search->plugin
638 * aa->session_id == cac->search->session_id
640 * return as exact address
642 if (0 == cac->search->addr_len)
644 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
645 cac->exact_address = aa;
648 if (cac->exact_address == NULL)
649 return GNUNET_YES; /* Continue iteration to find exact address */
651 return GNUNET_NO; /* Stop iteration since we have an exact address */
656 * Find an existing equivalent address record.
657 * Compares by peer identity and network address OR by session ID
658 * (one of the two must match).
660 * @param handle the address handle
661 * @param peer peer to lookup addresses for
662 * @param addr existing address record
663 * @return existing address record, NULL for none
666 find_equivalent_address (struct GAS_Addresses_Handle *handle,
667 const struct GNUNET_PeerIdentity *peer,
668 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, &peer->hashPubKey,
676 &compare_address_it, &cac);
678 if (cac.exact_address == NULL)
679 return cac.base_address;
680 return cac.exact_address;
685 * Find the exact address
687 * @param handle the address handle to use
689 * @param plugin_name transport plugin name
690 * @param plugin_addr plugin address
691 * @param plugin_addr_len length of the plugin address
692 * @param session_id session id, can be 0
693 * @return an ATS_address or NULL
696 static struct ATS_Address *
697 find_exact_address (struct GAS_Addresses_Handle *handle,
698 const struct GNUNET_PeerIdentity *peer,
699 const char *plugin_name,
700 const void *plugin_addr,
701 size_t plugin_addr_len,
704 struct ATS_Address *aa;
705 struct ATS_Address *ea;
707 aa = create_address (peer,
709 plugin_addr, plugin_addr_len,
712 /* Get existing address or address with session == 0 */
713 ea = find_equivalent_address (handle, peer, aa);
717 else if (ea->session_id != session_id)
724 * Extract an ATS performance info from an address
726 * @param address the address
727 * @param type the type to extract in HBO
728 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
731 get_performance_info (struct ATS_Address *address, uint32_t type)
734 GNUNET_assert (NULL != address);
736 if ((NULL == address->atsi) || (0 == address->atsi_count))
737 return GNUNET_ATS_VALUE_UNDEFINED;
739 for (c1 = 0; c1 < address->atsi_count; c1++)
741 if (ntohl(address->atsi[c1].type) == type)
742 return ntohl(address->atsi[c1].value);
744 return GNUNET_ATS_VALUE_UNDEFINED;
749 * Add a new address for a peer.
751 * @param handle the address handle to use
753 * @param plugin_name transport plugin name
754 * @param plugin_addr plugin address
755 * @param plugin_addr_len length of the plugin address
756 * @param session_id session id, can be 0
757 * @param atsi performance information for this address
758 * @param atsi_count number of performance information contained
761 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
762 const struct GNUNET_PeerIdentity *peer,
763 const char *plugin_name,
764 const void *plugin_addr,
765 size_t plugin_addr_len,
767 const struct GNUNET_ATS_Information *atsi,
770 struct ATS_Address *new_address;
771 struct ATS_Address *existing_address;
772 struct GNUNET_ATS_Information *atsi_delta;
773 uint32_t atsi_delta_count;
775 uint32_t previous_session;
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "Received `%s' for peer `%s'\n",
783 if (GNUNET_NO == handle->running)
786 GNUNET_assert (NULL != handle->addresses);
788 new_address = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
791 disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta, &atsi_delta_count);
792 GNUNET_free_non_null (atsi_delta);
793 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
794 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
795 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
797 /* Get existing address or address with session == 0 */
798 existing_address = find_equivalent_address (handle, peer, new_address);
799 if (existing_address == NULL)
801 /* Add a new address */
802 GNUNET_assert (GNUNET_OK ==
803 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
804 &peer->hashPubKey, new_address,
805 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
807 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Adding new address %p for peer `%s' session id %u, %s\n",
808 new_address, GNUNET_i2s (peer), session_id, GNUNET_ATS_print_network_type(addr_net));
810 /* Tell solver about new address */
811 handle->s_add (handle->solver, new_address, addr_net);
813 handle->s_bulk_start (handle->solver);
814 GAS_normalization_normalize_property (handle->addresses, new_address, atsi, atsi_count);
815 handle->s_bulk_stop (handle->solver);
817 /* Notify performance clients about new address */
818 GAS_performance_notify_all_clients (&new_address->peer,
820 new_address->addr, new_address->addr_len,
821 new_address->session_id,
822 new_address->atsi, new_address->atsi_count,
823 new_address->assigned_bw_out,
824 new_address->assigned_bw_in);
828 /* We have an existing address we can use, clean up new */
829 GNUNET_free (new_address->plugin);
830 GNUNET_free_non_null (new_address->atsi);
831 GNUNET_free (new_address);
834 if (0 != existing_address->session_id)
836 /* Should not happen */
841 addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
842 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
843 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
846 "Found existing address for peer `%s' %p with new session %u in network %s\n",
850 GNUNET_ATS_print_network_type (addr_net));
851 /* We have an address without an session, update this address */
853 atsi_delta_count = 0;
854 if (GNUNET_YES == disassemble_ats_information (existing_address, atsi, atsi_count, &atsi_delta, &atsi_delta_count))
856 /* Notify performance clients about properties */
857 GAS_performance_notify_all_clients (&existing_address->peer,
858 existing_address->plugin,
859 existing_address->addr, existing_address->addr_len,
860 existing_address->session_id,
861 existing_address->atsi, existing_address->atsi_count,
862 existing_address->assigned_bw_out,
863 existing_address->assigned_bw_in);
865 for (c1 = 0; c1 < atsi_delta_count; c1++)
867 if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type)) &&
868 (addr_net != ntohl (atsi_delta[c1].value)))
870 /* Network type changed */
871 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Address for peer `%s' %p changed from network %s to %s\n",
874 GNUNET_ATS_print_network_type (addr_net),
875 GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
876 handle->s_address_update_network (handle->solver, existing_address,
877 ntohl (atsi_delta[c1].value),
878 get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
879 addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
883 /* Notify solver about update with atsi information and session */
884 handle->s_bulk_start (handle->solver);
885 GAS_normalization_normalize_property (handle->addresses, existing_address, atsi, atsi_count);
886 handle->s_bulk_stop (handle->solver);
888 GNUNET_free_non_null (atsi_delta);
890 /* Notify solver about new session */
891 if (existing_address->session_id == session_id)
892 return; /* possible, can both be 0 since address is revalidated */
894 previous_session = existing_address->session_id;
895 existing_address->session_id = session_id;
896 handle->s_address_update_session (handle->solver, existing_address,
897 previous_session, session_id);
899 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
900 "Updated existing address for peer `%s' %p with new session %u in network %s\n",
904 GNUNET_ATS_print_network_type(addr_net));
909 * Update an address with a session or performance information for a peer.
911 * If an address was added without a session it will be updated with the
914 * @param handle the address handle to use
916 * @param plugin_name transport plugin name
917 * @param plugin_addr plugin address
918 * @param plugin_addr_len length of the plugin address
919 * @param session_id session id, can be 0
920 * @param atsi performance information for this address
921 * @param atsi_count number of performance information contained
924 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
925 const struct GNUNET_PeerIdentity *peer,
926 const char *plugin_name, const void *plugin_addr,
927 size_t plugin_addr_len, uint32_t session_id,
928 const struct GNUNET_ATS_Information *atsi,
931 struct ATS_Address *aa;
932 struct GNUNET_ATS_Information *atsi_delta;
933 uint32_t atsi_delta_count;
934 uint32_t prev_session;
937 if (GNUNET_NO == handle->running)
940 GNUNET_assert (NULL != handle->addresses);
942 /* Get existing address */
943 aa = find_exact_address (handle, peer, plugin_name,
944 plugin_addr, plugin_addr_len, session_id);
947 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n", */
948 /* GNUNET_i2s (peer), plugin_name, session_id); */
949 /* GNUNET_break (0); */
953 if (NULL == aa->solver_information)
955 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
956 GNUNET_i2s (peer), plugin_name, session_id);
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
961 "Received `%s' for peer `%s' address \n",
963 GNUNET_i2s (peer), aa);
966 if (session_id != aa->session_id)
968 /* Session changed */
969 prev_session = aa->session_id;
970 aa->session_id = session_id;
971 handle->s_address_update_session (handle->solver, aa, prev_session, aa->session_id);
975 atsi_delta_count = 0;
976 if (GNUNET_YES == disassemble_ats_information (aa, atsi, atsi_count, &atsi_delta, &atsi_delta_count))
978 /* ATS properties changed */
979 for (c1 = 0; c1 < atsi_delta_count; c1++)
981 if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
983 /* Network type changed */
984 handle->s_address_update_network (handle->solver, aa,
985 ntohl (atsi_delta[c1].value),
986 get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
990 /* Notify performance clients about updated address */
991 GAS_performance_notify_all_clients (&aa->peer,
993 aa->addr, aa->addr_len,
995 aa->atsi, aa->atsi_count,
999 handle->s_bulk_start (handle->solver);
1000 GAS_normalization_normalize_property (handle->addresses, aa, atsi, atsi_count);
1001 handle->s_bulk_stop (handle->solver);
1003 GNUNET_free_non_null (atsi_delta);
1007 struct DestroyContext
1009 struct ATS_Address *aa;
1011 struct GAS_Addresses_Handle *handle;
1014 * GNUNET_NO : full address
1015 * GNUNET_YES : just session
1024 * If session != 0, just the session is deleted, the address itself still exists
1025 * If session == 0, remove full address
1026 * If session == 0 and addrlen == 0, destroy inbound address
1030 * @param value the 'struct ATS_Address'
1031 * @return GNUNET_OK (continue to iterate)
1034 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
1036 struct DestroyContext *dc = cls;
1037 struct GAS_Addresses_Handle *handle = dc->handle;
1038 const struct ATS_Address *des = dc->aa;
1039 struct ATS_Address *aa = value;
1041 GNUNET_assert (0 == memcmp (&aa->peer, &des->peer,
1042 sizeof (struct GNUNET_PeerIdentity)));
1045 if (des->session_id == 0)
1047 /* Session == 0, remove full address */
1048 if ((0 == strcmp (des->plugin, aa->plugin)) &&
1049 (aa->addr_len == des->addr_len) &&
1050 (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
1053 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1054 "Deleting full address for peer `%s' session %u %p\n",
1055 GNUNET_i2s (&aa->peer), aa->session_id, aa);
1057 /* Notify solver about deletion */
1058 GNUNET_assert (GNUNET_YES ==
1059 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
1060 &aa->peer.hashPubKey, aa));
1061 handle->s_del (handle->solver, aa, GNUNET_NO);
1063 dc->result = GNUNET_NO;
1064 return GNUNET_OK; /* Continue iteration */
1069 /* Session != 0, just remove session */
1070 if (aa->session_id != des->session_id)
1071 return GNUNET_OK; /* irrelevant */
1073 if ((aa->session_id != 0) &&
1074 (0 != strcmp (des->plugin, aa->plugin)))
1076 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1077 "Different plugins during removal: `%s' vs `%s' \n",
1078 des->plugin, aa->plugin);
1083 if (aa->addr_len == 0)
1085 /* Inbound connection died, delete full address */
1086 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1087 "Deleting inbound address for peer `%s': `%s' session %u\n",
1088 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
1090 /* Notify solver about deletion */
1091 GNUNET_assert (GNUNET_YES ==
1092 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
1093 &aa->peer.hashPubKey, aa));
1094 handle->s_del (handle->solver, aa, GNUNET_NO);
1096 dc->result = GNUNET_NO;
1097 return GNUNET_OK; /* Continue iteration */
1102 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1103 "Deleting session for peer `%s': `%s' %u\n",
1104 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
1105 /* Notify solver to delete session */
1106 handle->s_del (handle->solver, aa, GNUNET_YES);
1116 * Remove an address or just a session for a peer.
1118 * @param handle the address handle to use
1120 * @param plugin_name transport plugin name
1121 * @param plugin_addr plugin address
1122 * @param plugin_addr_len length of the plugin address
1123 * @param session_id session id, can be 0
1126 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
1127 const struct GNUNET_PeerIdentity *peer,
1128 const char *plugin_name,
1129 const void *plugin_addr,
1130 size_t plugin_addr_len,
1131 uint32_t session_id)
1133 struct ATS_Address *ea;
1134 struct DestroyContext dc;
1135 if (GNUNET_NO == handle->running)
1138 /* Get existing address */
1139 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1140 plugin_addr_len, session_id);
1143 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
1144 GNUNET_i2s (peer), plugin_name, session_id);
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1149 "Received `%s' for peer `%s' address %p session %u\n",
1151 GNUNET_i2s (peer), ea, session_id);
1153 GNUNET_break (0 < strlen (plugin_name));
1155 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
1157 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
1158 &destroy_by_session_id, &dc);
1159 free_address (dc.aa);
1164 * Notification about active use of an address.
1165 * in_use == GNUNET_YES:
1166 * This address is used to maintain an active connection with a peer.
1167 * in_use == GNUNET_NO:
1168 * This address is no longer used to maintain an active connection with a peer.
1170 * Note: can only be called with in_use == GNUNET_NO if called with GNUNET_YES
1173 * @param handle the address handle to use
1175 * @param plugin_name transport plugin name
1176 * @param plugin_addr plugin address
1177 * @param plugin_addr_len length of the plugin address
1178 * @param session_id session id, can be 0
1179 * @param in_use GNUNET_YES if GNUNET_NO
1180 * @return GNUNET_SYSERR on failure (address unknown ...)
1183 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
1184 const struct GNUNET_PeerIdentity *peer,
1185 const char *plugin_name,
1186 const void *plugin_addr,
1187 size_t plugin_addr_len,
1188 uint32_t session_id,
1191 struct ATS_Address *ea;
1192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1193 "Received `%s' for peer `%s'\n",
1197 if (GNUNET_NO == handle->running)
1198 return GNUNET_SYSERR;
1200 ea = find_exact_address (handle, peer, plugin_name,
1201 plugin_addr, plugin_addr_len, session_id);
1204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1205 "Trying to set unknown address `%s', %s %u %s \n",
1207 plugin_name, session_id,
1208 (GNUNET_NO == in_use) ? "NO" : "YES");
1210 return GNUNET_SYSERR;
1212 if (ea->used == in_use)
1215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1216 "Address in use called multiple times for peer `%s': %s -> %s \n",
1218 (GNUNET_NO == ea->used) ? "NO" : "YES",
1219 (GNUNET_NO == in_use) ? "NO" : "YES");
1220 return GNUNET_SYSERR;
1223 /* Tell solver about update */
1225 handle->s_address_update_inuse (handle->solver, ea, ea->used);
1231 * Cancel address suggestions for a peer
1233 * @param handle the address handle
1234 * @param peer the peer id
1237 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
1238 const struct GNUNET_PeerIdentity *peer)
1240 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1243 "Received request: `%s' for peer %s\n", "request_address_cancel", GNUNET_i2s (peer));
1247 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
1254 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1255 "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
1258 handle->s_get_stop (handle->solver, peer);
1259 GAS_addresses_handle_backoff_reset (handle, peer);
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1261 "Removed request pending for peer `%s\n", GNUNET_i2s (peer));
1262 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1268 * Request address suggestions for a peer
1270 * @param handle the address handle
1271 * @param peer the peer id
1274 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
1275 const struct GNUNET_PeerIdentity *peer)
1277 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1278 struct ATS_Address *aa;
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1281 "Received `%s' for peer `%s'\n",
1285 if (GNUNET_NO == handle->running)
1289 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
1290 break; /* already suggesting */
1295 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
1297 GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
1300 /* Get prefered address from solver */
1301 aa = (struct ATS_Address *) handle->s_get (handle->solver, peer);
1304 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1305 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
1309 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1310 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
1312 GAS_scheduling_transmit_address_suggestion (peer,
1314 aa->addr, aa->addr_len,
1316 aa->atsi, aa->atsi_count,
1317 aa->assigned_bw_out,
1318 aa->assigned_bw_in);
1320 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
1321 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "Address %p ready for suggestion, block interval now %llu \n",
1325 aa, aa->block_interval);
1330 * Iterator to reset address blocking
1332 * @param cls not used
1333 * @param key the peer
1334 * @param value the address to reset
1335 * @return GNUNET_OK to continue
1338 reset_address_it (void *cls, const struct GNUNET_HashCode *key, void *value)
1340 struct ATS_Address *aa = value;
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1343 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1344 GNUNET_i2s (&aa->peer), aa, aa->block_interval);
1346 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1347 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1353 * Reset suggestion backoff for a peer
1355 * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1356 * reset using this function
1358 * @param handle the address handle
1359 * @param peer the peer id
1362 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1363 const struct GNUNET_PeerIdentity *peer)
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1366 "Received `%s' for peer `%s'\n",
1370 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
1378 * The preference changed for a peer
1380 * @param cls the address handle
1381 * @param peer the peer
1382 * @param kind the ATS kind
1383 * @param pref_rel the new relative preference value
1386 normalized_preference_changed_cb (void *cls,
1387 const struct GNUNET_PeerIdentity *peer,
1388 enum GNUNET_ATS_PreferenceKind kind,
1391 GNUNET_assert (NULL != cls);
1392 struct GAS_Addresses_Handle *handle = cls;
1393 /* Tell solver about update */
1394 handle->s_pref (handle->solver, peer, kind, pref_rel);
1399 * The relative value for a property changed
1401 * @param cls the address handle
1402 * @param address the peer
1403 * @param type the ATS type
1404 * @param prop_rel the new relative preference value
1407 normalized_property_changed_cb (void *cls,
1408 struct ATS_Address *address,
1412 struct GAS_Addresses_Handle *ah = (struct GAS_Addresses_Handle *) cls;
1413 GNUNET_assert (NULL != ah);
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1416 "Normalized property %s for peer `%s' changed to %.3f \n",
1417 GNUNET_ATS_print_property_type (type),
1418 GNUNET_i2s (&address->peer),
1421 ah->s_address_update_property (ah->solver,
1430 * Function allowing the solver to obtain normalized preference
1431 * values from solver
1434 * @param id the peer to return the normalized properties for
1435 * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
1438 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
1440 return GAS_normalization_get_preferences (id);
1444 * Function allowing the solver to obtain normalized property
1445 * values for an address from solver
1448 * @param address the address
1449 * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
1452 get_property_cb (void *cls, const struct ATS_Address *address)
1454 return GAS_normalization_get_properties ((struct ATS_Address *) address);
1458 * Change the preference for a peer
1460 * @param handle the address handle
1461 * @param client the client sending this request
1462 * @param peer the peer id
1463 * @param kind the preference kind to change
1464 * @param score_abs the new preference score
1467 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
1469 const struct GNUNET_PeerIdentity *peer,
1470 enum GNUNET_ATS_PreferenceKind kind,
1473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1474 "Received `%s' for peer `%s' for client %p\n",
1475 "CHANGE PREFERENCE",
1476 GNUNET_i2s (peer), client);
1478 if (GNUNET_NO == handle->running)
1481 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->addresses,
1484 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1485 "Received `%s' for unknown peer `%s' from client %p\n",
1486 "CHANGE PREFERENCE",
1487 GNUNET_i2s (peer), client);
1491 handle->s_bulk_start (handle->solver);
1492 /* Tell normalization about change, normalization will call callback if preference changed */
1493 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1494 handle->s_bulk_stop (handle->solver);
1499 * Change the preference for a peer
1501 * @param handle the address handle
1502 * @param application the client sending this request
1503 * @param peer the peer id
1504 * @param scope the time interval for this feedback: [now - scope .. now]
1505 * @param kind the preference kind to change
1506 * @param score_abs the new preference score
1509 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1511 const struct GNUNET_PeerIdentity *peer,
1512 const struct GNUNET_TIME_Relative scope,
1513 enum GNUNET_ATS_PreferenceKind kind,
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "Received `%s' for peer `%s' for client %p\n",
1518 "PREFERENCE FEEDBACK",
1519 GNUNET_i2s (peer), application);
1521 if (GNUNET_NO == handle->running)
1524 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->addresses,
1527 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1528 "Received `%s' for unknown peer `%s' from client %p\n",
1529 "PREFERENCE FEEDBACK",
1530 GNUNET_i2s (peer), application);
1534 handle->s_feedback (handle->solver, application, peer, scope, kind, score_abs);
1539 * Load quotas for networks from configuration
1541 * @param cfg configuration handle
1542 * @param out_dest where to write outbound quotas
1543 * @param in_dest where to write inbound quotas
1544 * @param dest_length length of inbound and outbound arrays
1545 * @return number of networks loaded
1548 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1550 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1551 char * entry_in = NULL;
1552 char * entry_out = NULL;
1553 char * quota_out_str;
1554 char * quota_in_str;
1558 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1562 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1563 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1566 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
1569 if (0 == strcmp(quota_out_str, GNUNET_ATS_MaxBandwidthString))
1571 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1574 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
1576 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out, &out_dest[c])))
1579 if (GNUNET_NO == res)
1581 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1582 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1583 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1587 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Outbound quota configure for network `%s' is %llu\n"),
1588 network_str[c], out_dest[c]);
1590 GNUNET_free (quota_out_str);
1594 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1595 network_str[c], GNUNET_ATS_DefaultBandwidth);
1596 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1600 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
1603 if (0 == strcmp(quota_in_str, GNUNET_ATS_MaxBandwidthString))
1605 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1608 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1610 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in, &in_dest[c])))
1613 if (GNUNET_NO == res)
1615 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1616 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1617 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1621 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Inbound quota configured for network `%s' is %llu\n"),
1622 network_str[c], in_dest[c]);
1624 GNUNET_free (quota_in_str);
1628 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1629 network_str[c], GNUNET_ATS_DefaultBandwidth);
1630 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c], in_dest[c], out_dest[c]);
1633 GNUNET_free (entry_out);
1634 GNUNET_free (entry_in);
1636 return GNUNET_ATS_NetworkTypeCount;
1641 * Callback for solver to notify about assignment changes
1643 * @param cls the GAS_Addresses_Handle
1644 * @param address the address with changes
1647 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1649 struct GAS_Addresses_Handle *handle = cls;
1650 struct GAS_Addresses_Suggestion_Requests *cur;
1652 GNUNET_assert (handle != NULL);
1653 GNUNET_assert (address != NULL);
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bandwidth assignment changed for peer %s \n", GNUNET_i2s(&address->peer));
1657 /* Notify performance clients about changes to address */
1658 GAS_performance_notify_all_clients (&address->peer,
1660 address->addr, address->addr_len,
1661 address->session_id,
1662 address->atsi, address->atsi_count,
1663 address->assigned_bw_out,
1664 address->assigned_bw_in);
1665 cur = handle->r_head;
1668 if (0 == memcmp (&address->peer, &cur->id, sizeof (cur->id)))
1669 break; /* we have an address request pending*/
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1675 "Nobody is interested in peer `%s' :(\n",GNUNET_i2s (&address->peer));
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1680 "Sending bandwidth update for peer `%s'\n",GNUNET_i2s (&address->peer));
1682 /* *Notify scheduling clients about suggestion */
1683 GAS_scheduling_transmit_address_suggestion (&address->peer,
1685 address->addr, address->addr_len,
1686 address->session_id,
1687 address->atsi, address->atsi_count,
1688 address->assigned_bw_out,
1689 address->assigned_bw_in);
1694 * Initialize address subsystem. The addresses subsystem manages the addresses
1695 * known and current performance information. It has a solver component
1696 * responsible for the resource allocation. It tells the solver about changes
1697 * and receives updates when the solver changes the resource allocation.
1699 * @param cfg configuration to use
1700 * @param stats the statistics handle to use
1701 * @return an address handle
1703 struct GAS_Addresses_Handle *
1704 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1705 const struct GNUNET_STATISTICS_Handle *stats)
1707 struct GAS_Addresses_Handle *ah;
1708 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1709 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1710 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1715 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1716 ah->running = GNUNET_NO;
1718 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1719 /* Initialize the addresses database */
1720 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1721 GNUNET_assert (NULL != ah->addresses);
1723 /* Figure out configured solution method */
1724 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1726 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No resource assignment method configured, using proportional approach\n");
1727 ah->ats_mode = MODE_PROPORTIONAL;
1731 for (c = 0; c < strlen (mode_str); c++)
1732 mode_str[c] = toupper (mode_str[c]);
1733 if (0 == strcmp (mode_str, "PROPORTIONAL"))
1735 ah->ats_mode = MODE_PROPORTIONAL;
1737 else if (0 == strcmp (mode_str, "MLP"))
1739 ah->ats_mode = MODE_MLP;
1741 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not available, please install \n", mode_str);
1742 ah->ats_mode = MODE_PROPORTIONAL;
1745 else if (0 == strcmp (mode_str, "RIL"))
1747 ah->ats_mode = MODE_RIL;
1751 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid resource assignment method `%s' configured, using proportional approach\n", mode_str);
1752 ah->ats_mode = MODE_PROPORTIONAL;
1754 GNUNET_free (mode_str);
1756 /* Start configured solution method */
1757 switch (ah->ats_mode)
1760 /* Init the MLP solver with default values */
1762 ah->ats_mode = MODE_MLP;
1763 ah->s_init = &GAS_mlp_init;
1764 ah->s_add = &GAS_mlp_address_add;
1765 ah->s_address_update_property = &GAS_mlp_address_property_changed;
1766 ah->s_address_update_session = &GAS_mlp_address_session_changed;
1767 ah->s_address_update_inuse = &GAS_mlp_address_inuse_changed;
1768 ah->s_address_update_network = &GAS_mlp_address_change_network;
1769 ah->s_get = &GAS_mlp_get_preferred_address;
1770 ah->s_get_stop = &GAS_mlp_stop_get_preferred_address;
1771 ah->s_pref = &GAS_mlp_address_change_preference;
1772 ah->s_feedback = &GAS_mlp_address_preference_feedback;
1773 ah->s_del = &GAS_mlp_address_delete;
1774 ah->s_bulk_start = &GAS_mlp_bulk_start;
1775 ah->s_bulk_stop = &GAS_mlp_bulk_stop;
1776 ah->s_done = &GAS_mlp_done;
1782 case MODE_PROPORTIONAL:
1783 /* Init the proportional solver with default values */
1784 ah->ats_mode = MODE_PROPORTIONAL;
1785 ah->s_init = &GAS_proportional_init;
1786 ah->s_add = &GAS_proportional_address_add;
1787 ah->s_address_update_property = &GAS_proportional_address_property_changed;
1788 ah->s_address_update_session = &GAS_proportional_address_session_changed;
1789 ah->s_address_update_inuse = &GAS_proportional_address_inuse_changed;
1790 ah->s_address_update_network = &GAS_proportional_address_change_network;
1791 ah->s_get = &GAS_proportional_get_preferred_address;
1792 ah->s_get_stop = &GAS_proportional_stop_get_preferred_address;
1793 ah->s_pref = &GAS_proportional_address_change_preference;
1794 ah->s_feedback = &GAS_proportional_address_preference_feedback;
1795 ah->s_del = &GAS_proportional_address_delete;
1796 ah->s_bulk_start = &GAS_proportional_bulk_start;
1797 ah->s_bulk_stop = &GAS_proportional_bulk_stop;
1798 ah->s_done = &GAS_proportional_done;
1799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "PROPORTIONAL");
1802 /* Init the ril solver with default values */
1803 ah->s_init = &GAS_ril_init;
1804 ah->s_add = &GAS_ril_address_add;
1805 ah->s_address_update_property = &GAS_ril_address_property_changed;
1806 ah->s_address_update_session = &GAS_ril_address_session_changed;
1807 ah->s_address_update_inuse = &GAS_ril_address_inuse_changed;
1808 ah->s_address_update_network = &GAS_ril_address_change_network;
1809 ah->s_get = &GAS_ril_get_preferred_address;
1810 ah->s_get_stop = &GAS_ril_stop_get_preferred_address;
1811 ah->s_pref = &GAS_ril_address_change_preference;
1812 ah->s_feedback = &GAS_ril_address_preference_feedback;
1813 ah->s_del = &GAS_ril_address_delete;
1814 ah->s_bulk_start = &GAS_ril_bulk_start;
1815 ah->s_bulk_stop = &GAS_ril_bulk_stop;
1816 ah->s_done = &GAS_ril_done;
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "RIL");
1824 GNUNET_assert (NULL != ah->s_init);
1825 GNUNET_assert (NULL != ah->s_add);
1826 GNUNET_assert (NULL != ah->s_address_update_inuse);
1827 GNUNET_assert (NULL != ah->s_address_update_property);
1828 GNUNET_assert (NULL != ah->s_address_update_session);
1829 GNUNET_assert (NULL != ah->s_address_update_network);
1830 GNUNET_assert (NULL != ah->s_get);
1831 GNUNET_assert (NULL != ah->s_get_stop);
1832 GNUNET_assert (NULL != ah->s_pref);
1833 GNUNET_assert (NULL != ah->s_feedback);
1834 GNUNET_assert (NULL != ah->s_del);
1835 GNUNET_assert (NULL != ah->s_done);
1836 GNUNET_assert (NULL != ah->s_bulk_start);
1837 GNUNET_assert (NULL != ah->s_bulk_stop);
1839 GAS_normalization_start (&normalized_preference_changed_cb, ah,
1840 &normalized_property_changed_cb, ah);
1841 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1843 ah->solver = ah->s_init (cfg, stats, ah->addresses,
1844 quotas, quotas_in, quotas_out, quota_count,
1845 &bandwidth_changed_cb, ah,
1846 &get_preferences_cb, NULL,
1847 &get_property_cb, NULL);
1848 if (NULL == ah->solver)
1850 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver!\n"));
1855 /* up and running */
1856 ah->running = GNUNET_YES;
1862 * Destroy all addresses iterator
1865 * @param key peer identity (unused)
1866 * @param value the 'struct ATS_Address' to free
1867 * @return GNUNET_OK (continue to iterate)
1870 destroy_all_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1872 struct GAS_Addresses_Handle *handle = cls;
1873 struct ATS_Address *aa = value;
1876 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (handle->addresses, key, value));
1878 handle->s_del (handle->solver, aa, GNUNET_NO);
1887 * Remove all addresses
1889 * @param handle the address handle to use
1892 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1894 if (GNUNET_NO == handle->running)
1897 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1898 "Destroying all addresses\n");
1899 if (handle->addresses != NULL)
1900 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &destroy_all_address_it, handle);
1905 * Shutdown address subsystem.
1907 * @param handle the address handle to shutdown
1910 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1912 struct GAS_Addresses_Suggestion_Requests *cur;
1914 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1915 "Shutting down addresses\n");
1916 GNUNET_assert (NULL != handle);
1917 GAS_addresses_destroy_all (handle);
1918 handle->running = GNUNET_NO;
1919 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1920 handle->addresses = NULL;
1921 while (NULL != (cur = handle->r_head))
1923 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1926 handle->s_done (handle->solver);
1927 GNUNET_free (handle);
1928 /* Stop configured solution method */
1929 GAS_normalization_stop ();
1933 struct PeerIteratorContext
1935 GNUNET_ATS_Peer_Iterator it;
1937 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1942 * Iterator to iterate over all peers
1944 * @param cls a PeerIteratorContext
1945 * @param key the peer id
1946 * @param value the ATS_address
1947 * @return GNUNET_OK to continue
1951 const struct GNUNET_HashCode * key,
1954 struct PeerIteratorContext *ip_ctx = cls;
1955 struct GNUNET_PeerIdentity tmp;
1957 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1959 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1960 tmp.hashPubKey = (*key);
1961 ip_ctx->it (ip_ctx->it_cls, &tmp);
1969 * Return information all peers currently known to ATS
1971 * @param handle the address handle to use
1972 * @param p_it the iterator to call for every peer
1973 * @param p_it_cls the closure for the iterator
1976 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
1977 GNUNET_ATS_Peer_Iterator p_it,
1980 struct PeerIteratorContext ip_ctx;
1985 GNUNET_assert (NULL != handle->addresses);
1987 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1991 ip_ctx.it_cls = p_it_cls;
1992 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1993 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1994 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1996 p_it (p_it_cls, NULL);
2000 struct PeerInfoIteratorContext
2002 GNUNET_ATS_PeerInfo_Iterator it;
2008 * Iterator to iterate over a peer's addresses
2010 * @param cls a PeerInfoIteratorContext
2011 * @param key the peer id
2012 * @param value the ATS_address
2013 * @return GNUNET_OK to continue
2016 peerinfo_it (void *cls,
2017 const struct GNUNET_HashCode * key,
2020 struct PeerInfoIteratorContext *pi_ctx = cls;
2021 struct ATS_Address *addr = (struct ATS_Address *) value;
2023 if (NULL != pi_ctx->it)
2025 pi_ctx->it (pi_ctx->it_cls,
2028 addr->addr, addr->addr_len,
2030 addr->atsi, addr->atsi_count,
2031 addr->assigned_bw_out,
2032 addr->assigned_bw_in);
2039 * Return information all peers currently known to ATS
2041 * @param handle the address handle to use
2042 * @param peer the respective peer
2043 * @param pi_it the iterator to call for every peer
2044 * @param pi_it_cls the closure for the iterator
2047 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
2048 const struct GNUNET_PeerIdentity *peer,
2049 GNUNET_ATS_PeerInfo_Iterator pi_it,
2052 struct PeerInfoIteratorContext pi_ctx;
2053 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
2054 GNUNET_assert (NULL != peer);
2055 GNUNET_assert (NULL != handle->addresses);
2057 return; /* does not make sense without callback */
2059 zero_bw = GNUNET_BANDWIDTH_value_init (0);
2061 pi_ctx.it_cls = pi_it_cls;
2063 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
2066 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
2071 /* end of gnunet-service-ats_addresses.c */