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;
56 static struct GAS_MLP_Handle *mlp;
59 static unsigned long long wan_quota_in;
61 static unsigned long long wan_quota_out;
63 static unsigned int active_addr_count;
69 * Update a bandwidth assignment for a peer. This trivial method currently
70 * simply assigns the same share to all active connections.
74 * @param value the 'struct ATS_Address'
75 * @return GNUNET_OK (continue to iterate)
78 update_bw_it (void *cls, const GNUNET_HashCode * key, void *value)
80 struct ATS_Address *aa = value;
84 if (GNUNET_YES != aa->active)
86 GNUNET_assert (active_addr_count > 0);
87 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
88 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
90 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
91 ntohl (aa->assigned_bw_out.value__));
92 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
93 aa->addr_len, aa->session_id,
94 aa->ats, aa->ats_count,
97 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
98 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
99 aa->ats, aa->ats_count, aa->assigned_bw_out,
106 * Some (significant) input changed, recalculate bandwidth assignment
110 recalculate_assigned_bw ()
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113 "Recalculating bandwidth for all active connections\n");
114 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
116 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
118 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL);
122 * Free the given address
123 * @param addr address to destroy
126 free_address (struct ATS_Address *addr)
128 GNUNET_free_non_null (addr->ats);
129 GNUNET_free (addr->plugin);
134 * Create a ATS_address with the given information
136 * @param plugin_name plugin
137 * @param plugin_addr address
138 * @param plugin_addr_len address length
139 * @param session_id session
140 * @return the ATS_Address
142 static struct ATS_Address *
143 create_address (const struct GNUNET_PeerIdentity *peer,
144 const char *plugin_name,
145 const void *plugin_addr, size_t plugin_addr_len,
148 struct ATS_Address *aa = NULL;
150 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
152 aa->addr_len = plugin_addr_len;
154 memcpy (&aa[1], plugin_addr, plugin_addr_len);
155 aa->plugin = GNUNET_strdup (plugin_name);
156 aa->session_id = session_id;
163 * Destroy the given address.
165 * @param addr address to destroy
166 * @return GNUNET_YES if bandwidth allocations should be recalcualted
169 destroy_address (struct ATS_Address *addr)
174 GNUNET_assert (GNUNET_YES ==
175 GNUNET_CONTAINER_multihashmap_remove (addresses,
176 &addr->peer.hashPubKey,
181 GAS_mlp_address_delete (mlp, addresses, addr);
184 if (GNUNET_YES == addr->active)
187 addr->active = GNUNET_NO;
195 struct CompareAddressContext
197 const struct ATS_Address *search;
198 struct ATS_Address *result;
203 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
205 struct CompareAddressContext *cac = cls;
206 struct ATS_Address *aa = value;
208 if (((aa->addr_len != cac->search->addr_len) ||
209 (0 != strcmp (aa->plugin, cac->search->plugin)) ||
210 (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len))) &&
211 ((aa->session_id != cac->search->session_id) ||
212 (cac->search->session_id == 0)))
220 * Find an existing equivalent address record.
221 * Compares by peer identity and network address OR by session ID
222 * (one of the two must match).
224 * @param peer peer to lookup addresses for
225 * @param addr existing address record
226 * @return existing address record, NULL for none
229 find_address (const struct GNUNET_PeerIdentity *peer,
230 const struct ATS_Address *addr)
232 struct CompareAddressContext cac;
236 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
237 &compare_address_it, &cac);
243 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
244 const char *plugin_name, const void *plugin_addr,
245 size_t plugin_addr_len, uint32_t session_id,
246 const struct GNUNET_ATS_Information *atsi,
249 struct ATS_Address *aa;
250 struct ATS_Address *old;
253 aa = create_address (peer,
255 plugin_addr, plugin_addr_len,
258 aa->mlp_information = NULL;
259 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
260 aa->ats_count = atsi_count;
261 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
263 old = find_address (peer, aa);
266 GNUNET_assert (GNUNET_OK ==
267 GNUNET_CONTAINER_multihashmap_put (addresses,
268 &peer->hashPubKey, aa,
269 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
271 GNUNET_i2s (peer), aa);
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
277 "Updated existing address for peer `%s' %X \n",
278 GNUNET_i2s (peer), old);
279 GNUNET_free_non_null (old->ats);
280 old->session_id = session_id;
284 old->ats_count = aa->ats_count;
285 GNUNET_free (aa->plugin);
288 for (i = 0; i < atsi_count; i++)
289 switch (ntohl (atsi[i].type))
291 case GNUNET_ATS_UTILIZATION_UP:
292 old->atsp_utilization_out.value__ = atsi[i].value;
294 case GNUNET_ATS_UTILIZATION_DOWN:
295 old->atsp_utilization_in.value__ = atsi[i].value;
297 case GNUNET_ATS_QUALITY_NET_DELAY:
298 old->atsp_latency.rel_value = ntohl (atsi[i].value);
300 case GNUNET_ATS_QUALITY_NET_DISTANCE:
301 old->atsp_distance = ntohl (atsi[i].value);
303 case GNUNET_ATS_COST_WAN:
304 old->atsp_cost_wan = ntohl (atsi[i].value);
306 case GNUNET_ATS_COST_LAN:
307 old->atsp_cost_lan = ntohl (atsi[i].value);
309 case GNUNET_ATS_COST_WLAN:
310 old->atsp_cost_wlan = ntohl (atsi[i].value);
312 case GNUNET_ATS_NETWORK_TYPE:
313 old->atsp_network_type = ntohl (atsi[i].value);
317 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
318 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
324 GAS_mlp_address_update (mlp, addresses, old);
332 * If session != 0, just the session is deleted, the address itself still exists
333 * If session == 0, remove full address
334 * If session == 0 and addrlen == 0, destroy inbound address
338 * @param value the 'struct ATS_Address'
339 * @return GNUNET_OK (continue to iterate)
342 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
344 const struct ATS_Address *info = cls;
345 struct ATS_Address *aa = value;
348 memcmp (&aa->peer, &info->peer,
349 sizeof (struct GNUNET_PeerIdentity)));
350 /* session == 0, remove full address */
351 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
352 (aa->addr_len == info->addr_len) &&
353 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "Deleting address for peer `%s': `%s'\n",
357 GNUNET_i2s (&aa->peer), aa->plugin);
358 if (GNUNET_YES == destroy_address (aa))
359 recalculate_assigned_bw ();
362 /* session != 0, just remove session */
363 if (aa->session_id != info->session_id)
364 return GNUNET_OK; /* irrelevant */
365 if (aa->session_id != 0)
366 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
370 if (GNUNET_YES == aa->active)
372 aa->active = GNUNET_NO;
374 recalculate_assigned_bw ();
377 /* session == 0 and addrlen == 0 : destroy address */
378 if (aa->addr_len == 0)
380 (void) destroy_address (aa);
384 /* session was set to 0, update address */
387 GAS_mlp_address_update (mlp, addresses, aa);
395 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
396 const char *plugin_name, const void *plugin_addr,
397 size_t plugin_addr_len, uint32_t session_id)
399 struct ATS_Address *aa;
401 GNUNET_break (0 < strlen (plugin_name));
402 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
404 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
405 &destroy_by_session_id, aa);
412 * Find a "good" address to use for a peer. If we already have an existing
413 * address, we stick to it. Otherwise, we pick by lowest distance and then
416 * @param cls the 'struct ATS_Address**' where we store the result
418 * @param value another 'struct ATS_Address*' to consider using
419 * @return GNUNET_OK (continue to iterate)
422 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
424 struct ATS_Address **ap = cls;
425 struct ATS_Address *aa = (struct ATS_Address *) value;
426 struct ATS_Address *ab = *ap;
433 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
434 (ntohl (aa->assigned_bw_in.value__) > 0))
436 /* stick to existing connection */
440 if (ab->atsp_distance > aa->atsp_distance)
442 /* user shorter distance */
446 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
448 /* user lower latency */
458 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
459 const char *plugin_name, const void *plugin_addr,
460 size_t plugin_addr_len, uint32_t session_id, int in_use)
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
465 GNUNET_i2s (peer), in_use);
468 struct ATS_Address *aa;
469 struct ATS_Address *old;
471 aa = create_address(peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
472 old = find_address (peer, aa);
475 GNUNET_assert (old != NULL);
476 GNUNET_assert (in_use != old->used);
481 GAS_mlp_address_update (mlp, addresses, old);
486 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
488 struct ATS_Address *aa;
491 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
492 &find_address_it, &aa);
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
499 if (aa->active == GNUNET_NO)
501 aa->active = GNUNET_YES;
503 recalculate_assigned_bw ();
507 /* just to be sure... */
508 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
509 aa->addr_len, aa->session_id,
510 aa->ats, aa->ats_count,
517 // FIXME: this function should likely end up in the LP-subsystem and
518 // not with 'addresses' in the future...
520 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
521 enum GNUNET_ATS_PreferenceKind kind,
524 // do nothing for now...
530 * Initialize address subsystem.
532 * @param cfg configuration to use
533 * @param stats the statistics handle to use
536 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
537 const struct GNUNET_STATISTICS_Handle *stats)
543 long long unsigned int tmp;
547 /* Get diversity coefficient from configuration */
548 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
551 D = (double) tmp / 100;
555 /* Get proportionality coefficient from configuration */
556 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
559 R = (double) tmp / 100;
563 /* Get utilization coefficient from configuration */
564 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
567 U = (double) tmp / 100;
571 /* Get minimum bandwidth per used address from configuration */
572 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
579 /* Get minimum number of connections from configuration */
580 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
588 GNUNET_assert (GNUNET_OK ==
589 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
592 GNUNET_assert (GNUNET_OK ==
593 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
597 switch (GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP"))
603 /* Init the MLP solver with default values */
604 mlp = GAS_mlp_init (stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS, D, U, R, b_min, n_min);
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
616 /* No configuration value */
624 addresses = GNUNET_CONTAINER_multihashmap_create (128);
629 * Free memory of address.
632 * @param key peer identity (unused)
633 * @param value the 'struct ATS_Address' to free
634 * @return GNUNET_OK (continue to iterate)
637 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
639 struct ATS_Address *aa = value;
641 destroy_address (aa);
647 GAS_addresses_destroy_all ()
649 if (addresses != NULL)
650 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
651 GNUNET_assert (active_addr_count == 0);
656 * Shutdown address subsystem.
659 GAS_addresses_done ()
661 GAS_addresses_destroy_all ();
662 GNUNET_CONTAINER_multihashmap_destroy (addresses);
674 /* end of gnunet-service-ats_addresses.c */