doxygen: group/module definitions (part 1)
[oweals/gnunet.git] / src / include / gnunet_ats_service.h
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010-2015 Christian Grothoff (and other contributing authors)
4
5  GNUnet is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published
7  by the Free Software Foundation; either version 3, or (at your
8  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  General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with GNUnet; see the file COPYING.  If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
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  */
31 #ifndef GNUNET_ATS_SERVICE_H
32 #define GNUNET_ATS_SERVICE_H
33
34 #include "gnunet_constants.h"
35 #include "gnunet_util_lib.h"
36 #include "gnunet_hello_lib.h"
37
38 /**
39  * Types of networks (with separate quotas) we support.
40  */
41 enum GNUNET_ATS_Network_Type
42 {
43   /**
44    * Category of last resort.
45    */
46   GNUNET_ATS_NET_UNSPECIFIED = 0,
47
48   /**
49    * Loopback (same host).
50    */
51   GNUNET_ATS_NET_LOOPBACK = 1,
52
53   /**
54    * Local area network.
55    */
56   GNUNET_ATS_NET_LAN = 2,
57
58   /**
59    * Wide area network (i.e. Internet)
60    */
61   GNUNET_ATS_NET_WAN = 3,
62
63   /**
64    * Wireless LAN (i.e. 802.11abgn)
65    */
66   GNUNET_ATS_NET_WLAN = 4,
67
68   /**
69    * Bluetooth LAN
70    */
71   GNUNET_ATS_NET_BT = 5
72
73 /**
74  * Number of network types supported by ATS
75  */
76 #define GNUNET_ATS_NetworkTypeCount 6
77
78 };
79
80
81 /**
82  * Default bandwidth assigned to a network : 64 KB/s
83  */
84 #define GNUNET_ATS_DefaultBandwidth 65536
85
86 /**
87  * Undefined value for an `enum GNUNET_ATS_Property`
88  */
89 #define GNUNET_ATS_VALUE_UNDEFINED UINT32_MAX
90
91 /**
92  * String representation for GNUNET_ATS_VALUE_UNDEFINED
93  */
94 #define GNUNET_ATS_VALUE_UNDEFINED_STR "undefined"
95
96 /**
97  * Maximum bandwidth assigned to a network : 4095 MB/s
98  */
99 #define GNUNET_ATS_MaxBandwidth UINT32_MAX
100
101 /**
102  * Textual equivalent for GNUNET_ATS_MaxBandwidth
103  */
104 #define GNUNET_ATS_MaxBandwidthString "unlimited"
105
106
107 /**
108  * ATS performance characteristics for an address.
109  */
110 struct GNUNET_ATS_Properties
111 {
112
113   /**
114    * Delay.  Time between when the time packet is sent and the packet
115    * arrives.  FOREVER if we did not measure yet.
116    */
117   struct GNUNET_TIME_Relative delay;
118
119   /**
120    * Actual traffic on this connection from this peer to the other peer.
121    * Includes transport overhead.
122    *
123    * Unit: [bytes/second]
124    */
125   uint32_t utilization_out;
126
127   /**
128    * Actual traffic on this connection from the other peer to this peer.
129    * Includes transport overhead.
130    *
131    * Unit: [bytes/second]
132    */
133   uint32_t utilization_in;
134
135   /**
136    * Distance on network layer (required for distance-vector routing)
137    * in hops.  Zero for direct connections (i.e. plain TCP/UDP).
138    */
139   unsigned int distance;
140
141   /**
142    * Which network scope does the respective address belong to?
143    * This property does not change.
144    */
145   enum GNUNET_ATS_Network_Type scope;
146
147 };
148
149
150 /**
151  * ATS performance characteristics for an address in
152  * network byte order (for IPC).
153  */
154 struct GNUNET_ATS_PropertiesNBO
155 {
156
157   /**
158    * Actual traffic on this connection from this peer to the other peer.
159    * Includes transport overhead.
160    *
161    * Unit: [bytes/second]
162    */
163   uint32_t utilization_out GNUNET_PACKED;
164
165   /**
166    * Actual traffic on this connection from the other peer to this peer.
167    * Includes transport overhead.
168    *
169    * Unit: [bytes/second]
170    */
171   uint32_t utilization_in GNUNET_PACKED;
172
173   /**
174    * Which network scope does the respective address belong to?
175    * This property does not change.
176    */
177   uint32_t scope GNUNET_PACKED;
178
179   /**
180    * Distance on network layer (required for distance-vector routing)
181    * in hops.  Zero for direct connections (i.e. plain TCP/UDP).
182    */
183   uint32_t distance GNUNET_PACKED;
184
185   /**
186    * Delay.  Time between when the time packet is sent and the packet
187    * arrives.  FOREVER if we did not measure yet.
188    */
189   struct GNUNET_TIME_RelativeNBO delay;
190
191 };
192
193
194
195 /* ********************* LAN Characterization library ************************ */
196 /* Note: these functions do not really communicate with the ATS service */
197
198
199 /**
200  * Convert ATS properties from host to network byte order.
201  *
202  * @param nbo[OUT] value written
203  * @param hbo value read
204  */
205 void
206 GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
207                             const struct GNUNET_ATS_Properties *hbo);
208
209
210 /**
211  * Convert ATS properties from network to host byte order.
212  *
213  * @param hbo[OUT] value written
214  * @param nbo value read
215  */
216 void
217 GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
218                             const struct GNUNET_ATS_PropertiesNBO *nbo);
219
220
221
222 /**
223  * Convert a `enum GNUNET_ATS_Network_Type` to a string
224  *
225  * @param net the network type
226  * @return a string or NULL if invalid
227  */
228 const char *
229 GNUNET_ATS_print_network_type (enum GNUNET_ATS_Network_Type net);
230
231
232 /**
233  * Handle for the LAN Characterization library.
234  */
235 struct GNUNET_ATS_InterfaceScanner;
236
237
238 /**
239  * Returns where the address is located: loopback, LAN or WAN.
240  *
241  * @param is handle from #GNUNET_ATS_interface_scanner_init()
242  * @param addr address
243  * @param addrlen address length
244  * @return type of the network the address belongs to
245  */
246 enum GNUNET_ATS_Network_Type
247 GNUNET_ATS_scanner_address_get_type (struct GNUNET_ATS_InterfaceScanner *is,
248                                      const struct sockaddr *addr,
249                                      socklen_t addrlen);
250
251
252 /**
253  * Initialize the ATS address characterization client handle.
254  *
255  * @return scanner handle, NULL on error
256  */
257 struct GNUNET_ATS_InterfaceScanner *
258 GNUNET_ATS_scanner_init (void);
259
260
261 /**
262  * Terminate interface scanner.
263  *
264  * @param is scanner we are done with
265  */
266 void
267 GNUNET_ATS_scanner_done (struct GNUNET_ATS_InterfaceScanner *is);
268
269
270
271 /* ********************Connection Suggestion API ***************************** */
272
273 /**
274  * Handle to the ATS subsystem for making suggestions about
275  * connections the peer would like to have.
276  */
277 struct GNUNET_ATS_ConnectivityHandle;
278
279 /**
280  * Handle for address suggestion requests.
281  */
282 struct GNUNET_ATS_ConnectivitySuggestHandle;
283
284
285 /**
286  * Initialize the ATS connectivity suggestion client handle.
287  *
288  * @param cfg configuration to use
289  * @return ats connectivity handle, NULL on error
290  */
291 struct GNUNET_ATS_ConnectivityHandle *
292 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
293
294
295 /**
296  * Shutdown ATS connectivity suggestion client.
297  *
298  * @param ch handle to destroy
299  */
300 void
301 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch);
302
303
304 /**
305  * We would like to establish a new connection with a peer.  ATS
306  * should suggest a good address to begin with.
307  *
308  * @param ch handle
309  * @param peer identity of the peer we need an address for
310  * @param strength how urgent is the need for such a suggestion
311  * @return suggestion handle, NULL if request is already pending
312  */
313 struct GNUNET_ATS_ConnectivitySuggestHandle *
314 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
315                                  const struct GNUNET_PeerIdentity *peer,
316                                  uint32_t strength);
317
318
319 /**
320  * We no longer care about being connected to a peer.
321  *
322  * @param sh handle
323  */
324 void
325 GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh);
326
327
328 /* ******************************** Scheduling API ***************************** */
329
330 /**
331  * Handle to the ATS subsystem for bandwidth/transport scheduling information.
332  */
333 struct GNUNET_ATS_SchedulingHandle;
334
335 /**
336  * Opaque session handle, defined by plugins.  Contents not known to ATS.
337  */
338 struct GNUNET_ATS_Session;
339
340
341 /**
342  * Signature of a function called by ATS with the current bandwidth
343  * and address preferences as determined by ATS.  If our connection
344  * to ATS dies and thus all suggestions become invalid, this function
345  * is called ONCE with all arguments (except @a cls) being NULL/0.
346  *
347  * @param cls closure
348  * @param peer for which we suggest an address, NULL if ATS connection died
349  * @param address suggested address (including peer identity of the peer),
350  *             may be NULL to signal disconnect from peer
351  * @param session session to use, NULL to establish a new outgoing session
352  * @param bandwidth_out assigned outbound bandwidth for the connection,
353  *        0 to signal disconnect
354  * @param bandwidth_in assigned inbound bandwidth for the connection,
355  *        0 to signal disconnect
356  */
357 typedef void
358 (*GNUNET_ATS_AddressSuggestionCallback) (void *cls,
359                                          const struct GNUNET_PeerIdentity *peer,
360                                          const struct GNUNET_HELLO_Address *address,
361                                          struct GNUNET_ATS_Session *session,
362                                          struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
363                                          struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in);
364
365
366 /**
367  * Initialize the ATS scheduling subsystem.
368  *
369  * @param cfg configuration to use
370  * @param suggest_cb notification to call whenever the suggestation changed
371  * @param suggest_cb_cls closure for @a suggest_cb
372  * @return ats context
373  */
374 struct GNUNET_ATS_SchedulingHandle *
375 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
376                             GNUNET_ATS_AddressSuggestionCallback suggest_cb,
377                             void *suggest_cb_cls);
378
379
380 /**
381  * Client is done with ATS scheduling, release resources.
382  *
383  * @param sh handle to release
384  */
385 void
386 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh);
387
388
389 /**
390  * Handle used within ATS to track an address.
391  */
392 struct GNUNET_ATS_AddressRecord;
393
394
395 /**
396  * We have a new address ATS should know. Addresses have to be added with this
397  * function before they can be: updated, set in use and destroyed
398  *
399  * @param sh handle
400  * @param address the address
401  * @param session session handle (if available, i.e. for incoming connections)
402  * @param prop performance data for the address
403  * @return handle to the address representation inside ATS, NULL
404  *         on error (i.e. ATS knows this exact address already, or
405  *         address is invalid)
406  */
407 struct GNUNET_ATS_AddressRecord *
408 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
409                         const struct GNUNET_HELLO_Address *address,
410                         struct GNUNET_ATS_Session *session,
411                         const struct GNUNET_ATS_Properties *prop);
412
413
414 /**
415  * An address was used to initiate a session.
416  *
417  * @param ar address record to update information for
418  * @param session session handle
419  */
420 void
421 GNUNET_ATS_address_add_session (struct GNUNET_ATS_AddressRecord *ar,
422                                 struct GNUNET_ATS_Session *session);
423
424
425 /**
426  * A @a session was destroyed, disassociate it from the given address
427  * record.  If this was an incoming addess, destroys the address as
428  * well.
429  *
430  * @param ar address record to update information for
431  * @param session session handle
432  * @return #GNUNET_YES if the @a ar was destroyed because
433  *                     it was an incoming address,
434  *         #GNUNET_NO if the @ar was kept because we can
435  *                    use it still to establish a new session
436  */
437 int
438 GNUNET_ATS_address_del_session (struct GNUNET_ATS_AddressRecord *ar,
439                                 struct GNUNET_ATS_Session *session);
440
441
442 /**
443  * We have updated performance statistics for a given address.  Note
444  * that this function can be called for addresses that are currently
445  * in use as well as addresses that are valid but not actively in use.
446  * Furthermore, the peer may not even be connected to us right now (@a
447  * session value of NULL used to signal disconnect, or somehow we
448  * otherwise got updated on @a ats information).  Based on the
449  * information provided, ATS may update bandwidth assignments and
450  * suggest to switch addresses.
451  *
452  * @param ar address record to update information for
453  * @param prop performance data for the address
454  */
455 void
456 GNUNET_ATS_address_update (struct GNUNET_ATS_AddressRecord *ar,
457                            const struct GNUNET_ATS_Properties *prop);
458
459
460 /**
461  * An address got destroyed, stop using it as a valid address.
462  *
463  * @param ar address record to destroy, it's validation has
464  *           expired and ATS may no longer use it
465  */
466 void
467 GNUNET_ATS_address_destroy (struct GNUNET_ATS_AddressRecord *ar);
468
469
470
471 /* ******************************** Performance API ***************************** */
472
473 /**
474  * ATS Handle to obtain and/or modify performance information.
475  */
476 struct GNUNET_ATS_PerformanceHandle;
477
478 /**
479  * Signature of a function that is called with QoS information about an address.
480  *
481  * @param cls closure
482  * @param address the address, NULL if ATS service was disconnected or
483  *        when the iteration is completed in the case of
484  *        #GNUNET_ATS_performance_list_addresses()
485  * @param address_active #GNUNET_YES if this address is actively used
486  *        to maintain a connection to a peer;
487  *        #GNUNET_NO if the address is not actively used;
488  *        #GNUNET_SYSERR if this address is no longer available for ATS
489  * @param bandwidth_out assigned outbound bandwidth for the connection
490  * @param bandwidth_in assigned inbound bandwidth for the connection
491  * @param prop performance data for the address
492  */
493 typedef void
494 (*GNUNET_ATS_AddressInformationCallback) (void *cls,
495                                           const struct GNUNET_HELLO_Address *address,
496                                           int address_active,
497                                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
498                                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
499                                           const struct GNUNET_ATS_Properties *prop);
500
501
502 /**
503  * Handle for an address listing operation
504  */
505 struct GNUNET_ATS_AddressListHandle;
506
507
508 /**
509  * Get handle to access performance API of the ATS subsystem.
510  *
511  * @param cfg configuration to use
512  * @param addr_info_cb callback called when performance characteristics for
513  *      an address change
514  * @param addr_info_cb_cls closure for @a addr_info_cb
515  * @return ats performance context
516  */
517 struct GNUNET_ATS_PerformanceHandle *
518 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
519                              GNUNET_ATS_AddressInformationCallback addr_info_cb,
520                              void *addr_info_cb_cls);
521
522
523 /**
524  * Get information about addresses known to the ATS subsystem.
525  *
526  * @param handle the performance handle to use
527  * @param peer peer idm can be NULL for all peers
528  * @param all #GNUNET_YES to get information about all addresses or #GNUNET_NO to
529  *        get only address currently used
530  * @param infocb callback to call with the addresses,
531  *        will callback with address == NULL when done
532  * @param infocb_cls closure for @a infocb
533  * @return handle to abort the operation
534  */
535 struct GNUNET_ATS_AddressListHandle *
536 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *handle,
537                                        const struct GNUNET_PeerIdentity *peer,
538                                        int all,
539                                        GNUNET_ATS_AddressInformationCallback infocb,
540                                        void *infocb_cls);
541
542
543 /**
544  * Cancel a pending address listing operation
545  *
546  * @param handle the `struct GNUNET_ATS_AddressListHandle` handle to cancel
547  */
548 void
549 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *handle);
550
551
552 /**
553  * Client is done using the ATS performance subsystem, release resources.
554  *
555  * @param ph handle
556  */
557 void
558 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph);
559
560
561 /**
562  * Function called with reservation result.
563  *
564  * @param cls closure
565  * @param peer identifies the peer
566  * @param amount set to the amount that was actually reserved or unreserved;
567  *               either the full requested amount or zero (no partial reservations)
568  * @param res_delay if the reservation could not be satisfied (amount was 0), how
569  *        long should the client wait until re-trying?
570  */
571 typedef void
572 (*GNUNET_ATS_ReservationCallback) (void *cls,
573                                    const struct GNUNET_PeerIdentity *peer,
574                                    int32_t amount,
575                                    struct GNUNET_TIME_Relative res_delay);
576
577
578 /**
579  * Context that can be used to cancel a peer information request.
580  */
581 struct GNUNET_ATS_ReservationContext;
582
583
584 /**
585  * Reserve inbound bandwidth from the given peer.  ATS will look at
586  * the current amount of traffic we receive from the peer and ensure
587  * that the peer could add 'amount' of data to its stream.
588  *
589  * @param ph performance handle
590  * @param peer identifies the peer
591  * @param amount reserve N bytes for receiving, negative
592  *                amounts can be used to undo a (recent) reservation;
593  * @param rcb function to call with the resulting reservation information
594  * @param rcb_cls closure for @a rcb
595  * @return NULL on error
596  * @deprecated will be replaced soon
597  */
598 struct GNUNET_ATS_ReservationContext *
599 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
600                               const struct GNUNET_PeerIdentity *peer,
601                               int32_t amount,
602                               GNUNET_ATS_ReservationCallback rcb,
603                               void *rcb_cls);
604
605
606 /**
607  * Cancel request for reserving bandwidth.
608  *
609  * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
610  */
611 void
612 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc);
613
614
615 /**
616  * ATS preference types as array initializer
617  */
618 #define GNUNET_ATS_PreferenceType {GNUNET_ATS_PREFERENCE_BANDWIDTH, GNUNET_ATS_PREFERENCE_LATENCY, GNUNET_ATS_PREFERENCE_END}
619
620 /**
621  * ATS preference types as string array initializer
622  */
623 #define GNUNET_ATS_PreferenceTypeString {"BANDWIDTH", "LATENCY", "END" }
624
625 /**
626  * Enum defining all known preference categories.
627  */
628 enum GNUNET_ATS_PreferenceKind
629 {
630
631   /**
632    * Change the peer's bandwidth value (value per byte of bandwidth in
633    * the goal function) to the given amount.  The argument is followed
634    * by a double value giving the desired value (can be negative).
635    * Preference changes are forgotten if peers disconnect.
636    */
637   GNUNET_ATS_PREFERENCE_BANDWIDTH = 0,
638
639   /**
640    * Change the peer's latency value to the given amount.  The
641    * argument is followed by a double value giving the desired value
642    * (can be negative).  The absolute score in the goal function is
643    * the inverse of the latency in microseconds (minimum: 1
644    * microsecond) multiplied by the latency preferences.
645    */
646   GNUNET_ATS_PREFERENCE_LATENCY = 1,
647
648   /**
649    * End of preference list.
650    */
651   GNUNET_ATS_PREFERENCE_END = 2
652
653 };
654
655
656 /**
657  * Convert a GNUNET_ATS_PreferenceType to a string
658  *
659  * @param type the preference type
660  * @return a string or NULL if invalid
661  */
662 const char *
663 GNUNET_ATS_print_preference_type (enum GNUNET_ATS_PreferenceKind type);
664
665
666 /**
667  * Change preferences for the given peer. Preference changes are forgotten if peers
668  * disconnect.
669  *
670  * @param ph performance handle @param peer identifies the peer
671  * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the
672  * desired changes
673  */
674 void
675 GNUNET_ATS_performance_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
676                                           const struct GNUNET_PeerIdentity *peer,
677                                           ...);
678
679
680 /**
681  * Application feedback on how good preference requirements are fulfilled
682  * for the preferences included in the given time scope [now - scope .. now]
683  *
684  * An application notifies ATS if (and only if) it has feedback information
685  * for specific properties. This values are valid until the feedback scores are
686  * updated by the application.
687  *
688  * If the application has no feedback for this preference kind the application
689  * will not explicitly call for this property and will not include it in this
690  * function call.
691  *
692  * @param ph performance handle
693  * @param scope the time interval this valid for: [now - scope .. now]
694  * @param peer identifies the peer
695  * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
696  */
697 void
698 GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
699                                       const struct GNUNET_PeerIdentity *peer,
700                                       const struct GNUNET_TIME_Relative scope,
701                                       ...);
702
703 /** @} */  /* end of group */
704
705 #endif
706 /* end of file gnunet-service-transport_ats.h */