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