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
27 * - extend API to get performance data
28 * - implement simplistic strategy based on say 'lowest latency' or strict ordering
29 * - extend API to get peer preferences, implement proportional bandwidth assignment
30 * - re-implement API against a real ATS service (!)
33 #include "gnunet_ats_service.h"
35 // NOTE: this implementation is simply supposed
36 // to implement a simplistic strategy in-process;
37 // in the future, we plan to replace it with a real
38 // service implementation
41 * Allocation record for a peer's address.
43 struct AllocationRecord
47 * Performance information associated with this address (array).
49 struct GNUNET_TRANSPORT_ATS_Information *ats;
57 * Address this record represents, allocated at the end of this struct.
59 const void *plugin_addr;
62 * Session associated with this record.
64 struct Session *session;
67 * Number of bytes in plugin_addr.
69 size_t plugin_addr_len;
72 * Number of entries in 'ats'.
77 * Bandwidth assigned to this address right now, 0 for none.
79 struct GNUNET_BANDWIDTH_Value32NBO bandwidth;
82 * Set to GNUNET_YES if this is the connected address of a connected peer.
90 * Opaque handle to obtain address suggestions.
92 struct GNUNET_ATS_SuggestionContext
96 * Function to call with our final suggestion.
98 GNUNET_ATS_AddressSuggestionCallback cb;
108 struct GNUNET_ATS_Handle *atc;
111 * Which peer are we monitoring?
113 struct GNUNET_PeerIdentity target;
119 * Handle to the ATS subsystem.
121 struct GNUNET_ATS_Handle
126 const struct GNUNET_CONFIGURATION_Handle *cfg;
129 * Function to call when the allocation changes.
131 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb;
134 * Closure for 'alloc_cb'.
139 * Information about all connected peers. Maps peer identities
140 * to one or more 'struct AllocationRecord' values.
142 struct GNUNET_CONTAINER_MultiHashMap *peers;
145 * Map of PeerIdentities to 'struct GNUNET_ATS_SuggestionContext's.
147 struct GNUNET_CONTAINER_MultiHashMap *notify_map;
151 * Task scheduled to update our bandwidth assignment.
153 GNUNET_SCHEDULER_TaskIdentifier ba_task;
156 * Total bandwidth per configuration.
158 unsigned long long total_bps;
163 * Count number of connected records.
165 * @param cls pointer to counter
166 * @param key identity of the peer associated with the records
167 * @param value a 'struct AllocationRecord'
168 * @return GNUNET_YES (continue iteration)
171 count_connections (void *cls,
172 const GNUNET_HashCode *key,
175 unsigned int *ac = cls;
176 struct AllocationRecord *ar = value;
178 if (GNUNET_YES == ar->connected)
185 * Closure for 'set_bw_connections'.
187 struct SetBandwidthContext
192 struct GNUNET_ATS_Handle *atc;
195 * Bandwidth to assign.
197 struct GNUNET_BANDWIDTH_Value32NBO bw;
202 * Set bandwidth based on record.
204 * @param cls 'struct SetBandwidthContext'
205 * @param key identity of the peer associated with the records
206 * @param value a 'struct AllocationRecord'
207 * @return GNUNET_YES (continue iteration)
210 set_bw_connections (void *cls,
211 const GNUNET_HashCode *key,
214 struct SetBandwidthContext *sbc = cls;
215 struct AllocationRecord *ar = value;
217 if (GNUNET_YES == ar->connected)
219 ar->bandwidth = sbc->bw;
220 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
221 (const struct GNUNET_PeerIdentity*) key,
228 else if (ntohl(ar->bandwidth.value__) > 0)
230 ar->bandwidth = GNUNET_BANDWIDTH_value_init (0);
231 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
232 (const struct GNUNET_PeerIdentity*) key,
244 * Task run to update bandwidth assignments.
246 * @param cls the 'struct GNUNET_ATS_Handle'
247 * @param tc scheduler context
250 update_bandwidth_task (void *cls,
251 const struct GNUNET_SCHEDULER_TaskContext *tc)
253 struct GNUNET_ATS_Handle *atc = cls;
255 struct SetBandwidthContext bwc;
257 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
258 /* FIXME: update calculations NICELY; what follows is a naive version */
259 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
263 bwc.bw = GNUNET_BANDWIDTH_value_init (atc->total_bps / ac);
264 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
271 * Calculate an updated bandwidth assignment and notify.
274 * @param change which allocation record changed?
277 update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc,
278 struct AllocationRecord *change)
280 /* FIXME: based on the 'change', update the LP-problem... */
281 if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK)
282 atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task,
288 * Function called with feasbile addresses we might want to suggest.
290 * @param cls the 'struct GNUNET_ATS_SuggestionContext'
291 * @param key identity of the peer
292 * @param value a 'struct AllocationRecord' for the peer
293 * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet
296 suggest_address (void *cls,
297 const GNUNET_HashCode *key,
300 struct GNUNET_ATS_SuggestionContext *asc = cls;
301 struct AllocationRecord *ar = value;
303 /* trivial strategy: pick first available address... */
304 asc->cb (asc->cb_cls,
309 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32),
310 ar->ats, ar->ats_count);
317 * We would like to establish a new connection with a peer.
318 * ATS should suggest a good address to begin with.
321 * @param peer identity of the new peer
322 * @param cb function to call with the address
323 * @param cb_cls closure for cb
325 struct GNUNET_ATS_SuggestionContext *
326 GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
327 const struct GNUNET_PeerIdentity *peer,
328 GNUNET_ATS_AddressSuggestionCallback cb,
331 struct GNUNET_ATS_SuggestionContext *asc;
333 asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
335 asc->cb_cls = cb_cls;
338 GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
347 GNUNET_CONTAINER_multihashmap_put (atc->notify_map,
350 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
356 * Cancel suggestion request.
358 * @param asc handle of the request to cancel
361 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
363 GNUNET_assert (GNUNET_OK ==
364 GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
365 &asc->target.hashPubKey,
372 * Initialize the ATS subsystem.
374 * @param cfg configuration to use
375 * @param alloc_cb notification to call whenever the allocation changed
376 * @param alloc_cb_cls closure for 'alloc_cb'
377 * @return ats context
379 struct GNUNET_ATS_Handle *
380 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
381 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb,
384 struct GNUNET_ATS_Handle *atc;
386 atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_Handle));
388 atc->alloc_cb = alloc_cb;
389 atc->alloc_cb_cls = alloc_cb_cls;
390 atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
391 GNUNET_CONFIGURATION_get_value_number (cfg,
400 * Free an allocation record.
403 * @param key identity of the peer associated with the record
404 * @param value the 'struct AllocationRecord' to free
405 * @return GNUNET_OK (continue to iterate)
408 destroy_allocation_record (void *cls,
409 const GNUNET_HashCode *key,
412 struct AllocationRecord *ar = value;
414 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
415 GNUNET_free (ar->plugin_name);
422 * Shutdown the ATS subsystem.
427 GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc)
429 if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
431 GNUNET_SCHEDULER_cancel (atc->ba_task);
432 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
434 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
435 &destroy_allocation_record,
437 GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
438 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
439 GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
440 atc->notify_map = NULL;
446 * Closure for 'update_session'
448 struct UpdateSessionContext
453 struct GNUNET_ATS_Handle *atc;
456 * Allocation record with new information.
458 struct AllocationRecord *arnew;
463 * Update an allocation record, merging with the new information
465 * @param cls a new 'struct AllocationRecord'
466 * @param key identity of the peer associated with the records
467 * @param value the old 'struct AllocationRecord'
468 * @return GNUNET_YES if the records do not match,
469 * GNUNET_NO if the record do match and 'old' was updated
472 update_session (void *cls,
473 const GNUNET_HashCode *key,
476 struct UpdateSessionContext *usc = cls;
477 struct AllocationRecord *arnew = usc->arnew;
478 struct AllocationRecord *arold = value;
481 if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
483 if ( (arnew->session == arold->session) ||
484 ( (arold->session == NULL) &&
485 (arold->plugin_addr_len == arnew->plugin_addr_len) &&
486 (0 == memcmp (arold->plugin_addr,
488 arnew->plugin_addr_len)) ) )
492 if (arnew->session != arold->session)
494 arold->session = arnew->session;
497 if ( (arnew->connected == GNUNET_YES) &&
498 (arold->connected == GNUNET_NO) )
500 arold->connected = GNUNET_YES;
503 // FIXME: merge ats arrays of (arold, arnew);
505 if (GNUNET_YES == change)
506 update_bandwidth_assignment (usc->atc, arold);
514 * Create an allocation record with the given properties.
516 * @param plugin_name name of the currently used transport plugin
517 * @param session session in use (if available)
518 * @param plugin_addr address in use (if available)
519 * @param plugin_addr_len number of bytes in plugin_addr
520 * @param ats performance data for the connection
521 * @param ats_count number of performance records in 'ats'
523 static struct AllocationRecord *
524 create_allocation_record (const char *plugin_name,
525 struct Session *session,
526 const void *plugin_addr,
527 size_t plugin_addr_len,
528 const struct GNUNET_TRANSPORT_ATS_Information *ats,
531 struct AllocationRecord *ar;
533 ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
534 ar->plugin_name = GNUNET_strdup (plugin_name);
535 ar->plugin_addr = &ar[1];
536 memcpy (&ar[1], plugin_addr, plugin_addr_len);
537 ar->session = session;
538 ar->plugin_addr_len = plugin_addr_len;
539 GNUNET_array_grow (ar->ats,
544 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
550 * Mark all matching allocation records as not connected.
552 * @param cls 'struct GTS_AtsHandle'
553 * @param key identity of the peer associated with the record
554 * @param value the 'struct AllocationRecord' to clear the 'connected' flag
555 * @return GNUNET_OK (continue to iterate)
558 disconnect_peer (void *cls,
559 const GNUNET_HashCode *key,
562 struct GNUNET_ATS_Handle *atc = cls;
563 struct AllocationRecord *ar = value;
565 if (GNUNET_YES == ar->connected)
567 ar->connected = GNUNET_NO;
568 update_bandwidth_assignment (atc, ar);
575 * We established a new connection with a peer (for example, because
576 * core asked for it or because the other peer connected to us).
577 * Calculate bandwidth assignments including the new peer.
580 * @param peer identity of the new peer
581 * @param plugin_name name of the currently used transport plugin
582 * @param session session in use (if available)
583 * @param plugin_addr address in use (if available)
584 * @param plugin_addr_len number of bytes in plugin_addr
585 * @param ats performance data for the connection
586 * @param ats_count number of performance records in 'ats'
589 GNUNET_ATS_peer_connect (struct GNUNET_ATS_Handle *atc,
590 const struct GNUNET_PeerIdentity *peer,
591 const char *plugin_name,
592 struct Session *session,
593 const void *plugin_addr,
594 size_t plugin_addr_len,
595 const struct GNUNET_TRANSPORT_ATS_Information *ats,
598 struct AllocationRecord *ar;
599 struct UpdateSessionContext usc;
601 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
604 ar = create_allocation_record (plugin_name,
610 ar->connected = GNUNET_YES;
614 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
618 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
621 GNUNET_assert (GNUNET_OK ==
622 GNUNET_CONTAINER_multihashmap_put (atc->peers,
625 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
630 * We disconnected from the given peer (for example, because ats, core
631 * or blacklist asked for it or because the other peer disconnected).
632 * Calculate bandwidth assignments without the peer.
635 * @param peer identity of the new peer
638 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc,
639 const struct GNUNET_PeerIdentity *peer)
641 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
649 * Closure for 'destroy_allocation_record'
651 struct SessionDestroyContext
656 struct GNUNET_ATS_Handle *atc;
659 * Session being destroyed.
661 const struct Session *session;
666 * Free an allocation record matching the given session.
668 * @param cls the 'struct SessionDestroyContext'
669 * @param key identity of the peer associated with the record
670 * @param value the 'struct AllocationRecord' to free
671 * @return GNUNET_OK (continue to iterate)
674 destroy_session (void *cls,
675 const GNUNET_HashCode *key,
678 struct SessionDestroyContext *sdc = cls;
679 struct AllocationRecord *ar = value;
681 if (ar->session != sdc->session)
684 if (ar->plugin_addr != NULL)
686 GNUNET_assert (GNUNET_OK ==
687 GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers,
690 if (GNUNET_YES == ar->connected);
692 /* FIXME: is this supposed to be allowed? What to do then? */
695 destroy_allocation_record (NULL, key, ar);
701 * A session got destroyed, stop including it as a valid address.
704 * @param peer identity of the peer
705 * @param session session handle that is no longer valid
708 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc,
709 const struct GNUNET_PeerIdentity *peer,
710 const struct Session *session)
712 struct SessionDestroyContext sdc;
715 sdc.session = session;
716 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
723 * Notify validation watcher that an entry is now valid
725 * @param cls 'struct ValidationEntry' that is now valid
726 * @param key peer identity (unused)
727 * @param value a 'GST_ValidationIteratorContext' to notify
728 * @return GNUNET_YES (continue to iterate)
731 notify_valid (void *cls,
732 const GNUNET_HashCode *key,
735 struct AllocationRecord *ar = cls;
736 struct GNUNET_ATS_SuggestionContext *asc = value;
738 asc->cb (asc->cb_cls,
743 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32),
744 ar->ats, ar->ats_count);
745 GNUNET_ATS_suggest_address_cancel (asc);
751 * We have updated performance statistics for a given address. Note
752 * that this function can be called for addresses that are currently
753 * in use as well as addresses that are valid but not actively in use.
754 * Furthermore, the peer may not even be connected to us right now (in
755 * which case the call may be ignored or the information may be stored
756 * for later use). Update bandwidth assignments.
759 * @param peer identity of the peer
760 * @param valid_until how long is the address valid?
761 * @param plugin_name name of the transport plugin
762 * @param session session handle (if available)
763 * @param plugin_addr address (if available)
764 * @param plugin_addr_len number of bytes in plugin_addr
765 * @param ats performance data for the address
766 * @param ats_count number of performance records in 'ats'
769 GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc,
770 const struct GNUNET_PeerIdentity *peer,
771 struct GNUNET_TIME_Absolute valid_until,
772 const char *plugin_name,
773 struct Session *session,
774 const void *plugin_addr,
775 size_t plugin_addr_len,
776 const struct GNUNET_TRANSPORT_ATS_Information *ats,
779 struct AllocationRecord *ar;
780 struct UpdateSessionContext usc;
782 ar = create_allocation_record (plugin_name,
791 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
795 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
798 GNUNET_assert (GNUNET_OK ==
799 GNUNET_CONTAINER_multihashmap_put (atc->peers,
802 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
803 GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
809 /* end of file gnunet-service-transport_ats.c */