paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / include / gnunet_bandwidth_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @author Christian Grothoff
21  *
22  * @file
23  * Functions related to bandwidth (unit)
24  *
25  * @defgroup bandwidth  Bandwidth library
26  * Functions related to bandwidth (unit)
27  * @{
28  */
29
30 #ifndef GNUNET_BANDWIDTH_LIB_H
31 #define GNUNET_BANDWIDTH_LIB_H
32
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #if 0                           /* keep Emacsens' auto-indent happy */
37 }
38 #endif
39 #endif
40
41 #include "gnunet_common.h"
42 #include "gnunet_time_lib.h"
43
44 GNUNET_NETWORK_STRUCT_BEGIN
45
46 /**
47  * 32-bit bandwidth used for network exchange by GNUnet, in bytes per second.
48  */
49 struct GNUNET_BANDWIDTH_Value32NBO
50 {
51   /**
52    * The actual value (bytes per second).
53    */
54   uint32_t value__ GNUNET_PACKED;
55 };
56 GNUNET_NETWORK_STRUCT_END
57
58
59 /**
60  * Callback to be called by the bandwidth tracker if the tracker
61  * was updated and the client should update it's delay values
62  *
63  * @param cls a closure to pass
64  */
65 typedef void
66 (*GNUNET_BANDWIDTH_TrackerUpdateCallback) (void *cls);
67
68
69 /**
70  * Callback to be called by the bandwidth tracker if the tracker
71  * was updated and the client should update it's delay values
72  *
73  * @param cls a closure to pass
74  */
75 typedef void
76 (*GNUNET_BANDWIDTH_ExcessNotificationCallback) (void *cls);
77
78
79 /**
80  * Struct to track available bandwidth.  Combines a time stamp with a
81  * number of bytes transmitted, a quota and a maximum amount that
82  * carries over.  Not opaque so that it can be inlined into data
83  * structures (reducing malloc-ing); however, values should not be
84  * accessed directly by clients (hence the '__').
85  */
86 struct GNUNET_BANDWIDTH_Tracker
87 {
88   /**
89    * Closure for @e update_cb.
90    */
91   void *update_cb_cls;
92
93   /**
94    * Function we call if the tracker's bandwidth is increased and a
95    * previously returned timeout might now expire earlier.
96    */
97   GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb;
98
99   /**
100    * Closure for @e excess_cb.
101    */
102   void *excess_cb_cls;
103
104   /**
105    * Function we call if the tracker is about to throw
106    * away bandwidth due to excess (max carry exceeded).
107    */
108   GNUNET_BANDWIDTH_ExcessNotificationCallback excess_cb;
109
110   /**
111    * Number of bytes consumed since we last updated the tracker.
112    */
113   int64_t consumption_since_last_update__;
114
115   /**
116    * Task scheduled to call the @e excess_cb once we have
117    * reached the maximum bandwidth the tracker can hold.
118    */
119   struct GNUNET_SCHEDULER_Task *excess_task;
120
121   /**
122    * Time when we last updated the tracker.
123    */
124   struct GNUNET_TIME_Absolute last_update__;
125
126   /**
127    * Bandwidth limit to enforce in bytes per second.
128    */
129   uint32_t available_bytes_per_s__;
130
131   /**
132    * Maximum number of seconds over which bandwidth may "accumulate".
133    * Note that additionally, we also always allow at least
134    * #GNUNET_MAX_MESSAGE_SIZE to accumulate.
135    */
136   uint32_t max_carry_s__;
137 };
138
139
140 /**
141  * Convenience definition to use for 0-bandwidth.
142  */
143 #define GNUNET_BANDWIDTH_ZERO GNUNET_BANDWIDTH_value_init (0)
144
145
146 /**
147  * Create a new bandwidth value.
148  *
149  * @param bytes_per_second value to create
150  * @return the new bandwidth value
151  */
152 struct GNUNET_BANDWIDTH_Value32NBO
153 GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second);
154
155
156 /**
157  * Maximum possible bandwidth value.
158  */
159 #define GNUNET_BANDWIDTH_VALUE_MAX GNUNET_BANDWIDTH_value_init(UINT32_MAX)
160
161
162 /**
163  * At the given bandwidth, calculate how much traffic will be
164  * available until the given deadline.
165  *
166  * @param bps bandwidth
167  * @param deadline when is the deadline
168  * @return number of bytes available at bps until deadline
169  */
170 uint64_t
171 GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO bps,
172                                             struct GNUNET_TIME_Relative deadline);
173
174
175 /**
176  * At the given bandwidth, calculate how long it would take for
177  * 'size' bytes to be transmitted.
178  *
179  * @param bps bandwidth
180  * @param size number of bytes we want to have available
181  * @return how long it would take
182  */
183 struct GNUNET_TIME_Relative
184 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
185                                       uint64_t size);
186
187
188 /**
189  * Compute the MIN of two bandwidth values.
190  *
191  * @param b1 first value
192  * @param b2 second value
193  * @return the min of b1 and b2
194  */
195 struct GNUNET_BANDWIDTH_Value32NBO
196 GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
197                             struct GNUNET_BANDWIDTH_Value32NBO b2);
198
199
200 /**
201  * Compute the MAX of two bandwidth values.
202  *
203  * @param b1 first value
204  * @param b2 second value
205  * @return the min of b1 and b2
206  */
207 struct GNUNET_BANDWIDTH_Value32NBO
208 GNUNET_BANDWIDTH_value_max (struct GNUNET_BANDWIDTH_Value32NBO b1,
209                             struct GNUNET_BANDWIDTH_Value32NBO b2);
210
211
212 /**
213  * Initialize bandwidth tracker.  Note that in addition to the
214  * 'max_carry_s' limit, we also always allow at least
215  * #GNUNET_MAX_MESSAGE_SIZE to accumulate.  So if the
216  * bytes-per-second limit is so small that within 'max_carry_s' not
217  * even #GNUNET_MAX_MESSAGE_SIZE is allowed to accumulate, it is
218  * ignored and replaced by #GNUNET_MAX_MESSAGE_SIZE (which is in
219  * bytes).
220  *
221  * @param av tracker to initialize
222  * @param update_cb callback to notify a client about the tracker being updated
223  * @param update_cb_cls cls for the @a update_cb callback
224  * @param bytes_per_second_limit initial limit to assume
225  * @param max_carry_s maximum number of seconds unused bandwidth
226  *        may accumulate before it expires
227  */
228 void
229 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
230                                GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
231                                void *update_cb_cls,
232                                struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
233                                uint32_t max_carry_s);
234
235
236 /**
237  * Initialize bandwidth tracker.  Note that in addition to the
238  * 'max_carry_s' limit, we also always allow at least
239  * GNUNET_MAX_MESSAGE_SIZE to accumulate.  So if the
240  * bytes-per-second limit is so small that within 'max_carry_s' not
241  * even GNUNET_MAX_MESSAGE_SIZE is allowed to accumulate, it is
242  * ignored and replaced by GNUNET_MAX_MESSAGE_SIZE (which is in
243  * bytes).
244  *
245  * @param av tracker to initialize
246  * @param update_cb callback to notify a client about the tracker being updated
247  * @param update_cb_cls cls for the @a update_cb callback
248  * @param bytes_per_second_limit initial limit to assume
249  * @param max_carry_s maximum number of seconds unused bandwidth
250  *        may accumulate before it expires
251  * @param excess_cb callback to notify if we have excess bandwidth
252  * @param excess_cb_cls closure for @a excess_cb
253  */
254 void
255 GNUNET_BANDWIDTH_tracker_init2 (struct GNUNET_BANDWIDTH_Tracker *av,
256                                 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
257                                 void *update_cb_cls,
258                                 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
259                                 uint32_t max_carry_s,
260                                 GNUNET_BANDWIDTH_ExcessNotificationCallback excess_cb,
261                                 void *excess_cb_cls);
262
263
264 /**
265  * Stop notifying about tracker updates and excess notifications
266  *
267  * @param av the respective trackers
268  */
269 void
270 GNUNET_BANDWIDTH_tracker_notification_stop (struct GNUNET_BANDWIDTH_Tracker *av);
271
272
273 /**
274  * Notify the tracker that a certain number of bytes of bandwidth have
275  * been consumed.  Note that it is legal to consume bytes even if not
276  * enough bandwidth is available (in that case,
277  * #GNUNET_BANDWIDTH_tracker_get_delay() may return non-zero delay values
278  * even for a size of zero for a while).
279  *
280  * @param av tracker to update
281  * @param size number of bytes consumed
282  * @return #GNUNET_YES if this consumption is above the limit
283  */
284 int
285 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
286                                   ssize_t size);
287
288
289 /**
290  * Compute how long we should wait until consuming @a size
291  * bytes of bandwidth in order to stay within the given
292  * quota.
293  *
294  * @param av tracker to query
295  * @param size number of bytes we would like to consume
296  * @return time to wait for consumption to be OK
297  */
298 struct GNUNET_TIME_Relative
299 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
300                                     size_t size);
301
302
303 /**
304  * Compute how many bytes are available for consumption right now.
305  * quota.
306  *
307  * @param av tracker to query
308  * @return number of bytes available for consumption right now
309  */
310 int64_t
311 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av);
312
313
314 /**
315  * Update quota of bandwidth tracker.
316  *
317  * @param av tracker to initialize
318  * @param bytes_per_second_limit new limit to assume
319  */
320 void
321 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
322                                        struct GNUNET_BANDWIDTH_Value32NBO
323                                        bytes_per_second_limit);
324
325
326 #if 0                           /* keep Emacsens' auto-indent happy */
327 {
328 #endif
329 #ifdef __cplusplus
330 }
331 #endif
332
333 /* ifndef GNUNET_BANDWIDTH_LIB_H */
334 #endif
335
336 /** @} */  /* end of group */
337
338 /* end of gnunet_bandwidth_lib.h */