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 component manages the addresses known to ATS service and suggests
45 * addresses to transport service when it is interested in address suggestions
46 * for a peer. ATS addresses also instantiates the bandwidth assignment
47 * mechanism, notifies it about changes to addresses and forwards changes
48 * to bandwidth assignments to transport, depending if transport is interested
57 * - Address management and suggestions:
59 * Transport service notifies ATS about changes to the addresses known to him.
61 * When transport learns a new address it tells ATS about it using
62 * GAS_address_add. It creates a new address object and tells the solver using
64 * add it. ATS informationen are extracted given t
68 * - Bandwidth assignment
70 * The addresses are used to perform resource allocation operations. ATS
71 * addresses takes care of instantiating the solver configured and notifies the
72 * respective solver about address changes and receives changes to the bandwidth
73 * assignment from the solver. The current bandwidth assignment is sent to
82 * - If you add a new address without a session, a new address will be added
83 * - If you add this address again now with a session a, the existing address
84 * will be updated with this session
85 * - If you add this address again now with a session b, a new address object
86 * with this session will be added
88 * Destroying addresses:
90 * - If you destroy an address without a session, the address itself and all
91 * address instances with an session will be removed
92 * - If you destroy an address with a session, the session for this address
96 * Addresses without a session will be updated with a new session and if the
97 * the session is destroyed the session is removed and address itself still
98 * exists for suggestion
104 * Available ressource assignment modes
111 * Assign each peer an equal amount of bandwidth (bw)
113 * bw_per_peer = bw_total / #active addresses
120 * Solve ressource assignment as an optimization problem
121 * Uses an mixed integer programming solver
127 * Pending Address suggestion requests
129 struct GAS_Addresses_Suggestion_Requests
134 struct GAS_Addresses_Suggestion_Requests *next;
139 struct GAS_Addresses_Suggestion_Requests *prev;
144 struct GNUNET_PeerIdentity id;
148 * Handle for ATS address component
150 struct GAS_Addresses_Handle
155 struct GNUNET_STATISTICS_Handle *stat;
158 * A multihashmap to store all addresses
160 struct GNUNET_CONTAINER_MultiHashMap *addresses;
163 * Configure WAN quota in
165 unsigned long long wan_quota_in;
168 * Configure WAN quota out
170 unsigned long long wan_quota_out;
173 * Is ATS addresses running
178 * Configured ATS solver
188 * Address suggestion requests DLL head
190 struct GAS_Addresses_Suggestion_Requests *r_head;
193 * Address suggestion requests DLL tail
195 struct GAS_Addresses_Suggestion_Requests *r_tail;
197 /* Solver functions */
202 GAS_solver_init s_init;
205 * Add an address to the solver
207 GAS_solver_address_add s_add;
210 * Update address in solver
212 GAS_solver_address_update s_update;
215 * Get address from solver
217 GAS_solver_get_preferred_address s_get;
220 * Delete address in solver
222 GAS_solver_address_delete s_del;
225 * Change preference for quality in solver
227 GAS_solver_address_change_preference s_pref;
232 GAS_solver_done s_done;
237 assemble_ats_information (const struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
239 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
240 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
243 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
244 ats[0].value = aa->atsp_utilization_out.value__;
245 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
246 ats[1].value = aa->atsp_utilization_in.value__;
247 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
248 ats[2].value = ntohl(aa->atsp_network_type);
249 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
250 ats[3].value = ntohl(aa->atsp_latency.rel_value);
251 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
252 ats[4].value = ntohl(aa->atsp_distance);
253 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
254 ats[5].value = ntohl (aa->atsp_cost_wan);
255 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
256 ats[6].value = ntohl (aa->atsp_cost_lan);
257 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
258 ats[7].value = ntohl (aa->atsp_cost_wlan);
263 disassemble_ats_information (const struct GNUNET_ATS_Information *src,
265 struct ATS_Address *dest)
269 for (i = 0; i < ats_count; i++)
270 switch (ntohl (src[i].type))
272 case GNUNET_ATS_UTILIZATION_UP:
273 dest->atsp_utilization_out.value__ = src[i].value;
276 case GNUNET_ATS_UTILIZATION_DOWN:
277 dest->atsp_utilization_in.value__ = src[i].value;
280 case GNUNET_ATS_QUALITY_NET_DELAY:
281 dest->atsp_latency.rel_value = ntohl (src[i].value);
284 case GNUNET_ATS_QUALITY_NET_DISTANCE:
285 dest->atsp_distance = ntohl (src[i].value);
288 case GNUNET_ATS_COST_WAN:
289 dest->atsp_cost_wan = ntohl (src[i].value);
292 case GNUNET_ATS_COST_LAN:
293 dest->atsp_cost_lan = ntohl (src[i].value);
296 case GNUNET_ATS_COST_WLAN:
297 dest->atsp_cost_wlan = ntohl (src[i].value);
300 case GNUNET_ATS_NETWORK_TYPE:
301 dest->atsp_network_type = ntohl (src[i].value);
304 case GNUNET_ATS_ARRAY_TERMINATOR:
307 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
308 "Received unsupported ATS type %u\n", ntohl (src[i].type));
316 * Free the given address
317 * @param addr address to destroy
320 free_address (struct ATS_Address *addr)
322 GNUNET_free (addr->plugin);
327 * Create a ATS_address with the given information
329 * @param plugin_name plugin
330 * @param plugin_addr address
331 * @param plugin_addr_len address length
332 * @param session_id session
333 * @return the ATS_Address
335 static struct ATS_Address *
336 create_address (const struct GNUNET_PeerIdentity *peer,
337 const char *plugin_name,
338 const void *plugin_addr, size_t plugin_addr_len,
341 struct ATS_Address *aa = NULL;
343 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
345 aa->addr_len = plugin_addr_len;
347 memcpy (&aa[1], plugin_addr, plugin_addr_len);
348 aa->plugin = GNUNET_strdup (plugin_name);
349 aa->session_id = session_id;
350 aa->active = GNUNET_NO;
351 aa->used = GNUNET_NO;
352 aa->solver_information = NULL;
353 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
354 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
360 * Destroy the given address.
362 * @param handle the address handle
363 * @param addr address to destroy
364 * @return GNUNET_YES if bandwidth allocations should be recalcualted
367 destroy_address (struct GAS_Addresses_Handle *handle, struct ATS_Address *addr)
372 GNUNET_assert (GNUNET_YES ==
373 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
374 &addr->peer.hashPubKey,
381 struct CompareAddressContext
383 const struct ATS_Address *search;
385 /* exact_address != NULL if address and session is equal */
386 struct ATS_Address *exact_address;
387 /* exact_address != NULL if address and session is 0 */
388 struct ATS_Address *base_address;
393 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
395 struct CompareAddressContext *cac = cls;
396 struct ATS_Address *aa = value;
398 /* Find an matching exact address:
401 * aa->addr_len == cac->search->addr_len
402 * aa->plugin == cac->search->plugin
403 * aa->addr == cac->search->addr
404 * aa->session == cac->search->session
406 * return as exact address
408 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
410 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
411 cac->exact_address = aa;
414 /* Find an matching base address:
418 * aa->session_id == 0
421 * aa->addr_len == cac->search->addr_len
422 * aa->plugin == cac->search->plugin
423 * aa->addr == cac->search->addr
425 * return as base address
427 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
429 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
430 cac->base_address = aa;
433 /* Find an matching exact address based on session:
437 * cac->search->addr_len == 0
440 * aa->plugin == cac->search->plugin
441 * aa->session_id == cac->search->session_id
443 * return as exact address
445 if (0 == cac->search->addr_len)
447 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
448 cac->exact_address = aa;
451 if (cac->exact_address == NULL)
452 return GNUNET_YES; /* Continue iteration to find exact address */
454 return GNUNET_NO; /* Stop iteration since we have an exact address */
459 * Find an existing equivalent address record.
460 * Compares by peer identity and network address OR by session ID
461 * (one of the two must match).
463 * @param handle the address handle
464 * @param peer peer to lookup addresses for
465 * @param addr existing address record
466 * @return existing address record, NULL for none
469 find_equivalent_address (struct GAS_Addresses_Handle *handle,
470 const struct GNUNET_PeerIdentity *peer,
471 const struct ATS_Address *addr)
473 struct CompareAddressContext cac;
475 cac.exact_address = NULL;
476 cac.base_address = NULL;
478 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
479 &compare_address_it, &cac);
481 if (cac.exact_address == NULL)
482 return cac.base_address;
483 return cac.exact_address;
487 static struct ATS_Address *
488 lookup_address (struct GAS_Addresses_Handle *handle,
489 const struct GNUNET_PeerIdentity *peer,
490 const char *plugin_name,
491 const void *plugin_addr,
492 size_t plugin_addr_len,
494 const struct GNUNET_ATS_Information *atsi,
497 struct ATS_Address *aa;
498 struct ATS_Address *ea;
500 aa = create_address (peer,
502 plugin_addr, plugin_addr_len,
505 /* Get existing address or address with session == 0 */
506 ea = find_equivalent_address (handle, peer, aa);
512 else if (ea->session_id != session_id)
521 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
522 const struct GNUNET_PeerIdentity *peer,
523 const char *plugin_name, const void *plugin_addr,
524 size_t plugin_addr_len, uint32_t session_id,
525 const struct GNUNET_ATS_Information *atsi,
528 struct ATS_Address *aa;
529 struct ATS_Address *ea;
530 unsigned int ats_res;
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "Received `%s' for peer `%s'\n",
537 if (GNUNET_NO == handle->running)
540 GNUNET_assert (NULL != handle->addresses);
542 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
544 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, aa)))
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "While adding address: had %u ATS elements to add, could only add %u\n",
548 atsi_count, ats_res);
551 /* Get existing address or address with session == 0 */
552 ea = find_equivalent_address (handle, peer, aa);
555 /* We have a new address */
556 GNUNET_assert (GNUNET_OK ==
557 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
558 &peer->hashPubKey, aa,
559 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
561 GNUNET_i2s (peer), session_id, aa);
562 /* Tell solver about new address */
563 handle->s_add (handle->solver, handle->addresses, aa);
566 GNUNET_free (aa->plugin);
569 if (ea->session_id != 0)
571 /* This address with the same session is already existing
572 * Should not happen */
573 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
574 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
575 GNUNET_i2s (peer), plugin_name, session_id);
580 /* We have an address without an session, update this address */
582 /* Notify solver about update with atsi information and session */
583 handle->s_update (handle->solver, handle->addresses, ea, session_id, ea->used, atsi, atsi_count);
586 ea->session_id = session_id;
587 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, ea)))
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "While updating address: had %u ATS elements to add, could only add %u\n",
591 atsi_count, ats_res);
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
594 "Updated existing address for peer `%s' %p with new session %u\n",
595 GNUNET_i2s (peer), ea, session_id);
600 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
601 const struct GNUNET_PeerIdentity *peer,
602 const char *plugin_name, const void *plugin_addr,
603 size_t plugin_addr_len, uint32_t session_id,
604 const struct GNUNET_ATS_Information *atsi,
607 struct ATS_Address *aa;
610 if (GNUNET_NO == handle->running)
613 GNUNET_assert (NULL != handle->addresses);
615 /* Get existing address */
616 aa = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
617 session_id, atsi, atsi_count);
620 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
621 GNUNET_i2s (peer), plugin_name, session_id);
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "Received `%s' for peer `%s' address \n",
629 GNUNET_i2s (peer), aa);
631 /* Tell solver about update */
632 handle->s_update (handle->solver, handle->addresses, aa, session_id, aa->used, atsi, atsi_count);
635 if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, aa)))
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "While adding address: had %u ATS elements to add, could only add %u\n",
639 atsi_count, ats_res);
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "Updated %u ATS elements for address %p\n",
647 struct DestroyContext
649 struct ATS_Address *aa;
651 struct GAS_Addresses_Handle *handle;
654 * GNUNET_NO : full address
655 * GNUNET_YES : just session
664 * If session != 0, just the session is deleted, the address itself still exists
665 * If session == 0, remove full address
666 * If session == 0 and addrlen == 0, destroy inbound address
670 * @param value the 'struct ATS_Address'
671 * @return GNUNET_OK (continue to iterate)
674 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
676 struct DestroyContext *dc = cls;
677 struct GAS_Addresses_Handle *handle = dc->handle;
678 const struct ATS_Address *des = dc->aa;
679 struct ATS_Address *aa = value;
681 GNUNET_assert (0 == memcmp (&aa->peer, &des->peer,
682 sizeof (struct GNUNET_PeerIdentity)));
685 if (des->session_id == 0)
687 /* Session == 0, remove full address */
688 if ((0 == strcmp (des->plugin, aa->plugin)) &&
689 (aa->addr_len == des->addr_len) &&
690 (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694 "Deleting full address for peer `%s' session %u %p\n",
695 GNUNET_i2s (&aa->peer), aa->session_id, aa);
697 /* Notify solver about deletion */
698 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
699 destroy_address (handle, aa);
700 dc->result = GNUNET_NO;
701 return GNUNET_OK; /* Continue iteration */
706 /* Session != 0, just remove session */
707 if (aa->session_id != des->session_id)
708 return GNUNET_OK; /* irrelevant */
710 if ((aa->session_id != 0) &&
711 (0 != strcmp (des->plugin, aa->plugin)))
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Different plugins during removal: `%s' vs `%s' \n",
715 des->plugin, aa->plugin);
720 if (aa->addr_len == 0)
722 /* Inbound connection died, delete full address */
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "Deleting inbound address for peer `%s': `%s' session %u\n",
725 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
727 /* Notify solver about deletion */
728 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
729 destroy_address (handle, aa);
730 dc->result = GNUNET_NO;
731 return GNUNET_OK; /* Continue iteration */
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 "Deleting session for peer `%s': `%s' %u\n",
738 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
739 /* Notify solver to delete session */
740 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_YES);
749 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
750 const struct GNUNET_PeerIdentity *peer,
751 const char *plugin_name, const void *plugin_addr,
752 size_t plugin_addr_len, uint32_t session_id)
754 struct ATS_Address *ea;
755 struct DestroyContext dc;
757 if (GNUNET_NO == handle->running)
760 /* Get existing address */
761 ea = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
762 session_id, NULL, 0);
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "Received `%s' for peer `%s' address %p session %u\n",
767 GNUNET_i2s (peer), ea, session_id);
771 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
772 GNUNET_i2s (peer), plugin_name, session_id);
776 GNUNET_break (0 < strlen (plugin_name));
778 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
780 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
781 &destroy_by_session_id, &dc);
782 free_address (dc.aa);
787 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
788 const struct GNUNET_PeerIdentity *peer,
789 const char *plugin_name, const void *plugin_addr,
790 size_t plugin_addr_len, uint32_t session_id, int in_use)
792 struct ATS_Address *ea;
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
795 "Received `%s' for peer `%s'\n",
799 if (GNUNET_NO == handle->running)
800 return GNUNET_SYSERR;
802 ea = lookup_address (handle, peer, plugin_name,
803 plugin_addr, plugin_addr_len,
804 session_id, NULL, 0);
807 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
808 "Trying to set unknown address `%s', %s %u %s \n",
810 plugin_name, session_id,
811 (GNUNET_NO == in_use) ? "NO" : "YES");
813 return GNUNET_SYSERR;
815 if (ea->used == in_use)
818 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
819 "Address in use called multiple times for peer `%s': %s -> %s \n",
821 (GNUNET_NO == ea->used) ? "NO" : "YES",
822 (GNUNET_NO == in_use) ? "NO" : "YES");
823 return GNUNET_SYSERR;
826 /* Tell solver about update */
827 handle->s_update (handle->solver, handle->addresses, ea, session_id, in_use, NULL, 0);
835 * Cancel address suggestions for a peer
837 * @param handle the address handle
838 * @param peer the respective peer
841 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
842 const struct GNUNET_PeerIdentity *peer)
844 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
847 "Received request: `%s' for peer %s\n", "request_address_cancel", GNUNET_i2s (peer));
851 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
858 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
859 "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
862 GAS_addresses_handle_backoff_reset (handle, peer);
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Removed request pending for peer `%s\n", GNUNET_i2s (peer));
865 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
871 * Add an address suggestions for a peer
873 * @param handle the address handle
874 * @param peer the respective peer
877 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
878 const struct GNUNET_PeerIdentity *peer)
880 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
881 struct ATS_Address *aa;
882 struct GNUNET_ATS_Information *ats;
883 unsigned int ats_count;
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "Received `%s' for peer `%s'\n",
890 if (GNUNET_NO == handle->running)
894 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
895 break; /* already suggesting */
900 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
902 GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
905 /* Get prefered address from solver */
906 aa = (struct ATS_Address *) handle->s_get (handle->solver, handle->addresses, peer);
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
910 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
915 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
917 ats_count = assemble_ats_information (aa, &ats);
918 GAS_scheduling_transmit_address_suggestion (peer,
920 aa->addr, aa->addr_len,
926 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
927 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 "Address %p ready for suggestion, block interval now %llu \n",
931 aa, aa->block_interval);
938 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
940 struct ATS_Address *aa = value;
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 "Resetting interval for peer `%s' address %p from %llu to 0\n",
944 GNUNET_i2s (&aa->peer), aa, aa->block_interval);
946 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
947 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
953 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
954 const struct GNUNET_PeerIdentity *peer)
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 "Received `%s' for peer `%s'\n",
961 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
969 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
971 const struct GNUNET_PeerIdentity *peer,
972 enum GNUNET_ATS_PreferenceKind kind,
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
976 "Received `%s' for peer `%s' for client %p\n",
978 GNUNET_i2s (peer), client);
980 if (GNUNET_NO == handle->running)
983 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->addresses,
986 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
987 "Received `%s' for unknown peer `%s' from client %p\n",
989 GNUNET_i2s (peer), client);
993 /* Tell solver about update */
994 handle->s_pref (handle->solver, client, peer, kind, score);
999 * Load quotas for networks from configuration
1001 * @param cfg configuration handle
1002 * @param out_dest where to write outbound quotas
1003 * @param in_dest where to write inbound quotas
1004 * @param dest_length length of inbound and outbound arrays
1005 * @return number of networks loaded
1008 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1010 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1011 char * entry_in = NULL;
1012 char * entry_out = NULL;
1013 char * quota_out_str;
1014 char * quota_in_str;
1018 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1022 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1023 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1026 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
1029 if (0 == strcmp(quota_out_str, BIG_M_STRING))
1031 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1034 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
1036 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out, &out_dest[c])))
1039 if (GNUNET_NO == res)
1041 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1042 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1043 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1047 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Outbound quota configure for network `%s' is %llu\n"),
1048 network_str[c], out_dest[c]);
1050 GNUNET_free (quota_out_str);
1054 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1055 network_str[c], GNUNET_ATS_DefaultBandwidth);
1056 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1060 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
1063 if (0 == strcmp(quota_in_str, BIG_M_STRING))
1065 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1068 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1070 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in, &in_dest[c])))
1073 if (GNUNET_NO == res)
1075 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1076 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1077 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1081 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Inbound quota configured for network `%s' is %llu\n"),
1082 network_str[c], in_dest[c]);
1084 GNUNET_free (quota_in_str);
1088 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1089 network_str[c], GNUNET_ATS_DefaultBandwidth);
1090 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1092 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]);
1093 GNUNET_free (entry_out);
1094 GNUNET_free (entry_in);
1096 return GNUNET_ATS_NetworkTypeCount;
1101 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1103 struct GAS_Addresses_Handle *handle = cls;
1104 struct GAS_Addresses_Suggestion_Requests *cur;
1106 GNUNET_assert (handle != NULL);
1107 GNUNET_assert (address != NULL);
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bandwidth assignment changed for peer %s \n", GNUNET_i2s(&address->peer));
1110 struct GNUNET_ATS_Information *ats;
1111 unsigned int ats_count;
1113 cur = handle->r_head;
1116 if (0 == memcmp (&address->peer, &cur->id, sizeof (cur->id)))
1117 break; /* we have an address request pending*/
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1123 "Nobody is interested in peer `%s' :(\n",GNUNET_i2s (&address->peer));
1127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1128 "Sending bandwidth update for peer `%s'\n",GNUNET_i2s (&address->peer));
1130 ats_count = assemble_ats_information (address, &ats);
1131 GAS_scheduling_transmit_address_suggestion (&address->peer,
1133 address->addr, address->addr_len,
1134 address->session_id,
1136 address->assigned_bw_out,
1137 address->assigned_bw_in);
1143 * Initialize address subsystem.
1145 * @param cfg configuration to use
1146 * @param stats the statistics handle to use
1148 struct GAS_Addresses_Handle *
1149 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1150 const struct GNUNET_STATISTICS_Handle *stats)
1152 struct GAS_Addresses_Handle *ah;
1153 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1154 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1155 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1160 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1161 ah->running = GNUNET_NO;
1163 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1164 /* Initialize the addresses database */
1165 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1166 GNUNET_assert (NULL != ah->addresses);
1168 /* Figure out configured solution method */
1169 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1172 ah->ats_mode = MODE_SIMPLISTIC;
1176 for (c = 0; c < strlen (mode_str); c++)
1177 mode_str[c] = toupper (mode_str[c]);
1178 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1180 ah->ats_mode = MODE_SIMPLISTIC;
1182 else if (0 == strcmp (mode_str, "MLP"))
1184 ah->ats_mode = MODE_MLP;
1186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1187 ah->ats_mode = MODE_SIMPLISTIC;
1192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1193 ah->ats_mode = MODE_SIMPLISTIC;
1195 GNUNET_free (mode_str);
1197 /* Start configured solution method */
1198 switch (ah->ats_mode)
1201 /* Init the MLP solver with default values */
1203 ah->ats_mode = MODE_MLP;
1204 ah->s_init = &GAS_mlp_init;
1205 ah->s_add = &GAS_mlp_address_add;
1206 ah->s_update = &GAS_mlp_address_update;
1207 ah->s_get = &GAS_mlp_get_preferred_address;
1208 ah->s_pref = &GAS_mlp_address_change_preference;
1209 ah->s_del = &GAS_mlp_address_delete;
1210 ah->s_done = &GAS_mlp_done;
1216 case MODE_SIMPLISTIC:
1217 /* Init the simplistic solver with default values */
1218 ah->ats_mode = MODE_SIMPLISTIC;
1219 ah->s_init = &GAS_simplistic_init;
1220 ah->s_add = &GAS_simplistic_address_add;
1221 ah->s_update = &GAS_simplistic_address_update;
1222 ah->s_get = &GAS_simplistic_get_preferred_address;
1223 ah->s_pref = &GAS_simplistic_address_change_preference;
1224 ah->s_del = &GAS_simplistic_address_delete;
1225 ah->s_done = &GAS_simplistic_done;
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1233 GNUNET_assert (NULL != ah->s_init);
1234 GNUNET_assert (NULL != ah->s_add);
1235 GNUNET_assert (NULL != ah->s_update);
1236 GNUNET_assert (NULL != ah->s_get);
1237 GNUNET_assert (NULL != ah->s_pref);
1238 GNUNET_assert (NULL != ah->s_del);
1239 GNUNET_assert (NULL != ah->s_done);
1241 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1243 ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count, &bandwidth_changed_cb, ah);
1244 if (NULL == ah->solver)
1246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
1251 /* up and running */
1252 ah->running = GNUNET_YES;
1258 * Free memory of address.
1261 * @param key peer identity (unused)
1262 * @param value the 'struct ATS_Address' to free
1263 * @return GNUNET_OK (continue to iterate)
1266 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1268 struct GAS_Addresses_Handle *handle = cls;
1269 struct ATS_Address *aa = value;
1270 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
1271 destroy_address (handle, aa);
1277 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1279 if (GNUNET_NO == handle->running)
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1286 if (handle->addresses != NULL)
1287 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, handle);
1292 * Shutdown address subsystem.
1295 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1297 struct GAS_Addresses_Suggestion_Requests *cur;
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300 "Shutting down addresses\n");
1301 GNUNET_assert (NULL != handle);
1302 GAS_addresses_destroy_all (handle);
1303 handle->running = GNUNET_NO;
1304 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1305 handle->addresses = NULL;
1306 while (NULL != (cur = handle->r_head))
1308 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1311 handle->s_done (handle->solver);
1312 GNUNET_free (handle);
1313 /* Stop configured solution method */
1317 struct PeerIteratorContext
1319 GNUNET_ATS_Peer_Iterator it;
1321 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1326 const struct GNUNET_HashCode * key,
1329 struct PeerIteratorContext *ip_ctx = cls;
1330 struct GNUNET_PeerIdentity tmp;
1332 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1334 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1335 tmp.hashPubKey = (*key);
1336 ip_ctx->it (ip_ctx->it_cls, &tmp);
1343 * Return all peers currently known to ATS
1345 * @param handle the address handle
1346 * @param p_it the iterator to call for every peer, callbach with id == NULL
1348 * @param p_it_cls the closure for the iterator
1351 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle, GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1353 struct PeerIteratorContext ip_ctx;
1358 GNUNET_assert (NULL != handle->addresses);
1360 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1364 ip_ctx.it_cls = p_it_cls;
1365 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1366 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1367 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1369 p_it (p_it_cls, NULL);
1372 struct PeerInfoIteratorContext
1374 GNUNET_ATS_PeerInfo_Iterator it;
1380 peerinfo_it (void *cls,
1381 const struct GNUNET_HashCode * key,
1384 struct PeerInfoIteratorContext *pi_ctx = cls;
1385 struct ATS_Address *addr = (struct ATS_Address *) value;
1386 struct GNUNET_ATS_Information *ats;
1389 if (NULL != pi_ctx->it)
1391 ats_count = assemble_ats_information (addr, &ats);
1393 pi_ctx->it (pi_ctx->it_cls,
1396 addr->addr, addr->addr_len,
1399 addr->assigned_bw_out,
1400 addr->assigned_bw_in);
1408 * Return all peers currently known to ATS
1410 * @param handle the address handle
1411 * @param peer the respective peer
1412 * @param pi_it the iterator to call for every peer
1413 * @param pi_it_cls the closure for the iterator
1416 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
1417 const struct GNUNET_PeerIdentity *peer,
1418 GNUNET_ATS_PeerInfo_Iterator pi_it,
1421 struct PeerInfoIteratorContext pi_ctx;
1422 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1423 GNUNET_assert (NULL != peer);
1424 GNUNET_assert (NULL != handle->addresses);
1426 return; /* does not make sense without callback */
1428 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1430 pi_ctx.it_cls = pi_it_cls;
1432 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1435 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1440 /* end of gnunet-service-ats_addresses.c */