2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 GNUnet e.V.
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
30 * @see [Documentation](https://gnunet.org/ats-subsystem)
34 #ifndef GNUNET_ATS_SERVICE_H
35 #define GNUNET_ATS_SERVICE_H
37 #include "gnunet_constants.h"
38 #include "gnunet_util_lib.h"
39 #include "gnunet_hello_lib.h"
42 * Types of networks (with separate quotas) we support.
44 enum GNUNET_ATS_Network_Type
47 * Category of last resort.
49 GNUNET_ATS_NET_UNSPECIFIED = 0,
52 * Loopback (same host).
54 GNUNET_ATS_NET_LOOPBACK = 1,
59 GNUNET_ATS_NET_LAN = 2,
62 * Wide area network (i.e. Internet)
64 GNUNET_ATS_NET_WAN = 3,
67 * Wireless LAN (i.e. 802.11abgn)
69 GNUNET_ATS_NET_WLAN = 4,
74 GNUNET_ATS_NET_BT = 5,
77 * Number of network types supported by ATS
79 #define GNUNET_ATS_NetworkTypeCount 6
85 * Default bandwidth assigned to a network : 64 KB/s
87 #define GNUNET_ATS_DefaultBandwidth 65536
90 * Undefined value for an `enum GNUNET_ATS_Property`
92 #define GNUNET_ATS_VALUE_UNDEFINED UINT32_MAX
95 * String representation for GNUNET_ATS_VALUE_UNDEFINED
97 #define GNUNET_ATS_VALUE_UNDEFINED_STR "undefined"
100 * Maximum bandwidth assigned to a network : 4095 MB/s
102 #define GNUNET_ATS_MaxBandwidth UINT32_MAX
105 * Textual equivalent for GNUNET_ATS_MaxBandwidth
107 #define GNUNET_ATS_MaxBandwidthString "unlimited"
111 * ATS performance characteristics for an address.
113 struct GNUNET_ATS_Properties
117 * Delay. Time between when the time packet is sent and the packet
118 * arrives. FOREVER if we did not measure yet.
120 struct GNUNET_TIME_Relative delay;
123 * Actual traffic on this connection from this peer to the other peer.
124 * Includes transport overhead.
126 * Unit: [bytes/second]
128 uint32_t utilization_out;
131 * Actual traffic on this connection from the other peer to this peer.
132 * Includes transport overhead.
134 * Unit: [bytes/second]
136 uint32_t utilization_in;
139 * Distance on network layer (required for distance-vector routing)
140 * in hops. Zero for direct connections (i.e. plain TCP/UDP).
142 unsigned int distance;
145 * Which network scope does the respective address belong to?
146 * This property does not change.
148 enum GNUNET_ATS_Network_Type scope;
154 * ATS performance characteristics for an address in
155 * network byte order (for IPC).
157 struct GNUNET_ATS_PropertiesNBO
161 * Actual traffic on this connection from this peer to the other peer.
162 * Includes transport overhead.
164 * Unit: [bytes/second]
166 uint32_t utilization_out GNUNET_PACKED;
169 * Actual traffic on this connection from the other peer to this peer.
170 * Includes transport overhead.
172 * Unit: [bytes/second]
174 uint32_t utilization_in GNUNET_PACKED;
177 * Which network scope does the respective address belong to?
178 * This property does not change.
180 uint32_t scope GNUNET_PACKED;
183 * Distance on network layer (required for distance-vector routing)
184 * in hops. Zero for direct connections (i.e. plain TCP/UDP).
186 uint32_t distance GNUNET_PACKED;
189 * Delay. Time between when the time packet is sent and the packet
190 * arrives. FOREVER if we did not measure yet.
192 struct GNUNET_TIME_RelativeNBO delay;
198 /* ********************* LAN Characterization library ************************ */
199 /* Note: these functions do not really communicate with the ATS service */
203 * Convert ATS properties from host to network byte order.
205 * @param nbo[OUT] value written
206 * @param hbo value read
209 GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
210 const struct GNUNET_ATS_Properties *hbo);
214 * Convert ATS properties from network to host byte order.
216 * @param hbo[OUT] value written
217 * @param nbo value read
220 GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
221 const struct GNUNET_ATS_PropertiesNBO *nbo);
226 * Convert a `enum GNUNET_ATS_Network_Type` to a string
228 * @param net the network type
229 * @return a string or NULL if invalid
232 GNUNET_ATS_print_network_type (enum GNUNET_ATS_Network_Type net);
236 * Handle for the LAN Characterization library.
238 struct GNUNET_ATS_InterfaceScanner;
242 * Returns where the address is located: loopback, LAN or WAN.
244 * @param is handle from #GNUNET_ATS_interface_scanner_init()
245 * @param addr address
246 * @param addrlen address length
247 * @return type of the network the address belongs to
249 enum GNUNET_ATS_Network_Type
250 GNUNET_ATS_scanner_address_get_type (struct GNUNET_ATS_InterfaceScanner *is,
251 const struct sockaddr *addr,
256 * Initialize the ATS address characterization client handle.
258 * @return scanner handle, NULL on error
260 struct GNUNET_ATS_InterfaceScanner *
261 GNUNET_ATS_scanner_init (void);
265 * Terminate interface scanner.
267 * @param is scanner we are done with
270 GNUNET_ATS_scanner_done (struct GNUNET_ATS_InterfaceScanner *is);
274 /* ********************Connection Suggestion API ***************************** */
277 * Handle to the ATS subsystem for making suggestions about
278 * connections the peer would like to have.
280 struct GNUNET_ATS_ConnectivityHandle;
283 * Handle for address suggestion requests.
285 struct GNUNET_ATS_ConnectivitySuggestHandle;
289 * Initialize the ATS connectivity suggestion client handle.
291 * @param cfg configuration to use
292 * @return ats connectivity handle, NULL on error
294 struct GNUNET_ATS_ConnectivityHandle *
295 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
299 * Shutdown ATS connectivity suggestion client.
301 * @param ch handle to destroy
304 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch);
308 * We would like to establish a new connection with a peer. ATS
309 * should suggest a good address to begin with.
312 * @param peer identity of the peer we need an address for
313 * @param strength how urgent is the need for such a suggestion
314 * @return suggestion handle, NULL if request is already pending
316 struct GNUNET_ATS_ConnectivitySuggestHandle *
317 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
318 const struct GNUNET_PeerIdentity *peer,
323 * We no longer care about being connected to a peer.
328 GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh);
331 /* ******************************** Scheduling API ***************************** */
334 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
336 struct GNUNET_ATS_SchedulingHandle;
339 * Opaque session handle, defined by plugins. Contents not known to ATS.
341 struct GNUNET_ATS_Session;
345 * Signature of a function called by ATS with the current bandwidth
346 * and address preferences as determined by ATS. If our connection
347 * to ATS dies and thus all suggestions become invalid, this function
348 * is called ONCE with all arguments (except @a cls) being NULL/0.
351 * @param peer for which we suggest an address, NULL if ATS connection died
352 * @param address suggested address (including peer identity of the peer),
353 * may be NULL to signal disconnect from peer
354 * @param session session to use, NULL to establish a new outgoing session
355 * @param bandwidth_out assigned outbound bandwidth for the connection,
356 * 0 to signal disconnect
357 * @param bandwidth_in assigned inbound bandwidth for the connection,
358 * 0 to signal disconnect
361 (*GNUNET_ATS_AddressSuggestionCallback) (void *cls,
362 const struct GNUNET_PeerIdentity *peer,
363 const struct GNUNET_HELLO_Address *address,
364 struct GNUNET_ATS_Session *session,
365 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
366 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in);
370 * Initialize the ATS scheduling subsystem.
372 * @param cfg configuration to use
373 * @param suggest_cb notification to call whenever the suggestation changed
374 * @param suggest_cb_cls closure for @a suggest_cb
375 * @return ats context
377 struct GNUNET_ATS_SchedulingHandle *
378 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
379 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
380 void *suggest_cb_cls);
384 * Client is done with ATS scheduling, release resources.
386 * @param sh handle to release
389 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh);
393 * Handle used within ATS to track an address.
395 struct GNUNET_ATS_AddressRecord;
399 * We have a new address ATS should know. Addresses have to be added with this
400 * function before they can be: updated, set in use and destroyed
403 * @param address the address
404 * @param session session handle (if available, i.e. for incoming connections)
405 * @param prop performance data for the address
406 * @return handle to the address representation inside ATS, NULL
407 * on error (i.e. ATS knows this exact address already, or
408 * address is invalid)
410 struct GNUNET_ATS_AddressRecord *
411 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
412 const struct GNUNET_HELLO_Address *address,
413 struct GNUNET_ATS_Session *session,
414 const struct GNUNET_ATS_Properties *prop);
418 * An address was used to initiate a session.
420 * @param ar address record to update information for
421 * @param session session handle
424 GNUNET_ATS_address_add_session (struct GNUNET_ATS_AddressRecord *ar,
425 struct GNUNET_ATS_Session *session);
429 * A @a session was destroyed, disassociate it from the given address
430 * record. If this was an incoming addess, destroys the address as
433 * @param ar address record to update information for
434 * @param session session handle
435 * @return #GNUNET_YES if the @a ar was destroyed because
436 * it was an incoming address,
437 * #GNUNET_NO if the @ar was kept because we can
438 * use it still to establish a new session
441 GNUNET_ATS_address_del_session (struct GNUNET_ATS_AddressRecord *ar,
442 struct GNUNET_ATS_Session *session);
446 * We have updated performance statistics for a given address. Note
447 * that this function can be called for addresses that are currently
448 * in use as well as addresses that are valid but not actively in use.
449 * Furthermore, the peer may not even be connected to us right now (@a
450 * session value of NULL used to signal disconnect, or somehow we
451 * otherwise got updated on @a ats information). Based on the
452 * information provided, ATS may update bandwidth assignments and
453 * suggest to switch addresses.
455 * @param ar address record to update information for
456 * @param prop performance data for the address
459 GNUNET_ATS_address_update (struct GNUNET_ATS_AddressRecord *ar,
460 const struct GNUNET_ATS_Properties *prop);
464 * An address got destroyed, stop using it as a valid address.
466 * @param ar address record to destroy, it's validation has
467 * expired and ATS may no longer use it
470 GNUNET_ATS_address_destroy (struct GNUNET_ATS_AddressRecord *ar);
474 /* ******************************** Performance API ***************************** */
477 * ATS Handle to obtain and/or modify performance information.
479 struct GNUNET_ATS_PerformanceHandle;
482 * Signature of a function that is called with QoS information about an address.
485 * @param address the address, NULL if ATS service was disconnected or
486 * when the iteration is completed in the case of
487 * #GNUNET_ATS_performance_list_addresses()
488 * @param address_active #GNUNET_YES if this address is actively used
489 * to maintain a connection to a peer;
490 * #GNUNET_NO if the address is not actively used;
491 * #GNUNET_SYSERR if this address is no longer available for ATS
492 * @param bandwidth_out assigned outbound bandwidth for the connection
493 * @param bandwidth_in assigned inbound bandwidth for the connection
494 * @param prop performance data for the address
497 (*GNUNET_ATS_AddressInformationCallback) (void *cls,
498 const struct GNUNET_HELLO_Address *address,
500 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
501 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
502 const struct GNUNET_ATS_Properties *prop);
506 * Handle for an address listing operation
508 struct GNUNET_ATS_AddressListHandle;
512 * Get handle to access performance API of the ATS subsystem.
514 * @param cfg configuration to use
515 * @param addr_info_cb callback called when performance characteristics for
517 * @param addr_info_cb_cls closure for @a addr_info_cb
518 * @return ats performance context
520 struct GNUNET_ATS_PerformanceHandle *
521 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
522 GNUNET_ATS_AddressInformationCallback addr_info_cb,
523 void *addr_info_cb_cls);
527 * Get information about addresses known to the ATS subsystem.
529 * @param ph the performance handle to use
530 * @param peer peer idm can be NULL for all peers
531 * @param all #GNUNET_YES to get information about all addresses or #GNUNET_NO to
532 * get only address currently used
533 * @param infocb callback to call with the addresses,
534 * will callback with address == NULL when done
535 * @param infocb_cls closure for @a infocb
536 * @return handle to abort the operation
538 struct GNUNET_ATS_AddressListHandle *
539 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *ph,
540 const struct GNUNET_PeerIdentity *peer,
542 GNUNET_ATS_AddressInformationCallback infocb,
547 * Cancel a pending address listing operation
549 * @param alh the `struct GNUNET_ATS_AddressListHandle` handle to cancel
552 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *alh);
556 * Client is done using the ATS performance subsystem, release resources.
561 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph);
565 * Function called with reservation result.
568 * @param peer identifies the peer
569 * @param amount set to the amount that was actually reserved or unreserved;
570 * either the full requested amount or zero (no partial reservations)
571 * @param res_delay if the reservation could not be satisfied (amount was 0), how
572 * long should the client wait until re-trying?
575 (*GNUNET_ATS_ReservationCallback) (void *cls,
576 const struct GNUNET_PeerIdentity *peer,
578 struct GNUNET_TIME_Relative res_delay);
582 * Context that can be used to cancel a peer information request.
584 struct GNUNET_ATS_ReservationContext;
588 * Reserve inbound bandwidth from the given peer. ATS will look at
589 * the current amount of traffic we receive from the peer and ensure
590 * that the peer could add 'amount' of data to its stream.
592 * @param ph performance handle
593 * @param peer identifies the peer
594 * @param amount reserve N bytes for receiving, negative
595 * amounts can be used to undo a (recent) reservation;
596 * @param rcb function to call with the resulting reservation information
597 * @param rcb_cls closure for @a rcb
598 * @return NULL on error
599 * @deprecated will be replaced soon
601 struct GNUNET_ATS_ReservationContext *
602 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
603 const struct GNUNET_PeerIdentity *peer,
605 GNUNET_ATS_ReservationCallback rcb,
610 * Cancel request for reserving bandwidth.
612 * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
615 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc);
619 * ATS preference types as array initializer
621 #define GNUNET_ATS_PreferenceType {GNUNET_ATS_PREFERENCE_BANDWIDTH, GNUNET_ATS_PREFERENCE_LATENCY, GNUNET_ATS_PREFERENCE_END}
624 * ATS preference types as string array initializer
626 #define GNUNET_ATS_PreferenceTypeString {"BANDWIDTH", "LATENCY", "END" }
629 * Enum defining all known preference categories.
631 enum GNUNET_ATS_PreferenceKind
635 * Change the peer's bandwidth value (value per byte of bandwidth in
636 * the goal function) to the given amount. The argument is followed
637 * by a double value giving the desired value (can be negative).
638 * Preference changes are forgotten if peers disconnect.
640 GNUNET_ATS_PREFERENCE_BANDWIDTH = 0,
643 * Change the peer's latency value to the given amount. The
644 * argument is followed by a double value giving the desired value
645 * (can be negative). The absolute score in the goal function is
646 * the inverse of the latency in microseconds (minimum: 1
647 * microsecond) multiplied by the latency preferences.
649 GNUNET_ATS_PREFERENCE_LATENCY = 1,
652 * End of preference list.
654 GNUNET_ATS_PREFERENCE_END = 2
660 * Convert a GNUNET_ATS_PreferenceType to a string
662 * @param type the preference type
663 * @return a string or NULL if invalid
666 GNUNET_ATS_print_preference_type (enum GNUNET_ATS_PreferenceKind type);
670 * Change preferences for the given peer. Preference changes are forgotten if peers
673 * @param ph performance handle @param peer identifies the peer
674 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the
678 GNUNET_ATS_performance_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
679 const struct GNUNET_PeerIdentity *peer,
684 * Application feedback on how good preference requirements are fulfilled
685 * for the preferences included in the given time scope [now - scope .. now]
687 * An application notifies ATS if (and only if) it has feedback information
688 * for specific properties. This values are valid until the feedback scores are
689 * updated by the application.
691 * If the application has no feedback for this preference kind the application
692 * will not explicitly call for this property and will not include it in this
695 * @param ph performance handle
696 * @param scope the time interval this valid for: [now - scope .. now]
697 * @param peer identifies the peer
698 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
701 GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
702 const struct GNUNET_PeerIdentity *peer,
703 const struct GNUNET_TIME_Relative scope,
708 /** @} */ /* end of group */
710 /* end of file gnunet-service-transport_ats.h */