gnunet_bandwidth_lib.h 9.8 KB

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