2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
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 2, or (at your
8 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 General Public License for more details.
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.
22 * @file include/gnunet_bandwidth_lib.h
23 * @brief functions related to bandwidth (unit)
24 * @author Christian Grothoff
27 #ifndef GNUNET_BANDWIDTH_LIB_H
28 #define GNUNET_BANDWIDTH_LIB_H
33 #if 0 /* keep Emacsens' auto-indent happy */
38 #include "gnunet_common.h"
39 #include "gnunet_time_lib.h"
42 * 32-bit bandwidth used for network exchange by GNUnet, in bytes per second.
44 struct GNUNET_BANDWIDTH_Value32NBO
47 * The actual value (bytes per second).
49 uint32_t value__ GNUNET_PACKED;
54 * Struct to track available bandwidth. Combines a time stamp with a
55 * number of bytes transmitted, a quota and a maximum amount that
56 * carries over. Not opaque so that it can be inlined into data
57 * structures (reducing malloc-ing); however, values should not be
58 * accessed directly by clients (hence the '__').
60 struct GNUNET_BANDWIDTH_Tracker
63 * Number of bytes consumed since we last updated the tracker.
65 int64_t consumption_since_last_update__;
68 * Time when we last updated the tracker.
70 struct GNUNET_TIME_Absolute last_update__;
73 * Bandwidth limit to enforce in bytes per s.
75 uint32_t available_bytes_per_s__;
78 * Maximum number of seconds over which bandwidth may "accumulate".
79 * Note that additionally, we also always allow at least
80 * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate.
82 uint32_t max_carry_s__;
87 * Create a new bandwidth value.
89 * @param bytes_per_second value to create
90 * @return the new bandwidth value
92 struct GNUNET_BANDWIDTH_Value32NBO GNUNET_BANDWIDTH_value_init (uint32_t
97 * Maximum possible bandwidth value.
99 #define GNUNET_BANDWIDTH_VALUE_MAX GNUNET_BANDWIDTH_value_init(UINT32_MAX)
103 * At the given bandwidth, calculate how much traffic will be
104 * available until the given deadline.
106 * @param bps bandwidth
107 * @param deadline when is the deadline
108 * @return number of bytes available at bps until deadline
110 uint64_t GNUNET_BANDWIDTH_value_get_available_until (struct
111 GNUNET_BANDWIDTH_Value32NBO
119 * At the given bandwidth, calculate how long it would take for
120 * 'size' bytes to be transmitted.
122 * @param bps bandwidth
123 * @param size number of bytes we want to have available
124 * @return how long it would take
126 struct GNUNET_TIME_Relative GNUNET_BANDWIDTH_value_get_delay_for (struct
127 GNUNET_BANDWIDTH_Value32NBO
135 * Compute the MIN of two bandwidth values.
137 * @param b1 first value
138 * @param b2 second value
139 * @return the min of b1 and b2
141 struct GNUNET_BANDWIDTH_Value32NBO GNUNET_BANDWIDTH_value_min (struct
142 GNUNET_BANDWIDTH_Value32NBO
145 GNUNET_BANDWIDTH_Value32NBO
150 * Initialize bandwidth tracker. Note that in addition to the
151 * 'max_carry_s' limit, we also always allow at least
152 * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the
153 * bytes-per-second limit is so small that within 'max_carry_s' not
154 * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
155 * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
158 * @param av tracker to initialize
159 * @param bytes_per_second_limit initial limit to assume
160 * @param max_carry_s maximum number of seconds unused bandwidth
161 * may accumulate before it expires
163 void GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
164 struct GNUNET_BANDWIDTH_Value32NBO
165 bytes_per_second_limit,
166 uint32_t max_carry_s);
170 * Notify the tracker that a certain number of bytes of bandwidth have
171 * been consumed. Note that it is legal to consume bytes even if not
172 * enough bandwidth is available (in that case,
173 * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
174 * even for a size of zero for a while).
176 * @param av tracker to update
177 * @param size number of bytes consumed
178 * @return GNUNET_YES if this consumption is above the limit
180 int GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
185 * Compute how long we should wait until consuming 'size'
186 * bytes of bandwidth in order to stay within the given
189 * @param av tracker to query
190 * @param size number of bytes we would like to consume
191 * @return time to wait for consumption to be OK
193 struct GNUNET_TIME_Relative GNUNET_BANDWIDTH_tracker_get_delay (struct
194 GNUNET_BANDWIDTH_Tracker
200 * Compute how many bytes are available for consumption right now.
203 * @param av tracker to query
204 * @return number of bytes available for consumption right now
206 int64_t GNUNET_BANDWIDTH_tracker_get_available (struct
207 GNUNET_BANDWIDTH_Tracker
212 * Update quota of bandwidth tracker.
214 * @param av tracker to initialize
215 * @param bytes_per_second_limit new limit to assume
217 void GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
218 struct GNUNET_BANDWIDTH_Value32NBO
219 bytes_per_second_limit);
222 #if 0 /* keep Emacsens' auto-indent happy */
229 /* ifndef GNUNET_BANDWIDTH_LIB_H */
231 /* end of gnunet_bandwidth_lib.h */