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 addresswithout 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
105 * -- Requesting an address suggestion
108 * - Address suggestions:
110 * - Bandwidth assignment
112 * The addresses are used to perform resource allocation operations. ATS
113 * addresses takes care of instantiating the solver configured and notifies the
114 * respective solver about address changes and receives changes to the bandwidth
115 * assignment from the solver. The current bandwidth assignment is sent to
124 * - If you add a new address without a session, a new address will be added
125 * - If you add this address again now with a session a, the existing address
126 * will be updated with this session
127 * - If you add this address again now with a session b, a new address object
128 * with this session will be added
130 * Destroying addresses:
132 * - If you destroy an address without a session, the address itself and all
133 * address instances with an session will be removed
134 * - If you destroy an address with a session, the session for this address
138 * Addresses without a session will be updated with a new session and if the
139 * the session is destroyed the session is removed and address itself still
140 * exists for suggestion
146 * Available ressource assignment modes
153 * Assign each peer an equal amount of bandwidth (bw)
155 * bw_per_peer = bw_total / #active addresses
162 * Solve ressource assignment as an optimization problem
163 * Uses an mixed integer programming solver
169 * Pending Address suggestion requests
171 struct GAS_Addresses_Suggestion_Requests
176 struct GAS_Addresses_Suggestion_Requests *next;
181 struct GAS_Addresses_Suggestion_Requests *prev;
186 struct GNUNET_PeerIdentity id;
190 * Handle for ATS address component
192 struct GAS_Addresses_Handle
197 struct GNUNET_STATISTICS_Handle *stat;
200 * A multihashmap to store all addresses
202 struct GNUNET_CONTAINER_MultiHashMap *addresses;
205 * Configure WAN quota in
207 unsigned long long wan_quota_in;
210 * Configure WAN quota out
212 unsigned long long wan_quota_out;
215 * Is ATS addresses running
220 * Configured ATS solver
230 * Address suggestion requests DLL head
232 struct GAS_Addresses_Suggestion_Requests *r_head;
235 * Address suggestion requests DLL tail
237 struct GAS_Addresses_Suggestion_Requests *r_tail;
239 /* Solver functions */
244 GAS_solver_init s_init;
247 * Add an address to the solver
249 GAS_solver_address_add s_add;
252 * Update address in solver
254 GAS_solver_address_update s_update;
257 * Get address from solver
259 GAS_solver_get_preferred_address s_get;
262 * Delete address in solver
264 GAS_solver_address_delete s_del;
267 * Change preference for quality in solver
269 GAS_solver_address_change_preference s_pref;
274 GAS_solver_done s_done;
279 assemble_ats_information (const struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
281 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
282 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
285 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
286 ats[0].value = aa->atsp_utilization_out.value__;
287 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
288 ats[1].value = aa->atsp_utilization_in.value__;
289 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
290 ats[2].value = ntohl(aa->atsp_network_type);
291 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
292 ats[3].value = ntohl(aa->atsp_latency.rel_value);
293 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
294 ats[4].value = ntohl(aa->atsp_distance);
295 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
296 ats[5].value = ntohl (aa->atsp_cost_wan);
297 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
298 ats[6].value = ntohl (aa->atsp_cost_lan);
299 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
300 ats[7].value = ntohl (aa->atsp_cost_wlan);
305 disassemble_ats_information (const struct GNUNET_ATS_Information *src,
307 struct ATS_Address *dest)
311 for (i = 0; i < ats_count; i++)
312 switch (ntohl (src[i].type))
314 case GNUNET_ATS_UTILIZATION_UP:
315 dest->atsp_utilization_out.value__ = src[i].value;
318 case GNUNET_ATS_UTILIZATION_DOWN:
319 dest->atsp_utilization_in.value__ = src[i].value;
322 case GNUNET_ATS_QUALITY_NET_DELAY:
323 dest->atsp_latency.rel_value = ntohl (src[i].value);
326 case GNUNET_ATS_QUALITY_NET_DISTANCE:
327 dest->atsp_distance = ntohl (src[i].value);
330 case GNUNET_ATS_COST_WAN:
331 dest->atsp_cost_wan = ntohl (src[i].value);
334 case GNUNET_ATS_COST_LAN:
335 dest->atsp_cost_lan = ntohl (src[i].value);
338 case GNUNET_ATS_COST_WLAN:
339 dest->atsp_cost_wlan = ntohl (src[i].value);
342 case GNUNET_ATS_NETWORK_TYPE:
343 dest->atsp_network_type = ntohl (src[i].value);
346 case GNUNET_ATS_ARRAY_TERMINATOR:
349 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
350 "Received unsupported ATS type %u\n", ntohl (src[i].type));
358 * Free the given address
359 * @param addr address to destroy
362 free_address (struct ATS_Address *addr)
364 GNUNET_free (addr->plugin);
369 * Create a ATS_address with the given information
371 * @param plugin_name plugin
372 * @param plugin_addr address
373 * @param plugin_addr_len address length
374 * @param session_id session
375 * @return the ATS_Address
377 static struct ATS_Address *
378 create_address (const struct GNUNET_PeerIdentity *peer,
379 const char *plugin_name,
380 const void *plugin_addr, size_t plugin_addr_len,
383 struct ATS_Address *aa = NULL;
385 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
387 aa->addr_len = plugin_addr_len;
389 memcpy (&aa[1], plugin_addr, plugin_addr_len);
390 aa->plugin = GNUNET_strdup (plugin_name);
391 aa->session_id = session_id;
392 aa->active = GNUNET_NO;
393 aa->used = GNUNET_NO;
394 aa->solver_information = NULL;
395 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
396 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
402 * Destroy the given address.
404 * @param handle the address handle
405 * @param addr address to destroy
406 * @return GNUNET_YES if bandwidth allocations should be recalcualted
409 destroy_address (struct GAS_Addresses_Handle *handle, struct ATS_Address *addr)
414 GNUNET_assert (GNUNET_YES ==
415 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
416 &addr->peer.hashPubKey,
423 struct CompareAddressContext
425 const struct ATS_Address *search;
427 /* exact_address != NULL if address and session is equal */
428 struct ATS_Address *exact_address;
429 /* exact_address != NULL if address and session is 0 */
430 struct ATS_Address *base_address;
435 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
437 struct CompareAddressContext *cac = cls;
438 struct ATS_Address *aa = value;
440 /* Find an matching exact address:
443 * aa->addr_len == cac->search->addr_len
444 * aa->plugin == cac->search->plugin
445 * aa->addr == cac->search->addr
446 * aa->session == cac->search->session
448 * return as exact address
450 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
452 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
453 cac->exact_address = aa;
456 /* Find an matching base address:
460 * aa->session_id == 0
463 * aa->addr_len == cac->search->addr_len
464 * aa->plugin == cac->search->plugin
465 * aa->addr == cac->search->addr
467 * return as base address
469 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
471 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
472 cac->base_address = aa;
475 /* Find an matching exact address based on session:
479 * cac->search->addr_len == 0
482 * aa->plugin == cac->search->plugin
483 * aa->session_id == cac->search->session_id
485 * return as exact address
487 if (0 == cac->search->addr_len)
489 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
490 cac->exact_address = aa;
493 if (cac->exact_address == NULL)
494 return GNUNET_YES; /* Continue iteration to find exact address */
496 return GNUNET_NO; /* Stop iteration since we have an exact address */
501 * Find an existing equivalent address record.
502 * Compares by peer identity and network address OR by session ID
503 * (one of the two must match).
505 * @param handle the address handle
506 * @param peer peer to lookup addresses for
507 * @param addr existing address record
508 * @return existing address record, NULL for none
511 find_equivalent_address (struct GAS_Addresses_Handle *handle,
512 const struct GNUNET_PeerIdentity *peer,
513 const struct ATS_Address *addr)
515 struct CompareAddressContext cac;
517 cac.exact_address = NULL;
518 cac.base_address = NULL;
520 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
521 &compare_address_it, &cac);
523 if (cac.exact_address == NULL)
524 return cac.base_address;
525 return cac.exact_address;
529 static struct ATS_Address *
530 lookup_address (struct GAS_Addresses_Handle *handle,
531 const struct GNUNET_PeerIdentity *peer,
532 const char *plugin_name,
533 const void *plugin_addr,
534 size_t plugin_addr_len,
536 const struct GNUNET_ATS_Information *atsi,
539 struct ATS_Address *aa;
540 struct ATS_Address *ea;
542 aa = create_address (peer,
544 plugin_addr, plugin_addr_len,
547 /* Get existing address or address with session == 0 */
548 ea = find_equivalent_address (handle, peer, aa);
554 else if (ea->session_id != session_id)
563 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
564 const struct GNUNET_PeerIdentity *peer,
565 const char *plugin_name, const void *plugin_addr,
566 size_t plugin_addr_len, uint32_t session_id,
567 const struct GNUNET_ATS_Information *atsi,
570 struct ATS_Address *aa;
571 struct ATS_Address *ea;
572 unsigned int ats_res;
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "Received `%s' for peer `%s'\n",
579 if (GNUNET_NO == handle->running)
582 GNUNET_assert (NULL != handle->addresses);
584 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
586 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, aa)))
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "While adding address: had %u ATS elements to add, could only add %u\n",
590 atsi_count, ats_res);
593 /* Get existing address or address with session == 0 */
594 ea = find_equivalent_address (handle, peer, aa);
597 /* We have a new address */
598 GNUNET_assert (GNUNET_OK ==
599 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
600 &peer->hashPubKey, aa,
601 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
603 GNUNET_i2s (peer), session_id, aa);
604 /* Tell solver about new address */
605 handle->s_add (handle->solver, handle->addresses, aa);
608 GNUNET_free (aa->plugin);
611 if (ea->session_id != 0)
613 /* This address with the same session is already existing
614 * Should not happen */
615 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
616 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
617 GNUNET_i2s (peer), plugin_name, session_id);
622 /* We have an address without an session, update this address */
624 /* Notify solver about update with atsi information and session */
625 handle->s_update (handle->solver, handle->addresses, ea, session_id, ea->used, atsi, atsi_count);
628 ea->session_id = session_id;
629 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, ea)))
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "While updating address: had %u ATS elements to add, could only add %u\n",
633 atsi_count, ats_res);
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Updated existing address for peer `%s' %p with new session %u\n",
637 GNUNET_i2s (peer), ea, session_id);
642 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
643 const struct GNUNET_PeerIdentity *peer,
644 const char *plugin_name, const void *plugin_addr,
645 size_t plugin_addr_len, uint32_t session_id,
646 const struct GNUNET_ATS_Information *atsi,
649 struct ATS_Address *aa;
652 if (GNUNET_NO == handle->running)
655 GNUNET_assert (NULL != handle->addresses);
657 /* Get existing address */
658 aa = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
659 session_id, atsi, atsi_count);
662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
663 GNUNET_i2s (peer), plugin_name, session_id);
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 "Received `%s' for peer `%s' address \n",
671 GNUNET_i2s (peer), aa);
673 /* Tell solver about update */
674 handle->s_update (handle->solver, handle->addresses, aa, session_id, aa->used, atsi, atsi_count);
677 if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, aa)))
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "While adding address: had %u ATS elements to add, could only add %u\n",
681 atsi_count, ats_res);
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684 "Updated %u ATS elements for address %p\n",
689 struct DestroyContext
691 struct ATS_Address *aa;
693 struct GAS_Addresses_Handle *handle;
696 * GNUNET_NO : full address
697 * GNUNET_YES : just session
706 * If session != 0, just the session is deleted, the address itself still exists
707 * If session == 0, remove full address
708 * If session == 0 and addrlen == 0, destroy inbound address
712 * @param value the 'struct ATS_Address'
713 * @return GNUNET_OK (continue to iterate)
716 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
718 struct DestroyContext *dc = cls;
719 struct GAS_Addresses_Handle *handle = dc->handle;
720 const struct ATS_Address *des = dc->aa;
721 struct ATS_Address *aa = value;
723 GNUNET_assert (0 == memcmp (&aa->peer, &des->peer,
724 sizeof (struct GNUNET_PeerIdentity)));
727 if (des->session_id == 0)
729 /* Session == 0, remove full address */
730 if ((0 == strcmp (des->plugin, aa->plugin)) &&
731 (aa->addr_len == des->addr_len) &&
732 (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Deleting full address for peer `%s' session %u %p\n",
737 GNUNET_i2s (&aa->peer), aa->session_id, aa);
739 /* Notify solver about deletion */
740 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
741 destroy_address (handle, aa);
742 dc->result = GNUNET_NO;
743 return GNUNET_OK; /* Continue iteration */
748 /* Session != 0, just remove session */
749 if (aa->session_id != des->session_id)
750 return GNUNET_OK; /* irrelevant */
752 if ((aa->session_id != 0) &&
753 (0 != strcmp (des->plugin, aa->plugin)))
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "Different plugins during removal: `%s' vs `%s' \n",
757 des->plugin, aa->plugin);
762 if (aa->addr_len == 0)
764 /* Inbound connection died, delete full address */
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Deleting inbound address for peer `%s': `%s' session %u\n",
767 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
769 /* Notify solver about deletion */
770 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
771 destroy_address (handle, aa);
772 dc->result = GNUNET_NO;
773 return GNUNET_OK; /* Continue iteration */
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "Deleting session for peer `%s': `%s' %u\n",
780 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
781 /* Notify solver to delete session */
782 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_YES);
791 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
792 const struct GNUNET_PeerIdentity *peer,
793 const char *plugin_name, const void *plugin_addr,
794 size_t plugin_addr_len, uint32_t session_id)
796 struct ATS_Address *ea;
797 struct DestroyContext dc;
799 if (GNUNET_NO == handle->running)
802 /* Get existing address */
803 ea = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
804 session_id, NULL, 0);
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
807 "Received `%s' for peer `%s' address %p session %u\n",
809 GNUNET_i2s (peer), ea, session_id);
813 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
814 GNUNET_i2s (peer), plugin_name, session_id);
818 GNUNET_break (0 < strlen (plugin_name));
820 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
822 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
823 &destroy_by_session_id, &dc);
824 free_address (dc.aa);
829 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
830 const struct GNUNET_PeerIdentity *peer,
831 const char *plugin_name, const void *plugin_addr,
832 size_t plugin_addr_len, uint32_t session_id, int in_use)
834 struct ATS_Address *ea;
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
837 "Received `%s' for peer `%s'\n",
841 if (GNUNET_NO == handle->running)
842 return GNUNET_SYSERR;
844 ea = lookup_address (handle, peer, plugin_name,
845 plugin_addr, plugin_addr_len,
846 session_id, NULL, 0);
849 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
850 "Trying to set unknown address `%s', %s %u %s \n",
852 plugin_name, session_id,
853 (GNUNET_NO == in_use) ? "NO" : "YES");
855 return GNUNET_SYSERR;
857 if (ea->used == in_use)
860 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
861 "Address in use called multiple times for peer `%s': %s -> %s \n",
863 (GNUNET_NO == ea->used) ? "NO" : "YES",
864 (GNUNET_NO == in_use) ? "NO" : "YES");
865 return GNUNET_SYSERR;
868 /* Tell solver about update */
869 handle->s_update (handle->solver, handle->addresses, ea, session_id, in_use, NULL, 0);
877 * Cancel address suggestions for a peer
879 * @param handle the address handle
880 * @param peer the respective peer
883 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
884 const struct GNUNET_PeerIdentity *peer)
886 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "Received request: `%s' for peer %s\n", "request_address_cancel", GNUNET_i2s (peer));
893 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
900 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
901 "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
904 GAS_addresses_handle_backoff_reset (handle, peer);
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "Removed request pending for peer `%s\n", GNUNET_i2s (peer));
907 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
913 * Add an address suggestions for a peer
915 * @param handle the address handle
916 * @param peer the respective peer
919 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
920 const struct GNUNET_PeerIdentity *peer)
922 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
923 struct ATS_Address *aa;
924 struct GNUNET_ATS_Information *ats;
925 unsigned int ats_count;
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928 "Received `%s' for peer `%s'\n",
932 if (GNUNET_NO == handle->running)
936 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
937 break; /* already suggesting */
942 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
944 GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
947 /* Get prefered address from solver */
948 aa = (struct ATS_Address *) handle->s_get (handle->solver, handle->addresses, peer);
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
959 ats_count = assemble_ats_information (aa, &ats);
960 GAS_scheduling_transmit_address_suggestion (peer,
962 aa->addr, aa->addr_len,
968 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
969 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 "Address %p ready for suggestion, block interval now %llu \n",
973 aa, aa->block_interval);
980 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
982 struct ATS_Address *aa = value;
984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
985 "Resetting interval for peer `%s' address %p from %llu to 0\n",
986 GNUNET_i2s (&aa->peer), aa, aa->block_interval);
988 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
989 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
995 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
996 const struct GNUNET_PeerIdentity *peer)
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "Received `%s' for peer `%s'\n",
1003 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
1011 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
1013 const struct GNUNET_PeerIdentity *peer,
1014 enum GNUNET_ATS_PreferenceKind kind,
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "Received `%s' for peer `%s' for client %p\n",
1019 "CHANGE PREFERENCE",
1020 GNUNET_i2s (peer), client);
1022 if (GNUNET_NO == handle->running)
1025 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->addresses,
1028 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1029 "Received `%s' for unknown peer `%s' from client %p\n",
1030 "CHANGE PREFERENCE",
1031 GNUNET_i2s (peer), client);
1035 /* Tell solver about update */
1036 handle->s_pref (handle->solver, client, peer, kind, score);
1041 * Load quotas for networks from configuration
1043 * @param cfg configuration handle
1044 * @param out_dest where to write outbound quotas
1045 * @param in_dest where to write inbound quotas
1046 * @param dest_length length of inbound and outbound arrays
1047 * @return number of networks loaded
1050 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1052 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1053 char * entry_in = NULL;
1054 char * entry_out = NULL;
1055 char * quota_out_str;
1056 char * quota_in_str;
1060 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1064 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1065 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1068 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
1071 if (0 == strcmp(quota_out_str, BIG_M_STRING))
1073 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1076 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
1078 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out, &out_dest[c])))
1081 if (GNUNET_NO == res)
1083 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1084 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1085 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1089 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Outbound quota configure for network `%s' is %llu\n"),
1090 network_str[c], out_dest[c]);
1092 GNUNET_free (quota_out_str);
1096 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1097 network_str[c], GNUNET_ATS_DefaultBandwidth);
1098 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1102 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
1105 if (0 == strcmp(quota_in_str, BIG_M_STRING))
1107 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1110 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1112 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in, &in_dest[c])))
1115 if (GNUNET_NO == res)
1117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1118 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1119 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1123 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Inbound quota configured for network `%s' is %llu\n"),
1124 network_str[c], in_dest[c]);
1126 GNUNET_free (quota_in_str);
1130 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1131 network_str[c], GNUNET_ATS_DefaultBandwidth);
1132 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1134 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]);
1135 GNUNET_free (entry_out);
1136 GNUNET_free (entry_in);
1138 return GNUNET_ATS_NetworkTypeCount;
1143 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1145 struct GAS_Addresses_Handle *handle = cls;
1146 struct GAS_Addresses_Suggestion_Requests *cur;
1148 GNUNET_assert (handle != NULL);
1149 GNUNET_assert (address != NULL);
1151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bandwidth assignment changed for peer %s \n", GNUNET_i2s(&address->peer));
1152 struct GNUNET_ATS_Information *ats;
1153 unsigned int ats_count;
1155 cur = handle->r_head;
1158 if (0 == memcmp (&address->peer, &cur->id, sizeof (cur->id)))
1159 break; /* we have an address request pending*/
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165 "Nobody is interested in peer `%s' :(\n",GNUNET_i2s (&address->peer));
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Sending bandwidth update for peer `%s'\n",GNUNET_i2s (&address->peer));
1172 ats_count = assemble_ats_information (address, &ats);
1173 GAS_scheduling_transmit_address_suggestion (&address->peer,
1175 address->addr, address->addr_len,
1176 address->session_id,
1178 address->assigned_bw_out,
1179 address->assigned_bw_in);
1185 * Initialize address subsystem.
1187 * @param cfg configuration to use
1188 * @param stats the statistics handle to use
1190 struct GAS_Addresses_Handle *
1191 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1192 const struct GNUNET_STATISTICS_Handle *stats)
1194 struct GAS_Addresses_Handle *ah;
1195 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1196 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1197 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1202 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1203 ah->running = GNUNET_NO;
1205 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1206 /* Initialize the addresses database */
1207 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1208 GNUNET_assert (NULL != ah->addresses);
1210 /* Figure out configured solution method */
1211 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1213 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1214 ah->ats_mode = MODE_SIMPLISTIC;
1218 for (c = 0; c < strlen (mode_str); c++)
1219 mode_str[c] = toupper (mode_str[c]);
1220 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1222 ah->ats_mode = MODE_SIMPLISTIC;
1224 else if (0 == strcmp (mode_str, "MLP"))
1226 ah->ats_mode = MODE_MLP;
1228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1229 ah->ats_mode = MODE_SIMPLISTIC;
1234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1235 ah->ats_mode = MODE_SIMPLISTIC;
1237 GNUNET_free (mode_str);
1239 /* Start configured solution method */
1240 switch (ah->ats_mode)
1243 /* Init the MLP solver with default values */
1245 ah->ats_mode = MODE_MLP;
1246 ah->s_init = &GAS_mlp_init;
1247 ah->s_add = &GAS_mlp_address_add;
1248 ah->s_update = &GAS_mlp_address_update;
1249 ah->s_get = &GAS_mlp_get_preferred_address;
1250 ah->s_pref = &GAS_mlp_address_change_preference;
1251 ah->s_del = &GAS_mlp_address_delete;
1252 ah->s_done = &GAS_mlp_done;
1258 case MODE_SIMPLISTIC:
1259 /* Init the simplistic solver with default values */
1260 ah->ats_mode = MODE_SIMPLISTIC;
1261 ah->s_init = &GAS_simplistic_init;
1262 ah->s_add = &GAS_simplistic_address_add;
1263 ah->s_update = &GAS_simplistic_address_update;
1264 ah->s_get = &GAS_simplistic_get_preferred_address;
1265 ah->s_pref = &GAS_simplistic_address_change_preference;
1266 ah->s_del = &GAS_simplistic_address_delete;
1267 ah->s_done = &GAS_simplistic_done;
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1275 GNUNET_assert (NULL != ah->s_init);
1276 GNUNET_assert (NULL != ah->s_add);
1277 GNUNET_assert (NULL != ah->s_update);
1278 GNUNET_assert (NULL != ah->s_get);
1279 GNUNET_assert (NULL != ah->s_pref);
1280 GNUNET_assert (NULL != ah->s_del);
1281 GNUNET_assert (NULL != ah->s_done);
1283 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1285 ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count, &bandwidth_changed_cb, ah);
1286 if (NULL == ah->solver)
1288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
1293 /* up and running */
1294 ah->running = GNUNET_YES;
1300 * Free memory of address.
1303 * @param key peer identity (unused)
1304 * @param value the 'struct ATS_Address' to free
1305 * @return GNUNET_OK (continue to iterate)
1308 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1310 struct GAS_Addresses_Handle *handle = cls;
1311 struct ATS_Address *aa = value;
1312 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
1313 destroy_address (handle, aa);
1319 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1321 if (GNUNET_NO == handle->running)
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1328 if (handle->addresses != NULL)
1329 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, handle);
1334 * Shutdown address subsystem.
1337 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1339 struct GAS_Addresses_Suggestion_Requests *cur;
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1342 "Shutting down addresses\n");
1343 GNUNET_assert (NULL != handle);
1344 GAS_addresses_destroy_all (handle);
1345 handle->running = GNUNET_NO;
1346 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1347 handle->addresses = NULL;
1348 while (NULL != (cur = handle->r_head))
1350 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1353 handle->s_done (handle->solver);
1354 GNUNET_free (handle);
1355 /* Stop configured solution method */
1359 struct PeerIteratorContext
1361 GNUNET_ATS_Peer_Iterator it;
1363 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1368 const struct GNUNET_HashCode * key,
1371 struct PeerIteratorContext *ip_ctx = cls;
1372 struct GNUNET_PeerIdentity tmp;
1374 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1376 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1377 tmp.hashPubKey = (*key);
1378 ip_ctx->it (ip_ctx->it_cls, &tmp);
1385 * Return all peers currently known to ATS
1387 * @param handle the address handle
1388 * @param p_it the iterator to call for every peer, callbach with id == NULL
1390 * @param p_it_cls the closure for the iterator
1393 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle, GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1395 struct PeerIteratorContext ip_ctx;
1400 GNUNET_assert (NULL != handle->addresses);
1402 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1406 ip_ctx.it_cls = p_it_cls;
1407 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1408 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1409 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1411 p_it (p_it_cls, NULL);
1414 struct PeerInfoIteratorContext
1416 GNUNET_ATS_PeerInfo_Iterator it;
1422 peerinfo_it (void *cls,
1423 const struct GNUNET_HashCode * key,
1426 struct PeerInfoIteratorContext *pi_ctx = cls;
1427 struct ATS_Address *addr = (struct ATS_Address *) value;
1428 struct GNUNET_ATS_Information *ats;
1431 if (NULL != pi_ctx->it)
1433 ats_count = assemble_ats_information (addr, &ats);
1435 pi_ctx->it (pi_ctx->it_cls,
1438 addr->addr, addr->addr_len,
1441 addr->assigned_bw_out,
1442 addr->assigned_bw_in);
1450 * Return all peers currently known to ATS
1452 * @param handle the address handle
1453 * @param peer the respective peer
1454 * @param pi_it the iterator to call for every peer
1455 * @param pi_it_cls the closure for the iterator
1458 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
1459 const struct GNUNET_PeerIdentity *peer,
1460 GNUNET_ATS_PeerInfo_Iterator pi_it,
1463 struct PeerInfoIteratorContext pi_ctx;
1464 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1465 GNUNET_assert (NULL != peer);
1466 GNUNET_assert (NULL != handle->addresses);
1468 return; /* does not make sense without callback */
1470 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1472 pi_ctx.it_cls = pi_it_cls;
1474 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1477 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1482 /* end of gnunet-service-ats_addresses.c */