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?
78 struct ATS_Network * next;
80 struct ATS_Network * prev;
87 struct ATS_Network * net_head;
89 struct ATS_Network * net_tail;
91 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
93 static unsigned long long wan_quota_in;
95 static unsigned long long wan_quota_out;
97 static unsigned int active_addr_count;
99 static GNUNET_SCHEDULER_TaskIdentifier interface_task;
103 * Update a bandwidth assignment for a peer. This trivial method currently
104 * simply assigns the same share to all active connections.
108 * @param value the 'struct ATS_Address'
109 * @return GNUNET_OK (continue to iterate)
112 update_bw_it (void *cls, const GNUNET_HashCode * key, void *value)
114 struct ATS_Address *aa = value;
116 if (GNUNET_YES != aa->active)
118 GNUNET_assert (active_addr_count > 0);
119 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
120 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
122 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
123 ntohl (aa->assigned_bw_out.value__));
124 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
125 aa->addr_len, aa->session_id,
126 aa->ats, aa->ats_count,
129 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
130 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
131 aa->ats, aa->ats_count, aa->assigned_bw_out,
138 * Some (significant) input changed, recalculate bandwidth assignment
142 recalculate_assigned_bw ()
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 "Recalculating bandwidth for all active connections\n");
146 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
148 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
150 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL);
155 * Destroy the given address.
157 * @param addr address to destroy
158 * @return GNUNET_YES if bandwidth allocations should be recalcualted
161 destroy_address (struct ATS_Address *addr)
166 GNUNET_assert (GNUNET_YES ==
167 GNUNET_CONTAINER_multihashmap_remove (addresses,
168 &addr->peer.hashPubKey,
170 if (GNUNET_YES == addr->active)
173 addr->active = GNUNET_NO;
176 GNUNET_free_non_null (addr->ats);
177 GNUNET_free (addr->plugin);
183 struct CompareAddressContext
185 const struct ATS_Address *search;
186 struct ATS_Address *result;
191 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
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, &peer->hashPubKey,
225 &compare_address_it, &cac);
231 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
232 const char *plugin_name, const void *plugin_addr,
233 size_t plugin_addr_len, uint32_t session_id,
234 const struct GNUNET_ATS_Information *atsi,
237 struct ATS_Address *aa;
238 struct ATS_Address *old;
241 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
242 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
244 aa->addr_len = plugin_addr_len;
245 aa->ats_count = atsi_count;
246 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
248 memcpy (&aa[1], plugin_addr, plugin_addr_len);
249 aa->plugin = GNUNET_strdup (plugin_name);
250 aa->session_id = session_id;
251 old = find_address (peer, aa);
254 GNUNET_assert (GNUNET_OK ==
255 GNUNET_CONTAINER_multihashmap_put (addresses,
256 &peer->hashPubKey, aa,
257 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
259 GNUNET_i2s (peer), aa);
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Updated existing address for peer `%s' %X \n",
266 GNUNET_i2s (peer), old);
267 GNUNET_free_non_null (old->ats);
268 old->session_id = session_id;
272 old->ats_count = aa->ats_count;
273 GNUNET_free (aa->plugin);
276 for (i = 0; i < atsi_count; i++)
277 switch (ntohl (atsi[i].type))
279 case GNUNET_ATS_UTILIZATION_UP:
280 old->atsp_utilization_out.value__ = atsi[i].value;
282 case GNUNET_ATS_UTILIZATION_DOWN:
283 old->atsp_utilization_in.value__ = atsi[i].value;
285 case GNUNET_ATS_QUALITY_NET_DELAY:
286 old->atsp_latency.rel_value = ntohl (atsi[i].value);
288 case GNUNET_ATS_QUALITY_NET_DISTANCE:
289 old->atsp_distance = ntohl (atsi[i].value);
291 case GNUNET_ATS_COST_WAN:
292 old->atsp_cost_wan = ntohl (atsi[i].value);
294 case GNUNET_ATS_COST_LAN:
295 old->atsp_cost_lan = ntohl (atsi[i].value);
297 case GNUNET_ATS_COST_WLAN:
298 old->atsp_cost_wlan = ntohl (atsi[i].value);
301 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
302 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
310 * Update a bandwidth assignment for a peer. This trivial method currently
311 * simply assigns the same share to all active connections.
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 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
328 (aa->addr_len == info->addr_len) &&
329 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "Deleting address for peer `%s': `%s'\n",
333 GNUNET_i2s (&aa->peer), aa->plugin);
334 if (GNUNET_YES == destroy_address (aa))
335 recalculate_assigned_bw ();
338 if (aa->session_id != info->session_id)
339 return GNUNET_OK; /* irrelevant */
340 if (aa->session_id != 0)
341 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
345 if (GNUNET_YES == aa->active)
347 aa->active = GNUNET_NO;
349 recalculate_assigned_bw ();
352 /* session == 0 and addrlen == 0 : destroy address */
353 if (aa->addr_len == 0)
354 (void) destroy_address (aa);
361 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
362 const char *plugin_name, const void *plugin_addr,
363 size_t plugin_addr_len, uint32_t session_id)
365 struct ATS_Address aa;
367 GNUNET_break (0 < strlen (plugin_name));
369 aa.addr_len = plugin_addr_len;
370 aa.addr = plugin_addr;
371 aa.plugin = (char *) plugin_name;
372 aa.session_id = session_id;
373 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
374 &destroy_by_session_id, &aa);
379 * Find a "good" address to use for a peer. If we already have an existing
380 * address, we stick to it. Otherwise, we pick by lowest distance and then
383 * @param cls the 'struct ATS_Address**' where we store the result
385 * @param value another 'struct ATS_Address*' to consider using
386 * @return GNUNET_OK (continue to iterate)
389 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
391 struct ATS_Address **ap = cls;
392 struct ATS_Address *aa = (struct ATS_Address *) value;
393 struct ATS_Address *ab = *ap;
400 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
401 (ntohl (aa->assigned_bw_in.value__) > 0))
403 /* stick to existing connection */
407 if (ab->atsp_distance > aa->atsp_distance)
409 /* user shorter distance */
413 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
415 /* user lower latency */
425 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
426 const char *plugin_name, const void *plugin_addr,
427 size_t plugin_addr_len, uint32_t session_id, int in_use)
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
432 GNUNET_i2s (peer), in_use);
436 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
438 struct ATS_Address *aa;
441 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
442 &find_address_it, &aa);
445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
446 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
449 if (aa->active == GNUNET_NO)
451 aa->active = GNUNET_YES;
453 recalculate_assigned_bw ();
457 /* just to be sure... */
458 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
459 aa->addr_len, aa->session_id,
460 aa->ats, aa->ats_count,
467 // FIXME: this function should likely end up in the LP-subsystem and
468 // not with 'addresses' in the future...
470 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
471 enum GNUNET_ATS_PreferenceKind kind,
474 // do nothing for now...
478 * Returns where the address is located: LAN or WAN or ...
479 * @param addr address
480 * @param addrlen address length
481 * @return location as GNUNET_ATS_Information
484 struct GNUNET_ATS_Information
485 GAS_addresses_type (struct sockaddr * addr, socklen_t addrlen)
487 struct GNUNET_ATS_Information ats;
489 ats.type = ntohl(GNUNET_ATS_ARRAY_TERMINATOR);
490 ats.value = ntohl(GNUNET_ATS_ARRAY_TERMINATOR);
495 interface_proc (void *cls, const char *name,
497 const struct sockaddr *
499 const struct sockaddr *
501 const struct sockaddr *
502 netmask, socklen_t addrlen)
504 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Address: %s\n", GNUNET_a2s(addr, addrlen));
506 struct ATS_Network *net = GNUNET_malloc(sizeof (struct ATS_Network) + addrlen);
507 /* Calculate network */
508 net->length = addrlen;
511 GNUNET_CONTAINER_DLL_insert(net_head, net_tail, net);
519 struct ATS_Network * cur = net_head;
522 GNUNET_CONTAINER_DLL_remove(net_head, net_tail, cur);
529 get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
531 interface_task = GNUNET_SCHEDULER_NO_TASK;
533 GNUNET_OS_network_interfaces_list(interface_proc, NULL);
535 interface_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, get_addresses, NULL);
539 * Initialize address subsystem.
541 * @param cfg configuration to use
544 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
546 GNUNET_assert (GNUNET_OK ==
547 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
550 GNUNET_assert (GNUNET_OK ==
551 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
554 addresses = GNUNET_CONTAINER_multihashmap_create (128);
556 interface_task = GNUNET_SCHEDULER_add_now(get_addresses, NULL);
561 * Free memory of address.
564 * @param key peer identity (unused)
565 * @param value the 'struct ATS_Address' to free
566 * @return GNUNET_OK (continue to iterate)
569 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
571 struct ATS_Address *aa = value;
573 destroy_address (aa);
579 GAS_addresses_destroy_all ()
581 if (addresses != NULL)
582 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
583 GNUNET_assert (active_addr_count == 0);
588 * Shutdown address subsystem.
591 GAS_addresses_done ()
594 if (interface_task != GNUNET_SCHEDULER_NO_TASK)
596 GNUNET_SCHEDULER_cancel(interface_task);
597 interface_task = GNUNET_SCHEDULER_NO_TASK;
599 GAS_addresses_destroy_all ();
600 GNUNET_CONTAINER_multihashmap_destroy (addresses);
605 /* end of gnunet-service-ats_addresses.c */