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"
41 * Default bandwidth assigned to a network : 64 KB/s
43 #define GNUNET_ATS_DefaultBandwidth 65536
46 * Undefined value for an `enum GNUNET_ATS_Property`
48 #define GNUNET_ATS_VALUE_UNDEFINED UINT32_MAX
51 * String representation for GNUNET_ATS_VALUE_UNDEFINED
53 #define GNUNET_ATS_VALUE_UNDEFINED_STR "undefined"
56 * Maximum bandwidth assigned to a network : 4095 MB/s
58 #define GNUNET_ATS_MaxBandwidth UINT32_MAX
61 * Textual equivalent for GNUNET_ATS_MaxBandwidth
63 #define GNUNET_ATS_MaxBandwidthString "unlimited"
67 * ATS performance characteristics for an address.
69 struct GNUNET_ATS_Properties
73 * Delay. Time between when the time packet is sent and the packet
74 * arrives. FOREVER if we did not measure yet.
76 struct GNUNET_TIME_Relative delay;
79 * Actual traffic on this connection from this peer to the other peer.
80 * Includes transport overhead.
82 * Unit: [bytes/second]
84 uint32_t utilization_out;
87 * Actual traffic on this connection from the other peer to this peer.
88 * Includes transport overhead.
90 * Unit: [bytes/second]
92 uint32_t utilization_in;
95 * Distance on network layer (required for distance-vector routing)
96 * in hops. Zero for direct connections (i.e. plain TCP/UDP).
98 unsigned int distance;
101 * Which network scope does the respective address belong to?
102 * This property does not change.
104 enum GNUNET_NetworkType scope;
110 * ATS performance characteristics for an address in
111 * network byte order (for IPC).
113 struct GNUNET_ATS_PropertiesNBO
117 * Actual traffic on this connection from this peer to the other peer.
118 * Includes transport overhead.
120 * Unit: [bytes/second]
122 uint32_t utilization_out GNUNET_PACKED;
125 * Actual traffic on this connection from the other peer to this peer.
126 * Includes transport overhead.
128 * Unit: [bytes/second]
130 uint32_t utilization_in GNUNET_PACKED;
133 * Which network scope does the respective address belong to?
134 * This property does not change.
136 uint32_t scope GNUNET_PACKED;
139 * Distance on network layer (required for distance-vector routing)
140 * in hops. Zero for direct connections (i.e. plain TCP/UDP).
142 uint32_t distance GNUNET_PACKED;
145 * Delay. Time between when the time packet is sent and the packet
146 * arrives. FOREVER if we did not measure yet.
148 struct GNUNET_TIME_RelativeNBO delay;
154 /* ********************* LAN Characterization library ************************ */
155 /* Note: these functions do not really communicate with the ATS service */
159 * Convert ATS properties from host to network byte order.
161 * @param nbo[OUT] value written
162 * @param hbo value read
165 GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
166 const struct GNUNET_ATS_Properties *hbo);
170 * Convert ATS properties from network to host byte order.
172 * @param hbo[OUT] value written
173 * @param nbo value read
176 GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
177 const struct GNUNET_ATS_PropertiesNBO *nbo);
181 /* ********************Connection Suggestion API ***************************** */
184 * Handle to the ATS subsystem for making suggestions about
185 * connections the peer would like to have.
187 struct GNUNET_ATS_ConnectivityHandle;
190 * Handle for address suggestion requests.
192 struct GNUNET_ATS_ConnectivitySuggestHandle;
196 * Initialize the ATS connectivity suggestion client handle.
198 * @param cfg configuration to use
199 * @return ats connectivity handle, NULL on error
201 struct GNUNET_ATS_ConnectivityHandle *
202 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
206 * Shutdown ATS connectivity suggestion client.
208 * @param ch handle to destroy
211 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch);
215 * We would like to establish a new connection with a peer. ATS
216 * should suggest a good address to begin with.
219 * @param peer identity of the peer we need an address for
220 * @param strength how urgent is the need for such a suggestion
221 * @return suggestion handle, NULL if request is already pending
223 struct GNUNET_ATS_ConnectivitySuggestHandle *
224 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
225 const struct GNUNET_PeerIdentity *peer,
230 * We no longer care about being connected to a peer.
235 GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh);
238 /* ******************************** Scheduling API ***************************** */
241 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
243 struct GNUNET_ATS_SchedulingHandle;
246 * Opaque session handle, defined by plugins. Contents not known to ATS.
248 struct GNUNET_ATS_Session;
252 * Signature of a function called by ATS with the current bandwidth
253 * and address preferences as determined by ATS. If our connection
254 * to ATS dies and thus all suggestions become invalid, this function
255 * is called ONCE with all arguments (except @a cls) being NULL/0.
258 * @param peer for which we suggest an address, NULL if ATS connection died
259 * @param address suggested address (including peer identity of the peer),
260 * may be NULL to signal disconnect from peer
261 * @param session session to use, NULL to establish a new outgoing session
262 * @param bandwidth_out assigned outbound bandwidth for the connection,
263 * 0 to signal disconnect
264 * @param bandwidth_in assigned inbound bandwidth for the connection,
265 * 0 to signal disconnect
268 (*GNUNET_ATS_AddressSuggestionCallback) (void *cls,
269 const struct GNUNET_PeerIdentity *peer,
270 const struct GNUNET_HELLO_Address *address,
271 struct GNUNET_ATS_Session *session,
272 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
273 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in);
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);
381 /* ******************************** Performance API ***************************** */
384 * ATS Handle to obtain and/or modify performance information.
386 struct GNUNET_ATS_PerformanceHandle;
389 * Signature of a function that is called with QoS information about an address.
392 * @param address the address, NULL if ATS service was disconnected or
393 * when the iteration is completed in the case of
394 * #GNUNET_ATS_performance_list_addresses()
395 * @param address_active #GNUNET_YES if this address is actively used
396 * to maintain a connection to a peer;
397 * #GNUNET_NO if the address is not actively used;
398 * #GNUNET_SYSERR if this address is no longer available for ATS
399 * @param bandwidth_out assigned outbound bandwidth for the connection
400 * @param bandwidth_in assigned inbound bandwidth for the connection
401 * @param prop performance data for the address
404 (*GNUNET_ATS_AddressInformationCallback) (void *cls,
405 const struct GNUNET_HELLO_Address *address,
407 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
408 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
409 const struct GNUNET_ATS_Properties *prop);
413 * Handle for an address listing operation
415 struct GNUNET_ATS_AddressListHandle;
419 * Get handle to access performance API of the ATS subsystem.
421 * @param cfg configuration to use
422 * @param addr_info_cb callback called when performance characteristics for
424 * @param addr_info_cb_cls closure for @a addr_info_cb
425 * @return ats performance context
427 struct GNUNET_ATS_PerformanceHandle *
428 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
429 GNUNET_ATS_AddressInformationCallback addr_info_cb,
430 void *addr_info_cb_cls);
434 * Get information about addresses known to the ATS subsystem.
436 * @param ph the performance handle to use
437 * @param peer peer idm can be NULL for all peers
438 * @param all #GNUNET_YES to get information about all addresses or #GNUNET_NO to
439 * get only address currently used
440 * @param infocb callback to call with the addresses,
441 * will callback with address == NULL when done
442 * @param infocb_cls closure for @a infocb
443 * @return handle to abort the operation
445 struct GNUNET_ATS_AddressListHandle *
446 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *ph,
447 const struct GNUNET_PeerIdentity *peer,
449 GNUNET_ATS_AddressInformationCallback infocb,
454 * Cancel a pending address listing operation
456 * @param alh the `struct GNUNET_ATS_AddressListHandle` handle to cancel
459 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *alh);
463 * Client is done using the ATS performance subsystem, release resources.
468 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph);
472 * Function called with reservation result.
475 * @param peer identifies the peer
476 * @param amount set to the amount that was actually reserved or unreserved;
477 * either the full requested amount or zero (no partial reservations)
478 * @param res_delay if the reservation could not be satisfied (amount was 0), how
479 * long should the client wait until re-trying?
482 (*GNUNET_ATS_ReservationCallback) (void *cls,
483 const struct GNUNET_PeerIdentity *peer,
485 struct GNUNET_TIME_Relative res_delay);
489 * Context that can be used to cancel a peer information request.
491 struct GNUNET_ATS_ReservationContext;
495 * Reserve inbound bandwidth from the given peer. ATS will look at
496 * the current amount of traffic we receive from the peer and ensure
497 * that the peer could add 'amount' of data to its stream.
499 * @param ph performance handle
500 * @param peer identifies the peer
501 * @param amount reserve N bytes for receiving, negative
502 * amounts can be used to undo a (recent) reservation;
503 * @param rcb function to call with the resulting reservation information
504 * @param rcb_cls closure for @a rcb
505 * @return NULL on error
506 * @deprecated will be replaced soon
508 struct GNUNET_ATS_ReservationContext *
509 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
510 const struct GNUNET_PeerIdentity *peer,
512 GNUNET_ATS_ReservationCallback rcb,
517 * Cancel request for reserving bandwidth.
519 * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
522 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc);
526 * ATS preference types as array initializer
528 #define GNUNET_ATS_PreferenceType {GNUNET_ATS_PREFERENCE_BANDWIDTH, GNUNET_ATS_PREFERENCE_LATENCY, GNUNET_ATS_PREFERENCE_END}
531 * ATS preference types as string array initializer
533 #define GNUNET_ATS_PreferenceTypeString {"BANDWIDTH", "LATENCY", "END" }
536 * Enum defining all known preference categories.
538 enum GNUNET_ATS_PreferenceKind
542 * Change the peer's bandwidth value (value per byte of bandwidth in
543 * the goal function) to the given amount. The argument is followed
544 * by a double value giving the desired value (can be negative).
545 * Preference changes are forgotten if peers disconnect.
547 GNUNET_ATS_PREFERENCE_BANDWIDTH = 0,
550 * Change the peer's latency value to the given amount. The
551 * argument is followed by a double value giving the desired value
552 * (can be negative). The absolute score in the goal function is
553 * the inverse of the latency in microseconds (minimum: 1
554 * microsecond) multiplied by the latency preferences.
556 GNUNET_ATS_PREFERENCE_LATENCY = 1,
559 * End of preference list.
561 GNUNET_ATS_PREFERENCE_END = 2
567 * Convert a GNUNET_ATS_PreferenceType to a string
569 * @param type the preference type
570 * @return a string or NULL if invalid
573 GNUNET_ATS_print_preference_type (enum GNUNET_ATS_PreferenceKind type);
577 * Change preferences for the given peer. Preference changes are forgotten if peers
580 * @param ph performance handle @param peer identifies the peer
581 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the
585 GNUNET_ATS_performance_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
586 const struct GNUNET_PeerIdentity *peer,
591 * Application feedback on how good preference requirements are fulfilled
592 * for the preferences included in the given time scope [now - scope .. now]
594 * An application notifies ATS if (and only if) it has feedback information
595 * for specific properties. This values are valid until the feedback scores are
596 * updated by the application.
598 * If the application has no feedback for this preference kind the application
599 * will not explicitly call for this property and will not include it in this
602 * @param ph performance handle
603 * @param scope the time interval this valid for: [now - scope .. now]
604 * @param peer identifies the peer
605 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
608 GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
609 const struct GNUNET_PeerIdentity *peer,
610 const struct GNUNET_TIME_Relative scope,
615 /** @} */ /* end of group */
617 /* end of file gnunet-service-transport_ats.h */