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 #define DEBUG_ATS GNUNET_NO
38 // NOTE: this implementation is simply supposed
39 // to implement a simplistic strategy in-process;
40 // in the future, we plan to replace it with a real
41 // service implementation
44 * Allocation record for a peer's address.
46 struct AllocationRecord
50 * Performance information associated with this address (array).
52 struct GNUNET_TRANSPORT_ATS_Information *ats;
60 * Address this record represents, allocated at the end of this struct.
62 const void *plugin_addr;
65 * Session associated with this record.
67 struct Session *session;
70 * Number of bytes in plugin_addr.
72 size_t plugin_addr_len;
75 * Number of entries in 'ats'.
80 * Bandwidth assigned to this address right now, 0 for none.
82 struct GNUNET_BANDWIDTH_Value32NBO bandwidth;
85 * Set to GNUNET_YES if this is the connected address of a connected peer.
93 * Opaque handle to obtain address suggestions.
95 struct GNUNET_ATS_SuggestionContext
99 * Function to call with our final suggestion.
101 GNUNET_ATS_AddressSuggestionCallback cb;
111 struct GNUNET_ATS_Handle *atc;
114 * Which peer are we monitoring?
116 struct GNUNET_PeerIdentity target;
122 * Handle to the ATS subsystem.
124 struct GNUNET_ATS_Handle
129 const struct GNUNET_CONFIGURATION_Handle *cfg;
132 * Function to call when the allocation changes.
134 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb;
137 * Closure for 'alloc_cb'.
142 * Information about all connected peers. Maps peer identities
143 * to one or more 'struct AllocationRecord' values.
145 struct GNUNET_CONTAINER_MultiHashMap *peers;
148 * Map of PeerIdentities to 'struct GNUNET_ATS_SuggestionContext's.
150 struct GNUNET_CONTAINER_MultiHashMap *notify_map;
154 * Task scheduled to update our bandwidth assignment.
156 GNUNET_SCHEDULER_TaskIdentifier ba_task;
159 * Total bandwidth per configuration.
161 unsigned long long total_bps;
166 * Count number of connected records.
168 * @param cls pointer to counter
169 * @param key identity of the peer associated with the records
170 * @param value a 'struct AllocationRecord'
171 * @return GNUNET_YES (continue iteration)
174 count_connections (void *cls, const GNUNET_HashCode * key, void *value)
176 unsigned int *ac = cls;
177 struct AllocationRecord *ar = value;
179 if (GNUNET_YES == ar->connected)
186 * Closure for 'set_bw_connections'.
188 struct SetBandwidthContext
193 struct GNUNET_ATS_Handle *atc;
196 * Bandwidth to assign.
198 struct GNUNET_BANDWIDTH_Value32NBO bw;
203 * Set bandwidth based on record.
205 * @param cls 'struct SetBandwidthContext'
206 * @param key identity of the peer associated with the records
207 * @param value a 'struct AllocationRecord'
208 * @return GNUNET_YES (continue iteration)
211 set_bw_connections (void *cls, const GNUNET_HashCode * key, void *value)
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,
221 ar->plugin_name, ar->session, ar->plugin_addr,
222 ar->plugin_addr_len, ar->bandwidth);
224 else if (ntohl (ar->bandwidth.value__) > 0)
226 ar->bandwidth = GNUNET_BANDWIDTH_value_init (0);
227 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
228 (const struct GNUNET_PeerIdentity *) key,
229 ar->plugin_name, ar->session, ar->plugin_addr,
230 ar->plugin_addr_len, ar->bandwidth);
237 * Task run to update bandwidth assignments.
239 * @param cls the 'struct GNUNET_ATS_Handle'
240 * @param tc scheduler context
243 update_bandwidth_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
245 struct GNUNET_ATS_Handle *atc = cls;
247 struct SetBandwidthContext bwc;
249 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
250 /* FIXME: update calculations NICELY; what follows is a naive version */
251 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &count_connections, &ac);
255 GNUNET_assert (ac > 0);
256 bwc.bw = GNUNET_BANDWIDTH_value_init (atc->total_bps / ac);
257 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &set_bw_connections, &bwc);
262 * Calculate an updated bandwidth assignment and notify.
265 * @param change which allocation record changed?
268 update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc,
269 struct AllocationRecord *change)
271 /* FIXME: based on the 'change', update the LP-problem... */
272 if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK)
273 atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task, atc);
278 * Function called with feasbile addresses we might want to suggest.
280 * @param cls the 'struct GNUNET_ATS_SuggestionContext'
281 * @param key identity of the peer
282 * @param value a 'struct AllocationRecord' for the peer
283 * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet
286 suggest_address (void *cls, const GNUNET_HashCode * key, void *value)
288 struct GNUNET_ATS_SuggestionContext *asc = cls;
289 struct AllocationRecord *ar = value;
292 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
293 "Suggesting address for peer `%s'\n", GNUNET_h2s (key));
296 /* trivial strategy: pick first available address... */
297 asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
298 ar->plugin_addr_len, ar->session,
299 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), ar->ats,
306 map_it (void *cls, const GNUNET_HashCode * key, void *value)
308 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api", "Found entry for %s\n",
314 * We would like to establish a new connection with a peer.
315 * ATS should suggest a good address to begin with.
318 * @param peer identity of the new peer
319 * @param cb function to call with the address
320 * @param cb_cls closure for cb
322 struct GNUNET_ATS_SuggestionContext *
323 GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
324 const struct GNUNET_PeerIdentity *peer,
325 GNUNET_ATS_AddressSuggestionCallback cb,
328 struct GNUNET_ATS_SuggestionContext *asc;
331 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
332 "Looking up suggested address for peer `%s'\n",
337 asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
339 asc->cb_cls = cb_cls;
343 GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers, &peer->hashPubKey,
344 &suggest_address, asc);
347 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &map_it, (void *) peer);
348 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
349 "Addresses %u (of %i) processed, \n", count,
350 GNUNET_CONTAINER_multihashmap_size (atc->peers));
358 GNUNET_CONTAINER_multihashmap_put (atc->notify_map, &peer->hashPubKey, asc,
359 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
365 * Cancel suggestion request.
367 * @param asc handle of the request to cancel
370 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
372 GNUNET_assert (GNUNET_OK ==
373 GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
374 &asc->target.hashPubKey,
381 * Initialize the ATS subsystem.
383 * @param cfg configuration to use
384 * @param alloc_cb notification to call whenever the allocation changed
385 * @param alloc_cb_cls closure for 'alloc_cb'
386 * @return ats context
388 struct GNUNET_ATS_Handle *
389 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
390 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb,
393 struct GNUNET_ATS_Handle *atc;
396 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api", "ATS init\n");
398 atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_Handle));
400 atc->alloc_cb = alloc_cb;
401 atc->alloc_cb_cls = alloc_cb_cls;
402 atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
403 atc->notify_map = GNUNET_CONTAINER_multihashmap_create (256);
404 GNUNET_CONFIGURATION_get_value_number (cfg, "core", "TOTAL_QUOTA_OUT",
411 * Free an allocation record.
414 * @param key identity of the peer associated with the record
415 * @param value the 'struct AllocationRecord' to free
416 * @return GNUNET_OK (continue to iterate)
419 destroy_allocation_record (void *cls, const GNUNET_HashCode * key, void *value)
421 struct AllocationRecord *ar = value;
423 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
424 GNUNET_free (ar->plugin_name);
431 * Shutdown the ATS subsystem.
436 GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc)
439 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api", "ATS shutdown\n");
441 if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
443 GNUNET_SCHEDULER_cancel (atc->ba_task);
444 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
446 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_allocation_record,
448 GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
449 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
450 GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
451 atc->notify_map = NULL;
457 * Closure for 'update_session'
459 struct UpdateSessionContext
464 struct GNUNET_ATS_Handle *atc;
467 * Allocation record with new information.
469 struct AllocationRecord *arnew;
474 * Update an allocation record, merging with the new information
476 * @param cls a new 'struct AllocationRecord'
477 * @param key identity of the peer associated with the records
478 * @param value the old 'struct AllocationRecord'
479 * @return GNUNET_YES if the records do not match,
480 * GNUNET_NO if the record do match and 'old' was updated
483 update_session (void *cls, const GNUNET_HashCode * key, void *value)
485 struct UpdateSessionContext *usc = cls;
486 struct AllocationRecord *arnew = usc->arnew;
487 struct AllocationRecord *arold = value;
495 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
496 "Updating session for peer `%s' plugin `%s'\n",
497 GNUNET_h2s (key), arold->plugin_name);
500 if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
502 if (((arnew->session == arold->session) && (arnew->session != NULL)) ||
503 ((arold->session == NULL) &&
504 (arold->plugin_addr_len == arnew->plugin_addr_len) &&
506 memcmp (arold->plugin_addr, arnew->plugin_addr,
507 arnew->plugin_addr_len))))
511 if (arnew->session != arold->session)
513 arold->session = arnew->session;
516 if ((arnew->connected == GNUNET_YES) && (arold->connected == GNUNET_NO))
518 arold->connected = GNUNET_YES;
522 /* Update existing value */
524 while (c_new < arnew->ats_count)
528 while (c_old < arold->ats_count)
530 if (arold->ats[c_old].type == arnew->ats[c_new].type)
533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
534 "Found type %u, old value=%u new value=%u\n",
535 ntohl (arold->ats[c_old].type),
536 ntohl (arold->ats[c_old].value),
537 ntohl (arnew->ats[c_new].value));
539 arold->ats[c_old].value = arnew->ats[c_new].value;
545 if (found == GNUNET_NO)
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Added new value type %u, old value=%u new value=%u\n",
550 ntohl (arnew->ats[c_new].type),
551 ntohl (arnew->ats[c_new].value));
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Old array size: %u\n",
555 GNUNET_array_grow (arold->ats, arold->ats_count, arold->ats_count + 1);
556 arold->ats[arold->ats_count - 1].type = arnew->ats[c_new].type;
557 arold->ats[arold->ats_count - 1].value = arnew->ats[c_new].value;
558 arold->ats[arold->ats_count].type = htonl (0);
559 arold->ats[arold->ats_count].value = htonl (0);
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New array size: %i\n",
568 if (GNUNET_YES == change)
569 update_bandwidth_assignment (usc->atc, arold);
577 * Create an allocation record with the given properties.
579 * @param plugin_name name of the currently used transport plugin
580 * @param session session in use (if available)
581 * @param plugin_addr address in use (if available)
582 * @param plugin_addr_len number of bytes in plugin_addr
583 * @param ats performance data for the connection
584 * @param ats_count number of performance records in 'ats'
586 static struct AllocationRecord *
587 create_allocation_record (const char *plugin_name, struct Session *session,
588 const void *plugin_addr, size_t plugin_addr_len,
589 const struct GNUNET_TRANSPORT_ATS_Information *ats,
592 struct AllocationRecord *ar;
594 ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
595 ar->plugin_name = GNUNET_strdup (plugin_name);
596 ar->plugin_addr = &ar[1];
597 memcpy (&ar[1], plugin_addr, plugin_addr_len);
598 ar->session = session;
599 ar->plugin_addr_len = plugin_addr_len;
600 GNUNET_assert (ats_count > 0);
601 GNUNET_array_grow (ar->ats, ar->ats_count, ats_count);
602 memcpy (ar->ats, ats,
603 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
609 * Mark all matching allocation records as not connected.
611 * @param cls 'struct GTS_AtsHandle'
612 * @param key identity of the peer associated with the record
613 * @param value the 'struct AllocationRecord' to clear the 'connected' flag
614 * @return GNUNET_OK (continue to iterate)
617 disconnect_peer (void *cls, const GNUNET_HashCode * key, void *value)
619 struct GNUNET_ATS_Handle *atc = cls;
620 struct AllocationRecord *ar = value;
622 if (GNUNET_YES == ar->connected)
624 ar->connected = GNUNET_NO;
625 update_bandwidth_assignment (atc, ar);
632 * We established a new connection with a peer (for example, because
633 * core asked for it or because the other peer connected to us).
634 * Calculate bandwidth assignments including the new peer.
637 * @param peer identity of the new peer
638 * @param plugin_name name of the currently used transport plugin
639 * @param session session in use (if available)
640 * @param plugin_addr address in use (if available)
641 * @param plugin_addr_len number of bytes in plugin_addr
642 * @param ats performance data for the connection
643 * @param ats_count number of performance records in 'ats'
646 GNUNET_ATS_peer_connect (struct GNUNET_ATS_Handle *atc,
647 const struct GNUNET_PeerIdentity *peer,
648 const char *plugin_name, struct Session *session,
649 const void *plugin_addr, size_t plugin_addr_len,
650 const struct GNUNET_TRANSPORT_ATS_Information *ats,
653 struct AllocationRecord *ar;
654 struct UpdateSessionContext usc;
656 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &disconnect_peer,
658 ar = create_allocation_record (plugin_name, session, plugin_addr,
659 plugin_addr_len, ats, ats_count);
660 ar->connected = GNUNET_YES;
664 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
666 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
669 GNUNET_assert (GNUNET_OK ==
670 GNUNET_CONTAINER_multihashmap_put (atc->peers,
671 &peer->hashPubKey, ar,
672 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
677 * We disconnected from the given peer (for example, because ats, core
678 * or blacklist asked for it or because the other peer disconnected).
679 * Calculate bandwidth assignments without the peer.
682 * @param peer identity of the new peer
685 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc,
686 const struct GNUNET_PeerIdentity *peer)
688 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
690 &disconnect_peer, atc);
695 * Closure for 'destroy_allocation_record'
697 struct SessionDestroyContext
702 struct GNUNET_ATS_Handle *atc;
705 * Session being destroyed.
707 const struct Session *session;
712 * Free an allocation record matching the given session.
714 * @param cls the 'struct SessionDestroyContext'
715 * @param key identity of the peer associated with the record
716 * @param value the 'struct AllocationRecord' to free
717 * @return GNUNET_OK (continue to iterate)
720 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
722 struct SessionDestroyContext *sdc = cls;
723 struct AllocationRecord *ar = value;
725 if (ar->session != sdc->session)
728 if (ar->plugin_addr != NULL)
730 GNUNET_assert (GNUNET_OK ==
731 GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers, key,
733 if (GNUNET_YES == ar->connected) ;
735 /* FIXME: is this supposed to be allowed? What to do then? */
738 destroy_allocation_record (NULL, key, ar);
744 * A session got destroyed, stop including it as a valid address.
747 * @param peer identity of the peer
748 * @param session session handle that is no longer valid
751 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc,
752 const struct GNUNET_PeerIdentity *peer,
753 const struct Session *session)
755 struct SessionDestroyContext sdc;
758 sdc.session = session;
759 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_session,
765 * Notify validation watcher that an entry is now valid
767 * @param cls 'struct ValidationEntry' that is now valid
768 * @param key peer identity (unused)
769 * @param value a 'GST_ValidationIteratorContext' to notify
770 * @return GNUNET_YES (continue to iterate)
773 notify_valid (void *cls, const GNUNET_HashCode * key, void *value)
775 struct AllocationRecord *ar = cls;
776 struct GNUNET_ATS_SuggestionContext *asc = value;
778 asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
779 ar->plugin_addr_len, ar->session,
780 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), ar->ats,
782 GNUNET_ATS_suggest_address_cancel (asc);
788 * We have updated performance statistics for a given address. Note
789 * that this function can be called for addresses that are currently
790 * in use as well as addresses that are valid but not actively in use.
791 * Furthermore, the peer may not even be connected to us right now (in
792 * which case the call may be ignored or the information may be stored
793 * for later use). Update bandwidth assignments.
796 * @param peer identity of the peer
797 * @param valid_until how long is the address valid?
798 * @param plugin_name name of the transport plugin
799 * @param session session handle (if available)
800 * @param plugin_addr address (if available)
801 * @param plugin_addr_len number of bytes in plugin_addr
802 * @param ats performance data for the address
803 * @param ats_count number of performance records in 'ats'
806 GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc,
807 const struct GNUNET_PeerIdentity *peer,
808 struct GNUNET_TIME_Absolute valid_until,
809 const char *plugin_name, struct Session *session,
810 const void *plugin_addr, size_t plugin_addr_len,
811 const struct GNUNET_TRANSPORT_ATS_Information *ats,
814 struct AllocationRecord *ar;
815 struct UpdateSessionContext usc;
819 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
820 "Updating address for peer `%s', plugin `%s'\n",
821 GNUNET_i2s (peer), plugin_name);
824 ar = create_allocation_record (plugin_name, session, plugin_addr,
825 plugin_addr_len, ats, ats_count);
829 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
831 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
835 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
836 "Adding new address for peer `%s', plugin `%s'\n",
837 GNUNET_i2s (peer), plugin_name);
839 GNUNET_assert (GNUNET_OK ==
840 GNUNET_CONTAINER_multihashmap_put (atc->peers,
841 &peer->hashPubKey, ar,
842 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
843 GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
844 &peer->hashPubKey, ¬ify_valid,
848 /* end of file gnunet-service-transport_ats.c */