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 unsigned long long wan_quota_in;
57 static unsigned long long wan_quota_out;
59 static unsigned int active_addr_count;
65 * Update a bandwidth assignment for a peer. This trivial method currently
66 * simply assigns the same share to all active connections.
70 * @param value the 'struct ATS_Address'
71 * @return GNUNET_OK (continue to iterate)
74 update_bw_it (void *cls, const GNUNET_HashCode * key, void *value)
76 struct ATS_Address *aa = value;
80 if (GNUNET_YES != aa->active)
82 GNUNET_assert (active_addr_count > 0);
83 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
84 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
86 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
87 ntohl (aa->assigned_bw_out.value__));
88 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
89 aa->addr_len, aa->session_id,
90 aa->ats, aa->ats_count,
93 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
94 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
95 aa->ats, aa->ats_count, aa->assigned_bw_out,
102 * Some (significant) input changed, recalculate bandwidth assignment
106 recalculate_assigned_bw ()
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109 "Recalculating bandwidth for all active connections\n");
110 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
112 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
114 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL);
118 * Free the given address
119 * @param addr address to destroy
122 free_address (struct ATS_Address *addr)
124 GNUNET_free_non_null (addr->ats);
125 GNUNET_free (addr->plugin);
130 * Create a ATS_address with the given information
132 * @param plugin_name plugin
133 * @param plugin_addr address
134 * @param plugin_addr_len address length
135 * @param session_id session
136 * @return the ATS_Address
138 static struct ATS_Address *
139 create_address (const struct GNUNET_PeerIdentity *peer,
140 const char *plugin_name,
141 const void *plugin_addr, size_t plugin_addr_len,
144 struct ATS_Address *aa = NULL;
146 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
148 aa->addr_len = plugin_addr_len;
150 memcpy (&aa[1], plugin_addr, plugin_addr_len);
151 aa->plugin = GNUNET_strdup (plugin_name);
152 aa->session_id = session_id;
159 * Destroy the given address.
161 * @param addr address to destroy
162 * @return GNUNET_YES if bandwidth allocations should be recalcualted
165 destroy_address (struct ATS_Address *addr)
170 GNUNET_assert (GNUNET_YES ==
171 GNUNET_CONTAINER_multihashmap_remove (addresses,
172 &addr->peer.hashPubKey,
177 GAS_mlp_address_delete (addresses, addr);
180 if (GNUNET_YES == addr->active)
183 addr->active = GNUNET_NO;
191 struct CompareAddressContext
193 const struct ATS_Address *search;
194 struct ATS_Address *result;
199 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
201 struct CompareAddressContext *cac = cls;
202 struct ATS_Address *aa = value;
204 if (((aa->addr_len != cac->search->addr_len) ||
205 (0 != strcmp (aa->plugin, cac->search->plugin)) ||
206 (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len))) &&
207 ((aa->session_id != cac->search->session_id) ||
208 (cac->search->session_id == 0)))
216 * Find an existing equivalent address record.
217 * Compares by peer identity and network address OR by session ID
218 * (one of the two must match).
220 * @param peer peer to lookup addresses for
221 * @param addr existing address record
222 * @return existing address record, NULL for none
225 find_address (const struct GNUNET_PeerIdentity *peer,
226 const struct ATS_Address *addr)
228 struct CompareAddressContext cac;
232 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
233 &compare_address_it, &cac);
239 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
240 const char *plugin_name, const void *plugin_addr,
241 size_t plugin_addr_len, uint32_t session_id,
242 const struct GNUNET_ATS_Information *atsi,
245 struct ATS_Address *aa;
246 struct ATS_Address *old;
249 aa = create_address (peer,
251 plugin_addr, plugin_addr_len,
254 aa->mlp_information = NULL;
255 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
256 aa->ats_count = atsi_count;
257 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
259 old = find_address (peer, aa);
262 GNUNET_assert (GNUNET_OK ==
263 GNUNET_CONTAINER_multihashmap_put (addresses,
264 &peer->hashPubKey, aa,
265 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
267 GNUNET_i2s (peer), aa);
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Updated existing address for peer `%s' %X \n",
274 GNUNET_i2s (peer), old);
275 GNUNET_free_non_null (old->ats);
276 old->session_id = session_id;
280 old->ats_count = aa->ats_count;
281 GNUNET_free (aa->plugin);
284 for (i = 0; i < atsi_count; i++)
285 switch (ntohl (atsi[i].type))
287 case GNUNET_ATS_UTILIZATION_UP:
288 old->atsp_utilization_out.value__ = atsi[i].value;
290 case GNUNET_ATS_UTILIZATION_DOWN:
291 old->atsp_utilization_in.value__ = atsi[i].value;
293 case GNUNET_ATS_QUALITY_NET_DELAY:
294 old->atsp_latency.rel_value = ntohl (atsi[i].value);
296 case GNUNET_ATS_QUALITY_NET_DISTANCE:
297 old->atsp_distance = ntohl (atsi[i].value);
299 case GNUNET_ATS_COST_WAN:
300 old->atsp_cost_wan = ntohl (atsi[i].value);
302 case GNUNET_ATS_COST_LAN:
303 old->atsp_cost_lan = ntohl (atsi[i].value);
305 case GNUNET_ATS_COST_WLAN:
306 old->atsp_cost_wlan = ntohl (atsi[i].value);
308 case GNUNET_ATS_NETWORK_TYPE:
309 old->atsp_network_type = ntohl (atsi[i].value);
313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
314 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
320 GAS_mlp_address_update (addresses, old);
328 * If session != 0, just the session is deleted, the address itself still exists
329 * If session == 0, remove full address
330 * If session == 0 and addrlen == 0, destroy inbound address
334 * @param value the 'struct ATS_Address'
335 * @return GNUNET_OK (continue to iterate)
338 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
340 const struct ATS_Address *info = cls;
341 struct ATS_Address *aa = value;
344 memcmp (&aa->peer, &info->peer,
345 sizeof (struct GNUNET_PeerIdentity)));
346 /* session == 0, remove full address */
347 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
348 (aa->addr_len == info->addr_len) &&
349 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Deleting address for peer `%s': `%s'\n",
353 GNUNET_i2s (&aa->peer), aa->plugin);
354 if (GNUNET_YES == destroy_address (aa))
355 recalculate_assigned_bw ();
358 /* session != 0, just remove session */
359 if (aa->session_id != info->session_id)
360 return GNUNET_OK; /* irrelevant */
361 if (aa->session_id != 0)
362 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
366 if (GNUNET_YES == aa->active)
368 aa->active = GNUNET_NO;
370 recalculate_assigned_bw ();
373 /* session == 0 and addrlen == 0 : destroy address */
374 if (aa->addr_len == 0)
376 (void) destroy_address (aa);
380 /* session was set to 0, update address */
383 GAS_mlp_address_update (addresses, aa);
391 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
392 const char *plugin_name, const void *plugin_addr,
393 size_t plugin_addr_len, uint32_t session_id)
395 struct ATS_Address *aa;
397 GNUNET_break (0 < strlen (plugin_name));
398 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
400 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
401 &destroy_by_session_id, aa);
408 * Find a "good" address to use for a peer. If we already have an existing
409 * address, we stick to it. Otherwise, we pick by lowest distance and then
412 * @param cls the 'struct ATS_Address**' where we store the result
414 * @param value another 'struct ATS_Address*' to consider using
415 * @return GNUNET_OK (continue to iterate)
418 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
420 struct ATS_Address **ap = cls;
421 struct ATS_Address *aa = (struct ATS_Address *) value;
422 struct ATS_Address *ab = *ap;
429 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
430 (ntohl (aa->assigned_bw_in.value__) > 0))
432 /* stick to existing connection */
436 if (ab->atsp_distance > aa->atsp_distance)
438 /* user shorter distance */
442 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
444 /* user lower latency */
454 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
455 const char *plugin_name, const void *plugin_addr,
456 size_t plugin_addr_len, uint32_t session_id, int in_use)
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
461 GNUNET_i2s (peer), in_use);
464 struct ATS_Address *aa;
465 struct ATS_Address *old;
467 aa = create_address(peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
468 old = find_address (peer, aa);
471 GNUNET_assert (old != NULL);
472 GNUNET_assert (in_use != old->used);
477 GAS_mlp_address_update (addresses, old);
482 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
484 struct ATS_Address *aa;
487 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
488 &find_address_it, &aa);
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
495 if (aa->active == GNUNET_NO)
497 aa->active = GNUNET_YES;
499 recalculate_assigned_bw ();
503 /* just to be sure... */
504 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
505 aa->addr_len, aa->session_id,
506 aa->ats, aa->ats_count,
513 // FIXME: this function should likely end up in the LP-subsystem and
514 // not with 'addresses' in the future...
516 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
517 enum GNUNET_ATS_PreferenceKind kind,
520 // do nothing for now...
526 * Initialize address subsystem.
528 * @param cfg configuration to use
529 * @param stats the statistics handle to use
532 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
533 const struct GNUNET_STATISTICS_Handle *stats)
535 GNUNET_assert (GNUNET_OK ==
536 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
539 GNUNET_assert (GNUNET_OK ==
540 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
546 switch (GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP"))
552 /* Init the MLP solver with default values */
553 GAS_mlp_init (stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
556 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
564 /* No configuration value */
572 addresses = GNUNET_CONTAINER_multihashmap_create (128);
577 * Free memory of address.
580 * @param key peer identity (unused)
581 * @param value the 'struct ATS_Address' to free
582 * @return GNUNET_OK (continue to iterate)
585 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
587 struct ATS_Address *aa = value;
589 destroy_address (aa);
595 GAS_addresses_destroy_all ()
597 if (addresses != NULL)
598 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
599 GNUNET_assert (active_addr_count == 0);
604 * Shutdown address subsystem.
607 GAS_addresses_done ()
609 GAS_addresses_destroy_all ();
610 GNUNET_CONTAINER_multihashmap_destroy (addresses);
622 /* end of gnunet-service-ats_addresses.c */