error handling
[oweals/gnunet.git] / src / include / gnunet_ats_service.h
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2010-2015 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file
22  * Automatic transport selection and outbound bandwidth determination
23  *
24  * @author Christian Grothoff
25  * @author Matthias Wachs
26  *
27  * @defgroup ats  ATS service
28  * Automatic Transport Selection and outbound bandwidth determination
29  *
30  * @see [Documentation](https://gnunet.org/ats-subsystem)
31  *
32  * @{
33  */
34 #ifndef GNUNET_ATS_SERVICE_H
35 #define GNUNET_ATS_SERVICE_H
36
37 #include "gnunet_constants.h"
38 #include "gnunet_util_lib.h"
39 #include "gnunet_hello_lib.h"
40
41
42 /**
43  * Default bandwidth assigned to a network : 64 KB/s
44  */
45 #define GNUNET_ATS_DefaultBandwidth 65536
46
47 /**
48  * Undefined value for an `enum GNUNET_ATS_Property`
49  */
50 #define GNUNET_ATS_VALUE_UNDEFINED UINT32_MAX
51
52 /**
53  * String representation for GNUNET_ATS_VALUE_UNDEFINED
54  */
55 #define GNUNET_ATS_VALUE_UNDEFINED_STR "undefined"
56
57 /**
58  * Maximum bandwidth assigned to a network : 4095 MB/s
59  */
60 #define GNUNET_ATS_MaxBandwidth UINT32_MAX
61
62 /**
63  * Textual equivalent for GNUNET_ATS_MaxBandwidth
64  */
65 #define GNUNET_ATS_MaxBandwidthString "unlimited"
66
67
68 /**
69  * ATS performance characteristics for an address.
70  */
71 struct GNUNET_ATS_Properties
72 {
73   /**
74    * Delay.  Time between when the time packet is sent and the packet
75    * arrives.  FOREVER if we did not measure yet.
76    */
77   struct GNUNET_TIME_Relative delay;
78
79   /**
80    * Actual traffic on this connection from this peer to the other peer.
81    * Includes transport overhead.
82    *
83    * Unit: [bytes/second]
84    */
85   uint32_t utilization_out;
86
87   /**
88    * Actual traffic on this connection from the other peer to this peer.
89    * Includes transport overhead.
90    *
91    * Unit: [bytes/second]
92    */
93   uint32_t utilization_in;
94
95   /**
96    * Distance on network layer (required for distance-vector routing)
97    * in hops.  Zero for direct connections (i.e. plain TCP/UDP).
98    */
99   unsigned int distance;
100
101   /**
102    * Which network scope does the respective address belong to?
103    * This property does not change.
104    */
105   enum GNUNET_NetworkType scope;
106 };
107
108
109 /**
110  * ATS performance characteristics for an address in
111  * network byte order (for IPC).
112  */
113 struct GNUNET_ATS_PropertiesNBO
114 {
115   /**
116    * Actual traffic on this connection from this peer to the other peer.
117    * Includes transport overhead.
118    *
119    * Unit: [bytes/second]
120    */
121   uint32_t utilization_out GNUNET_PACKED;
122
123   /**
124    * Actual traffic on this connection from the other peer to this peer.
125    * Includes transport overhead.
126    *
127    * Unit: [bytes/second]
128    */
129   uint32_t utilization_in GNUNET_PACKED;
130
131   /**
132    * Which network scope does the respective address belong to?
133    * This property does not change.
134    */
135   uint32_t scope GNUNET_PACKED;
136
137   /**
138    * Distance on network layer (required for distance-vector routing)
139    * in hops.  Zero for direct connections (i.e. plain TCP/UDP).
140    */
141   uint32_t distance GNUNET_PACKED;
142
143   /**
144    * Delay.  Time between when the time packet is sent and the packet
145    * arrives.  FOREVER if we did not measure yet.
146    */
147   struct GNUNET_TIME_RelativeNBO delay;
148 };
149
150
151 /* ********************* LAN Characterization library ************************ */
152 /* Note: these functions do not really communicate with the ATS service */
153
154
155 /**
156  * Convert ATS properties from host to network byte order.
157  *
158  * @param nbo[OUT] value written
159  * @param hbo value read
160  */
161 void
162 GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
163                             const struct GNUNET_ATS_Properties *hbo);
164
165
166 /**
167  * Convert ATS properties from network to host byte order.
168  *
169  * @param hbo[OUT] value written
170  * @param nbo value read
171  */
172 void
173 GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
174                             const struct GNUNET_ATS_PropertiesNBO *nbo);
175
176
177 /* ********************Connection Suggestion API ***************************** */
178
179 /**
180  * Handle to the ATS subsystem for making suggestions about
181  * connections the peer would like to have.
182  */
183 struct GNUNET_ATS_ConnectivityHandle;
184
185 /**
186  * Handle for address suggestion requests.
187  */
188 struct GNUNET_ATS_ConnectivitySuggestHandle;
189
190
191 /**
192  * Initialize the ATS connectivity suggestion client handle.
193  *
194  * @param cfg configuration to use
195  * @return ats connectivity handle, NULL on error
196  */
197 struct GNUNET_ATS_ConnectivityHandle *
198 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
199
200
201 /**
202  * Shutdown ATS connectivity suggestion client.
203  *
204  * @param ch handle to destroy
205  */
206 void
207 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch);
208
209
210 /**
211  * We would like to establish a new connection with a peer.  ATS
212  * should suggest a good address to begin with.
213  *
214  * @param ch handle
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
218  */
219 struct GNUNET_ATS_ConnectivitySuggestHandle *
220 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
221                                  const struct GNUNET_PeerIdentity *peer,
222                                  uint32_t strength);
223
224
225 /**
226  * We no longer care about being connected to a peer.
227  *
228  * @param sh handle
229  */
230 void
231 GNUNET_ATS_connectivity_suggest_cancel (struct
232                                         GNUNET_ATS_ConnectivitySuggestHandle *sh);
233
234
235 /* ******************************** Scheduling API ***************************** */
236
237 /**
238  * Handle to the ATS subsystem for bandwidth/transport scheduling information.
239  */
240 struct GNUNET_ATS_SchedulingHandle;
241
242 /**
243  * Opaque session handle, defined by plugins.  Contents not known to ATS.
244  */
245 struct GNUNET_ATS_Session;
246
247
248 /**
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.
253  *
254  * @param cls closure
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
263  */
264 typedef void
265 (*GNUNET_ATS_AddressSuggestionCallback) (void *cls,
266                                          const struct GNUNET_PeerIdentity *peer,
267                                          const struct
268                                          GNUNET_HELLO_Address *address,
269                                          struct GNUNET_ATS_Session *session,
270                                          struct GNUNET_BANDWIDTH_Value32NBO
271                                          bandwidth_out,
272                                          struct GNUNET_BANDWIDTH_Value32NBO
273                                          bandwidth_in);
274
275
276 /**
277  * Initialize the ATS scheduling subsystem.
278  *
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
283  */
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);
288
289
290 /**
291  * Client is done with ATS scheduling, release resources.
292  *
293  * @param sh handle to release
294  */
295 void
296 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh);
297
298
299 /**
300  * Handle used within ATS to track an address.
301  */
302 struct GNUNET_ATS_AddressRecord;
303
304
305 /**
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
308  *
309  * @param sh handle
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)
316  */
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);
322
323
324 /**
325  * An address was used to initiate a session.
326  *
327  * @param ar address record to update information for
328  * @param session session handle
329  */
330 void
331 GNUNET_ATS_address_add_session (struct GNUNET_ATS_AddressRecord *ar,
332                                 struct GNUNET_ATS_Session *session);
333
334
335 /**
336  * A @a session was destroyed, disassociate it from the given address
337  * record.  If this was an incoming addess, destroys the address as
338  * well.
339  *
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
346  */
347 int
348 GNUNET_ATS_address_del_session (struct GNUNET_ATS_AddressRecord *ar,
349                                 struct GNUNET_ATS_Session *session);
350
351
352 /**
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.
361  *
362  * @param ar address record to update information for
363  * @param prop performance data for the address
364  */
365 void
366 GNUNET_ATS_address_update (struct GNUNET_ATS_AddressRecord *ar,
367                            const struct GNUNET_ATS_Properties *prop);
368
369
370 /**
371  * An address got destroyed, stop using it as a valid address.
372  *
373  * @param ar address record to destroy, it's validation has
374  *           expired and ATS may no longer use it
375  */
376 void
377 GNUNET_ATS_address_destroy (struct GNUNET_ATS_AddressRecord *ar);
378
379
380 /* ******************************** Performance API ***************************** */
381
382 /**
383  * ATS Handle to obtain and/or modify performance information.
384  */
385 struct GNUNET_ATS_PerformanceHandle;
386
387 /**
388  * Signature of a function that is called with QoS information about an address.
389  *
390  * @param cls closure
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
401  */
402 typedef void
403 (*GNUNET_ATS_AddressInformationCallback) (void *cls,
404                                           const struct
405                                           GNUNET_HELLO_Address *address,
406                                           int address_active,
407                                           struct GNUNET_BANDWIDTH_Value32NBO
408                                           bandwidth_out,
409                                           struct GNUNET_BANDWIDTH_Value32NBO
410                                           bandwidth_in,
411                                           const struct
412                                           GNUNET_ATS_Properties *prop);
413
414
415 /**
416  * Handle for an address listing operation
417  */
418 struct GNUNET_ATS_AddressListHandle;
419
420
421 /**
422  * Get handle to access performance API of the ATS subsystem.
423  *
424  * @param cfg configuration to use
425  * @param addr_info_cb callback called when performance characteristics for
426  *      an address change
427  * @param addr_info_cb_cls closure for @a addr_info_cb
428  * @return ats performance context
429  */
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);
434
435
436 /**
437  * Get information about addresses known to the ATS subsystem.
438  *
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
447  */
448 struct GNUNET_ATS_AddressListHandle *
449 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *ph,
450                                        const struct GNUNET_PeerIdentity *peer,
451                                        int all,
452                                        GNUNET_ATS_AddressInformationCallback
453                                        infocb,
454                                        void *infocb_cls);
455
456
457 /**
458  * Cancel a pending address listing operation
459  *
460  * @param alh the `struct GNUNET_ATS_AddressListHandle` handle to cancel
461  */
462 void
463 GNUNET_ATS_performance_list_addresses_cancel (struct
464                                               GNUNET_ATS_AddressListHandle *alh);
465
466
467 /**
468  * Client is done using the ATS performance subsystem, release resources.
469  *
470  * @param ph handle
471  */
472 void
473 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph);
474
475
476 /**
477  * Function called with reservation result.
478  *
479  * @param cls closure
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?
485  */
486 typedef void
487 (*GNUNET_ATS_ReservationCallback) (void *cls,
488                                    const struct GNUNET_PeerIdentity *peer,
489                                    int32_t amount,
490                                    struct GNUNET_TIME_Relative res_delay);
491
492
493 /**
494  * Context that can be used to cancel a peer information request.
495  */
496 struct GNUNET_ATS_ReservationContext;
497
498
499 /**
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.
503  *
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
512  */
513 struct GNUNET_ATS_ReservationContext *
514 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
515                               const struct GNUNET_PeerIdentity *peer,
516                               int32_t amount,
517                               GNUNET_ATS_ReservationCallback rcb,
518                               void *rcb_cls);
519
520
521 /**
522  * Cancel request for reserving bandwidth.
523  *
524  * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
525  */
526 void
527 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc);
528
529
530 /**
531  * ATS preference types as array initializer
532  */
533 #define GNUNET_ATS_PreferenceType { GNUNET_ATS_PREFERENCE_BANDWIDTH, \
534                                     GNUNET_ATS_PREFERENCE_LATENCY, \
535                                     GNUNET_ATS_PREFERENCE_END }
536
537 /**
538  * ATS preference types as string array initializer
539  */
540 #define GNUNET_ATS_PreferenceTypeString { "BANDWIDTH", "LATENCY", "END" }
541
542 /**
543  * Enum defining all known preference categories.
544  */
545 enum GNUNET_ATS_PreferenceKind
546 {
547   /**
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.
552    */
553   GNUNET_ATS_PREFERENCE_BANDWIDTH = 0,
554
555   /**
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.
561    */
562   GNUNET_ATS_PREFERENCE_LATENCY = 1,
563
564   /**
565    * End of preference list.
566    */
567   GNUNET_ATS_PREFERENCE_END = 2
568 };
569
570
571 /**
572  * Convert a GNUNET_ATS_PreferenceType to a string
573  *
574  * @param type the preference type
575  * @return a string or NULL if invalid
576  */
577 const char *
578 GNUNET_ATS_print_preference_type (enum GNUNET_ATS_PreferenceKind type);
579
580
581 /**
582  * Change preferences for the given peer. Preference changes are forgotten if peers
583  * disconnect.
584  *
585  * @param ph performance handle @param peer identifies the peer
586  * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the
587  * desired changes
588  */
589 void
590 GNUNET_ATS_performance_change_preference (struct
591                                           GNUNET_ATS_PerformanceHandle *ph,
592                                           const struct
593                                           GNUNET_PeerIdentity *peer,
594                                           ...);
595
596
597 /**
598  * Application feedback on how good preference requirements are fulfilled
599  * for the preferences included in the given time scope [now - scope .. now]
600  *
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.
604  *
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
607  * function call.
608  *
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
613  */
614 void
615 GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
616                                       const struct GNUNET_PeerIdentity *peer,
617                                       const struct GNUNET_TIME_Relative scope,
618                                       ...);
619
620 #endif
621
622 /** @} */  /* end of group */
623
624 /* end of file gnunet-service-transport_ats.h */