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 * Performance information associated with this address (array).
43 struct GNUNET_TRANSPORT_ATS_Information *ats;
51 * Address this record represents, allocated at the end of this struct.
53 const void *plugin_addr;
56 * Session associated with this record.
58 struct Session *session;
61 * Number of bytes in plugin_addr.
63 size_t plugin_addr_len;
66 * Number of entries in 'ats'.
71 * Bandwidth assigned to this address right now, 0 for none.
73 struct GNUNET_BANDWIDTH_Value32NBO bandwidth;
76 * Set to GNUNET_YES if this is the connected address of a connected peer.
84 * Opaque handle to obtain address suggestions.
86 struct GNUNET_ATS_SuggestionContext
90 * Function to call with our final suggestion.
92 GNUNET_ATS_AddressSuggestionCallback cb;
102 struct GNUNET_ATS_Handle *atc;
105 * Which peer are we monitoring?
107 struct GNUNET_PeerIdentity target;
113 * Handle to the ATS subsystem.
115 struct GNUNET_ATS_Handle
120 const struct GNUNET_CONFIGURATION_Handle *cfg;
123 * Function to call when the allocation changes.
125 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb;
128 * Closure for 'alloc_cb'.
133 * Information about all connected peers. Maps peer identities
134 * to one or more 'struct AllocationRecord' values.
136 struct GNUNET_CONTAINER_MultiHashMap *peers;
139 * Map of PeerIdentities to 'struct GNUNET_ATS_SuggestionContext's.
141 struct GNUNET_CONTAINER_MultiHashMap *notify_map;
145 * Task scheduled to update our bandwidth assignment.
147 GNUNET_SCHEDULER_TaskIdentifier ba_task;
150 * Total bandwidth per configuration.
152 unsigned long long total_bps;
157 * Count number of connected records.
159 * @param cls pointer to counter
160 * @param key identity of the peer associated with the records
161 * @param value a 'struct AllocationRecord'
162 * @return GNUNET_YES (continue iteration)
165 count_connections (void *cls,
166 const GNUNET_HashCode *key,
169 unsigned int *ac = cls;
170 struct AllocationRecord *ar = value;
172 if (GNUNET_YES == ar->connected)
179 * Closure for 'set_bw_connections'.
181 struct SetBandwidthContext
186 struct GNUNET_ATS_Handle *atc;
189 * Bandwidth to assign.
191 struct GNUNET_BANDWIDTH_Value32NBO bw;
196 * Set bandwidth based on record.
198 * @param cls 'struct SetBandwidthContext'
199 * @param key identity of the peer associated with the records
200 * @param value a 'struct AllocationRecord'
201 * @return GNUNET_YES (continue iteration)
204 set_bw_connections (void *cls,
205 const GNUNET_HashCode *key,
208 struct SetBandwidthContext *sbc = cls;
209 struct AllocationRecord *ar = value;
211 if (GNUNET_YES == ar->connected)
213 ar->bandwidth = sbc->bw;
214 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
215 (const struct GNUNET_PeerIdentity*) key,
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,
238 * Task run to update bandwidth assignments.
240 * @param cls the 'struct GNUNET_ATS_Handle'
241 * @param tc scheduler context
244 update_bandwidth_task (void *cls,
245 const struct GNUNET_SCHEDULER_TaskContext *tc)
247 struct GNUNET_ATS_Handle *atc = cls;
249 struct SetBandwidthContext bwc;
251 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
252 /* FIXME: update calculations NICELY; what follows is a naive version */
253 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
257 bwc.bw = GNUNET_BANDWIDTH_value_init (atc->total_bps / ac);
258 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
265 * Calculate an updated bandwidth assignment and notify.
268 * @param change which allocation record changed?
271 update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc,
272 struct AllocationRecord *change)
274 /* FIXME: based on the 'change', update the LP-problem... */
275 if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK)
276 atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task,
282 * Function called with feasbile addresses we might want to suggest.
284 * @param cls the 'struct GNUNET_ATS_SuggestionContext'
285 * @param key identity of the peer
286 * @param value a 'struct AllocationRecord' for the peer
287 * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet
290 suggest_address (void *cls,
291 const GNUNET_HashCode *key,
294 struct GNUNET_ATS_SuggestionContext *asc = cls;
295 struct AllocationRecord *ar = value;
297 /* trivial strategy: pick first available address... */
298 asc->cb (asc->cb_cls,
303 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32),
304 ar->ats, ar->ats_count);
311 * We would like to establish a new connection with a peer.
312 * ATS should suggest a good address to begin with.
315 * @param peer identity of the new peer
316 * @param cb function to call with the address
317 * @param cb_cls closure for cb
319 struct GNUNET_ATS_SuggestionContext *
320 GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
321 const struct GNUNET_PeerIdentity *peer,
322 GNUNET_ATS_AddressSuggestionCallback cb,
325 struct GNUNET_ATS_SuggestionContext *asc;
327 asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
329 asc->cb_cls = cb_cls;
332 GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
341 GNUNET_CONTAINER_multihashmap_put (atc->notify_map,
344 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
350 * Cancel suggestion request.
352 * @param asc handle of the request to cancel
355 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
357 GNUNET_assert (GNUNET_OK ==
358 GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
359 &asc->target.hashPubKey,
366 * Initialize the ATS subsystem.
368 * @param cfg configuration to use
369 * @param alloc_cb notification to call whenever the allocation changed
370 * @param alloc_cb_cls closure for 'alloc_cb'
371 * @return ats context
373 struct GNUNET_ATS_Handle *
374 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
375 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb,
378 struct GNUNET_ATS_Handle *atc;
380 atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_Handle));
382 atc->alloc_cb = alloc_cb;
383 atc->alloc_cb_cls = alloc_cb_cls;
384 atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
385 GNUNET_CONFIGURATION_get_value_number (cfg,
394 * Free an allocation record.
397 * @param key identity of the peer associated with the record
398 * @param value the 'struct AllocationRecord' to free
399 * @return GNUNET_OK (continue to iterate)
402 destroy_allocation_record (void *cls,
403 const GNUNET_HashCode *key,
406 struct AllocationRecord *ar = value;
408 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
409 GNUNET_free (ar->plugin_name);
416 * Shutdown the ATS subsystem.
421 GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc)
423 if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
425 GNUNET_SCHEDULER_cancel (atc->ba_task);
426 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
428 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
429 &destroy_allocation_record,
431 GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
432 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
433 GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
434 atc->notify_map = NULL;
440 * Closure for 'update_session'
442 struct UpdateSessionContext
447 struct GNUNET_ATS_Handle *atc;
450 * Allocation record with new information.
452 struct AllocationRecord *arnew;
457 * Update an allocation record, merging with the new information
459 * @param cls a new 'struct AllocationRecord'
460 * @param key identity of the peer associated with the records
461 * @param value the old 'struct AllocationRecord'
462 * @return GNUNET_YES if the records do not match,
463 * GNUNET_NO if the record do match and 'old' was updated
466 update_session (void *cls,
467 const GNUNET_HashCode *key,
470 struct UpdateSessionContext *usc = cls;
471 struct AllocationRecord *arnew = usc->arnew;
472 struct AllocationRecord *arold = value;
475 if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
477 if ( (arnew->session == arold->session) ||
478 ( (arold->session == NULL) &&
479 (arold->plugin_addr_len == arnew->plugin_addr_len) &&
480 (0 == memcmp (arold->plugin_addr,
482 arnew->plugin_addr_len)) ) )
486 if (arnew->session != arold->session)
488 arold->session = arnew->session;
491 if ( (arnew->connected == GNUNET_YES) &&
492 (arold->connected == GNUNET_NO) )
494 arold->connected = GNUNET_YES;
497 // FIXME: merge ats arrays of (arold, arnew);
499 if (GNUNET_YES == change)
500 update_bandwidth_assignment (usc->atc, arold);
508 * Create an allocation record with the given properties.
510 * @param plugin_name name of the currently used transport plugin
511 * @param session session in use (if available)
512 * @param plugin_addr address in use (if available)
513 * @param plugin_addr_len number of bytes in plugin_addr
514 * @param ats performance data for the connection
515 * @param ats_count number of performance records in 'ats'
517 static struct AllocationRecord *
518 create_allocation_record (const char *plugin_name,
519 struct Session *session,
520 const void *plugin_addr,
521 size_t plugin_addr_len,
522 const struct GNUNET_TRANSPORT_ATS_Information *ats,
525 struct AllocationRecord *ar;
527 ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
528 ar->plugin_name = GNUNET_strdup (plugin_name);
529 ar->plugin_addr = &ar[1];
530 memcpy (&ar[1], plugin_addr, plugin_addr_len);
531 ar->session = session;
532 ar->plugin_addr_len = plugin_addr_len;
533 GNUNET_array_grow (ar->ats,
538 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
544 * Mark all matching allocation records as not connected.
546 * @param cls 'struct GTS_AtsHandle'
547 * @param key identity of the peer associated with the record
548 * @param value the 'struct AllocationRecord' to clear the 'connected' flag
549 * @return GNUNET_OK (continue to iterate)
552 disconnect_peer (void *cls,
553 const GNUNET_HashCode *key,
556 struct GNUNET_ATS_Handle *atc = cls;
557 struct AllocationRecord *ar = value;
559 if (GNUNET_YES == ar->connected)
561 ar->connected = GNUNET_NO;
562 update_bandwidth_assignment (atc, ar);
569 * We established a new connection with a peer (for example, because
570 * core asked for it or because the other peer connected to us).
571 * Calculate bandwidth assignments including the new 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_PeerIdentity *peer,
585 const char *plugin_name,
586 struct Session *session,
587 const void *plugin_addr,
588 size_t plugin_addr_len,
589 const struct GNUNET_TRANSPORT_ATS_Information *ats,
592 struct AllocationRecord *ar;
593 struct UpdateSessionContext usc;
595 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
598 ar = create_allocation_record (plugin_name,
604 ar->connected = GNUNET_YES;
608 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
612 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
615 GNUNET_assert (GNUNET_OK ==
616 GNUNET_CONTAINER_multihashmap_put (atc->peers,
619 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
624 * We disconnected from the given peer (for example, because ats, core
625 * or blacklist asked for it or because the other peer disconnected).
626 * Calculate bandwidth assignments without the peer.
629 * @param peer identity of the new peer
632 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc,
633 const struct GNUNET_PeerIdentity *peer)
635 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
643 * Closure for 'destroy_allocation_record'
645 struct SessionDestroyContext
650 struct GNUNET_ATS_Handle *atc;
653 * Session being destroyed.
655 const struct Session *session;
660 * Free an allocation record matching the given session.
662 * @param cls the 'struct SessionDestroyContext'
663 * @param key identity of the peer associated with the record
664 * @param value the 'struct AllocationRecord' to free
665 * @return GNUNET_OK (continue to iterate)
668 destroy_session (void *cls,
669 const GNUNET_HashCode *key,
672 struct SessionDestroyContext *sdc = cls;
673 struct AllocationRecord *ar = value;
675 if (ar->session != sdc->session)
678 if (ar->plugin_addr != NULL)
680 GNUNET_assert (GNUNET_OK ==
681 GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers,
684 if (GNUNET_YES == ar->connected);
686 /* FIXME: is this supposed to be allowed? What to do then? */
689 destroy_allocation_record (NULL, key, ar);
695 * A session got destroyed, stop including it as a valid address.
698 * @param peer identity of the peer
699 * @param session session handle that is no longer valid
702 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc,
703 const struct GNUNET_PeerIdentity *peer,
704 const struct Session *session)
706 struct SessionDestroyContext sdc;
709 sdc.session = session;
710 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
717 * Notify validation watcher that an entry is now valid
719 * @param cls 'struct ValidationEntry' that is now valid
720 * @param key peer identity (unused)
721 * @param value a 'GST_ValidationIteratorContext' to notify
722 * @return GNUNET_YES (continue to iterate)
725 notify_valid (void *cls,
726 const GNUNET_HashCode *key,
729 struct AllocationRecord *ar = cls;
730 struct GNUNET_ATS_SuggestionContext *asc = value;
732 asc->cb (asc->cb_cls,
737 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32),
738 ar->ats, ar->ats_count);
739 GNUNET_ATS_suggest_address_cancel (asc);
745 * We have updated performance statistics for a given address. Note
746 * that this function can be called for addresses that are currently
747 * in use as well as addresses that are valid but not actively in use.
748 * Furthermore, the peer may not even be connected to us right now (in
749 * which case the call may be ignored or the information may be stored
750 * for later use). Update bandwidth assignments.
753 * @param peer identity of the peer
754 * @param valid_until how long is the address valid?
755 * @param plugin_name name of the transport plugin
756 * @param session session handle (if available)
757 * @param plugin_addr address (if available)
758 * @param plugin_addr_len number of bytes in plugin_addr
759 * @param ats performance data for the address
760 * @param ats_count number of performance records in 'ats'
763 GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc,
764 const struct GNUNET_PeerIdentity *peer,
765 struct GNUNET_TIME_Absolute valid_until,
766 const char *plugin_name,
767 struct Session *session,
768 const void *plugin_addr,
769 size_t plugin_addr_len,
770 const struct GNUNET_TRANSPORT_ATS_Information *ats,
773 struct AllocationRecord *ar;
774 struct UpdateSessionContext usc;
776 ar = create_allocation_record (plugin_name,
785 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
789 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
792 GNUNET_assert (GNUNET_OK ==
793 GNUNET_CONTAINER_multihashmap_put (atc->peers,
796 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
797 GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
803 /* end of file gnunet-service-transport_ats.c */