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