2 This file is part of GNUnet.
3 (C) 2010,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 * @brief automatic transport selection API
23 * @author Christian Grothoff
24 * @author Matthias Wachs
28 * - extend API to get performance data
29 * - implement simplistic strategy based on say 'lowest latency' or strict ordering
30 * - extend API to get peer preferences, implement proportional bandwidth assignment
31 * - re-implement API against a real ATS service (!)
34 #include "gnunet_ats_service.h"
37 #define DEBUG_ATS GNUNET_EXTRA_LOGGING
39 #define LOG(kind,...) GNUNET_log_from (kind, "ats-api", __VA_ARGS__)
42 * Receive and send buffer windows grow over time. For
43 * how long can 'unused' bandwidth accumulate before we
44 * need to cap it? (specified in seconds).
46 #define MAX_WINDOW_TIME_S (5 * 60)
48 // NOTE: this implementation is simply supposed
49 // to implement a simplistic strategy in-process;
50 // in the future, we plan to replace it with a real
51 // service implementation
55 * Opaque handle to obtain address suggestions.
57 struct GNUNET_ATS_SuggestionContext
61 * Function to call with our final suggestion.
63 GNUNET_ATS_AddressSuggestionCallback cb;
73 struct GNUNET_ATS_SchedulingHandle *atc;
76 * Which peer are we monitoring?
78 struct GNUNET_PeerIdentity target;
84 * Count number of connected records.
86 * @param cls pointer to counter
87 * @param key identity of the peer associated with the records
88 * @param value a 'struct AllocationRecord'
89 * @return GNUNET_YES (continue iteration)
92 count_connections (void *cls, const GNUNET_HashCode * key, void *value)
94 unsigned int *ac = cls;
95 struct AllocationRecord *ar = value;
97 if (GNUNET_YES == ar->connected)
104 * Closure for 'set_bw_connections'.
106 struct SetBandwidthContext
111 struct GNUNET_ATS_SchedulingHandle *atc;
114 * Inbound bandwidth to assign.
116 struct GNUNET_BANDWIDTH_Value32NBO bw_in;
119 * Outbound bandwidth to assign.
121 struct GNUNET_BANDWIDTH_Value32NBO bw_out;
126 * Set bandwidth based on record.
128 * @param cls 'struct SetBandwidthContext'
129 * @param key identity of the peer associated with the records
130 * @param value a 'struct AllocationRecord'
131 * @return GNUNET_YES (continue iteration)
134 set_bw_connections (void *cls, const GNUNET_HashCode * key, void *value)
136 struct SetBandwidthContext *sbc = cls;
137 struct AllocationRecord *ar = value;
139 GNUNET_assert (GNUNET_SYSERR != ar->connected);
140 /* FIXME: ||1 because we currently NEVER get 'connected' events... */
141 if ((GNUNET_YES == ar->connected) || 1)
143 ar->bandwidth_in = sbc->bw_in;
144 ar->bandwidth_out = sbc->bw_out;
145 GNUNET_BANDWIDTH_tracker_update_quota (&ar->available_recv_window,
147 LOG (GNUNET_ERROR_TYPE_DEBUG,
148 "Bandwidth assigned to peer %s is i:%u/o:%u bytes/s\n",
149 GNUNET_i2s ((const struct GNUNET_PeerIdentity *) key),
150 ntohl (ar->bandwidth_in.value__), ntohl (ar->bandwidth_out.value__));
151 if (NULL != sbc->atc->alloc_cb)
152 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
153 (const struct GNUNET_PeerIdentity *) key,
154 ar->plugin_name, ar->plugin_addr, ar->plugin_addr_len,
155 ar->session, ar->bandwidth_out, ar->bandwidth_in,
158 else if (ntohl (ar->bandwidth_out.value__) > 0)
160 ar->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
161 ar->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
162 if (NULL != sbc->atc->alloc_cb)
163 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
164 (const struct GNUNET_PeerIdentity *) key,
165 ar->plugin_name, ar->plugin_addr, ar->plugin_addr_len,
166 ar->session, ar->bandwidth_out, ar->bandwidth_in,
170 LOG (GNUNET_ERROR_TYPE_DEBUG,
171 "Not communicating bandwidth assigned to peer %s: not connected and bw is: i:%u/o:%u bytes/s\n",
172 GNUNET_i2s ((const struct GNUNET_PeerIdentity *) key),
173 ntohl (ar->bandwidth_in.value__), ntohl (ar->bandwidth_out.value__));
180 * Task run to update bandwidth assignments.
182 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
183 * @param tc scheduler context
186 update_bandwidth_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
188 struct GNUNET_ATS_SchedulingHandle *atc = cls;
190 struct SetBandwidthContext bwc;
192 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
193 /* FIXME: update calculations NICELY; what follows is a naive version */
194 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &count_connections, &ac);
198 GNUNET_assert (ac > 0);
199 bwc.bw_in = GNUNET_BANDWIDTH_value_init (atc->total_bps_in / ac);
200 bwc.bw_out = GNUNET_BANDWIDTH_value_init (atc->total_bps_out / ac);
201 LOG (GNUNET_ERROR_TYPE_DEBUG,
202 "Trivial implementation: bandwidth assigned to each peer is i:%u/o:%u bytes/s\n",
203 ntohl (bwc.bw_in.value__), ntohl (bwc.bw_out.value__));
204 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &set_bw_connections, &bwc);
209 * Calculate an updated bandwidth assignment and notify.
212 * @param change which allocation record changed?
215 update_bandwidth_assignment (struct GNUNET_ATS_SchedulingHandle *atc,
216 struct AllocationRecord *change)
218 /* FIXME: based on the 'change', update the LP-problem... */
219 if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK)
220 atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task, atc);
225 * Function called with feasbile addresses we might want to suggest.
227 * @param cls the 'struct GNUNET_ATS_SuggestionContext'
228 * @param key identity of the peer
229 * @param value a 'struct AllocationRecord' for the peer
230 * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet
233 suggest_address (void *cls, const GNUNET_HashCode * key, void *value)
235 struct GNUNET_ATS_SuggestionContext *asc = cls;
236 struct AllocationRecord *ar = value;
239 LOG (GNUNET_ERROR_TYPE_DEBUG,
240 "Suggesting address for peer `%s', starting with i:%u/o:%u bytes/s\n",
241 GNUNET_h2s (key), asc->atc->total_bps_in / 32,
242 asc->atc->total_bps_out / 32);
245 /* trivial strategy: pick first available address... */
246 asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
247 ar->plugin_addr_len, ar->session,
248 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_out / 32),
249 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_in / 32), ar->ats,
257 map_it (void *cls, const GNUNET_HashCode * key, void *value)
259 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found entry for %s\n", GNUNET_h2s (key));
264 * We would like to establish a new connection with a peer.
265 * ATS should suggest a good address to begin with.
268 * @param peer identity of the new peer
269 * @param cb function to call with the address
270 * @param cb_cls closure for cb
272 struct GNUNET_ATS_SuggestionContext *
273 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *atc,
274 const struct GNUNET_PeerIdentity *peer,
275 GNUNET_ATS_AddressSuggestionCallback cb,
278 struct GNUNET_ATS_SuggestionContext *asc;
281 LOG (GNUNET_ERROR_TYPE_DEBUG, "Looking up suggested address for peer `%s'\n",
284 asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
286 asc->cb_cls = cb_cls;
289 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
291 &suggest_address, asc);
298 GNUNET_CONTAINER_multihashmap_put (atc->notify_map, &peer->hashPubKey, asc,
299 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
305 * Cancel suggestion request.
307 * @param asc handle of the request to cancel
310 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
312 GNUNET_assert (GNUNET_OK ==
313 GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
314 &asc->target.hashPubKey,
321 * Initialize the ATS subsystem.
323 * @param cfg configuration to use
324 * @param alloc_cb notification to call whenever the allocation changed
325 * @param alloc_cb_cls closure for 'alloc_cb'
326 * @return ats context
328 struct GNUNET_ATS_SchedulingHandle *
329 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
330 GNUNET_ATS_AddressSuggestionCallback alloc_cb,
333 struct GNUNET_ATS_SchedulingHandle *atc;
336 LOG (GNUNET_ERROR_TYPE_DEBUG, "ATS init\n");
338 atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
340 atc->alloc_cb = alloc_cb;
341 atc->alloc_cb_cls = alloc_cb_cls;
342 atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
343 atc->notify_map = GNUNET_CONTAINER_multihashmap_create (256);
344 GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "WAN_QUOTA_OUT",
345 &atc->total_bps_out);
346 GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "WAN_QUOTA_IN",
353 * Free an allocation record.
356 * @param key identity of the peer associated with the record
357 * @param value the 'struct AllocationRecord' to free
358 * @return GNUNET_OK (continue to iterate)
361 destroy_allocation_record (void *cls, const GNUNET_HashCode * key, void *value)
363 struct AllocationRecord *ar = value;
365 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
366 GNUNET_free (ar->plugin_name);
373 * Shutdown the ATS subsystem.
378 GNUNET_ATS_shutdown (struct GNUNET_ATS_SchedulingHandle *atc)
381 LOG (GNUNET_ERROR_TYPE_DEBUG, "ATS shutdown\n");
383 if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
385 GNUNET_SCHEDULER_cancel (atc->ba_task);
386 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
388 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_allocation_record,
390 GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
391 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
392 GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
393 atc->notify_map = NULL;
399 * Closure for 'update_session'
401 struct UpdateSessionContext
406 struct GNUNET_ATS_SchedulingHandle *atc;
409 * Allocation record with new information.
411 struct AllocationRecord *arnew;
416 * Update an allocation record, merging with the new information
418 * @param cls a new 'struct AllocationRecord'
419 * @param key identity of the peer associated with the records
420 * @param value the old 'struct AllocationRecord'
421 * @return GNUNET_YES if the records do not match,
422 * GNUNET_NO if the record do match and 'old' was updated
425 update_session (void *cls, const GNUNET_HashCode * key, void *value)
427 struct UpdateSessionContext *usc = cls;
428 struct AllocationRecord *arnew = usc->arnew;
429 struct AllocationRecord *arold = value;
435 if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
438 (((arnew->session == arold->session) && (arnew->session != NULL)) ||
439 ((arold->session == NULL) &&
440 (arold->plugin_addr_len == arnew->plugin_addr_len) &&
442 memcmp (arold->plugin_addr, arnew->plugin_addr,
443 arnew->plugin_addr_len)))))
444 return GNUNET_YES; /* no match */
447 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating session for peer `%s' plugin `%s'\n",
448 GNUNET_h2s (key), arold->plugin_name);
450 if (arnew->session != arold->session)
452 arold->session = arnew->session;
454 if ((arnew->connected == GNUNET_YES) && (arold->connected == GNUNET_NO))
456 arold->connected = GNUNET_YES;
459 /* Update existing value */
461 while (c_new < arnew->ats_count)
465 while (c_old < arold->ats_count)
467 if (arold->ats[c_old].type == arnew->ats[c_new].type)
470 LOG (GNUNET_ERROR_TYPE_DEBUG,
471 "Found type %i, old value=%i new value=%i\n",
472 ntohl (arold->ats[c_old].type), ntohl (arold->ats[c_old].value),
473 ntohl (arnew->ats[c_new].value));
475 arold->ats[c_old].value = arnew->ats[c_new].value;
481 if (found == GNUNET_NO)
484 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added new type %i new value=%i\n",
485 ntohl (arnew->ats[c_new].type), ntohl (arnew->ats[c_new].value));
486 LOG (GNUNET_ERROR_TYPE_DEBUG, "Old array size: %u\n", arold->ats_count);
488 GNUNET_array_grow (arold->ats, arold->ats_count, arold->ats_count + 1);
489 GNUNET_assert (arold->ats_count >= 2);
490 arold->ats[arold->ats_count - 2].type = arnew->ats[c_new].type;
491 arold->ats[arold->ats_count - 2].value = arnew->ats[c_new].value;
492 arold->ats[arold->ats_count - 1].type = htonl (0);
493 arold->ats[arold->ats_count - 1].value = htonl (0);
495 LOG (GNUNET_ERROR_TYPE_DEBUG, "New array size: %i\n", arold->ats_count);
501 update_bandwidth_assignment (usc->atc, arold);
507 * Create an allocation record with the given properties.
509 * @param plugin_name name of the currently used transport plugin
510 * @param session session in use (if available)
511 * @param plugin_addr address in use (if available)
512 * @param plugin_addr_len number of bytes in plugin_addr
513 * @param ats performance data for the connection
514 * @param ats_count number of performance records in 'ats'
516 static struct AllocationRecord *
517 create_allocation_record (const char *plugin_name, struct Session *session,
518 const void *plugin_addr, size_t plugin_addr_len,
519 const struct GNUNET_ATS_Information *ats,
522 struct AllocationRecord *ar;
524 ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
525 ar->plugin_name = GNUNET_strdup (plugin_name);
526 ar->plugin_addr = &ar[1];
527 memcpy (&ar[1], plugin_addr, plugin_addr_len);
528 ar->session = session;
529 ar->plugin_addr_len = plugin_addr_len;
530 GNUNET_BANDWIDTH_tracker_init (&ar->available_recv_window, ar->bandwidth_in,
532 GNUNET_assert (ats_count > 0);
533 GNUNET_array_grow (ar->ats, ar->ats_count, ats_count);
534 memcpy (ar->ats, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
535 ar->connected = GNUNET_SYSERR; /* aka: not known / no change */
541 * Mark all matching allocation records as not connected.
543 * @param cls 'struct GTS_AtsHandle'
544 * @param key identity of the peer associated with the record
545 * @param value the 'struct AllocationRecord' to clear the 'connected' flag
546 * @return GNUNET_OK (continue to iterate)
549 disconnect_peer (void *cls, const GNUNET_HashCode * key, void *value)
551 struct GNUNET_ATS_SchedulingHandle *atc = cls;
552 struct AllocationRecord *ar = value;
554 if (GNUNET_YES == ar->connected)
556 ar->connected = GNUNET_NO;
557 update_bandwidth_assignment (atc, ar);
564 * We established a new connection with a peer (for example, because
565 * core asked for it or because the other peer connected to us).
566 * Calculate bandwidth assignments including the new peer.
569 * @param peer identity of the new peer
570 * @param plugin_name name of the currently used transport plugin
571 * @param session session in use (if available)
572 * @param plugin_addr address in use (if available)
573 * @param plugin_addr_len number of bytes in plugin_addr
574 * @param ats performance data for the connection
575 * @param ats_count number of performance records in 'ats'
578 GNUNET_ATS_peer_connect (struct GNUNET_ATS_SchedulingHandle *atc,
579 const struct GNUNET_PeerIdentity *peer,
580 const char *plugin_name, struct Session *session,
581 const void *plugin_addr, size_t plugin_addr_len,
582 const struct GNUNET_ATS_Information *ats,
585 struct AllocationRecord *ar;
586 struct UpdateSessionContext usc;
589 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s\n", GNUNET_i2s (peer));
592 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &disconnect_peer,
594 ar = create_allocation_record (plugin_name, session, plugin_addr,
595 plugin_addr_len, ats, ats_count);
596 ar->connected = GNUNET_YES;
600 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
602 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
605 GNUNET_assert (GNUNET_OK ==
606 GNUNET_CONTAINER_multihashmap_put (atc->peers,
607 &peer->hashPubKey, ar,
608 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
613 * We disconnected from the given peer (for example, because ats, core
614 * or blacklist asked for it or because the other peer disconnected).
615 * Calculate bandwidth assignments without the peer.
618 * @param peer identity of the new peer
621 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_SchedulingHandle *atc,
622 const struct GNUNET_PeerIdentity *peer)
625 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnected from peer %s\n",
628 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
630 &disconnect_peer, atc);
635 * Closure for 'destroy_allocation_record'
637 struct SessionDestroyContext
642 struct GNUNET_ATS_SchedulingHandle *atc;
645 * Session being destroyed.
647 const struct Session *session;
652 * Free an allocation record matching the given session.
654 * @param cls the 'struct SessionDestroyContext'
655 * @param key identity of the peer associated with the record
656 * @param value the 'struct AllocationRecord' to free
657 * @return GNUNET_OK (continue to iterate)
660 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
662 struct SessionDestroyContext *sdc = cls;
663 struct AllocationRecord *ar = value;
665 if (ar->session != sdc->session)
668 if (ar->plugin_addr != NULL)
670 GNUNET_assert (GNUNET_OK ==
671 GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers, key,
673 if (GNUNET_YES == ar->connected) ;
675 /* FIXME: is this supposed to be allowed? What to do then? */
678 destroy_allocation_record (NULL, key, ar);
684 * A session got destroyed, stop including it as a valid address.
687 * @param peer identity of the peer
688 * @param session session handle that is no longer valid
691 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_SchedulingHandle *atc,
692 const struct GNUNET_PeerIdentity *peer,
693 const struct Session *session)
695 struct SessionDestroyContext sdc;
698 sdc.session = session;
699 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_session,
705 * Notify validation watcher that an entry is now valid
707 * @param cls 'struct ValidationEntry' that is now valid
708 * @param key peer identity (unused)
709 * @param value a 'GST_ValidationIteratorContext' to notify
710 * @return GNUNET_YES (continue to iterate)
713 notify_valid (void *cls, const GNUNET_HashCode * key, void *value)
715 struct AllocationRecord *ar = cls;
716 struct GNUNET_ATS_SuggestionContext *asc = value;
718 asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
719 ar->plugin_addr_len, ar->session,
720 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_out / 32),
721 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_in / 32), ar->ats,
723 GNUNET_ATS_suggest_address_cancel (asc);
730 * We have updated performance statistics for a given address. Note
731 * that this function can be called for addresses that are currently
732 * in use as well as addresses that are valid but not actively in use.
733 * Furthermore, the peer may not even be connected to us right now (in
734 * which case the call may be ignored or the information may be stored
735 * for later use). Update bandwidth assignments.
738 * @param peer identity of the peer
739 * @param valid_until how long is the address valid?
740 * @param plugin_name name of the transport plugin
741 * @param session session handle (if available)
742 * @param plugin_addr address (if available)
743 * @param plugin_addr_len number of bytes in plugin_addr
744 * @param ats performance data for the address
745 * @param ats_count number of performance records in 'ats'
748 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *atc,
749 const struct GNUNET_PeerIdentity *peer,
750 struct GNUNET_TIME_Absolute valid_until,
751 const char *plugin_name, struct Session *session,
752 const void *plugin_addr, size_t plugin_addr_len,
753 const struct GNUNET_ATS_Information *ats,
756 struct AllocationRecord *ar;
757 struct UpdateSessionContext usc;
760 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s', plugin `%s'\n",
761 GNUNET_i2s (peer), plugin_name);
763 ar = create_allocation_record (plugin_name, session, plugin_addr,
764 plugin_addr_len, ats, ats_count);
768 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
770 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
774 LOG (GNUNET_ERROR_TYPE_DEBUG,
775 "Adding new address for peer `%s', plugin `%s'\n", GNUNET_i2s (peer),
778 ar->connected = GNUNET_NO;
779 GNUNET_assert (GNUNET_OK ==
780 GNUNET_CONTAINER_multihashmap_put (atc->peers,
781 &peer->hashPubKey, ar,
782 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
783 GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
784 &peer->hashPubKey, ¬ify_valid,
788 /* end of file gnunet-service-transport_ats.c */