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"
37 struct GNUNET_PeerIdentity peer;
49 struct GNUNET_ATS_Information *ats;
51 struct GNUNET_TIME_Relative atsp_latency;
53 struct GNUNET_BANDWIDTH_Value32NBO atsp_utilization_in;
55 struct GNUNET_BANDWIDTH_Value32NBO atsp_utilization_out;
57 uint32_t atsp_distance;
59 uint32_t atsp_cost_wan;
61 uint32_t atsp_cost_lan;
63 uint32_t atsp_cost_wlan;
65 struct GNUNET_BANDWIDTH_Value32NBO assigned_bw_in;
67 struct GNUNET_BANDWIDTH_Value32NBO assigned_bw_out;
70 * Is this the active address for this peer?
76 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
78 static unsigned long long wan_quota_in;
80 static unsigned long long wan_quota_out;
82 static unsigned int active_addr_count;
85 * Update a bandwidth assignment for a peer. This trivial method currently
86 * simply assigns the same share to all active connections.
90 * @param value the 'struct ATS_Address'
91 * @return GNUNET_OK (continue to iterate)
94 update_bw_it (void *cls, const GNUNET_HashCode * key, void *value)
96 struct ATS_Address *aa = value;
98 if (GNUNET_YES != aa->active)
100 GNUNET_assert (active_addr_count > 0);
101 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
102 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
104 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
105 ntohl (aa->assigned_bw_out.value__));
106 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
107 aa->addr_len, aa->session_id,
108 aa->ats, aa->ats_count,
111 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
112 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
113 aa->ats, aa->ats_count, aa->assigned_bw_out,
120 * Some (significant) input changed, recalculate bandwidth assignment
124 recalculate_assigned_bw ()
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "Recalculating bandwidth for all active connections\n");
128 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
130 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
132 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL);
137 * Destroy the given address.
139 * @param addr address to destroy
140 * @return GNUNET_YES if bandwidth allocations should be recalcualted
143 destroy_address (struct ATS_Address *addr)
148 GNUNET_assert (GNUNET_YES ==
149 GNUNET_CONTAINER_multihashmap_remove (addresses,
150 &addr->peer.hashPubKey,
152 if (GNUNET_YES == addr->active)
155 addr->active = GNUNET_NO;
158 GNUNET_free_non_null (addr->ats);
159 GNUNET_free (addr->plugin);
165 struct CompareAddressContext
167 const struct ATS_Address *search;
168 struct ATS_Address *result;
173 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
175 struct CompareAddressContext *cac = cls;
176 struct ATS_Address *aa = value;
178 if (((aa->addr_len != cac->search->addr_len) ||
179 (0 != strcmp (aa->plugin, cac->search->plugin)) ||
180 (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len))) &&
181 ((aa->session_id != cac->search->session_id) ||
182 (cac->search->session_id == 0)))
190 * Find an existing equivalent address record.
191 * Compares by peer identity and network address OR by session ID
192 * (one of the two must match).
194 * @param peer peer to lookup addresses for
195 * @param addr existing address record
196 * @return existing address record, NULL for none
199 find_address (const struct GNUNET_PeerIdentity *peer,
200 const struct ATS_Address *addr)
202 struct CompareAddressContext cac;
206 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
207 &compare_address_it, &cac);
213 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
214 const char *plugin_name, const void *plugin_addr,
215 size_t plugin_addr_len, uint32_t session_id,
216 const struct GNUNET_ATS_Information *atsi,
219 struct ATS_Address *aa;
220 struct ATS_Address *old;
223 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
224 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
226 aa->addr_len = plugin_addr_len;
227 aa->ats_count = atsi_count;
228 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
230 memcpy (&aa[1], plugin_addr, plugin_addr_len);
231 aa->plugin = GNUNET_strdup (plugin_name);
232 aa->session_id = session_id;
233 old = find_address (peer, aa);
236 GNUNET_assert (GNUNET_OK ==
237 GNUNET_CONTAINER_multihashmap_put (addresses,
238 &peer->hashPubKey, aa,
239 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
241 GNUNET_i2s (peer), aa);
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "Updated existing address for peer `%s' %X \n",
248 GNUNET_i2s (peer), old);
249 GNUNET_free_non_null (old->ats);
250 old->session_id = session_id;
254 old->ats_count = aa->ats_count;
255 GNUNET_free (aa->plugin);
258 for (i = 0; i < atsi_count; i++)
259 switch (ntohl (atsi[i].type))
261 case GNUNET_ATS_UTILIZATION_UP:
262 old->atsp_utilization_out.value__ = atsi[i].value;
264 case GNUNET_ATS_UTILIZATION_DOWN:
265 old->atsp_utilization_in.value__ = atsi[i].value;
267 case GNUNET_ATS_QUALITY_NET_DELAY:
268 old->atsp_latency.rel_value = ntohl (atsi[i].value);
270 case GNUNET_ATS_QUALITY_NET_DISTANCE:
271 old->atsp_distance = ntohl (atsi[i].value);
273 case GNUNET_ATS_COST_WAN:
274 old->atsp_cost_wan = ntohl (atsi[i].value);
276 case GNUNET_ATS_COST_LAN:
277 old->atsp_cost_lan = ntohl (atsi[i].value);
279 case GNUNET_ATS_COST_WLAN:
280 old->atsp_cost_wlan = ntohl (atsi[i].value);
283 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
284 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
292 * Update a bandwidth assignment for a peer. This trivial method currently
293 * simply assigns the same share to all active connections.
297 * @param value the 'struct ATS_Address'
298 * @return GNUNET_OK (continue to iterate)
301 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
303 const struct ATS_Address *info = cls;
304 struct ATS_Address *aa = value;
307 memcmp (&aa->peer, &info->peer,
308 sizeof (struct GNUNET_PeerIdentity)));
309 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
310 (aa->addr_len == info->addr_len) &&
311 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Deleting address for peer `%s': `%s'\n",
315 GNUNET_i2s (&aa->peer), aa->plugin);
316 if (GNUNET_YES == destroy_address (aa))
317 recalculate_assigned_bw ();
320 if (aa->session_id != info->session_id)
321 return GNUNET_OK; /* irrelevant */
322 if (aa->session_id != 0)
323 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
327 if (GNUNET_YES == aa->active)
329 aa->active = GNUNET_NO;
331 recalculate_assigned_bw ();
334 /* session == 0 and addrlen == 0 : destroy address */
335 if (aa->addr_len == 0)
336 (void) destroy_address (aa);
343 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
344 const char *plugin_name, const void *plugin_addr,
345 size_t plugin_addr_len, uint32_t session_id)
347 struct ATS_Address aa;
349 GNUNET_break (0 < strlen (plugin_name));
351 aa.addr_len = plugin_addr_len;
352 aa.addr = plugin_addr;
353 aa.plugin = (char *) plugin_name;
354 aa.session_id = session_id;
355 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
356 &destroy_by_session_id, &aa);
361 * Find a "good" address to use for a peer. If we already have an existing
362 * address, we stick to it. Otherwise, we pick by lowest distance and then
365 * @param cls the 'struct ATS_Address**' where we store the result
367 * @param value another 'struct ATS_Address*' to consider using
368 * @return GNUNET_OK (continue to iterate)
371 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
373 struct ATS_Address **ap = cls;
374 struct ATS_Address *aa = (struct ATS_Address *) value;
375 struct ATS_Address *ab = *ap;
382 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
383 (ntohl (aa->assigned_bw_in.value__) > 0))
385 /* stick to existing connection */
389 if (ab->atsp_distance > aa->atsp_distance)
391 /* user shorter distance */
395 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
397 /* user lower latency */
407 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
408 const char *plugin_name, const void *plugin_addr,
409 size_t plugin_addr_len, uint32_t session_id, int in_use)
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
414 GNUNET_i2s (peer), in_use);
418 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
420 struct ATS_Address *aa;
423 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
424 &find_address_it, &aa);
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
431 if (aa->active == GNUNET_NO)
433 aa->active = GNUNET_YES;
435 recalculate_assigned_bw ();
439 /* just to be sure... */
440 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
441 aa->addr_len, aa->session_id,
442 aa->ats, aa->ats_count,
449 // FIXME: this function should likely end up in the LP-subsystem and
450 // not with 'addresses' in the future...
452 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
453 enum GNUNET_ATS_PreferenceKind kind,
456 // do nothing for now...
462 * Initialize address subsystem.
464 * @param cfg configuration to use
467 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
469 GNUNET_assert (GNUNET_OK ==
470 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
473 GNUNET_assert (GNUNET_OK ==
474 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
477 addresses = GNUNET_CONTAINER_multihashmap_create (128);
482 * Free memory of address.
485 * @param key peer identity (unused)
486 * @param value the 'struct ATS_Address' to free
487 * @return GNUNET_OK (continue to iterate)
490 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
492 struct ATS_Address *aa = value;
494 destroy_address (aa);
500 GAS_addresses_destroy_all ()
502 if (addresses != NULL)
503 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
504 GNUNET_assert (active_addr_count == 0);
509 * Shutdown address subsystem.
512 GAS_addresses_done ()
514 GAS_addresses_destroy_all ();
515 GNUNET_CONTAINER_multihashmap_destroy (addresses);
520 /* end of gnunet-service-ats_addresses.c */