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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Automatic transport selection and outbound bandwidth determination
22 * @author Christian Grothoff
23 * @author Matthias Wachs
25 * @defgroup ats ATS service
26 * Automatic Transport Selection and outbound bandwidth determination
28 * @see [Documentation](https://gnunet.org/ats-subsystem)
32 #ifndef GNUNET_ATS_SERVICE_H
33 #define GNUNET_ATS_SERVICE_H
35 #include "gnunet_constants.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_hello_lib.h"
40 * Types of networks (with separate quotas) we support.
42 enum GNUNET_ATS_Network_Type
45 * Category of last resort.
47 GNUNET_ATS_NET_UNSPECIFIED = 0,
50 * Loopback (same host).
52 GNUNET_ATS_NET_LOOPBACK = 1,
57 GNUNET_ATS_NET_LAN = 2,
60 * Wide area network (i.e. Internet)
62 GNUNET_ATS_NET_WAN = 3,
65 * Wireless LAN (i.e. 802.11abgn)
67 GNUNET_ATS_NET_WLAN = 4,
72 GNUNET_ATS_NET_BT = 5,
75 * Number of network types supported by ATS
77 #define GNUNET_ATS_NetworkTypeCount 6
83 * Default bandwidth assigned to a network : 64 KB/s
85 #define GNUNET_ATS_DefaultBandwidth 65536
88 * Undefined value for an `enum GNUNET_ATS_Property`
90 #define GNUNET_ATS_VALUE_UNDEFINED UINT32_MAX
93 * String representation for GNUNET_ATS_VALUE_UNDEFINED
95 #define GNUNET_ATS_VALUE_UNDEFINED_STR "undefined"
98 * Maximum bandwidth assigned to a network : 4095 MB/s
100 #define GNUNET_ATS_MaxBandwidth UINT32_MAX
103 * Textual equivalent for GNUNET_ATS_MaxBandwidth
105 #define GNUNET_ATS_MaxBandwidthString "unlimited"
109 * ATS performance characteristics for an address.
111 struct GNUNET_ATS_Properties
115 * Delay. Time between when the time packet is sent and the packet
116 * arrives. FOREVER if we did not measure yet.
118 struct GNUNET_TIME_Relative delay;
121 * Actual traffic on this connection from this peer to the other peer.
122 * Includes transport overhead.
124 * Unit: [bytes/second]
126 uint32_t utilization_out;
129 * Actual traffic on this connection from the other peer to this peer.
130 * Includes transport overhead.
132 * Unit: [bytes/second]
134 uint32_t utilization_in;
137 * Distance on network layer (required for distance-vector routing)
138 * in hops. Zero for direct connections (i.e. plain TCP/UDP).
140 unsigned int distance;
143 * Which network scope does the respective address belong to?
144 * This property does not change.
146 enum GNUNET_ATS_Network_Type scope;
152 * ATS performance characteristics for an address in
153 * network byte order (for IPC).
155 struct GNUNET_ATS_PropertiesNBO
159 * Actual traffic on this connection from this peer to the other peer.
160 * Includes transport overhead.
162 * Unit: [bytes/second]
164 uint32_t utilization_out GNUNET_PACKED;
167 * Actual traffic on this connection from the other peer to this peer.
168 * Includes transport overhead.
170 * Unit: [bytes/second]
172 uint32_t utilization_in GNUNET_PACKED;
175 * Which network scope does the respective address belong to?
176 * This property does not change.
178 uint32_t scope GNUNET_PACKED;
181 * Distance on network layer (required for distance-vector routing)
182 * in hops. Zero for direct connections (i.e. plain TCP/UDP).
184 uint32_t distance GNUNET_PACKED;
187 * Delay. Time between when the time packet is sent and the packet
188 * arrives. FOREVER if we did not measure yet.
190 struct GNUNET_TIME_RelativeNBO delay;
196 /* ********************* LAN Characterization library ************************ */
197 /* Note: these functions do not really communicate with the ATS service */
201 * Convert ATS properties from host to network byte order.
203 * @param nbo[OUT] value written
204 * @param hbo value read
207 GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
208 const struct GNUNET_ATS_Properties *hbo);
212 * Convert ATS properties from network to host byte order.
214 * @param hbo[OUT] value written
215 * @param nbo value read
218 GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
219 const struct GNUNET_ATS_PropertiesNBO *nbo);
224 * Convert a `enum GNUNET_ATS_Network_Type` to a string
226 * @param net the network type
227 * @return a string or NULL if invalid
230 GNUNET_ATS_print_network_type (enum GNUNET_ATS_Network_Type net);
234 * Handle for the LAN Characterization library.
236 struct GNUNET_ATS_InterfaceScanner;
240 * Returns where the address is located: loopback, LAN or WAN.
242 * @param is handle from #GNUNET_ATS_interface_scanner_init()
243 * @param addr address
244 * @param addrlen address length
245 * @return type of the network the address belongs to
247 enum GNUNET_ATS_Network_Type
248 GNUNET_ATS_scanner_address_get_type (struct GNUNET_ATS_InterfaceScanner *is,
249 const struct sockaddr *addr,
254 * Initialize the ATS address characterization client handle.
256 * @return scanner handle, NULL on error
258 struct GNUNET_ATS_InterfaceScanner *
259 GNUNET_ATS_scanner_init (void);
263 * Terminate interface scanner.
265 * @param is scanner we are done with
268 GNUNET_ATS_scanner_done (struct GNUNET_ATS_InterfaceScanner *is);
272 /* ********************Connection Suggestion API ***************************** */
275 * Handle to the ATS subsystem for making suggestions about
276 * connections the peer would like to have.
278 struct GNUNET_ATS_ConnectivityHandle;
281 * Handle for address suggestion requests.
283 struct GNUNET_ATS_ConnectivitySuggestHandle;
287 * Initialize the ATS connectivity suggestion client handle.
289 * @param cfg configuration to use
290 * @return ats connectivity handle, NULL on error
292 struct GNUNET_ATS_ConnectivityHandle *
293 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
297 * Shutdown ATS connectivity suggestion client.
299 * @param ch handle to destroy
302 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch);
306 * We would like to establish a new connection with a peer. ATS
307 * should suggest a good address to begin with.
310 * @param peer identity of the peer we need an address for
311 * @param strength how urgent is the need for such a suggestion
312 * @return suggestion handle, NULL if request is already pending
314 struct GNUNET_ATS_ConnectivitySuggestHandle *
315 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
316 const struct GNUNET_PeerIdentity *peer,
321 * We no longer care about being connected to a peer.
326 GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh);
329 /* ******************************** Scheduling API ***************************** */
332 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
334 struct GNUNET_ATS_SchedulingHandle;
337 * Opaque session handle, defined by plugins. Contents not known to ATS.
339 struct GNUNET_ATS_Session;
343 * Signature of a function called by ATS with the current bandwidth
344 * and address preferences as determined by ATS. If our connection
345 * to ATS dies and thus all suggestions become invalid, this function
346 * is called ONCE with all arguments (except @a cls) being NULL/0.
349 * @param peer for which we suggest an address, NULL if ATS connection died
350 * @param address suggested address (including peer identity of the peer),
351 * may be NULL to signal disconnect from peer
352 * @param session session to use, NULL to establish a new outgoing session
353 * @param bandwidth_out assigned outbound bandwidth for the connection,
354 * 0 to signal disconnect
355 * @param bandwidth_in assigned inbound bandwidth for the connection,
356 * 0 to signal disconnect
359 (*GNUNET_ATS_AddressSuggestionCallback) (void *cls,
360 const struct GNUNET_PeerIdentity *peer,
361 const struct GNUNET_HELLO_Address *address,
362 struct GNUNET_ATS_Session *session,
363 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
364 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in);
368 * Initialize the ATS scheduling subsystem.
370 * @param cfg configuration to use
371 * @param suggest_cb notification to call whenever the suggestation changed
372 * @param suggest_cb_cls closure for @a suggest_cb
373 * @return ats context
375 struct GNUNET_ATS_SchedulingHandle *
376 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
377 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
378 void *suggest_cb_cls);
382 * Client is done with ATS scheduling, release resources.
384 * @param sh handle to release
387 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh);
391 * Handle used within ATS to track an address.
393 struct GNUNET_ATS_AddressRecord;
397 * We have a new address ATS should know. Addresses have to be added with this
398 * function before they can be: updated, set in use and destroyed
401 * @param address the address
402 * @param session session handle (if available, i.e. for incoming connections)
403 * @param prop performance data for the address
404 * @return handle to the address representation inside ATS, NULL
405 * on error (i.e. ATS knows this exact address already, or
406 * address is invalid)
408 struct GNUNET_ATS_AddressRecord *
409 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
410 const struct GNUNET_HELLO_Address *address,
411 struct GNUNET_ATS_Session *session,
412 const struct GNUNET_ATS_Properties *prop);
416 * An address was used to initiate a session.
418 * @param ar address record to update information for
419 * @param session session handle
422 GNUNET_ATS_address_add_session (struct GNUNET_ATS_AddressRecord *ar,
423 struct GNUNET_ATS_Session *session);
427 * A @a session was destroyed, disassociate it from the given address
428 * record. If this was an incoming addess, destroys the address as
431 * @param ar address record to update information for
432 * @param session session handle
433 * @return #GNUNET_YES if the @a ar was destroyed because
434 * it was an incoming address,
435 * #GNUNET_NO if the @ar was kept because we can
436 * use it still to establish a new session
439 GNUNET_ATS_address_del_session (struct GNUNET_ATS_AddressRecord *ar,
440 struct GNUNET_ATS_Session *session);
444 * We have updated performance statistics for a given address. Note
445 * that this function can be called for addresses that are currently
446 * in use as well as addresses that are valid but not actively in use.
447 * Furthermore, the peer may not even be connected to us right now (@a
448 * session value of NULL used to signal disconnect, or somehow we
449 * otherwise got updated on @a ats information). Based on the
450 * information provided, ATS may update bandwidth assignments and
451 * suggest to switch addresses.
453 * @param ar address record to update information for
454 * @param prop performance data for the address
457 GNUNET_ATS_address_update (struct GNUNET_ATS_AddressRecord *ar,
458 const struct GNUNET_ATS_Properties *prop);
462 * An address got destroyed, stop using it as a valid address.
464 * @param ar address record to destroy, it's validation has
465 * expired and ATS may no longer use it
468 GNUNET_ATS_address_destroy (struct GNUNET_ATS_AddressRecord *ar);
472 /* ******************************** Performance API ***************************** */
475 * ATS Handle to obtain and/or modify performance information.
477 struct GNUNET_ATS_PerformanceHandle;
480 * Signature of a function that is called with QoS information about an address.
483 * @param address the address, NULL if ATS service was disconnected or
484 * when the iteration is completed in the case of
485 * #GNUNET_ATS_performance_list_addresses()
486 * @param address_active #GNUNET_YES if this address is actively used
487 * to maintain a connection to a peer;
488 * #GNUNET_NO if the address is not actively used;
489 * #GNUNET_SYSERR if this address is no longer available for ATS
490 * @param bandwidth_out assigned outbound bandwidth for the connection
491 * @param bandwidth_in assigned inbound bandwidth for the connection
492 * @param prop performance data for the address
495 (*GNUNET_ATS_AddressInformationCallback) (void *cls,
496 const struct GNUNET_HELLO_Address *address,
498 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
499 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
500 const struct GNUNET_ATS_Properties *prop);
504 * Handle for an address listing operation
506 struct GNUNET_ATS_AddressListHandle;
510 * Get handle to access performance API of the ATS subsystem.
512 * @param cfg configuration to use
513 * @param addr_info_cb callback called when performance characteristics for
515 * @param addr_info_cb_cls closure for @a addr_info_cb
516 * @return ats performance context
518 struct GNUNET_ATS_PerformanceHandle *
519 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
520 GNUNET_ATS_AddressInformationCallback addr_info_cb,
521 void *addr_info_cb_cls);
525 * Get information about addresses known to the ATS subsystem.
527 * @param ph the performance handle to use
528 * @param peer peer idm can be NULL for all peers
529 * @param all #GNUNET_YES to get information about all addresses or #GNUNET_NO to
530 * get only address currently used
531 * @param infocb callback to call with the addresses,
532 * will callback with address == NULL when done
533 * @param infocb_cls closure for @a infocb
534 * @return handle to abort the operation
536 struct GNUNET_ATS_AddressListHandle *
537 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *ph,
538 const struct GNUNET_PeerIdentity *peer,
540 GNUNET_ATS_AddressInformationCallback infocb,
545 * Cancel a pending address listing operation
547 * @param alh the `struct GNUNET_ATS_AddressListHandle` handle to cancel
550 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *alh);
554 * Client is done using the ATS performance subsystem, release resources.
559 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph);
563 * Function called with reservation result.
566 * @param peer identifies the peer
567 * @param amount set to the amount that was actually reserved or unreserved;
568 * either the full requested amount or zero (no partial reservations)
569 * @param res_delay if the reservation could not be satisfied (amount was 0), how
570 * long should the client wait until re-trying?
573 (*GNUNET_ATS_ReservationCallback) (void *cls,
574 const struct GNUNET_PeerIdentity *peer,
576 struct GNUNET_TIME_Relative res_delay);
580 * Context that can be used to cancel a peer information request.
582 struct GNUNET_ATS_ReservationContext;
586 * Reserve inbound bandwidth from the given peer. ATS will look at
587 * the current amount of traffic we receive from the peer and ensure
588 * that the peer could add 'amount' of data to its stream.
590 * @param ph performance handle
591 * @param peer identifies the peer
592 * @param amount reserve N bytes for receiving, negative
593 * amounts can be used to undo a (recent) reservation;
594 * @param rcb function to call with the resulting reservation information
595 * @param rcb_cls closure for @a rcb
596 * @return NULL on error
597 * @deprecated will be replaced soon
599 struct GNUNET_ATS_ReservationContext *
600 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
601 const struct GNUNET_PeerIdentity *peer,
603 GNUNET_ATS_ReservationCallback rcb,
608 * Cancel request for reserving bandwidth.
610 * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
613 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc);
617 * ATS preference types as array initializer
619 #define GNUNET_ATS_PreferenceType {GNUNET_ATS_PREFERENCE_BANDWIDTH, GNUNET_ATS_PREFERENCE_LATENCY, GNUNET_ATS_PREFERENCE_END}
622 * ATS preference types as string array initializer
624 #define GNUNET_ATS_PreferenceTypeString {"BANDWIDTH", "LATENCY", "END" }
627 * Enum defining all known preference categories.
629 enum GNUNET_ATS_PreferenceKind
633 * Change the peer's bandwidth value (value per byte of bandwidth in
634 * the goal function) to the given amount. The argument is followed
635 * by a double value giving the desired value (can be negative).
636 * Preference changes are forgotten if peers disconnect.
638 GNUNET_ATS_PREFERENCE_BANDWIDTH = 0,
641 * Change the peer's latency value to the given amount. The
642 * argument is followed by a double value giving the desired value
643 * (can be negative). The absolute score in the goal function is
644 * the inverse of the latency in microseconds (minimum: 1
645 * microsecond) multiplied by the latency preferences.
647 GNUNET_ATS_PREFERENCE_LATENCY = 1,
650 * End of preference list.
652 GNUNET_ATS_PREFERENCE_END = 2
658 * Convert a GNUNET_ATS_PreferenceType to a string
660 * @param type the preference type
661 * @return a string or NULL if invalid
664 GNUNET_ATS_print_preference_type (enum GNUNET_ATS_PreferenceKind type);
668 * Change preferences for the given peer. Preference changes are forgotten if peers
671 * @param ph performance handle @param peer identifies the peer
672 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the
676 GNUNET_ATS_performance_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
677 const struct GNUNET_PeerIdentity *peer,
682 * Application feedback on how good preference requirements are fulfilled
683 * for the preferences included in the given time scope [now - scope .. now]
685 * An application notifies ATS if (and only if) it has feedback information
686 * for specific properties. This values are valid until the feedback scores are
687 * updated by the application.
689 * If the application has no feedback for this preference kind the application
690 * will not explicitly call for this property and will not include it in this
693 * @param ph performance handle
694 * @param scope the time interval this valid for: [now - scope .. now]
695 * @param peer identifies the peer
696 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
699 GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
700 const struct GNUNET_PeerIdentity *peer,
701 const struct GNUNET_TIME_Relative scope,
706 /** @} */ /* end of group */
708 /* end of file gnunet-service-transport_ats.h */