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
51 * Address management and suggestions
53 * Transport service notifies ATS about changes to the addresses known to him.
54 * When transport learns a new address it tells ATS about it using
57 * Bandwidth assignment
59 * The addresses are used to perform resource allocation operations. ATS
60 * addresses takes care of instantiating the solver configured and notifies the
61 * respective solver about address changes and receives changes to the bandwidth
62 * assignment from the solver. The current bandwidth assignment is sent to
70 * - If you add a new address without a session, a new address will be added
71 * - If you add this address again now with a session a, the existing address
72 * will be updated with this session
73 * - If you add this address again now with a session b, a new address object
74 * with this session will be added
76 * Destroying addresses:
78 * - If you destroy an address without a session, the address itself and all
79 * address instances with an session will be removed
80 * - If you destroy an address with a session, the session for this address
84 * Addresses without a session will be updated with a new session and if the
85 * the session is destroyed the session is removed and address itself still
86 * exists for suggestion
92 * Available ressource assignment modes
99 * Assign each peer an equal amount of bandwidth (bw)
101 * bw_per_peer = bw_total / #active addresses
108 * Solve ressource assignment as an optimization problem
109 * Uses an mixed integer programming solver
115 * Pending Address suggestion requests
117 struct GAS_Addresses_Suggestion_Requests
122 struct GAS_Addresses_Suggestion_Requests *next;
127 struct GAS_Addresses_Suggestion_Requests *prev;
132 struct GNUNET_PeerIdentity id;
136 * Handle for ATS address component
138 struct GAS_Addresses_Handle
143 struct GNUNET_STATISTICS_Handle *stat;
146 * A multihashmap to store all addresses
148 struct GNUNET_CONTAINER_MultiHashMap *addresses;
151 * Configure WAN quota in
153 unsigned long long wan_quota_in;
156 * Configure WAN quota out
158 unsigned long long wan_quota_out;
161 * Is ATS addresses running
166 * Configured ATS solver
176 * Address suggestion requests DLL head
178 struct GAS_Addresses_Suggestion_Requests *r_head;
181 * Address suggestion requests DLL tail
183 struct GAS_Addresses_Suggestion_Requests *r_tail;
185 /* Solver functions */
190 GAS_solver_init s_init;
193 * Add an address to the solver
195 GAS_solver_address_add s_add;
198 * Update address in solver
200 GAS_solver_address_update s_update;
203 * Get address from solver
205 GAS_solver_get_preferred_address s_get;
208 * Delete address in solver
210 GAS_solver_address_delete s_del;
213 * Change preference for quality in solver
215 GAS_solver_address_change_preference s_pref;
220 GAS_solver_done s_done;
225 assemble_ats_information (const struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
227 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
228 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
231 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
232 ats[0].value = aa->atsp_utilization_out.value__;
233 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
234 ats[1].value = aa->atsp_utilization_in.value__;
235 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
236 ats[2].value = ntohl(aa->atsp_network_type);
237 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
238 ats[3].value = ntohl(aa->atsp_latency.rel_value);
239 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
240 ats[4].value = ntohl(aa->atsp_distance);
241 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
242 ats[5].value = ntohl (aa->atsp_cost_wan);
243 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
244 ats[6].value = ntohl (aa->atsp_cost_lan);
245 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
246 ats[7].value = ntohl (aa->atsp_cost_wlan);
251 disassemble_ats_information (const struct GNUNET_ATS_Information *src,
253 struct ATS_Address *dest)
257 for (i = 0; i < ats_count; i++)
258 switch (ntohl (src[i].type))
260 case GNUNET_ATS_UTILIZATION_UP:
261 dest->atsp_utilization_out.value__ = src[i].value;
264 case GNUNET_ATS_UTILIZATION_DOWN:
265 dest->atsp_utilization_in.value__ = src[i].value;
268 case GNUNET_ATS_QUALITY_NET_DELAY:
269 dest->atsp_latency.rel_value = ntohl (src[i].value);
272 case GNUNET_ATS_QUALITY_NET_DISTANCE:
273 dest->atsp_distance = ntohl (src[i].value);
276 case GNUNET_ATS_COST_WAN:
277 dest->atsp_cost_wan = ntohl (src[i].value);
280 case GNUNET_ATS_COST_LAN:
281 dest->atsp_cost_lan = ntohl (src[i].value);
284 case GNUNET_ATS_COST_WLAN:
285 dest->atsp_cost_wlan = ntohl (src[i].value);
288 case GNUNET_ATS_NETWORK_TYPE:
289 dest->atsp_network_type = ntohl (src[i].value);
292 case GNUNET_ATS_ARRAY_TERMINATOR:
295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
296 "Received unsupported ATS type %u\n", ntohl (src[i].type));
304 * Free the given address
305 * @param addr address to destroy
308 free_address (struct ATS_Address *addr)
310 GNUNET_free (addr->plugin);
315 * Create a ATS_address with the given information
317 * @param plugin_name plugin
318 * @param plugin_addr address
319 * @param plugin_addr_len address length
320 * @param session_id session
321 * @return the ATS_Address
323 static struct ATS_Address *
324 create_address (const struct GNUNET_PeerIdentity *peer,
325 const char *plugin_name,
326 const void *plugin_addr, size_t plugin_addr_len,
329 struct ATS_Address *aa = NULL;
331 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
333 aa->addr_len = plugin_addr_len;
335 memcpy (&aa[1], plugin_addr, plugin_addr_len);
336 aa->plugin = GNUNET_strdup (plugin_name);
337 aa->session_id = session_id;
338 aa->active = GNUNET_NO;
339 aa->used = GNUNET_NO;
340 aa->solver_information = NULL;
341 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
342 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
348 * Destroy the given address.
350 * @param handle the address handle
351 * @param addr address to destroy
352 * @return GNUNET_YES if bandwidth allocations should be recalcualted
355 destroy_address (struct GAS_Addresses_Handle *handle, struct ATS_Address *addr)
360 GNUNET_assert (GNUNET_YES ==
361 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
362 &addr->peer.hashPubKey,
369 struct CompareAddressContext
371 const struct ATS_Address *search;
373 /* exact_address != NULL if address and session is equal */
374 struct ATS_Address *exact_address;
375 /* exact_address != NULL if address and session is 0 */
376 struct ATS_Address *base_address;
381 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
383 struct CompareAddressContext *cac = cls;
384 struct ATS_Address *aa = value;
386 /* Find an matching exact address:
389 * aa->addr_len == cac->search->addr_len
390 * aa->plugin == cac->search->plugin
391 * aa->addr == cac->search->addr
392 * aa->session == cac->search->session
394 * return as exact address
396 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
398 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
399 cac->exact_address = aa;
402 /* Find an matching base address:
406 * aa->session_id == 0
409 * aa->addr_len == cac->search->addr_len
410 * aa->plugin == cac->search->plugin
411 * aa->addr == cac->search->addr
413 * return as base address
415 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
417 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
418 cac->base_address = aa;
421 /* Find an matching exact address based on session:
425 * cac->search->addr_len == 0
428 * aa->plugin == cac->search->plugin
429 * aa->session_id == cac->search->session_id
431 * return as exact address
433 if (0 == cac->search->addr_len)
435 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
436 cac->exact_address = aa;
439 if (cac->exact_address == NULL)
440 return GNUNET_YES; /* Continue iteration to find exact address */
442 return GNUNET_NO; /* Stop iteration since we have an exact address */
447 * Find an existing equivalent address record.
448 * Compares by peer identity and network address OR by session ID
449 * (one of the two must match).
451 * @param handle the address handle
452 * @param peer peer to lookup addresses for
453 * @param addr existing address record
454 * @return existing address record, NULL for none
457 find_equivalent_address (struct GAS_Addresses_Handle *handle,
458 const struct GNUNET_PeerIdentity *peer,
459 const struct ATS_Address *addr)
461 struct CompareAddressContext cac;
463 cac.exact_address = NULL;
464 cac.base_address = NULL;
466 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
467 &compare_address_it, &cac);
469 if (cac.exact_address == NULL)
470 return cac.base_address;
471 return cac.exact_address;
475 static struct ATS_Address *
476 lookup_address (struct GAS_Addresses_Handle *handle,
477 const struct GNUNET_PeerIdentity *peer,
478 const char *plugin_name,
479 const void *plugin_addr,
480 size_t plugin_addr_len,
482 const struct GNUNET_ATS_Information *atsi,
485 struct ATS_Address *aa;
486 struct ATS_Address *ea;
488 aa = create_address (peer,
490 plugin_addr, plugin_addr_len,
493 /* Get existing address or address with session == 0 */
494 ea = find_equivalent_address (handle, peer, aa);
500 else if (ea->session_id != session_id)
509 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
510 const struct GNUNET_PeerIdentity *peer,
511 const char *plugin_name, const void *plugin_addr,
512 size_t plugin_addr_len, uint32_t session_id,
513 const struct GNUNET_ATS_Information *atsi,
516 struct ATS_Address *aa;
517 struct ATS_Address *ea;
518 unsigned int ats_res;
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521 "Received `%s' for peer `%s'\n",
525 if (GNUNET_NO == handle->running)
528 GNUNET_assert (NULL != handle->addresses);
530 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
532 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, aa)))
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535 "While adding address: had %u ATS elements to add, could only add %u\n",
536 atsi_count, ats_res);
539 /* Get existing address or address with session == 0 */
540 ea = find_equivalent_address (handle, peer, aa);
543 /* We have a new address */
544 GNUNET_assert (GNUNET_OK ==
545 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
546 &peer->hashPubKey, aa,
547 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
549 GNUNET_i2s (peer), session_id, aa);
550 /* Tell solver about new address */
551 handle->s_add (handle->solver, handle->addresses, aa);
554 GNUNET_free (aa->plugin);
557 if (ea->session_id != 0)
559 /* This address with the same session is already existing
560 * Should not happen */
561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
562 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
563 GNUNET_i2s (peer), plugin_name, session_id);
568 /* We have an address without an session, update this address */
570 /* Notify solver about update with atsi information and session */
571 handle->s_update (handle->solver, handle->addresses, ea, session_id, ea->used, atsi, atsi_count);
574 ea->session_id = session_id;
575 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, ea)))
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
578 "While updating address: had %u ATS elements to add, could only add %u\n",
579 atsi_count, ats_res);
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
582 "Updated existing address for peer `%s' %p with new session %u\n",
583 GNUNET_i2s (peer), ea, session_id);
588 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
589 const struct GNUNET_PeerIdentity *peer,
590 const char *plugin_name, const void *plugin_addr,
591 size_t plugin_addr_len, uint32_t session_id,
592 const struct GNUNET_ATS_Information *atsi,
595 struct ATS_Address *aa;
598 if (GNUNET_NO == handle->running)
601 GNUNET_assert (NULL != handle->addresses);
603 /* Get existing address */
604 aa = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
605 session_id, atsi, atsi_count);
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
609 GNUNET_i2s (peer), plugin_name, session_id);
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
615 "Received `%s' for peer `%s' address \n",
617 GNUNET_i2s (peer), aa);
619 /* Tell solver about update */
620 handle->s_update (handle->solver, handle->addresses, aa, session_id, aa->used, atsi, atsi_count);
623 if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, aa)))
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "While adding address: had %u ATS elements to add, could only add %u\n",
627 atsi_count, ats_res);
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
630 "Updated %u ATS elements for address %p\n",
635 struct DestroyContext
637 struct ATS_Address *aa;
639 struct GAS_Addresses_Handle *handle;
642 * GNUNET_NO : full address
643 * GNUNET_YES : just session
652 * If session != 0, just the session is deleted, the address itself still exists
653 * If session == 0, remove full address
654 * If session == 0 and addrlen == 0, destroy inbound address
658 * @param value the 'struct ATS_Address'
659 * @return GNUNET_OK (continue to iterate)
662 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
664 struct DestroyContext *dc = cls;
665 struct GAS_Addresses_Handle *handle = dc->handle;
666 const struct ATS_Address *des = dc->aa;
667 struct ATS_Address *aa = value;
669 GNUNET_assert (0 == memcmp (&aa->peer, &des->peer,
670 sizeof (struct GNUNET_PeerIdentity)));
673 if (des->session_id == 0)
675 /* Session == 0, remove full address */
676 if ((0 == strcmp (des->plugin, aa->plugin)) &&
677 (aa->addr_len == des->addr_len) &&
678 (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682 "Deleting full address for peer `%s' session %u %p\n",
683 GNUNET_i2s (&aa->peer), aa->session_id, aa);
685 /* Notify solver about deletion */
686 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
687 destroy_address (handle, aa);
688 dc->result = GNUNET_NO;
689 return GNUNET_OK; /* Continue iteration */
694 /* Session != 0, just remove session */
695 if (aa->session_id != des->session_id)
696 return GNUNET_OK; /* irrelevant */
698 if ((aa->session_id != 0) &&
699 (0 != strcmp (des->plugin, aa->plugin)))
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "Different plugins during removal: `%s' vs `%s' \n",
703 des->plugin, aa->plugin);
708 if (aa->addr_len == 0)
710 /* Inbound connection died, delete full address */
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Deleting inbound address for peer `%s': `%s' session %u\n",
713 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
715 /* Notify solver about deletion */
716 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
717 destroy_address (handle, aa);
718 dc->result = GNUNET_NO;
719 return GNUNET_OK; /* Continue iteration */
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
725 "Deleting session for peer `%s': `%s' %u\n",
726 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
727 /* Notify solver to delete session */
728 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_YES);
737 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
738 const struct GNUNET_PeerIdentity *peer,
739 const char *plugin_name, const void *plugin_addr,
740 size_t plugin_addr_len, uint32_t session_id)
742 struct ATS_Address *ea;
743 struct DestroyContext dc;
745 if (GNUNET_NO == handle->running)
748 /* Get existing address */
749 ea = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
750 session_id, NULL, 0);
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
753 "Received `%s' for peer `%s' address %p session %u\n",
755 GNUNET_i2s (peer), ea, session_id);
759 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
760 GNUNET_i2s (peer), plugin_name, session_id);
764 GNUNET_break (0 < strlen (plugin_name));
766 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
768 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
769 &destroy_by_session_id, &dc);
770 free_address (dc.aa);
775 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
776 const struct GNUNET_PeerIdentity *peer,
777 const char *plugin_name, const void *plugin_addr,
778 size_t plugin_addr_len, uint32_t session_id, int in_use)
780 struct ATS_Address *ea;
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783 "Received `%s' for peer `%s'\n",
787 if (GNUNET_NO == handle->running)
788 return GNUNET_SYSERR;
790 ea = lookup_address (handle, peer, plugin_name,
791 plugin_addr, plugin_addr_len,
792 session_id, NULL, 0);
795 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
796 "Trying to set unknown address `%s', %s %u %s \n",
798 plugin_name, session_id,
799 (GNUNET_NO == in_use) ? "NO" : "YES");
801 return GNUNET_SYSERR;
803 if (ea->used == in_use)
806 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
807 "Address in use called multiple times for peer `%s': %s -> %s \n",
809 (GNUNET_NO == ea->used) ? "NO" : "YES",
810 (GNUNET_NO == in_use) ? "NO" : "YES");
811 return GNUNET_SYSERR;
814 /* Tell solver about update */
815 handle->s_update (handle->solver, handle->addresses, ea, session_id, in_use, NULL, 0);
823 * Cancel address suggestions for a peer
825 * @param handle the address handle
826 * @param peer the respective peer
829 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
830 const struct GNUNET_PeerIdentity *peer)
832 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
835 "Received request: `%s' for peer %s\n", "request_address_cancel", GNUNET_i2s (peer));
839 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
846 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
847 "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
850 GAS_addresses_handle_backoff_reset (handle, peer);
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "Removed request pending for peer `%s\n", GNUNET_i2s (peer));
853 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
859 * Add an address suggestions for a peer
861 * @param handle the address handle
862 * @param peer the respective peer
865 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
866 const struct GNUNET_PeerIdentity *peer)
868 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
869 struct ATS_Address *aa;
870 struct GNUNET_ATS_Information *ats;
871 unsigned int ats_count;
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
874 "Received `%s' for peer `%s'\n",
878 if (GNUNET_NO == handle->running)
882 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
883 break; /* already suggesting */
888 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
890 GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
893 /* Get prefered address from solver */
894 aa = (struct ATS_Address *) handle->s_get (handle->solver, handle->addresses, peer);
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
903 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
905 ats_count = assemble_ats_information (aa, &ats);
906 GAS_scheduling_transmit_address_suggestion (peer,
908 aa->addr, aa->addr_len,
914 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
915 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 "Address %p ready for suggestion, block interval now %llu \n",
919 aa, aa->block_interval);
926 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
928 struct ATS_Address *aa = value;
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
931 "Resetting interval for peer `%s' address %p from %llu to 0\n",
932 GNUNET_i2s (&aa->peer), aa, aa->block_interval);
934 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
935 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
941 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
942 const struct GNUNET_PeerIdentity *peer)
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 "Received `%s' for peer `%s'\n",
949 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
957 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
959 const struct GNUNET_PeerIdentity *peer,
960 enum GNUNET_ATS_PreferenceKind kind,
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Received `%s' for peer `%s' for client %p\n",
966 GNUNET_i2s (peer), client);
968 if (GNUNET_NO == handle->running)
971 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->addresses,
974 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
975 "Received `%s' for unknown peer `%s' from client %p\n",
977 GNUNET_i2s (peer), client);
981 /* Tell solver about update */
982 handle->s_pref (handle->solver, client, peer, kind, score);
987 * Load quotas for networks from configuration
989 * @param cfg configuration handle
990 * @param out_dest where to write outbound quotas
991 * @param in_dest where to write inbound quotas
992 * @param dest_length length of inbound and outbound arrays
993 * @return number of networks loaded
996 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
998 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
999 char * entry_in = NULL;
1000 char * entry_out = NULL;
1001 char * quota_out_str;
1002 char * quota_in_str;
1006 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1010 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1011 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1014 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
1017 if (0 == strcmp(quota_out_str, BIG_M_STRING))
1019 out_dest[c] = GNUNET_ATS_MaxBandwidth;
1022 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
1024 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out, &out_dest[c])))
1027 if (GNUNET_NO == res)
1029 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1030 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1031 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1035 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Outbound quota configure for network `%s' is %llu\n"),
1036 network_str[c], out_dest[c]);
1038 GNUNET_free (quota_out_str);
1042 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1043 network_str[c], GNUNET_ATS_DefaultBandwidth);
1044 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1048 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
1051 if (0 == strcmp(quota_in_str, BIG_M_STRING))
1053 in_dest[c] = GNUNET_ATS_MaxBandwidth;
1056 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1058 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in, &in_dest[c])))
1061 if (GNUNET_NO == res)
1063 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
1064 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1065 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1069 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Inbound quota configured for network `%s' is %llu\n"),
1070 network_str[c], in_dest[c]);
1072 GNUNET_free (quota_in_str);
1076 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1077 network_str[c], GNUNET_ATS_DefaultBandwidth);
1078 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1080 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]);
1081 GNUNET_free (entry_out);
1082 GNUNET_free (entry_in);
1084 return GNUNET_ATS_NetworkTypeCount;
1089 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1091 struct GAS_Addresses_Handle *handle = cls;
1092 struct GAS_Addresses_Suggestion_Requests *cur;
1094 GNUNET_assert (handle != NULL);
1095 GNUNET_assert (address != NULL);
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bandwidth assignment changed for peer %s \n", GNUNET_i2s(&address->peer));
1098 struct GNUNET_ATS_Information *ats;
1099 unsigned int ats_count;
1101 cur = handle->r_head;
1104 if (0 == memcmp (&address->peer, &cur->id, sizeof (cur->id)))
1105 break; /* we have an address request pending*/
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 "Nobody is interested in peer `%s' :(\n",GNUNET_i2s (&address->peer));
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1116 "Sending bandwidth update for peer `%s'\n",GNUNET_i2s (&address->peer));
1118 ats_count = assemble_ats_information (address, &ats);
1119 GAS_scheduling_transmit_address_suggestion (&address->peer,
1121 address->addr, address->addr_len,
1122 address->session_id,
1124 address->assigned_bw_out,
1125 address->assigned_bw_in);
1131 * Initialize address subsystem.
1133 * @param cfg configuration to use
1134 * @param stats the statistics handle to use
1136 struct GAS_Addresses_Handle *
1137 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1138 const struct GNUNET_STATISTICS_Handle *stats)
1140 struct GAS_Addresses_Handle *ah;
1141 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1142 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1143 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1148 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1149 ah->running = GNUNET_NO;
1151 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1152 /* Initialize the addresses database */
1153 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1154 GNUNET_assert (NULL != ah->addresses);
1156 /* Figure out configured solution method */
1157 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1159 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1160 ah->ats_mode = MODE_SIMPLISTIC;
1164 for (c = 0; c < strlen (mode_str); c++)
1165 mode_str[c] = toupper (mode_str[c]);
1166 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1168 ah->ats_mode = MODE_SIMPLISTIC;
1170 else if (0 == strcmp (mode_str, "MLP"))
1172 ah->ats_mode = MODE_MLP;
1174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1175 ah->ats_mode = MODE_SIMPLISTIC;
1180 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1181 ah->ats_mode = MODE_SIMPLISTIC;
1183 GNUNET_free (mode_str);
1185 /* Start configured solution method */
1186 switch (ah->ats_mode)
1189 /* Init the MLP solver with default values */
1191 ah->ats_mode = MODE_MLP;
1192 ah->s_init = &GAS_mlp_init;
1193 ah->s_add = &GAS_mlp_address_add;
1194 ah->s_update = &GAS_mlp_address_update;
1195 ah->s_get = &GAS_mlp_get_preferred_address;
1196 ah->s_pref = &GAS_mlp_address_change_preference;
1197 ah->s_del = &GAS_mlp_address_delete;
1198 ah->s_done = &GAS_mlp_done;
1204 case MODE_SIMPLISTIC:
1205 /* Init the simplistic solver with default values */
1206 ah->ats_mode = MODE_SIMPLISTIC;
1207 ah->s_init = &GAS_simplistic_init;
1208 ah->s_add = &GAS_simplistic_address_add;
1209 ah->s_update = &GAS_simplistic_address_update;
1210 ah->s_get = &GAS_simplistic_get_preferred_address;
1211 ah->s_pref = &GAS_simplistic_address_change_preference;
1212 ah->s_del = &GAS_simplistic_address_delete;
1213 ah->s_done = &GAS_simplistic_done;
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1221 GNUNET_assert (NULL != ah->s_init);
1222 GNUNET_assert (NULL != ah->s_add);
1223 GNUNET_assert (NULL != ah->s_update);
1224 GNUNET_assert (NULL != ah->s_get);
1225 GNUNET_assert (NULL != ah->s_pref);
1226 GNUNET_assert (NULL != ah->s_del);
1227 GNUNET_assert (NULL != ah->s_done);
1229 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1231 ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count, &bandwidth_changed_cb, ah);
1232 if (NULL == ah->solver)
1234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
1239 /* up and running */
1240 ah->running = GNUNET_YES;
1246 * Free memory of address.
1249 * @param key peer identity (unused)
1250 * @param value the 'struct ATS_Address' to free
1251 * @return GNUNET_OK (continue to iterate)
1254 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1256 struct GAS_Addresses_Handle *handle = cls;
1257 struct ATS_Address *aa = value;
1258 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
1259 destroy_address (handle, aa);
1265 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1267 if (GNUNET_NO == handle->running)
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1274 if (handle->addresses != NULL)
1275 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, handle);
1280 * Shutdown address subsystem.
1283 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1285 struct GAS_Addresses_Suggestion_Requests *cur;
1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1288 "Shutting down addresses\n");
1289 GNUNET_assert (NULL != handle);
1290 GAS_addresses_destroy_all (handle);
1291 handle->running = GNUNET_NO;
1292 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1293 handle->addresses = NULL;
1294 while (NULL != (cur = handle->r_head))
1296 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1299 handle->s_done (handle->solver);
1300 GNUNET_free (handle);
1301 /* Stop configured solution method */
1305 struct PeerIteratorContext
1307 GNUNET_ATS_Peer_Iterator it;
1309 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1314 const struct GNUNET_HashCode * key,
1317 struct PeerIteratorContext *ip_ctx = cls;
1318 struct GNUNET_PeerIdentity tmp;
1320 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1322 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1323 tmp.hashPubKey = (*key);
1324 ip_ctx->it (ip_ctx->it_cls, &tmp);
1331 * Return all peers currently known to ATS
1333 * @param handle the address handle
1334 * @param p_it the iterator to call for every peer, callbach with id == NULL
1336 * @param p_it_cls the closure for the iterator
1339 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle, GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1341 struct PeerIteratorContext ip_ctx;
1346 GNUNET_assert (NULL != handle->addresses);
1348 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1352 ip_ctx.it_cls = p_it_cls;
1353 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1354 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1355 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1357 p_it (p_it_cls, NULL);
1360 struct PeerInfoIteratorContext
1362 GNUNET_ATS_PeerInfo_Iterator it;
1368 peerinfo_it (void *cls,
1369 const struct GNUNET_HashCode * key,
1372 struct PeerInfoIteratorContext *pi_ctx = cls;
1373 struct ATS_Address *addr = (struct ATS_Address *) value;
1374 struct GNUNET_ATS_Information *ats;
1377 if (NULL != pi_ctx->it)
1379 ats_count = assemble_ats_information (addr, &ats);
1381 pi_ctx->it (pi_ctx->it_cls,
1384 addr->addr, addr->addr_len,
1387 addr->assigned_bw_out,
1388 addr->assigned_bw_in);
1396 * Return all peers currently known to ATS
1398 * @param handle the address handle
1399 * @param peer the respective peer
1400 * @param pi_it the iterator to call for every peer
1401 * @param pi_it_cls the closure for the iterator
1404 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
1405 const struct GNUNET_PeerIdentity *peer,
1406 GNUNET_ATS_PeerInfo_Iterator pi_it,
1409 struct PeerInfoIteratorContext pi_ctx;
1410 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1411 GNUNET_assert (NULL != peer);
1412 GNUNET_assert (NULL != handle->addresses);
1414 return; /* does not make sense without callback */
1416 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1418 pi_ctx.it_cls = pi_it_cls;
1420 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1423 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1428 /* end of gnunet-service-ats_addresses.c */