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)
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,
210 const GNUNET_HashCode *key,
213 struct SetBandwidthContext *sbc = cls;
214 struct AllocationRecord *ar = value;
216 if (GNUNET_YES == ar->connected)
218 ar->bandwidth = sbc->bw;
219 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
220 (const struct GNUNET_PeerIdentity*) key,
227 else if (ntohl(ar->bandwidth.value__) > 0)
229 ar->bandwidth = GNUNET_BANDWIDTH_value_init (0);
230 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
231 (const struct GNUNET_PeerIdentity*) key,
243 * Task run to update bandwidth assignments.
245 * @param cls the 'struct GNUNET_ATS_Handle'
246 * @param tc scheduler context
249 update_bandwidth_task (void *cls,
250 const struct GNUNET_SCHEDULER_TaskContext *tc)
252 struct GNUNET_ATS_Handle *atc = cls;
254 struct SetBandwidthContext bwc;
256 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
257 /* FIXME: update calculations NICELY; what follows is a naive version */
258 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
262 bwc.bw = GNUNET_BANDWIDTH_value_init (atc->total_bps / ac);
263 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
270 * Calculate an updated bandwidth assignment and notify.
273 * @param change which allocation record changed?
276 update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc,
277 struct AllocationRecord *change)
279 /* FIXME: based on the 'change', update the LP-problem... */
280 if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK)
281 atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task,
287 * Function called with feasbile addresses we might want to suggest.
289 * @param cls the 'struct GNUNET_ATS_SuggestionContext'
290 * @param key identity of the peer
291 * @param value a 'struct AllocationRecord' for the peer
292 * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet
295 suggest_address (void *cls,
296 const GNUNET_HashCode *key,
299 struct GNUNET_ATS_SuggestionContest *asc = cls;
300 struct AllocationRecord *ar = value;
308 * We would like to establish a new connection with a peer.
309 * ATS should suggest a good address to begin with.
312 * @param peer identity of the new peer
313 * @param cb function to call with the address
314 * @param cb_cls closure for cb
316 struct GNUNET_ATS_SuggestionContext *
317 GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
318 const struct GNUNET_PeerIdentity *peer,
319 GNUNET_ATS_AddressSuggestionCallback cb,
322 struct GNUNET_ATS_SuggestionContext *asc;
324 asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
326 asc->cb_cls = cb_cls;
329 GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
338 GNUNET_CONTAINER_multihashmap_put (atc->notify_map,
341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
347 * Cancel suggestion request.
349 * @param asc handle of the request to cancel
352 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
354 GNUNET_assert (GNUNET_OK ==
355 GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
356 &asc->target.hashPubKey,
363 * Initialize the ATS subsystem.
365 * @param cfg configuration to use
366 * @param alloc_cb notification to call whenever the allocation changed
367 * @param alloc_cb_cls closure for 'alloc_cb'
368 * @return ats context
370 struct GNUNET_ATS_Handle *
371 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
372 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb,
375 struct GNUNET_ATS_Handle *atc;
377 atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_Handle));
379 atc->alloc_cb = alloc_cb;
380 atc->alloc_cb_cls = alloc_cb_cls;
381 atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
382 GNUNET_CONFIGURATION_get_value_number (cfg,
391 * Free an allocation record.
394 * @param key identity of the peer associated with the record
395 * @param value the 'struct AllocationRecord' to free
396 * @return GNUNET_OK (continue to iterate)
399 destroy_allocation_record (void *cls,
400 const GNUNET_HashCode *key,
403 struct AllocationRecord *ar = value;
405 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
406 GNUNET_free (ar->plugin_name);
413 * Shutdown the ATS subsystem.
418 GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc)
420 if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
422 GNUNET_SCHEDULER_cancel (atc->ba_task);
423 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
425 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
426 &destroy_allocation_record,
428 GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
429 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
430 GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
431 atc->notify_map = NULL;
437 * Closure for 'update_session'
439 struct UpdateSessionContext
444 struct GNUNET_ATS_Handle *atc;
447 * Allocation record with new information.
449 struct AllocationRecord *arnew;
454 * Update an allocation record, merging with the new information
456 * @param cls a new 'struct AllocationRecord'
457 * @param key identity of the peer associated with the records
458 * @param value the old 'struct AllocationRecord'
459 * @return GNUNET_YES if the records do not match,
460 * GNUNET_NO if the record do match and 'old' was updated
463 update_session (void *cls,
464 const GNUNET_HashCode *key,
467 struct UpdateSessionContext *usc = cls;
468 struct AllocationRecord *arnew = usc->arnew;
469 struct AllocationRecord *arold = value;
472 if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
474 if ( (arnew->session == arold->session) ||
475 ( (arold->session == NULL) &&
476 (arold->plugin_addr_len == arnew->plugin_addr_len) &&
477 (0 == memcmp (arold->plugin_addr,
479 arnew->plugin_addr_len)) ) )
483 if (arnew->session != arold->session)
485 arold->session = arnew->session;
488 if ( (arnew->connected == GNUNET_YES) &&
489 (arold->connected == GNUNET_NO) )
491 arold->connected = GNUNET_YES;
494 // FIXME: merge ats arrays of (arold, arnew);
496 if (GNUNET_YES == change)
497 update_bandwidth_assignment (usc->atc, arold);
505 * Create an allocation record with the given properties.
507 * @param plugin_name name of the currently used transport plugin
508 * @param session session in use (if available)
509 * @param plugin_addr address in use (if available)
510 * @param plugin_addr_len number of bytes in plugin_addr
511 * @param ats performance data for the connection
512 * @param ats_count number of performance records in 'ats'
514 static struct AllocationRecord *
515 create_allocation_record (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
516 const char *plugin_name,
517 struct Session *session,
518 const void *plugin_addr,
519 size_t plugin_addr_len,
520 const struct GNUNET_TRANSPORT_ATS_Information *ats,
523 struct AllocationRecord *ar;
525 ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
526 ar->public_key = *public_key;
527 ar->plugin_name = GNUNET_strdup (plugin_name);
528 ar->plugin_addr = &ar[1];
529 memcpy (&ar[1], plugin_addr, plugin_addr_len);
530 ar->session = session;
531 ar->plugin_addr_len = plugin_addr_len;
532 GNUNET_array_grow (ar->ats,
537 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
543 * Mark all matching allocation records as not connected.
545 * @param cls 'struct GTS_AtsHandle'
546 * @param key identity of the peer associated with the record
547 * @param value the 'struct AllocationRecord' to clear the 'connected' flag
548 * @return GNUNET_OK (continue to iterate)
551 disconnect_peer (void *cls,
552 const GNUNET_HashCode *key,
555 struct GNUNET_ATS_Handle *atc = cls;
556 struct AllocationRecord *ar = value;
558 if (GNUNET_YES == ar->connected)
560 ar->connected = GNUNET_NO;
561 update_bandwidth_assignment (atc, ar);
568 * We established a new connection with a peer (for example, because
569 * core asked for it or because the other peer connected to us).
570 * Calculate bandwidth assignments including the new peer.
573 * @param public_key public key of the peer
574 * @param peer identity of the new peer
575 * @param plugin_name name of the currently used transport plugin
576 * @param session session in use (if available)
577 * @param plugin_addr address in use (if available)
578 * @param plugin_addr_len number of bytes in plugin_addr
579 * @param ats performance data for the connection
580 * @param ats_count number of performance records in 'ats'
583 GNUNET_ATS_peer_connect (struct GNUNET_ATS_Handle *atc,
584 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
585 const struct GNUNET_PeerIdentity *peer,
586 const char *plugin_name,
587 struct Session *session,
588 const void *plugin_addr,
589 size_t plugin_addr_len,
590 const struct GNUNET_TRANSPORT_ATS_Information *ats,
593 struct AllocationRecord *ar;
594 struct UpdateSessionContext usc;
596 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
599 ar = create_allocation_record (public_key,
606 ar->connected = GNUNET_YES;
610 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
614 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
617 GNUNET_assert (GNUNET_OK ==
618 GNUNET_CONTAINER_multihashmap_put (atc->peers,
621 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
626 * We disconnected from the given peer (for example, because ats, core
627 * or blacklist asked for it or because the other peer disconnected).
628 * Calculate bandwidth assignments without the peer.
631 * @param peer identity of the new peer
634 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc,
635 const struct GNUNET_PeerIdentity *peer)
637 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
645 * Closure for 'destroy_allocation_record'
647 struct SessionDestroyContext
652 struct GNUNET_ATS_Handle *atc;
655 * Session being destroyed.
657 const struct Session *session;
662 * Free an allocation record matching the given session.
664 * @param cls the 'struct SessionDestroyContext'
665 * @param key identity of the peer associated with the record
666 * @param value the 'struct AllocationRecord' to free
667 * @return GNUNET_OK (continue to iterate)
670 destroy_session (void *cls,
671 const GNUNET_HashCode *key,
674 struct SessionDestroyContext *sdc = cls;
675 struct AllocationRecord *ar = value;
677 if (ar->session != sdc->session)
680 if (ar->plugin_addr != NULL)
682 GNUNET_assert (GNUNET_OK ==
683 GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers,
686 if (GNUNET_YES == ar->connected);
688 /* FIXME: is this supposed to be allowed? What to do then? */
691 destroy_allocation_record (NULL, key, ar);
697 * A session got destroyed, stop including it as a valid address.
700 * @param peer identity of the peer
701 * @param session session handle that is no longer valid
704 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc,
705 const struct GNUNET_PeerIdentity *peer,
706 const struct Session *session)
708 struct SessionDestroyContext sdc;
711 sdc.session = session;
712 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
719 * Notify validation watcher that an entry is now valid
721 * @param cls 'struct ValidationEntry' that is now valid
722 * @param key peer identity (unused)
723 * @param value a 'GST_ValidationIteratorContext' to notify
724 * @return GNUNET_YES (continue to iterate)
727 notify_valid (void *cls,
728 const GNUNET_HashCode *key,
731 struct AllocationRecord *ar = cls;
732 struct GNUNET_ATS_SuggestionContext *asc = value;
734 asc->cb (asc->cb_cls,
740 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32),
741 ar->ats, ar->ats_count);
747 * We have updated performance statistics for a given address. Note
748 * that this function can be called for addresses that are currently
749 * in use as well as addresses that are valid but not actively in use.
750 * Furthermore, the peer may not even be connected to us right now (in
751 * which case the call may be ignored or the information may be stored
752 * for later use). Update bandwidth assignments.
755 * @param public_key public key of the peer
756 * @param peer identity of the peer
757 * @param valid_until how long is the address valid?
758 * @param plugin_name name of the transport plugin
759 * @param session session handle (if available)
760 * @param plugin_addr address (if available)
761 * @param plugin_addr_len number of bytes in plugin_addr
762 * @param ats performance data for the address
763 * @param ats_count number of performance records in 'ats'
766 GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc,
767 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
768 const struct GNUNET_PeerIdentity *peer,
769 struct GNUNET_TIME_Absolute valid_until,
770 const char *plugin_name,
771 struct Session *session,
772 const void *plugin_addr,
773 size_t plugin_addr_len,
774 const struct GNUNET_TRANSPORT_ATS_Information *ats,
777 struct AllocationRecord *ar;
778 struct UpdateSessionContext usc;
780 ar = create_allocation_record (public_key,
790 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
794 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
797 GNUNET_assert (GNUNET_OK ==
798 GNUNET_CONTAINER_multihashmap_put (atc->peers,
801 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
802 GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
808 /* end of file gnunet-service-transport_ats.c */