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,
290 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), ar->ats,
298 * We would like to establish a new connection with a peer.
299 * ATS should suggest a good address to begin with.
302 * @param peer identity of the new peer
303 * @param cb function to call with the address
304 * @param cb_cls closure for cb
306 struct GNUNET_ATS_SuggestionContext *
307 GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
308 const struct GNUNET_PeerIdentity *peer,
309 GNUNET_ATS_AddressSuggestionCallback cb,
312 struct GNUNET_ATS_SuggestionContext *asc;
314 asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
316 asc->cb_cls = cb_cls;
319 GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers, &peer->hashPubKey,
320 &suggest_address, asc);
326 GNUNET_CONTAINER_multihashmap_put (atc->notify_map, &peer->hashPubKey, asc,
327 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
333 * Cancel suggestion request.
335 * @param asc handle of the request to cancel
338 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
340 GNUNET_assert (GNUNET_OK ==
341 GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
342 &asc->target.hashPubKey,
349 * Initialize the ATS subsystem.
351 * @param cfg configuration to use
352 * @param alloc_cb notification to call whenever the allocation changed
353 * @param alloc_cb_cls closure for 'alloc_cb'
354 * @return ats context
356 struct GNUNET_ATS_Handle *
357 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
358 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb,
361 struct GNUNET_ATS_Handle *atc;
363 atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_Handle));
365 atc->alloc_cb = alloc_cb;
366 atc->alloc_cb_cls = alloc_cb_cls;
367 atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
368 atc->notify_map = GNUNET_CONTAINER_multihashmap_create (256);
369 GNUNET_CONFIGURATION_get_value_number (cfg, "core", "TOTAL_QUOTA_OUT",
376 * Free an allocation record.
379 * @param key identity of the peer associated with the record
380 * @param value the 'struct AllocationRecord' to free
381 * @return GNUNET_OK (continue to iterate)
384 destroy_allocation_record (void *cls, const GNUNET_HashCode * key, void *value)
386 struct AllocationRecord *ar = value;
388 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
389 GNUNET_free (ar->plugin_name);
396 * Shutdown the ATS subsystem.
401 GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc)
403 if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
405 GNUNET_SCHEDULER_cancel (atc->ba_task);
406 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
408 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_allocation_record,
410 GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
411 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
412 GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
413 atc->notify_map = NULL;
419 * Closure for 'update_session'
421 struct UpdateSessionContext
426 struct GNUNET_ATS_Handle *atc;
429 * Allocation record with new information.
431 struct AllocationRecord *arnew;
436 * Update an allocation record, merging with the new information
438 * @param cls a new 'struct AllocationRecord'
439 * @param key identity of the peer associated with the records
440 * @param value the old 'struct AllocationRecord'
441 * @return GNUNET_YES if the records do not match,
442 * GNUNET_NO if the record do match and 'old' was updated
445 update_session (void *cls, const GNUNET_HashCode * key, void *value)
447 struct UpdateSessionContext *usc = cls;
448 struct AllocationRecord *arnew = usc->arnew;
449 struct AllocationRecord *arold = value;
452 if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
454 if ((arnew->session == arold->session) ||
455 ((arold->session == NULL) &&
456 (arold->plugin_addr_len == arnew->plugin_addr_len) &&
458 memcmp (arold->plugin_addr, arnew->plugin_addr,
459 arnew->plugin_addr_len))))
463 if (arnew->session != arold->session)
465 arold->session = arnew->session;
468 if ((arnew->connected == GNUNET_YES) && (arold->connected == GNUNET_NO))
470 arold->connected = GNUNET_YES;
473 // FIXME: merge ats arrays of (arold, arnew);
475 if (GNUNET_YES == change)
476 update_bandwidth_assignment (usc->atc, arold);
484 * Create an allocation record with the given properties.
486 * @param plugin_name name of the currently used transport plugin
487 * @param session session in use (if available)
488 * @param plugin_addr address in use (if available)
489 * @param plugin_addr_len number of bytes in plugin_addr
490 * @param ats performance data for the connection
491 * @param ats_count number of performance records in 'ats'
493 static struct AllocationRecord *
494 create_allocation_record (const char *plugin_name, struct Session *session,
495 const void *plugin_addr, size_t plugin_addr_len,
496 const struct GNUNET_TRANSPORT_ATS_Information *ats,
499 struct AllocationRecord *ar;
501 ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
502 ar->plugin_name = GNUNET_strdup (plugin_name);
503 ar->plugin_addr = &ar[1];
504 memcpy (&ar[1], plugin_addr, plugin_addr_len);
505 ar->session = session;
506 ar->plugin_addr_len = plugin_addr_len;
507 GNUNET_array_grow (ar->ats, ar->ats_count, ats_count);
508 memcpy (ar->ats, ats,
509 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
515 * Mark all matching allocation records as not connected.
517 * @param cls 'struct GTS_AtsHandle'
518 * @param key identity of the peer associated with the record
519 * @param value the 'struct AllocationRecord' to clear the 'connected' flag
520 * @return GNUNET_OK (continue to iterate)
523 disconnect_peer (void *cls, const GNUNET_HashCode * key, void *value)
525 struct GNUNET_ATS_Handle *atc = cls;
526 struct AllocationRecord *ar = value;
528 if (GNUNET_YES == ar->connected)
530 ar->connected = GNUNET_NO;
531 update_bandwidth_assignment (atc, ar);
538 * We established a new connection with a peer (for example, because
539 * core asked for it or because the other peer connected to us).
540 * Calculate bandwidth assignments including the new peer.
543 * @param peer identity of the new peer
544 * @param plugin_name name of the currently used transport plugin
545 * @param session session in use (if available)
546 * @param plugin_addr address in use (if available)
547 * @param plugin_addr_len number of bytes in plugin_addr
548 * @param ats performance data for the connection
549 * @param ats_count number of performance records in 'ats'
552 GNUNET_ATS_peer_connect (struct GNUNET_ATS_Handle *atc,
553 const struct GNUNET_PeerIdentity *peer,
554 const char *plugin_name, struct Session *session,
555 const void *plugin_addr, size_t plugin_addr_len,
556 const struct GNUNET_TRANSPORT_ATS_Information *ats,
559 struct AllocationRecord *ar;
560 struct UpdateSessionContext usc;
562 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &disconnect_peer,
564 ar = create_allocation_record (plugin_name, session, plugin_addr,
565 plugin_addr_len, ats, ats_count);
566 ar->connected = GNUNET_YES;
570 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
572 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
575 GNUNET_assert (GNUNET_OK ==
576 GNUNET_CONTAINER_multihashmap_put (atc->peers,
577 &peer->hashPubKey, ar,
578 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
583 * We disconnected from the given peer (for example, because ats, core
584 * or blacklist asked for it or because the other peer disconnected).
585 * Calculate bandwidth assignments without the peer.
588 * @param peer identity of the new peer
591 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc,
592 const struct GNUNET_PeerIdentity *peer)
594 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
596 &disconnect_peer, atc);
601 * Closure for 'destroy_allocation_record'
603 struct SessionDestroyContext
608 struct GNUNET_ATS_Handle *atc;
611 * Session being destroyed.
613 const struct Session *session;
618 * Free an allocation record matching the given session.
620 * @param cls the 'struct SessionDestroyContext'
621 * @param key identity of the peer associated with the record
622 * @param value the 'struct AllocationRecord' to free
623 * @return GNUNET_OK (continue to iterate)
626 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
628 struct SessionDestroyContext *sdc = cls;
629 struct AllocationRecord *ar = value;
631 if (ar->session != sdc->session)
634 if (ar->plugin_addr != NULL)
636 GNUNET_assert (GNUNET_OK ==
637 GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers, key,
639 if (GNUNET_YES == ar->connected) ;
641 /* FIXME: is this supposed to be allowed? What to do then? */
644 destroy_allocation_record (NULL, key, ar);
650 * A session got destroyed, stop including it as a valid address.
653 * @param peer identity of the peer
654 * @param session session handle that is no longer valid
657 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc,
658 const struct GNUNET_PeerIdentity *peer,
659 const struct Session *session)
661 struct SessionDestroyContext sdc;
664 sdc.session = session;
665 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_session,
671 * Notify validation watcher that an entry is now valid
673 * @param cls 'struct ValidationEntry' that is now valid
674 * @param key peer identity (unused)
675 * @param value a 'GST_ValidationIteratorContext' to notify
676 * @return GNUNET_YES (continue to iterate)
679 notify_valid (void *cls, const GNUNET_HashCode * key, void *value)
681 struct AllocationRecord *ar = cls;
682 struct GNUNET_ATS_SuggestionContext *asc = value;
684 asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
687 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), ar->ats,
689 GNUNET_ATS_suggest_address_cancel (asc);
695 * We have updated performance statistics for a given address. Note
696 * that this function can be called for addresses that are currently
697 * in use as well as addresses that are valid but not actively in use.
698 * Furthermore, the peer may not even be connected to us right now (in
699 * which case the call may be ignored or the information may be stored
700 * for later use). Update bandwidth assignments.
703 * @param peer identity of the peer
704 * @param valid_until how long is the address valid?
705 * @param plugin_name name of the transport plugin
706 * @param session session handle (if available)
707 * @param plugin_addr address (if available)
708 * @param plugin_addr_len number of bytes in plugin_addr
709 * @param ats performance data for the address
710 * @param ats_count number of performance records in 'ats'
713 GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc,
714 const struct GNUNET_PeerIdentity *peer,
715 struct GNUNET_TIME_Absolute valid_until,
716 const char *plugin_name, struct Session *session,
717 const void *plugin_addr, size_t plugin_addr_len,
718 const struct GNUNET_TRANSPORT_ATS_Information *ats,
721 struct AllocationRecord *ar;
722 struct UpdateSessionContext usc;
724 ar = create_allocation_record (plugin_name, session, plugin_addr,
725 plugin_addr_len, ats, ats_count);
729 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
731 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
734 GNUNET_assert (GNUNET_OK ==
735 GNUNET_CONTAINER_multihashmap_put (atc->peers,
736 &peer->hashPubKey, ar,
737 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
738 GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
739 &peer->hashPubKey, ¬ify_valid,
743 /* end of file gnunet-service-transport_ats.c */