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,
97 const GNUNET_HashCode * key,
100 struct ATS_Address *aa = value;
102 if (GNUNET_YES != aa->active)
104 GNUNET_assert (active_addr_count > 0);
105 aa->assigned_bw_in.value__ = htonl (total_quota_in / active_addr_count);
106 aa->assigned_bw_out.value__ = htonl (total_quota_out / active_addr_count);
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108 "New bandwidth for peer %s is %u/%u\n",
109 GNUNET_i2s (&aa->peer),
110 ntohl (aa->assigned_bw_in.value__),
111 ntohl (aa->assigned_bw_out.value__));
112 GAS_scheduling_transmit_address_suggestion (&aa->peer,
114 aa->addr, aa->addr_len,
116 aa->ats, aa->ats_count,
117 aa->assigned_bw_out, aa->assigned_bw_in);
118 GAS_reservations_set_bandwidth (&aa->peer,
120 GAS_performance_notify_clients (&aa->peer,
122 aa->addr, aa->addr_len,
123 aa->ats, aa->ats_count,
124 aa->assigned_bw_out, aa->assigned_bw_in);
130 * Some (significant) input changed, recalculate bandwidth assignment
134 recalculate_assigned_bw ()
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137 "Recalculating bandwidth for all active connections\n");
138 GNUNET_STATISTICS_update (GSA_stats,
139 "# bandwidth recalculations performed",
142 GNUNET_STATISTICS_set (GSA_stats,
143 "# active addresses",
146 GNUNET_CONTAINER_multihashmap_iterate (addresses,
153 * Destroy the given address.
155 * @param addr address to destroy
156 * @return GNUNET_YES if bandwidth allocations should be recalcualted
159 destroy_address (struct ATS_Address *addr)
164 GNUNET_assert (GNUNET_YES ==
165 GNUNET_CONTAINER_multihashmap_remove(addresses,
166 &addr->peer.hashPubKey,
168 if (GNUNET_YES == addr->active)
171 addr->active = GNUNET_NO;
174 GNUNET_free_non_null (addr->ats);
175 GNUNET_free (addr->plugin);
181 struct CompareAddressContext
183 const struct ATS_Address * search;
184 struct ATS_Address * result;
189 compare_address_it (void *cls,
190 const GNUNET_HashCode * key,
193 struct CompareAddressContext * cac = cls;
194 struct ATS_Address * aa = value;
196 if ( ( (aa->addr_len != cac->search->addr_len) ||
197 (0 != strcmp(aa->plugin, cac->search->plugin)) ||
198 (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len)) ) &&
199 ( (aa->session_id != cac->search->session_id) ||
200 (cac->search->session_id == 0) ))
208 * Find an existing equivalent address record.
209 * Compares by peer identity and network address OR by session ID
210 * (one of the two must match).
212 * @param peer peer to lookup addresses for
213 * @param addr existing address record
214 * @return existing address record, NULL for none
217 find_address (const struct GNUNET_PeerIdentity *peer,
218 const struct ATS_Address * addr)
220 struct CompareAddressContext cac;
224 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
233 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
234 const char *plugin_name,
235 const void *plugin_addr, size_t plugin_addr_len,
237 const struct GNUNET_ATS_Information *atsi,
240 struct ATS_Address * aa;
241 struct ATS_Address * old;
244 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
245 aa->ats = GNUNET_malloc(atsi_count * sizeof (struct GNUNET_ATS_Information));
247 aa->addr_len = plugin_addr_len;
248 aa->ats_count = atsi_count;
249 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
251 memcpy (&aa[1], plugin_addr, plugin_addr_len);
252 aa->plugin = GNUNET_strdup (plugin_name);
253 aa->session_id = session_id;
254 old = find_address (peer, aa);
257 GNUNET_assert (GNUNET_OK ==
258 GNUNET_CONTAINER_multihashmap_put (addresses,
261 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263 "Added new address for peer `%s' %X\n",
264 GNUNET_i2s (peer), aa);
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270 "Updated existing address for peer `%s' %X \n",
271 GNUNET_i2s (peer), old);
272 GNUNET_free_non_null (old->ats);
273 old->session_id = session_id;
277 old->ats_count = aa->ats_count;
278 GNUNET_free (aa->plugin);
281 for (i=0;i<atsi_count;i++)
282 switch (ntohl (atsi[i].type))
284 case GNUNET_ATS_UTILIZATION_UP:
285 old->atsp_utilization_out.value__ = atsi[i].value;
287 case GNUNET_ATS_UTILIZATION_DOWN:
288 old->atsp_utilization_in.value__ = atsi[i].value;
290 case GNUNET_ATS_QUALITY_NET_DELAY:
291 old->atsp_latency.rel_value = ntohl (atsi[i].value);
293 case GNUNET_ATS_QUALITY_NET_DISTANCE:
294 old->atsp_distance = ntohl (atsi[i].value);
296 case GNUNET_ATS_COST_WAN:
297 old->atsp_cost_wan = ntohl (atsi[i].value);
299 case GNUNET_ATS_COST_LAN:
300 old->atsp_cost_lan = ntohl (atsi[i].value);
302 case GNUNET_ATS_COST_WLAN:
303 old->atsp_cost_wlan = ntohl (atsi[i].value);
306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
307 "Received unsupported ATS type %u\n",
308 ntohl (atsi[i].type));
316 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
317 const char *plugin_name,
318 const void *plugin_addr, size_t plugin_addr_len,
321 struct ATS_Address aa;
322 struct ATS_Address *res;
325 aa.addr_len = plugin_addr_len;
326 aa.addr = plugin_addr;
327 aa.plugin = (char*) plugin_name;
328 aa.session_id = session_id;
329 res = find_address (peer, &aa);
332 /* we don't even know this one, can this happen? */
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334 "Asked to delete unknown address for peer `%s'\n",
338 if ( (aa.session_id == session_id) &&
340 (res->addr_len > 0) )
342 /* just session died */
344 if (GNUNET_YES == res->active)
346 res->active = GNUNET_NO;
348 recalculate_assigned_bw ();
352 /* destroy address entirely (either was only session or was
353 not even with a session) */
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Deleting address for peer `%s': `%s'\n",
356 GNUNET_i2s (peer), plugin_name);
357 if (GNUNET_YES == destroy_address (res))
358 recalculate_assigned_bw ();
363 * Find a "good" address to use for a peer. If we already have an existing
364 * address, we stick to it. Otherwise, we pick by lowest distance and then
367 * @param cls the 'struct ATS_Address**' where we store the result
369 * @param value another 'struct ATS_Address*' to consider using
370 * @return GNUNET_OK (continue to iterate)
373 find_address_it (void *cls,
374 const GNUNET_HashCode * key,
377 struct ATS_Address **ap = cls;
378 struct ATS_Address * aa = (struct ATS_Address *) value;
379 struct ATS_Address * ab = *ap;
386 if ( (ntohl (ab->assigned_bw_in.value__) == 0) &&
387 (ntohl (aa->assigned_bw_in.value__) > 0) )
389 /* stick to existing connection */
393 if (ab->atsp_distance > aa->atsp_distance)
395 /* user shorter distance */
399 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
401 /* user lower latency */
411 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
413 struct ATS_Address * aa;
416 GNUNET_CONTAINER_multihashmap_get_multiple (addresses,
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Cannot suggest address for peer `%s'\n",
427 if (aa->active == GNUNET_NO)
429 aa->active = GNUNET_YES;
431 recalculate_assigned_bw ();
435 /* just to be sure... */
436 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin,
437 aa->addr, aa->addr_len,
439 aa->ats, aa->ats_count,
440 aa->assigned_bw_out, aa->assigned_bw_in);
445 // FIXME: this function should likely end up in the LP-subsystem and
446 // not with 'addresses' in the future...
448 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
449 enum GNUNET_ATS_PreferenceKind kind,
452 // do nothing for now...
457 * Initialize address subsystem.
459 * @param cfg configuration to use
462 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
464 GNUNET_assert (GNUNET_OK ==
465 GNUNET_CONFIGURATION_get_value_number (cfg,
469 GNUNET_assert (GNUNET_OK ==
470 GNUNET_CONFIGURATION_get_value_number (cfg,
474 addresses = GNUNET_CONTAINER_multihashmap_create(128);
479 * Free memory of address.
482 * @param key peer identity (unused)
483 * @param value the 'struct ATS_Address' to free
484 * @return GNUNET_OK (continue to iterate)
487 free_address_it (void *cls,
488 const GNUNET_HashCode * key,
491 struct ATS_Address * aa = value;
493 destroy_address (aa);
499 GAS_addresses_destroy_all ()
501 if (addresses != NULL)
502 GNUNET_CONTAINER_multihashmap_iterate(addresses,
503 &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 */