token_bucket.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Copyright (c) 2018-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file token_bucket.c
  5. * \brief Functions to use and manipulate token buckets, used for
  6. * rate-limiting on connections and globally.
  7. *
  8. * Tor uses these token buckets to keep track of bandwidth usage, and
  9. * sometimes other things too.
  10. *
  11. * There are two layers of abstraction here: "raw" token buckets, in which all
  12. * the pieces are decoupled, and "read-write" token buckets, which combine all
  13. * the moving parts into one.
  14. *
  15. * Token buckets may become negative.
  16. **/
  17. #define TOKEN_BUCKET_PRIVATE
  18. #include "common/token_bucket.h"
  19. #include "common/compat.h"
  20. #include "lib/log/util_bug.h"
  21. /**
  22. * Set the <b>rate</b> and <b>burst</b> value in a token_bucket_cfg.
  23. *
  24. * Note that the <b>rate</b> value is in arbitrary units, but those units will
  25. * determine the units of token_bucket_raw_dec(), token_bucket_raw_refill, and
  26. * so on.
  27. */
  28. void
  29. token_bucket_cfg_init(token_bucket_cfg_t *cfg,
  30. uint32_t rate,
  31. uint32_t burst)
  32. {
  33. tor_assert_nonfatal(rate > 0);
  34. tor_assert_nonfatal(burst > 0);
  35. if (burst > TOKEN_BUCKET_MAX_BURST)
  36. burst = TOKEN_BUCKET_MAX_BURST;
  37. cfg->rate = rate;
  38. cfg->burst = burst;
  39. }
  40. /**
  41. * Initialize a raw token bucket and its associated timestamp to the "full"
  42. * state, according to <b>cfg</b>.
  43. */
  44. void
  45. token_bucket_raw_reset(token_bucket_raw_t *bucket,
  46. const token_bucket_cfg_t *cfg)
  47. {
  48. bucket->bucket = cfg->burst;
  49. }
  50. /**
  51. * Adust a preexisting token bucket to respect the new configuration
  52. * <b>cfg</b>, by decreasing its current level if needed. */
  53. void
  54. token_bucket_raw_adjust(token_bucket_raw_t *bucket,
  55. const token_bucket_cfg_t *cfg)
  56. {
  57. bucket->bucket = MIN(bucket->bucket, cfg->burst);
  58. }
  59. /**
  60. * Given an amount of <b>elapsed</b> time units, and a bucket configuration
  61. * <b>cfg</b>, refill the level of <b>bucket</b> accordingly. Note that the
  62. * units of time in <b>elapsed</b> must correspond to those used to set the
  63. * rate in <b>cfg</b>, or the result will be illogical.
  64. */
  65. int
  66. token_bucket_raw_refill_steps(token_bucket_raw_t *bucket,
  67. const token_bucket_cfg_t *cfg,
  68. const uint32_t elapsed)
  69. {
  70. const int was_empty = (bucket->bucket <= 0);
  71. /* The casts here prevent an underflow.
  72. *
  73. * Note that even if the bucket value is negative, subtracting it from
  74. * "burst" will still produce a correct result. If this result is
  75. * ridiculously high, then the "elapsed > gap / rate" check below
  76. * should catch it. */
  77. const size_t gap = ((size_t)cfg->burst) - ((size_t)bucket->bucket);
  78. if (elapsed > gap / cfg->rate) {
  79. bucket->bucket = cfg->burst;
  80. } else {
  81. bucket->bucket += cfg->rate * elapsed;
  82. }
  83. return was_empty && bucket->bucket > 0;
  84. }
  85. /**
  86. * Decrement a provided bucket by <b>n</b> units. Note that <b>n</b>
  87. * must be nonnegative.
  88. */
  89. int
  90. token_bucket_raw_dec(token_bucket_raw_t *bucket,
  91. ssize_t n)
  92. {
  93. if (BUG(n < 0))
  94. return 0;
  95. const int becomes_empty = bucket->bucket > 0 && n >= bucket->bucket;
  96. bucket->bucket -= n;
  97. return becomes_empty;
  98. }
  99. /** Convert a rate in bytes per second to a rate in bytes per step */
  100. STATIC uint32_t
  101. rate_per_sec_to_rate_per_step(uint32_t rate)
  102. {
  103. /*
  104. The precise calculation we'd want to do is
  105. (rate / 1000) * to_approximate_msec(TICKS_PER_STEP). But to minimize
  106. rounding error, we do it this way instead, and divide last.
  107. */
  108. uint64_t units = (uint64_t) rate * TICKS_PER_STEP;
  109. uint32_t val = (uint32_t)
  110. (monotime_coarse_stamp_units_to_approx_msec(units) / 1000);
  111. return val ? val : 1;
  112. }
  113. /**
  114. * Initialize a token bucket in *<b>bucket</b>, set up to allow <b>rate</b>
  115. * bytes per second, with a maximum burst of <b>burst</b> bytes. The bucket
  116. * is created such that <b>now_ts</b> is the current timestamp. The bucket
  117. * starts out full.
  118. */
  119. void
  120. token_bucket_rw_init(token_bucket_rw_t *bucket,
  121. uint32_t rate,
  122. uint32_t burst,
  123. uint32_t now_ts)
  124. {
  125. memset(bucket, 0, sizeof(token_bucket_rw_t));
  126. token_bucket_rw_adjust(bucket, rate, burst);
  127. token_bucket_rw_reset(bucket, now_ts);
  128. }
  129. /**
  130. * Change the configured rate (in bytes per second) and burst (in bytes)
  131. * for the token bucket in *<b>bucket</b>.
  132. */
  133. void
  134. token_bucket_rw_adjust(token_bucket_rw_t *bucket,
  135. uint32_t rate,
  136. uint32_t burst)
  137. {
  138. token_bucket_cfg_init(&bucket->cfg,
  139. rate_per_sec_to_rate_per_step(rate),
  140. burst);
  141. token_bucket_raw_adjust(&bucket->read_bucket, &bucket->cfg);
  142. token_bucket_raw_adjust(&bucket->write_bucket, &bucket->cfg);
  143. }
  144. /**
  145. * Reset <b>bucket</b> to be full, as of timestamp <b>now_ts</b>.
  146. */
  147. void
  148. token_bucket_rw_reset(token_bucket_rw_t *bucket,
  149. uint32_t now_ts)
  150. {
  151. token_bucket_raw_reset(&bucket->read_bucket, &bucket->cfg);
  152. token_bucket_raw_reset(&bucket->write_bucket, &bucket->cfg);
  153. bucket->last_refilled_at_timestamp = now_ts;
  154. }
  155. /**
  156. * Refill <b>bucket</b> as appropriate, given that the current timestamp
  157. * is <b>now_ts</b>.
  158. *
  159. * Return a bitmask containing TB_READ iff read bucket was empty and became
  160. * nonempty, and TB_WRITE iff the write bucket was empty and became nonempty.
  161. */
  162. int
  163. token_bucket_rw_refill(token_bucket_rw_t *bucket,
  164. uint32_t now_ts)
  165. {
  166. const uint32_t elapsed_ticks =
  167. (now_ts - bucket->last_refilled_at_timestamp);
  168. if (elapsed_ticks > UINT32_MAX-(300*1000)) {
  169. /* Either about 48 days have passed since the last refill, or the
  170. * monotonic clock has somehow moved backwards. (We're looking at you,
  171. * Windows.). We accept up to a 5 minute jump backwards as
  172. * "unremarkable".
  173. */
  174. return 0;
  175. }
  176. const uint32_t elapsed_steps = elapsed_ticks / TICKS_PER_STEP;
  177. if (!elapsed_steps) {
  178. /* Note that if less than one whole step elapsed, we don't advance the
  179. * time in last_refilled_at. That's intentional: we want to make sure
  180. * that we add some bytes to it eventually. */
  181. return 0;
  182. }
  183. int flags = 0;
  184. if (token_bucket_raw_refill_steps(&bucket->read_bucket,
  185. &bucket->cfg, elapsed_steps))
  186. flags |= TB_READ;
  187. if (token_bucket_raw_refill_steps(&bucket->write_bucket,
  188. &bucket->cfg, elapsed_steps))
  189. flags |= TB_WRITE;
  190. bucket->last_refilled_at_timestamp = now_ts;
  191. return flags;
  192. }
  193. /**
  194. * Decrement the read token bucket in <b>bucket</b> by <b>n</b> bytes.
  195. *
  196. * Return true if the bucket was nonempty and became empty; return false
  197. * otherwise.
  198. */
  199. int
  200. token_bucket_rw_dec_read(token_bucket_rw_t *bucket,
  201. ssize_t n)
  202. {
  203. return token_bucket_raw_dec(&bucket->read_bucket, n);
  204. }
  205. /**
  206. * Decrement the write token bucket in <b>bucket</b> by <b>n</b> bytes.
  207. *
  208. * Return true if the bucket was nonempty and became empty; return false
  209. * otherwise.
  210. */
  211. int
  212. token_bucket_rw_dec_write(token_bucket_rw_t *bucket,
  213. ssize_t n)
  214. {
  215. return token_bucket_raw_dec(&bucket->write_bucket, n);
  216. }
  217. /**
  218. * As token_bucket_rw_dec_read and token_bucket_rw_dec_write, in a single
  219. * operation. Return a bitmask of TB_READ and TB_WRITE to indicate
  220. * which buckets became empty.
  221. */
  222. int
  223. token_bucket_rw_dec(token_bucket_rw_t *bucket,
  224. ssize_t n_read, ssize_t n_written)
  225. {
  226. int flags = 0;
  227. if (token_bucket_rw_dec_read(bucket, n_read))
  228. flags |= TB_READ;
  229. if (token_bucket_rw_dec_write(bucket, n_written))
  230. flags |= TB_WRITE;
  231. return flags;
  232. }