2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * Automatic transport selection and outbound bandwidth determination
24 * @author Christian Grothoff
25 * @author Matthias Wachs
27 * @defgroup ats ATS service
28 * Automatic Transport Selection and outbound bandwidth determination
31 #ifndef GNUNET_ATS_SERVICE_H
32 #define GNUNET_ATS_SERVICE_H
34 #include "gnunet_constants.h"
35 #include "gnunet_util_lib.h"
36 #include "gnunet_hello_lib.h"
39 * Types of networks (with separate quotas) we support.
41 enum GNUNET_ATS_Network_Type
44 * Category of last resort.
46 GNUNET_ATS_NET_UNSPECIFIED = 0,
49 * Loopback (same host).
51 GNUNET_ATS_NET_LOOPBACK = 1,
56 GNUNET_ATS_NET_LAN = 2,
59 * Wide area network (i.e. Internet)
61 GNUNET_ATS_NET_WAN = 3,
64 * Wireless LAN (i.e. 802.11abgn)
66 GNUNET_ATS_NET_WLAN = 4,
74 * Number of network types supported by ATS
76 #define GNUNET_ATS_NetworkTypeCount 6
82 * Default bandwidth assigned to a network : 64 KB/s
84 #define GNUNET_ATS_DefaultBandwidth 65536
87 * Undefined value for an `enum GNUNET_ATS_Property`
89 #define GNUNET_ATS_VALUE_UNDEFINED UINT32_MAX
92 * String representation for GNUNET_ATS_VALUE_UNDEFINED
94 #define GNUNET_ATS_VALUE_UNDEFINED_STR "undefined"
97 * Maximum bandwidth assigned to a network : 4095 MB/s
99 #define GNUNET_ATS_MaxBandwidth UINT32_MAX
102 * Textual equivalent for GNUNET_ATS_MaxBandwidth
104 #define GNUNET_ATS_MaxBandwidthString "unlimited"
108 * ATS performance characteristics for an address.
110 struct GNUNET_ATS_Properties
114 * Delay. Time between when the time packet is sent and the packet
115 * arrives. FOREVER if we did not measure yet.
117 struct GNUNET_TIME_Relative delay;
120 * Actual traffic on this connection from this peer to the other peer.
121 * Includes transport overhead.
123 * Unit: [bytes/second]
125 uint32_t utilization_out;
128 * Actual traffic on this connection from the other peer to this peer.
129 * Includes transport overhead.
131 * Unit: [bytes/second]
133 uint32_t utilization_in;
136 * Distance on network layer (required for distance-vector routing)
137 * in hops. Zero for direct connections (i.e. plain TCP/UDP).
139 unsigned int distance;
142 * Which network scope does the respective address belong to?
143 * This property does not change.
145 enum GNUNET_ATS_Network_Type scope;
151 * ATS performance characteristics for an address in
152 * network byte order (for IPC).
154 struct GNUNET_ATS_PropertiesNBO
158 * Actual traffic on this connection from this peer to the other peer.
159 * Includes transport overhead.
161 * Unit: [bytes/second]
163 uint32_t utilization_out GNUNET_PACKED;
166 * Actual traffic on this connection from the other peer to this peer.
167 * Includes transport overhead.
169 * Unit: [bytes/second]
171 uint32_t utilization_in GNUNET_PACKED;
174 * Which network scope does the respective address belong to?
175 * This property does not change.
177 uint32_t scope GNUNET_PACKED;
180 * Distance on network layer (required for distance-vector routing)
181 * in hops. Zero for direct connections (i.e. plain TCP/UDP).
183 uint32_t distance GNUNET_PACKED;
186 * Delay. Time between when the time packet is sent and the packet
187 * arrives. FOREVER if we did not measure yet.
189 struct GNUNET_TIME_RelativeNBO delay;
195 /* ********************* LAN Characterization library ************************ */
196 /* Note: these functions do not really communicate with the ATS service */
200 * Convert ATS properties from host to network byte order.
202 * @param nbo[OUT] value written
203 * @param hbo value read
206 GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
207 const struct GNUNET_ATS_Properties *hbo);
211 * Convert ATS properties from network to host byte order.
213 * @param hbo[OUT] value written
214 * @param nbo value read
217 GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
218 const struct GNUNET_ATS_PropertiesNBO *nbo);
223 * Convert a `enum GNUNET_ATS_Network_Type` to a string
225 * @param net the network type
226 * @return a string or NULL if invalid
229 GNUNET_ATS_print_network_type (enum GNUNET_ATS_Network_Type net);
233 * Handle for the LAN Characterization library.
235 struct GNUNET_ATS_InterfaceScanner;
239 * Returns where the address is located: loopback, LAN or WAN.
241 * @param is handle from #GNUNET_ATS_interface_scanner_init()
242 * @param addr address
243 * @param addrlen address length
244 * @return type of the network the address belongs to
246 enum GNUNET_ATS_Network_Type
247 GNUNET_ATS_scanner_address_get_type (struct GNUNET_ATS_InterfaceScanner *is,
248 const struct sockaddr *addr,
253 * Initialize the ATS address characterization client handle.
255 * @return scanner handle, NULL on error
257 struct GNUNET_ATS_InterfaceScanner *
258 GNUNET_ATS_scanner_init (void);
262 * Terminate interface scanner.
264 * @param is scanner we are done with
267 GNUNET_ATS_scanner_done (struct GNUNET_ATS_InterfaceScanner *is);
271 /* ********************Connection Suggestion API ***************************** */
274 * Handle to the ATS subsystem for making suggestions about
275 * connections the peer would like to have.
277 struct GNUNET_ATS_ConnectivityHandle;
280 * Handle for address suggestion requests.
282 struct GNUNET_ATS_ConnectivitySuggestHandle;
286 * Initialize the ATS connectivity suggestion client handle.
288 * @param cfg configuration to use
289 * @return ats connectivity handle, NULL on error
291 struct GNUNET_ATS_ConnectivityHandle *
292 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
296 * Shutdown ATS connectivity suggestion client.
298 * @param ch handle to destroy
301 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch);
305 * We would like to establish a new connection with a peer. ATS
306 * should suggest a good address to begin with.
309 * @param peer identity of the peer we need an address for
310 * @param strength how urgent is the need for such a suggestion
311 * @return suggestion handle, NULL if request is already pending
313 struct GNUNET_ATS_ConnectivitySuggestHandle *
314 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
315 const struct GNUNET_PeerIdentity *peer,
320 * We no longer care about being connected to a peer.
325 GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh);
328 /* ******************************** Scheduling API ***************************** */
331 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
333 struct GNUNET_ATS_SchedulingHandle;
336 * Opaque session handle, defined by plugins. Contents not known to ATS.
338 struct GNUNET_ATS_Session;
342 * Signature of a function called by ATS with the current bandwidth
343 * and address preferences as determined by ATS. If our connection
344 * to ATS dies and thus all suggestions become invalid, this function
345 * is called ONCE with all arguments (except @a cls) being NULL/0.
348 * @param peer for which we suggest an address, NULL if ATS connection died
349 * @param address suggested address (including peer identity of the peer),
350 * may be NULL to signal disconnect from peer
351 * @param session session to use, NULL to establish a new outgoing session
352 * @param bandwidth_out assigned outbound bandwidth for the connection,
353 * 0 to signal disconnect
354 * @param bandwidth_in assigned inbound bandwidth for the connection,
355 * 0 to signal disconnect
358 (*GNUNET_ATS_AddressSuggestionCallback) (void *cls,
359 const struct GNUNET_PeerIdentity *peer,
360 const struct GNUNET_HELLO_Address *address,
361 struct GNUNET_ATS_Session *session,
362 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
363 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in);
367 * Initialize the ATS scheduling subsystem.
369 * @param cfg configuration to use
370 * @param suggest_cb notification to call whenever the suggestation changed
371 * @param suggest_cb_cls closure for @a suggest_cb
372 * @return ats context
374 struct GNUNET_ATS_SchedulingHandle *
375 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
376 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
377 void *suggest_cb_cls);
381 * Client is done with ATS scheduling, release resources.
383 * @param sh handle to release
386 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh);
390 * Handle used within ATS to track an address.
392 struct GNUNET_ATS_AddressRecord;
396 * We have a new address ATS should know. Addresses have to be added with this
397 * function before they can be: updated, set in use and destroyed
400 * @param address the address
401 * @param session session handle (if available, i.e. for incoming connections)
402 * @param prop performance data for the address
403 * @return handle to the address representation inside ATS, NULL
404 * on error (i.e. ATS knows this exact address already, or
405 * address is invalid)
407 struct GNUNET_ATS_AddressRecord *
408 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
409 const struct GNUNET_HELLO_Address *address,
410 struct GNUNET_ATS_Session *session,
411 const struct GNUNET_ATS_Properties *prop);
415 * An address was used to initiate a session.
417 * @param ar address record to update information for
418 * @param session session handle
421 GNUNET_ATS_address_add_session (struct GNUNET_ATS_AddressRecord *ar,
422 struct GNUNET_ATS_Session *session);
426 * A @a session was destroyed, disassociate it from the given address
427 * record. If this was an incoming addess, destroys the address as
430 * @param ar address record to update information for
431 * @param session session handle
432 * @return #GNUNET_YES if the @a ar was destroyed because
433 * it was an incoming address,
434 * #GNUNET_NO if the @ar was kept because we can
435 * use it still to establish a new session
438 GNUNET_ATS_address_del_session (struct GNUNET_ATS_AddressRecord *ar,
439 struct GNUNET_ATS_Session *session);
443 * We have updated performance statistics for a given address. Note
444 * that this function can be called for addresses that are currently
445 * in use as well as addresses that are valid but not actively in use.
446 * Furthermore, the peer may not even be connected to us right now (@a
447 * session value of NULL used to signal disconnect, or somehow we
448 * otherwise got updated on @a ats information). Based on the
449 * information provided, ATS may update bandwidth assignments and
450 * suggest to switch addresses.
452 * @param ar address record to update information for
453 * @param prop performance data for the address
456 GNUNET_ATS_address_update (struct GNUNET_ATS_AddressRecord *ar,
457 const struct GNUNET_ATS_Properties *prop);
461 * An address got destroyed, stop using it as a valid address.
463 * @param ar address record to destroy, it's validation has
464 * expired and ATS may no longer use it
467 GNUNET_ATS_address_destroy (struct GNUNET_ATS_AddressRecord *ar);
471 /* ******************************** Performance API ***************************** */
474 * ATS Handle to obtain and/or modify performance information.
476 struct GNUNET_ATS_PerformanceHandle;
479 * Signature of a function that is called with QoS information about an address.
482 * @param address the address, NULL if ATS service was disconnected or
483 * when the iteration is completed in the case of
484 * #GNUNET_ATS_performance_list_addresses()
485 * @param address_active #GNUNET_YES if this address is actively used
486 * to maintain a connection to a peer;
487 * #GNUNET_NO if the address is not actively used;
488 * #GNUNET_SYSERR if this address is no longer available for ATS
489 * @param bandwidth_out assigned outbound bandwidth for the connection
490 * @param bandwidth_in assigned inbound bandwidth for the connection
491 * @param prop performance data for the address
494 (*GNUNET_ATS_AddressInformationCallback) (void *cls,
495 const struct GNUNET_HELLO_Address *address,
497 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
498 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
499 const struct GNUNET_ATS_Properties *prop);
503 * Handle for an address listing operation
505 struct GNUNET_ATS_AddressListHandle;
509 * Get handle to access performance API of the ATS subsystem.
511 * @param cfg configuration to use
512 * @param addr_info_cb callback called when performance characteristics for
514 * @param addr_info_cb_cls closure for @a addr_info_cb
515 * @return ats performance context
517 struct GNUNET_ATS_PerformanceHandle *
518 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
519 GNUNET_ATS_AddressInformationCallback addr_info_cb,
520 void *addr_info_cb_cls);
524 * Get information about addresses known to the ATS subsystem.
526 * @param handle the performance handle to use
527 * @param peer peer idm can be NULL for all peers
528 * @param all #GNUNET_YES to get information about all addresses or #GNUNET_NO to
529 * get only address currently used
530 * @param infocb callback to call with the addresses,
531 * will callback with address == NULL when done
532 * @param infocb_cls closure for @a infocb
533 * @return handle to abort the operation
535 struct GNUNET_ATS_AddressListHandle *
536 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *handle,
537 const struct GNUNET_PeerIdentity *peer,
539 GNUNET_ATS_AddressInformationCallback infocb,
544 * Cancel a pending address listing operation
546 * @param handle the `struct GNUNET_ATS_AddressListHandle` handle to cancel
549 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *handle);
553 * Client is done using the ATS performance subsystem, release resources.
558 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph);
562 * Function called with reservation result.
565 * @param peer identifies the peer
566 * @param amount set to the amount that was actually reserved or unreserved;
567 * either the full requested amount or zero (no partial reservations)
568 * @param res_delay if the reservation could not be satisfied (amount was 0), how
569 * long should the client wait until re-trying?
572 (*GNUNET_ATS_ReservationCallback) (void *cls,
573 const struct GNUNET_PeerIdentity *peer,
575 struct GNUNET_TIME_Relative res_delay);
579 * Context that can be used to cancel a peer information request.
581 struct GNUNET_ATS_ReservationContext;
585 * Reserve inbound bandwidth from the given peer. ATS will look at
586 * the current amount of traffic we receive from the peer and ensure
587 * that the peer could add 'amount' of data to its stream.
589 * @param ph performance handle
590 * @param peer identifies the peer
591 * @param amount reserve N bytes for receiving, negative
592 * amounts can be used to undo a (recent) reservation;
593 * @param rcb function to call with the resulting reservation information
594 * @param rcb_cls closure for @a rcb
595 * @return NULL on error
596 * @deprecated will be replaced soon
598 struct GNUNET_ATS_ReservationContext *
599 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
600 const struct GNUNET_PeerIdentity *peer,
602 GNUNET_ATS_ReservationCallback rcb,
607 * Cancel request for reserving bandwidth.
609 * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
612 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc);
616 * ATS preference types as array initializer
618 #define GNUNET_ATS_PreferenceType {GNUNET_ATS_PREFERENCE_BANDWIDTH, GNUNET_ATS_PREFERENCE_LATENCY, GNUNET_ATS_PREFERENCE_END}
621 * ATS preference types as string array initializer
623 #define GNUNET_ATS_PreferenceTypeString {"BANDWIDTH", "LATENCY", "END" }
626 * Enum defining all known preference categories.
628 enum GNUNET_ATS_PreferenceKind
632 * Change the peer's bandwidth value (value per byte of bandwidth in
633 * the goal function) to the given amount. The argument is followed
634 * by a double value giving the desired value (can be negative).
635 * Preference changes are forgotten if peers disconnect.
637 GNUNET_ATS_PREFERENCE_BANDWIDTH = 0,
640 * Change the peer's latency value to the given amount. The
641 * argument is followed by a double value giving the desired value
642 * (can be negative). The absolute score in the goal function is
643 * the inverse of the latency in microseconds (minimum: 1
644 * microsecond) multiplied by the latency preferences.
646 GNUNET_ATS_PREFERENCE_LATENCY = 1,
649 * End of preference list.
651 GNUNET_ATS_PREFERENCE_END = 2
657 * Convert a GNUNET_ATS_PreferenceType to a string
659 * @param type the preference type
660 * @return a string or NULL if invalid
663 GNUNET_ATS_print_preference_type (enum GNUNET_ATS_PreferenceKind type);
667 * Change preferences for the given peer. Preference changes are forgotten if peers
670 * @param ph performance handle @param peer identifies the peer
671 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the
675 GNUNET_ATS_performance_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
676 const struct GNUNET_PeerIdentity *peer,
681 * Application feedback on how good preference requirements are fulfilled
682 * for the preferences included in the given time scope [now - scope .. now]
684 * An application notifies ATS if (and only if) it has feedback information
685 * for specific properties. This values are valid until the feedback scores are
686 * updated by the application.
688 * If the application has no feedback for this preference kind the application
689 * will not explicitly call for this property and will not include it in this
692 * @param ph performance handle
693 * @param scope the time interval this valid for: [now - scope .. now]
694 * @param peer identifies the peer
695 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
698 GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
699 const struct GNUNET_PeerIdentity *peer,
700 const struct GNUNET_TIME_Relative scope,
705 /** @} */ /* end of group */
707 /* end of file gnunet-service-transport_ats.h */