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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @author Christian Grothoff
25 * Functions related to bandwidth (unit)
27 * @defgroup bandwidth Bandwidth library
28 * Functions related to bandwidth (unit)
32 #ifndef GNUNET_BANDWIDTH_LIB_H
33 #define GNUNET_BANDWIDTH_LIB_H
37 #if 0 /* keep Emacsens' auto-indent happy */
42 #include "gnunet_common.h"
43 #include "gnunet_time_lib.h"
45 GNUNET_NETWORK_STRUCT_BEGIN
48 * 32-bit bandwidth used for network exchange by GNUnet, in bytes per second.
50 struct GNUNET_BANDWIDTH_Value32NBO
53 * The actual value (bytes per second).
55 uint32_t value__ GNUNET_PACKED;
58 GNUNET_NETWORK_STRUCT_END
62 * Callback to be called by the bandwidth tracker if the tracker
63 * was updated and the client should update it's delay values
65 * @param cls a closure to pass
67 typedef void (*GNUNET_BANDWIDTH_TrackerUpdateCallback) (void *cls);
71 * Callback to be called by the bandwidth tracker if the tracker
72 * was updated and the client should update it's delay values
74 * @param cls a closure to pass
76 typedef void (*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 (
172 struct GNUNET_BANDWIDTH_Value32NBO bps,
173 struct GNUNET_TIME_Relative deadline);
177 * At the given bandwidth, calculate how long it would take for
178 * 'size' bytes to be transmitted.
180 * @param bps bandwidth
181 * @param size number of bytes we want to have available
182 * @return how long it would take
184 struct GNUNET_TIME_Relative
185 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
190 * Compute the MIN of two bandwidth values.
192 * @param b1 first value
193 * @param b2 second value
194 * @return the min of b1 and b2
196 struct GNUNET_BANDWIDTH_Value32NBO
197 GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
198 struct GNUNET_BANDWIDTH_Value32NBO b2);
202 * Compute the MAX of two bandwidth values.
204 * @param b1 first value
205 * @param b2 second value
206 * @return the min of b1 and b2
208 struct GNUNET_BANDWIDTH_Value32NBO
209 GNUNET_BANDWIDTH_value_max (struct GNUNET_BANDWIDTH_Value32NBO b1,
210 struct GNUNET_BANDWIDTH_Value32NBO b2);
214 * Compute the SUM of two bandwidth values.
216 * @param b1 first value
217 * @param b2 second value
218 * @return the sum of b1 and b2
220 struct GNUNET_BANDWIDTH_Value32NBO
221 GNUNET_BANDWIDTH_value_sum (struct GNUNET_BANDWIDTH_Value32NBO b1,
222 struct GNUNET_BANDWIDTH_Value32NBO b2);
226 * Initialize bandwidth tracker. Note that in addition to the
227 * 'max_carry_s' limit, we also always allow at least
228 * #GNUNET_MAX_MESSAGE_SIZE to accumulate. So if the
229 * bytes-per-second limit is so small that within 'max_carry_s' not
230 * even #GNUNET_MAX_MESSAGE_SIZE is allowed to accumulate, it is
231 * ignored and replaced by #GNUNET_MAX_MESSAGE_SIZE (which is in
234 * @param av tracker to initialize
235 * @param update_cb callback to notify a client about the tracker being updated
236 * @param update_cb_cls cls for the @a update_cb callback
237 * @param bytes_per_second_limit initial limit to assume
238 * @param max_carry_s maximum number of seconds unused bandwidth
239 * may accumulate before it expires
242 GNUNET_BANDWIDTH_tracker_init (
243 struct GNUNET_BANDWIDTH_Tracker *av,
244 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
246 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
247 uint32_t max_carry_s);
251 * Initialize bandwidth tracker. Note that in addition to the
252 * 'max_carry_s' limit, we also always allow at least
253 * #GNUNET_MAX_MESSAGE_SIZE to accumulate. So if the
254 * bytes-per-second limit is so small that within 'max_carry_s' not
255 * even #GNUNET_MAX_MESSAGE_SIZE is allowed to accumulate, it is
256 * ignored and replaced by #GNUNET_MAX_MESSAGE_SIZE (which is in
259 * @param av tracker to initialize
260 * @param update_cb callback to notify a client about the tracker being updated
261 * @param update_cb_cls cls for the @a update_cb callback
262 * @param bytes_per_second_limit initial limit to assume
263 * @param max_carry_s maximum number of seconds unused bandwidth
264 * may accumulate before it expires
265 * @param excess_cb callback to notify if we have excess bandwidth
266 * @param excess_cb_cls closure for @a excess_cb
269 GNUNET_BANDWIDTH_tracker_init2 (
270 struct GNUNET_BANDWIDTH_Tracker *av,
271 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
273 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
274 uint32_t max_carry_s,
275 GNUNET_BANDWIDTH_ExcessNotificationCallback excess_cb,
276 void *excess_cb_cls);
280 * Stop notifying about tracker updates and excess notifications
282 * @param av the respective trackers
285 GNUNET_BANDWIDTH_tracker_notification_stop (
286 struct GNUNET_BANDWIDTH_Tracker *av);
290 * Notify the tracker that a certain number of bytes of bandwidth have
291 * been consumed. Note that it is legal to consume bytes even if not
292 * enough bandwidth is available (in that case,
293 * #GNUNET_BANDWIDTH_tracker_get_delay() may return non-zero delay values
294 * even for a size of zero for a while).
296 * @param av tracker to update
297 * @param size number of bytes consumed
298 * @return #GNUNET_YES if this consumption is above the limit
301 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
306 * Compute how long we should wait until consuming @a size
307 * bytes of bandwidth in order to stay within the given
310 * @param av tracker to query
311 * @param size number of bytes we would like to consume
312 * @return time to wait for consumption to be OK
314 struct GNUNET_TIME_Relative
315 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
320 * Compute how many bytes are available for consumption right now.
323 * @param av tracker to query
324 * @return number of bytes available for consumption right now
327 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av);
331 * Update quota of bandwidth tracker.
333 * @param av tracker to initialize
334 * @param bytes_per_second_limit new limit to assume
337 GNUNET_BANDWIDTH_tracker_update_quota (
338 struct GNUNET_BANDWIDTH_Tracker *av,
339 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit);
342 #if 0 /* keep Emacsens' auto-indent happy */
349 /* ifndef GNUNET_BANDWIDTH_LIB_H */
352 /** @} */ /* end of group */
354 /* end of gnunet_bandwidth_lib.h */