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 #include "gnunet_ats_service.h"
29 // NOTE: this implementation is simply supposed
30 // to implement a simplistic strategy in-process;
31 // in the future, we plan to replace it with a real
32 // service implementation
35 * Allocation record for a peer's address.
37 struct AllocationRecord
41 * Public key of the peer.
43 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
46 * Performance information associated with this address (array).
48 struct GNUNET_TRANSPORT_ATS_Information *ats;
56 * Address this record represents, allocated at the end of this struct.
58 const void *plugin_addr;
61 * Session associated with this record.
63 struct Session *session;
66 * Number of bytes in plugin_addr.
68 size_t plugin_addr_len;
71 * Number of entries in 'ats'.
76 * Bandwidth assigned to this address right now, 0 for none.
78 struct GNUNET_BANDWIDTH_Value32NBO bandwidth;
81 * Set to GNUNET_YES if this is the connected address of a connected peer.
89 * Opaque handle to stop incremental validation address callbacks.
91 struct GNUNET_ATS_SuggestionContext
95 * Function to call with our final suggestion.
97 GNUNET_ATS_AddressSuggestionCallback cb;
107 struct GNUNET_ATS_Handle *atc;
110 * Which peer are we monitoring?
112 struct GNUNET_PeerIdentity target;
118 * Handle to the ATS subsystem.
120 struct GNUNET_ATS_Handle
125 const struct GNUNET_CONFIGURATION_Handle *cfg;
128 * Function to call when the allocation changes.
130 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb;
133 * Closure for 'alloc_cb'.
138 * Information about all connected peers. Maps peer identities
139 * to one or more 'struct AllocationRecord' values.
141 struct GNUNET_CONTAINER_MultiHashMap *peers;
144 * Map of PeerIdentities to 'struct GNUNET_ATS_SuggestionContext's.
146 struct GNUNET_CONTAINER_MultiHashMap *notify_map;
150 * Task scheduled to update our bandwidth assignment.
152 GNUNET_SCHEDULER_TaskIdentifier ba_task;
155 * Total bandwidth per configuration.
157 unsigned long long total_bps;
162 * Count number of connected records.
164 * @param cls pointer to counter
165 * @param key identity of the peer associated with the records
166 * @param value a 'struct AllocationRecord'
167 * @return GNUNET_YES (continue iteration)
170 count_connections (void *cls,
171 const GNUNET_HashCode *key,
174 unsigned int *ac = cls;
175 struct AllocationRecord *ar = value;
177 if (GNUNET_YES == ar->connected)
182 struct SetBandwidthContext
184 struct GNUNET_ATS_Handle *atc;
185 struct GNUNET_BANDWIDTH_Value32NBO bw;
189 * Set bandwidth based on record.
191 * @param cls 'struct SetBandwidthContext'
192 * @param key identity of the peer associated with the records
193 * @param value a 'struct AllocationRecord'
194 * @return GNUNET_YES (continue iteration)
197 set_bw_connections (void *cls,
198 const GNUNET_HashCode *key,
201 struct SetBandwidthContext *sbc = cls;
202 struct AllocationRecord *ar = value;
204 if (GNUNET_YES == ar->connected)
206 ar->bandwidth = sbc->bw;
207 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
208 (const struct GNUNET_PeerIdentity*) key,
215 else if (ntohl(ar->bandwidth.value__) > 0)
217 ar->bandwidth = GNUNET_BANDWIDTH_value_init (0);
218 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
219 (const struct GNUNET_PeerIdentity*) key,
231 * Task run to update bandwidth assignments.
233 * @param cls the 'struct GNUNET_ATS_Handle'
234 * @param tc scheduler context
237 update_bandwidth_task (void *cls,
238 const struct GNUNET_SCHEDULER_TaskContext *tc)
240 struct GNUNET_ATS_Handle *atc = cls;
242 struct SetBandwidthContext bwc;
244 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
245 /* FIXME: update calculations NICELY; what follows is a naive version */
246 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
250 bwc.bw = GNUNET_BANDWIDTH_value_init (atc->total_bps / ac);
251 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
258 * Calculate an updated bandwidth assignment and notify.
261 * @param change which allocation record changed?
264 update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc,
265 struct AllocationRecord *change)
267 /* FIXME: based on the 'change', update the LP-problem... */
268 if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK)
269 atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task,
275 * Function called with feasbile addresses we might want to suggest.
277 * @param cls the 'struct GNUNET_ATS_SuggestionContext'
278 * @param key identity of the peer
279 * @param value a 'struct AllocationRecord' for the peer
280 * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet
283 suggest_address (void *cls,
284 const GNUNET_HashCode *key,
287 struct GNUNET_ATS_SuggestionContest *asc = cls;
288 struct AllocationRecord *ar = value;
296 * We would like to establish a new connection with a peer.
297 * ATS should suggest a good address to begin with.
300 * @param peer identity of the new peer
301 * @param cb function to call with the address
302 * @param cb_cls closure for cb
304 struct GNUNET_ATS_SuggestionContext *
305 GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
306 const struct GNUNET_PeerIdentity *peer,
307 GNUNET_ATS_AddressSuggestionCallback cb,
310 struct GNUNET_ATS_SuggestionContext *asc;
312 asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
314 asc->cb_cls = cb_cls;
317 GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
326 GNUNET_CONTAINER_multihashmap_put (atc->notify_map,
329 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
335 * Cancel suggestion request.
337 * @param asc handle of the request to cancel
340 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
342 GNUNET_assert (GNUNET_OK ==
343 GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
344 &asc->target.hashPubKey,
351 * Initialize the ATS subsystem.
353 * @param cfg configuration to use
354 * @param alloc_cb notification to call whenever the allocation changed
355 * @param alloc_cb_cls closure for 'alloc_cb'
356 * @return ats context
358 struct GNUNET_ATS_Handle *
359 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
360 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb,
363 struct GNUNET_ATS_Handle *atc;
365 atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_Handle));
367 atc->alloc_cb = alloc_cb;
368 atc->alloc_cb_cls = alloc_cb_cls;
369 atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
370 GNUNET_CONFIGURATION_get_value_number (cfg,
379 * Free an allocation record.
382 * @param key identity of the peer associated with the record
383 * @param value the 'struct AllocationRecord' to free
384 * @return GNUNET_OK (continue to iterate)
387 destroy_allocation_record (void *cls,
388 const GNUNET_HashCode *key,
391 struct AllocationRecord *ar = value;
393 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
394 GNUNET_free (ar->plugin_name);
401 * Shutdown the ATS subsystem.
406 GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc)
408 if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
410 GNUNET_SCHEDULER_cancel (atc->ba_task);
411 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
413 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
414 &destroy_allocation_record,
416 GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
417 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
418 GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
419 atc->notify_map = NULL;
425 * Closure for 'update_session'
427 struct UpdateSessionContext
432 struct GNUNET_ATS_Handle *atc;
435 * Allocation record with new information.
437 struct AllocationRecord *arnew;
442 * Update an allocation record, merging with the new information
444 * @param cls a new 'struct AllocationRecord'
445 * @param key identity of the peer associated with the records
446 * @param value the old 'struct AllocationRecord'
447 * @return GNUNET_YES if the records do not match,
448 * GNUNET_NO if the record do match and 'old' was updated
451 update_session (void *cls,
452 const GNUNET_HashCode *key,
455 struct UpdateSessionContext *usc = cls;
456 struct AllocationRecord *arnew = usc->arnew;
457 struct AllocationRecord *arold = value;
460 if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
462 if ( (arnew->session == arold->session) ||
463 ( (arold->session == NULL) &&
464 (arold->plugin_addr_len == arnew->plugin_addr_len) &&
465 (0 == memcmp (arold->plugin_addr,
467 arnew->plugin_addr_len)) ) )
471 if (arnew->session != arold->session)
473 arold->session = arnew->session;
476 if ( (arnew->connected == GNUNET_YES) &&
477 (arold->connected == GNUNET_NO) )
479 arold->connected = GNUNET_YES;
482 // FIXME: merge ats arrays of (arold, arnew);
484 if (GNUNET_YES == change)
485 update_bandwidth_assignment (usc->atc, arold);
493 * Create an allocation record with the given properties.
495 * @param plugin_name name of the currently used transport plugin
496 * @param session session in use (if available)
497 * @param plugin_addr address in use (if available)
498 * @param plugin_addr_len number of bytes in plugin_addr
499 * @param ats performance data for the connection
500 * @param ats_count number of performance records in 'ats'
502 static struct AllocationRecord *
503 create_allocation_record (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
504 const char *plugin_name,
505 struct Session *session,
506 const void *plugin_addr,
507 size_t plugin_addr_len,
508 const struct GNUNET_TRANSPORT_ATS_Information *ats,
511 struct AllocationRecord *ar;
513 ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
514 ar->public_key = *public_key;
515 ar->plugin_name = GNUNET_strdup (plugin_name);
516 ar->plugin_addr = &ar[1];
517 memcpy (&ar[1], plugin_addr, plugin_addr_len);
518 ar->session = session;
519 ar->plugin_addr_len = plugin_addr_len;
520 GNUNET_array_grow (ar->ats,
525 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
531 * Mark all matching allocation records as not connected.
533 * @param cls 'struct GTS_AtsHandle'
534 * @param key identity of the peer associated with the record
535 * @param value the 'struct AllocationRecord' to clear the 'connected' flag
536 * @return GNUNET_OK (continue to iterate)
539 disconnect_peer (void *cls,
540 const GNUNET_HashCode *key,
543 struct GNUNET_ATS_Handle *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 public_key public key of the 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_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
573 const struct GNUNET_PeerIdentity *peer,
574 const char *plugin_name,
575 struct Session *session,
576 const void *plugin_addr,
577 size_t plugin_addr_len,
578 const struct GNUNET_TRANSPORT_ATS_Information *ats,
581 struct AllocationRecord *ar;
582 struct UpdateSessionContext usc;
584 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
587 ar = create_allocation_record (public_key,
594 ar->connected = GNUNET_YES;
598 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
602 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
605 GNUNET_assert (GNUNET_OK ==
606 GNUNET_CONTAINER_multihashmap_put (atc->peers,
609 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
614 * We disconnected from the given peer (for example, because ats, core
615 * or blacklist asked for it or because the other peer disconnected).
616 * Calculate bandwidth assignments without the peer.
619 * @param peer identity of the new peer
622 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc,
623 const struct GNUNET_PeerIdentity *peer)
625 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
633 * Closure for 'destroy_allocation_record'
635 struct SessionDestroyContext
640 struct GNUNET_ATS_Handle *atc;
643 * Session being destroyed.
645 const struct Session *session;
650 * Free an allocation record matching the given session.
652 * @param cls the 'struct SessionDestroyContext'
653 * @param key identity of the peer associated with the record
654 * @param value the 'struct AllocationRecord' to free
655 * @return GNUNET_OK (continue to iterate)
658 destroy_session (void *cls,
659 const GNUNET_HashCode *key,
662 struct SessionDestroyContext *sdc = cls;
663 struct AllocationRecord *ar = value;
665 if (ar->session != sdc->session)
668 if (ar->plugin_addr != NULL)
670 GNUNET_assert (GNUNET_OK ==
671 GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers,
674 if (GNUNET_YES == ar->connected);
676 /* FIXME: is this supposed to be allowed? What to do then? */
679 destroy_allocation_record (NULL, key, ar);
685 * A session got destroyed, stop including it as a valid address.
688 * @param peer identity of the peer
689 * @param session session handle that is no longer valid
692 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc,
693 const struct GNUNET_PeerIdentity *peer,
694 const struct Session *session)
696 struct SessionDestroyContext sdc;
699 sdc.session = session;
700 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
707 * Notify validation watcher that an entry is now valid
709 * @param cls 'struct ValidationEntry' that is now valid
710 * @param key peer identity (unused)
711 * @param value a 'GST_ValidationIteratorContext' to notify
712 * @return GNUNET_YES (continue to iterate)
715 notify_valid (void *cls,
716 const GNUNET_HashCode *key,
719 struct AllocationRecord *ar = cls;
720 struct GNUNET_ATS_SuggestionContext *asc = value;
722 asc->cb (asc->cb_cls,
728 ar->ats, ar->ats_count);
734 * We have updated performance statistics for a given address. Note
735 * that this function can be called for addresses that are currently
736 * in use as well as addresses that are valid but not actively in use.
737 * Furthermore, the peer may not even be connected to us right now (in
738 * which case the call may be ignored or the information may be stored
739 * for later use). Update bandwidth assignments.
742 * @param public_key public key of the peer
743 * @param peer identity of the peer
744 * @param plugin_name name of the transport plugin
745 * @param session session handle (if available)
746 * @param plugin_addr address (if available)
747 * @param plugin_addr_len number of bytes in plugin_addr
748 * @param ats performance data for the address
749 * @param ats_count number of performance records in 'ats'
752 GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc,
753 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
754 const struct GNUNET_PeerIdentity *peer,
755 const char *plugin_name,
756 struct Session *session,
757 const void *plugin_addr,
758 size_t plugin_addr_len,
759 const struct GNUNET_TRANSPORT_ATS_Information *ats,
762 struct AllocationRecord *ar;
763 struct UpdateSessionContext usc;
765 ar = create_allocation_record (public_key,
775 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
779 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
782 GNUNET_assert (GNUNET_OK ==
783 GNUNET_CONTAINER_multihashmap_put (atc->peers,
786 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
787 GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
793 /* end of file gnunet-service-transport_ats.c */