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"
41 * Assign each peer an equal amount of bandwidth (bw)
43 * bw_per_peer = bw_total / #active addresses
48 * Use MLP solver to assign bandwidth
53 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
55 static struct GAS_MLP_Handle *mlp;
57 static unsigned long long wan_quota_in;
59 static unsigned long long wan_quota_out;
61 static unsigned int active_addr_count;
67 * Update a bandwidth assignment for a peer. This trivial method currently
68 * simply assigns the same share to all active connections.
72 * @param value the 'struct ATS_Address'
73 * @return GNUNET_OK (continue to iterate)
76 update_bw_it (void *cls, const GNUNET_HashCode * key, void *value)
78 struct ATS_Address *aa = value;
82 if (GNUNET_YES != aa->active)
84 GNUNET_assert (active_addr_count > 0);
85 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
86 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
88 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
89 ntohl (aa->assigned_bw_out.value__));
90 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
91 aa->addr_len, aa->session_id,
92 aa->ats, aa->ats_count,
95 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
96 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
97 aa->ats, aa->ats_count, aa->assigned_bw_out,
104 * Some (significant) input changed, recalculate bandwidth assignment
108 recalculate_assigned_bw ()
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111 "Recalculating bandwidth for all active connections\n");
112 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
114 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
116 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL);
120 * Free the given address
121 * @param addr address to destroy
124 free_address (struct ATS_Address *addr)
126 GNUNET_free_non_null (addr->ats);
127 GNUNET_free (addr->plugin);
132 * Create a ATS_address with the given information
134 * @param plugin_name plugin
135 * @param plugin_addr address
136 * @param plugin_addr_len address length
137 * @param session_id session
138 * @return the ATS_Address
140 static struct ATS_Address *
141 create_address (const struct GNUNET_PeerIdentity *peer,
142 const char *plugin_name,
143 const void *plugin_addr, size_t plugin_addr_len,
146 struct ATS_Address *aa = NULL;
148 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
150 aa->addr_len = plugin_addr_len;
152 memcpy (&aa[1], plugin_addr, plugin_addr_len);
153 aa->plugin = GNUNET_strdup (plugin_name);
154 aa->session_id = session_id;
161 * Destroy the given address.
163 * @param addr address to destroy
164 * @return GNUNET_YES if bandwidth allocations should be recalcualted
167 destroy_address (struct ATS_Address *addr)
172 GNUNET_assert (GNUNET_YES ==
173 GNUNET_CONTAINER_multihashmap_remove (addresses,
174 &addr->peer.hashPubKey,
179 GAS_mlp_address_delete (mlp, addresses, addr);
182 if (GNUNET_YES == addr->active)
185 addr->active = GNUNET_NO;
193 struct CompareAddressContext
195 const struct ATS_Address *search;
196 struct ATS_Address *result;
201 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
203 struct CompareAddressContext *cac = cls;
204 struct ATS_Address *aa = value;
206 if (((aa->addr_len != cac->search->addr_len) ||
207 (0 != strcmp (aa->plugin, cac->search->plugin)) ||
208 (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len))) &&
209 ((aa->session_id != cac->search->session_id) ||
210 (cac->search->session_id == 0)))
218 * Find an existing equivalent address record.
219 * Compares by peer identity and network address OR by session ID
220 * (one of the two must match).
222 * @param peer peer to lookup addresses for
223 * @param addr existing address record
224 * @return existing address record, NULL for none
227 find_address (const struct GNUNET_PeerIdentity *peer,
228 const struct ATS_Address *addr)
230 struct CompareAddressContext cac;
234 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
235 &compare_address_it, &cac);
241 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
242 const char *plugin_name, const void *plugin_addr,
243 size_t plugin_addr_len, uint32_t session_id,
244 const struct GNUNET_ATS_Information *atsi,
247 struct ATS_Address *aa;
248 struct ATS_Address *old;
251 aa = create_address (peer,
253 plugin_addr, plugin_addr_len,
256 aa->mlp_information = NULL;
257 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
258 aa->ats_count = atsi_count;
259 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
261 old = find_address (peer, aa);
264 GNUNET_assert (GNUNET_OK ==
265 GNUNET_CONTAINER_multihashmap_put (addresses,
266 &peer->hashPubKey, aa,
267 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
269 GNUNET_i2s (peer), aa);
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Updated existing address for peer `%s' %X \n",
276 GNUNET_i2s (peer), old);
277 GNUNET_free_non_null (old->ats);
278 old->session_id = session_id;
282 old->ats_count = aa->ats_count;
283 GNUNET_free (aa->plugin);
286 for (i = 0; i < atsi_count; i++)
287 switch (ntohl (atsi[i].type))
289 case GNUNET_ATS_UTILIZATION_UP:
290 old->atsp_utilization_out.value__ = atsi[i].value;
292 case GNUNET_ATS_UTILIZATION_DOWN:
293 old->atsp_utilization_in.value__ = atsi[i].value;
295 case GNUNET_ATS_QUALITY_NET_DELAY:
296 old->atsp_latency.rel_value = ntohl (atsi[i].value);
298 case GNUNET_ATS_QUALITY_NET_DISTANCE:
299 old->atsp_distance = ntohl (atsi[i].value);
301 case GNUNET_ATS_COST_WAN:
302 old->atsp_cost_wan = ntohl (atsi[i].value);
304 case GNUNET_ATS_COST_LAN:
305 old->atsp_cost_lan = ntohl (atsi[i].value);
307 case GNUNET_ATS_COST_WLAN:
308 old->atsp_cost_wlan = ntohl (atsi[i].value);
310 case GNUNET_ATS_NETWORK_TYPE:
311 old->atsp_network_type = ntohl (atsi[i].value);
315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
316 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
322 GAS_mlp_address_update (mlp, addresses, old);
330 * If session != 0, just the session is deleted, the address itself still exists
331 * If session == 0, remove full address
332 * If session == 0 and addrlen == 0, destroy inbound address
336 * @param value the 'struct ATS_Address'
337 * @return GNUNET_OK (continue to iterate)
340 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
342 const struct ATS_Address *info = cls;
343 struct ATS_Address *aa = value;
346 memcmp (&aa->peer, &info->peer,
347 sizeof (struct GNUNET_PeerIdentity)));
348 /* session == 0, remove full address */
349 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
350 (aa->addr_len == info->addr_len) &&
351 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "Deleting address for peer `%s': `%s'\n",
355 GNUNET_i2s (&aa->peer), aa->plugin);
356 if (GNUNET_YES == destroy_address (aa))
357 recalculate_assigned_bw ();
360 /* session != 0, just remove session */
361 if (aa->session_id != info->session_id)
362 return GNUNET_OK; /* irrelevant */
363 if (aa->session_id != 0)
364 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
368 if (GNUNET_YES == aa->active)
370 aa->active = GNUNET_NO;
372 recalculate_assigned_bw ();
375 /* session == 0 and addrlen == 0 : destroy address */
376 if (aa->addr_len == 0)
378 (void) destroy_address (aa);
382 /* session was set to 0, update address */
385 GAS_mlp_address_update (mlp, addresses, aa);
393 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
394 const char *plugin_name, const void *plugin_addr,
395 size_t plugin_addr_len, uint32_t session_id)
397 struct ATS_Address *aa;
399 GNUNET_break (0 < strlen (plugin_name));
400 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
402 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
403 &destroy_by_session_id, aa);
410 * Find a "good" address to use for a peer. If we already have an existing
411 * address, we stick to it. Otherwise, we pick by lowest distance and then
414 * @param cls the 'struct ATS_Address**' where we store the result
416 * @param value another 'struct ATS_Address*' to consider using
417 * @return GNUNET_OK (continue to iterate)
420 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
422 struct ATS_Address **ap = cls;
423 struct ATS_Address *aa = (struct ATS_Address *) value;
424 struct ATS_Address *ab = *ap;
431 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
432 (ntohl (aa->assigned_bw_in.value__) > 0))
434 /* stick to existing connection */
438 if (ab->atsp_distance > aa->atsp_distance)
440 /* user shorter distance */
444 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
446 /* user lower latency */
456 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
457 const char *plugin_name, const void *plugin_addr,
458 size_t plugin_addr_len, uint32_t session_id, int in_use)
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
463 GNUNET_i2s (peer), in_use);
466 struct ATS_Address *aa;
467 struct ATS_Address *old;
469 aa = create_address(peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
470 old = find_address (peer, aa);
473 GNUNET_assert (old != NULL);
474 GNUNET_assert (in_use != old->used);
479 GAS_mlp_address_update (mlp, addresses, old);
484 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
486 struct ATS_Address *aa;
489 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
490 &find_address_it, &aa);
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
497 if (aa->active == GNUNET_NO)
499 aa->active = GNUNET_YES;
501 recalculate_assigned_bw ();
505 /* just to be sure... */
506 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
507 aa->addr_len, aa->session_id,
508 aa->ats, aa->ats_count,
515 // FIXME: this function should likely end up in the LP-subsystem and
516 // not with 'addresses' in the future...
518 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
519 enum GNUNET_ATS_PreferenceKind kind,
522 // do nothing for now...
528 * Initialize address subsystem.
530 * @param cfg configuration to use
531 * @param stats the statistics handle to use
534 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
535 const struct GNUNET_STATISTICS_Handle *stats)
537 GNUNET_assert (GNUNET_OK ==
538 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
541 GNUNET_assert (GNUNET_OK ==
542 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
548 switch (GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP"))
554 /* Init the MLP solver with default values */
555 mlp = GAS_mlp_init (stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
566 /* No configuration value */
574 addresses = GNUNET_CONTAINER_multihashmap_create (128);
579 * Free memory of address.
582 * @param key peer identity (unused)
583 * @param value the 'struct ATS_Address' to free
584 * @return GNUNET_OK (continue to iterate)
587 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
589 struct ATS_Address *aa = value;
591 destroy_address (aa);
597 GAS_addresses_destroy_all ()
599 if (addresses != NULL)
600 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
601 GNUNET_assert (active_addr_count == 0);
606 * Shutdown address subsystem.
609 GAS_addresses_done ()
611 GAS_addresses_destroy_all ();
612 GNUNET_CONTAINER_multihashmap_destroy (addresses);
624 /* end of gnunet-service-ats_addresses.c */