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"
36 // NOTE: this implementation is simply supposed
37 // to implement a simplistic strategy in-process;
38 // in the future, we plan to replace it with a real
39 // service implementation
42 * Allocation record for a peer's address.
44 struct AllocationRecord
48 * Performance information associated with this address (array).
50 struct GNUNET_TRANSPORT_ATS_Information *ats;
58 * Address this record represents, allocated at the end of this struct.
60 const void *plugin_addr;
63 * Session associated with this record.
65 struct Session *session;
68 * Number of bytes in plugin_addr.
70 size_t plugin_addr_len;
73 * Number of entries in 'ats'.
78 * Bandwidth assigned to this address right now, 0 for none.
80 struct GNUNET_BANDWIDTH_Value32NBO bandwidth;
83 * Set to GNUNET_YES if this is the connected address of a connected peer.
91 * Opaque handle to obtain address suggestions.
93 struct GNUNET_ATS_SuggestionContext
97 * Function to call with our final suggestion.
99 GNUNET_ATS_AddressSuggestionCallback cb;
109 struct GNUNET_ATS_Handle *atc;
112 * Which peer are we monitoring?
114 struct GNUNET_PeerIdentity target;
120 * Handle to the ATS subsystem.
122 struct GNUNET_ATS_Handle
127 const struct GNUNET_CONFIGURATION_Handle *cfg;
130 * Function to call when the allocation changes.
132 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb;
135 * Closure for 'alloc_cb'.
140 * Information about all connected peers. Maps peer identities
141 * to one or more 'struct AllocationRecord' values.
143 struct GNUNET_CONTAINER_MultiHashMap *peers;
146 * Map of PeerIdentities to 'struct GNUNET_ATS_SuggestionContext's.
148 struct GNUNET_CONTAINER_MultiHashMap *notify_map;
152 * Task scheduled to update our bandwidth assignment.
154 GNUNET_SCHEDULER_TaskIdentifier ba_task;
157 * Total bandwidth per configuration.
159 unsigned long long total_bps;
164 * Count number of connected records.
166 * @param cls pointer to counter
167 * @param key identity of the peer associated with the records
168 * @param value a 'struct AllocationRecord'
169 * @return GNUNET_YES (continue iteration)
172 count_connections (void *cls, const GNUNET_HashCode * key, void *value)
174 unsigned int *ac = cls;
175 struct AllocationRecord *ar = value;
177 if (GNUNET_YES == ar->connected)
184 * Closure for 'set_bw_connections'.
186 struct SetBandwidthContext
191 struct GNUNET_ATS_Handle *atc;
194 * Bandwidth to assign.
196 struct GNUNET_BANDWIDTH_Value32NBO bw;
201 * Set bandwidth based on record.
203 * @param cls 'struct SetBandwidthContext'
204 * @param key identity of the peer associated with the records
205 * @param value a 'struct AllocationRecord'
206 * @return GNUNET_YES (continue iteration)
209 set_bw_connections (void *cls, const GNUNET_HashCode * key, void *value)
211 struct SetBandwidthContext *sbc = cls;
212 struct AllocationRecord *ar = value;
214 if (GNUNET_YES == ar->connected)
216 ar->bandwidth = sbc->bw;
217 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
218 (const struct GNUNET_PeerIdentity *) key,
219 ar->plugin_name, ar->session, ar->plugin_addr,
220 ar->plugin_addr_len, ar->bandwidth);
222 else if (ntohl (ar->bandwidth.value__) > 0)
224 ar->bandwidth = GNUNET_BANDWIDTH_value_init (0);
225 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
226 (const struct GNUNET_PeerIdentity *) key,
227 ar->plugin_name, ar->session, ar->plugin_addr,
228 ar->plugin_addr_len, ar->bandwidth);
235 * Task run to update bandwidth assignments.
237 * @param cls the 'struct GNUNET_ATS_Handle'
238 * @param tc scheduler context
241 update_bandwidth_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
243 struct GNUNET_ATS_Handle *atc = cls;
245 struct SetBandwidthContext bwc;
247 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
248 /* FIXME: update calculations NICELY; what follows is a naive version */
249 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &count_connections, &ac);
251 bwc.bw = GNUNET_BANDWIDTH_value_init (atc->total_bps / ac);
252 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &set_bw_connections, &bwc);
257 * Calculate an updated bandwidth assignment and notify.
260 * @param change which allocation record changed?
263 update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc,
264 struct AllocationRecord *change)
266 /* FIXME: based on the 'change', update the LP-problem... */
267 if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK)
268 atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task, atc);
273 * Function called with feasbile addresses we might want to suggest.
275 * @param cls the 'struct GNUNET_ATS_SuggestionContext'
276 * @param key identity of the peer
277 * @param value a 'struct AllocationRecord' for the peer
278 * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet
281 suggest_address (void *cls, const GNUNET_HashCode * key, void *value)
283 struct GNUNET_ATS_SuggestionContext *asc = cls;
284 struct AllocationRecord *ar = value;
286 /* trivial strategy: pick first available address... */
287 asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
289 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), ar->ats,
297 * We would like to establish a new connection with a peer.
298 * ATS should suggest a good address to begin with.
301 * @param peer identity of the new peer
302 * @param cb function to call with the address
303 * @param cb_cls closure for cb
305 struct GNUNET_ATS_SuggestionContext *
306 GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
307 const struct GNUNET_PeerIdentity *peer,
308 GNUNET_ATS_AddressSuggestionCallback cb,
311 struct GNUNET_ATS_SuggestionContext *asc;
313 asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
315 asc->cb_cls = cb_cls;
318 GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers, &peer->hashPubKey,
319 &suggest_address, asc);
325 GNUNET_CONTAINER_multihashmap_put (atc->notify_map, &peer->hashPubKey, asc,
326 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
332 * Cancel suggestion request.
334 * @param asc handle of the request to cancel
337 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
339 GNUNET_assert (GNUNET_OK ==
340 GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
341 &asc->target.hashPubKey,
348 * Initialize the ATS subsystem.
350 * @param cfg configuration to use
351 * @param alloc_cb notification to call whenever the allocation changed
352 * @param alloc_cb_cls closure for 'alloc_cb'
353 * @return ats context
355 struct GNUNET_ATS_Handle *
356 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
357 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb,
360 struct GNUNET_ATS_Handle *atc;
362 atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_Handle));
364 atc->alloc_cb = alloc_cb;
365 atc->alloc_cb_cls = alloc_cb_cls;
366 atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
367 GNUNET_CONFIGURATION_get_value_number (cfg, "core", "TOTAL_QUOTA_OUT",
374 * Free an allocation record.
377 * @param key identity of the peer associated with the record
378 * @param value the 'struct AllocationRecord' to free
379 * @return GNUNET_OK (continue to iterate)
382 destroy_allocation_record (void *cls, const GNUNET_HashCode * key, void *value)
384 struct AllocationRecord *ar = value;
386 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
387 GNUNET_free (ar->plugin_name);
394 * Shutdown the ATS subsystem.
399 GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc)
401 if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
403 GNUNET_SCHEDULER_cancel (atc->ba_task);
404 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
406 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_allocation_record,
408 GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
409 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
410 GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
411 atc->notify_map = NULL;
417 * Closure for 'update_session'
419 struct UpdateSessionContext
424 struct GNUNET_ATS_Handle *atc;
427 * Allocation record with new information.
429 struct AllocationRecord *arnew;
434 * Update an allocation record, merging with the new information
436 * @param cls a new 'struct AllocationRecord'
437 * @param key identity of the peer associated with the records
438 * @param value the old 'struct AllocationRecord'
439 * @return GNUNET_YES if the records do not match,
440 * GNUNET_NO if the record do match and 'old' was updated
443 update_session (void *cls, const GNUNET_HashCode * key, void *value)
445 struct UpdateSessionContext *usc = cls;
446 struct AllocationRecord *arnew = usc->arnew;
447 struct AllocationRecord *arold = value;
450 if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
452 if ((arnew->session == arold->session) ||
453 ((arold->session == NULL) &&
454 (arold->plugin_addr_len == arnew->plugin_addr_len) &&
456 memcmp (arold->plugin_addr, arnew->plugin_addr,
457 arnew->plugin_addr_len))))
461 if (arnew->session != arold->session)
463 arold->session = arnew->session;
466 if ((arnew->connected == GNUNET_YES) && (arold->connected == GNUNET_NO))
468 arold->connected = GNUNET_YES;
471 // FIXME: merge ats arrays of (arold, arnew);
473 if (GNUNET_YES == change)
474 update_bandwidth_assignment (usc->atc, arold);
482 * Create an allocation record with the given properties.
484 * @param plugin_name name of the currently used transport plugin
485 * @param session session in use (if available)
486 * @param plugin_addr address in use (if available)
487 * @param plugin_addr_len number of bytes in plugin_addr
488 * @param ats performance data for the connection
489 * @param ats_count number of performance records in 'ats'
491 static struct AllocationRecord *
492 create_allocation_record (const char *plugin_name, struct Session *session,
493 const void *plugin_addr, size_t plugin_addr_len,
494 const struct GNUNET_TRANSPORT_ATS_Information *ats,
497 struct AllocationRecord *ar;
499 ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
500 ar->plugin_name = GNUNET_strdup (plugin_name);
501 ar->plugin_addr = &ar[1];
502 memcpy (&ar[1], plugin_addr, plugin_addr_len);
503 ar->session = session;
504 ar->plugin_addr_len = plugin_addr_len;
505 GNUNET_array_grow (ar->ats, ar->ats_count, ats_count);
506 memcpy (ar->ats, ats,
507 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
513 * Mark all matching allocation records as not connected.
515 * @param cls 'struct GTS_AtsHandle'
516 * @param key identity of the peer associated with the record
517 * @param value the 'struct AllocationRecord' to clear the 'connected' flag
518 * @return GNUNET_OK (continue to iterate)
521 disconnect_peer (void *cls, const GNUNET_HashCode * key, void *value)
523 struct GNUNET_ATS_Handle *atc = cls;
524 struct AllocationRecord *ar = value;
526 if (GNUNET_YES == ar->connected)
528 ar->connected = GNUNET_NO;
529 update_bandwidth_assignment (atc, ar);
536 * We established a new connection with a peer (for example, because
537 * core asked for it or because the other peer connected to us).
538 * Calculate bandwidth assignments including the new peer.
541 * @param peer identity of the new peer
542 * @param plugin_name name of the currently used transport plugin
543 * @param session session in use (if available)
544 * @param plugin_addr address in use (if available)
545 * @param plugin_addr_len number of bytes in plugin_addr
546 * @param ats performance data for the connection
547 * @param ats_count number of performance records in 'ats'
550 GNUNET_ATS_peer_connect (struct GNUNET_ATS_Handle *atc,
551 const struct GNUNET_PeerIdentity *peer,
552 const char *plugin_name, struct Session *session,
553 const void *plugin_addr, size_t plugin_addr_len,
554 const struct GNUNET_TRANSPORT_ATS_Information *ats,
557 struct AllocationRecord *ar;
558 struct UpdateSessionContext usc;
560 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &disconnect_peer,
562 ar = create_allocation_record (plugin_name, session, plugin_addr,
563 plugin_addr_len, ats, ats_count);
564 ar->connected = GNUNET_YES;
568 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
570 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
573 GNUNET_assert (GNUNET_OK ==
574 GNUNET_CONTAINER_multihashmap_put (atc->peers,
575 &peer->hashPubKey, ar,
576 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
581 * We disconnected from the given peer (for example, because ats, core
582 * or blacklist asked for it or because the other peer disconnected).
583 * Calculate bandwidth assignments without the peer.
586 * @param peer identity of the new peer
589 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc,
590 const struct GNUNET_PeerIdentity *peer)
592 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
594 &disconnect_peer, atc);
599 * Closure for 'destroy_allocation_record'
601 struct SessionDestroyContext
606 struct GNUNET_ATS_Handle *atc;
609 * Session being destroyed.
611 const struct Session *session;
616 * Free an allocation record matching the given session.
618 * @param cls the 'struct SessionDestroyContext'
619 * @param key identity of the peer associated with the record
620 * @param value the 'struct AllocationRecord' to free
621 * @return GNUNET_OK (continue to iterate)
624 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
626 struct SessionDestroyContext *sdc = cls;
627 struct AllocationRecord *ar = value;
629 if (ar->session != sdc->session)
632 if (ar->plugin_addr != NULL)
634 GNUNET_assert (GNUNET_OK ==
635 GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers, key,
637 if (GNUNET_YES == ar->connected) ;
639 /* FIXME: is this supposed to be allowed? What to do then? */
642 destroy_allocation_record (NULL, key, ar);
648 * A session got destroyed, stop including it as a valid address.
651 * @param peer identity of the peer
652 * @param session session handle that is no longer valid
655 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc,
656 const struct GNUNET_PeerIdentity *peer,
657 const struct Session *session)
659 struct SessionDestroyContext sdc;
662 sdc.session = session;
663 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_session,
669 * Notify validation watcher that an entry is now valid
671 * @param cls 'struct ValidationEntry' that is now valid
672 * @param key peer identity (unused)
673 * @param value a 'GST_ValidationIteratorContext' to notify
674 * @return GNUNET_YES (continue to iterate)
677 notify_valid (void *cls, const GNUNET_HashCode * key, void *value)
679 struct AllocationRecord *ar = cls;
680 struct GNUNET_ATS_SuggestionContext *asc = value;
682 asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
684 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), ar->ats,
686 GNUNET_ATS_suggest_address_cancel (asc);
692 * We have updated performance statistics for a given address. Note
693 * that this function can be called for addresses that are currently
694 * in use as well as addresses that are valid but not actively in use.
695 * Furthermore, the peer may not even be connected to us right now (in
696 * which case the call may be ignored or the information may be stored
697 * for later use). Update bandwidth assignments.
700 * @param peer identity of the peer
701 * @param valid_until how long is the address valid?
702 * @param plugin_name name of the transport plugin
703 * @param session session handle (if available)
704 * @param plugin_addr address (if available)
705 * @param plugin_addr_len number of bytes in plugin_addr
706 * @param ats performance data for the address
707 * @param ats_count number of performance records in 'ats'
710 GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc,
711 const struct GNUNET_PeerIdentity *peer,
712 struct GNUNET_TIME_Absolute valid_until,
713 const char *plugin_name, struct Session *session,
714 const void *plugin_addr, size_t plugin_addr_len,
715 const struct GNUNET_TRANSPORT_ATS_Information *ats,
718 struct AllocationRecord *ar;
719 struct UpdateSessionContext usc;
721 ar = create_allocation_record (plugin_name, session, plugin_addr,
722 plugin_addr_len, ats, ats_count);
726 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
728 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
731 GNUNET_assert (GNUNET_OK ==
732 GNUNET_CONTAINER_multihashmap_put (atc->peers,
733 &peer->hashPubKey, ar,
734 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
735 GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
736 &peer->hashPubKey, ¬ify_valid,
740 /* end of file gnunet-service-transport_ats.c */