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"
35 #include "gnunet-service-ats_addresses_mlp.h"
44 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
46 static unsigned long long wan_quota_in;
48 static unsigned long long wan_quota_out;
50 static unsigned int active_addr_count;
55 * Update a bandwidth assignment for a peer. This trivial method currently
56 * simply assigns the same share to all active connections.
60 * @param value the 'struct ATS_Address'
61 * @return GNUNET_OK (continue to iterate)
64 update_bw_it (void *cls, const GNUNET_HashCode * key, void *value)
66 struct ATS_Address *aa = value;
68 if (GNUNET_YES != aa->active)
70 GNUNET_assert (active_addr_count > 0);
71 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
72 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
74 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
75 ntohl (aa->assigned_bw_out.value__));
76 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
77 aa->addr_len, aa->session_id,
78 aa->ats, aa->ats_count,
81 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
82 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
83 aa->ats, aa->ats_count, aa->assigned_bw_out,
90 * Some (significant) input changed, recalculate bandwidth assignment
94 recalculate_assigned_bw ()
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97 "Recalculating bandwidth for all active connections\n");
98 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
100 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
102 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL);
107 * Destroy the given address.
109 * @param addr address to destroy
110 * @return GNUNET_YES if bandwidth allocations should be recalcualted
113 destroy_address (struct ATS_Address *addr)
118 GNUNET_assert (GNUNET_YES ==
119 GNUNET_CONTAINER_multihashmap_remove (addresses,
120 &addr->peer.hashPubKey,
122 if (GNUNET_YES == addr->active)
125 addr->active = GNUNET_NO;
128 GNUNET_free_non_null (addr->ats);
129 GNUNET_free (addr->plugin);
135 struct CompareAddressContext
137 const struct ATS_Address *search;
138 struct ATS_Address *result;
143 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
145 struct CompareAddressContext *cac = cls;
146 struct ATS_Address *aa = value;
148 if (((aa->addr_len != cac->search->addr_len) ||
149 (0 != strcmp (aa->plugin, cac->search->plugin)) ||
150 (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len))) &&
151 ((aa->session_id != cac->search->session_id) ||
152 (cac->search->session_id == 0)))
160 * Find an existing equivalent address record.
161 * Compares by peer identity and network address OR by session ID
162 * (one of the two must match).
164 * @param peer peer to lookup addresses for
165 * @param addr existing address record
166 * @return existing address record, NULL for none
169 find_address (const struct GNUNET_PeerIdentity *peer,
170 const struct ATS_Address *addr)
172 struct CompareAddressContext cac;
176 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
177 &compare_address_it, &cac);
183 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
184 const char *plugin_name, const void *plugin_addr,
185 size_t plugin_addr_len, uint32_t session_id,
186 const struct GNUNET_ATS_Information *atsi,
189 struct ATS_Address *aa;
190 struct ATS_Address *old;
193 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
194 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
196 aa->addr_len = plugin_addr_len;
197 aa->ats_count = atsi_count;
198 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
200 memcpy (&aa[1], plugin_addr, plugin_addr_len);
201 aa->plugin = GNUNET_strdup (plugin_name);
202 aa->session_id = session_id;
203 old = find_address (peer, aa);
206 GNUNET_assert (GNUNET_OK ==
207 GNUNET_CONTAINER_multihashmap_put (addresses,
208 &peer->hashPubKey, aa,
209 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
211 GNUNET_i2s (peer), aa);
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "Updated existing address for peer `%s' %X \n",
218 GNUNET_i2s (peer), old);
219 GNUNET_free_non_null (old->ats);
220 old->session_id = session_id;
224 old->ats_count = aa->ats_count;
225 GNUNET_free (aa->plugin);
228 for (i = 0; i < atsi_count; i++)
229 switch (ntohl (atsi[i].type))
231 case GNUNET_ATS_UTILIZATION_UP:
232 old->atsp_utilization_out.value__ = atsi[i].value;
234 case GNUNET_ATS_UTILIZATION_DOWN:
235 old->atsp_utilization_in.value__ = atsi[i].value;
237 case GNUNET_ATS_QUALITY_NET_DELAY:
238 old->atsp_latency.rel_value = ntohl (atsi[i].value);
240 case GNUNET_ATS_QUALITY_NET_DISTANCE:
241 old->atsp_distance = ntohl (atsi[i].value);
243 case GNUNET_ATS_COST_WAN:
244 old->atsp_cost_wan = ntohl (atsi[i].value);
246 case GNUNET_ATS_COST_LAN:
247 old->atsp_cost_lan = ntohl (atsi[i].value);
249 case GNUNET_ATS_COST_WLAN:
250 old->atsp_cost_wlan = ntohl (atsi[i].value);
252 case GNUNET_ATS_NETWORK_TYPE:
253 old->atsp_network_type = ntohl (atsi[i].value);
257 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
258 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
268 * If session != 0, just the session is deleted, the address itself still exists
269 * If session == 0, remove full address
270 * If session == 0 and addrlen == 0, destroy inbound address
274 * @param value the 'struct ATS_Address'
275 * @return GNUNET_OK (continue to iterate)
278 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
280 const struct ATS_Address *info = cls;
281 struct ATS_Address *aa = value;
284 memcmp (&aa->peer, &info->peer,
285 sizeof (struct GNUNET_PeerIdentity)));
286 /* session == 0, remove full address */
287 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
288 (aa->addr_len == info->addr_len) &&
289 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Deleting address for peer `%s': `%s'\n",
293 GNUNET_i2s (&aa->peer), aa->plugin);
294 if (GNUNET_YES == destroy_address (aa))
295 recalculate_assigned_bw ();
298 /* session != 0, just remove session */
299 if (aa->session_id != info->session_id)
300 return GNUNET_OK; /* irrelevant */
301 if (aa->session_id != 0)
302 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
306 if (GNUNET_YES == aa->active)
308 aa->active = GNUNET_NO;
310 recalculate_assigned_bw ();
313 /* session == 0 and addrlen == 0 : destroy address */
314 if (aa->addr_len == 0)
315 (void) destroy_address (aa);
322 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
323 const char *plugin_name, const void *plugin_addr,
324 size_t plugin_addr_len, uint32_t session_id)
326 struct ATS_Address aa;
328 GNUNET_break (0 < strlen (plugin_name));
330 aa.addr_len = plugin_addr_len;
331 aa.addr = plugin_addr;
332 aa.plugin = (char *) plugin_name;
333 aa.session_id = session_id;
334 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
335 &destroy_by_session_id, &aa);
340 * Find a "good" address to use for a peer. If we already have an existing
341 * address, we stick to it. Otherwise, we pick by lowest distance and then
344 * @param cls the 'struct ATS_Address**' where we store the result
346 * @param value another 'struct ATS_Address*' to consider using
347 * @return GNUNET_OK (continue to iterate)
350 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
352 struct ATS_Address **ap = cls;
353 struct ATS_Address *aa = (struct ATS_Address *) value;
354 struct ATS_Address *ab = *ap;
361 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
362 (ntohl (aa->assigned_bw_in.value__) > 0))
364 /* stick to existing connection */
368 if (ab->atsp_distance > aa->atsp_distance)
370 /* user shorter distance */
374 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
376 /* user lower latency */
386 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
387 const char *plugin_name, const void *plugin_addr,
388 size_t plugin_addr_len, uint32_t session_id, int in_use)
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
393 GNUNET_i2s (peer), in_use);
397 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
399 struct ATS_Address *aa;
402 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
403 &find_address_it, &aa);
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
410 if (aa->active == GNUNET_NO)
412 aa->active = GNUNET_YES;
414 recalculate_assigned_bw ();
418 /* just to be sure... */
419 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
420 aa->addr_len, aa->session_id,
421 aa->ats, aa->ats_count,
428 // FIXME: this function should likely end up in the LP-subsystem and
429 // not with 'addresses' in the future...
431 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
432 enum GNUNET_ATS_PreferenceKind kind,
435 // do nothing for now...
441 * Initialize address subsystem.
443 * @param cfg configuration to use
446 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
448 GNUNET_assert (GNUNET_OK ==
449 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
452 GNUNET_assert (GNUNET_OK ==
453 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
459 switch (GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP"))
465 /* Init the MLP solver with default values */
466 GAS_mlp_init (MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
477 /* No configuration value */
485 addresses = GNUNET_CONTAINER_multihashmap_create (128);
490 * Free memory of address.
493 * @param key peer identity (unused)
494 * @param value the 'struct ATS_Address' to free
495 * @return GNUNET_OK (continue to iterate)
498 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
500 struct ATS_Address *aa = value;
502 destroy_address (aa);
508 GAS_addresses_destroy_all ()
510 if (addresses != NULL)
511 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
512 GNUNET_assert (active_addr_count == 0);
517 * Shutdown address subsystem.
520 GAS_addresses_done ()
522 GAS_addresses_destroy_all ();
523 GNUNET_CONTAINER_multihashmap_destroy (addresses);
535 /* end of gnunet-service-ats_addresses.c */