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
43 * Assign each peer an equal amount of bandwidth (bw)
45 * bw_per_peer = bw_total / #active addresses
50 * Use MLP solver to assign bandwidth
55 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
58 static struct GAS_MLP_Handle *mlp;
61 static unsigned long long wan_quota_in;
63 static unsigned long long wan_quota_out;
65 static unsigned int active_addr_count;
73 send_bw_notification (struct ATS_Address *aa)
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
76 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
77 ntohl (aa->assigned_bw_out.value__));
78 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
79 aa->addr_len, aa->session_id,
80 aa->ats, aa->ats_count,
83 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
84 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
85 aa->ats, aa->ats_count, aa->assigned_bw_out,
90 * Update a bandwidth assignment for a peer. This trivial method currently
91 * simply assigns the same share to all active connections.
95 * @param value the 'struct ATS_Address'
96 * @return GNUNET_OK (continue to iterate)
99 update_bw_simple_it (void *cls, const GNUNET_HashCode * key, void *value)
101 struct ATS_Address *aa = value;
103 if (GNUNET_YES != aa->active)
105 GNUNET_assert (active_addr_count > 0);
109 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
110 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
112 send_bw_notification (aa);
119 * Some (significant) input changed, recalculate bandwidth assignment
123 recalculate_assigned_bw ()
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126 "Recalculating bandwidth for all active connections\n");
127 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
129 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
132 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_simple_it, NULL);
136 * Free the given address
137 * @param addr address to destroy
140 free_address (struct ATS_Address *addr)
142 GNUNET_free_non_null (addr->ats);
143 GNUNET_free (addr->plugin);
148 * Create a ATS_address with the given information
150 * @param plugin_name plugin
151 * @param plugin_addr address
152 * @param plugin_addr_len address length
153 * @param session_id session
154 * @return the ATS_Address
156 static struct ATS_Address *
157 create_address (const struct GNUNET_PeerIdentity *peer,
158 const char *plugin_name,
159 const void *plugin_addr, size_t plugin_addr_len,
162 struct ATS_Address *aa = NULL;
164 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
166 aa->addr_len = plugin_addr_len;
168 memcpy (&aa[1], plugin_addr, plugin_addr_len);
169 aa->plugin = GNUNET_strdup (plugin_name);
170 aa->session_id = session_id;
171 aa->mlp_information = NULL;
179 * Destroy the given address.
181 * @param addr address to destroy
182 * @return GNUNET_YES if bandwidth allocations should be recalcualted
185 destroy_address (struct ATS_Address *addr)
190 GNUNET_assert (GNUNET_YES ==
191 GNUNET_CONTAINER_multihashmap_remove (addresses,
192 &addr->peer.hashPubKey,
197 GAS_mlp_address_delete (mlp, addresses, addr);
200 if (GNUNET_YES == addr->active)
203 addr->active = GNUNET_NO;
211 struct CompareAddressContext
213 const struct ATS_Address *search;
215 /* exact_address != NULL if address and session is equal */
216 struct ATS_Address *exact_address;
217 /* exact_address != NULL if address and session is 0 */
218 struct ATS_Address *base_address;
223 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
225 struct CompareAddressContext *cac = cls;
226 struct ATS_Address *aa = value;
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
229 "Comparing to: %s %s %u session %u\n",
230 GNUNET_i2s (&aa->peer), aa->plugin, aa->addr_len, aa->session_id);
233 /* find an exact matching address: aa->addr == cac->search->addr && aa->session == cac->search->session */
234 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
236 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
238 cac->exact_address = aa;
242 /* find an matching address: aa->addr == cac->search->addr && aa->session == 0 */
243 /* this address can be used to be updated */
244 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
246 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
248 cac->base_address = aa;
252 if (cac->exact_address == NULL)
260 * Find an existing equivalent address record.
261 * Compares by peer identity and network address OR by session ID
262 * (one of the two must match).
264 * @param peer peer to lookup addresses for
265 * @param addr existing address record
266 * @return existing address record, NULL for none
269 find_address (const struct GNUNET_PeerIdentity *peer,
270 const struct ATS_Address *addr)
272 struct CompareAddressContext cac;
274 cac.exact_address = NULL;
275 cac.base_address = NULL;
277 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
278 &compare_address_it, &cac);
281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282 "exact address: %s base address: %s\n",
283 (cac.exact_address != NULL) ? "YES" : "NO",
284 (cac.base_address != NULL) ? "YES" : "NO");
286 if (cac.exact_address == NULL)
287 return cac.base_address;
288 return cac.exact_address;
293 compare_address_session_it (void *cls, const GNUNET_HashCode * key, void *value)
295 struct CompareAddressContext *cac = cls;
296 struct ATS_Address *aa = value;
298 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
300 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
302 cac->exact_address = aa;
311 * Find an existing equivalent address record.
312 * Compares by peer identity and network address AND by session ID
313 * (one of the two must match).
315 * @param peer peer to lookup addresses for
316 * @param addr existing address record
317 * @return existing address record, NULL for none
320 find_exact_address (const struct GNUNET_PeerIdentity *peer,
321 const struct ATS_Address *addr)
323 struct CompareAddressContext cac;
325 cac.exact_address = NULL;
327 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
328 &compare_address_session_it, &cac);
329 return cac.exact_address;
334 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
335 const char *plugin_name, const void *plugin_addr,
336 size_t plugin_addr_len, uint32_t session_id,
337 const struct GNUNET_ATS_Information *atsi,
340 struct ATS_Address *aa;
341 struct ATS_Address *old;
344 if (GNUNET_NO == running)
347 GNUNET_assert (NULL != addresses);
349 aa = create_address (peer,
351 plugin_addr, plugin_addr_len,
354 aa->mlp_information = NULL;
355 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
356 aa->ats_count = atsi_count;
357 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' %u\n",
364 /* Get existing address or address with session == 0 */
365 old = find_address (peer, aa);
368 GNUNET_assert (GNUNET_OK ==
369 GNUNET_CONTAINER_multihashmap_put (addresses,
370 &peer->hashPubKey, aa,
371 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
374 GNUNET_i2s (peer), aa);
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Updated existing address for peer `%s' %p old session %u new session %u\n",
383 GNUNET_i2s (peer), old,
384 old->session_id, session_id);
386 GNUNET_free_non_null (old->ats);
387 old->session_id = session_id;
391 old->ats_count = aa->ats_count;
392 GNUNET_free (aa->plugin);
395 for (i = 0; i < atsi_count; i++)
396 switch (ntohl (atsi[i].type))
398 case GNUNET_ATS_UTILIZATION_UP:
399 old->atsp_utilization_out.value__ = atsi[i].value;
401 case GNUNET_ATS_UTILIZATION_DOWN:
402 old->atsp_utilization_in.value__ = atsi[i].value;
404 case GNUNET_ATS_QUALITY_NET_DELAY:
405 old->atsp_latency.rel_value = ntohl (atsi[i].value);
407 case GNUNET_ATS_QUALITY_NET_DISTANCE:
408 old->atsp_distance = ntohl (atsi[i].value);
410 case GNUNET_ATS_COST_WAN:
411 old->atsp_cost_wan = ntohl (atsi[i].value);
413 case GNUNET_ATS_COST_LAN:
414 old->atsp_cost_lan = ntohl (atsi[i].value);
416 case GNUNET_ATS_COST_WLAN:
417 old->atsp_cost_wlan = ntohl (atsi[i].value);
419 case GNUNET_ATS_NETWORK_TYPE:
420 old->atsp_network_type = ntohl (atsi[i].value);
424 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
425 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
431 GAS_mlp_address_update (mlp, addresses, old);
439 * If session != 0, just the session is deleted, the address itself still exists
440 * If session == 0, remove full address
441 * If session == 0 and addrlen == 0, destroy inbound address
445 * @param value the 'struct ATS_Address'
446 * @return GNUNET_OK (continue to iterate)
449 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
451 const struct ATS_Address *info = cls;
452 struct ATS_Address *aa = value;
455 memcmp (&aa->peer, &info->peer,
456 sizeof (struct GNUNET_PeerIdentity)));
457 /* session == 0, remove full address */
458 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
459 (aa->addr_len == info->addr_len) &&
460 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464 "Deleting address for peer `%s': `%s' %u\n",
465 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
467 if (GNUNET_YES == destroy_address (aa))
468 recalculate_assigned_bw ();
471 /* session != 0, just remove session */
472 if (aa->session_id != info->session_id)
473 return GNUNET_OK; /* irrelevant */
474 if (aa->session_id != 0)
475 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Deleting session for peer `%s': `%s' %u\n",
480 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
484 if (GNUNET_YES == aa->active)
486 aa->active = GNUNET_NO;
488 recalculate_assigned_bw ();
491 /* session == 0 and addrlen == 0 : destroy address */
492 if (aa->addr_len == 0)
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Deleting session and address for peer `%s': `%s' %u\n",
497 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
499 (void) destroy_address (aa);
503 /* session was set to 0, update address */
506 GAS_mlp_address_update (mlp, addresses, aa);
514 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
515 const char *plugin_name, const void *plugin_addr,
516 size_t plugin_addr_len, uint32_t session_id)
518 struct ATS_Address *aa;
520 if (GNUNET_NO == running)
523 GNUNET_break (0 < strlen (plugin_name));
524 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
526 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
527 &destroy_by_session_id, aa);
534 * Find a "good" address to use for a peer. If we already have an existing
535 * address, we stick to it. Otherwise, we pick by lowest distance and then
538 * @param cls the 'struct ATS_Address**' where we store the result
540 * @param value another 'struct ATS_Address*' to consider using
541 * @return GNUNET_OK (continue to iterate)
544 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
546 struct ATS_Address **ap = cls;
547 struct ATS_Address *aa = (struct ATS_Address *) value;
548 struct ATS_Address *ab = *ap;
555 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
556 (ntohl (aa->assigned_bw_in.value__) > 0))
558 /* stick to existing connection */
562 if (ab->atsp_distance > aa->atsp_distance)
564 /* user shorter distance */
568 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
570 /* user lower latency */
580 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
581 const char *plugin_name, const void *plugin_addr,
582 size_t plugin_addr_len, uint32_t session_id, int in_use)
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
587 GNUNET_i2s (peer), in_use);
590 struct ATS_Address *aa;
591 struct ATS_Address *old;
593 if (GNUNET_NO == running)
594 return GNUNET_SYSERR;
596 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
597 old = find_exact_address (peer, aa);
603 return GNUNET_SYSERR;
605 if (old->used == in_use)
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 "Address in use called multiple times for peer `%s': %s -> %s \n",
611 (GNUNET_NO == old->used) ? "NO" : "YES",
612 (GNUNET_NO == in_use) ? "NO" : "YES");
613 return GNUNET_SYSERR;
618 GAS_mlp_address_update (mlp, addresses, old);
624 void request_address_mlp (const struct GNUNET_PeerIdentity *peer)
626 struct ATS_Address *aa;
630 /* Get preferred address from MLP */
631 struct ATS_PreferedAddress * paddr = NULL;
632 paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
634 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
635 /* FIXME use bw in value */
636 paddr->bandwidth_in = paddr->bandwidth_out;
637 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
644 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
647 if (aa->active == GNUNET_NO)
649 aa->active = GNUNET_YES;
652 send_bw_notification (aa);
656 /* just to be sure... */
657 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
658 aa->addr_len, aa->session_id,
659 aa->ats, aa->ats_count,
666 void request_address_simple (const struct GNUNET_PeerIdentity *peer)
668 struct ATS_Address *aa;
671 /* Get address with: stick to current address, lower distance, lower latency */
672 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
673 &find_address_it, &aa);
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
677 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
681 if (aa->active == GNUNET_NO)
683 aa->active = GNUNET_YES;
685 if (ats_mode == SIMPLE)
687 recalculate_assigned_bw ();
692 /* just to be sure... */
693 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
694 aa->addr_len, aa->session_id,
695 aa->ats, aa->ats_count,
703 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
705 if (GNUNET_NO == running)
708 if (ats_mode == SIMPLE)
710 request_address_simple (peer);
714 request_address_mlp(peer);
719 // FIXME: this function should likely end up in the LP-subsystem and
720 // not with 'addresses' in the future...
722 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
723 enum GNUNET_ATS_PreferenceKind kind,
726 if (GNUNET_NO == running)
730 GAS_mlp_address_change_preference (mlp, peer, kind, score);
737 * Initialize address subsystem.
739 * @param cfg configuration to use
740 * @param stats the statistics handle to use
743 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
744 const struct GNUNET_STATISTICS_Handle *stats)
748 char *quota_wan_in_str;
749 char *quota_wan_out_str;
753 addresses = GNUNET_CONTAINER_multihashmap_create (128);
754 GNUNET_assert (NULL != addresses);
756 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_wan_in_str))
758 if (0 == strcmp(quota_wan_in_str, "unlimited") ||
759 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &wan_quota_in)))
760 wan_quota_in = (UINT32_MAX) /10;
762 GNUNET_free (quota_wan_in_str);
763 quota_wan_in_str = NULL;
767 wan_quota_in = (UINT32_MAX) /10;
770 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_wan_out_str))
772 if (0 == strcmp(quota_wan_out_str, "unlimited") ||
773 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &wan_quota_out)))
774 wan_quota_out = (UINT32_MAX) /10;
776 GNUNET_free (quota_wan_out_str);
777 quota_wan_out_str = NULL;
781 wan_quota_out = (UINT32_MAX) /10;
784 mode = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP");
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP mode %u", mode);
792 /* Init the MLP solver with default values */
793 mlp = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
796 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode\n");
797 GNUNET_STATISTICS_update (GSA_stats, "MLP mode enabled", 0, GNUNET_NO);
802 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 1, GNUNET_NO);
806 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
807 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
813 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
816 /* No configuration value */
818 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started with %s mode\n", (SIMPLE == ats_mode) ? "SIMPLE" : "MLP");
825 running = GNUNET_YES;
830 * Free memory of address.
833 * @param key peer identity (unused)
834 * @param value the 'struct ATS_Address' to free
835 * @return GNUNET_OK (continue to iterate)
838 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
840 struct ATS_Address *aa = value;
842 destroy_address (aa);
848 GAS_addresses_destroy_all ()
850 if (GNUNET_NO == running)
853 if (addresses != NULL)
854 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
855 GNUNET_assert (active_addr_count == 0);
860 * Shutdown address subsystem.
863 GAS_addresses_done ()
865 GAS_addresses_destroy_all ();
867 GNUNET_CONTAINER_multihashmap_destroy (addresses);
879 /* end of gnunet-service-ats_addresses.c */