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?
77 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
79 static unsigned long long total_quota_in;
81 static unsigned long long total_quota_out;
83 static unsigned int active_addr_count;
87 * Update a bandwidth assignment for a peer. This trivial method currently
88 * simply assigns the same share to all active connections.
92 * @param value the 'struct ATS_Address'
93 * @return GNUNET_OK (continue to iterate)
96 update_bw_it (void *cls, const GNUNET_HashCode * key, void *value)
98 struct ATS_Address *aa = value;
100 if (GNUNET_YES != aa->active)
102 GNUNET_assert (active_addr_count > 0);
103 aa->assigned_bw_in.value__ = htonl (total_quota_in / active_addr_count);
104 aa->assigned_bw_out.value__ = htonl (total_quota_out / active_addr_count);
105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
106 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
107 ntohl (aa->assigned_bw_out.value__));
108 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
109 aa->addr_len, aa->session_id,
110 aa->ats, aa->ats_count,
113 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
114 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
115 aa->ats, aa->ats_count, aa->assigned_bw_out,
122 * Some (significant) input changed, recalculate bandwidth assignment
126 recalculate_assigned_bw ()
128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129 "Recalculating bandwidth for all active connections\n");
130 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
132 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
134 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL);
139 * Destroy the given address.
141 * @param addr address to destroy
142 * @return GNUNET_YES if bandwidth allocations should be recalcualted
145 destroy_address (struct ATS_Address *addr)
150 GNUNET_assert (GNUNET_YES ==
151 GNUNET_CONTAINER_multihashmap_remove (addresses,
152 &addr->peer.hashPubKey,
154 if (GNUNET_YES == addr->active)
157 addr->active = GNUNET_NO;
160 GNUNET_free_non_null (addr->ats);
161 GNUNET_free (addr->plugin);
167 struct CompareAddressContext
169 const struct ATS_Address *search;
170 struct ATS_Address *result;
175 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
177 struct CompareAddressContext *cac = cls;
178 struct ATS_Address *aa = value;
180 if (((aa->addr_len != cac->search->addr_len) ||
181 (0 != strcmp (aa->plugin, cac->search->plugin)) ||
182 (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len))) &&
183 ((aa->session_id != cac->search->session_id) ||
184 (cac->search->session_id == 0)))
192 * Find an existing equivalent address record.
193 * Compares by peer identity and network address OR by session ID
194 * (one of the two must match).
196 * @param peer peer to lookup addresses for
197 * @param addr existing address record
198 * @return existing address record, NULL for none
201 find_address (const struct GNUNET_PeerIdentity *peer,
202 const struct ATS_Address *addr)
204 struct CompareAddressContext cac;
208 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
209 &compare_address_it, &cac);
215 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
216 const char *plugin_name, const void *plugin_addr,
217 size_t plugin_addr_len, uint32_t session_id,
218 const struct GNUNET_ATS_Information *atsi,
221 struct ATS_Address *aa;
222 struct ATS_Address *old;
225 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
226 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
228 aa->addr_len = plugin_addr_len;
229 aa->ats_count = atsi_count;
230 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
232 memcpy (&aa[1], plugin_addr, plugin_addr_len);
233 aa->plugin = GNUNET_strdup (plugin_name);
234 aa->session_id = session_id;
235 old = find_address (peer, aa);
238 GNUNET_assert (GNUNET_OK ==
239 GNUNET_CONTAINER_multihashmap_put (addresses,
240 &peer->hashPubKey, aa,
241 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
243 GNUNET_i2s (peer), aa);
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 "Updated existing address for peer `%s' %X \n",
250 GNUNET_i2s (peer), old);
251 GNUNET_free_non_null (old->ats);
252 old->session_id = session_id;
256 old->ats_count = aa->ats_count;
257 GNUNET_free (aa->plugin);
260 for (i = 0; i < atsi_count; i++)
261 switch (ntohl (atsi[i].type))
263 case GNUNET_ATS_UTILIZATION_UP:
264 old->atsp_utilization_out.value__ = atsi[i].value;
266 case GNUNET_ATS_UTILIZATION_DOWN:
267 old->atsp_utilization_in.value__ = atsi[i].value;
269 case GNUNET_ATS_QUALITY_NET_DELAY:
270 old->atsp_latency.rel_value = ntohl (atsi[i].value);
272 case GNUNET_ATS_QUALITY_NET_DISTANCE:
273 old->atsp_distance = ntohl (atsi[i].value);
275 case GNUNET_ATS_COST_WAN:
276 old->atsp_cost_wan = ntohl (atsi[i].value);
278 case GNUNET_ATS_COST_LAN:
279 old->atsp_cost_lan = ntohl (atsi[i].value);
281 case GNUNET_ATS_COST_WLAN:
282 old->atsp_cost_wlan = ntohl (atsi[i].value);
285 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
286 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
294 * Update a bandwidth assignment for a peer. This trivial method currently
295 * simply assigns the same share to all active connections.
299 * @param value the 'struct ATS_Address'
300 * @return GNUNET_OK (continue to iterate)
303 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
305 const struct ATS_Address *info = cls;
306 struct ATS_Address *aa = value;
308 GNUNET_assert (0 == memcmp (&aa->peer,
310 sizeof (struct GNUNET_PeerIdentity)));
311 if ( (info->session_id == 0) &&
312 (0 == strcmp (info->plugin,
314 (aa->addr_len == info->addr_len) &&
315 (0 == memcmp (info->addr,
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 "Deleting address for peer `%s': `%s'\n",
321 GNUNET_i2s (&aa->peer), aa->plugin);
322 if (GNUNET_YES == destroy_address (aa))
323 recalculate_assigned_bw ();
326 if (aa->session_id != info->session_id)
327 return GNUNET_OK; /* irrelevant */
328 GNUNET_break (0 == strcmp (info->plugin,
332 if (GNUNET_YES == aa->active)
334 aa->active = GNUNET_NO;
336 if (aa->addr_len == 0)
337 (void) destroy_address (aa);
338 recalculate_assigned_bw ();
345 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
346 const char *plugin_name, const void *plugin_addr,
347 size_t plugin_addr_len, uint32_t session_id)
349 struct ATS_Address aa;
352 aa.addr_len = plugin_addr_len;
353 aa.addr = plugin_addr;
354 aa.plugin = (char *) plugin_name;
355 aa.session_id = session_id;
356 GNUNET_CONTAINER_multihashmap_get_multiple (addresses,
358 &destroy_by_session_id,
364 * Find a "good" address to use for a peer. If we already have an existing
365 * address, we stick to it. Otherwise, we pick by lowest distance and then
368 * @param cls the 'struct ATS_Address**' where we store the result
370 * @param value another 'struct ATS_Address*' to consider using
371 * @return GNUNET_OK (continue to iterate)
374 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
376 struct ATS_Address **ap = cls;
377 struct ATS_Address *aa = (struct ATS_Address *) value;
378 struct ATS_Address *ab = *ap;
385 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
386 (ntohl (aa->assigned_bw_in.value__) > 0))
388 /* stick to existing connection */
392 if (ab->atsp_distance > aa->atsp_distance)
394 /* user shorter distance */
398 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
400 /* user lower latency */
410 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
411 const char *plugin_name, const void *plugin_addr,
412 size_t plugin_addr_len, uint32_t session_id,
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message for peer `%s': %i\n",
418 GNUNET_i2s (peer), in_use);
422 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
424 struct ATS_Address *aa;
427 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
428 &find_address_it, &aa);
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
435 if (aa->active == GNUNET_NO)
437 aa->active = GNUNET_YES;
439 recalculate_assigned_bw ();
443 /* just to be sure... */
444 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
445 aa->addr_len, aa->session_id,
446 aa->ats, aa->ats_count,
453 // FIXME: this function should likely end up in the LP-subsystem and
454 // not with 'addresses' in the future...
456 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
457 enum GNUNET_ATS_PreferenceKind kind,
460 // do nothing for now...
465 * Initialize address subsystem.
467 * @param cfg configuration to use
470 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
472 GNUNET_assert (GNUNET_OK ==
473 GNUNET_CONFIGURATION_get_value_number (cfg, "core",
476 GNUNET_assert (GNUNET_OK ==
477 GNUNET_CONFIGURATION_get_value_number (cfg, "core",
480 addresses = GNUNET_CONTAINER_multihashmap_create (128);
485 * Free memory of address.
488 * @param key peer identity (unused)
489 * @param value the 'struct ATS_Address' to free
490 * @return GNUNET_OK (continue to iterate)
493 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
495 struct ATS_Address *aa = value;
497 destroy_address (aa);
503 GAS_addresses_destroy_all ()
505 if (addresses != NULL)
506 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
507 GNUNET_assert (active_addr_count == 0);
512 * Shutdown address subsystem.
515 GAS_addresses_done ()
517 GAS_addresses_destroy_all ();
518 GNUNET_CONTAINER_multihashmap_destroy (addresses);
523 /* end of gnunet-service-ats_addresses.c */