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_addresses_mlp.h"
32 #include "gnunet-service-ats_performance.h"
33 #include "gnunet-service-ats_scheduling.h"
34 #include "gnunet-service-ats_reservations.h"
38 struct GNUNET_PeerIdentity peer;
50 struct GNUNET_ATS_Information *ats;
52 struct GNUNET_TIME_Relative atsp_latency;
54 struct GNUNET_BANDWIDTH_Value32NBO atsp_utilization_in;
56 struct GNUNET_BANDWIDTH_Value32NBO atsp_utilization_out;
58 uint32_t atsp_distance;
60 uint32_t atsp_cost_wan;
62 uint32_t atsp_cost_lan;
64 uint32_t atsp_cost_wlan;
66 uint32_t atsp_network_type;
68 struct GNUNET_BANDWIDTH_Value32NBO assigned_bw_in;
70 struct GNUNET_BANDWIDTH_Value32NBO assigned_bw_out;
73 * Is this the active address for this peer?
85 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
87 static unsigned long long wan_quota_in;
89 static unsigned long long wan_quota_out;
91 static unsigned int active_addr_count;
96 * Update a bandwidth assignment for a peer. This trivial method currently
97 * simply assigns the same share to all active connections.
101 * @param value the 'struct ATS_Address'
102 * @return GNUNET_OK (continue to iterate)
105 update_bw_it (void *cls, const GNUNET_HashCode * key, void *value)
107 struct ATS_Address *aa = value;
109 if (GNUNET_YES != aa->active)
111 GNUNET_assert (active_addr_count > 0);
112 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
113 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
115 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
116 ntohl (aa->assigned_bw_out.value__));
117 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
118 aa->addr_len, aa->session_id,
119 aa->ats, aa->ats_count,
122 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
123 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
124 aa->ats, aa->ats_count, aa->assigned_bw_out,
131 * Some (significant) input changed, recalculate bandwidth assignment
135 recalculate_assigned_bw ()
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138 "Recalculating bandwidth for all active connections\n");
139 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
141 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
143 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL);
148 * Destroy the given address.
150 * @param addr address to destroy
151 * @return GNUNET_YES if bandwidth allocations should be recalcualted
154 destroy_address (struct ATS_Address *addr)
159 GNUNET_assert (GNUNET_YES ==
160 GNUNET_CONTAINER_multihashmap_remove (addresses,
161 &addr->peer.hashPubKey,
163 if (GNUNET_YES == addr->active)
166 addr->active = GNUNET_NO;
169 GNUNET_free_non_null (addr->ats);
170 GNUNET_free (addr->plugin);
176 struct CompareAddressContext
178 const struct ATS_Address *search;
179 struct ATS_Address *result;
184 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
186 struct CompareAddressContext *cac = cls;
187 struct ATS_Address *aa = value;
189 if (((aa->addr_len != cac->search->addr_len) ||
190 (0 != strcmp (aa->plugin, cac->search->plugin)) ||
191 (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len))) &&
192 ((aa->session_id != cac->search->session_id) ||
193 (cac->search->session_id == 0)))
201 * Find an existing equivalent address record.
202 * Compares by peer identity and network address OR by session ID
203 * (one of the two must match).
205 * @param peer peer to lookup addresses for
206 * @param addr existing address record
207 * @return existing address record, NULL for none
210 find_address (const struct GNUNET_PeerIdentity *peer,
211 const struct ATS_Address *addr)
213 struct CompareAddressContext cac;
217 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
218 &compare_address_it, &cac);
224 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
225 const char *plugin_name, const void *plugin_addr,
226 size_t plugin_addr_len, uint32_t session_id,
227 const struct GNUNET_ATS_Information *atsi,
230 struct ATS_Address *aa;
231 struct ATS_Address *old;
234 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
235 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
237 aa->addr_len = plugin_addr_len;
238 aa->ats_count = atsi_count;
239 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
241 memcpy (&aa[1], plugin_addr, plugin_addr_len);
242 aa->plugin = GNUNET_strdup (plugin_name);
243 aa->session_id = session_id;
244 old = find_address (peer, aa);
247 GNUNET_assert (GNUNET_OK ==
248 GNUNET_CONTAINER_multihashmap_put (addresses,
249 &peer->hashPubKey, aa,
250 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
252 GNUNET_i2s (peer), aa);
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258 "Updated existing address for peer `%s' %X \n",
259 GNUNET_i2s (peer), old);
260 GNUNET_free_non_null (old->ats);
261 old->session_id = session_id;
265 old->ats_count = aa->ats_count;
266 GNUNET_free (aa->plugin);
269 for (i = 0; i < atsi_count; i++)
270 switch (ntohl (atsi[i].type))
272 case GNUNET_ATS_UTILIZATION_UP:
273 old->atsp_utilization_out.value__ = atsi[i].value;
275 case GNUNET_ATS_UTILIZATION_DOWN:
276 old->atsp_utilization_in.value__ = atsi[i].value;
278 case GNUNET_ATS_QUALITY_NET_DELAY:
279 old->atsp_latency.rel_value = ntohl (atsi[i].value);
281 case GNUNET_ATS_QUALITY_NET_DISTANCE:
282 old->atsp_distance = ntohl (atsi[i].value);
284 case GNUNET_ATS_COST_WAN:
285 old->atsp_cost_wan = ntohl (atsi[i].value);
287 case GNUNET_ATS_COST_LAN:
288 old->atsp_cost_lan = ntohl (atsi[i].value);
290 case GNUNET_ATS_COST_WLAN:
291 old->atsp_cost_wlan = ntohl (atsi[i].value);
293 case GNUNET_ATS_NETWORK_TYPE:
294 old->atsp_network_type = ntohl (atsi[i].value);
298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
299 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
309 * If session != 0, just the session is deleted, the address itself still exists
310 * If session == 0, remove full address
311 * If session == 0 and addrlen == 0, destroy inbound address
315 * @param value the 'struct ATS_Address'
316 * @return GNUNET_OK (continue to iterate)
319 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
321 const struct ATS_Address *info = cls;
322 struct ATS_Address *aa = value;
325 memcmp (&aa->peer, &info->peer,
326 sizeof (struct GNUNET_PeerIdentity)));
327 /* session == 0, remove full address */
328 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
329 (aa->addr_len == info->addr_len) &&
330 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Deleting address for peer `%s': `%s'\n",
334 GNUNET_i2s (&aa->peer), aa->plugin);
335 if (GNUNET_YES == destroy_address (aa))
336 recalculate_assigned_bw ();
339 /* session != 0, just remove session */
340 if (aa->session_id != info->session_id)
341 return GNUNET_OK; /* irrelevant */
342 if (aa->session_id != 0)
343 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
347 if (GNUNET_YES == aa->active)
349 aa->active = GNUNET_NO;
351 recalculate_assigned_bw ();
354 /* session == 0 and addrlen == 0 : destroy address */
355 if (aa->addr_len == 0)
356 (void) destroy_address (aa);
363 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
364 const char *plugin_name, const void *plugin_addr,
365 size_t plugin_addr_len, uint32_t session_id)
367 struct ATS_Address aa;
369 GNUNET_break (0 < strlen (plugin_name));
371 aa.addr_len = plugin_addr_len;
372 aa.addr = plugin_addr;
373 aa.plugin = (char *) plugin_name;
374 aa.session_id = session_id;
375 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
376 &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);
438 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
440 struct ATS_Address *aa;
443 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
444 &find_address_it, &aa);
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
451 if (aa->active == GNUNET_NO)
453 aa->active = GNUNET_YES;
455 recalculate_assigned_bw ();
459 /* just to be sure... */
460 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
461 aa->addr_len, aa->session_id,
462 aa->ats, aa->ats_count,
469 // FIXME: this function should likely end up in the LP-subsystem and
470 // not with 'addresses' in the future...
472 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
473 enum GNUNET_ATS_PreferenceKind kind,
476 // do nothing for now...
482 * Initialize address subsystem.
484 * @param cfg configuration to use
487 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
489 GNUNET_assert (GNUNET_OK ==
490 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
493 GNUNET_assert (GNUNET_OK ==
494 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
500 switch (GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP"))
505 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
517 /* No configuration value */
525 addresses = GNUNET_CONTAINER_multihashmap_create (128);
530 * Free memory of address.
533 * @param key peer identity (unused)
534 * @param value the 'struct ATS_Address' to free
535 * @return GNUNET_OK (continue to iterate)
538 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
540 struct ATS_Address *aa = value;
542 destroy_address (aa);
548 GAS_addresses_destroy_all ()
550 if (addresses != NULL)
551 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
552 GNUNET_assert (active_addr_count == 0);
557 * Shutdown address subsystem.
560 GAS_addresses_done ()
562 GAS_addresses_destroy_all ();
563 GNUNET_CONTAINER_multihashmap_destroy (addresses);
572 /* end of gnunet-service-ats_addresses.c */