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