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
40 * Receive and send buffer windows grow over time. For
41 * how long can 'unused' bandwidth accumulate before we
42 * need to cap it? (specified in seconds).
44 #define MAX_WINDOW_TIME_S (5 * 60)
46 // NOTE: this implementation is simply supposed
47 // to implement a simplistic strategy in-process;
48 // in the future, we plan to replace it with a real
49 // service implementation
53 * Opaque handle to obtain address suggestions.
55 struct GNUNET_ATS_SuggestionContext
59 * Function to call with our final suggestion.
61 GNUNET_ATS_AddressSuggestionCallback cb;
71 struct GNUNET_ATS_Handle *atc;
74 * Which peer are we monitoring?
76 struct GNUNET_PeerIdentity target;
82 * Count number of connected records.
84 * @param cls pointer to counter
85 * @param key identity of the peer associated with the records
86 * @param value a 'struct AllocationRecord'
87 * @return GNUNET_YES (continue iteration)
90 count_connections (void *cls, const GNUNET_HashCode * key, void *value)
92 unsigned int *ac = cls;
93 struct AllocationRecord *ar = value;
95 if (GNUNET_YES == ar->connected)
102 * Closure for 'set_bw_connections'.
104 struct SetBandwidthContext
109 struct GNUNET_ATS_Handle *atc;
112 * Inbound bandwidth to assign.
114 struct GNUNET_BANDWIDTH_Value32NBO bw_in;
117 * Outbound bandwidth to assign.
119 struct GNUNET_BANDWIDTH_Value32NBO bw_out;
124 * Set bandwidth based on record.
126 * @param cls 'struct SetBandwidthContext'
127 * @param key identity of the peer associated with the records
128 * @param value a 'struct AllocationRecord'
129 * @return GNUNET_YES (continue iteration)
132 set_bw_connections (void *cls, const GNUNET_HashCode * key, void *value)
134 struct SetBandwidthContext *sbc = cls;
135 struct AllocationRecord *ar = value;
137 if (GNUNET_YES == ar->connected)
139 ar->bandwidth_in = sbc->bw_in;
140 ar->bandwidth_out = sbc->bw_out;
141 GNUNET_BANDWIDTH_tracker_update_quota (&ar->available_recv_window,
143 if (NULL != sbc->atc->alloc_cb)
144 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
145 (const struct GNUNET_PeerIdentity *) key,
146 ar->plugin_name, ar->session, ar->plugin_addr,
147 ar->plugin_addr_len, ar->bandwidth_out, ar->bandwidth_in);
149 else if (ntohl (ar->bandwidth_out.value__) > 0)
151 ar->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
152 ar->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
153 if (NULL != sbc->atc->alloc_cb)
154 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
155 (const struct GNUNET_PeerIdentity *) key,
156 ar->plugin_name, ar->session, ar->plugin_addr,
157 ar->plugin_addr_len, ar->bandwidth_out, ar->bandwidth_in);
164 * Task run to update bandwidth assignments.
166 * @param cls the 'struct GNUNET_ATS_Handle'
167 * @param tc scheduler context
170 update_bandwidth_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
172 struct GNUNET_ATS_Handle *atc = cls;
174 struct SetBandwidthContext bwc;
176 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
177 /* FIXME: update calculations NICELY; what follows is a naive version */
178 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &count_connections, &ac);
182 GNUNET_assert (ac > 0);
183 bwc.bw_in = GNUNET_BANDWIDTH_value_init (atc->total_bps_in / ac);
184 bwc.bw_out = GNUNET_BANDWIDTH_value_init (atc->total_bps_out / ac);
185 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &set_bw_connections, &bwc);
190 * Calculate an updated bandwidth assignment and notify.
193 * @param change which allocation record changed?
196 update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc,
197 struct AllocationRecord *change)
199 /* FIXME: based on the 'change', update the LP-problem... */
200 if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK)
201 atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task, atc);
206 * Function called with feasbile addresses we might want to suggest.
208 * @param cls the 'struct GNUNET_ATS_SuggestionContext'
209 * @param key identity of the peer
210 * @param value a 'struct AllocationRecord' for the peer
211 * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet
214 suggest_address (void *cls, const GNUNET_HashCode * key, void *value)
216 struct GNUNET_ATS_SuggestionContext *asc = cls;
217 struct AllocationRecord *ar = value;
220 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
221 "Suggesting address for peer `%s'\n", GNUNET_h2s (key));
224 /* trivial strategy: pick first available address... */
225 asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
226 ar->plugin_addr_len, ar->session,
227 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_out / 32),
228 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_in / 32),
236 map_it (void *cls, const GNUNET_HashCode * key, void *value)
238 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api", "Found entry for %s\n",
244 * We would like to establish a new connection with a peer.
245 * ATS should suggest a good address to begin with.
248 * @param peer identity of the new peer
249 * @param cb function to call with the address
250 * @param cb_cls closure for cb
252 struct GNUNET_ATS_SuggestionContext *
253 GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
254 const struct GNUNET_PeerIdentity *peer,
255 GNUNET_ATS_AddressSuggestionCallback cb,
258 struct GNUNET_ATS_SuggestionContext *asc;
261 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
262 "Looking up suggested address for peer `%s'\n",
265 asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
267 asc->cb_cls = cb_cls;
270 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers, &peer->hashPubKey,
271 &suggest_address, asc);
278 GNUNET_CONTAINER_multihashmap_put (atc->notify_map, &peer->hashPubKey, asc,
279 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
285 * Cancel suggestion request.
287 * @param asc handle of the request to cancel
290 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
292 GNUNET_assert (GNUNET_OK ==
293 GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
294 &asc->target.hashPubKey,
301 * Initialize the ATS subsystem.
303 * @param cfg configuration to use
304 * @param alloc_cb notification to call whenever the allocation changed
305 * @param alloc_cb_cls closure for 'alloc_cb'
306 * @return ats context
308 struct GNUNET_ATS_Handle *
309 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
310 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb,
313 struct GNUNET_ATS_Handle *atc;
316 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api", "ATS init\n");
318 atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_Handle));
320 atc->alloc_cb = alloc_cb;
321 atc->alloc_cb_cls = alloc_cb_cls;
322 atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
323 atc->notify_map = GNUNET_CONTAINER_multihashmap_create (256);
324 GNUNET_CONFIGURATION_get_value_number (cfg, "core", "TOTAL_QUOTA_OUT",
325 &atc->total_bps_out);
326 GNUNET_CONFIGURATION_get_value_number (cfg, "core", "TOTAL_QUOTA_IN",
333 * Free an allocation record.
336 * @param key identity of the peer associated with the record
337 * @param value the 'struct AllocationRecord' to free
338 * @return GNUNET_OK (continue to iterate)
341 destroy_allocation_record (void *cls, const GNUNET_HashCode * key, void *value)
343 struct AllocationRecord *ar = value;
345 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
346 GNUNET_free (ar->plugin_name);
353 * Shutdown the ATS subsystem.
358 GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc)
361 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api", "ATS shutdown\n");
363 if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
365 GNUNET_SCHEDULER_cancel (atc->ba_task);
366 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
368 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_allocation_record,
370 GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
371 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
372 GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
373 atc->notify_map = NULL;
379 * Closure for 'update_session'
381 struct UpdateSessionContext
386 struct GNUNET_ATS_Handle *atc;
389 * Allocation record with new information.
391 struct AllocationRecord *arnew;
396 * Update an allocation record, merging with the new information
398 * @param cls a new 'struct AllocationRecord'
399 * @param key identity of the peer associated with the records
400 * @param value the old 'struct AllocationRecord'
401 * @return GNUNET_YES if the records do not match,
402 * GNUNET_NO if the record do match and 'old' was updated
405 update_session (void *cls, const GNUNET_HashCode * key, void *value)
407 struct UpdateSessionContext *usc = cls;
408 struct AllocationRecord *arnew = usc->arnew;
409 struct AllocationRecord *arold = value;
417 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
418 "Updating session for peer `%s' plugin `%s'\n",
419 GNUNET_h2s (key), arold->plugin_name);
422 if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
424 if (((arnew->session == arold->session) && (arnew->session != NULL)) ||
425 ((arold->session == NULL) &&
426 (arold->plugin_addr_len == arnew->plugin_addr_len) &&
428 memcmp (arold->plugin_addr, arnew->plugin_addr,
429 arnew->plugin_addr_len))))
433 if (arnew->session != arold->session)
435 arold->session = arnew->session;
438 if ((arnew->connected == GNUNET_YES) && (arold->connected == GNUNET_NO))
440 arold->connected = GNUNET_YES;
444 /* Update existing value */
446 while (c_new < arnew->ats_count)
450 while (c_old < arold->ats_count)
452 if (arold->ats[c_old].type == arnew->ats[c_new].type)
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
456 "Found type %i, old value=%i new value=%i\n",
457 ntohl (arold->ats[c_old].type),
458 ntohl (arold->ats[c_old].value),
459 ntohl (arnew->ats[c_new].value));
461 arold->ats[c_old].value = arnew->ats[c_new].value;
467 if (found == GNUNET_NO)
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new type %i new value=%i\n",
471 ntohl (arnew->ats[c_new].type),
472 ntohl (arnew->ats[c_new].value));
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Old array size: %u\n",
476 GNUNET_array_grow (arold->ats, arold->ats_count, arold->ats_count + 1);
477 GNUNET_assert (arold->ats_count >= 2);
478 arold->ats[arold->ats_count - 2].type = arnew->ats[c_new].type;
479 arold->ats[arold->ats_count - 2].value = arnew->ats[c_new].value;
480 arold->ats[arold->ats_count - 1].type = htonl (0);
481 arold->ats[arold->ats_count - 1].value = htonl (0);
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New array size: %i\n",
490 if (GNUNET_YES == change)
491 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_TRANSPORT_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,
525 GNUNET_assert (ats_count > 0);
526 GNUNET_array_grow (ar->ats, ar->ats_count, ats_count);
527 memcpy (ar->ats, ats,
528 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
534 * Mark all matching allocation records as not connected.
536 * @param cls 'struct GTS_AtsHandle'
537 * @param key identity of the peer associated with the record
538 * @param value the 'struct AllocationRecord' to clear the 'connected' flag
539 * @return GNUNET_OK (continue to iterate)
542 disconnect_peer (void *cls, const GNUNET_HashCode * key, void *value)
544 struct GNUNET_ATS_Handle *atc = cls;
545 struct AllocationRecord *ar = value;
547 if (GNUNET_YES == ar->connected)
549 ar->connected = GNUNET_NO;
550 update_bandwidth_assignment (atc, ar);
557 * We established a new connection with a peer (for example, because
558 * core asked for it or because the other peer connected to us).
559 * Calculate bandwidth assignments including the new peer.
562 * @param peer identity of the new peer
563 * @param plugin_name name of the currently used transport plugin
564 * @param session session in use (if available)
565 * @param plugin_addr address in use (if available)
566 * @param plugin_addr_len number of bytes in plugin_addr
567 * @param ats performance data for the connection
568 * @param ats_count number of performance records in 'ats'
571 GNUNET_ATS_peer_connect (struct GNUNET_ATS_Handle *atc,
572 const struct GNUNET_PeerIdentity *peer,
573 const char *plugin_name, struct Session *session,
574 const void *plugin_addr, size_t plugin_addr_len,
575 const struct GNUNET_TRANSPORT_ATS_Information *ats,
578 struct AllocationRecord *ar;
579 struct UpdateSessionContext usc;
581 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &disconnect_peer,
583 ar = create_allocation_record (plugin_name, session, plugin_addr,
584 plugin_addr_len, ats, ats_count);
585 ar->connected = GNUNET_YES;
589 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
591 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
594 GNUNET_assert (GNUNET_OK ==
595 GNUNET_CONTAINER_multihashmap_put (atc->peers,
596 &peer->hashPubKey, ar,
597 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
602 * We disconnected from the given peer (for example, because ats, core
603 * or blacklist asked for it or because the other peer disconnected).
604 * Calculate bandwidth assignments without the peer.
607 * @param peer identity of the new peer
610 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc,
611 const struct GNUNET_PeerIdentity *peer)
613 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
615 &disconnect_peer, atc);
620 * Closure for 'destroy_allocation_record'
622 struct SessionDestroyContext
627 struct GNUNET_ATS_Handle *atc;
630 * Session being destroyed.
632 const struct Session *session;
637 * Free an allocation record matching the given session.
639 * @param cls the 'struct SessionDestroyContext'
640 * @param key identity of the peer associated with the record
641 * @param value the 'struct AllocationRecord' to free
642 * @return GNUNET_OK (continue to iterate)
645 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
647 struct SessionDestroyContext *sdc = cls;
648 struct AllocationRecord *ar = value;
650 if (ar->session != sdc->session)
653 if (ar->plugin_addr != NULL)
655 GNUNET_assert (GNUNET_OK ==
656 GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers, key,
658 if (GNUNET_YES == ar->connected) ;
660 /* FIXME: is this supposed to be allowed? What to do then? */
663 destroy_allocation_record (NULL, key, ar);
669 * A session got destroyed, stop including it as a valid address.
672 * @param peer identity of the peer
673 * @param session session handle that is no longer valid
676 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc,
677 const struct GNUNET_PeerIdentity *peer,
678 const struct Session *session)
680 struct SessionDestroyContext sdc;
683 sdc.session = session;
684 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_session,
690 * Notify validation watcher that an entry is now valid
692 * @param cls 'struct ValidationEntry' that is now valid
693 * @param key peer identity (unused)
694 * @param value a 'GST_ValidationIteratorContext' to notify
695 * @return GNUNET_YES (continue to iterate)
698 notify_valid (void *cls, const GNUNET_HashCode * key, void *value)
700 struct AllocationRecord *ar = cls;
701 struct GNUNET_ATS_SuggestionContext *asc = value;
703 asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
704 ar->plugin_addr_len, ar->session,
705 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_out / 32),
706 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_in / 32),
707 ar->ats, ar->ats_count);
708 GNUNET_ATS_suggest_address_cancel (asc);
715 * We have updated performance statistics for a given address. Note
716 * that this function can be called for addresses that are currently
717 * in use as well as addresses that are valid but not actively in use.
718 * Furthermore, the peer may not even be connected to us right now (in
719 * which case the call may be ignored or the information may be stored
720 * for later use). Update bandwidth assignments.
723 * @param peer identity of the peer
724 * @param valid_until how long is the address valid?
725 * @param plugin_name name of the transport plugin
726 * @param session session handle (if available)
727 * @param plugin_addr address (if available)
728 * @param plugin_addr_len number of bytes in plugin_addr
729 * @param ats performance data for the address
730 * @param ats_count number of performance records in 'ats'
733 GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc,
734 const struct GNUNET_PeerIdentity *peer,
735 struct GNUNET_TIME_Absolute valid_until,
736 const char *plugin_name, struct Session *session,
737 const void *plugin_addr, size_t plugin_addr_len,
738 const struct GNUNET_TRANSPORT_ATS_Information *ats,
741 struct AllocationRecord *ar;
742 struct UpdateSessionContext usc;
745 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
746 "Updating address for peer `%s', plugin `%s'\n",
747 GNUNET_i2s (peer), plugin_name);
749 ar = create_allocation_record (plugin_name, session, plugin_addr,
750 plugin_addr_len, ats, ats_count);
754 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
756 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
760 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
761 "Adding new address for peer `%s', plugin `%s'\n",
762 GNUNET_i2s (peer), plugin_name);
764 GNUNET_assert (GNUNET_OK ==
765 GNUNET_CONTAINER_multihashmap_put (atc->peers,
766 &peer->hashPubKey, ar,
767 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
768 GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
769 &peer->hashPubKey, ¬ify_valid,
773 /* end of file gnunet-service-transport_ats.c */