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"
44 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
46 static unsigned long long wan_quota_in;
48 static unsigned long long wan_quota_out;
50 static unsigned int active_addr_count;
55 * Update a bandwidth assignment for a peer. This trivial method currently
56 * simply assigns the same share to all active connections.
60 * @param value the 'struct ATS_Address'
61 * @return GNUNET_OK (continue to iterate)
64 update_bw_it (void *cls, const GNUNET_HashCode * key, void *value)
66 struct ATS_Address *aa = value;
68 if (GNUNET_YES != aa->active)
70 GNUNET_assert (active_addr_count > 0);
71 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
72 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
74 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
75 ntohl (aa->assigned_bw_out.value__));
76 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
77 aa->addr_len, aa->session_id,
78 aa->ats, aa->ats_count,
81 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
82 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
83 aa->ats, aa->ats_count, aa->assigned_bw_out,
90 * Some (significant) input changed, recalculate bandwidth assignment
94 recalculate_assigned_bw ()
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97 "Recalculating bandwidth for all active connections\n");
98 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
100 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
102 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL);
106 * Free the given address
107 * @param addr address to destroy
110 free_address (struct ATS_Address *addr)
112 GNUNET_free_non_null (addr->ats);
113 GNUNET_free (addr->plugin);
118 * Create a ATS_address with the given information
120 * @param plugin_name plugin
121 * @param plugin_addr address
122 * @param plugin_addr_len address length
123 * @param session_id session
124 * @return the ATS_Address
126 static struct ATS_Address *
127 create_address (const struct GNUNET_PeerIdentity *peer,
128 const char *plugin_name,
129 const void *plugin_addr, size_t plugin_addr_len,
132 struct ATS_Address *aa = NULL;
134 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
136 aa->addr_len = plugin_addr_len;
138 memcpy (&aa[1], plugin_addr, plugin_addr_len);
139 aa->plugin = GNUNET_strdup (plugin_name);
140 aa->session_id = session_id;
147 * Destroy the given address.
149 * @param addr address to destroy
150 * @return GNUNET_YES if bandwidth allocations should be recalcualted
153 destroy_address (struct ATS_Address *addr)
158 GNUNET_assert (GNUNET_YES ==
159 GNUNET_CONTAINER_multihashmap_remove (addresses,
160 &addr->peer.hashPubKey,
162 if (GNUNET_YES == addr->active)
165 addr->active = GNUNET_NO;
173 struct CompareAddressContext
175 const struct ATS_Address *search;
176 struct ATS_Address *result;
181 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
183 struct CompareAddressContext *cac = cls;
184 struct ATS_Address *aa = value;
186 if (((aa->addr_len != cac->search->addr_len) ||
187 (0 != strcmp (aa->plugin, cac->search->plugin)) ||
188 (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len))) &&
189 ((aa->session_id != cac->search->session_id) ||
190 (cac->search->session_id == 0)))
198 * Find an existing equivalent address record.
199 * Compares by peer identity and network address OR by session ID
200 * (one of the two must match).
202 * @param peer peer to lookup addresses for
203 * @param addr existing address record
204 * @return existing address record, NULL for none
207 find_address (const struct GNUNET_PeerIdentity *peer,
208 const struct ATS_Address *addr)
210 struct CompareAddressContext cac;
214 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
215 &compare_address_it, &cac);
221 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
222 const char *plugin_name, const void *plugin_addr,
223 size_t plugin_addr_len, uint32_t session_id,
224 const struct GNUNET_ATS_Information *atsi,
227 struct ATS_Address *aa;
228 struct ATS_Address *old;
231 aa = create_address (peer,
233 plugin_addr, plugin_addr_len,
236 aa->mlp_information = NULL;
237 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
238 aa->ats_count = atsi_count;
239 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
241 old = find_address (peer, aa);
244 GNUNET_assert (GNUNET_OK ==
245 GNUNET_CONTAINER_multihashmap_put (addresses,
246 &peer->hashPubKey, aa,
247 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
249 GNUNET_i2s (peer), aa);
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Updated existing address for peer `%s' %X \n",
256 GNUNET_i2s (peer), old);
257 GNUNET_free_non_null (old->ats);
258 old->session_id = session_id;
262 old->ats_count = aa->ats_count;
263 GNUNET_free (aa->plugin);
266 for (i = 0; i < atsi_count; i++)
267 switch (ntohl (atsi[i].type))
269 case GNUNET_ATS_UTILIZATION_UP:
270 old->atsp_utilization_out.value__ = atsi[i].value;
272 case GNUNET_ATS_UTILIZATION_DOWN:
273 old->atsp_utilization_in.value__ = atsi[i].value;
275 case GNUNET_ATS_QUALITY_NET_DELAY:
276 old->atsp_latency.rel_value = ntohl (atsi[i].value);
278 case GNUNET_ATS_QUALITY_NET_DISTANCE:
279 old->atsp_distance = ntohl (atsi[i].value);
281 case GNUNET_ATS_COST_WAN:
282 old->atsp_cost_wan = ntohl (atsi[i].value);
284 case GNUNET_ATS_COST_LAN:
285 old->atsp_cost_lan = ntohl (atsi[i].value);
287 case GNUNET_ATS_COST_WLAN:
288 old->atsp_cost_wlan = ntohl (atsi[i].value);
290 case GNUNET_ATS_NETWORK_TYPE:
291 old->atsp_network_type = ntohl (atsi[i].value);
295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
296 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
302 GAS_mlp_address_update (addresses, old);
310 * If session != 0, just the session is deleted, the address itself still exists
311 * If session == 0, remove full address
312 * If session == 0 and addrlen == 0, destroy inbound address
316 * @param value the 'struct ATS_Address'
317 * @return GNUNET_OK (continue to iterate)
320 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
322 const struct ATS_Address *info = cls;
323 struct ATS_Address *aa = value;
326 memcmp (&aa->peer, &info->peer,
327 sizeof (struct GNUNET_PeerIdentity)));
328 /* session == 0, remove full address */
329 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
330 (aa->addr_len == info->addr_len) &&
331 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334 "Deleting address for peer `%s': `%s'\n",
335 GNUNET_i2s (&aa->peer), aa->plugin);
336 if (GNUNET_YES == destroy_address (aa))
337 recalculate_assigned_bw ();
340 /* session != 0, just remove session */
341 if (aa->session_id != info->session_id)
342 return GNUNET_OK; /* irrelevant */
343 if (aa->session_id != 0)
344 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
348 if (GNUNET_YES == aa->active)
350 aa->active = GNUNET_NO;
352 recalculate_assigned_bw ();
355 /* session == 0 and addrlen == 0 : destroy address */
356 if (aa->addr_len == 0)
357 (void) destroy_address (aa);
364 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
365 const char *plugin_name, const void *plugin_addr,
366 size_t plugin_addr_len, uint32_t session_id)
368 struct ATS_Address *aa;
370 GNUNET_break (0 < strlen (plugin_name));
371 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
373 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
374 &destroy_by_session_id, aa);
381 * Find a "good" address to use for a peer. If we already have an existing
382 * address, we stick to it. Otherwise, we pick by lowest distance and then
385 * @param cls the 'struct ATS_Address**' where we store the result
387 * @param value another 'struct ATS_Address*' to consider using
388 * @return GNUNET_OK (continue to iterate)
391 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
393 struct ATS_Address **ap = cls;
394 struct ATS_Address *aa = (struct ATS_Address *) value;
395 struct ATS_Address *ab = *ap;
402 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
403 (ntohl (aa->assigned_bw_in.value__) > 0))
405 /* stick to existing connection */
409 if (ab->atsp_distance > aa->atsp_distance)
411 /* user shorter distance */
415 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
417 /* user lower latency */
427 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
428 const char *plugin_name, const void *plugin_addr,
429 size_t plugin_addr_len, uint32_t session_id, int in_use)
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
434 GNUNET_i2s (peer), in_use);
437 struct ATS_Address *aa;
438 struct ATS_Address *old;
440 aa = create_address(peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
441 old = find_address (peer, aa);
444 GNUNET_assert (old != NULL);
445 GNUNET_assert (in_use != old->used);
450 GAS_mlp_address_update (addresses, old);
455 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
457 struct ATS_Address *aa;
460 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
461 &find_address_it, &aa);
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
468 if (aa->active == GNUNET_NO)
470 aa->active = GNUNET_YES;
472 recalculate_assigned_bw ();
476 /* just to be sure... */
477 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
478 aa->addr_len, aa->session_id,
479 aa->ats, aa->ats_count,
486 // FIXME: this function should likely end up in the LP-subsystem and
487 // not with 'addresses' in the future...
489 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
490 enum GNUNET_ATS_PreferenceKind kind,
493 // do nothing for now...
499 * Initialize address subsystem.
501 * @param cfg configuration to use
502 * @param stats the statistics handle to use
505 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
506 const struct GNUNET_STATISTICS_Handle *stats)
508 GNUNET_assert (GNUNET_OK ==
509 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
512 GNUNET_assert (GNUNET_OK ==
513 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
519 switch (GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP"))
525 /* Init the MLP solver with default values */
526 GAS_mlp_init (stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
529 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
537 /* No configuration value */
545 addresses = GNUNET_CONTAINER_multihashmap_create (128);
550 * Free memory of address.
553 * @param key peer identity (unused)
554 * @param value the 'struct ATS_Address' to free
555 * @return GNUNET_OK (continue to iterate)
558 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
560 struct ATS_Address *aa = value;
562 destroy_address (aa);
568 GAS_addresses_destroy_all ()
570 if (addresses != NULL)
571 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
572 GNUNET_assert (active_addr_count == 0);
577 * Shutdown address subsystem.
580 GAS_addresses_done ()
582 GAS_addresses_destroy_all ();
583 GNUNET_CONTAINER_multihashmap_destroy (addresses);
595 /* end of gnunet-service-ats_addresses.c */