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_performance.h"
32 #include "gnunet-service-ats_scheduling.h"
33 #include "gnunet-service-ats_reservations.h"
35 #include "gnunet-service-ats_addresses_mlp.h"
37 #include "gnunet-service-ats_addresses_simplistic.h"
40 * ATS addresses : ATS address management
44 * This ATS addresses ("addresses") component manages the addresses known to ATS
45 * service and suggests addresses to transport service when it is interested in
46 * address suggestion for a peer. ATS addresses also instantiates the bandwidth assignment
47 * mechanism (solver), notifies it about changes to addresses and forwards changes
48 * to bandwidth assignments to transport, depending if transport is interested
52 * During initialization a hashmap to store addresses is created. The most
53 * important step is to load the configured solver using configuration
54 * "[ats]:MODE". Current solvers are MODE_SIMPLISTIC, MODE_MLP. Interaction
55 * is done using a solver API
60 * s_init: init the solver with required information
61 * s_add: add a new address
62 * s_update: update ATS values or session for an address
63 * s_get: get prefered address for a peer
64 * s_del: delete an address
65 * s_pref: change preference value for a peer
66 * s_done: shutdown solver
68 * addresses provides a bandwidth_changed_cb callback to the solver which is
69 * called when bandwidth assigned to peer has changed
72 * During shutdown all addresses are freed and the solver told to shutdown
74 * - Address management:
75 * Transport service notifies ATS about changes to the addresses known to him.
77 * -- Addresses and sessions
78 * Addresses consist of the address itself and a numerical session.
79 * When a new address without a session is added it has no session, so it gets
80 * session 0 assigned. When an address with a session is added and an address
81 * object with session 0 is found, this object is updated with the
82 * session otherwise a new address object with this session assigned is created.
85 * Addresses a1,a2 with session s1, s2 are "exact" if:
86 * (a1 == a2) && (s1 == s2)
87 * Addresses a1,a2 with session s1, s2 are "equivalent" if:
88 * (a1 == a2) && ((s1 == s2) || (s1 == 0) || (s2 ==0)
90 * -- Adding an address:
92 * When transport learns a new address it tells ATS and ATS is telling addresses
93 * about it using GAS_address_add. If not known to addresses it creates a new
94 * address object and calls solver's s_add. ATS information are deserialized
95 * and solver is notified about the session and ATS information using s_update.
97 * -- Updating an address
98 * Addresses does an lookup up for the existing address with the given session.
99 * If disassembles included ATS information and notifies the solver using
100 * s_update about the update.
102 * -- Deleting an address
103 * Addresses does an lookup for the exact address and session and if removes
104 * this address. If session != 0 the session is set to 0 and the address is
105 * kept. If session == 0, the addresses is removed.
107 * -- Requesting an address suggestion
108 * The address client issues a request address message to be notified about
109 * address suggestions for a specific peer. Addresses asks the solver with s_get.
110 * If no address is available, it will not send a response, otherwise it will
111 * respond with the choosen address.
113 * -- Address suggestions
114 * Addresses will notify the client automatically on any bandwidth_changed_cb
115 * by the solver if a address suggestion request is pending. If no address is
116 * available it will not respond at all If the client is not interested anymore,
117 * it has to cancel the address suggestion request.
119 * -- Suggestions blocks and reset
120 * After suggesting an address it is blocked for ATS_BLOCKING_DELTA sec. to
121 * prevent the client from being thrashed. If the client requires immediately
122 * it can reset this block using GAS_addresses_handle_backoff_reset.
125 * The client can notify addresses that it successfully uses an address and
126 * wants this address to be kept by calling GSA_address_in_use. Adresses
127 * will mark the address as used an notify the solver about the use.
129 * - Bandwidth assignment
131 * The addresses are used to perform resource allocation operations. ATS
132 * addresses takes care of instantiating the solver configured and notifies the
133 * respective solver about address changes and receives changes to the bandwidth
134 * assignment from the solver. The current bandwidth assignment is sent to
135 * transport. The specific solvers will be described in the specific section.
140 * - (updated address) || (address in use)
145 * - If you add a new address without a session, a new address with session 0
146 * will be added or an existing address with session 0 used
147 * - If you add this address again now with a session a, the existing address
148 * will be updated with this session
149 * - If you add this address again now with a session b, a new address object
150 * with this session will be added
152 * Destroying addresses:
154 * - If you destroy an address without a session, the address itself and all
155 * address instances with an session will be removed
156 * - If you destroy an address with a session, the session for this address
160 * Addresses without a session will be updated with a new session and if the
161 * the session is destroyed the session is removed and address itself still
162 * exists for suggestion
168 * Available ressource assignment modes
175 * Assign each peer an equal amount of bandwidth (bw)
177 * bw_per_peer = bw_total / #active addresses
184 * Solve ressource assignment as an optimization problem
185 * Uses an mixed integer programming solver
191 * Pending Address suggestion requests
193 struct GAS_Addresses_Suggestion_Requests
198 struct GAS_Addresses_Suggestion_Requests *next;
203 struct GAS_Addresses_Suggestion_Requests *prev;
208 struct GNUNET_PeerIdentity id;
212 * Handle for ATS address component
214 struct GAS_Addresses_Handle
219 struct GNUNET_STATISTICS_Handle *stat;
222 * A multihashmap to store all addresses
224 struct GNUNET_CONTAINER_MultiHashMap *addresses;
227 * Configure WAN quota in
229 unsigned long long wan_quota_in;
232 * Configure WAN quota out
234 unsigned long long wan_quota_out;
237 * Is ATS addresses running
242 * Configured ATS solver
252 * Address suggestion requests DLL head
254 struct GAS_Addresses_Suggestion_Requests *r_head;
257 * Address suggestion requests DLL tail
259 struct GAS_Addresses_Suggestion_Requests *r_tail;
261 /* Solver functions */
266 GAS_solver_init s_init;
269 * Add an address to the solver
271 GAS_solver_address_add s_add;
274 * Update address in solver
276 GAS_solver_address_update s_update;
279 * Get address from solver
281 GAS_solver_get_preferred_address s_get;
284 * Delete address in solver
286 GAS_solver_address_delete s_del;
289 * Change preference for quality in solver
291 GAS_solver_address_change_preference s_pref;
296 GAS_solver_done s_done;
301 assemble_ats_information (const struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
303 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
304 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
307 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
308 ats[0].value = aa->atsp_utilization_out.value__;
309 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
310 ats[1].value = aa->atsp_utilization_in.value__;
311 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
312 ats[2].value = ntohl(aa->atsp_network_type);
313 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
314 ats[3].value = ntohl(aa->atsp_latency.rel_value);
315 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
316 ats[4].value = ntohl(aa->atsp_distance);
317 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
318 ats[5].value = ntohl (aa->atsp_cost_wan);
319 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
320 ats[6].value = ntohl (aa->atsp_cost_lan);
321 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
322 ats[7].value = ntohl (aa->atsp_cost_wlan);
327 disassemble_ats_information (const struct GNUNET_ATS_Information *src,
329 struct ATS_Address *dest)
333 for (i = 0; i < ats_count; i++)
334 switch (ntohl (src[i].type))
336 case GNUNET_ATS_UTILIZATION_UP:
337 dest->atsp_utilization_out.value__ = src[i].value;
340 case GNUNET_ATS_UTILIZATION_DOWN:
341 dest->atsp_utilization_in.value__ = src[i].value;
344 case GNUNET_ATS_QUALITY_NET_DELAY:
345 dest->atsp_latency.rel_value = ntohl (src[i].value);
348 case GNUNET_ATS_QUALITY_NET_DISTANCE:
349 dest->atsp_distance = ntohl (src[i].value);
352 case GNUNET_ATS_COST_WAN:
353 dest->atsp_cost_wan = ntohl (src[i].value);
356 case GNUNET_ATS_COST_LAN:
357 dest->atsp_cost_lan = ntohl (src[i].value);
360 case GNUNET_ATS_COST_WLAN:
361 dest->atsp_cost_wlan = ntohl (src[i].value);
364 case GNUNET_ATS_NETWORK_TYPE:
365 dest->atsp_network_type = ntohl (src[i].value);
368 case GNUNET_ATS_ARRAY_TERMINATOR:
371 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
372 "Received unsupported ATS type %u\n", ntohl (src[i].type));
380 * Free the given address
381 * @param addr address to destroy
384 free_address (struct ATS_Address *addr)
386 GNUNET_free (addr->plugin);
391 * Create a ATS_address with the given information
393 * @param plugin_name plugin
394 * @param plugin_addr address
395 * @param plugin_addr_len address length
396 * @param session_id session
397 * @return the ATS_Address
399 static struct ATS_Address *
400 create_address (const struct GNUNET_PeerIdentity *peer,
401 const char *plugin_name,
402 const void *plugin_addr, size_t plugin_addr_len,
405 struct ATS_Address *aa = NULL;
407 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
409 aa->addr_len = plugin_addr_len;
411 memcpy (&aa[1], plugin_addr, plugin_addr_len);
412 aa->plugin = GNUNET_strdup (plugin_name);
413 aa->session_id = session_id;
414 aa->active = GNUNET_NO;
415 aa->used = GNUNET_NO;
416 aa->solver_information = NULL;
417 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
418 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
424 * Destroy the given address.
426 * @param handle the address handle
427 * @param addr address to destroy
428 * @return GNUNET_YES if bandwidth allocations should be recalcualted
431 destroy_address (struct GAS_Addresses_Handle *handle, struct ATS_Address *addr)
436 GNUNET_assert (GNUNET_YES ==
437 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
438 &addr->peer.hashPubKey,
445 struct CompareAddressContext
447 const struct ATS_Address *search;
449 /* exact_address != NULL if address and session is equal */
450 struct ATS_Address *exact_address;
451 /* exact_address != NULL if address and session is 0 */
452 struct ATS_Address *base_address;
457 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
459 struct CompareAddressContext *cac = cls;
460 struct ATS_Address *aa = value;
462 /* Find an matching exact address:
465 * aa->addr_len == cac->search->addr_len
466 * aa->plugin == cac->search->plugin
467 * aa->addr == cac->search->addr
468 * aa->session == cac->search->session
470 * return as exact address
472 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
474 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
475 cac->exact_address = aa;
478 /* Find an matching base address:
482 * aa->session_id == 0
485 * aa->addr_len == cac->search->addr_len
486 * aa->plugin == cac->search->plugin
487 * aa->addr == cac->search->addr
489 * return as base address
491 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
493 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
494 cac->base_address = aa;
497 /* Find an matching exact address based on session:
501 * cac->search->addr_len == 0
504 * aa->plugin == cac->search->plugin
505 * aa->session_id == cac->search->session_id
507 * return as exact address
509 if (0 == cac->search->addr_len)
511 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
512 cac->exact_address = aa;
515 if (cac->exact_address == NULL)
516 return GNUNET_YES; /* Continue iteration to find exact address */
518 return GNUNET_NO; /* Stop iteration since we have an exact address */
523 * Find an existing equivalent address record.
524 * Compares by peer identity and network address OR by session ID
525 * (one of the two must match).
527 * @param handle the address handle
528 * @param peer peer to lookup addresses for
529 * @param addr existing address record
530 * @return existing address record, NULL for none
533 find_equivalent_address (struct GAS_Addresses_Handle *handle,
534 const struct GNUNET_PeerIdentity *peer,
535 const struct ATS_Address *addr)
537 struct CompareAddressContext cac;
539 cac.exact_address = NULL;
540 cac.base_address = NULL;
542 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
543 &compare_address_it, &cac);
545 if (cac.exact_address == NULL)
546 return cac.base_address;
547 return cac.exact_address;
551 static struct ATS_Address *
552 lookup_address (struct GAS_Addresses_Handle *handle,
553 const struct GNUNET_PeerIdentity *peer,
554 const char *plugin_name,
555 const void *plugin_addr,
556 size_t plugin_addr_len,
558 const struct GNUNET_ATS_Information *atsi,
561 struct ATS_Address *aa;
562 struct ATS_Address *ea;
564 aa = create_address (peer,
566 plugin_addr, plugin_addr_len,
569 /* Get existing address or address with session == 0 */
570 ea = find_equivalent_address (handle, peer, aa);
576 else if (ea->session_id != session_id)
585 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
586 const struct GNUNET_PeerIdentity *peer,
587 const char *plugin_name, const void *plugin_addr,
588 size_t plugin_addr_len, uint32_t session_id,
589 const struct GNUNET_ATS_Information *atsi,
592 struct ATS_Address *aa;
593 struct ATS_Address *ea;
594 unsigned int ats_res;
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597 "Received `%s' for peer `%s'\n",
601 if (GNUNET_NO == handle->running)
604 GNUNET_assert (NULL != handle->addresses);
606 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
608 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, aa)))
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611 "While adding address: had %u ATS elements to add, could only add %u\n",
612 atsi_count, ats_res);
615 /* Get existing address or address with session == 0 */
616 ea = find_equivalent_address (handle, peer, aa);
619 /* We have a new address */
620 GNUNET_assert (GNUNET_OK ==
621 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
622 &peer->hashPubKey, aa,
623 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
625 GNUNET_i2s (peer), session_id, aa);
626 /* Tell solver about new address */
627 handle->s_add (handle->solver, handle->addresses, aa);
630 GNUNET_free (aa->plugin);
633 if (ea->session_id != 0)
635 /* This address with the same session is already existing
636 * Should not happen */
637 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
638 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
639 GNUNET_i2s (peer), plugin_name, session_id);
644 /* We have an address without an session, update this address */
646 /* Notify solver about update with atsi information and session */
647 handle->s_update (handle->solver, handle->addresses, ea, session_id, ea->used, atsi, atsi_count);
650 ea->session_id = session_id;
651 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, ea)))
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "While updating address: had %u ATS elements to add, could only add %u\n",
655 atsi_count, ats_res);
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
658 "Updated existing address for peer `%s' %p with new session %u\n",
659 GNUNET_i2s (peer), ea, session_id);
664 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
665 const struct GNUNET_PeerIdentity *peer,
666 const char *plugin_name, const void *plugin_addr,
667 size_t plugin_addr_len, uint32_t session_id,
668 const struct GNUNET_ATS_Information *atsi,
671 struct ATS_Address *aa;
674 if (GNUNET_NO == handle->running)
677 GNUNET_assert (NULL != handle->addresses);
679 /* Get existing address */
680 aa = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
681 session_id, atsi, atsi_count);
684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
685 GNUNET_i2s (peer), plugin_name, session_id);
690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
691 "Received `%s' for peer `%s' address \n",
693 GNUNET_i2s (peer), aa);
695 /* Tell solver about update */
696 handle->s_update (handle->solver, handle->addresses, aa, session_id, aa->used, atsi, atsi_count);
699 if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, aa)))
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "While adding address: had %u ATS elements to add, could only add %u\n",
703 atsi_count, ats_res);
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
706 "Updated %u ATS elements for address %p\n",
711 struct DestroyContext
713 struct ATS_Address *aa;
715 struct GAS_Addresses_Handle *handle;
718 * GNUNET_NO : full address
719 * GNUNET_YES : just session
728 * If session != 0, just the session is deleted, the address itself still exists
729 * If session == 0, remove full address
730 * If session == 0 and addrlen == 0, destroy inbound address
734 * @param value the 'struct ATS_Address'
735 * @return GNUNET_OK (continue to iterate)
738 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
740 struct DestroyContext *dc = cls;
741 struct GAS_Addresses_Handle *handle = dc->handle;
742 const struct ATS_Address *des = dc->aa;
743 struct ATS_Address *aa = value;
745 GNUNET_assert (0 == memcmp (&aa->peer, &des->peer,
746 sizeof (struct GNUNET_PeerIdentity)));
749 if (des->session_id == 0)
751 /* Session == 0, remove full address */
752 if ((0 == strcmp (des->plugin, aa->plugin)) &&
753 (aa->addr_len == des->addr_len) &&
754 (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
758 "Deleting full address for peer `%s' session %u %p\n",
759 GNUNET_i2s (&aa->peer), aa->session_id, aa);
761 /* Notify solver about deletion */
762 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
763 destroy_address (handle, aa);
764 dc->result = GNUNET_NO;
765 return GNUNET_OK; /* Continue iteration */
770 /* Session != 0, just remove session */
771 if (aa->session_id != des->session_id)
772 return GNUNET_OK; /* irrelevant */
774 if ((aa->session_id != 0) &&
775 (0 != strcmp (des->plugin, aa->plugin)))
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
778 "Different plugins during removal: `%s' vs `%s' \n",
779 des->plugin, aa->plugin);
784 if (aa->addr_len == 0)
786 /* Inbound connection died, delete full address */
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
788 "Deleting inbound address for peer `%s': `%s' session %u\n",
789 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
791 /* Notify solver about deletion */
792 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
793 destroy_address (handle, aa);
794 dc->result = GNUNET_NO;
795 return GNUNET_OK; /* Continue iteration */
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Deleting session for peer `%s': `%s' %u\n",
802 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
803 /* Notify solver to delete session */
804 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_YES);
813 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
814 const struct GNUNET_PeerIdentity *peer,
815 const char *plugin_name, const void *plugin_addr,
816 size_t plugin_addr_len, uint32_t session_id)
818 struct ATS_Address *ea;
819 struct DestroyContext dc;
821 if (GNUNET_NO == handle->running)
824 /* Get existing address */
825 ea = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
826 session_id, NULL, 0);
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "Received `%s' for peer `%s' address %p session %u\n",
831 GNUNET_i2s (peer), ea, session_id);
835 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
836 GNUNET_i2s (peer), plugin_name, session_id);
840 GNUNET_break (0 < strlen (plugin_name));
842 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
844 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
845 &destroy_by_session_id, &dc);
846 free_address (dc.aa);
851 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
852 const struct GNUNET_PeerIdentity *peer,
853 const char *plugin_name, const void *plugin_addr,
854 size_t plugin_addr_len, uint32_t session_id, int in_use)
856 struct ATS_Address *ea;
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Received `%s' for peer `%s'\n",
863 if (GNUNET_NO == handle->running)
864 return GNUNET_SYSERR;
866 ea = lookup_address (handle, peer, plugin_name,
867 plugin_addr, plugin_addr_len,
868 session_id, NULL, 0);
871 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
872 "Trying to set unknown address `%s', %s %u %s \n",
874 plugin_name, session_id,
875 (GNUNET_NO == in_use) ? "NO" : "YES");
877 return GNUNET_SYSERR;
879 if (ea->used == in_use)
882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
883 "Address in use called multiple times for peer `%s': %s -> %s \n",
885 (GNUNET_NO == ea->used) ? "NO" : "YES",
886 (GNUNET_NO == in_use) ? "NO" : "YES");
887 return GNUNET_SYSERR;
890 /* Tell solver about update */
891 handle->s_update (handle->solver, handle->addresses, ea, session_id, in_use, NULL, 0);
899 * Cancel address suggestions for a peer
901 * @param handle the address handle
902 * @param peer the respective peer
905 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
906 const struct GNUNET_PeerIdentity *peer)
908 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911 "Received request: `%s' for peer %s\n", "request_address_cancel", GNUNET_i2s (peer));
915 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
922 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
923 "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
926 GAS_addresses_handle_backoff_reset (handle, peer);
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928 "Removed request pending for peer `%s\n", GNUNET_i2s (peer));
929 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
935 * Add an address suggestions for a peer
937 * @param handle the address handle
938 * @param peer the respective peer
941 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
942 const struct GNUNET_PeerIdentity *peer)
944 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
945 struct ATS_Address *aa;
946 struct GNUNET_ATS_Information *ats;
947 unsigned int ats_count;
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "Received `%s' for peer `%s'\n",
954 if (GNUNET_NO == handle->running)
958 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
959 break; /* already suggesting */
964 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
966 GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
969 /* Get prefered address from solver */
970 aa = (struct ATS_Address *) handle->s_get (handle->solver, handle->addresses, peer);
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
981 ats_count = assemble_ats_information (aa, &ats);
982 GAS_scheduling_transmit_address_suggestion (peer,
984 aa->addr, aa->addr_len,
990 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
991 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 "Address %p ready for suggestion, block interval now %llu \n",
995 aa, aa->block_interval);
1002 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1004 struct ATS_Address *aa = value;
1006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1007 "Resetting interval for peer `%s' address %p from %llu to 0\n",
1008 GNUNET_i2s (&aa->peer), aa, aa->block_interval);
1010 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1011 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1017 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1018 const struct GNUNET_PeerIdentity *peer)
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021 "Received `%s' for peer `%s'\n",
1025 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
1033 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
1035 const struct GNUNET_PeerIdentity *peer,
1036 enum GNUNET_ATS_PreferenceKind kind,
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1040 "Received `%s' for peer `%s' for client %p\n",
1041 "CHANGE PREFERENCE",
1042 GNUNET_i2s (peer), client);
1044 if (GNUNET_NO == handle->running)
1047 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->addresses,
1050 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1051 "Received `%s' for unknown peer `%s' from client %p\n",
1052 "CHANGE PREFERENCE",
1053 GNUNET_i2s (peer), client);
1057 /* Tell solver about update */
1058 handle->s_pref (handle->solver, client, peer, kind, score);
1063 * Load quotas for networks from configuration
1065 * @param cfg configuration handle
1066 * @param out_dest where to write outbound quotas
1067 * @param in_dest where to write inbound quotas
1068 * @param dest_length length of inbound and outbound arrays
1069 * @return number of networks loaded
1072 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1074 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1075 char * entry_in = NULL;
1076 char * entry_out = NULL;
1077 char * quota_out_str;
1078 char * quota_in_str;
1082 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1086 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1087 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1090 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
1093 if (0 == strcmp(quota_out_str, BIG_M_STRING))
1095 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1098 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
1100 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out, &out_dest[c])))
1103 if (GNUNET_NO == res)
1105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1106 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1107 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1111 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Outbound quota configure for network `%s' is %llu\n"),
1112 network_str[c], out_dest[c]);
1114 GNUNET_free (quota_out_str);
1118 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1119 network_str[c], GNUNET_ATS_DefaultBandwidth);
1120 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1124 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
1127 if (0 == strcmp(quota_in_str, BIG_M_STRING))
1129 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1132 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1134 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in, &in_dest[c])))
1137 if (GNUNET_NO == res)
1139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1140 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1141 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1145 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Inbound quota configured for network `%s' is %llu\n"),
1146 network_str[c], in_dest[c]);
1148 GNUNET_free (quota_in_str);
1152 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1153 network_str[c], GNUNET_ATS_DefaultBandwidth);
1154 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c], in_dest[c], out_dest[c]);
1157 GNUNET_free (entry_out);
1158 GNUNET_free (entry_in);
1160 return GNUNET_ATS_NetworkTypeCount;
1165 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1167 struct GAS_Addresses_Handle *handle = cls;
1168 struct GAS_Addresses_Suggestion_Requests *cur;
1170 GNUNET_assert (handle != NULL);
1171 GNUNET_assert (address != NULL);
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bandwidth assignment changed for peer %s \n", GNUNET_i2s(&address->peer));
1174 struct GNUNET_ATS_Information *ats;
1175 unsigned int ats_count;
1177 cur = handle->r_head;
1180 if (0 == memcmp (&address->peer, &cur->id, sizeof (cur->id)))
1181 break; /* we have an address request pending*/
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187 "Nobody is interested in peer `%s' :(\n",GNUNET_i2s (&address->peer));
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1192 "Sending bandwidth update for peer `%s'\n",GNUNET_i2s (&address->peer));
1194 ats_count = assemble_ats_information (address, &ats);
1195 GAS_scheduling_transmit_address_suggestion (&address->peer,
1197 address->addr, address->addr_len,
1198 address->session_id,
1200 address->assigned_bw_out,
1201 address->assigned_bw_in);
1207 * Initialize address subsystem.
1209 * @param cfg configuration to use
1210 * @param stats the statistics handle to use
1212 struct GAS_Addresses_Handle *
1213 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1214 const struct GNUNET_STATISTICS_Handle *stats)
1216 struct GAS_Addresses_Handle *ah;
1217 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1218 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1219 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1224 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1225 ah->running = GNUNET_NO;
1227 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1228 /* Initialize the addresses database */
1229 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1230 GNUNET_assert (NULL != ah->addresses);
1232 /* Figure out configured solution method */
1233 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1236 ah->ats_mode = MODE_SIMPLISTIC;
1240 for (c = 0; c < strlen (mode_str); c++)
1241 mode_str[c] = toupper (mode_str[c]);
1242 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1244 ah->ats_mode = MODE_SIMPLISTIC;
1246 else if (0 == strcmp (mode_str, "MLP"))
1248 ah->ats_mode = MODE_MLP;
1250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1251 ah->ats_mode = MODE_SIMPLISTIC;
1256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1257 ah->ats_mode = MODE_SIMPLISTIC;
1259 GNUNET_free (mode_str);
1261 /* Start configured solution method */
1262 switch (ah->ats_mode)
1265 /* Init the MLP solver with default values */
1267 ah->ats_mode = MODE_MLP;
1268 ah->s_init = &GAS_mlp_init;
1269 ah->s_add = &GAS_mlp_address_add;
1270 ah->s_update = &GAS_mlp_address_update;
1271 ah->s_get = &GAS_mlp_get_preferred_address;
1272 ah->s_pref = &GAS_mlp_address_change_preference;
1273 ah->s_del = &GAS_mlp_address_delete;
1274 ah->s_done = &GAS_mlp_done;
1280 case MODE_SIMPLISTIC:
1281 /* Init the simplistic solver with default values */
1282 ah->ats_mode = MODE_SIMPLISTIC;
1283 ah->s_init = &GAS_simplistic_init;
1284 ah->s_add = &GAS_simplistic_address_add;
1285 ah->s_update = &GAS_simplistic_address_update;
1286 ah->s_get = &GAS_simplistic_get_preferred_address;
1287 ah->s_pref = &GAS_simplistic_address_change_preference;
1288 ah->s_del = &GAS_simplistic_address_delete;
1289 ah->s_done = &GAS_simplistic_done;
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1297 GNUNET_assert (NULL != ah->s_init);
1298 GNUNET_assert (NULL != ah->s_add);
1299 GNUNET_assert (NULL != ah->s_update);
1300 GNUNET_assert (NULL != ah->s_get);
1301 GNUNET_assert (NULL != ah->s_pref);
1302 GNUNET_assert (NULL != ah->s_del);
1303 GNUNET_assert (NULL != ah->s_done);
1305 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1307 ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count, &bandwidth_changed_cb, ah);
1308 if (NULL == ah->solver)
1310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
1315 /* up and running */
1316 ah->running = GNUNET_YES;
1322 * Free memory of address.
1325 * @param key peer identity (unused)
1326 * @param value the 'struct ATS_Address' to free
1327 * @return GNUNET_OK (continue to iterate)
1330 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1332 struct GAS_Addresses_Handle *handle = cls;
1333 struct ATS_Address *aa = value;
1334 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
1335 destroy_address (handle, aa);
1341 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1343 if (GNUNET_NO == handle->running)
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1350 if (handle->addresses != NULL)
1351 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, handle);
1356 * Shutdown address subsystem.
1359 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1361 struct GAS_Addresses_Suggestion_Requests *cur;
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "Shutting down addresses\n");
1365 GNUNET_assert (NULL != handle);
1366 GAS_addresses_destroy_all (handle);
1367 handle->running = GNUNET_NO;
1368 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1369 handle->addresses = NULL;
1370 while (NULL != (cur = handle->r_head))
1372 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1375 handle->s_done (handle->solver);
1376 GNUNET_free (handle);
1377 /* Stop configured solution method */
1381 struct PeerIteratorContext
1383 GNUNET_ATS_Peer_Iterator it;
1385 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1390 const struct GNUNET_HashCode * key,
1393 struct PeerIteratorContext *ip_ctx = cls;
1394 struct GNUNET_PeerIdentity tmp;
1396 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1398 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1399 tmp.hashPubKey = (*key);
1400 ip_ctx->it (ip_ctx->it_cls, &tmp);
1407 * Return all peers currently known to ATS
1409 * @param handle the address handle
1410 * @param p_it the iterator to call for every peer, callbach with id == NULL
1412 * @param p_it_cls the closure for the iterator
1415 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle, GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1417 struct PeerIteratorContext ip_ctx;
1422 GNUNET_assert (NULL != handle->addresses);
1424 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1428 ip_ctx.it_cls = p_it_cls;
1429 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1430 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1431 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1433 p_it (p_it_cls, NULL);
1436 struct PeerInfoIteratorContext
1438 GNUNET_ATS_PeerInfo_Iterator it;
1444 peerinfo_it (void *cls,
1445 const struct GNUNET_HashCode * key,
1448 struct PeerInfoIteratorContext *pi_ctx = cls;
1449 struct ATS_Address *addr = (struct ATS_Address *) value;
1450 struct GNUNET_ATS_Information *ats;
1453 if (NULL != pi_ctx->it)
1455 ats_count = assemble_ats_information (addr, &ats);
1457 pi_ctx->it (pi_ctx->it_cls,
1460 addr->addr, addr->addr_len,
1463 addr->assigned_bw_out,
1464 addr->assigned_bw_in);
1472 * Return all peers currently known to ATS
1474 * @param handle the address handle
1475 * @param peer the respective peer
1476 * @param pi_it the iterator to call for every peer
1477 * @param pi_it_cls the closure for the iterator
1480 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
1481 const struct GNUNET_PeerIdentity *peer,
1482 GNUNET_ATS_PeerInfo_Iterator pi_it,
1485 struct PeerInfoIteratorContext pi_ctx;
1486 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1487 GNUNET_assert (NULL != peer);
1488 GNUNET_assert (NULL != handle->addresses);
1490 return; /* does not make sense without callback */
1492 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1494 pi_ctx.it_cls = pi_it_cls;
1496 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1499 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1504 /* end of gnunet-service-ats_addresses.c */