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_MultiPeerMap *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;
581 * @param cls a CompareAddressContext containin the source address
583 * @param value the address to compare with
584 * @return #GNUNET_YES to continue, #GNUNET_NO if address is founce
587 compare_address_it (void *cls,
588 const struct GNUNET_PeerIdentity *key,
591 struct CompareAddressContext *cac = cls;
592 struct ATS_Address *aa = value;
594 /* Find an matching exact address:
597 * aa->addr_len == cac->search->addr_len
598 * aa->plugin == cac->search->plugin
599 * aa->addr == cac->search->addr
600 * aa->session == cac->search->session
602 * return as exact address
604 if ((aa->addr_len == cac->search->addr_len)
605 && (0 == strcmp (aa->plugin, cac->search->plugin)))
607 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
608 && (aa->session_id == cac->search->session_id))
609 cac->exact_address = aa;
612 /* Find an matching base address:
616 * aa->session_id == 0
619 * aa->addr_len == cac->search->addr_len
620 * aa->plugin == cac->search->plugin
621 * aa->addr == cac->search->addr
623 * return as base address
625 if ((aa->addr_len == cac->search->addr_len)
626 && (0 == strcmp (aa->plugin, cac->search->plugin)))
628 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
629 && (aa->session_id == 0))
630 cac->base_address = aa;
633 /* Find an matching exact address based on session:
637 * cac->search->addr_len == 0
640 * aa->plugin == cac->search->plugin
641 * aa->session_id == cac->search->session_id
643 * return as exact address
645 if (0 == cac->search->addr_len)
647 if ((0 == strcmp (aa->plugin, cac->search->plugin))
648 && (aa->session_id == cac->search->session_id))
649 cac->exact_address = aa;
652 if (cac->exact_address == NULL )
653 return GNUNET_YES; /* Continue iteration to find exact address */
655 return GNUNET_NO; /* Stop iteration since we have an exact address */
659 * Find an existing equivalent address record.
660 * Compares by peer identity and network address OR by session ID
661 * (one of the two must match).
663 * @param handle the address handle
664 * @param peer peer to lookup addresses for
665 * @param addr existing address record
666 * @return existing address record, NULL for none
669 find_equivalent_address (struct GAS_Addresses_Handle *handle,
670 const struct GNUNET_PeerIdentity *peer, const struct ATS_Address *addr)
672 struct CompareAddressContext cac;
674 cac.exact_address = NULL;
675 cac.base_address = NULL;
677 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
679 &compare_address_it, &cac);
681 if (cac.exact_address == NULL)
682 return cac.base_address;
683 return cac.exact_address;
688 * Find the exact address
690 * @param handle the address handle to use
692 * @param plugin_name transport plugin name
693 * @param plugin_addr plugin address
694 * @param plugin_addr_len length of the plugin address
695 * @param session_id session id, can be 0
696 * @return an ATS_address or NULL
699 static struct ATS_Address *
700 find_exact_address (struct GAS_Addresses_Handle *handle,
701 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
702 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
704 struct ATS_Address *aa;
705 struct ATS_Address *ea;
707 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
710 /* Get existing address or address with session == 0 */
711 ea = find_equivalent_address (handle, peer, aa);
715 else if (ea->session_id != session_id)
721 * Extract an ATS performance info from an address
723 * @param address the address
724 * @param type the type to extract in HBO
725 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
728 get_performance_info (struct ATS_Address *address, uint32_t type)
731 GNUNET_assert(NULL != address);
733 if ((NULL == address->atsi) || (0 == address->atsi_count))
734 return GNUNET_ATS_VALUE_UNDEFINED;
736 for (c1 = 0; c1 < address->atsi_count; c1++)
738 if (ntohl (address->atsi[c1].type) == type)
739 return ntohl (address->atsi[c1].value);
741 return GNUNET_ATS_VALUE_UNDEFINED;
745 * Add a new address for a peer.
747 * @param handle the address handle to use
749 * @param plugin_name transport plugin name
750 * @param plugin_addr plugin address
751 * @param plugin_addr_len length of the plugin address
752 * @param session_id session id, can be 0
753 * @param atsi performance information for this address
754 * @param atsi_count number of performance information contained
757 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
758 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
759 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
760 const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
762 struct ATS_Address *new_address;
763 struct ATS_Address *existing_address;
764 struct GNUNET_ATS_Information *atsi_delta;
765 uint32_t atsi_delta_count;
767 uint32_t previous_session;
770 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
771 "ADDRESS ADD", GNUNET_i2s (peer));
773 if (GNUNET_NO == handle->running)
776 GNUNET_assert(NULL != handle->addresses);
778 new_address = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
781 disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
783 GNUNET_free_non_null(atsi_delta);
784 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
785 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
786 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
788 /* Get existing address or address with session == 0 */
789 existing_address = find_equivalent_address (handle, peer, new_address);
790 if (existing_address == NULL )
792 /* Add a new address */
794 GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (handle->addresses,
797 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
799 GNUNET_STATISTICS_set (handle->stat, "# addresses",
800 GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
802 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
803 "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
804 new_address, GNUNET_i2s (peer), plugin_addr_len, session_id,
805 GNUNET_ATS_print_network_type (addr_net));
807 /* Tell solver about new address */
808 handle->s_add (handle->solver, new_address, addr_net);
810 handle->s_bulk_start (handle->solver);
811 GAS_normalization_normalize_property (handle->addresses, new_address, atsi,
813 handle->s_bulk_stop (handle->solver);
815 /* Notify performance clients about new address */
816 GAS_performance_notify_all_clients (&new_address->peer, new_address->plugin,
817 new_address->addr, new_address->addr_len, new_address->session_id,
818 new_address->atsi, new_address->atsi_count,
819 new_address->assigned_bw_out, new_address->assigned_bw_in);
823 /* We have an existing address we can use, clean up new */
824 GNUNET_free(new_address->plugin);
825 GNUNET_free_non_null(new_address->atsi);
826 GNUNET_free(new_address);
829 if (0 != existing_address->session_id)
831 /* Should not happen */
836 addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
837 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
838 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
840 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
841 "Found existing address for peer `%s' %p with new session %u in network %s\n",
842 GNUNET_i2s (peer), existing_address, session_id,
843 GNUNET_ATS_print_network_type (addr_net));
844 /* We have an address without an session, update this address */
846 atsi_delta_count = 0;
848 == disassemble_ats_information (existing_address, atsi, atsi_count,
849 &atsi_delta, &atsi_delta_count))
851 /* Notify performance clients about properties */
852 GAS_performance_notify_all_clients (&existing_address->peer,
853 existing_address->plugin, existing_address->addr,
854 existing_address->addr_len, existing_address->session_id,
855 existing_address->atsi, existing_address->atsi_count,
856 existing_address->assigned_bw_out, existing_address->assigned_bw_in);
858 for (c1 = 0; c1 < atsi_delta_count; c1++)
860 if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
861 && (addr_net != ntohl (atsi_delta[c1].value)))
863 /* Network type changed */
864 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
865 "Address for peer `%s' %p changed from network %s to %s\n",
866 GNUNET_i2s (peer), existing_address,
867 GNUNET_ATS_print_network_type (addr_net),
868 GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
869 handle->s_address_update_network (handle->solver, existing_address,
870 ntohl (atsi_delta[c1].value),
871 get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
872 addr_net = get_performance_info (existing_address,
873 GNUNET_ATS_NETWORK_TYPE);
876 /* Notify solver about update with atsi information and session */
877 handle->s_bulk_start (handle->solver);
878 GAS_normalization_normalize_property (handle->addresses, existing_address,
880 handle->s_bulk_stop (handle->solver);
882 GNUNET_free_non_null(atsi_delta);
884 /* Notify solver about new session */
885 if (existing_address->session_id == session_id)
886 return; /* possible, can both be 0 since address is revalidated */
888 previous_session = existing_address->session_id;
889 existing_address->session_id = session_id;
890 handle->s_address_update_session (handle->solver, existing_address,
891 previous_session, session_id);
893 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
894 "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
895 GNUNET_i2s (peer), existing_address, existing_address->addr_len,
896 session_id, GNUNET_ATS_print_network_type (addr_net));
900 * Update an address with a session or performance information for a peer.
902 * If an address was added without a session it will be updated with the
905 * @param handle the address handle to use
907 * @param plugin_name transport plugin name
908 * @param plugin_addr plugin address
909 * @param plugin_addr_len length of the plugin address
910 * @param session_id session id, can be 0
911 * @param atsi performance information for this address
912 * @param atsi_count number of performance information contained
915 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
916 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
917 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
918 const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
920 struct ATS_Address *aa;
921 struct GNUNET_ATS_Information *atsi_delta;
922 uint32_t atsi_delta_count;
923 uint32_t prev_session;
926 if (GNUNET_NO == handle->running)
929 GNUNET_assert(NULL != handle->addresses);
931 /* Get existing address */
932 aa = find_exact_address (handle, peer, plugin_name, plugin_addr,
933 plugin_addr_len, session_id);
936 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n", */
937 /* GNUNET_i2s (peer), plugin_name, session_id); */
938 /* GNUNET_break (0); */
942 if (NULL == aa->solver_information)
944 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
945 "Tried to update unknown address for peer `%s' `%s' session id %u\n",
946 GNUNET_i2s (peer), plugin_name, session_id);
950 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s' address \n",
951 "ADDRESS UPDATE", GNUNET_i2s (peer), aa);
954 if (session_id != aa->session_id)
956 /* Session changed */
957 prev_session = aa->session_id;
958 aa->session_id = session_id;
959 handle->s_address_update_session (handle->solver, aa, prev_session,
964 atsi_delta_count = 0;
966 == disassemble_ats_information (aa, atsi, atsi_count, &atsi_delta,
969 /* ATS properties changed */
970 for (c1 = 0; c1 < atsi_delta_count; c1++)
972 if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
974 /* Network type changed */
975 handle->s_address_update_network (handle->solver, aa,
976 ntohl (atsi_delta[c1].value),
977 get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
981 /* Notify performance clients about updated address */
982 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr,
983 aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
984 aa->assigned_bw_out, aa->assigned_bw_in);
986 handle->s_bulk_start (handle->solver);
987 GAS_normalization_normalize_property (handle->addresses, aa, atsi,
989 handle->s_bulk_stop (handle->solver);
991 GNUNET_free_non_null(atsi_delta);
994 struct DestroyContext
996 struct ATS_Address *aa;
998 struct GAS_Addresses_Handle *handle;
1001 * GNUNET_NO : full address
1002 * GNUNET_YES : just session
1010 * If session != 0, just the session is deleted, the address itself still exists
1011 * If session == 0, remove full address
1012 * If session == 0 and addrlen == 0, destroy inbound address
1016 * @param value the 'struct ATS_Address'
1017 * @return GNUNET_OK (continue to iterate)
1020 destroy_by_session_id (void *cls,
1021 const struct GNUNET_PeerIdentity *key,
1024 struct DestroyContext *dc = cls;
1025 struct GAS_Addresses_Handle *handle = dc->handle;
1026 const struct ATS_Address *des = dc->aa;
1027 struct ATS_Address *aa = value;
1030 0 == memcmp (&aa->peer, &des->peer, sizeof(struct GNUNET_PeerIdentity)));
1032 if (des->session_id == 0)
1034 /* Session == 0, remove full address */
1035 if ((0 == strcmp (des->plugin, aa->plugin))
1036 && (aa->addr_len == des->addr_len)
1037 && (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
1040 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1041 "Deleting full address for peer `%s' session %u %p\n",
1042 GNUNET_i2s (&aa->peer), aa->session_id, aa);
1044 /* Notify solver about deletion */
1046 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1049 handle->s_del (handle->solver, aa, GNUNET_NO);
1051 dc->result = GNUNET_NO;
1052 return GNUNET_OK; /* Continue iteration */
1057 /* Session != 0, just remove session */
1058 if (aa->session_id != des->session_id)
1059 return GNUNET_OK; /* irrelevant */
1061 if ((aa->session_id != 0) && (0 != strcmp (des->plugin, aa->plugin)))
1063 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1064 "Different plugins during removal: `%s' vs `%s' \n", des->plugin,
1070 if (aa->addr_len == 0)
1072 /* Inbound connection died, delete full address */
1073 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1074 "Deleting inbound address for peer `%s': `%s' session %u\n",
1075 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
1077 /* Notify solver about deletion */
1079 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1081 handle->s_del (handle->solver, aa, GNUNET_NO);
1083 dc->result = GNUNET_NO;
1084 return GNUNET_OK; /* Continue iteration */
1089 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1090 "Deleting session for peer `%s': `%s' %u\n", GNUNET_i2s (&aa->peer),
1091 aa->plugin, aa->session_id);
1092 /* Notify solver to delete session */
1093 handle->s_del (handle->solver, aa, GNUNET_YES);
1095 aa->active = GNUNET_NO;
1104 * Remove an address or just a session for a peer.
1106 * @param handle the address handle to use
1108 * @param plugin_name transport plugin name
1109 * @param plugin_addr plugin address
1110 * @param plugin_addr_len length of the plugin address
1111 * @param session_id session id, can be 0
1114 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
1115 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1116 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
1118 struct ATS_Address *ea;
1119 struct DestroyContext dc;
1120 if (GNUNET_NO == handle->running)
1123 /* Get existing address */
1124 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1125 plugin_addr_len, session_id);
1128 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1129 "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
1130 GNUNET_i2s (peer), plugin_name, session_id);
1134 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1135 "Received `%s' for peer `%s' address %p session %u\n", "ADDRESS DESTROY",
1136 GNUNET_i2s (peer), ea, session_id);
1138 GNUNET_break(0 < strlen (plugin_name));
1140 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
1143 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1145 &destroy_by_session_id, &dc);
1146 GNUNET_STATISTICS_set (handle->stat, "# addresses",
1147 GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
1148 free_address (dc.aa);
1152 * Notification about active use of an address.
1153 * in_use == GNUNET_YES:
1154 * This address is used to maintain an active connection with a peer.
1155 * in_use == GNUNET_NO:
1156 * This address is no longer used to maintain an active connection with a peer.
1158 * Note: can only be called with in_use == GNUNET_NO if called with GNUNET_YES
1161 * @param handle the address handle to use
1163 * @param plugin_name transport plugin name
1164 * @param plugin_addr plugin address
1165 * @param plugin_addr_len length of the plugin address
1166 * @param session_id session id, can be 0
1167 * @param in_use GNUNET_YES if GNUNET_NO
1168 * @return GNUNET_SYSERR on failure (address unknown ...)
1171 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
1172 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1173 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
1176 struct ATS_Address *ea;
1177 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1178 "ADDRESS IN USE", GNUNET_i2s (peer));
1180 if (GNUNET_NO == handle->running)
1181 return GNUNET_SYSERR;
1183 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1184 plugin_addr_len, session_id);
1187 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1188 "Trying to set unknown address `%s' `%s' `%u' to %s \n",
1189 GNUNET_i2s (peer), plugin_name, session_id,
1190 (GNUNET_NO == in_use) ? "NO" : "YES");
1192 return GNUNET_SYSERR;
1194 if (ea->used == in_use)
1197 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1198 "Address in use called multiple times for peer `%s': %s -> %s \n",
1199 GNUNET_i2s (peer), (GNUNET_NO == ea->used) ? "NO" : "YES",
1200 (GNUNET_NO == in_use) ? "NO" : "YES");
1201 return GNUNET_SYSERR;
1204 /* Tell solver about update */
1206 handle->s_address_update_inuse (handle->solver, ea, ea->used);
1211 * Cancel address suggestions for a peer
1213 * @param handle the address handle
1214 * @param peer the peer id
1217 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
1218 const struct GNUNET_PeerIdentity *peer)
1220 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1222 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received request: `%s' for peer %s\n",
1223 "request_address_cancel", GNUNET_i2s (peer));
1227 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1234 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1235 "No address requests pending for peer `%s', cannot remove!\n",
1239 handle->s_get_stop (handle->solver, peer);
1240 GAS_addresses_handle_backoff_reset (handle, peer);
1241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Removed request pending for peer `%s\n",
1243 GNUNET_CONTAINER_DLL_remove(handle->r_head, handle->r_tail, cur);
1249 * Request address suggestions for a peer
1251 * @param handle the address handle
1252 * @param peer the peer id
1255 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
1256 const struct GNUNET_PeerIdentity *peer)
1258 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1259 struct ATS_Address *aa;
1261 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received `%s' for peer `%s'\n",
1262 "REQUEST ADDRESS", GNUNET_i2s (peer));
1264 if (GNUNET_NO == handle->running)
1268 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1269 break; /* already suggesting */
1274 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
1276 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1277 "Adding new address suggestion request for `%s'\n",
1279 GNUNET_CONTAINER_DLL_insert(handle->r_head, handle->r_tail, cur);
1283 * Debuging information about addresses
1285 * GNUNET_CONTAINER_multihashmap_get_multiple(handle->addresses,
1286 * &peer->hashPubKey, &addrinfo_it, (void *) peer);
1289 /* Get prefered address from solver */
1290 aa = (struct ATS_Address *) handle->s_get (handle->solver, peer);
1293 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1298 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1299 aa, GNUNET_i2s (peer));
1301 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
1302 aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
1303 aa->assigned_bw_out, aa->assigned_bw_in);
1305 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1306 ATS_BLOCKING_DELTA);
1307 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1308 aa->block_interval);
1310 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1311 "Address %p ready for suggestion, block interval now %llu \n", aa,
1312 aa->block_interval);
1316 * Iterator to reset address blocking
1318 * @param cls not used
1319 * @param key the peer
1320 * @param value the address to reset
1321 * @return #GNUNET_OK to continue
1324 reset_address_it (void *cls,
1325 const struct GNUNET_PeerIdentity *key,
1328 struct ATS_Address *aa = value;
1330 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1331 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1332 GNUNET_i2s (&aa->peer),
1334 aa->block_interval);
1335 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1336 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1342 * Reset suggestion backoff for a peer
1344 * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1345 * reset using this function
1347 * @param handle the address handle
1348 * @param peer the peer id
1351 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1352 const struct GNUNET_PeerIdentity *peer)
1354 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1355 "RESET BACKOFF", GNUNET_i2s (peer));
1358 GNUNET_SYSERR != GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1360 &reset_address_it, NULL));
1365 * The preference changed for a peer
1367 * @param cls the address handle
1368 * @param peer the peer
1369 * @param kind the ATS kind
1370 * @param pref_rel the new relative preference value
1373 normalized_preference_changed_cb (void *cls,
1374 const struct GNUNET_PeerIdentity *peer, enum GNUNET_ATS_PreferenceKind kind,
1377 GNUNET_assert(NULL != cls);
1378 struct GAS_Addresses_Handle *handle = cls;
1380 /* Tell solver about update */
1381 handle->s_pref (handle->solver, peer, kind, pref_rel);
1385 * The relative value for a property changed
1387 * @param cls the address handle
1388 * @param address the peer
1389 * @param type the ATS type
1390 * @param prop_rel the new relative preference value
1393 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
1394 uint32_t type, double prop_rel)
1396 struct GAS_Addresses_Handle *ah = (struct GAS_Addresses_Handle *) cls;
1397 GNUNET_assert(NULL != ah);
1399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1400 "Normalized property %s for peer `%s' changed to %.3f \n",
1401 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1404 ah->s_address_update_property (ah->solver, address, type, 0, prop_rel);
1408 * Function allowing the solver to obtain normalized preference
1409 * values from solver
1412 * @param id the peer to return the normalized properties for
1413 * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
1416 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
1418 return GAS_normalization_get_preferences (id);
1422 * Function allowing the solver to obtain normalized property
1423 * values for an address from solver
1426 * @param address the address
1427 * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
1430 get_property_cb (void *cls, const struct ATS_Address *address)
1432 return GAS_normalization_get_properties ((struct ATS_Address *) address);
1436 * Change the preference for a peer
1438 * @param handle the address handle
1439 * @param client the client sending this request
1440 * @param peer the peer id
1441 * @param kind the preference kind to change
1442 * @param score_abs the new preference score
1445 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
1446 void *client, const struct GNUNET_PeerIdentity *peer,
1447 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1450 "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1451 GNUNET_i2s (peer), client);
1453 if (GNUNET_NO == handle->running)
1457 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1460 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1461 "Received `%s' for unknown peer `%s' from client %p\n",
1462 "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1466 handle->s_bulk_start (handle->solver);
1467 /* Tell normalization about change, normalization will call callback if preference changed */
1468 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1469 handle->s_bulk_stop (handle->solver);
1473 * Change the preference for a peer
1475 * @param handle the address handle
1476 * @param application the client sending this request
1477 * @param peer the peer id
1478 * @param scope the time interval for this feedback: [now - scope .. now]
1479 * @param kind the preference kind to change
1480 * @param score_abs the new preference score
1483 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1484 void *application, const struct GNUNET_PeerIdentity *peer,
1485 const struct GNUNET_TIME_Relative scope,
1486 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1488 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1489 "Received `%s' for peer `%s' for client %p\n", "PREFERENCE FEEDBACK",
1490 GNUNET_i2s (peer), application);
1492 if (GNUNET_NO == handle->running)
1496 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1499 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1500 "Received `%s' for unknown peer `%s' from client %p\n",
1501 "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1505 handle->s_feedback (handle->solver, application, peer, scope, kind,
1510 * Load quotas for networks from configuration
1512 * @param cfg configuration handle
1513 * @param out_dest where to write outbound quotas
1514 * @param in_dest where to write inbound quotas
1515 * @param dest_length length of inbound and outbound arrays
1516 * @return number of networks loaded
1519 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1520 unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1522 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1523 char * entry_in = NULL;
1524 char * entry_out = NULL;
1525 char * quota_out_str;
1526 char * quota_in_str;
1530 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1534 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1535 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1539 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
1543 if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
1545 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1548 if ((GNUNET_NO == res)
1550 == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
1553 if ((GNUNET_NO == res)
1555 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
1559 if (GNUNET_NO == res)
1561 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1562 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1563 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1564 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1568 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1569 _("Outbound quota configure for network `%s' is %llu\n"),
1570 network_str[c], out_dest[c]);
1572 GNUNET_free(quota_out_str);
1576 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1577 _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1578 network_str[c], GNUNET_ATS_DefaultBandwidth);
1579 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1584 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
1588 if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
1590 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1593 if ((GNUNET_NO == res)
1595 == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1597 if ((GNUNET_NO == res)
1599 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
1603 if (GNUNET_NO == res)
1605 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1606 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1607 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1608 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1612 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1613 _("Inbound quota configured for network `%s' is %llu\n"),
1614 network_str[c], in_dest[c]);
1616 GNUNET_free(quota_in_str);
1620 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1621 _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1622 network_str[c], GNUNET_ATS_DefaultBandwidth);
1623 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1625 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1626 "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c],
1627 in_dest[c], out_dest[c]);
1628 GNUNET_free(entry_out);
1629 GNUNET_free(entry_in);
1631 return GNUNET_ATS_NetworkTypeCount;
1635 * Callback for solver to notify about assignment changes
1637 * @param cls the GAS_Addresses_Handle
1638 * @param address the address with changes
1641 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1643 struct GAS_Addresses_Handle *handle = cls;
1644 struct GAS_Addresses_Suggestion_Requests *cur;
1646 GNUNET_assert(handle != NULL);
1647 GNUNET_assert(address != NULL);
1649 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1650 "Bandwidth assignment changed for peer %s \n",
1651 GNUNET_i2s (&address->peer));
1653 /* Notify performance clients about changes to address */
1654 GAS_performance_notify_all_clients (&address->peer, address->plugin,
1655 address->addr, address->addr_len, address->session_id, address->atsi,
1656 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1657 cur = handle->r_head;
1660 if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
1661 break; /* we have an address request pending*/
1666 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Nobody is interested in peer `%s' :(\n",
1667 GNUNET_i2s (&address->peer));
1671 if ((0 == ntohl (address->assigned_bw_in.value__))
1672 && (0 == ntohl (address->assigned_bw_out.value__)))
1674 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1675 "Telling transport to disconnect peer `%s'\n",
1676 GNUNET_i2s (&address->peer));
1680 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1681 "Sending bandwidth update for peer `%s': %llu %llu\n",
1682 GNUNET_i2s (&address->peer), address->assigned_bw_out,
1683 address->assigned_bw_out);
1686 /* *Notify scheduling clients about suggestion */
1687 GAS_scheduling_transmit_address_suggestion (&address->peer, address->plugin,
1688 address->addr, address->addr_len, address->session_id, address->atsi,
1689 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1693 * Initialize address subsystem. The addresses subsystem manages the addresses
1694 * known and current performance information. It has a solver component
1695 * responsible for the resource allocation. It tells the solver about changes
1696 * and receives updates when the solver changes the resource allocation.
1698 * @param cfg configuration to use
1699 * @param stats the statistics handle to use
1700 * @return an address handle
1702 struct GAS_Addresses_Handle *
1703 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1704 const struct GNUNET_STATISTICS_Handle *stats)
1706 struct GAS_Addresses_Handle *ah;
1707 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1708 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1709 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1714 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1715 ah->running = GNUNET_NO;
1717 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1718 /* Initialize the addresses database */
1719 ah->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1720 GNUNET_assert(NULL != ah->addresses);
1722 /* Figure out configured solution method */
1724 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1726 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1727 "No resource assignment method configured, using proportional approach\n");
1728 ah->ats_mode = MODE_PROPORTIONAL;
1732 for (c = 0; c < strlen (mode_str); c++)
1733 mode_str[c] = toupper (mode_str[c]);
1734 if (0 == strcmp (mode_str, "PROPORTIONAL"))
1736 ah->ats_mode = MODE_PROPORTIONAL;
1738 else if (0 == strcmp (mode_str, "MLP"))
1740 ah->ats_mode = MODE_MLP;
1742 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1743 "Assignment method `%s' configured, but GLPK is not available, please install \n",
1745 ah->ats_mode = MODE_PROPORTIONAL;
1748 else if (0 == strcmp (mode_str, "RIL"))
1750 ah->ats_mode = MODE_RIL;
1754 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1755 "Invalid resource assignment method `%s' configured, using proportional approach\n",
1757 ah->ats_mode = MODE_PROPORTIONAL;
1759 GNUNET_free(mode_str);
1761 /* Start configured solution method */
1762 switch (ah->ats_mode)
1765 /* Init the MLP solver with default values */
1767 ah->s_init = &GAS_mlp_init;
1768 ah->s_add = &GAS_mlp_address_add;
1769 ah->s_address_update_property = &GAS_mlp_address_property_changed;
1770 ah->s_address_update_session = &GAS_mlp_address_session_changed;
1771 ah->s_address_update_inuse = &GAS_mlp_address_inuse_changed;
1772 ah->s_address_update_network = &GAS_mlp_address_change_network;
1773 ah->s_get = &GAS_mlp_get_preferred_address;
1774 ah->s_get_stop = &GAS_mlp_stop_get_preferred_address;
1775 ah->s_pref = &GAS_mlp_address_change_preference;
1776 ah->s_feedback = &GAS_mlp_address_preference_feedback;
1777 ah->s_del = &GAS_mlp_address_delete;
1778 ah->s_bulk_start = &GAS_mlp_bulk_start;
1779 ah->s_bulk_stop = &GAS_mlp_bulk_stop;
1780 ah->s_done = &GAS_mlp_done;
1786 case MODE_PROPORTIONAL:
1787 /* Init the proportional solver with default values */
1788 ah->s_init = &GAS_proportional_init;
1789 ah->s_add = &GAS_proportional_address_add;
1790 ah->s_address_update_property = &GAS_proportional_address_property_changed;
1791 ah->s_address_update_session = &GAS_proportional_address_session_changed;
1792 ah->s_address_update_inuse = &GAS_proportional_address_inuse_changed;
1793 ah->s_address_update_network = &GAS_proportional_address_change_network;
1794 ah->s_get = &GAS_proportional_get_preferred_address;
1795 ah->s_get_stop = &GAS_proportional_stop_get_preferred_address;
1796 ah->s_pref = &GAS_proportional_address_change_preference;
1797 ah->s_feedback = &GAS_proportional_address_preference_feedback;
1798 ah->s_del = &GAS_proportional_address_delete;
1799 ah->s_bulk_start = &GAS_proportional_bulk_start;
1800 ah->s_bulk_stop = &GAS_proportional_bulk_stop;
1801 ah->s_done = &GAS_proportional_done;
1802 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n",
1806 /* Init the ril solver with default values */
1807 ah->s_init = &GAS_ril_init;
1808 ah->s_add = &GAS_ril_address_add;
1809 ah->s_address_update_property = &GAS_ril_address_property_changed;
1810 ah->s_address_update_session = &GAS_ril_address_session_changed;
1811 ah->s_address_update_inuse = &GAS_ril_address_inuse_changed;
1812 ah->s_address_update_network = &GAS_ril_address_change_network;
1813 ah->s_get = &GAS_ril_get_preferred_address;
1814 ah->s_get_stop = &GAS_ril_stop_get_preferred_address;
1815 ah->s_pref = &GAS_ril_address_change_preference;
1816 ah->s_feedback = &GAS_ril_address_preference_feedback;
1817 ah->s_del = &GAS_ril_address_delete;
1818 ah->s_bulk_start = &GAS_ril_bulk_start;
1819 ah->s_bulk_stop = &GAS_ril_bulk_stop;
1820 ah->s_done = &GAS_ril_done;
1821 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "RIL");
1828 GNUNET_assert (NULL != ah->s_init);
1829 GNUNET_assert (NULL != ah->s_add);
1830 GNUNET_assert (NULL != ah->s_address_update_inuse);
1831 GNUNET_assert (NULL != ah->s_address_update_property);
1832 GNUNET_assert (NULL != ah->s_address_update_session);
1833 GNUNET_assert (NULL != ah->s_address_update_network);
1834 GNUNET_assert (NULL != ah->s_get);
1835 GNUNET_assert (NULL != ah->s_get_stop);
1836 GNUNET_assert (NULL != ah->s_pref);
1837 GNUNET_assert (NULL != ah->s_feedback);
1838 GNUNET_assert (NULL != ah->s_del);
1839 GNUNET_assert (NULL != ah->s_done);
1840 GNUNET_assert (NULL != ah->s_bulk_start);
1841 GNUNET_assert (NULL != ah->s_bulk_stop);
1843 GAS_normalization_start (&normalized_preference_changed_cb, ah,
1844 &normalized_property_changed_cb, ah);
1845 quota_count = load_quotas (cfg, quotas_in, quotas_out,
1846 GNUNET_ATS_NetworkTypeCount);
1848 ah->solver = ah->s_init (cfg, stats, ah->addresses, quotas, quotas_in,
1849 quotas_out, quota_count,
1850 &bandwidth_changed_cb, ah, &get_preferences_cb,
1851 NULL, &get_property_cb, NULL );
1852 if (NULL == ah->solver)
1854 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver!\n"));
1858 /* up and running */
1859 ah->running = GNUNET_YES;
1861 GNUNET_STATISTICS_set (ah->stat, "# addresses",
1862 GNUNET_CONTAINER_multipeermap_size (ah->addresses), GNUNET_NO);
1868 * Destroy all addresses iterator
1871 * @param key peer identity (unused)
1872 * @param value the 'struct ATS_Address' to free
1873 * @return #GNUNET_OK (continue to iterate)
1876 destroy_all_address_it (void *cls,
1877 const struct GNUNET_PeerIdentity *key,
1880 struct GAS_Addresses_Handle *handle = cls;
1881 struct ATS_Address *aa = value;
1884 GNUNET_assert(GNUNET_YES ==
1885 GNUNET_CONTAINER_multipeermap_remove (handle->addresses, key, value));
1887 handle->s_del (handle->solver, aa, GNUNET_NO);
1896 * Remove all addresses
1898 * @param handle the address handle to use
1901 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1903 if (GNUNET_NO == handle->running)
1906 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Destroying all addresses\n");
1907 handle->s_bulk_start (handle->solver);
1908 if (handle->addresses != NULL )
1909 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
1910 &destroy_all_address_it,
1912 handle->s_bulk_start (handle->solver);
1917 * Shutdown address subsystem.
1919 * @param handle the address handle to shutdown
1922 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1924 struct GAS_Addresses_Suggestion_Requests *cur;
1926 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Shutting down addresses\n");
1927 GNUNET_assert(NULL != handle);
1928 GAS_addresses_destroy_all (handle);
1929 handle->running = GNUNET_NO;
1930 GNUNET_CONTAINER_multipeermap_destroy (handle->addresses);
1931 handle->addresses = NULL;
1932 while (NULL != (cur = handle->r_head))
1934 GNUNET_CONTAINER_DLL_remove(handle->r_head, handle->r_tail, cur);
1937 handle->s_done (handle->solver);
1938 GNUNET_free(handle);
1939 /* Stop configured solution method */
1940 GAS_normalization_stop ();
1944 struct PeerIteratorContext
1946 GNUNET_ATS_Peer_Iterator it;
1948 struct GNUNET_CONTAINER_MultiPeerMap *peers_returned;
1953 * Iterator to iterate over all peers
1955 * @param cls a PeerIteratorContext
1956 * @param key the peer id
1957 * @param value the ATS_address
1958 * @return #GNUNET_OK to continue
1962 const struct GNUNET_PeerIdentity *key,
1965 struct PeerIteratorContext *ip_ctx = cls;
1968 GNUNET_CONTAINER_multipeermap_contains (ip_ctx->peers_returned, key))
1970 GNUNET_CONTAINER_multipeermap_put (ip_ctx->peers_returned, key, NULL,
1971 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1972 ip_ctx->it (ip_ctx->it_cls, key);
1979 * Return information all peers currently known to ATS
1981 * @param handle the address handle to use
1982 * @param p_it the iterator to call for every peer
1983 * @param p_it_cls the closure for the iterator
1986 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
1987 GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1989 struct PeerIteratorContext ip_ctx;
1994 GNUNET_assert(NULL != handle->addresses);
1996 size = GNUNET_CONTAINER_multipeermap_size (handle->addresses);
2000 ip_ctx.it_cls = p_it_cls;
2001 ip_ctx.peers_returned = GNUNET_CONTAINER_multipeermap_create (size,
2003 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2006 GNUNET_CONTAINER_multipeermap_destroy (ip_ctx.peers_returned);
2008 p_it (p_it_cls, NULL );
2011 struct PeerInfoIteratorContext
2013 GNUNET_ATS_PeerInfo_Iterator it;
2019 * Iterator to iterate over a peer's addresses
2021 * @param cls a `struct PeerInfoIteratorContext`
2022 * @param key the peer id
2023 * @param value the `struct ATS_address`
2024 * @return #GNUNET_OK to continue
2027 peerinfo_it (void *cls,
2028 const struct GNUNET_PeerIdentity *key,
2031 struct PeerInfoIteratorContext *pi_ctx = cls;
2032 struct ATS_Address *addr = value;
2034 if (NULL != pi_ctx->it)
2036 pi_ctx->it (pi_ctx->it_cls, &addr->peer, addr->plugin, addr->addr,
2037 addr->addr_len, addr->active, addr->atsi, addr->atsi_count,
2038 addr->assigned_bw_out, addr->assigned_bw_in);
2045 * Return information all peers currently known to ATS
2047 * @param handle the address handle to use
2048 * @param peer the respective peer
2049 * @param pi_it the iterator to call for every peer
2050 * @param pi_it_cls the closure for the iterator
2053 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
2054 const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it,
2057 struct PeerInfoIteratorContext pi_ctx;
2058 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
2060 GNUNET_assert(NULL != peer);
2061 GNUNET_assert(NULL != handle->addresses);
2063 return; /* does not make sense without callback */
2065 zero_bw = GNUNET_BANDWIDTH_value_init (0);
2067 pi_ctx.it_cls = pi_it_cls;
2069 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
2071 &peerinfo_it, &pi_ctx);
2074 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw,
2079 /* end of gnunet-service-ats_addresses.c */