(no commit message)
[oweals/gnunet.git] / src / include / gnunet_ats_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 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 /**
35  * Enum defining all known property types for ATS Enum values are used
36  * in the GNUNET_ATS_Information struct as
37  * (key,value)-pairs.
38  *
39  * Cost are always stored in uint32_t, so all units used to define costs
40  * have to be normalized to fit in uint32_t [0 .. 4.294.967.295]
41  */
42 enum GNUNET_ATS_Property
43 {
44
45   /**
46    * End of the array.
47    * @deprecated
48    */
49   GNUNET_ATS_ARRAY_TERMINATOR = 0,
50
51   /**
52    * Actual traffic on this connection from the other peer to this peer.
53    *
54    * Unit: [bytes/second]
55    */
56   GNUNET_ATS_UTILIZATION_UP,
57
58   /**
59    * Actual traffic on this connection from this peer to the other peer.
60    *
61    * Unit: [bytes/second]
62    */
63   GNUNET_ATS_UTILIZATION_DOWN,
64
65   /**
66    * Delay
67    * Time between when the time packet is sent and the packet arrives
68    *
69    * Unit: [ms]
70    *
71    * Examples:
72    *
73    * LAN   :    1
74    * WLAN  :    2
75    * Dialup:  500
76    */
77   GNUNET_ATS_QUALITY_NET_DELAY,
78
79   /**
80    * Distance on network layer (required for distance-vector routing).
81    *
82    * Unit: [DV-hops]
83    */
84   GNUNET_ATS_QUALITY_NET_DISTANCE,
85
86   /**
87    * Network overhead on WAN (Wide-Area Network)
88    *
89    * How many bytes are sent on the WAN when 1 kilobyte (1024 bytes)
90    * of application data is transmitted?
91    * A factor used with connect cost, bandwidth cost and energy cost
92    * to describe the overhead produced by the transport protocol
93    *
94    * Unit: [bytes/kb]
95    *
96    * Interpretation: less is better
97    *
98    * Examples:
99    *
100    * TCP/IPv4 over Ethernet: 1024 + 38 + 20 + 20 = 1102 [bytes/kb]
101    * TCP/IPv6 over Ethernet: 1024 + 38 + 20 + 40 = 1122 [bytes/kb]
102    * UDP/IPv4 over Ethernet: 1024 + 38 + 20 + 8  = 1090 [bytes/kb]
103    * UDP/IPv6 over Ethernet: 1024 + 38 + 40 + 8  = 1110 [bytes/kb]
104    */
105   GNUNET_ATS_COST_WAN,
106
107   /**
108    * Network overhead on LAN (Local-Area Network)
109    *
110    * How many bytes are sent on the LAN when 1 kilobyte (1024 bytes)
111    * of application data is transmitted?
112    * A factor used with connect cost, bandwidth cost and energy cost
113    * to describe the overhead produced by the transport protocol
114    *
115    * Unit: [bytes/kb]
116    *
117    * Interpretation: less is better
118    *
119    * Examples:
120    *
121    * TCP/IPv4 over Ethernet: 1024 + 38 + 20 + 20 = 1102 [bytes/kb]
122    * TCP/IPv6 over Ethernet: 1024 + 38 + 20 + 40 = 1122 [bytes/kb]
123    * UDP/IPv4 over Ethernet: 1024 + 38 + 20 + 8  = 1090 [bytes/kb]
124    * UDP/IPv6 over Ethernet: 1024 + 38 + 40 + 8  = 1110 [bytes/kb]
125    */
126   GNUNET_ATS_COST_LAN,
127
128   /**
129    * Network overhead on WLAN (Wireless Local Area Network)
130    *
131    * How many bytes are sent on the LAN when 1 kilobyte (1024 bytes)
132    * of application data is transmitted?
133    * A factor used with connect cost, bandwidth cost and energy cost
134    * to describe the overhead produced by the transport protocol
135    *
136    * Unit: [bytes/kb]
137    *
138    * Interpretation: less is better
139    *
140    * Examples:
141    *
142    * TCP/IPv4 over Ethernet: 1024 + 38 + 20 + 20 = 1102 [bytes/kb]
143    * TCP/IPv6 over Ethernet: 1024 + 38 + 20 + 40 = 1122 [bytes/kb]
144    * UDP/IPv4 over Ethernet: 1024 + 38 + 20 + 8  = 1090 [bytes/kb]
145    * UDP/IPv6 over Ethernet: 1024 + 38 + 40 + 8  = 1110 [bytes/kb]
146    */
147   GNUNET_ATS_COST_WLAN
148       /* Cost related values */
149       /* =================== */
150   /**
151    * Volume based cost in financial units to transmit data
152    *
153    * Note: This value is not bound to a specific currency or unit and only
154    * used locally.
155    * "cent" just refers the smallest amount of money in the respective
156    * currency.
157    *
158    * Unit: [cent/MB]
159    *
160    * Interpretation: less is better
161    *
162    * Examples:
163    * LAN:  0 [cent/MB]
164    * 2G : 10 [cent/MB]
165    */
166       // GNUNET_ATS_COST_FINANCIAL_PER_VOLUME = 1,
167   /**
168    * Time based cost in financial units to transmit data
169    *
170    * Note: This value is not bound to a specific currency or unit and only
171    * used locally.
172    * "cent" just refers the smallest amount of money in the respective
173    * currency.
174    *
175    * Unit: [cent/h]
176    *
177    * Interpretation: less is better
178    *
179    * Examples:
180    * LAN   :  0 [cent/h]
181    * Dialup: 10 [cent/h]
182    */
183       // GNUNET_ATS_COST_FINANCIAL_PER_TIME = 2,
184   /**
185    * Computational costs
186    *
187    * Effort of preparing data to be sent with this transport
188    * Includes encoding, encryption and conversion of data
189    * Partial values can be summed up: c_sum = c_enc + c_enc + c_conv
190    * Resulting values depend on local system properties, e.g. CPU
191    *
192    * Unit: [ms/GB]
193    *
194    * Interpretation: less is better
195    *
196    * Examples:
197    *
198    * HTTPS with AES CBC-256:    7,382
199    * HTTPS with AES CBC-128:    5,279
200    * HTTPS with RC4-1024:       2,652
201    */
202       // GNUNET_ATS_COST_COMPUTATIONAL = 3,
203   /**
204    * Energy consumption
205    *
206    * Energy consumption using this transport when sending with a certain
207    * power at a certain bitrate. This is only an approximation based on:
208    * Energy consumption E = P / D
209    *
210    * with:
211    * Power P in Watt (J/s)
212    * Datarate D in MBit/s
213    *
214    * Conversion between power P and dBm used by WLAN in radiotap's dBm TX power:
215    *
216    * Lp(dbm) = 10 log10 (P/ 1mW)
217    *
218    * => P = 1 mW  * 10^(Lp(dbm)/10)
219    *
220    * Unit: [mJ/MB]
221    *
222    * Interpretation: less is better
223    *
224    * Examples:
225    *
226    * LAN:       0
227    * WLAN:      89 (600 mW @ 802.11g /w 54 MBit/s)
228    * Bluetooth: 267 (100 mW @ BT2.0 EDR /w 3 MBit/s)
229    */
230       // GNUNET_ATS_COST_ENERGY_CONSUMPTION = 4,
231   /**
232    * Connect cost
233    * How many bytes are transmitted to initiate a new connection using
234    * this transport?
235    *
236    * Unit: [bytes]
237    *
238    * Interpretation: less is better
239    *
240    * Examples:
241    *
242    * UDP (No connection)      :
243    *     0 bytes
244    * TCP (TCP 3-Way handshake):
245    *   220 bytes Ethernet,  172 bytes TCP/IP,  122 bytes TCP
246    * HTTP (TCP + Header)      :
247    *   477 bytes Ethernet,  429 bytes TCP/IP,  374 bytes TCP,  278 bytes HTTP
248    * HTTPS  HTTP+TLS Handshake:
249    *  2129 bytes Ethernet, 1975 bytes TCP/IP, 1755 bytes TCP, 1403 bytes HTTPS
250    *
251    * */
252       // GNUNET_ATS_COST_CONNECT = 5,
253   /**
254    * Bandwidth cost
255    *
256    * How many bandwidth is available to consume?
257    * Used to calculate which impact sending data with this transport has
258    *
259    * Unit: [kB/s]
260    *
261    * Interpretation: more is better
262    *
263    * Examples:
264    * LAN:     12,800  (100 MBit/s)
265    * WLAN:    6,912   (54 MBit/s)
266    * Dial-up: 8       (64 Kbit/s)
267    *
268    */
269       // GNUNET_ATS_COST_BANDWITH_AVAILABLE = 6,
270   /**
271    *  Network overhead
272    *
273    * How many bytes are sent over the wire when 1 kilobyte (1024 bytes)
274    * of application data is transmitted?
275    * A factor used with connect cost, bandwidth cost and energy cost
276    * to describe the overhead produced by the transport protocol
277    *
278    * Unit: [bytes/kb]
279    *
280    * Interpretation: less is better
281    *
282    * Examples:
283    *
284    * TCP/IPv4 over Ethernet: 1024 + 38 + 20 + 20 = 1102 [bytes/kb]
285    * TCP/IPv6 over Ethernet: 1024 + 38 + 20 + 40 = 1122 [bytes/kb]
286    * UDP/IPv4 over Ethernet: 1024 + 38 + 20 + 8  = 1090 [bytes/kb]
287    * UDP/IPv6 over Ethernet: 1024 + 38 + 40 + 8  = 1110 [bytes/kb]
288    */
289       // GNUNET_ATS_COST_NETWORK_OVERHEAD = 7,
290       /* Quality related values */
291       /* ====================== */
292       /* Physical layer quality properties */
293   /**
294    * Signal strength on physical layer
295    *
296    * Unit: [dBm]
297    */
298       // GNUNET_ATS_QUALITY_PHY_SIGNAL_STRENGTH = 1025,
299   /**
300    * Collision rate on physical layer
301    *
302    * Unit: [B/s]
303    */
304       // GNUNET_ATS_QUALITY_PHY_COLLISION_RATE = 1026,
305   /**
306    * Error rate on physical layer
307    *
308    * Unit: [B/s]
309    */
310       // GNUNET_ATS_QUALITY_PHY_ERROR_RATE = 1027,
311   /**
312    * Jitter
313    * Time variations of the delay
314    * 1st derivative of a delay function
315    *
316    * Unit: [ms]
317    */
318       // GNUNET_ATS_QUALITY_NET_JITTER = 1029,
319   /**
320    * Error rate on network layer
321    *
322    * Unit: [B/s]
323    *
324    * Examples:
325    *
326    * LAN       :    0
327    * WLAN      :  400
328    * Bluetooth :  100
329    * Note: This numbers are just assumptions as an example, not
330    * measured or somehow determined
331    */
332       // GNUNET_ATS_QUALITY_NET_ERRORRATE = 1030,
333   /**
334    * Drop rate on network layer
335    * Bytes actively dismissed by a network component during transmission
336    * Reasons for dropped data can be full queues, congestion, quota violations...
337    *
338    * Unit: [B/s]
339    *
340    * Examples:
341    *
342    * LAN       :    0
343    * WLAN      :  400
344    * Bluetooth :  100
345    * Note: This numbers are just assumptions as an example, not
346    * measured or somehow determined
347    */
348       // GNUNET_ATS_QUALITY_NET_DROPRATE = 1031,
349   /**
350    * Loss rate on network layer
351    * Bytes lost during transmission
352    * Reasons can be collisions, ...
353    *
354    * Unit: [B/s]
355    *
356    * Examples:
357    *
358    * LAN       :    0
359    * WLAN      :   40
360    * Bluetooth :   10
361    * Note: This numbers are just assumptions as an example, not measured
362    * or somehow determined
363    */
364       // GNUNET_ATS_QUALITY_NET_LOSSRATE = 1032,
365   /**
366    * Throughput on network layer
367    *
368    * Unit: [kB/s]
369    *
370    * Examples:
371    *
372    * LAN   : 3400
373    * WLAN  : 1200
374    * Dialup:      4
375    *
376    */
377       // GNUNET_ATS_QUALITY_NET_THROUGHPUT = 1033,
378       /* Availability related values */
379       /* =========================== */
380   /**
381    * Is a peer reachable?
382    */
383       // GNUNET_ATS_AVAILABILITY_REACHABLE = 2048,
384   /**
385    * Is there a connection established to a peer using this transport
386    */
387       // GNUNET_ATS_AVAILABILITY_CONNECTED = 2049
388 };
389
390
391 /**
392  * struct used to communicate the transport's properties like cost and
393  * quality of service as well as high-level constraints on resource
394  * consumption.
395  *
396  *                             +---+
397  *  +-----------+ Constraints  |   |  Plugin properties +---------+
398  *  | Highlevel |------------> |ATS| <------------------|Transport|
399  *  | Component | ATS struct   |   |    ATS struct      | Plugin  |
400  *  +-----------+              |   |                    +---------+
401  *                             +---+
402  *
403  * This structure will be used by transport plugins to communicate
404  * costs to ATS or by higher level components to tell ATS their
405  * constraints.  Always a pair of (GNUNET_ATS_Property,
406  * uint32_t value).  Value is always uint32_t, so all units used to
407  * define costs have to be normalized to fit uint32_t.
408  */
409 struct GNUNET_ATS_Information
410 {
411   /**
412    * ATS property type, in network byte order.
413    */
414   uint32_t type GNUNET_PACKED;
415
416   /**
417    * ATS property value, in network byte order.
418    */
419   uint32_t value GNUNET_PACKED;
420 };
421
422
423
424 /* ******************************** Scheduling API ***************************** */
425
426 /**
427  * Handle to the ATS subsystem for bandwidth/transport scheduling information.
428  */
429 struct GNUNET_ATS_SchedulingHandle;
430
431
432 /**
433  * Opaque session handle, defined by plugins.  Contents not known to ATS.
434  */
435 struct Session;
436
437
438 /**
439  * Signature of a function called by ATS with the current bandwidth
440  * and address preferences as determined by ATS.
441  *
442  * @param cls closure
443  * @param address suggested address (including peer identity of the peer)
444  * @param session session to use
445  * @param bandwidth_out assigned outbound bandwidth for the connection
446  * @param bandwidth_in assigned inbound bandwidth for the connection
447  * @param ats performance data for the address (as far as known)
448  * @param ats_count number of performance records in 'ats'
449  */
450 typedef void (*GNUNET_ATS_AddressSuggestionCallback) (void *cls,
451                                                       const struct GNUNET_HELLO_Address *address,
452                                                       struct Session * session,
453                                                       struct
454                                                       GNUNET_BANDWIDTH_Value32NBO
455                                                       bandwidth_out,
456                                                       struct
457                                                       GNUNET_BANDWIDTH_Value32NBO
458                                                       bandwidth_in,
459                                                       const struct
460                                                       GNUNET_ATS_Information *
461                                                       ats, uint32_t ats_count);
462
463
464 /**
465  * Initialize the ATS subsystem.
466  *
467  * @param cfg configuration to use
468  * @param suggest_cb notification to call whenever the suggestation changed
469  * @param suggest_cb_cls closure for 'suggest_cb'
470  * @return ats context
471  */
472 struct GNUNET_ATS_SchedulingHandle *
473 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
474                             GNUNET_ATS_AddressSuggestionCallback suggest_cb,
475                             void *suggest_cb_cls);
476
477
478 /**
479  * Client is done with ATS scheduling, release resources.
480  *
481  * @param sh handle to release
482  */
483 void
484 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh);
485
486
487 /**
488  * We would like to establish a new connection with a peer.  ATS
489  * should suggest a good address to begin with.
490  *
491  * @param sh handle
492  * @param peer identity of the peer we need an address for
493  */
494 void
495 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
496                             const struct GNUNET_PeerIdentity *peer);
497
498
499 /**
500  * We want to cancel ATS suggesting addresses for a peer.
501  *
502  * @param sh handle
503  * @param peer identity of the peer
504  */
505 void
506 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
507                             const struct GNUNET_PeerIdentity *peer);
508
509 /**
510  * We have updated performance statistics for a given address.  Note
511  * that this function can be called for addresses that are currently
512  * in use as well as addresses that are valid but not actively in use.
513  * Furthermore, the peer may not even be connected to us right now (in
514  * which case the call may be ignored or the information may be stored
515  * for later use).  Update bandwidth assignments.
516  *
517  * @param sh handle
518  * @param address updated address
519  * @param session session handle (if available)
520  * @param ats performance data for the address
521  * @param ats_count number of performance records in 'ats'
522  */
523 void
524 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
525                            const struct GNUNET_HELLO_Address *address,
526                            struct Session *session,
527                            const struct GNUNET_ATS_Information *ats,
528                            uint32_t ats_count);
529
530
531 /**
532  * An address is now in use or not used any more.
533  *
534  * @param sh handle
535  * @param address the address
536  * @param session session handle
537  * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
538  * if address is not used any more
539  */
540 void
541 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
542                            const struct GNUNET_HELLO_Address *address,
543                            struct Session *session,
544                            int in_use);
545
546 /**
547  * A session got destroyed, stop including it as a valid address.
548  *
549  * @param sh handle
550  * @param address the address
551  * @param session session handle that is no longer valid (if available)
552  */
553 void
554 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
555                               const struct GNUNET_HELLO_Address *address,
556                               struct Session *session);
557
558
559 /* ******************************** Performance API ***************************** */
560
561 /**
562  * ATS Handle to obtain and/or modify performance information.
563  */
564 struct GNUNET_ATS_PerformanceHandle;
565
566
567 /**
568  * Signature of a function that is called with QoS information about a peer.
569  *
570  * @param cls closure
571  * @param address the address
572  * @param bandwidth_out assigned outbound bandwidth for the connection
573  * @param bandwidth_in assigned inbound bandwidth for the connection
574  * @param ats performance data for the address (as far as known)
575  * @param ats_count number of performance records in 'ats'
576  */
577 typedef void (*GNUNET_ATS_PeerInformationCallback) (void *cls,
578                                                     const struct GNUNET_HELLO_Address *address,
579                                                     struct
580                                                     GNUNET_BANDWIDTH_Value32NBO
581                                                     bandwidth_out,
582                                                     struct
583                                                     GNUNET_BANDWIDTH_Value32NBO
584                                                     bandwidth_in,
585                                                     const struct
586                                                     GNUNET_ATS_Information *
587                                                     ats, uint32_t ats_count);
588
589
590 /**
591  * Get handle to access performance API of the ATS subsystem.
592  *
593  * @param cfg configuration to use
594  * @param infocb function to call on performance changes, can be NULL
595  * @param infocb_cls closure for infocb
596  * @return ats performance context
597  */
598 struct GNUNET_ATS_PerformanceHandle *
599 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
600                              GNUNET_ATS_PeerInformationCallback infocb,
601                              void *infocb_cls);
602
603
604 /**
605  * Client is done using the ATS performance subsystem, release resources.
606  *
607  * @param ph handle
608  */
609 void
610 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph);
611
612
613 /**
614  * Function called with reservation result.
615  *
616  * @param cls closure
617  * @param peer identifies the peer
618  * @param amount set to the amount that was actually reserved or unreserved;
619  *               either the full requested amount or zero (no partial reservations)
620  * @param res_delay if the reservation could not be satisfied (amount was 0), how
621  *        long should the client wait until re-trying?
622  */
623 typedef void (*GNUNET_ATS_ReservationCallback) (void *cls,
624                                                 const struct GNUNET_PeerIdentity
625                                                 * peer, int32_t amount,
626                                                 struct GNUNET_TIME_Relative
627                                                 res_delay);
628
629
630
631 /**
632  * Context that can be used to cancel a peer information request.
633  */
634 struct GNUNET_ATS_ReservationContext;
635
636
637 /**
638  * Reserve inbound bandwidth from the given peer.  ATS will look at
639  * the current amount of traffic we receive from the peer and ensure
640  * that the peer could add 'amount' of data to its stream.
641  *
642  * @param ph performance handle
643  * @param peer identifies the peer
644  * @param amount reserve N bytes for receiving, negative
645  *                amounts can be used to undo a (recent) reservation;
646  * @param rcb function to call with the resulting reservation information
647  * @param rcb_cls closure for info
648  * @return NULL on error
649  * @deprecated will be replaced soon
650  */
651 struct GNUNET_ATS_ReservationContext *
652 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
653                               const struct GNUNET_PeerIdentity *peer,
654                               int32_t amount,
655                               GNUNET_ATS_ReservationCallback rcb,
656                               void *rcb_cls);
657
658
659 /**
660  * Cancel request for reserving bandwidth.
661  *
662  * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
663  */
664 void
665 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc);
666
667
668
669 /**
670  * Enum defining all known preference categories.
671  */
672 enum GNUNET_ATS_PreferenceKind
673 {
674
675   /**
676    * End of preference list.
677    */
678   GNUNET_ATS_PREFERENCE_END = 0,
679
680   /**
681    * Change the peer's bandwidth value (value per byte of bandwidth in
682    * the goal function) to the given amount.  The argument is followed
683    * by a double value giving the desired value (can be negative).
684    * Preference changes are forgotten if peers disconnect.
685    */
686   GNUNET_ATS_PREFERENCE_BANDWIDTH,
687
688   /**
689    * Change the peer's latency value to the given amount.  The
690    * argument is followed by a double value giving the desired value
691    * (can be negative).  The absolute score in the goal function is
692    * the inverse of the latency in ms (minimum: 1 ms) multiplied by
693    * the latency preferences.
694    */
695   GNUNET_ATS_PREFERENCE_LATENCY
696 };
697
698
699 /**
700  * Change preferences for the given peer. Preference changes are forgotten if peers
701  * disconnect.
702  *
703  * @param ph performance handle
704  * @param peer identifies the peer
705  * @param ... 0-terminated specification of the desired changes
706  */
707 void
708 GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
709                               const struct GNUNET_PeerIdentity *peer, ...);
710
711
712
713 #endif
714 /* end of file gnunet-service-transport_ats.h */