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"
38 #define VERBOSE GNUNET_NO
40 #define ATS_BLOCKING_DELTA GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
45 * Assign each peer an equal amount of bandwidth (bw)
47 * bw_per_peer = bw_total / #active addresses
52 * Use MLP solver to assign bandwidth
57 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
60 static struct GAS_MLP_Handle *mlp;
63 static unsigned long long wan_quota_in;
65 static unsigned long long wan_quota_out;
67 static unsigned int active_addr_count;
75 send_bw_notification (struct ATS_Address *aa)
77 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
78 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
79 ntohl (aa->assigned_bw_out.value__));
80 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
81 aa->addr_len, aa->session_id,
82 aa->ats, aa->ats_count,
85 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
86 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
87 aa->ats, aa->ats_count, aa->assigned_bw_out,
92 * Update a bandwidth assignment for a peer. This trivial method currently
93 * simply assigns the same share to all active connections.
97 * @param value the 'struct ATS_Address'
98 * @return GNUNET_OK (continue to iterate)
101 update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
103 struct ATS_Address *aa = value;
105 if (GNUNET_YES != aa->active)
107 GNUNET_assert (active_addr_count > 0);
111 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
112 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
114 send_bw_notification (aa);
121 * Some (significant) input changed, recalculate bandwidth assignment
125 recalculate_assigned_bw ()
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 "Recalculating bandwidth for all active connections\n");
129 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
131 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
134 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_simple_it, NULL);
138 * Free the given address
139 * @param addr address to destroy
142 free_address (struct ATS_Address *addr)
144 GNUNET_free_non_null (addr->ats);
145 GNUNET_free (addr->plugin);
150 * Create a ATS_address with the given information
152 * @param plugin_name plugin
153 * @param plugin_addr address
154 * @param plugin_addr_len address length
155 * @param session_id session
156 * @return the ATS_Address
158 static struct ATS_Address *
159 create_address (const struct GNUNET_PeerIdentity *peer,
160 const char *plugin_name,
161 const void *plugin_addr, size_t plugin_addr_len,
164 struct ATS_Address *aa = NULL;
166 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
168 aa->addr_len = plugin_addr_len;
170 memcpy (&aa[1], plugin_addr, plugin_addr_len);
171 aa->plugin = GNUNET_strdup (plugin_name);
172 aa->session_id = session_id;
178 * Destroy the given address.
180 * @param addr address to destroy
181 * @return GNUNET_YES if bandwidth allocations should be recalcualted
184 destroy_address (struct ATS_Address *addr)
189 GNUNET_assert (GNUNET_YES ==
190 GNUNET_CONTAINER_multihashmap_remove (addresses,
191 &addr->peer.hashPubKey,
196 GAS_mlp_address_delete (mlp, addresses, addr);
199 if (GNUNET_YES == addr->active)
202 addr->active = GNUNET_NO;
210 struct CompareAddressContext
212 const struct ATS_Address *search;
214 /* exact_address != NULL if address and session is equal */
215 struct ATS_Address *exact_address;
216 /* exact_address != NULL if address and session is 0 */
217 struct ATS_Address *base_address;
222 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
224 struct CompareAddressContext *cac = cls;
225 struct ATS_Address *aa = value;
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing peer %4s: address length %u session %u <-> address length %u session %u\n",
229 aa->addr_len, aa->session_id,
230 cac->search->addr_len, cac->search->session_id);
232 /* Find an matching exact address:
235 * aa->addr_len == cac->search->addr_len
236 * aa->plugin == cac->search->plugin
237 * aa->addr == cac->search->addr
238 * aa->session == cac->search->session
240 * return as exact address
242 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
244 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
245 cac->exact_address = aa;
248 /* Find an matching base address:
252 * aa->session_id == 0
255 * aa->addr_len == cac->search->addr_len
256 * aa->plugin == cac->search->plugin
257 * aa->addr == cac->search->addr
259 * return as base address
261 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
263 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
264 cac->base_address = aa;
267 /* Find an matching exact address based on session:
271 * cac->search->addr_len == 0
274 * aa->plugin == cac->search->plugin
275 * aa->session_id == cac->search->session_id
277 * return as exact address
279 if (0 == cac->search->addr_len)
281 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
282 cac->exact_address = aa;
285 if (cac->exact_address == NULL)
286 return GNUNET_YES; /* Continue iteration to find exact address */
288 return GNUNET_NO; /* Stop iteration since we have an exact address */
293 * Find an existing equivalent address record.
294 * Compares by peer identity and network address OR by session ID
295 * (one of the two must match).
297 * @param peer peer to lookup addresses for
298 * @param addr existing address record
299 * @return existing address record, NULL for none
302 find_address (const struct GNUNET_PeerIdentity *peer,
303 const struct ATS_Address *addr)
305 struct CompareAddressContext cac;
307 cac.exact_address = NULL;
308 cac.base_address = NULL;
310 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
311 &compare_address_it, &cac);
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 "Found exact address: %s base address: %s\n",
316 (cac.exact_address != NULL) ? "YES" : "NO",
317 (cac.base_address != NULL) ? "YES" : "NO");
319 if (cac.exact_address == NULL)
320 return cac.base_address;
321 return cac.exact_address;
325 static struct ATS_Address *
326 lookup_address (const struct GNUNET_PeerIdentity *peer,
327 const char *plugin_name, const void *plugin_addr,
328 size_t plugin_addr_len, uint32_t session_id,
329 const struct GNUNET_ATS_Information *atsi,
332 struct ATS_Address *aa;
333 struct ATS_Address *old;
335 aa = create_address (peer,
337 plugin_addr, plugin_addr_len,
340 aa->mlp_information = NULL;
341 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
342 aa->ats_count = atsi_count;
343 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
345 /* Get existing address or address with session == 0 */
346 old = find_address (peer, aa);
352 else if (old->session_id != session_id)
360 compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
362 struct CompareAddressContext *cac = cls;
363 struct ATS_Address *aa = value;
365 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
367 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
369 cac->exact_address = aa;
378 * Find an existing equivalent address record.
379 * Compares by peer identity and network address AND by session ID
380 * (one of the two must match).
382 * @param peer peer to lookup addresses for
383 * @param addr existing address record
384 * @return existing address record, NULL for none
387 find_exact_address (const struct GNUNET_PeerIdentity *peer,
388 const struct ATS_Address *addr)
390 struct CompareAddressContext cac;
392 cac.exact_address = NULL;
394 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
395 &compare_address_session_it, &cac);
396 return cac.exact_address;
401 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
402 const char *plugin_name, const void *plugin_addr,
403 size_t plugin_addr_len, uint32_t session_id,
404 const struct GNUNET_ATS_Information *atsi,
407 struct ATS_Address *aa;
408 struct ATS_Address *old;
410 if (GNUNET_NO == running)
413 GNUNET_assert (NULL != addresses);
415 aa = create_address (peer,
417 plugin_addr, plugin_addr_len,
420 aa->mlp_information = NULL;
421 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
422 aa->ats_count = atsi_count;
423 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
425 /* Get existing address or address with session == 0 */
426 old = find_address (peer, aa);
429 /* We have a new address */
430 GNUNET_assert (GNUNET_OK ==
431 GNUNET_CONTAINER_multihashmap_put (addresses,
432 &peer->hashPubKey, aa,
433 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
435 GNUNET_i2s (peer), session_id, aa);
439 if (old->session_id == 0)
441 /* We have a base address with out an session, update this address */
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "Updated existing address for peer `%s' %p with new session %u\n",
444 GNUNET_i2s (peer), old, session_id);
445 GNUNET_free_non_null (old->ats);
446 old->session_id = session_id;
450 old->ats_count = aa->ats_count;
451 GNUNET_free (aa->plugin);
456 /* This address and session is already existing */
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
458 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
459 GNUNET_i2s (peer), plugin_name, session_id);
465 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
466 const char *plugin_name, const void *plugin_addr,
467 size_t plugin_addr_len, uint32_t session_id,
468 const struct GNUNET_ATS_Information *atsi,
471 struct ATS_Address *old;
474 if (GNUNET_NO == running)
477 GNUNET_assert (NULL != addresses);
479 /* Get existing address */
480 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
481 session_id, atsi, atsi_count);
484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
485 GNUNET_i2s (peer), plugin_name, session_id);
490 for (i = 0; i < atsi_count; i++)
491 switch (ntohl (atsi[i].type))
493 case GNUNET_ATS_UTILIZATION_UP:
494 old->atsp_utilization_out.value__ = atsi[i].value;
496 case GNUNET_ATS_UTILIZATION_DOWN:
497 old->atsp_utilization_in.value__ = atsi[i].value;
499 case GNUNET_ATS_QUALITY_NET_DELAY:
500 old->atsp_latency.rel_value = ntohl (atsi[i].value);
502 case GNUNET_ATS_QUALITY_NET_DISTANCE:
503 old->atsp_distance = ntohl (atsi[i].value);
505 case GNUNET_ATS_COST_WAN:
506 old->atsp_cost_wan = ntohl (atsi[i].value);
508 case GNUNET_ATS_COST_LAN:
509 old->atsp_cost_lan = ntohl (atsi[i].value);
511 case GNUNET_ATS_COST_WLAN:
512 old->atsp_cost_wlan = ntohl (atsi[i].value);
514 case GNUNET_ATS_NETWORK_TYPE:
515 old->atsp_network_type = ntohl (atsi[i].value);
519 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
520 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
526 GAS_mlp_address_update (mlp, addresses, old);
534 * If session != 0, just the session is deleted, the address itself still exists
535 * If session == 0, remove full address
536 * If session == 0 and addrlen == 0, destroy inbound address
540 * @param value the 'struct ATS_Address'
541 * @return GNUNET_OK (continue to iterate)
544 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
546 const struct ATS_Address *info = cls;
547 struct ATS_Address *aa = value;
550 memcmp (&aa->peer, &info->peer,
551 sizeof (struct GNUNET_PeerIdentity)));
552 /* session == 0, remove full address */
553 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
554 (aa->addr_len == info->addr_len) &&
555 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 "Deleting address for peer `%s': `%s' %u\n",
560 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
562 if (GNUNET_YES == destroy_address (aa))
563 recalculate_assigned_bw ();
566 /* session != 0, just remove session */
567 if (aa->session_id != info->session_id)
568 return GNUNET_OK; /* irrelevant */
569 if (aa->session_id != 0)
570 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 "Deleting session for peer `%s': `%s' %u\n",
575 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
579 if (GNUNET_YES == aa->active)
581 aa->active = GNUNET_NO;
583 recalculate_assigned_bw ();
586 /* session == 0 and addrlen == 0 : destroy address */
587 if (aa->addr_len == 0)
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "Deleting session and address for peer `%s': `%s' %u\n",
592 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
594 (void) destroy_address (aa);
598 /* session was set to 0, update address */
601 GAS_mlp_address_update (mlp, addresses, aa);
609 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
610 const char *plugin_name, const void *plugin_addr,
611 size_t plugin_addr_len, uint32_t session_id)
613 struct ATS_Address *aa;
614 struct ATS_Address *old;
616 if (GNUNET_NO == running)
619 /* Get existing address */
620 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
621 session_id, NULL, 0);
624 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
625 GNUNET_i2s (peer), plugin_name, session_id);
631 GNUNET_break (0 < strlen (plugin_name));
632 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
634 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
635 &destroy_by_session_id, aa);
642 * Find a "good" address to use for a peer. If we already have an existing
643 * address, we stick to it. Otherwise, we pick by lowest distance and then
646 * @param cls the 'struct ATS_Address**' where we store the result
648 * @param value another 'struct ATS_Address*' to consider using
649 * @return GNUNET_OK (continue to iterate)
652 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
654 struct ATS_Address **ap = cls;
655 struct ATS_Address *aa = (struct ATS_Address *) value;
656 struct ATS_Address *ab = *ap;
657 struct GNUNET_TIME_Absolute now;
659 now = GNUNET_TIME_absolute_get();
661 if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value)
663 /* This address is blocked for suggestion */
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "Address %p blocked for suggestion for %llu ms \n",
667 GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value);
671 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
672 aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
677 /* FIXME this is a hack */
682 if ((0 == strcmp (ab->plugin, "tcp")) &&
683 (0 == strcmp (aa->plugin, "tcp")))
685 if ((0 != ab->addr_len) &&
688 /* saved address was an outbound address, but we have an inbound address */
692 if (0 == ab->addr_len)
694 /* saved address was an inbound address, so do not overwrite */
699 /* FIXME end of hack */
706 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
707 (ntohl (aa->assigned_bw_in.value__) > 0))
709 /* stick to existing connection */
713 if (ab->atsp_distance > aa->atsp_distance)
715 /* user shorter distance */
719 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
721 /* user lower latency */
731 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
732 const char *plugin_name, const void *plugin_addr,
733 size_t plugin_addr_len, uint32_t session_id, int in_use)
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
738 GNUNET_i2s (peer), in_use);
741 struct ATS_Address *old;
743 if (GNUNET_NO == running)
744 return GNUNET_SYSERR;
746 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
749 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
750 "Trying to set unknown address `%s', %s %u %s \n",
752 plugin_name, session_id,
753 (GNUNET_NO == in_use) ? "NO" : "YES");
755 return GNUNET_SYSERR;
757 if (old->used == in_use)
760 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
761 "Address in use called multiple times for peer `%s': %s -> %s \n",
763 (GNUNET_NO == old->used) ? "NO" : "YES",
764 (GNUNET_NO == in_use) ? "NO" : "YES");
765 return GNUNET_SYSERR;
770 GAS_mlp_address_update (mlp, addresses, old);
776 void request_address_mlp (const struct GNUNET_PeerIdentity *peer)
778 struct ATS_Address *aa;
782 /* Get preferred address from MLP */
783 struct ATS_PreferedAddress * paddr = NULL;
784 paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
786 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
787 /* FIXME use bw in value */
788 paddr->bandwidth_in = paddr->bandwidth_out;
789 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
796 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
799 if (aa->active == GNUNET_NO)
801 aa->active = GNUNET_YES;
804 send_bw_notification (aa);
808 /* just to be sure... */
809 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
810 aa->addr_len, aa->session_id,
811 aa->ats, aa->ats_count,
818 void request_address_simple (const struct GNUNET_PeerIdentity *peer)
820 struct ATS_Address *aa;
823 /* Get address with: stick to current address, lower distance, lower latency */
824 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
825 &find_address_it, &aa);
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
829 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
834 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
836 if (aa->active == GNUNET_NO)
838 aa->active = GNUNET_YES;
840 if (ats_mode == SIMPLE)
842 recalculate_assigned_bw ();
847 /* just to be sure... */
848 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
849 aa->addr_len, aa->session_id,
850 aa->ats, aa->ats_count,
858 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
860 if (GNUNET_NO == running)
863 if (ats_mode == SIMPLE)
865 request_address_simple (peer);
869 request_address_mlp(peer);
875 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
877 struct ATS_Address *aa = value;
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880 "Resetting interval for peer `%s' address %p from %llu to 0\n", GNUNET_i2s (&aa->peer), aa, aa->block_interval);
882 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
883 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
888 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
890 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (addresses,
898 // FIXME: this function should likely end up in the LP-subsystem and
899 // not with 'addresses' in the future...
901 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
902 enum GNUNET_ATS_PreferenceKind kind,
905 if (GNUNET_NO == running)
909 GAS_mlp_address_change_preference (mlp, peer, kind, score);
916 * Initialize address subsystem.
918 * @param cfg configuration to use
919 * @param stats the statistics handle to use
922 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
923 const struct GNUNET_STATISTICS_Handle *stats)
927 char *quota_wan_in_str;
928 char *quota_wan_out_str;
932 addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
933 GNUNET_assert (NULL != addresses);
935 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_wan_in_str))
937 if (0 == strcmp(quota_wan_in_str, "unlimited") ||
938 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &wan_quota_in)))
939 wan_quota_in = (UINT32_MAX) /10;
941 GNUNET_free (quota_wan_in_str);
942 quota_wan_in_str = NULL;
946 wan_quota_in = (UINT32_MAX) /10;
949 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_wan_out_str))
951 if (0 == strcmp(quota_wan_out_str, "unlimited") ||
952 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &wan_quota_out)))
953 wan_quota_out = (UINT32_MAX) /10;
955 GNUNET_free (quota_wan_out_str);
956 quota_wan_out_str = NULL;
960 wan_quota_out = (UINT32_MAX) /10;
963 mode = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP");
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP mode %u", mode);
971 /* Init the MLP solver with default values */
972 mlp = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
975 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode\n");
976 GNUNET_STATISTICS_update (GSA_stats, "MLP mode enabled", 0, GNUNET_NO);
981 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 1, GNUNET_NO);
985 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
986 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
992 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
995 /* No configuration value */
997 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started with %s mode\n", (SIMPLE == ats_mode) ? "SIMPLE" : "MLP");
1004 running = GNUNET_YES;
1009 * Free memory of address.
1012 * @param key peer identity (unused)
1013 * @param value the 'struct ATS_Address' to free
1014 * @return GNUNET_OK (continue to iterate)
1017 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1019 struct ATS_Address *aa = value;
1021 destroy_address (aa);
1027 GAS_addresses_destroy_all ()
1029 if (GNUNET_NO == running)
1032 if (addresses != NULL)
1033 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
1034 GNUNET_assert (active_addr_count == 0);
1039 * Shutdown address subsystem.
1042 GAS_addresses_done ()
1044 GAS_addresses_destroy_all ();
1045 running = GNUNET_NO;
1046 GNUNET_CONTAINER_multihashmap_destroy (addresses);
1049 if (ats_mode == MLP)
1056 struct PeerIteratorContext
1058 GNUNET_ATS_Peer_Iterator it;
1060 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1065 const struct GNUNET_HashCode * key,
1068 struct PeerIteratorContext *ip_ctx = cls;
1069 struct GNUNET_PeerIdentity tmp;
1071 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1073 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1074 tmp.hashPubKey = (*key);
1075 ip_ctx->it (ip_ctx->it_cls, &tmp);
1082 * Return all peers currently known to ATS
1084 * @param p_it the iterator to call for every peer
1085 * @param p_it_cls the closure for the iterator
1088 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1090 struct PeerIteratorContext ip_ctx;
1095 GNUNET_assert (NULL != addresses);
1097 size = GNUNET_CONTAINER_multihashmap_size(addresses);
1101 ip_ctx.it_cls = p_it_cls;
1102 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1103 GNUNET_CONTAINER_multihashmap_iterate (addresses, &peer_it, &ip_ctx);
1104 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1106 p_it (p_it_cls, NULL);
1109 struct PeerInfoIteratorContext
1111 GNUNET_ATS_PeerInfo_Iterator it;
1115 int peerinfo_it (void *cls,
1116 const struct GNUNET_HashCode * key,
1119 struct PeerInfoIteratorContext *pi_ctx = cls;
1120 struct ATS_Address *addr = (struct ATS_Address *) value;
1122 if ((NULL != pi_ctx->it) && (GNUNET_YES == addr->used))
1123 pi_ctx->it (pi_ctx->it_cls,
1126 addr->addr, addr->addr_len,
1127 addr->ats, addr->ats_count,
1128 addr->assigned_bw_out,
1129 addr->assigned_bw_in);
1134 * Return all peers currently known to ATS
1136 * @param peer the respective peer
1137 * @param pi_it the iterator to call for every peer
1138 * @param pi_it_cls the closure for the iterator
1141 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1143 struct PeerInfoIteratorContext pi_ctx;
1144 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1145 GNUNET_assert (NULL != peer);
1146 GNUNET_assert (NULL != addresses);
1148 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1150 pi_ctx.it_cls = pi_it_cls;
1152 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1155 pi_it (pi_it_cls, NULL, NULL, NULL, 0, NULL, 0, zero_bw, zero_bw);
1160 /* end of gnunet-service-ats_addresses.c */