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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
43 * Default bandwidth assigned to a network : 64 KB/s
45 #define GNUNET_ATS_DefaultBandwidth 65536
48 * Undefined value for an `enum GNUNET_ATS_Property`
50 #define GNUNET_ATS_VALUE_UNDEFINED UINT32_MAX
53 * String representation for GNUNET_ATS_VALUE_UNDEFINED
55 #define GNUNET_ATS_VALUE_UNDEFINED_STR "undefined"
58 * Maximum bandwidth assigned to a network : 4095 MB/s
60 #define GNUNET_ATS_MaxBandwidth UINT32_MAX
63 * Textual equivalent for GNUNET_ATS_MaxBandwidth
65 #define GNUNET_ATS_MaxBandwidthString "unlimited"
69 * ATS performance characteristics for an address.
71 struct GNUNET_ATS_Properties
74 * Delay. Time between when the time packet is sent and the packet
75 * arrives. FOREVER if we did not measure yet.
77 struct GNUNET_TIME_Relative delay;
80 * Actual traffic on this connection from this peer to the other peer.
81 * Includes transport overhead.
83 * Unit: [bytes/second]
85 uint32_t utilization_out;
88 * Actual traffic on this connection from the other peer to this peer.
89 * Includes transport overhead.
91 * Unit: [bytes/second]
93 uint32_t utilization_in;
96 * Distance on network layer (required for distance-vector routing)
97 * in hops. Zero for direct connections (i.e. plain TCP/UDP).
99 unsigned int distance;
102 * Which network scope does the respective address belong to?
103 * This property does not change.
105 enum GNUNET_NetworkType scope;
110 * ATS performance characteristics for an address in
111 * network byte order (for IPC).
113 struct GNUNET_ATS_PropertiesNBO
116 * Actual traffic on this connection from this peer to the other peer.
117 * Includes transport overhead.
119 * Unit: [bytes/second]
121 uint32_t utilization_out GNUNET_PACKED;
124 * Actual traffic on this connection from the other peer to this peer.
125 * Includes transport overhead.
127 * Unit: [bytes/second]
129 uint32_t utilization_in GNUNET_PACKED;
132 * Which network scope does the respective address belong to?
133 * This property does not change.
135 uint32_t scope GNUNET_PACKED;
138 * Distance on network layer (required for distance-vector routing)
139 * in hops. Zero for direct connections (i.e. plain TCP/UDP).
141 uint32_t distance GNUNET_PACKED;
144 * Delay. Time between when the time packet is sent and the packet
145 * arrives. FOREVER if we did not measure yet.
147 struct GNUNET_TIME_RelativeNBO delay;
151 /* ********************* LAN Characterization library ************************ */
152 /* Note: these functions do not really communicate with the ATS service */
156 * Convert ATS properties from host to network byte order.
158 * @param nbo[OUT] value written
159 * @param hbo value read
162 GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
163 const struct GNUNET_ATS_Properties *hbo);
167 * Convert ATS properties from network to host byte order.
169 * @param hbo[OUT] value written
170 * @param nbo value read
173 GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
174 const struct GNUNET_ATS_PropertiesNBO *nbo);
177 /* ********************Connection Suggestion API ***************************** */
180 * Handle to the ATS subsystem for making suggestions about
181 * connections the peer would like to have.
183 struct GNUNET_ATS_ConnectivityHandle;
186 * Handle for address suggestion requests.
188 struct GNUNET_ATS_ConnectivitySuggestHandle;
192 * Initialize the ATS connectivity suggestion client handle.
194 * @param cfg configuration to use
195 * @return ats connectivity handle, NULL on error
197 struct GNUNET_ATS_ConnectivityHandle *
198 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
202 * Shutdown ATS connectivity suggestion client.
204 * @param ch handle to destroy
207 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch);
211 * We would like to establish a new connection with a peer. ATS
212 * should suggest a good address to begin with.
215 * @param peer identity of the peer we need an address for
216 * @param strength how urgent is the need for such a suggestion
217 * @return suggestion handle, NULL if request is already pending
219 struct GNUNET_ATS_ConnectivitySuggestHandle *
220 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
221 const struct GNUNET_PeerIdentity *peer,
226 * We no longer care about being connected to a peer.
231 GNUNET_ATS_connectivity_suggest_cancel (struct
232 GNUNET_ATS_ConnectivitySuggestHandle *sh);
235 /* ******************************** Scheduling API ***************************** */
238 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
240 struct GNUNET_ATS_SchedulingHandle;
243 * Opaque session handle, defined by plugins. Contents not known to ATS.
245 struct GNUNET_ATS_Session;
249 * Signature of a function called by ATS with the current bandwidth
250 * and address preferences as determined by ATS. If our connection
251 * to ATS dies and thus all suggestions become invalid, this function
252 * is called ONCE with all arguments (except @a cls) being NULL/0.
255 * @param peer for which we suggest an address, NULL if ATS connection died
256 * @param address suggested address (including peer identity of the peer),
257 * may be NULL to signal disconnect from peer
258 * @param session session to use, NULL to establish a new outgoing session
259 * @param bandwidth_out assigned outbound bandwidth for the connection,
260 * 0 to signal disconnect
261 * @param bandwidth_in assigned inbound bandwidth for the connection,
262 * 0 to signal disconnect
265 (*GNUNET_ATS_AddressSuggestionCallback) (void *cls,
266 const struct GNUNET_PeerIdentity *peer,
268 GNUNET_HELLO_Address *address,
269 struct GNUNET_ATS_Session *session,
270 struct GNUNET_BANDWIDTH_Value32NBO
272 struct GNUNET_BANDWIDTH_Value32NBO
277 * Initialize the ATS scheduling subsystem.
279 * @param cfg configuration to use
280 * @param suggest_cb notification to call whenever the suggestation changed
281 * @param suggest_cb_cls closure for @a suggest_cb
282 * @return ats context
284 struct GNUNET_ATS_SchedulingHandle *
285 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
286 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
287 void *suggest_cb_cls);
291 * Client is done with ATS scheduling, release resources.
293 * @param sh handle to release
296 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh);
300 * Handle used within ATS to track an address.
302 struct GNUNET_ATS_AddressRecord;
306 * We have a new address ATS should know. Addresses have to be added with this
307 * function before they can be: updated, set in use and destroyed
310 * @param address the address
311 * @param session session handle (if available, i.e. for incoming connections)
312 * @param prop performance data for the address
313 * @return handle to the address representation inside ATS, NULL
314 * on error (i.e. ATS knows this exact address already, or
315 * address is invalid)
317 struct GNUNET_ATS_AddressRecord *
318 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
319 const struct GNUNET_HELLO_Address *address,
320 struct GNUNET_ATS_Session *session,
321 const struct GNUNET_ATS_Properties *prop);
325 * An address was used to initiate a session.
327 * @param ar address record to update information for
328 * @param session session handle
331 GNUNET_ATS_address_add_session (struct GNUNET_ATS_AddressRecord *ar,
332 struct GNUNET_ATS_Session *session);
336 * A @a session was destroyed, disassociate it from the given address
337 * record. If this was an incoming addess, destroys the address as
340 * @param ar address record to update information for
341 * @param session session handle
342 * @return #GNUNET_YES if the @a ar was destroyed because
343 * it was an incoming address,
344 * #GNUNET_NO if the @ar was kept because we can
345 * use it still to establish a new session
348 GNUNET_ATS_address_del_session (struct GNUNET_ATS_AddressRecord *ar,
349 struct GNUNET_ATS_Session *session);
353 * We have updated performance statistics for a given address. Note
354 * that this function can be called for addresses that are currently
355 * in use as well as addresses that are valid but not actively in use.
356 * Furthermore, the peer may not even be connected to us right now (@a
357 * session value of NULL used to signal disconnect, or somehow we
358 * otherwise got updated on @a ats information). Based on the
359 * information provided, ATS may update bandwidth assignments and
360 * suggest to switch addresses.
362 * @param ar address record to update information for
363 * @param prop performance data for the address
366 GNUNET_ATS_address_update (struct GNUNET_ATS_AddressRecord *ar,
367 const struct GNUNET_ATS_Properties *prop);
371 * An address got destroyed, stop using it as a valid address.
373 * @param ar address record to destroy, it's validation has
374 * expired and ATS may no longer use it
377 GNUNET_ATS_address_destroy (struct GNUNET_ATS_AddressRecord *ar);
380 /* ******************************** Performance API ***************************** */
383 * ATS Handle to obtain and/or modify performance information.
385 struct GNUNET_ATS_PerformanceHandle;
388 * Signature of a function that is called with QoS information about an address.
391 * @param address the address, NULL if ATS service was disconnected or
392 * when the iteration is completed in the case of
393 * #GNUNET_ATS_performance_list_addresses()
394 * @param address_active #GNUNET_YES if this address is actively used
395 * to maintain a connection to a peer;
396 * #GNUNET_NO if the address is not actively used;
397 * #GNUNET_SYSERR if this address is no longer available for ATS
398 * @param bandwidth_out assigned outbound bandwidth for the connection
399 * @param bandwidth_in assigned inbound bandwidth for the connection
400 * @param prop performance data for the address
403 (*GNUNET_ATS_AddressInformationCallback) (void *cls,
405 GNUNET_HELLO_Address *address,
407 struct GNUNET_BANDWIDTH_Value32NBO
409 struct GNUNET_BANDWIDTH_Value32NBO
412 GNUNET_ATS_Properties *prop);
416 * Handle for an address listing operation
418 struct GNUNET_ATS_AddressListHandle;
422 * Get handle to access performance API of the ATS subsystem.
424 * @param cfg configuration to use
425 * @param addr_info_cb callback called when performance characteristics for
427 * @param addr_info_cb_cls closure for @a addr_info_cb
428 * @return ats performance context
430 struct GNUNET_ATS_PerformanceHandle *
431 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
432 GNUNET_ATS_AddressInformationCallback addr_info_cb,
433 void *addr_info_cb_cls);
437 * Get information about addresses known to the ATS subsystem.
439 * @param ph the performance handle to use
440 * @param peer peer idm can be NULL for all peers
441 * @param all #GNUNET_YES to get information about all addresses or #GNUNET_NO to
442 * get only address currently used
443 * @param infocb callback to call with the addresses,
444 * will callback with address == NULL when done
445 * @param infocb_cls closure for @a infocb
446 * @return handle to abort the operation
448 struct GNUNET_ATS_AddressListHandle *
449 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *ph,
450 const struct GNUNET_PeerIdentity *peer,
452 GNUNET_ATS_AddressInformationCallback
458 * Cancel a pending address listing operation
460 * @param alh the `struct GNUNET_ATS_AddressListHandle` handle to cancel
463 GNUNET_ATS_performance_list_addresses_cancel (struct
464 GNUNET_ATS_AddressListHandle *alh);
468 * Client is done using the ATS performance subsystem, release resources.
473 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph);
477 * Function called with reservation result.
480 * @param peer identifies the peer
481 * @param amount set to the amount that was actually reserved or unreserved;
482 * either the full requested amount or zero (no partial reservations)
483 * @param res_delay if the reservation could not be satisfied (amount was 0), how
484 * long should the client wait until re-trying?
487 (*GNUNET_ATS_ReservationCallback) (void *cls,
488 const struct GNUNET_PeerIdentity *peer,
490 struct GNUNET_TIME_Relative res_delay);
494 * Context that can be used to cancel a peer information request.
496 struct GNUNET_ATS_ReservationContext;
500 * Reserve inbound bandwidth from the given peer. ATS will look at
501 * the current amount of traffic we receive from the peer and ensure
502 * that the peer could add 'amount' of data to its stream.
504 * @param ph performance handle
505 * @param peer identifies the peer
506 * @param amount reserve N bytes for receiving, negative
507 * amounts can be used to undo a (recent) reservation;
508 * @param rcb function to call with the resulting reservation information
509 * @param rcb_cls closure for @a rcb
510 * @return NULL on error
511 * @deprecated will be replaced soon
513 struct GNUNET_ATS_ReservationContext *
514 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
515 const struct GNUNET_PeerIdentity *peer,
517 GNUNET_ATS_ReservationCallback rcb,
522 * Cancel request for reserving bandwidth.
524 * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
527 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc);
531 * ATS preference types as array initializer
533 #define GNUNET_ATS_PreferenceType { GNUNET_ATS_PREFERENCE_BANDWIDTH, \
534 GNUNET_ATS_PREFERENCE_LATENCY, \
535 GNUNET_ATS_PREFERENCE_END }
538 * ATS preference types as string array initializer
540 #define GNUNET_ATS_PreferenceTypeString { "BANDWIDTH", "LATENCY", "END" }
543 * Enum defining all known preference categories.
545 enum GNUNET_ATS_PreferenceKind
548 * Change the peer's bandwidth value (value per byte of bandwidth in
549 * the goal function) to the given amount. The argument is followed
550 * by a double value giving the desired value (can be negative).
551 * Preference changes are forgotten if peers disconnect.
553 GNUNET_ATS_PREFERENCE_BANDWIDTH = 0,
556 * Change the peer's latency value to the given amount. The
557 * argument is followed by a double value giving the desired value
558 * (can be negative). The absolute score in the goal function is
559 * the inverse of the latency in microseconds (minimum: 1
560 * microsecond) multiplied by the latency preferences.
562 GNUNET_ATS_PREFERENCE_LATENCY = 1,
565 * End of preference list.
567 GNUNET_ATS_PREFERENCE_END = 2
572 * Convert a GNUNET_ATS_PreferenceType to a string
574 * @param type the preference type
575 * @return a string or NULL if invalid
578 GNUNET_ATS_print_preference_type (enum GNUNET_ATS_PreferenceKind type);
582 * Change preferences for the given peer. Preference changes are forgotten if peers
585 * @param ph performance handle @param peer identifies the peer
586 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the
590 GNUNET_ATS_performance_change_preference (struct
591 GNUNET_ATS_PerformanceHandle *ph,
593 GNUNET_PeerIdentity *peer,
598 * Application feedback on how good preference requirements are fulfilled
599 * for the preferences included in the given time scope [now - scope .. now]
601 * An application notifies ATS if (and only if) it has feedback information
602 * for specific properties. This values are valid until the feedback scores are
603 * updated by the application.
605 * If the application has no feedback for this preference kind the application
606 * will not explicitly call for this property and will not include it in this
609 * @param ph performance handle
610 * @param scope the time interval this valid for: [now - scope .. now]
611 * @param peer identifies the peer
612 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
615 GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
616 const struct GNUNET_PeerIdentity *peer,
617 const struct GNUNET_TIME_Relative scope,
622 /** @} */ /* end of group */
624 /* end of file gnunet-service-transport_ats.h */