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_EXTRA_LOGGING
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;
71 * Update a bandwidth assignment for a peer. This trivial method currently
72 * simply assigns the same share to all active connections.
76 * @param value the 'struct ATS_Address'
77 * @return GNUNET_OK (continue to iterate)
80 update_bw_simple_it (void *cls, const GNUNET_HashCode * key, void *value)
82 struct ATS_Address *aa = value;
84 if (GNUNET_YES != aa->active)
86 GNUNET_assert (active_addr_count > 0);
90 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
91 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
94 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
95 ntohl (aa->assigned_bw_out.value__));
96 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
97 aa->addr_len, aa->session_id,
98 aa->ats, aa->ats_count,
101 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
102 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
103 aa->ats, aa->ats_count, aa->assigned_bw_out,
110 * Some (significant) input changed, recalculate bandwidth assignment
114 recalculate_assigned_bw ()
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117 "Recalculating bandwidth for all active connections\n");
118 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
120 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
123 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_simple_it, NULL);
127 * Free the given address
128 * @param addr address to destroy
131 free_address (struct ATS_Address *addr)
133 GNUNET_free_non_null (addr->ats);
134 GNUNET_free (addr->plugin);
139 * Create a ATS_address with the given information
141 * @param plugin_name plugin
142 * @param plugin_addr address
143 * @param plugin_addr_len address length
144 * @param session_id session
145 * @return the ATS_Address
147 static struct ATS_Address *
148 create_address (const struct GNUNET_PeerIdentity *peer,
149 const char *plugin_name,
150 const void *plugin_addr, size_t plugin_addr_len,
153 struct ATS_Address *aa = NULL;
155 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
157 aa->addr_len = plugin_addr_len;
159 memcpy (&aa[1], plugin_addr, plugin_addr_len);
160 aa->plugin = GNUNET_strdup (plugin_name);
161 aa->session_id = session_id;
162 aa->mlp_information = NULL;
170 * Destroy the given address.
172 * @param addr address to destroy
173 * @return GNUNET_YES if bandwidth allocations should be recalcualted
176 destroy_address (struct ATS_Address *addr)
181 GNUNET_assert (GNUNET_YES ==
182 GNUNET_CONTAINER_multihashmap_remove (addresses,
183 &addr->peer.hashPubKey,
188 GAS_mlp_address_delete (mlp, addresses, addr);
191 if (GNUNET_YES == addr->active)
194 addr->active = GNUNET_NO;
202 struct CompareAddressContext
204 const struct ATS_Address *search;
206 /* exact_address != NULL if address and session is equal */
207 struct ATS_Address *exact_address;
208 /* exact_address != NULL if address and session is 0 */
209 struct ATS_Address *base_address;
214 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
216 struct CompareAddressContext *cac = cls;
217 struct ATS_Address *aa = value;
219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
220 "Comparing to: %s %s %u session %u\n",
221 GNUNET_i2s (&aa->peer), aa->plugin, aa->addr_len, aa->session_id);
224 /* find an exact matching address: aa->addr == cac->search->addr && aa->session == cac->search->session */
225 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
227 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
229 cac->exact_address = aa;
233 /* find an matching address: aa->addr == cac->search->addr && aa->session == 0 */
234 /* this address can be used to be updated */
235 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
237 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
239 cac->base_address = aa;
243 if (cac->exact_address == NULL)
251 * Find an existing equivalent address record.
252 * Compares by peer identity and network address OR by session ID
253 * (one of the two must match).
255 * @param peer peer to lookup addresses for
256 * @param addr existing address record
257 * @return existing address record, NULL for none
260 find_address (const struct GNUNET_PeerIdentity *peer,
261 const struct ATS_Address *addr)
263 struct CompareAddressContext cac;
265 cac.exact_address = NULL;
266 cac.base_address = NULL;
268 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
269 &compare_address_it, &cac);
272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
273 "exact address: %s base address: %s\n",
274 (cac.exact_address != NULL) ? "YES" : "NO",
275 (cac.base_address != NULL) ? "YES" : "NO");
277 if (cac.exact_address == NULL)
278 return cac.base_address;
279 return cac.exact_address;
284 compare_address_session_it (void *cls, const GNUNET_HashCode * key, void *value)
286 struct CompareAddressContext *cac = cls;
287 struct ATS_Address *aa = value;
289 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
291 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
293 cac->exact_address = aa;
302 * Find an existing equivalent address record.
303 * Compares by peer identity and network address AND by session ID
304 * (one of the two must match).
306 * @param peer peer to lookup addresses for
307 * @param addr existing address record
308 * @return existing address record, NULL for none
311 find_exact_address (const struct GNUNET_PeerIdentity *peer,
312 const struct ATS_Address *addr)
314 struct CompareAddressContext cac;
316 cac.exact_address = NULL;
318 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
319 &compare_address_session_it, &cac);
320 return cac.exact_address;
325 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
326 const char *plugin_name, const void *plugin_addr,
327 size_t plugin_addr_len, uint32_t session_id,
328 const struct GNUNET_ATS_Information *atsi,
331 struct ATS_Address *aa;
332 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));
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' %u\n",
350 /* Get existing address or address with session == 0 */
351 old = find_address (peer, aa);
354 GNUNET_assert (GNUNET_OK ==
355 GNUNET_CONTAINER_multihashmap_put (addresses,
356 &peer->hashPubKey, aa,
357 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
360 GNUNET_i2s (peer), aa);
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 "Updated existing address for peer `%s' %p old session %u new session %u\n",
369 GNUNET_i2s (peer), old,
370 old->session_id, session_id);
372 GNUNET_free_non_null (old->ats);
373 old->session_id = session_id;
377 old->ats_count = aa->ats_count;
378 GNUNET_free (aa->plugin);
381 for (i = 0; i < atsi_count; i++)
382 switch (ntohl (atsi[i].type))
384 case GNUNET_ATS_UTILIZATION_UP:
385 old->atsp_utilization_out.value__ = atsi[i].value;
387 case GNUNET_ATS_UTILIZATION_DOWN:
388 old->atsp_utilization_in.value__ = atsi[i].value;
390 case GNUNET_ATS_QUALITY_NET_DELAY:
391 old->atsp_latency.rel_value = ntohl (atsi[i].value);
393 case GNUNET_ATS_QUALITY_NET_DISTANCE:
394 old->atsp_distance = ntohl (atsi[i].value);
396 case GNUNET_ATS_COST_WAN:
397 old->atsp_cost_wan = ntohl (atsi[i].value);
399 case GNUNET_ATS_COST_LAN:
400 old->atsp_cost_lan = ntohl (atsi[i].value);
402 case GNUNET_ATS_COST_WLAN:
403 old->atsp_cost_wlan = ntohl (atsi[i].value);
405 case GNUNET_ATS_NETWORK_TYPE:
406 old->atsp_network_type = ntohl (atsi[i].value);
410 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
411 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
417 GAS_mlp_address_update (mlp, addresses, old);
425 * If session != 0, just the session is deleted, the address itself still exists
426 * If session == 0, remove full address
427 * If session == 0 and addrlen == 0, destroy inbound address
431 * @param value the 'struct ATS_Address'
432 * @return GNUNET_OK (continue to iterate)
435 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
437 const struct ATS_Address *info = cls;
438 struct ATS_Address *aa = value;
441 memcmp (&aa->peer, &info->peer,
442 sizeof (struct GNUNET_PeerIdentity)));
443 /* session == 0, remove full address */
444 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
445 (aa->addr_len == info->addr_len) &&
446 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450 "Deleting address for peer `%s': `%s' %u\n",
451 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
453 if (GNUNET_YES == destroy_address (aa))
454 recalculate_assigned_bw ();
457 /* session != 0, just remove session */
458 if (aa->session_id != info->session_id)
459 return GNUNET_OK; /* irrelevant */
460 if (aa->session_id != 0)
461 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465 "Deleting session for peer `%s': `%s' %u\n",
466 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
470 if (GNUNET_YES == aa->active)
472 aa->active = GNUNET_NO;
474 recalculate_assigned_bw ();
477 /* session == 0 and addrlen == 0 : destroy address */
478 if (aa->addr_len == 0)
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Deleting session and address for peer `%s': `%s' %u\n",
483 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
485 (void) destroy_address (aa);
489 /* session was set to 0, update address */
492 GAS_mlp_address_update (mlp, addresses, aa);
500 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
501 const char *plugin_name, const void *plugin_addr,
502 size_t plugin_addr_len, uint32_t session_id)
504 struct ATS_Address *aa;
506 GNUNET_break (0 < strlen (plugin_name));
507 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
509 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
510 &destroy_by_session_id, aa);
517 * Find a "good" address to use for a peer. If we already have an existing
518 * address, we stick to it. Otherwise, we pick by lowest distance and then
521 * @param cls the 'struct ATS_Address**' where we store the result
523 * @param value another 'struct ATS_Address*' to consider using
524 * @return GNUNET_OK (continue to iterate)
527 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
529 struct ATS_Address **ap = cls;
530 struct ATS_Address *aa = (struct ATS_Address *) value;
531 struct ATS_Address *ab = *ap;
538 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
539 (ntohl (aa->assigned_bw_in.value__) > 0))
541 /* stick to existing connection */
545 if (ab->atsp_distance > aa->atsp_distance)
547 /* user shorter distance */
551 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
553 /* user lower latency */
563 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
564 const char *plugin_name, const void *plugin_addr,
565 size_t plugin_addr_len, uint32_t session_id, int in_use)
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
570 GNUNET_i2s (peer), in_use);
573 struct ATS_Address *aa;
574 struct ATS_Address *old;
577 aa = create_address(peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
578 old = find_exact_address (peer, aa);
581 GNUNET_assert (old != NULL);
582 GNUNET_assert (old->used != in_use);
587 GAS_mlp_address_update (mlp, addresses, old);
592 void request_address_mlp (const struct GNUNET_PeerIdentity *peer)
594 struct ATS_Address *aa;
598 /* Get preferred address from MLP */
599 struct ATS_PreferedAddress * paddr = NULL;
600 paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
602 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
603 /* FIXME use bw in value */
604 paddr->bandwidth_in = paddr->bandwidth_out;
605 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
615 if (aa->active == GNUNET_NO)
617 aa->active = GNUNET_YES;
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
621 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
622 ntohl (aa->assigned_bw_out.value__));
623 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
624 aa->addr_len, aa->session_id,
625 aa->ats, aa->ats_count,
628 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
629 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
630 aa->ats, aa->ats_count, aa->assigned_bw_out,
635 /* just to be sure... */
636 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
637 aa->addr_len, aa->session_id,
638 aa->ats, aa->ats_count,
645 void request_address_simple (const struct GNUNET_PeerIdentity *peer)
647 struct ATS_Address *aa;
650 /* Get address with: stick to current address, lower distance, lower latency */
651 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
652 &find_address_it, &aa);
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
660 if (aa->active == GNUNET_NO)
662 aa->active = GNUNET_YES;
664 if (ats_mode == SIMPLE)
666 recalculate_assigned_bw ();
671 /* just to be sure... */
672 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
673 aa->addr_len, aa->session_id,
674 aa->ats, aa->ats_count,
682 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
684 if (ats_mode == SIMPLE)
686 request_address_simple (peer);
690 request_address_mlp(peer);
695 // FIXME: this function should likely end up in the LP-subsystem and
696 // not with 'addresses' in the future...
698 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
699 enum GNUNET_ATS_PreferenceKind kind,
704 GAS_mlp_address_change_preference (mlp, peer, kind, score);
711 * Initialize address subsystem.
713 * @param cfg configuration to use
714 * @param stats the statistics handle to use
717 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
718 const struct GNUNET_STATISTICS_Handle *stats)
720 GNUNET_assert (GNUNET_OK ==
721 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
724 GNUNET_assert (GNUNET_OK ==
725 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
729 switch (GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP"))
735 /* Init the MLP solver with default values */
736 mlp = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
739 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
747 /* No configuration value */
755 addresses = GNUNET_CONTAINER_multihashmap_create (128);
760 * Free memory of address.
763 * @param key peer identity (unused)
764 * @param value the 'struct ATS_Address' to free
765 * @return GNUNET_OK (continue to iterate)
768 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
770 struct ATS_Address *aa = value;
772 destroy_address (aa);
778 GAS_addresses_destroy_all ()
780 if (addresses != NULL)
781 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
782 GNUNET_assert (active_addr_count == 0);
787 * Shutdown address subsystem.
790 GAS_addresses_done ()
792 GAS_addresses_destroy_all ();
793 GNUNET_CONTAINER_multihashmap_destroy (addresses);
805 /* end of gnunet-service-ats_addresses.c */