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,
256 * We would like to establish a new connection with a peer.
257 * ATS should suggest a good address to begin with.
260 * @param peer identity of the new peer
261 * @param cb function to call with the address
262 * @param cb_cls closure for cb
264 struct GNUNET_ATS_SuggestionContext *
265 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *atc,
266 const struct GNUNET_PeerIdentity *peer,
267 GNUNET_ATS_AddressSuggestionCallback cb,
270 struct GNUNET_ATS_SuggestionContext *asc;
273 LOG (GNUNET_ERROR_TYPE_DEBUG, "Looking up suggested address for peer `%s'\n",
276 asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
278 asc->cb_cls = cb_cls;
281 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
283 &suggest_address, asc);
290 GNUNET_CONTAINER_multihashmap_put (atc->notify_map, &peer->hashPubKey, asc,
291 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
297 * Cancel suggestion request.
299 * @param asc handle of the request to cancel
302 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
304 GNUNET_assert (GNUNET_OK ==
305 GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
306 &asc->target.hashPubKey,
313 * Initialize the ATS subsystem.
315 * @param cfg configuration to use
316 * @param alloc_cb notification to call whenever the allocation changed
317 * @param alloc_cb_cls closure for 'alloc_cb'
318 * @return ats context
320 struct GNUNET_ATS_SchedulingHandle *
321 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
322 GNUNET_ATS_AddressSuggestionCallback alloc_cb,
325 struct GNUNET_ATS_SchedulingHandle *atc;
328 LOG (GNUNET_ERROR_TYPE_DEBUG, "ATS init\n");
330 atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
332 atc->alloc_cb = alloc_cb;
333 atc->alloc_cb_cls = alloc_cb_cls;
334 atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
335 atc->notify_map = GNUNET_CONTAINER_multihashmap_create (256);
336 GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "WAN_QUOTA_OUT",
337 &atc->total_bps_out);
338 GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "WAN_QUOTA_IN",
345 * Free an allocation record.
348 * @param key identity of the peer associated with the record
349 * @param value the 'struct AllocationRecord' to free
350 * @return GNUNET_OK (continue to iterate)
353 destroy_allocation_record (void *cls, const GNUNET_HashCode * key, void *value)
355 struct AllocationRecord *ar = value;
357 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
358 GNUNET_free (ar->plugin_name);
365 * Shutdown the ATS subsystem.
370 GNUNET_ATS_shutdown (struct GNUNET_ATS_SchedulingHandle *atc)
373 LOG (GNUNET_ERROR_TYPE_DEBUG, "ATS shutdown\n");
375 if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
377 GNUNET_SCHEDULER_cancel (atc->ba_task);
378 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
380 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_allocation_record,
382 GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
383 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
384 GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
385 atc->notify_map = NULL;
391 * Closure for 'update_session'
393 struct UpdateSessionContext
398 struct GNUNET_ATS_SchedulingHandle *atc;
401 * Allocation record with new information.
403 struct AllocationRecord *arnew;
408 * Update an allocation record, merging with the new information
410 * @param cls a new 'struct AllocationRecord'
411 * @param key identity of the peer associated with the records
412 * @param value the old 'struct AllocationRecord'
413 * @return GNUNET_YES if the records do not match,
414 * GNUNET_NO if the record do match and 'old' was updated
417 update_session (void *cls, const GNUNET_HashCode * key, void *value)
419 struct UpdateSessionContext *usc = cls;
420 struct AllocationRecord *arnew = usc->arnew;
421 struct AllocationRecord *arold = value;
427 if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
430 (((arnew->session == arold->session) && (arnew->session != NULL)) ||
431 ((arold->session == NULL) &&
432 (arold->plugin_addr_len == arnew->plugin_addr_len) &&
434 memcmp (arold->plugin_addr, arnew->plugin_addr,
435 arnew->plugin_addr_len)))))
436 return GNUNET_YES; /* no match */
439 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating session for peer `%s' plugin `%s'\n",
440 GNUNET_h2s (key), arold->plugin_name);
442 if (arnew->session != arold->session)
444 arold->session = arnew->session;
446 if ((arnew->connected == GNUNET_YES) && (arold->connected == GNUNET_NO))
448 arold->connected = GNUNET_YES;
451 /* Update existing value */
453 while (c_new < arnew->ats_count)
457 while (c_old < arold->ats_count)
459 if (arold->ats[c_old].type == arnew->ats[c_new].type)
462 LOG (GNUNET_ERROR_TYPE_DEBUG,
463 "Found type %i, old value=%i new value=%i\n",
464 ntohl (arold->ats[c_old].type), ntohl (arold->ats[c_old].value),
465 ntohl (arnew->ats[c_new].value));
467 arold->ats[c_old].value = arnew->ats[c_new].value;
473 if (found == GNUNET_NO)
476 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added new type %i new value=%i\n",
477 ntohl (arnew->ats[c_new].type), ntohl (arnew->ats[c_new].value));
478 LOG (GNUNET_ERROR_TYPE_DEBUG, "Old array size: %u\n", arold->ats_count);
480 GNUNET_array_grow (arold->ats, arold->ats_count, arold->ats_count + 1);
481 GNUNET_assert (arold->ats_count >= 2);
482 arold->ats[arold->ats_count - 2].type = arnew->ats[c_new].type;
483 arold->ats[arold->ats_count - 2].value = arnew->ats[c_new].value;
484 arold->ats[arold->ats_count - 1].type = htonl (0);
485 arold->ats[arold->ats_count - 1].value = htonl (0);
487 LOG (GNUNET_ERROR_TYPE_DEBUG, "New array size: %i\n", arold->ats_count);
493 update_bandwidth_assignment (usc->atc, arold);
499 * Create an allocation record with the given properties.
501 * @param plugin_name name of the currently used transport plugin
502 * @param session session in use (if available)
503 * @param plugin_addr address in use (if available)
504 * @param plugin_addr_len number of bytes in plugin_addr
505 * @param ats performance data for the connection
506 * @param ats_count number of performance records in 'ats'
508 static struct AllocationRecord *
509 create_allocation_record (const char *plugin_name, struct Session *session,
510 const void *plugin_addr, size_t plugin_addr_len,
511 const struct GNUNET_ATS_Information *ats,
514 struct AllocationRecord *ar;
516 ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
517 ar->plugin_name = GNUNET_strdup (plugin_name);
518 ar->plugin_addr = &ar[1];
519 memcpy (&ar[1], plugin_addr, plugin_addr_len);
520 ar->session = session;
521 ar->plugin_addr_len = plugin_addr_len;
522 GNUNET_BANDWIDTH_tracker_init (&ar->available_recv_window, ar->bandwidth_in,
524 GNUNET_assert (ats_count > 0);
525 GNUNET_array_grow (ar->ats, ar->ats_count, ats_count);
526 memcpy (ar->ats, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
527 ar->connected = GNUNET_SYSERR; /* aka: not known / no change */
533 * Mark all matching allocation records as not connected.
535 * @param cls 'struct GTS_AtsHandle'
536 * @param key identity of the peer associated with the record
537 * @param value the 'struct AllocationRecord' to clear the 'connected' flag
538 * @return GNUNET_OK (continue to iterate)
541 disconnect_peer (void *cls, const GNUNET_HashCode * key, void *value)
543 struct GNUNET_ATS_SchedulingHandle *atc = cls;
544 struct AllocationRecord *ar = value;
546 if (GNUNET_YES == ar->connected)
548 ar->connected = GNUNET_NO;
549 update_bandwidth_assignment (atc, ar);
556 * We established a new connection with a peer (for example, because
557 * core asked for it or because the other peer connected to us).
558 * Calculate bandwidth assignments including the new peer.
561 * @param peer identity of the new peer
562 * @param plugin_name name of the currently used transport plugin
563 * @param session session in use (if available)
564 * @param plugin_addr address in use (if available)
565 * @param plugin_addr_len number of bytes in plugin_addr
566 * @param ats performance data for the connection
567 * @param ats_count number of performance records in 'ats'
570 GNUNET_ATS_peer_connect (struct GNUNET_ATS_SchedulingHandle *atc,
571 const struct GNUNET_PeerIdentity *peer,
572 const char *plugin_name, struct Session *session,
573 const void *plugin_addr, size_t plugin_addr_len,
574 const struct GNUNET_ATS_Information *ats,
577 struct AllocationRecord *ar;
578 struct UpdateSessionContext usc;
581 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s\n", GNUNET_i2s (peer));
584 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &disconnect_peer,
586 ar = create_allocation_record (plugin_name, session, plugin_addr,
587 plugin_addr_len, ats, ats_count);
588 ar->connected = GNUNET_YES;
592 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
594 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
597 GNUNET_assert (GNUNET_OK ==
598 GNUNET_CONTAINER_multihashmap_put (atc->peers,
599 &peer->hashPubKey, ar,
600 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
605 * We disconnected from the given peer (for example, because ats, core
606 * or blacklist asked for it or because the other peer disconnected).
607 * Calculate bandwidth assignments without the peer.
610 * @param peer identity of the new peer
613 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_SchedulingHandle *atc,
614 const struct GNUNET_PeerIdentity *peer)
617 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnected from peer %s\n",
620 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
622 &disconnect_peer, atc);
627 * Closure for 'destroy_allocation_record'
629 struct SessionDestroyContext
634 struct GNUNET_ATS_SchedulingHandle *atc;
637 * Session being destroyed.
639 const struct Session *session;
644 * Free an allocation record matching the given session.
646 * @param cls the 'struct SessionDestroyContext'
647 * @param key identity of the peer associated with the record
648 * @param value the 'struct AllocationRecord' to free
649 * @return GNUNET_OK (continue to iterate)
652 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
654 struct SessionDestroyContext *sdc = cls;
655 struct AllocationRecord *ar = value;
657 if (ar->session != sdc->session)
660 if (ar->plugin_addr != NULL)
662 GNUNET_assert (GNUNET_OK ==
663 GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers, key,
665 if (GNUNET_YES == ar->connected) ;
667 /* FIXME: is this supposed to be allowed? What to do then? */
670 destroy_allocation_record (NULL, key, ar);
676 * A session got destroyed, stop including it as a valid address.
679 * @param peer identity of the peer
680 * @param session session handle that is no longer valid
683 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_SchedulingHandle *atc,
684 const struct GNUNET_PeerIdentity *peer,
685 const struct Session *session)
687 struct SessionDestroyContext sdc;
690 sdc.session = session;
691 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_session,
697 * Notify validation watcher that an entry is now valid
699 * @param cls 'struct ValidationEntry' that is now valid
700 * @param key peer identity (unused)
701 * @param value a 'GST_ValidationIteratorContext' to notify
702 * @return GNUNET_YES (continue to iterate)
705 notify_valid (void *cls, const GNUNET_HashCode * key, void *value)
707 struct AllocationRecord *ar = cls;
708 struct GNUNET_ATS_SuggestionContext *asc = value;
710 asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
711 ar->plugin_addr_len, ar->session,
712 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_out / 32),
713 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_in / 32), ar->ats,
715 GNUNET_ATS_suggest_address_cancel (asc);
722 * We have updated performance statistics for a given address. Note
723 * that this function can be called for addresses that are currently
724 * in use as well as addresses that are valid but not actively in use.
725 * Furthermore, the peer may not even be connected to us right now (in
726 * which case the call may be ignored or the information may be stored
727 * for later use). Update bandwidth assignments.
730 * @param peer identity of the peer
731 * @param valid_until how long is the address valid?
732 * @param plugin_name name of the transport plugin
733 * @param session session handle (if available)
734 * @param plugin_addr address (if available)
735 * @param plugin_addr_len number of bytes in plugin_addr
736 * @param ats performance data for the address
737 * @param ats_count number of performance records in 'ats'
740 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *atc,
741 const struct GNUNET_PeerIdentity *peer,
742 struct GNUNET_TIME_Absolute valid_until,
743 const char *plugin_name, struct Session *session,
744 const void *plugin_addr, size_t plugin_addr_len,
745 const struct GNUNET_ATS_Information *ats,
748 struct AllocationRecord *ar;
749 struct UpdateSessionContext usc;
752 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s', plugin `%s'\n",
753 GNUNET_i2s (peer), plugin_name);
755 ar = create_allocation_record (plugin_name, session, plugin_addr,
756 plugin_addr_len, ats, ats_count);
760 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
762 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
766 LOG (GNUNET_ERROR_TYPE_DEBUG,
767 "Adding new address for peer `%s', plugin `%s'\n", GNUNET_i2s (peer),
770 ar->connected = GNUNET_NO;
771 GNUNET_assert (GNUNET_OK ==
772 GNUNET_CONTAINER_multihashmap_put (atc->peers,
773 &peer->hashPubKey, ar,
774 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
775 GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
776 &peer->hashPubKey, ¬ify_valid,
780 /* end of file gnunet-service-transport_ats.c */