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