b25cf3ec02cc154bdb507fdfd79f7463f808a188
[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 /**
19  * @file
20  * Automatic transport selection and outbound bandwidth determination
21  *
22  * @author Christian Grothoff
23  * @author Matthias Wachs
24  *
25  * @defgroup ats  ATS service
26  * Automatic Transport Selection and outbound bandwidth determination
27  *
28  * @see [Documentation](https://gnunet.org/ats-subsystem)
29  *
30  * @{
31  */
32 #ifndef GNUNET_ATS_SERVICE_H
33 #define GNUNET_ATS_SERVICE_H
34
35 #include "gnunet_constants.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_hello_lib.h"
38
39
40 /**
41  * Default bandwidth assigned to a network : 64 KB/s
42  */
43 #define GNUNET_ATS_DefaultBandwidth 65536
44
45 /**
46  * Undefined value for an `enum GNUNET_ATS_Property`
47  */
48 #define GNUNET_ATS_VALUE_UNDEFINED UINT32_MAX
49
50 /**
51  * String representation for GNUNET_ATS_VALUE_UNDEFINED
52  */
53 #define GNUNET_ATS_VALUE_UNDEFINED_STR "undefined"
54
55 /**
56  * Maximum bandwidth assigned to a network : 4095 MB/s
57  */
58 #define GNUNET_ATS_MaxBandwidth UINT32_MAX
59
60 /**
61  * Textual equivalent for GNUNET_ATS_MaxBandwidth
62  */
63 #define GNUNET_ATS_MaxBandwidthString "unlimited"
64
65
66 /**
67  * ATS performance characteristics for an address.
68  */
69 struct GNUNET_ATS_Properties
70 {
71
72   /**
73    * Delay.  Time between when the time packet is sent and the packet
74    * arrives.  FOREVER if we did not measure yet.
75    */
76   struct GNUNET_TIME_Relative delay;
77
78   /**
79    * Actual traffic on this connection from this peer to the other peer.
80    * Includes transport overhead.
81    *
82    * Unit: [bytes/second]
83    */
84   uint32_t utilization_out;
85
86   /**
87    * Actual traffic on this connection from the other peer to this peer.
88    * Includes transport overhead.
89    *
90    * Unit: [bytes/second]
91    */
92   uint32_t utilization_in;
93
94   /**
95    * Distance on network layer (required for distance-vector routing)
96    * in hops.  Zero for direct connections (i.e. plain TCP/UDP).
97    */
98   unsigned int distance;
99
100   /**
101    * Which network scope does the respective address belong to?
102    * This property does not change.
103    */
104   enum GNUNET_NetworkType scope;
105
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   /**
117    * Actual traffic on this connection from this peer to the other peer.
118    * Includes transport overhead.
119    *
120    * Unit: [bytes/second]
121    */
122   uint32_t utilization_out GNUNET_PACKED;
123
124   /**
125    * Actual traffic on this connection from the other peer to this peer.
126    * Includes transport overhead.
127    *
128    * Unit: [bytes/second]
129    */
130   uint32_t utilization_in GNUNET_PACKED;
131
132   /**
133    * Which network scope does the respective address belong to?
134    * This property does not change.
135    */
136   uint32_t scope GNUNET_PACKED;
137
138   /**
139    * Distance on network layer (required for distance-vector routing)
140    * in hops.  Zero for direct connections (i.e. plain TCP/UDP).
141    */
142   uint32_t distance GNUNET_PACKED;
143
144   /**
145    * Delay.  Time between when the time packet is sent and the packet
146    * arrives.  FOREVER if we did not measure yet.
147    */
148   struct GNUNET_TIME_RelativeNBO delay;
149
150 };
151
152
153
154 /* ********************* LAN Characterization library ************************ */
155 /* Note: these functions do not really communicate with the ATS service */
156
157
158 /**
159  * Convert ATS properties from host to network byte order.
160  *
161  * @param nbo[OUT] value written
162  * @param hbo value read
163  */
164 void
165 GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
166                             const struct GNUNET_ATS_Properties *hbo);
167
168
169 /**
170  * Convert ATS properties from network to host byte order.
171  *
172  * @param hbo[OUT] value written
173  * @param nbo value read
174  */
175 void
176 GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
177                             const struct GNUNET_ATS_PropertiesNBO *nbo);
178
179
180
181 /* ********************Connection Suggestion API ***************************** */
182
183 /**
184  * Handle to the ATS subsystem for making suggestions about
185  * connections the peer would like to have.
186  */
187 struct GNUNET_ATS_ConnectivityHandle;
188
189 /**
190  * Handle for address suggestion requests.
191  */
192 struct GNUNET_ATS_ConnectivitySuggestHandle;
193
194
195 /**
196  * Initialize the ATS connectivity suggestion client handle.
197  *
198  * @param cfg configuration to use
199  * @return ats connectivity handle, NULL on error
200  */
201 struct GNUNET_ATS_ConnectivityHandle *
202 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
203
204
205 /**
206  * Shutdown ATS connectivity suggestion client.
207  *
208  * @param ch handle to destroy
209  */
210 void
211 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch);
212
213
214 /**
215  * We would like to establish a new connection with a peer.  ATS
216  * should suggest a good address to begin with.
217  *
218  * @param ch handle
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
222  */
223 struct GNUNET_ATS_ConnectivitySuggestHandle *
224 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
225                                  const struct GNUNET_PeerIdentity *peer,
226                                  uint32_t strength);
227
228
229 /**
230  * We no longer care about being connected to a peer.
231  *
232  * @param sh handle
233  */
234 void
235 GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh);
236
237
238 /* ******************************** Scheduling API ***************************** */
239
240 /**
241  * Handle to the ATS subsystem for bandwidth/transport scheduling information.
242  */
243 struct GNUNET_ATS_SchedulingHandle;
244
245 /**
246  * Opaque session handle, defined by plugins.  Contents not known to ATS.
247  */
248 struct GNUNET_ATS_Session;
249
250
251 /**
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.
256  *
257  * @param cls closure
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
266  */
267 typedef void
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);
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
381 /* ******************************** Performance API ***************************** */
382
383 /**
384  * ATS Handle to obtain and/or modify performance information.
385  */
386 struct GNUNET_ATS_PerformanceHandle;
387
388 /**
389  * Signature of a function that is called with QoS information about an address.
390  *
391  * @param cls closure
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
402  */
403 typedef void
404 (*GNUNET_ATS_AddressInformationCallback) (void *cls,
405                                           const struct GNUNET_HELLO_Address *address,
406                                           int address_active,
407                                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
408                                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
409                                           const struct GNUNET_ATS_Properties *prop);
410
411
412 /**
413  * Handle for an address listing operation
414  */
415 struct GNUNET_ATS_AddressListHandle;
416
417
418 /**
419  * Get handle to access performance API of the ATS subsystem.
420  *
421  * @param cfg configuration to use
422  * @param addr_info_cb callback called when performance characteristics for
423  *      an address change
424  * @param addr_info_cb_cls closure for @a addr_info_cb
425  * @return ats performance context
426  */
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);
431
432
433 /**
434  * Get information about addresses known to the ATS subsystem.
435  *
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
444  */
445 struct GNUNET_ATS_AddressListHandle *
446 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *ph,
447                                        const struct GNUNET_PeerIdentity *peer,
448                                        int all,
449                                        GNUNET_ATS_AddressInformationCallback infocb,
450                                        void *infocb_cls);
451
452
453 /**
454  * Cancel a pending address listing operation
455  *
456  * @param alh the `struct GNUNET_ATS_AddressListHandle` handle to cancel
457  */
458 void
459 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *alh);
460
461
462 /**
463  * Client is done using the ATS performance subsystem, release resources.
464  *
465  * @param ph handle
466  */
467 void
468 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph);
469
470
471 /**
472  * Function called with reservation result.
473  *
474  * @param cls closure
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?
480  */
481 typedef void
482 (*GNUNET_ATS_ReservationCallback) (void *cls,
483                                    const struct GNUNET_PeerIdentity *peer,
484                                    int32_t amount,
485                                    struct GNUNET_TIME_Relative res_delay);
486
487
488 /**
489  * Context that can be used to cancel a peer information request.
490  */
491 struct GNUNET_ATS_ReservationContext;
492
493
494 /**
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.
498  *
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
507  */
508 struct GNUNET_ATS_ReservationContext *
509 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
510                               const struct GNUNET_PeerIdentity *peer,
511                               int32_t amount,
512                               GNUNET_ATS_ReservationCallback rcb,
513                               void *rcb_cls);
514
515
516 /**
517  * Cancel request for reserving bandwidth.
518  *
519  * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
520  */
521 void
522 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc);
523
524
525 /**
526  * ATS preference types as array initializer
527  */
528 #define GNUNET_ATS_PreferenceType {GNUNET_ATS_PREFERENCE_BANDWIDTH, GNUNET_ATS_PREFERENCE_LATENCY, GNUNET_ATS_PREFERENCE_END}
529
530 /**
531  * ATS preference types as string array initializer
532  */
533 #define GNUNET_ATS_PreferenceTypeString {"BANDWIDTH", "LATENCY", "END" }
534
535 /**
536  * Enum defining all known preference categories.
537  */
538 enum GNUNET_ATS_PreferenceKind
539 {
540
541   /**
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.
546    */
547   GNUNET_ATS_PREFERENCE_BANDWIDTH = 0,
548
549   /**
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.
555    */
556   GNUNET_ATS_PREFERENCE_LATENCY = 1,
557
558   /**
559    * End of preference list.
560    */
561   GNUNET_ATS_PREFERENCE_END = 2
562
563 };
564
565
566 /**
567  * Convert a GNUNET_ATS_PreferenceType to a string
568  *
569  * @param type the preference type
570  * @return a string or NULL if invalid
571  */
572 const char *
573 GNUNET_ATS_print_preference_type (enum GNUNET_ATS_PreferenceKind type);
574
575
576 /**
577  * Change preferences for the given peer. Preference changes are forgotten if peers
578  * disconnect.
579  *
580  * @param ph performance handle @param peer identifies the peer
581  * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the
582  * desired changes
583  */
584 void
585 GNUNET_ATS_performance_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
586                                           const struct GNUNET_PeerIdentity *peer,
587                                           ...);
588
589
590 /**
591  * Application feedback on how good preference requirements are fulfilled
592  * for the preferences included in the given time scope [now - scope .. now]
593  *
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.
597  *
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
600  * function call.
601  *
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
606  */
607 void
608 GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
609                                       const struct GNUNET_PeerIdentity *peer,
610                                       const struct GNUNET_TIME_Relative scope,
611                                       ...);
612
613 #endif
614
615 /** @} */  /* end of group */
616
617 /* end of file gnunet-service-transport_ats.h */