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_EXTRA_LOGGING
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 * Inbound bandwidth assigned to this address right now, 0 for none.
82 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
85 * Outbound bandwidth assigned to this address right now, 0 for none.
87 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
90 * Set to GNUNET_YES if this is the connected address of a connected peer.
98 * Opaque handle to obtain address suggestions.
100 struct GNUNET_ATS_SuggestionContext
104 * Function to call with our final suggestion.
106 GNUNET_ATS_AddressSuggestionCallback cb;
116 struct GNUNET_ATS_Handle *atc;
119 * Which peer are we monitoring?
121 struct GNUNET_PeerIdentity target;
127 * Handle to the ATS subsystem.
129 struct GNUNET_ATS_Handle
134 const struct GNUNET_CONFIGURATION_Handle *cfg;
137 * Function to call when the allocation changes.
139 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb;
142 * Closure for 'alloc_cb'.
147 * Information about all connected peers. Maps peer identities
148 * to one or more 'struct AllocationRecord' values.
150 struct GNUNET_CONTAINER_MultiHashMap *peers;
153 * Map of PeerIdentities to 'struct GNUNET_ATS_SuggestionContext's.
155 struct GNUNET_CONTAINER_MultiHashMap *notify_map;
159 * Task scheduled to update our bandwidth assignment.
161 GNUNET_SCHEDULER_TaskIdentifier ba_task;
164 * Total inbound bandwidth per configuration.
166 unsigned long long total_bps_in;
169 * Total outbound bandwidth per configuration.
171 unsigned long long total_bps_out;
176 * Count number of connected records.
178 * @param cls pointer to counter
179 * @param key identity of the peer associated with the records
180 * @param value a 'struct AllocationRecord'
181 * @return GNUNET_YES (continue iteration)
184 count_connections (void *cls, const GNUNET_HashCode * key, void *value)
186 unsigned int *ac = cls;
187 struct AllocationRecord *ar = value;
189 if (GNUNET_YES == ar->connected)
196 * Closure for 'set_bw_connections'.
198 struct SetBandwidthContext
203 struct GNUNET_ATS_Handle *atc;
206 * Inbound bandwidth to assign.
208 struct GNUNET_BANDWIDTH_Value32NBO bw_in;
211 * Outbound bandwidth to assign.
213 struct GNUNET_BANDWIDTH_Value32NBO bw_out;
218 * Set bandwidth based on record.
220 * @param cls 'struct SetBandwidthContext'
221 * @param key identity of the peer associated with the records
222 * @param value a 'struct AllocationRecord'
223 * @return GNUNET_YES (continue iteration)
226 set_bw_connections (void *cls, const GNUNET_HashCode * key, void *value)
228 struct SetBandwidthContext *sbc = cls;
229 struct AllocationRecord *ar = value;
231 if (GNUNET_YES == ar->connected)
233 ar->bandwidth_in = sbc->bw_in;
234 ar->bandwidth_out = sbc->bw_out;
235 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
236 (const struct GNUNET_PeerIdentity *) key,
237 ar->plugin_name, ar->session, ar->plugin_addr,
238 ar->plugin_addr_len, ar->bandwidth_out, ar->bandwidth_in);
240 else if (ntohl (ar->bandwidth_out.value__) > 0)
242 ar->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
243 ar->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
244 sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
245 (const struct GNUNET_PeerIdentity *) key,
246 ar->plugin_name, ar->session, ar->plugin_addr,
247 ar->plugin_addr_len, ar->bandwidth_out, ar->bandwidth_in);
254 * Task run to update bandwidth assignments.
256 * @param cls the 'struct GNUNET_ATS_Handle'
257 * @param tc scheduler context
260 update_bandwidth_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
262 struct GNUNET_ATS_Handle *atc = cls;
264 struct SetBandwidthContext bwc;
266 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
267 /* FIXME: update calculations NICELY; what follows is a naive version */
268 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &count_connections, &ac);
272 GNUNET_assert (ac > 0);
273 bwc.bw_in = GNUNET_BANDWIDTH_value_init (atc->total_bps_in / ac);
274 bwc.bw_out = GNUNET_BANDWIDTH_value_init (atc->total_bps_out / ac);
275 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &set_bw_connections, &bwc);
280 * Calculate an updated bandwidth assignment and notify.
283 * @param change which allocation record changed?
286 update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc,
287 struct AllocationRecord *change)
289 /* FIXME: based on the 'change', update the LP-problem... */
290 if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK)
291 atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task, atc);
296 * Function called with feasbile addresses we might want to suggest.
298 * @param cls the 'struct GNUNET_ATS_SuggestionContext'
299 * @param key identity of the peer
300 * @param value a 'struct AllocationRecord' for the peer
301 * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet
304 suggest_address (void *cls, const GNUNET_HashCode * key, void *value)
306 struct GNUNET_ATS_SuggestionContext *asc = cls;
307 struct AllocationRecord *ar = value;
310 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
311 "Suggesting address for peer `%s'\n", GNUNET_h2s (key));
314 /* trivial strategy: pick first available address... */
315 asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
316 ar->plugin_addr_len, ar->session,
317 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_out / 32),
318 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_in / 32),
326 map_it (void *cls, const GNUNET_HashCode * key, void *value)
328 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api", "Found entry for %s\n",
334 * We would like to establish a new connection with a peer.
335 * ATS should suggest a good address to begin with.
338 * @param peer identity of the new peer
339 * @param cb function to call with the address
340 * @param cb_cls closure for cb
342 struct GNUNET_ATS_SuggestionContext *
343 GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
344 const struct GNUNET_PeerIdentity *peer,
345 GNUNET_ATS_AddressSuggestionCallback cb,
348 struct GNUNET_ATS_SuggestionContext *asc;
351 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
352 "Looking up suggested address for peer `%s'\n",
355 asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
357 asc->cb_cls = cb_cls;
360 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers, &peer->hashPubKey,
361 &suggest_address, asc);
368 GNUNET_CONTAINER_multihashmap_put (atc->notify_map, &peer->hashPubKey, asc,
369 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
375 * Cancel suggestion request.
377 * @param asc handle of the request to cancel
380 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
382 GNUNET_assert (GNUNET_OK ==
383 GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
384 &asc->target.hashPubKey,
391 * Initialize the ATS subsystem.
393 * @param cfg configuration to use
394 * @param alloc_cb notification to call whenever the allocation changed
395 * @param alloc_cb_cls closure for 'alloc_cb'
396 * @return ats context
398 struct GNUNET_ATS_Handle *
399 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
400 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb,
403 struct GNUNET_ATS_Handle *atc;
406 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api", "ATS init\n");
408 atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_Handle));
410 atc->alloc_cb = alloc_cb;
411 atc->alloc_cb_cls = alloc_cb_cls;
412 atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
413 atc->notify_map = GNUNET_CONTAINER_multihashmap_create (256);
414 GNUNET_CONFIGURATION_get_value_number (cfg, "core", "TOTAL_QUOTA_OUT",
415 &atc->total_bps_out);
416 GNUNET_CONFIGURATION_get_value_number (cfg, "core", "TOTAL_QUOTA_IN",
423 * Free an allocation record.
426 * @param key identity of the peer associated with the record
427 * @param value the 'struct AllocationRecord' to free
428 * @return GNUNET_OK (continue to iterate)
431 destroy_allocation_record (void *cls, const GNUNET_HashCode * key, void *value)
433 struct AllocationRecord *ar = value;
435 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
436 GNUNET_free (ar->plugin_name);
443 * Shutdown the ATS subsystem.
448 GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc)
451 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api", "ATS shutdown\n");
453 if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
455 GNUNET_SCHEDULER_cancel (atc->ba_task);
456 atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
458 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_allocation_record,
460 GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
461 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
462 GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
463 atc->notify_map = NULL;
469 * Closure for 'update_session'
471 struct UpdateSessionContext
476 struct GNUNET_ATS_Handle *atc;
479 * Allocation record with new information.
481 struct AllocationRecord *arnew;
486 * Update an allocation record, merging with the new information
488 * @param cls a new 'struct AllocationRecord'
489 * @param key identity of the peer associated with the records
490 * @param value the old 'struct AllocationRecord'
491 * @return GNUNET_YES if the records do not match,
492 * GNUNET_NO if the record do match and 'old' was updated
495 update_session (void *cls, const GNUNET_HashCode * key, void *value)
497 struct UpdateSessionContext *usc = cls;
498 struct AllocationRecord *arnew = usc->arnew;
499 struct AllocationRecord *arold = value;
507 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
508 "Updating session for peer `%s' plugin `%s'\n",
509 GNUNET_h2s (key), arold->plugin_name);
512 if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
514 if (((arnew->session == arold->session) && (arnew->session != NULL)) ||
515 ((arold->session == NULL) &&
516 (arold->plugin_addr_len == arnew->plugin_addr_len) &&
518 memcmp (arold->plugin_addr, arnew->plugin_addr,
519 arnew->plugin_addr_len))))
523 if (arnew->session != arold->session)
525 arold->session = arnew->session;
528 if ((arnew->connected == GNUNET_YES) && (arold->connected == GNUNET_NO))
530 arold->connected = GNUNET_YES;
534 /* Update existing value */
536 while (c_new < arnew->ats_count)
540 while (c_old < arold->ats_count)
542 if (arold->ats[c_old].type == arnew->ats[c_new].type)
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "Found type %i, old value=%i new value=%i\n",
547 ntohl (arold->ats[c_old].type),
548 ntohl (arold->ats[c_old].value),
549 ntohl (arnew->ats[c_new].value));
551 arold->ats[c_old].value = arnew->ats[c_new].value;
557 if (found == GNUNET_NO)
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new type %i new value=%i\n",
561 ntohl (arnew->ats[c_new].type),
562 ntohl (arnew->ats[c_new].value));
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Old array size: %u\n",
566 GNUNET_array_grow (arold->ats, arold->ats_count, arold->ats_count + 1);
567 GNUNET_assert (arold->ats_count >= 2);
568 arold->ats[arold->ats_count - 2].type = arnew->ats[c_new].type;
569 arold->ats[arold->ats_count - 2].value = arnew->ats[c_new].value;
570 arold->ats[arold->ats_count - 1].type = htonl (0);
571 arold->ats[arold->ats_count - 1].value = htonl (0);
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New array size: %i\n",
580 if (GNUNET_YES == change)
581 update_bandwidth_assignment (usc->atc, arold);
589 * Create an allocation record with the given properties.
591 * @param plugin_name name of the currently used transport plugin
592 * @param session session in use (if available)
593 * @param plugin_addr address in use (if available)
594 * @param plugin_addr_len number of bytes in plugin_addr
595 * @param ats performance data for the connection
596 * @param ats_count number of performance records in 'ats'
598 static struct AllocationRecord *
599 create_allocation_record (const char *plugin_name, struct Session *session,
600 const void *plugin_addr, size_t plugin_addr_len,
601 const struct GNUNET_TRANSPORT_ATS_Information *ats,
604 struct AllocationRecord *ar;
606 ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
607 ar->plugin_name = GNUNET_strdup (plugin_name);
608 ar->plugin_addr = &ar[1];
609 memcpy (&ar[1], plugin_addr, plugin_addr_len);
610 ar->session = session;
611 ar->plugin_addr_len = plugin_addr_len;
612 GNUNET_assert (ats_count > 0);
613 GNUNET_array_grow (ar->ats, ar->ats_count, ats_count);
614 memcpy (ar->ats, ats,
615 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
621 * Mark all matching allocation records as not connected.
623 * @param cls 'struct GTS_AtsHandle'
624 * @param key identity of the peer associated with the record
625 * @param value the 'struct AllocationRecord' to clear the 'connected' flag
626 * @return GNUNET_OK (continue to iterate)
629 disconnect_peer (void *cls, const GNUNET_HashCode * key, void *value)
631 struct GNUNET_ATS_Handle *atc = cls;
632 struct AllocationRecord *ar = value;
634 if (GNUNET_YES == ar->connected)
636 ar->connected = GNUNET_NO;
637 update_bandwidth_assignment (atc, ar);
644 * We established a new connection with a peer (for example, because
645 * core asked for it or because the other peer connected to us).
646 * Calculate bandwidth assignments including the new peer.
649 * @param peer identity of the new peer
650 * @param plugin_name name of the currently used transport plugin
651 * @param session session in use (if available)
652 * @param plugin_addr address in use (if available)
653 * @param plugin_addr_len number of bytes in plugin_addr
654 * @param ats performance data for the connection
655 * @param ats_count number of performance records in 'ats'
658 GNUNET_ATS_peer_connect (struct GNUNET_ATS_Handle *atc,
659 const struct GNUNET_PeerIdentity *peer,
660 const char *plugin_name, struct Session *session,
661 const void *plugin_addr, size_t plugin_addr_len,
662 const struct GNUNET_TRANSPORT_ATS_Information *ats,
665 struct AllocationRecord *ar;
666 struct UpdateSessionContext usc;
668 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &disconnect_peer,
670 ar = create_allocation_record (plugin_name, session, plugin_addr,
671 plugin_addr_len, ats, ats_count);
672 ar->connected = GNUNET_YES;
676 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
678 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
681 GNUNET_assert (GNUNET_OK ==
682 GNUNET_CONTAINER_multihashmap_put (atc->peers,
683 &peer->hashPubKey, ar,
684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
689 * We disconnected from the given peer (for example, because ats, core
690 * or blacklist asked for it or because the other peer disconnected).
691 * Calculate bandwidth assignments without the peer.
694 * @param peer identity of the new peer
697 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc,
698 const struct GNUNET_PeerIdentity *peer)
700 (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
702 &disconnect_peer, atc);
707 * Closure for 'destroy_allocation_record'
709 struct SessionDestroyContext
714 struct GNUNET_ATS_Handle *atc;
717 * Session being destroyed.
719 const struct Session *session;
724 * Free an allocation record matching the given session.
726 * @param cls the 'struct SessionDestroyContext'
727 * @param key identity of the peer associated with the record
728 * @param value the 'struct AllocationRecord' to free
729 * @return GNUNET_OK (continue to iterate)
732 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
734 struct SessionDestroyContext *sdc = cls;
735 struct AllocationRecord *ar = value;
737 if (ar->session != sdc->session)
740 if (ar->plugin_addr != NULL)
742 GNUNET_assert (GNUNET_OK ==
743 GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers, key,
745 if (GNUNET_YES == ar->connected) ;
747 /* FIXME: is this supposed to be allowed? What to do then? */
750 destroy_allocation_record (NULL, key, ar);
756 * A session got destroyed, stop including it as a valid address.
759 * @param peer identity of the peer
760 * @param session session handle that is no longer valid
763 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc,
764 const struct GNUNET_PeerIdentity *peer,
765 const struct Session *session)
767 struct SessionDestroyContext sdc;
770 sdc.session = session;
771 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_session,
777 * Notify validation watcher that an entry is now valid
779 * @param cls 'struct ValidationEntry' that is now valid
780 * @param key peer identity (unused)
781 * @param value a 'GST_ValidationIteratorContext' to notify
782 * @return GNUNET_YES (continue to iterate)
785 notify_valid (void *cls, const GNUNET_HashCode * key, void *value)
787 struct AllocationRecord *ar = cls;
788 struct GNUNET_ATS_SuggestionContext *asc = value;
790 asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
791 ar->plugin_addr_len, ar->session,
792 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_out / 32),
793 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps_in / 32),
794 ar->ats, ar->ats_count);
795 GNUNET_ATS_suggest_address_cancel (asc);
802 * We have updated performance statistics for a given address. Note
803 * that this function can be called for addresses that are currently
804 * in use as well as addresses that are valid but not actively in use.
805 * Furthermore, the peer may not even be connected to us right now (in
806 * which case the call may be ignored or the information may be stored
807 * for later use). Update bandwidth assignments.
810 * @param peer identity of the peer
811 * @param valid_until how long is the address valid?
812 * @param plugin_name name of the transport plugin
813 * @param session session handle (if available)
814 * @param plugin_addr address (if available)
815 * @param plugin_addr_len number of bytes in plugin_addr
816 * @param ats performance data for the address
817 * @param ats_count number of performance records in 'ats'
820 GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc,
821 const struct GNUNET_PeerIdentity *peer,
822 struct GNUNET_TIME_Absolute valid_until,
823 const char *plugin_name, struct Session *session,
824 const void *plugin_addr, size_t plugin_addr_len,
825 const struct GNUNET_TRANSPORT_ATS_Information *ats,
828 struct AllocationRecord *ar;
829 struct UpdateSessionContext usc;
832 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
833 "Updating address for peer `%s', plugin `%s'\n",
834 GNUNET_i2s (peer), plugin_name);
836 ar = create_allocation_record (plugin_name, session, plugin_addr,
837 plugin_addr_len, ats, ats_count);
841 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
843 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
847 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
848 "Adding new address for peer `%s', plugin `%s'\n",
849 GNUNET_i2s (peer), plugin_name);
851 GNUNET_assert (GNUNET_OK ==
852 GNUNET_CONTAINER_multihashmap_put (atc->peers,
853 &peer->hashPubKey, ar,
854 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
855 GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
856 &peer->hashPubKey, ¬ify_valid,
860 /* end of file gnunet-service-transport_ats.c */