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 == res->active)
333 recalculate_assigned_bw ();
337 /* destroy address entirely (either was only session or was
338 not even with a session) */
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "Deleting address for peer `%s': `%s'\n",
341 GNUNET_i2s (peer), plugin_name);
342 if (GNUNET_YES == destroy_address (res))
343 recalculate_assigned_bw ();
348 * Find a "good" address to use for a peer. If we already have an existing
349 * address, we stick to it. Otherwise, we pick by lowest distance and then
352 * @param cls the 'struct ATS_Address**' where we store the result
354 * @param value another 'struct ATS_Address*' to consider using
355 * @return GNUNET_OK (continue to iterate)
358 find_address_it (void *cls,
359 const GNUNET_HashCode * key,
362 struct ATS_Address **ap = cls;
363 struct ATS_Address * aa = (struct ATS_Address *) value;
364 struct ATS_Address * ab = *ap;
371 if ( (ntohl (ab->assigned_bw_in.value__) == 0) &&
372 (ntohl (aa->assigned_bw_in.value__) > 0) )
374 /* stick to existing connection */
378 if (ab->atsp_distance > aa->atsp_distance)
380 /* user shorter distance */
384 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
386 /* user lower latency */
396 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
398 struct ATS_Address * aa;
401 GNUNET_CONTAINER_multihashmap_get_multiple (addresses,
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Cannot suggest address for peer `%s'\n",
412 if (aa->active == GNUNET_NO)
414 aa->active = GNUNET_YES;
416 recalculate_assigned_bw ();
420 /* just to be sure... */
421 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin,
422 aa->addr, aa->addr_len,
424 aa->ats, aa->ats_count,
425 aa->assigned_bw_out, aa->assigned_bw_in);
430 // FIXME: this function should likely end up in the LP-subsystem and
431 // not with 'addresses' in the future...
433 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
434 enum GNUNET_ATS_PreferenceKind kind,
437 // do nothing for now...
442 * Initialize address subsystem.
444 * @param cfg configuration to use
447 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
449 GNUNET_assert (GNUNET_OK ==
450 GNUNET_CONFIGURATION_get_value_number (cfg,
454 GNUNET_assert (GNUNET_OK ==
455 GNUNET_CONFIGURATION_get_value_number (cfg,
459 addresses = GNUNET_CONTAINER_multihashmap_create(128);
464 * Free memory of address.
467 * @param key peer identity (unused)
468 * @param value the 'struct ATS_Address' to free
469 * @return GNUNET_OK (continue to iterate)
472 free_address_it (void *cls,
473 const GNUNET_HashCode * key,
476 struct ATS_Address * aa = value;
478 destroy_address (aa);
484 GAS_addresses_destroy_all ()
486 if (addresses != NULL)
487 GNUNET_CONTAINER_multihashmap_iterate(addresses,
488 &free_address_it, NULL);
489 GNUNET_assert (active_addr_count == 0);
494 * Shutdown address subsystem.
497 GAS_addresses_done ()
499 GAS_addresses_destroy_all ();
500 GNUNET_CONTAINER_multihashmap_destroy (addresses);
505 /* end of gnunet-service-ats_addresses.c */