2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file ats/gnunet-service-ats_addresses.c
23 * @brief ats service address management
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_ats_service.h"
29 #include "gnunet_ats_plugin.h"
30 #include "gnunet-service-ats.h"
31 #include "gnunet-service-ats_addresses.h"
32 #include "gnunet-service-ats_normalization.h"
33 #include "gnunet-service-ats_performance.h"
34 #include "gnunet-service-ats_scheduling.h"
35 #include "gnunet-service-ats_reservations.h"
39 #include "gnunet-service-ats-solver_mlp.h"
41 #include "gnunet-service-ats-solver_proportional.h"
42 #include "gnunet-service-ats-solver_ril.h"
46 * NOTE: Do not change this documentation. This documentation is based on
47 * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
48 * use build_txt.sh to generate plaintext output
50 * 1 ATS addresses : ATS address management
52 * This ATS addresses ("addresses") component manages the addresses known to
53 * ATS service and suggests addresses to transport service when it is
54 * interested in address suggestion for a peer. ATS addresses also
55 * instantiates the bandwidth assignment mechanism (solver), notifies it
56 * about changes to addresses and forwards changes to bandwidth assignments
57 * to transport, depending if transport is interested in this change.
63 * Addresses are added by specifying peer ID, plugin, address, address length
64 * and session, if available. ATS information can be specified if available.
68 * ATS specifies a fix set of networks an address can belong to. For each
69 * network an inbound and outbound quota will be specified. The available
70 * networks and addtional helper varaibles are defined in
71 * gnunet_ats_service.h. At the moment 5 networks are defined:
72 * * GNUNET_ATS_NET_UNSPECIFIED
73 * * GNUNET_ATS_NET_LOOPBACK
74 * * GNUNET_ATS_NET_LAN
75 * * GNUNET_ATS_NET_WAN
76 * * GNUNET_ATS_NET_WLAN
78 * The total number of networks defined is stored in
79 * GNUNET_ATS_NetworkTypeCount GNUNET_ATS_NetworkType can be used array
80 * initializer for an int array, while GNUNET_ATS_NetworkType is an
81 * initializer for a char array containing a string description of all
86 * An inbound and outbound quota for each of the networks mentioned in 1.1.2
87 * is loaded from ats configuration during initialization. This quota defines
88 * to total amount of inbound and outbound traffic allowed for a specific
89 * network. The configuration values used are in section ats:
90 * * "NETWORK"_QUOTA_IN = <value>
91 * * "NETWORK"_QUOTA_IN = <value>
93 * You can specify quotas by setting the <value> to a:
94 * * unrestricted: unlimited
95 * * number of bytes: e.g. 10240
96 * * fancy value: e.g. 64 Kib
98 * unlimited is defined as GNUNET_ATS_MaxBandwidthString and equivalent to
99 * the value GNUNET_ATS_MaxBandwidth Important predefined values for quotas
101 * * GNUNET_ATS_DefaultBandwidth: 65536
102 * * GNUNET_ATS_MaxBandwidth: UINT32_MAX
103 * * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT: 1024
105 * Details of loading quotas and default values will be described on
107 * 1.1.4 Preference values
109 * 1.2 Data structures used
111 * Addresse uses struct ATS_Address for each address. The structs are stored
112 * in a linked list and provides a pointer void *solver_information for the
113 * solver to store address specific information. It provides the int values
114 * active which is set to GNUNET_YES if the address is select for transport
115 * use and used, representing that transport service is actively using this
116 * address. Address information are stored in peer, addr, addr_len, plugin.
120 * During initialization a hashmap to store addresses is created. The quotas
121 * for all networks defined for ATS are loaded from configuration. For each
122 * network first the logic will check if the string
123 * GNUNET_ATS_MaxBandwidthString is configured, if not it will try to convert
124 * the configured value as a fancy size and if this fails it will try to use
125 * it as a value_number. If no configuration value is found it will assign
126 * GNUNET_ATS_DefaultBandwidth. The most important step is to load the
127 * configured solver using configuration "[ats]:MODE". Current solvers are
128 * MODE_PROPORTIONAL, MODE_MLP. Interaction is done using a solver API
133 * * s_init: init the solver with required information
134 * * s_add: add a new address
135 * * s_update: update ATS values or session for an address
136 * * s_get: get prefered address for a peer
137 * * s_del: delete an address
138 * * s_pref: change preference value for a peer
139 * * s_done: shutdown solver
141 * Callbacks: addresses provides a bandwidth_changed_cb callback to the
142 * solver which is called when bandwidth assigned to peer has changed
146 * During shutdown all addresses are freed and the solver told to shutdown
148 * 1.6 Addresses and sessions
150 * Addresses consist of the address itself and a numerical session. When a
151 * new address without a session is added it has no session, so it gets
152 * session 0 assigned. When an address with a session is added and an address
153 * object with session 0 is found, this object is updated with the session
154 * otherwise a new address object with this session assigned is created.
158 * Addresses a1,a2 with session s1, s2 are "exact" if:
159 * (a1 == a2)&&(s1 == s2)
160 * Addresses a1,a2 with session s1, s2 are "equivalent" if:
161 * (a1 == a2)&&((s1 == s2)||(s1 == 0)||(s2 == 0)
163 * 1.7 Address management
165 * Transport service notifies ATS about changes to the addresses known to
168 * 1.7.1 Adding an address
170 * When transport learns a new address it tells ATS and ATS is telling
171 * addresses about it using GAS_address_add. If not known to addresses it
172 * creates a new address object and calls solver's s_add. ATS information are
173 * deserialized and solver is notified about the session and ATS information
176 * 1.7.2 Updating an address
178 * Addresses does an lookup up for the existing address with the given
179 * session. If disassembles included ATS information and notifies the solver
180 * using s_update about the update.
182 * 1.7.3 Deleting an address
184 * Addresses does an lookup for the exact address and session and if removes
185 * this address. If session != 0 the session is set to 0 and the address is
186 * kept. If session == 0, the addresses is removed.
188 * 1.7.4 Requesting an address suggestion
190 * The address client issues a request address message to be notified about
191 * address suggestions for a specific peer. Addresses asks the solver with
192 * s_get. If no address is available, it will not send a response, otherwise
193 * it will respond with the choosen address.
195 * 1.7.5 Address suggestions
197 * Addresses will notify the client automatically on any bandwidth_changed_cb
198 * by the solver if a address suggestion request is pending. If no address is
199 * available it will not respond at all If the client is not interested
200 * anymore, it has to cancel the address suggestion request.
202 * 1.7.6 Suggestions blocks and reset
204 * After suggesting an address it is blocked for ATS_BLOCKING_DELTA sec. to
205 * prevent the client from being thrashed. If the client requires immediately
206 * it can reset this block using GAS_addresses_handle_backoff_reset.
208 * 1.7.7 Marking address in use
210 * The client can notify addresses that it successfully uses an address and
211 * wants this address to be kept by calling GSA_address_in_use. Adresses will
212 * mark the address as used an notify the solver about the use.
214 * 1.7.8 Address lifecycle
217 * * (updated address) || (address in use)
220 * 1.8 Bandwidth assignment
222 * The addresses are used to perform resource allocation operations. ATS
223 * addresses takes care of instantiating the solver configured and notifies
224 * the respective solver about address changes and receives changes to the
225 * bandwidth assignment from the solver. The current bandwidth assignment is
226 * sent to transport. The specific solvers will be described in the specific
229 * 1.9 Changing peer preferences
231 * The bandwidth assigned to a peer can be influenced by setting a preference
232 * for a peer. The prefernce will be given to to the solver with s_pref which
233 * has to take care of the preference value
238 * Available ressource assignment modes
245 * Assign each peer an equal amount of bandwidth (bw)
247 * bw_per_peer = bw_total / #active addresses
254 * Solve ressource assignment as an optimization problem
255 * Uses an mixed integer programming solver
260 * Reinforcement Learning mode:
262 * Solve resource assignment using a learning agent
268 * Pending Address suggestion requests
270 struct GAS_Addresses_Suggestion_Requests
275 struct GAS_Addresses_Suggestion_Requests *next;
280 struct GAS_Addresses_Suggestion_Requests *prev;
285 struct GNUNET_PeerIdentity id;
289 * Handle for ATS address component
291 struct GAS_Addresses_Handle
296 struct GNUNET_STATISTICS_Handle *stat;
299 * A multihashmap to store all addresses
301 struct GNUNET_CONTAINER_MultiPeerMap *addresses;
304 * Configure WAN quota in
306 unsigned long long wan_quota_in;
309 * Configure WAN quota out
311 unsigned long long wan_quota_out;
314 * Is ATS addresses running
319 * Configured ATS solver
329 * Address suggestion requests DLL head
331 struct GAS_Addresses_Suggestion_Requests *r_head;
334 * Address suggestion requests DLL tail
336 struct GAS_Addresses_Suggestion_Requests *r_tail;
338 /* Solver functions */
339 struct GNUNET_ATS_PluginEnvironment env;
345 * Disassemble ATS information and update performance information in address
347 * Updates existing information and adds new information
349 * @param dest destination address
350 * @param update source ATS information
351 * @param update_count number of ATS information
352 * @param delta_dest ats performance information which were updated
353 * including previous value
354 * @param delta_count number of ATS information in the delta
355 * @return GNUNET_YES if address was address updated, GNUNET_NO otherwise
358 disassemble_ats_information (struct ATS_Address *dest,
359 const struct GNUNET_ATS_Information *update, uint32_t update_count,
360 struct GNUNET_ATS_Information **delta_dest, uint32_t *delta_count)
368 struct GNUNET_ATS_Information add_atsi[update_count];
369 struct GNUNET_ATS_Information delta_atsi[update_count];
370 struct GNUNET_ATS_Information *tmp_atsi;
371 uint32_t add_atsi_count;
372 uint32_t delta_atsi_count;
376 delta_atsi_count = 0;
378 if (0 == update_count)
381 if (NULL == dest->atsi)
383 /* Create performance information */
385 GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
386 dest->atsi_count = update_count;
387 memcpy (dest->atsi, update,
388 update_count * sizeof(struct GNUNET_ATS_Information));
390 GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
391 for (c1 = 0; c1 < update_count; c1++)
393 (*delta_dest)[c1].type = update[c1].type;
394 (*delta_dest)[c1].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
396 (*delta_count) = update_count;
400 for (c1 = 0; c1 < update_count; c1++)
402 /* Update existing performance information */
404 for (c2 = 0; c2 < dest->atsi_count; c2++)
406 if (update[c1].type == dest->atsi[c2].type)
408 if (update[c1].value != dest->atsi[c2].value)
410 /* Save previous value in delta */
411 delta_atsi[delta_atsi_count] = dest->atsi[c2];
414 dest->atsi[c2].value = update[c1].value;
421 if (GNUNET_NO == found)
423 add_atsi[add_atsi_count] = update[c1];
425 delta_atsi[delta_atsi_count].type = update[c1].type;
426 delta_atsi[delta_atsi_count].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
431 if (add_atsi_count > 0)
433 /* Extend ats performance information */
435 tmp_atsi = GNUNET_malloc ((dest->atsi_count + add_atsi_count) *
436 (sizeof (struct GNUNET_ATS_Information)));
437 memcpy (tmp_atsi, dest->atsi,
438 dest->atsi_count * sizeof(struct GNUNET_ATS_Information));
439 memcpy (&tmp_atsi[dest->atsi_count], add_atsi,
440 add_atsi_count * sizeof(struct GNUNET_ATS_Information));
441 GNUNET_free(dest->atsi);
442 dest->atsi = tmp_atsi;
443 dest->atsi_count = dest->atsi_count + add_atsi_count;
447 if (delta_atsi_count > 0)
451 GNUNET_malloc (delta_atsi_count * sizeof (struct GNUNET_ATS_Information));
452 memcpy ((*delta_dest), delta_atsi,
453 delta_atsi_count * sizeof(struct GNUNET_ATS_Information));
454 (*delta_count) = delta_atsi_count;
461 * Free the given address
463 * @param addr address to destroy
466 free_address (struct ATS_Address *addr)
468 GNUNET_free(addr->plugin);
469 GNUNET_free_non_null(addr->atsi);
474 * Create a ATS_address with the given information
477 * @param plugin_name plugin
478 * @param plugin_addr address
479 * @param plugin_addr_len address length
480 * @param session_id session
481 * @return the ATS_Address
483 static struct ATS_Address *
484 create_address (const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
485 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
487 struct ATS_Address *aa = NULL;
491 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
493 aa->addr_len = plugin_addr_len;
495 memcpy (&aa[1], plugin_addr, plugin_addr_len);
496 aa->plugin = GNUNET_strdup (plugin_name);
497 aa->session_id = session_id;
498 aa->active = GNUNET_NO;
499 aa->used = GNUNET_NO;
500 aa->solver_information = NULL;
503 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
504 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
506 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
508 aa->atsin[c1].avg_queue_index = 0;
509 for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
510 aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
516 struct CompareAddressContext
518 const struct ATS_Address *search;
520 /* exact_address != NULL if address and session is equal */
521 struct ATS_Address *exact_address;
522 /* exact_address != NULL if address and session is 0 */
523 struct ATS_Address *base_address;
530 * @param cls a CompareAddressContext containin the source address
532 * @param value the address to compare with
533 * @return #GNUNET_YES to continue, #GNUNET_NO if address is founce
536 compare_address_it (void *cls,
537 const struct GNUNET_PeerIdentity *key,
540 struct CompareAddressContext *cac = cls;
541 struct ATS_Address *aa = value;
543 /* Find an matching exact address:
546 * aa->addr_len == cac->search->addr_len
547 * aa->plugin == cac->search->plugin
548 * aa->addr == cac->search->addr
549 * aa->session == cac->search->session
551 * return as exact address
553 if ((aa->addr_len == cac->search->addr_len)
554 && (0 == strcmp (aa->plugin, cac->search->plugin)))
556 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
557 && (aa->session_id == cac->search->session_id))
558 cac->exact_address = aa;
561 /* Find an matching base address:
565 * aa->session_id == 0
568 * aa->addr_len == cac->search->addr_len
569 * aa->plugin == cac->search->plugin
570 * aa->addr == cac->search->addr
572 * return as base address
574 if ((aa->addr_len == cac->search->addr_len)
575 && (0 == strcmp (aa->plugin, cac->search->plugin)))
577 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
578 && (aa->session_id == 0))
579 cac->base_address = aa;
582 /* Find an matching exact address based on session:
586 * cac->search->addr_len == 0
589 * aa->plugin == cac->search->plugin
590 * aa->session_id == cac->search->session_id
592 * return as exact address
594 if (0 == cac->search->addr_len)
596 if ((0 == strcmp (aa->plugin, cac->search->plugin))
597 && (aa->session_id == cac->search->session_id))
598 cac->exact_address = aa;
601 if (cac->exact_address == NULL )
602 return GNUNET_YES; /* Continue iteration to find exact address */
604 return GNUNET_NO; /* Stop iteration since we have an exact address */
608 * Find an existing equivalent address record.
609 * Compares by peer identity and network address OR by session ID
610 * (one of the two must match).
612 * @param handle the address handle
613 * @param peer peer to lookup addresses for
614 * @param addr existing address record
615 * @return existing address record, NULL for none
618 find_equivalent_address (struct GAS_Addresses_Handle *handle,
619 const struct GNUNET_PeerIdentity *peer, const struct ATS_Address *addr)
621 struct CompareAddressContext cac;
623 cac.exact_address = NULL;
624 cac.base_address = NULL;
626 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
628 &compare_address_it, &cac);
630 if (cac.exact_address == NULL)
631 return cac.base_address;
632 return cac.exact_address;
637 * Find the exact address
639 * @param handle the address handle to use
641 * @param plugin_name transport plugin name
642 * @param plugin_addr plugin address
643 * @param plugin_addr_len length of the plugin address
644 * @param session_id session id, can be 0
645 * @return an ATS_address or NULL
648 static struct ATS_Address *
649 find_exact_address (struct GAS_Addresses_Handle *handle,
650 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
651 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
653 struct ATS_Address *aa;
654 struct ATS_Address *ea;
656 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
659 /* Get existing address or address with session == 0 */
660 ea = find_equivalent_address (handle, peer, aa);
664 else if (ea->session_id != session_id)
670 * Extract an ATS performance info from an address
672 * @param address the address
673 * @param type the type to extract in HBO
674 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
677 get_performance_info (struct ATS_Address *address, uint32_t type)
680 GNUNET_assert(NULL != address);
682 if ((NULL == address->atsi) || (0 == address->atsi_count))
683 return GNUNET_ATS_VALUE_UNDEFINED;
685 for (c1 = 0; c1 < address->atsi_count; c1++)
687 if (ntohl (address->atsi[c1].type) == type)
688 return ntohl (address->atsi[c1].value);
690 return GNUNET_ATS_VALUE_UNDEFINED;
694 * Add a new address for a peer.
696 * @param handle the address handle to use
698 * @param plugin_name transport plugin name
699 * @param plugin_addr plugin address
700 * @param plugin_addr_len length of the plugin address
701 * @param session_id session id, can be 0
702 * @param atsi performance information for this address
703 * @param atsi_count number of performance information contained
706 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
707 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
708 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
709 const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
711 struct ATS_Address *new_address;
712 struct ATS_Address *existing_address;
713 struct GNUNET_ATS_Information *atsi_delta;
714 uint32_t atsi_delta_count;
716 uint32_t previous_session;
719 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
720 "ADDRESS ADD", GNUNET_i2s (peer));
722 if (GNUNET_NO == handle->running)
725 GNUNET_assert(NULL != handle->addresses);
727 new_address = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
730 disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
732 GNUNET_free_non_null(atsi_delta);
733 addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
734 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
735 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
737 /* Get existing address or address with session == 0 */
738 existing_address = find_equivalent_address (handle, peer, new_address);
739 if (existing_address == NULL )
741 /* Add a new address */
743 GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (handle->addresses,
746 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
748 GNUNET_STATISTICS_set (handle->stat, "# addresses",
749 GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
751 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
752 "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
753 new_address, GNUNET_i2s (peer), plugin_addr_len, session_id,
754 GNUNET_ATS_print_network_type (addr_net));
756 /* Tell solver about new address */
757 handle->env.sf.s_add (handle->solver, new_address, addr_net);
759 handle->env.sf.s_bulk_start (handle->solver);
760 GAS_normalization_normalize_property (handle->addresses, new_address, atsi,
762 handle->env.sf.s_bulk_stop (handle->solver);
764 /* Notify performance clients about new address */
765 GAS_performance_notify_all_clients (&new_address->peer, new_address->plugin,
766 new_address->addr, new_address->addr_len, new_address->session_id,
767 new_address->atsi, new_address->atsi_count,
768 new_address->assigned_bw_out, new_address->assigned_bw_in);
772 /* We have an existing address we can use, clean up new */
773 GNUNET_free(new_address->plugin);
774 GNUNET_free_non_null(new_address->atsi);
775 GNUNET_free(new_address);
778 if (0 != existing_address->session_id)
780 /* Should not happen */
785 addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
786 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
787 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
789 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
790 "Found existing address for peer `%s' %p with new session %u in network %s\n",
791 GNUNET_i2s (peer), existing_address, session_id,
792 GNUNET_ATS_print_network_type (addr_net));
793 /* We have an address without an session, update this address */
795 atsi_delta_count = 0;
797 == disassemble_ats_information (existing_address, atsi, atsi_count,
798 &atsi_delta, &atsi_delta_count))
800 /* Notify performance clients about properties */
801 GAS_performance_notify_all_clients (&existing_address->peer,
802 existing_address->plugin, existing_address->addr,
803 existing_address->addr_len, existing_address->session_id,
804 existing_address->atsi, existing_address->atsi_count,
805 existing_address->assigned_bw_out, existing_address->assigned_bw_in);
807 for (c1 = 0; c1 < atsi_delta_count; c1++)
809 if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
810 && (addr_net != ntohl (atsi_delta[c1].value)))
812 /* Network type changed */
813 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
814 "Address for peer `%s' %p changed from network %s to %s\n",
815 GNUNET_i2s (peer), existing_address,
816 GNUNET_ATS_print_network_type (addr_net),
817 GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
818 handle->env.sf.s_address_update_network (handle->solver, existing_address,
819 ntohl (atsi_delta[c1].value),
820 get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
821 addr_net = get_performance_info (existing_address,
822 GNUNET_ATS_NETWORK_TYPE);
825 /* Notify solver about update with atsi information and session */
826 handle->env.sf.s_bulk_start (handle->solver);
827 GAS_normalization_normalize_property (handle->addresses, existing_address,
829 handle->env.sf.s_bulk_stop (handle->solver);
831 GNUNET_free_non_null(atsi_delta);
833 /* Notify solver about new session */
834 if (existing_address->session_id == session_id)
835 return; /* possible, can both be 0 since address is revalidated */
837 previous_session = existing_address->session_id;
838 existing_address->session_id = session_id;
839 handle->env.sf.s_address_update_session (handle->solver, existing_address,
840 previous_session, session_id);
842 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
843 "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
844 GNUNET_i2s (peer), existing_address, existing_address->addr_len,
845 session_id, GNUNET_ATS_print_network_type (addr_net));
849 * Update an address with a session or performance information for a peer.
851 * If an address was added without a session it will be updated with the
854 * @param handle the address handle to use
856 * @param plugin_name transport plugin name
857 * @param plugin_addr plugin address
858 * @param plugin_addr_len length of the plugin address
859 * @param session_id session id, can be 0
860 * @param atsi performance information for this address
861 * @param atsi_count number of performance information contained
864 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
865 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
866 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
867 const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
869 struct ATS_Address *aa;
870 struct GNUNET_ATS_Information *atsi_delta;
871 uint32_t atsi_delta_count;
872 uint32_t prev_session;
875 if (GNUNET_NO == handle->running)
878 GNUNET_assert(NULL != handle->addresses);
880 /* Get existing address */
881 aa = find_exact_address (handle, peer, plugin_name, plugin_addr,
882 plugin_addr_len, session_id);
885 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n", */
886 /* GNUNET_i2s (peer), plugin_name, session_id); */
887 /* GNUNET_break (0); */
891 if (NULL == aa->solver_information)
893 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
894 "Tried to update unknown address for peer `%s' `%s' session id %u\n",
895 GNUNET_i2s (peer), plugin_name, session_id);
899 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s' address \n",
900 "ADDRESS UPDATE", GNUNET_i2s (peer), aa);
903 if (session_id != aa->session_id)
905 /* Session changed */
906 prev_session = aa->session_id;
907 aa->session_id = session_id;
908 handle->env.sf.s_address_update_session (handle->solver, aa, prev_session,
913 atsi_delta_count = 0;
915 == disassemble_ats_information (aa, atsi, atsi_count, &atsi_delta,
918 /* ATS properties changed */
919 for (c1 = 0; c1 < atsi_delta_count; c1++)
921 if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
923 /* Network type changed */
924 handle->env.sf.s_address_update_network (handle->solver, aa,
925 ntohl (atsi_delta[c1].value),
926 get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
930 /* Notify performance clients about updated address */
931 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr,
932 aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
933 aa->assigned_bw_out, aa->assigned_bw_in);
935 handle->env.sf.s_bulk_start (handle->solver);
936 GAS_normalization_normalize_property (handle->addresses, aa, atsi,
938 handle->env.sf.s_bulk_stop (handle->solver);
940 GNUNET_free_non_null(atsi_delta);
943 struct DestroyContext
945 struct ATS_Address *aa;
947 struct GAS_Addresses_Handle *handle;
950 * GNUNET_NO : full address
951 * GNUNET_YES : just session
959 * If session != 0, just the session is deleted, the address itself still exists
960 * If session == 0, remove full address
961 * If session == 0 and addrlen == 0, destroy inbound address
965 * @param value the 'struct ATS_Address'
966 * @return GNUNET_OK (continue to iterate)
969 destroy_by_session_id (void *cls,
970 const struct GNUNET_PeerIdentity *key,
973 struct DestroyContext *dc = cls;
974 struct GAS_Addresses_Handle *handle = dc->handle;
975 const struct ATS_Address *des = dc->aa;
976 struct ATS_Address *aa = value;
979 0 == memcmp (&aa->peer, &des->peer, sizeof(struct GNUNET_PeerIdentity)));
981 if (des->session_id == 0)
983 /* Session == 0, remove full address */
984 if ((0 == strcmp (des->plugin, aa->plugin))
985 && (aa->addr_len == des->addr_len)
986 && (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
989 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
990 "Deleting full address for peer `%s' session %u %p\n",
991 GNUNET_i2s (&aa->peer), aa->session_id, aa);
993 /* Notify solver about deletion */
995 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
998 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1000 dc->result = GNUNET_NO;
1001 return GNUNET_OK; /* Continue iteration */
1006 /* Session != 0, just remove session */
1007 if (aa->session_id != des->session_id)
1008 return GNUNET_OK; /* irrelevant */
1010 if ((aa->session_id != 0) && (0 != strcmp (des->plugin, aa->plugin)))
1012 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1013 "Different plugins during removal: `%s' vs `%s' \n", des->plugin,
1019 if (aa->addr_len == 0)
1021 /* Inbound connection died, delete full address */
1022 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1023 "Deleting inbound address for peer `%s': `%s' session %u\n",
1024 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
1026 /* Notify solver about deletion */
1028 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1030 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1032 dc->result = GNUNET_NO;
1033 return GNUNET_OK; /* Continue iteration */
1038 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1039 "Deleting session for peer `%s': `%s' %u\n", GNUNET_i2s (&aa->peer),
1040 aa->plugin, aa->session_id);
1041 /* Notify solver to delete session */
1042 handle->env.sf.s_del (handle->solver, aa, GNUNET_YES);
1044 aa->active = GNUNET_NO;
1053 * Remove an address or just a session for a peer.
1055 * @param handle the address handle to use
1057 * @param plugin_name transport plugin name
1058 * @param plugin_addr plugin address
1059 * @param plugin_addr_len length of the plugin address
1060 * @param session_id session id, can be 0
1063 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
1064 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1065 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
1067 struct ATS_Address *ea;
1068 struct DestroyContext dc;
1069 if (GNUNET_NO == handle->running)
1072 /* Get existing address */
1073 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1074 plugin_addr_len, session_id);
1077 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1078 "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
1079 GNUNET_i2s (peer), plugin_name, session_id);
1083 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1084 "Received `%s' for peer `%s' address %p session %u\n", "ADDRESS DESTROY",
1085 GNUNET_i2s (peer), ea, session_id);
1087 GNUNET_break(0 < strlen (plugin_name));
1089 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
1092 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1094 &destroy_by_session_id, &dc);
1095 GNUNET_STATISTICS_set (handle->stat, "# addresses",
1096 GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
1097 free_address (dc.aa);
1101 * Notification about active use of an address.
1102 * in_use == GNUNET_YES:
1103 * This address is used to maintain an active connection with a peer.
1104 * in_use == GNUNET_NO:
1105 * This address is no longer used to maintain an active connection with a peer.
1107 * Note: can only be called with in_use == GNUNET_NO if called with GNUNET_YES
1110 * @param handle the address handle to use
1112 * @param plugin_name transport plugin name
1113 * @param plugin_addr plugin address
1114 * @param plugin_addr_len length of the plugin address
1115 * @param session_id session id, can be 0
1116 * @param in_use GNUNET_YES if GNUNET_NO
1117 * @return GNUNET_SYSERR on failure (address unknown ...)
1120 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
1121 const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1122 const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
1125 struct ATS_Address *ea;
1126 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1127 "ADDRESS IN USE", GNUNET_i2s (peer));
1129 if (GNUNET_NO == handle->running)
1130 return GNUNET_SYSERR;
1132 ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1133 plugin_addr_len, session_id);
1136 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1137 "Trying to set unknown address `%s' `%s' `%u' to %s \n",
1138 GNUNET_i2s (peer), plugin_name, session_id,
1139 (GNUNET_NO == in_use) ? "NO" : "YES");
1141 return GNUNET_SYSERR;
1143 if (ea->used == in_use)
1146 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1147 "Address in use called multiple times for peer `%s': %s -> %s \n",
1148 GNUNET_i2s (peer), (GNUNET_NO == ea->used) ? "NO" : "YES",
1149 (GNUNET_NO == in_use) ? "NO" : "YES");
1150 return GNUNET_SYSERR;
1153 /* Tell solver about update */
1155 handle->env.sf.s_address_update_inuse (handle->solver, ea, ea->used);
1160 * Cancel address suggestions for a peer
1162 * @param handle the address handle
1163 * @param peer the peer id
1166 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
1167 const struct GNUNET_PeerIdentity *peer)
1169 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1171 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received request: `%s' for peer %s\n",
1172 "request_address_cancel", GNUNET_i2s (peer));
1176 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1183 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1184 "No address requests pending for peer `%s', cannot remove!\n",
1188 handle->env.sf.s_get_stop (handle->solver, peer);
1189 GAS_addresses_handle_backoff_reset (handle, peer);
1190 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Removed request pending for peer `%s\n",
1192 GNUNET_CONTAINER_DLL_remove(handle->r_head, handle->r_tail, cur);
1198 * Request address suggestions for a peer
1200 * @param handle the address handle
1201 * @param peer the peer id
1204 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
1205 const struct GNUNET_PeerIdentity *peer)
1207 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1208 struct ATS_Address *aa;
1210 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received `%s' for peer `%s'\n",
1211 "REQUEST ADDRESS", GNUNET_i2s (peer));
1213 if (GNUNET_NO == handle->running)
1217 if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1218 break; /* already suggesting */
1223 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
1225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1226 "Adding new address suggestion request for `%s'\n",
1228 GNUNET_CONTAINER_DLL_insert(handle->r_head, handle->r_tail, cur);
1232 * Debuging information about addresses
1234 * GNUNET_CONTAINER_multihashmap_get_multiple(handle->addresses,
1235 * &peer->hashPubKey, &addrinfo_it, (void *) peer);
1238 /* Get prefered address from solver */
1239 aa = (struct ATS_Address *) handle->env.sf.s_get (handle->solver, peer);
1242 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1247 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1248 aa, GNUNET_i2s (peer));
1250 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
1251 aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
1252 aa->assigned_bw_out, aa->assigned_bw_in);
1254 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1255 ATS_BLOCKING_DELTA);
1256 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1257 aa->block_interval);
1259 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1260 "Address %p ready for suggestion, block interval now %llu \n", aa,
1261 aa->block_interval);
1265 * Iterator to reset address blocking
1267 * @param cls not used
1268 * @param key the peer
1269 * @param value the address to reset
1270 * @return #GNUNET_OK to continue
1273 reset_address_it (void *cls,
1274 const struct GNUNET_PeerIdentity *key,
1277 struct ATS_Address *aa = value;
1279 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1280 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1281 GNUNET_i2s (&aa->peer),
1283 aa->block_interval);
1284 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1285 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1291 * Reset suggestion backoff for a peer
1293 * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1294 * reset using this function
1296 * @param handle the address handle
1297 * @param peer the peer id
1300 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1301 const struct GNUNET_PeerIdentity *peer)
1303 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1304 "RESET BACKOFF", GNUNET_i2s (peer));
1307 GNUNET_SYSERR != GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1309 &reset_address_it, NULL));
1314 * The preference changed for a peer
1316 * @param cls the address handle
1317 * @param peer the peer
1318 * @param kind the ATS kind
1319 * @param pref_rel the new relative preference value
1322 normalized_preference_changed_cb (void *cls,
1323 const struct GNUNET_PeerIdentity *peer, enum GNUNET_ATS_PreferenceKind kind,
1326 GNUNET_assert(NULL != cls);
1327 struct GAS_Addresses_Handle *handle = cls;
1329 /* Tell solver about update */
1330 handle->env.sf.s_pref (handle->solver, peer, kind, pref_rel);
1334 * The relative value for a property changed
1336 * @param cls the address handle
1337 * @param address the peer
1338 * @param type the ATS type
1339 * @param prop_rel the new relative preference value
1342 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
1343 uint32_t type, double prop_rel)
1345 struct GAS_Addresses_Handle *ah = (struct GAS_Addresses_Handle *) cls;
1346 GNUNET_assert(NULL != ah);
1348 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1349 "Normalized property %s for peer `%s' changed to %.3f \n",
1350 GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1353 ah->env.sf.s_address_update_property (ah->solver, address, type, 0, prop_rel);
1357 * Function allowing the solver to obtain normalized preference
1358 * values from solver
1361 * @param id the peer to return the normalized properties for
1362 * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
1365 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
1367 return GAS_normalization_get_preferences (id);
1371 * Function allowing the solver to obtain normalized property
1372 * values for an address from solver
1375 * @param address the address
1376 * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
1379 get_property_cb (void *cls, const struct ATS_Address *address)
1381 return GAS_normalization_get_properties ((struct ATS_Address *) address);
1385 * Change the preference for a peer
1387 * @param handle the address handle
1388 * @param client the client sending this request
1389 * @param peer the peer id
1390 * @param kind the preference kind to change
1391 * @param score_abs the new preference score
1394 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
1395 void *client, const struct GNUNET_PeerIdentity *peer,
1396 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1398 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1399 "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1400 GNUNET_i2s (peer), client);
1402 if (GNUNET_NO == handle->running)
1406 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1409 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1410 "Received `%s' for unknown peer `%s' from client %p\n",
1411 "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1415 handle->env.sf.s_bulk_start (handle->solver);
1416 /* Tell normalization about change, normalization will call callback if preference changed */
1417 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1418 handle->env.sf.s_bulk_stop (handle->solver);
1422 * Change the preference for a peer
1424 * @param handle the address handle
1425 * @param application the client sending this request
1426 * @param peer the peer id
1427 * @param scope the time interval for this feedback: [now - scope .. now]
1428 * @param kind the preference kind to change
1429 * @param score_abs the new preference score
1432 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1433 void *application, const struct GNUNET_PeerIdentity *peer,
1434 const struct GNUNET_TIME_Relative scope,
1435 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1437 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1438 "Received `%s' for peer `%s' for client %p\n", "PREFERENCE FEEDBACK",
1439 GNUNET_i2s (peer), application);
1441 if (GNUNET_NO == handle->running)
1445 GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1448 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1449 "Received `%s' for unknown peer `%s' from client %p\n",
1450 "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1454 handle->env.sf.s_feedback (handle->solver, application, peer, scope, kind,
1459 * Load quotas for networks from configuration
1461 * @param cfg configuration handle
1462 * @param out_dest where to write outbound quotas
1463 * @param in_dest where to write inbound quotas
1464 * @param dest_length length of inbound and outbound arrays
1465 * @return number of networks loaded
1468 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1469 unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1471 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1472 char * entry_in = NULL;
1473 char * entry_out = NULL;
1474 char * quota_out_str;
1475 char * quota_in_str;
1479 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1483 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1484 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1488 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
1492 if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
1494 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1497 if ((GNUNET_NO == res)
1499 == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
1502 if ((GNUNET_NO == res)
1504 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
1508 if (GNUNET_NO == res)
1510 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1511 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1512 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1513 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1517 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1518 _("Outbound quota configure for network `%s' is %llu\n"),
1519 network_str[c], out_dest[c]);
1521 GNUNET_free(quota_out_str);
1525 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1526 _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1527 network_str[c], GNUNET_ATS_DefaultBandwidth);
1528 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1533 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
1537 if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
1539 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1542 if ((GNUNET_NO == res)
1544 == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1546 if ((GNUNET_NO == res)
1548 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
1552 if (GNUNET_NO == res)
1554 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1555 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1556 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1557 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1561 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1562 _("Inbound quota configured for network `%s' is %llu\n"),
1563 network_str[c], in_dest[c]);
1565 GNUNET_free(quota_in_str);
1569 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1570 _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1571 network_str[c], GNUNET_ATS_DefaultBandwidth);
1572 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1574 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1575 "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c],
1576 in_dest[c], out_dest[c]);
1577 GNUNET_free(entry_out);
1578 GNUNET_free(entry_in);
1580 return GNUNET_ATS_NetworkTypeCount;
1584 * Callback for solver to notify about assignment changes
1586 * @param cls the GAS_Addresses_Handle
1587 * @param address the address with changes
1590 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1592 struct GAS_Addresses_Handle *handle = cls;
1593 struct GAS_Addresses_Suggestion_Requests *cur;
1595 GNUNET_assert(handle != NULL);
1596 GNUNET_assert(address != NULL);
1598 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1599 "Bandwidth assignment changed for peer %s \n",
1600 GNUNET_i2s (&address->peer));
1602 /* Notify performance clients about changes to address */
1603 GAS_performance_notify_all_clients (&address->peer, address->plugin,
1604 address->addr, address->addr_len, address->session_id, address->atsi,
1605 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1606 cur = handle->r_head;
1609 if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
1610 break; /* we have an address request pending*/
1615 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Nobody is interested in peer `%s' :(\n",
1616 GNUNET_i2s (&address->peer));
1620 if ((0 == ntohl (address->assigned_bw_in.value__))
1621 && (0 == ntohl (address->assigned_bw_out.value__)))
1623 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1624 "Telling transport to disconnect peer `%s'\n",
1625 GNUNET_i2s (&address->peer));
1629 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1630 "Sending bandwidth update for peer `%s': %llu %llu\n",
1631 GNUNET_i2s (&address->peer), address->assigned_bw_out,
1632 address->assigned_bw_out);
1635 /* *Notify scheduling clients about suggestion */
1636 GAS_scheduling_transmit_address_suggestion (&address->peer, address->plugin,
1637 address->addr, address->addr_len, address->session_id, address->atsi,
1638 address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1642 * Initialize address subsystem. The addresses subsystem manages the addresses
1643 * known and current performance information. It has a solver component
1644 * responsible for the resource allocation. It tells the solver about changes
1645 * and receives updates when the solver changes the resource allocation.
1647 * @param cfg configuration to use
1648 * @param stats the statistics handle to use
1649 * @return an address handle
1651 struct GAS_Addresses_Handle *
1652 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1653 const struct GNUNET_STATISTICS_Handle *stats)
1655 struct GAS_Addresses_Handle *ah;
1656 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1657 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1662 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1663 ah->running = GNUNET_NO;
1665 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1666 /* Initialize the addresses database */
1667 ah->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1668 GNUNET_assert(NULL != ah->addresses);
1670 /* Figure out configured solution method */
1672 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1674 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1675 "No resource assignment method configured, using proportional approach\n");
1676 ah->ats_mode = MODE_PROPORTIONAL;
1680 for (c = 0; c < strlen (mode_str); c++)
1681 mode_str[c] = toupper (mode_str[c]);
1682 if (0 == strcmp (mode_str, "PROPORTIONAL"))
1684 ah->ats_mode = MODE_PROPORTIONAL;
1685 plugin_short = "proportional";
1687 else if (0 == strcmp (mode_str, "MLP"))
1689 ah->ats_mode = MODE_MLP;
1690 plugin_short = "mlp";
1692 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1693 "Assignment method `%s' configured, but GLPK is not available, please install \n",
1695 ah->ats_mode = MODE_PROPORTIONAL;
1696 plugin_short = "proportional";
1699 else if (0 == strcmp (mode_str, "RIL"))
1701 ah->ats_mode = MODE_RIL;
1702 plugin_short = "ril";
1706 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1707 "Invalid resource assignment method `%s' configured, using proportional approach\n",
1709 ah->ats_mode = MODE_PROPORTIONAL;
1710 plugin_short = "proportional";
1712 GNUNET_free(mode_str);
1715 load_quotas (cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1716 ah->env.bandwidth_changed_cb = &bandwidth_changed_cb;
1717 ah->env.bw_changed_cb_cls = ah;
1718 ah->env.get_preferences = &get_preferences_cb;
1719 ah->env.get_preference_cls = ah;
1720 ah->env.get_property = &get_property_cb;
1721 ah->env.get_property_cls = ah;
1723 ah->env.stats = stats;
1724 ah->env.addresses = ah->addresses;
1726 ah->env.network_count = GNUNET_ATS_NetworkTypeCount;
1727 int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1728 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1730 ah->env.networks[c] = networks[c];
1731 ah->env.out_quota[c] = quotas_out[c];
1732 ah->env.in_quota[c] = quotas_in[c];
1735 GNUNET_asprintf (&ah->plugin, "libgnunet_plugin_ats_%s", plugin_short);
1736 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s '`%s'\n"), plugin_short, ah->plugin);
1737 if (NULL == (ah->solver = GNUNET_PLUGIN_load (ah->plugin, &ah->env)))
1739 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver `%s'!\n"), ah->plugin);
1743 GNUNET_assert (NULL != ah->env.sf.s_add);
1744 GNUNET_assert (NULL != ah->env.sf.s_address_update_inuse);
1745 GNUNET_assert (NULL != ah->env.sf.s_address_update_property);
1746 GNUNET_assert (NULL != ah->env.sf.s_address_update_session);
1747 GNUNET_assert (NULL != ah->env.sf.s_address_update_network);
1748 GNUNET_assert (NULL != ah->env.sf.s_get);
1749 GNUNET_assert (NULL != ah->env.sf.s_get_stop);
1750 GNUNET_assert (NULL != ah->env.sf.s_pref);
1751 GNUNET_assert (NULL != ah->env.sf.s_feedback);
1752 GNUNET_assert (NULL != ah->env.sf.s_del);
1753 GNUNET_assert (NULL != ah->env.sf.s_bulk_start);
1754 GNUNET_assert (NULL != ah->env.sf.s_bulk_stop);
1757 GAS_normalization_start (&normalized_preference_changed_cb, ah,
1758 &normalized_property_changed_cb, ah);
1760 if (NULL == ah->solver)
1762 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver!\n"));
1766 /* up and running */
1767 ah->running = GNUNET_YES;
1769 GNUNET_STATISTICS_set (ah->stat, "# addresses",
1770 GNUNET_CONTAINER_multipeermap_size (ah->addresses), GNUNET_NO);
1776 * Destroy all addresses iterator
1779 * @param key peer identity (unused)
1780 * @param value the 'struct ATS_Address' to free
1781 * @return #GNUNET_OK (continue to iterate)
1784 destroy_all_address_it (void *cls,
1785 const struct GNUNET_PeerIdentity *key,
1788 struct GAS_Addresses_Handle *handle = cls;
1789 struct ATS_Address *aa = value;
1792 GNUNET_assert(GNUNET_YES ==
1793 GNUNET_CONTAINER_multipeermap_remove (handle->addresses, key, value));
1795 handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1804 * Remove all addresses
1806 * @param handle the address handle to use
1809 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1811 if (GNUNET_NO == handle->running)
1814 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Destroying all addresses\n");
1815 handle->env.sf.s_bulk_start (handle->solver);
1816 if (handle->addresses != NULL )
1817 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
1818 &destroy_all_address_it,
1820 handle->env.sf.s_bulk_start (handle->solver);
1825 * Shutdown address subsystem.
1827 * @param handle the address handle to shutdown
1830 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1832 struct GAS_Addresses_Suggestion_Requests *cur;
1834 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Shutting down addresses\n");
1835 GNUNET_assert(NULL != handle);
1836 GAS_addresses_destroy_all (handle);
1837 handle->running = GNUNET_NO;
1838 GNUNET_CONTAINER_multipeermap_destroy (handle->addresses);
1839 handle->addresses = NULL;
1840 while (NULL != (cur = handle->r_head))
1842 GNUNET_CONTAINER_DLL_remove(handle->r_head, handle->r_tail, cur);
1846 GNUNET_PLUGIN_unload (handle->plugin, handle->solver);
1847 GNUNET_free (handle->plugin);
1848 GNUNET_free(handle);
1849 /* Stop configured solution method */
1850 GAS_normalization_stop ();
1854 struct PeerIteratorContext
1856 GNUNET_ATS_Peer_Iterator it;
1858 struct GNUNET_CONTAINER_MultiPeerMap *peers_returned;
1863 * Iterator to iterate over all peers
1865 * @param cls a PeerIteratorContext
1866 * @param key the peer id
1867 * @param value the ATS_address
1868 * @return #GNUNET_OK to continue
1872 const struct GNUNET_PeerIdentity *key,
1875 struct PeerIteratorContext *ip_ctx = cls;
1878 GNUNET_CONTAINER_multipeermap_contains (ip_ctx->peers_returned, key))
1880 GNUNET_CONTAINER_multipeermap_put (ip_ctx->peers_returned, key, NULL,
1881 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1882 ip_ctx->it (ip_ctx->it_cls, key);
1889 * Return information all peers currently known to ATS
1891 * @param handle the address handle to use
1892 * @param p_it the iterator to call for every peer
1893 * @param p_it_cls the closure for the iterator
1896 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
1897 GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1899 struct PeerIteratorContext ip_ctx;
1904 GNUNET_assert(NULL != handle->addresses);
1906 size = GNUNET_CONTAINER_multipeermap_size (handle->addresses);
1910 ip_ctx.it_cls = p_it_cls;
1911 ip_ctx.peers_returned = GNUNET_CONTAINER_multipeermap_create (size,
1913 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
1916 GNUNET_CONTAINER_multipeermap_destroy (ip_ctx.peers_returned);
1918 p_it (p_it_cls, NULL );
1921 struct PeerInfoIteratorContext
1923 GNUNET_ATS_PeerInfo_Iterator it;
1929 * Iterator to iterate over a peer's addresses
1931 * @param cls a `struct PeerInfoIteratorContext`
1932 * @param key the peer id
1933 * @param value the `struct ATS_address`
1934 * @return #GNUNET_OK to continue
1937 peerinfo_it (void *cls,
1938 const struct GNUNET_PeerIdentity *key,
1941 struct PeerInfoIteratorContext *pi_ctx = cls;
1942 struct ATS_Address *addr = value;
1944 if (NULL != pi_ctx->it)
1946 pi_ctx->it (pi_ctx->it_cls, &addr->peer, addr->plugin, addr->addr,
1947 addr->addr_len, addr->active, addr->atsi, addr->atsi_count,
1948 addr->assigned_bw_out, addr->assigned_bw_in);
1955 * Return information all peers currently known to ATS
1957 * @param handle the address handle to use
1958 * @param peer the respective peer
1959 * @param pi_it the iterator to call for every peer
1960 * @param pi_it_cls the closure for the iterator
1963 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
1964 const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it,
1967 struct PeerInfoIteratorContext pi_ctx;
1968 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1970 GNUNET_assert(NULL != peer);
1971 GNUNET_assert(NULL != handle->addresses);
1973 return; /* does not make sense without callback */
1975 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1977 pi_ctx.it_cls = pi_it_cls;
1979 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1981 &peerinfo_it, &pi_ctx);
1984 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw,
1989 /* end of gnunet-service-ats_addresses.c */