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_it (void *cls, const GNUNET_HashCode * key, void *value)
82 struct ATS_Address *aa = value;
86 if (GNUNET_YES != aa->active)
88 GNUNET_assert (active_addr_count > 0);
89 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
90 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
92 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
93 ntohl (aa->assigned_bw_out.value__));
94 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
95 aa->addr_len, aa->session_id,
96 aa->ats, aa->ats_count,
99 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
100 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
101 aa->ats, aa->ats_count, aa->assigned_bw_out,
108 * Some (significant) input changed, recalculate bandwidth assignment
112 recalculate_assigned_bw ()
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
115 "Recalculating bandwidth for all active connections\n");
116 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
118 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
120 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL);
124 * Free the given address
125 * @param addr address to destroy
128 free_address (struct ATS_Address *addr)
130 GNUNET_free_non_null (addr->ats);
131 GNUNET_free (addr->plugin);
136 * Create a ATS_address with the given information
138 * @param plugin_name plugin
139 * @param plugin_addr address
140 * @param plugin_addr_len address length
141 * @param session_id session
142 * @return the ATS_Address
144 static struct ATS_Address *
145 create_address (const struct GNUNET_PeerIdentity *peer,
146 const char *plugin_name,
147 const void *plugin_addr, size_t plugin_addr_len,
150 struct ATS_Address *aa = NULL;
152 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
154 aa->addr_len = plugin_addr_len;
156 memcpy (&aa[1], plugin_addr, plugin_addr_len);
157 aa->plugin = GNUNET_strdup (plugin_name);
158 aa->session_id = session_id;
159 aa->mlp_information = NULL;
167 * Destroy the given address.
169 * @param addr address to destroy
170 * @return GNUNET_YES if bandwidth allocations should be recalcualted
173 destroy_address (struct ATS_Address *addr)
178 GNUNET_assert (GNUNET_YES ==
179 GNUNET_CONTAINER_multihashmap_remove (addresses,
180 &addr->peer.hashPubKey,
185 GAS_mlp_address_delete (mlp, addresses, addr);
188 if (GNUNET_YES == addr->active)
191 addr->active = GNUNET_NO;
199 struct CompareAddressContext
201 const struct ATS_Address *search;
203 /* exact_address != NULL if address and session is equal */
204 struct ATS_Address *exact_address;
205 /* exact_address != NULL if address and session is 0 */
206 struct ATS_Address *base_address;
211 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
213 struct CompareAddressContext *cac = cls;
214 struct ATS_Address *aa = value;
216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
217 "Comparing to: %s %s %u session %u\n",
218 GNUNET_i2s (&aa->peer), aa->plugin, aa->addr_len, aa->session_id);
221 /* find an exact matching address: aa->addr == cac->search->addr && aa->session == cac->search->session */
222 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
224 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
226 cac->exact_address = aa;
230 /* find an matching address: aa->addr == cac->search->addr && aa->session == 0 */
231 /* this address can be used to be updated */
232 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
234 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
236 cac->base_address = aa;
240 if (cac->exact_address == NULL)
248 * Find an existing equivalent address record.
249 * Compares by peer identity and network address OR by session ID
250 * (one of the two must match).
252 * @param peer peer to lookup addresses for
253 * @param addr existing address record
254 * @return existing address record, NULL for none
257 find_address (const struct GNUNET_PeerIdentity *peer,
258 const struct ATS_Address *addr)
260 struct CompareAddressContext cac;
262 cac.exact_address = NULL;
263 cac.base_address = NULL;
265 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
266 &compare_address_it, &cac);
269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
270 "exact address: %s base address: %s\n",
271 (cac.exact_address != NULL) ? "YES" : "NO",
272 (cac.base_address != NULL) ? "YES" : "NO");
274 if (cac.exact_address == NULL)
275 return cac.base_address;
276 return cac.exact_address;
281 compare_address_session_it (void *cls, const GNUNET_HashCode * key, void *value)
283 struct CompareAddressContext *cac = cls;
284 struct ATS_Address *aa = value;
286 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
288 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
290 cac->exact_address = aa;
299 * Find an existing equivalent address record.
300 * Compares by peer identity and network address AND by session ID
301 * (one of the two must match).
303 * @param peer peer to lookup addresses for
304 * @param addr existing address record
305 * @return existing address record, NULL for none
308 find_exact_address (const struct GNUNET_PeerIdentity *peer,
309 const struct ATS_Address *addr)
311 struct CompareAddressContext cac;
313 cac.exact_address = NULL;
315 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
316 &compare_address_session_it, &cac);
317 return cac.exact_address;
322 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
323 const char *plugin_name, const void *plugin_addr,
324 size_t plugin_addr_len, uint32_t session_id,
325 const struct GNUNET_ATS_Information *atsi,
328 struct ATS_Address *aa;
329 struct ATS_Address *old;
332 aa = create_address (peer,
334 plugin_addr, plugin_addr_len,
337 aa->mlp_information = NULL;
338 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
339 aa->ats_count = atsi_count;
340 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' %u\n",
347 /* Get existing address or address with session == 0 */
348 old = find_address (peer, aa);
351 GNUNET_assert (GNUNET_OK ==
352 GNUNET_CONTAINER_multihashmap_put (addresses,
353 &peer->hashPubKey, aa,
354 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
357 GNUNET_i2s (peer), aa);
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "Updated existing address for peer `%s' %p old session %u new session %u\n",
366 GNUNET_i2s (peer), old,
367 old->session_id, session_id);
369 GNUNET_free_non_null (old->ats);
370 old->session_id = session_id;
374 old->ats_count = aa->ats_count;
375 GNUNET_free (aa->plugin);
378 for (i = 0; i < atsi_count; i++)
379 switch (ntohl (atsi[i].type))
381 case GNUNET_ATS_UTILIZATION_UP:
382 old->atsp_utilization_out.value__ = atsi[i].value;
384 case GNUNET_ATS_UTILIZATION_DOWN:
385 old->atsp_utilization_in.value__ = atsi[i].value;
387 case GNUNET_ATS_QUALITY_NET_DELAY:
388 old->atsp_latency.rel_value = ntohl (atsi[i].value);
390 case GNUNET_ATS_QUALITY_NET_DISTANCE:
391 old->atsp_distance = ntohl (atsi[i].value);
393 case GNUNET_ATS_COST_WAN:
394 old->atsp_cost_wan = ntohl (atsi[i].value);
396 case GNUNET_ATS_COST_LAN:
397 old->atsp_cost_lan = ntohl (atsi[i].value);
399 case GNUNET_ATS_COST_WLAN:
400 old->atsp_cost_wlan = ntohl (atsi[i].value);
402 case GNUNET_ATS_NETWORK_TYPE:
403 old->atsp_network_type = ntohl (atsi[i].value);
407 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
408 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
414 GAS_mlp_address_update (mlp, addresses, old);
422 * If session != 0, just the session is deleted, the address itself still exists
423 * If session == 0, remove full address
424 * If session == 0 and addrlen == 0, destroy inbound address
428 * @param value the 'struct ATS_Address'
429 * @return GNUNET_OK (continue to iterate)
432 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
434 const struct ATS_Address *info = cls;
435 struct ATS_Address *aa = value;
438 memcmp (&aa->peer, &info->peer,
439 sizeof (struct GNUNET_PeerIdentity)));
440 /* session == 0, remove full address */
441 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
442 (aa->addr_len == info->addr_len) &&
443 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Deleting address for peer `%s': `%s' %u\n",
448 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
450 if (GNUNET_YES == destroy_address (aa))
451 recalculate_assigned_bw ();
454 /* session != 0, just remove session */
455 if (aa->session_id != info->session_id)
456 return GNUNET_OK; /* irrelevant */
457 if (aa->session_id != 0)
458 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 "Deleting session for peer `%s': `%s' %u\n",
463 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
467 if (GNUNET_YES == aa->active)
469 aa->active = GNUNET_NO;
471 recalculate_assigned_bw ();
474 /* session == 0 and addrlen == 0 : destroy address */
475 if (aa->addr_len == 0)
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Deleting session and address for peer `%s': `%s' %u\n",
480 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
482 (void) destroy_address (aa);
486 /* session was set to 0, update address */
489 GAS_mlp_address_update (mlp, addresses, aa);
497 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
498 const char *plugin_name, const void *plugin_addr,
499 size_t plugin_addr_len, uint32_t session_id)
501 struct ATS_Address *aa;
503 GNUNET_break (0 < strlen (plugin_name));
504 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
506 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
507 &destroy_by_session_id, aa);
514 * Find a "good" address to use for a peer. If we already have an existing
515 * address, we stick to it. Otherwise, we pick by lowest distance and then
518 * @param cls the 'struct ATS_Address**' where we store the result
520 * @param value another 'struct ATS_Address*' to consider using
521 * @return GNUNET_OK (continue to iterate)
524 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
526 struct ATS_Address **ap = cls;
527 struct ATS_Address *aa = (struct ATS_Address *) value;
528 struct ATS_Address *ab = *ap;
535 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
536 (ntohl (aa->assigned_bw_in.value__) > 0))
538 /* stick to existing connection */
542 if (ab->atsp_distance > aa->atsp_distance)
544 /* user shorter distance */
548 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
550 /* user lower latency */
560 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
561 const char *plugin_name, const void *plugin_addr,
562 size_t plugin_addr_len, uint32_t session_id, int in_use)
565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
566 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
567 GNUNET_i2s (peer), in_use);
570 struct ATS_Address *aa;
571 struct ATS_Address *old;
574 aa = create_address(peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
575 old = find_exact_address (peer, aa);
578 GNUNET_assert (old != NULL);
579 GNUNET_assert (old->used != in_use);
584 GAS_mlp_address_update (mlp, addresses, old);
589 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
591 struct ATS_Address *aa;
594 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
595 &find_address_it, &aa);
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
602 if (aa->active == GNUNET_NO)
604 aa->active = GNUNET_YES;
606 recalculate_assigned_bw ();
610 /* just to be sure... */
611 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
612 aa->addr_len, aa->session_id,
613 aa->ats, aa->ats_count,
620 // FIXME: this function should likely end up in the LP-subsystem and
621 // not with 'addresses' in the future...
623 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
624 enum GNUNET_ATS_PreferenceKind kind,
629 GAS_mlp_address_change_preference (mlp, peer, kind, score);
636 * Initialize address subsystem.
638 * @param cfg configuration to use
639 * @param stats the statistics handle to use
642 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
643 const struct GNUNET_STATISTICS_Handle *stats)
645 GNUNET_assert (GNUNET_OK ==
646 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
649 GNUNET_assert (GNUNET_OK ==
650 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
654 switch (GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP"))
660 /* Init the MLP solver with default values */
661 mlp = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
673 /* No configuration value */
681 addresses = GNUNET_CONTAINER_multihashmap_create (128);
686 * Free memory of address.
689 * @param key peer identity (unused)
690 * @param value the 'struct ATS_Address' to free
691 * @return GNUNET_OK (continue to iterate)
694 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
696 struct ATS_Address *aa = value;
698 destroy_address (aa);
704 GAS_addresses_destroy_all ()
706 if (addresses != NULL)
707 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
708 GNUNET_assert (active_addr_count == 0);
713 * Shutdown address subsystem.
716 GAS_addresses_done ()
718 GAS_addresses_destroy_all ();
719 GNUNET_CONTAINER_multihashmap_destroy (addresses);
731 /* end of gnunet-service-ats_addresses.c */