2 This file is part of GNUnet.
3 Copyright (C) 2010 GNUnet e.V.
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.
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.
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/>.
20 * @author Christian Grothoff
23 * Functions related to bandwidth (unit)
25 * @defgroup bandwidth Bandwidth library
26 * Functions related to bandwidth (unit)
30 #ifndef GNUNET_BANDWIDTH_LIB_H
31 #define GNUNET_BANDWIDTH_LIB_H
36 #if 0 /* keep Emacsens' auto-indent happy */
41 #include "gnunet_common.h"
42 #include "gnunet_time_lib.h"
44 GNUNET_NETWORK_STRUCT_BEGIN
47 * 32-bit bandwidth used for network exchange by GNUnet, in bytes per second.
49 struct GNUNET_BANDWIDTH_Value32NBO
52 * The actual value (bytes per second).
54 uint32_t value__ GNUNET_PACKED;
56 GNUNET_NETWORK_STRUCT_END
60 * Callback to be called by the bandwidth tracker if the tracker
61 * was updated and the client should update it's delay values
63 * @param cls a closure to pass
66 (*GNUNET_BANDWIDTH_TrackerUpdateCallback) (void *cls);
70 * Callback to be called by the bandwidth tracker if the tracker
71 * was updated and the client should update it's delay values
73 * @param cls a closure to pass
76 (*GNUNET_BANDWIDTH_ExcessNotificationCallback) (void *cls);
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 '__').
86 struct GNUNET_BANDWIDTH_Tracker
89 * Closure for @e update_cb.
94 * Function we call if the tracker's bandwidth is increased and a
95 * previously returned timeout might now expire earlier.
97 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb;
100 * Closure for @e excess_cb.
105 * Function we call if the tracker is about to throw
106 * away bandwidth due to excess (max carry exceeded).
108 GNUNET_BANDWIDTH_ExcessNotificationCallback excess_cb;
111 * Number of bytes consumed since we last updated the tracker.
113 int64_t consumption_since_last_update__;
116 * Task scheduled to call the @e excess_cb once we have
117 * reached the maximum bandwidth the tracker can hold.
119 struct GNUNET_SCHEDULER_Task *excess_task;
122 * Time when we last updated the tracker.
124 struct GNUNET_TIME_Absolute last_update__;
127 * Bandwidth limit to enforce in bytes per second.
129 uint32_t available_bytes_per_s__;
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.
136 uint32_t max_carry_s__;
141 * Convenience definition to use for 0-bandwidth.
143 #define GNUNET_BANDWIDTH_ZERO GNUNET_BANDWIDTH_value_init (0)
147 * Create a new bandwidth value.
149 * @param bytes_per_second value to create
150 * @return the new bandwidth value
152 struct GNUNET_BANDWIDTH_Value32NBO
153 GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second);
157 * Maximum possible bandwidth value.
159 #define GNUNET_BANDWIDTH_VALUE_MAX GNUNET_BANDWIDTH_value_init(UINT32_MAX)
163 * At the given bandwidth, calculate how much traffic will be
164 * available until the given deadline.
166 * @param bps bandwidth
167 * @param deadline when is the deadline
168 * @return number of bytes available at bps until deadline
171 GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO bps,
172 struct GNUNET_TIME_Relative deadline);
176 * At the given bandwidth, calculate how long it would take for
177 * 'size' bytes to be transmitted.
179 * @param bps bandwidth
180 * @param size number of bytes we want to have available
181 * @return how long it would take
183 struct GNUNET_TIME_Relative
184 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
189 * Compute the MIN of two bandwidth values.
191 * @param b1 first value
192 * @param b2 second value
193 * @return the min of b1 and b2
195 struct GNUNET_BANDWIDTH_Value32NBO
196 GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
197 struct GNUNET_BANDWIDTH_Value32NBO b2);
201 * Compute the MAX of two bandwidth values.
203 * @param b1 first value
204 * @param b2 second value
205 * @return the min of b1 and b2
207 struct GNUNET_BANDWIDTH_Value32NBO
208 GNUNET_BANDWIDTH_value_max (struct GNUNET_BANDWIDTH_Value32NBO b1,
209 struct GNUNET_BANDWIDTH_Value32NBO b2);
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
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
229 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
230 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
232 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
233 uint32_t max_carry_s);
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
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
255 GNUNET_BANDWIDTH_tracker_init2 (struct GNUNET_BANDWIDTH_Tracker *av,
256 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
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);
265 * Stop notifying about tracker updates and excess notifications
267 * @param av the respective trackers
270 GNUNET_BANDWIDTH_tracker_notification_stop (struct GNUNET_BANDWIDTH_Tracker *av);
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).
280 * @param av tracker to update
281 * @param size number of bytes consumed
282 * @return #GNUNET_YES if this consumption is above the limit
285 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
290 * Compute how long we should wait until consuming @a size
291 * bytes of bandwidth in order to stay within the given
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
298 struct GNUNET_TIME_Relative
299 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
304 * Compute how many bytes are available for consumption right now.
307 * @param av tracker to query
308 * @return number of bytes available for consumption right now
311 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av);
315 * Update quota of bandwidth tracker.
317 * @param av tracker to initialize
318 * @param bytes_per_second_limit new limit to assume
321 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
322 struct GNUNET_BANDWIDTH_Value32NBO
323 bytes_per_second_limit);
326 #if 0 /* keep Emacsens' auto-indent happy */
333 /* ifndef GNUNET_BANDWIDTH_LIB_H */
336 /** @} */ /* end of group */
338 /* end of gnunet_bandwidth_lib.h */