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_addresses.h"
30 #include "gnunet-service-ats_performance.h"
31 #include "gnunet-service-ats_scheduling.h"
32 #include "gnunet-service-ats_reservations.h"
36 struct GNUNET_PeerIdentity peer;
48 struct GNUNET_ATS_Information * ats;
50 struct GNUNET_TIME_Relative atsp_latency;
52 struct GNUNET_BANDWIDTH_Value32NBO atsp_utilization_in;
54 struct GNUNET_BANDWIDTH_Value32NBO atsp_utilization_out;
56 uint32_t atsp_distance;
58 uint32_t atsp_cost_wan;
60 uint32_t atsp_cost_lan;
62 uint32_t atsp_cost_wlan;
64 struct GNUNET_BANDWIDTH_Value32NBO assigned_bw_in;
66 struct GNUNET_BANDWIDTH_Value32NBO assigned_bw_out;
69 * Is this the active address for this peer?
76 static struct GNUNET_CONTAINER_MultiHashMap * addresses;
78 static unsigned long long total_quota_in;
80 static unsigned long long total_quota_out;
82 static unsigned int active_addr_count;
86 * Update a bandwidth assignment for a peer. This trivial method currently
87 * simply assigns the same share to all active connections.
91 * @param value the 'struct ATS_Address'
92 * @return GNUNET_OK (continue to iterate)
95 update_bw_it (void *cls,
96 const GNUNET_HashCode * key,
99 struct ATS_Address *aa = value;
101 if (GNUNET_YES != aa->active)
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,
106 "New bandwidth for peer %s is %u/%u\n",
107 GNUNET_i2s (&aa->peer),
108 ntohl (aa->assigned_bw_in.value__),
109 ntohl (aa->assigned_bw_out.value__));
110 GAS_scheduling_transmit_address_suggestion (&aa->peer,
112 aa->addr, aa->addr_len,
114 aa->ats, aa->ats_count,
115 aa->assigned_bw_out, aa->assigned_bw_in);
116 GAS_reservations_set_bandwidth (&aa->peer,
118 GAS_performance_notify_clients (&aa->peer,
120 aa->addr, aa->addr_len,
121 aa->ats, aa->ats_count,
122 aa->assigned_bw_out, aa->assigned_bw_in);
128 * Some (significant) input changed, recalculate bandwidth assignment
132 recalculate_assigned_bw ()
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Recalculating bandwidth for all active connections\n");
136 GNUNET_CONTAINER_multihashmap_iterate (addresses,
143 * Destroy the given address.
145 * @param addr address to destroy
146 * @return GNUNET_YES if bandwidth allocations should be recalcualted
149 destroy_address (struct ATS_Address *addr)
154 GNUNET_assert (GNUNET_YES ==
155 GNUNET_CONTAINER_multihashmap_remove(addresses,
156 &addr->peer.hashPubKey,
158 if (GNUNET_YES == addr->active)
163 GNUNET_free_non_null (addr->ats);
164 GNUNET_free (addr->plugin);
170 struct CompareAddressContext
172 const struct ATS_Address * search;
173 struct ATS_Address * result;
178 compare_address_it (void *cls,
179 const GNUNET_HashCode * key,
182 struct CompareAddressContext * cac = cls;
183 struct ATS_Address * aa = value;
185 if ( ( (aa->addr_len != cac->search->addr_len) ||
186 (0 != strcmp(aa->plugin, cac->search->plugin)) ||
187 (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len)) ) &&
188 ( (aa->session_id != cac->search->session_id) ||
189 (cac->search->session_id == 0) ))
197 * Find an existing equivalent address record.
198 * Compares by peer identity and network address OR by session ID
199 * (one of the two must match).
201 * @param peer peer to lookup addresses for
202 * @param addr existing address record
203 * @return existing address record, NULL for none
206 find_address (const struct GNUNET_PeerIdentity *peer,
207 const struct ATS_Address * addr)
209 struct CompareAddressContext cac;
213 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
222 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
223 const char *plugin_name,
224 const void *plugin_addr, size_t plugin_addr_len,
226 const struct GNUNET_ATS_Information *atsi,
229 struct ATS_Address * aa;
230 struct ATS_Address * old;
233 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
234 aa->ats = GNUNET_malloc(atsi_count * sizeof (struct GNUNET_ATS_Information));
236 aa->addr_len = plugin_addr_len;
237 aa->ats_count = atsi_count;
238 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
240 memcpy (&aa[1], plugin_addr, plugin_addr_len);
241 aa->plugin = GNUNET_strdup (plugin_name);
242 aa->session_id = session_id;
243 old = find_address (peer, aa);
246 GNUNET_assert (GNUNET_OK ==
247 GNUNET_CONTAINER_multihashmap_put (addresses,
250 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Added new address for peer `%s' %X\n",
253 GNUNET_i2s (peer), aa);
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Updated existing address for peer `%s' %X \n",
260 GNUNET_i2s (peer), old);
261 GNUNET_free_non_null (old->ats);
262 old->session_id = session_id;
266 old->ats_count = aa->ats_count;
267 GNUNET_free (aa->plugin);
270 for (i=0;i<atsi_count;i++)
271 switch (atsi[i].type)
273 case GNUNET_ATS_UTILIZATION_UP:
274 old->atsp_utilization_out.value__ = atsi[i].value;
276 case GNUNET_ATS_UTILIZATION_DOWN:
277 old->atsp_utilization_in.value__ = atsi[i].value;
279 case GNUNET_ATS_QUALITY_NET_DELAY:
280 old->atsp_latency.rel_value = ntohl (atsi[i].value);
282 case GNUNET_ATS_QUALITY_NET_DISTANCE:
283 old->atsp_distance = ntohl (atsi[i].value);
285 case GNUNET_ATS_COST_WAN:
286 old->atsp_cost_wan = ntohl (atsi[i].value);
288 case GNUNET_ATS_COST_LAN:
289 old->atsp_cost_lan = ntohl (atsi[i].value);
291 case GNUNET_ATS_COST_WLAN:
292 old->atsp_cost_wlan = ntohl (atsi[i].value);
302 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
303 const char *plugin_name,
304 const void *plugin_addr, size_t plugin_addr_len,
307 struct ATS_Address aa;
308 struct ATS_Address *res;
311 aa.addr_len = plugin_addr_len;
312 aa.addr = plugin_addr;
313 aa.plugin = (char*) plugin_name;
314 aa.session_id = session_id;
315 res = find_address (peer, &aa);
318 /* we don't even know this one, can this happen? */
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 "Asked to delete unknown address for peer `%s'\n",
324 if ( (aa.session_id == session_id) &&
326 (res->addr_len > 0) )
328 /* just session died */
330 if (GNUNET_YES == addr->active)
334 recalculate_assigned_bw ();
338 /* destroy address entirely (either was only session or was
339 not even with a session) */
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Deleting address for peer `%s': `%s'\n",
342 GNUNET_i2s (peer), plugin_name);
343 if (GNUNET_YES == destroy_address (res))
344 recalculate_assigned_bw ();
349 * Find a "good" address to use for a peer. If we already have an existing
350 * address, we stick to it. Otherwise, we pick by lowest distance and then
353 * @param cls the 'struct ATS_Address**' where we store the result
355 * @param value another 'struct ATS_Address*' to consider using
356 * @return GNUNET_OK (continue to iterate)
359 find_address_it (void *cls,
360 const GNUNET_HashCode * key,
363 struct ATS_Address **ap = cls;
364 struct ATS_Address * aa = (struct ATS_Address *) value;
365 struct ATS_Address * ab = *ap;
372 if ( (ntohl (ab->assigned_bw_in.value__) == 0) &&
373 (ntohl (aa->assigned_bw_in.value__) > 0) )
375 /* stick to existing connection */
379 if (ab->atsp_distance > aa->atsp_distance)
381 /* user shorter distance */
385 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
387 /* user lower latency */
397 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
399 struct ATS_Address * aa;
402 GNUNET_CONTAINER_multihashmap_get_multiple (addresses,
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Cannot suggest address for peer `%s'\n",
413 if (aa->active == GNUNET_NO)
415 aa->active = GNUNET_YES;
417 recalculate_assigned_bw ();
421 /* just to be sure... */
422 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin,
423 aa->addr, aa->addr_len,
425 aa->ats, aa->ats_count,
426 aa->assigned_bw_out, aa->assigned_bw_in);
431 // FIXME: this function should likely end up in the LP-subsystem and
432 // not with 'addresses' in the future...
434 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
435 enum GNUNET_ATS_PreferenceKind kind,
438 // do nothing for now...
443 * Initialize address subsystem.
445 * @param cfg configuration to use
448 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
450 GNUNET_assert (GNUNET_OK ==
451 GNUNET_CONFIGURATION_get_value_number (cfg,
455 GNUNET_assert (GNUNET_OK ==
456 GNUNET_CONFIGURATION_get_value_number (cfg,
460 addresses = GNUNET_CONTAINER_multihashmap_create(128);
465 * Free memory of address.
468 * @param key peer identity (unused)
469 * @param value the 'struct ATS_Address' to free
470 * @return GNUNET_OK (continue to iterate)
473 free_address_it (void *cls,
474 const GNUNET_HashCode * key,
477 struct ATS_Address * aa = value;
479 destroy_address (aa);
485 GAS_addresses_destroy_all ()
487 if (addresses != NULL)
488 GNUNET_CONTAINER_multihashmap_iterate(addresses,
489 &free_address_it, NULL);
490 GNUNET_assert (active_addr_count == 0);
495 * Shutdown address subsystem.
498 GAS_addresses_done ()
500 GAS_addresses_destroy_all ();
501 GNUNET_CONTAINER_multihashmap_destroy (addresses);
506 /* end of gnunet-service-ats_addresses.c */