gnunet_bandwidth_lib.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. This file is part of GNUnet.
  3. (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 2, 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., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file include/gnunet_bandwidth_lib.h
  19. * @brief functions related to bandwidth (unit)
  20. * @author Christian Grothoff
  21. */
  22. #ifndef GNUNET_BANDWIDTH_LIB_H
  23. #define GNUNET_BANDWIDTH_LIB_H
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #if 0 /* keep Emacsens' auto-indent happy */
  28. }
  29. #endif
  30. #endif
  31. #include "gnunet_common.h"
  32. #include "gnunet_time_lib.h"
  33. /**
  34. * 32-bit bandwidth used for network exchange by GNUnet, in bytes per second.
  35. */
  36. struct GNUNET_BANDWIDTH_Value32NBO
  37. {
  38. /**
  39. * The actual value (bytes per second).
  40. */
  41. uint32_t value__ GNUNET_PACKED;
  42. };
  43. /**
  44. * Struct to track available bandwidth. Combines a time stamp with a
  45. * number of bytes transmitted, a quota and a maximum amount that
  46. * carries over. Not opaque so that it can be inlined into data
  47. * structures (reducing malloc-ing); however, values should not be
  48. * accessed directly by clients (hence the '__').
  49. */
  50. struct GNUNET_BANDWIDTH_Tracker
  51. {
  52. /**
  53. * Number of bytes consumed since we last updated the tracker.
  54. */
  55. int64_t consumption_since_last_update__;
  56. /**
  57. * Time when we last updated the tracker.
  58. */
  59. struct GNUNET_TIME_Absolute last_update__;
  60. /**
  61. * Bandwidth limit to enforce in bytes per s.
  62. */
  63. uint32_t available_bytes_per_s__;
  64. /**
  65. * Maximum number of seconds over which bandwidth may "accumulate".
  66. * Note that additionally, we also always allow at least
  67. * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate.
  68. */
  69. uint32_t max_carry_s__;
  70. };
  71. /**
  72. * Create a new bandwidth value.
  73. *
  74. * @param bytes_per_second value to create
  75. * @return the new bandwidth value
  76. */
  77. struct GNUNET_BANDWIDTH_Value32NBO
  78. GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second);
  79. /**
  80. * Maximum possible bandwidth value.
  81. */
  82. #define GNUNET_BANDWIDTH_VALUE_MAX GNUNET_BANDWIDTH_value_init(UINT32_MAX)
  83. /**
  84. * At the given bandwidth, calculate how much traffic will be
  85. * available until the given deadline.
  86. *
  87. * @param bps bandwidth
  88. * @param deadline when is the deadline
  89. * @return number of bytes available at bps until deadline
  90. */
  91. uint64_t
  92. GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO
  93. bps,
  94. struct GNUNET_TIME_Relative
  95. deadline);
  96. /**
  97. * At the given bandwidth, calculate how long it would take for
  98. * 'size' bytes to be transmitted.
  99. *
  100. * @param bps bandwidth
  101. * @param size number of bytes we want to have available
  102. * @return how long it would take
  103. */
  104. struct GNUNET_TIME_Relative
  105. GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
  106. uint64_t size);
  107. /**
  108. * Compute the MIN of two bandwidth values.
  109. *
  110. * @param b1 first value
  111. * @param b2 second value
  112. * @return the min of b1 and b2
  113. */
  114. struct GNUNET_BANDWIDTH_Value32NBO
  115. GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
  116. struct GNUNET_BANDWIDTH_Value32NBO b2);
  117. /**
  118. * Initialize bandwidth tracker. Note that in addition to the
  119. * 'max_carry_s' limit, we also always allow at least
  120. * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the
  121. * bytes-per-second limit is so small that within 'max_carry_s' not
  122. * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
  123. * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
  124. * bytes).
  125. *
  126. * @param av tracker to initialize
  127. * @param bytes_per_second_limit initial limit to assume
  128. * @param max_carry_s maximum number of seconds unused bandwidth
  129. * may accumulate before it expires
  130. */
  131. void
  132. GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
  133. struct GNUNET_BANDWIDTH_Value32NBO
  134. bytes_per_second_limit, uint32_t max_carry_s);
  135. /**
  136. * Notify the tracker that a certain number of bytes of bandwidth have
  137. * been consumed. Note that it is legal to consume bytes even if not
  138. * enough bandwidth is available (in that case,
  139. * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
  140. * even for a size of zero for a while).
  141. *
  142. * @param av tracker to update
  143. * @param size number of bytes consumed
  144. * @return GNUNET_YES if this consumption is above the limit
  145. */
  146. int
  147. GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
  148. ssize_t size);
  149. /**
  150. * Compute how long we should wait until consuming 'size'
  151. * bytes of bandwidth in order to stay within the given
  152. * quota.
  153. *
  154. * @param av tracker to query
  155. * @param size number of bytes we would like to consume
  156. * @return time to wait for consumption to be OK
  157. */
  158. struct GNUNET_TIME_Relative
  159. GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
  160. size_t size);
  161. /**
  162. * Compute how many bytes are available for consumption right now.
  163. * quota.
  164. *
  165. * @param av tracker to query
  166. * @return number of bytes available for consumption right now
  167. */
  168. int64_t
  169. GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av);
  170. /**
  171. * Update quota of bandwidth tracker.
  172. *
  173. * @param av tracker to initialize
  174. * @param bytes_per_second_limit new limit to assume
  175. */
  176. void
  177. GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
  178. struct GNUNET_BANDWIDTH_Value32NBO
  179. bytes_per_second_limit);
  180. #if 0 /* keep Emacsens' auto-indent happy */
  181. {
  182. #endif
  183. #ifdef __cplusplus
  184. }
  185. #endif
  186. /* ifndef GNUNET_BANDWIDTH_LIB_H */
  187. #endif
  188. /* end of gnunet_bandwidth_lib.h */