aboutsummaryrefslogtreecommitdiff
path: root/src/lib/evloop/token_bucket.c
blob: a2b330fddb45670765d9b54bd323e64205ef17e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Copyright (c) 2018-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file token_bucket.c
 * \brief Functions to use and manipulate token buckets, used for
 *    rate-limiting on connections and globally.
 *
 * Tor uses these token buckets to keep track of bandwidth usage, and
 * sometimes other things too.
 *
 * There are two layers of abstraction here: "raw" token buckets, in which all
 * the pieces are decoupled, and "read-write" token buckets, which combine all
 * the moving parts into one.
 *
 * Token buckets may become negative.
 **/

#define TOKEN_BUCKET_PRIVATE

#include "lib/evloop/token_bucket.h"
#include "lib/log/util_bug.h"
#include "lib/intmath/cmp.h"
#include "lib/time/compat_time.h"

#include <string.h>

/**
 * Set the <b>rate</b> and <b>burst</b> value in a token_bucket_cfg.
 *
 * Note that the <b>rate</b> value is in arbitrary units, but those units will
 * determine the units of token_bucket_raw_dec(), token_bucket_raw_refill, and
 * so on.
 */
void
token_bucket_cfg_init(token_bucket_cfg_t *cfg,
                      uint32_t rate,
                      uint32_t burst)
{
  tor_assert_nonfatal(rate > 0);
  tor_assert_nonfatal(burst > 0);
  if (burst > TOKEN_BUCKET_MAX_BURST)
    burst = TOKEN_BUCKET_MAX_BURST;

  cfg->rate = rate;
  cfg->burst = burst;
}

/**
 * Initialize a raw token bucket and its associated timestamp to the "full"
 * state, according to <b>cfg</b>.
 */
void
token_bucket_raw_reset(token_bucket_raw_t *bucket,
                       const token_bucket_cfg_t *cfg)
{
  bucket->bucket = cfg->burst;
}

/**
 * Adust a preexisting token bucket to respect the new configuration
 * <b>cfg</b>, by decreasing its current level if needed. */
void
token_bucket_raw_adjust(token_bucket_raw_t *bucket,
                        const token_bucket_cfg_t *cfg)
{
  bucket->bucket = MIN(bucket->bucket, cfg->burst);
}

/**
 * Given an amount of <b>elapsed</b> time units, and a bucket configuration
 * <b>cfg</b>, refill the level of <b>bucket</b> accordingly.  Note that the
 * units of time in <b>elapsed</b> must correspond to those used to set the
 * rate in <b>cfg</b>, or the result will be illogical.
 */
int
token_bucket_raw_refill_steps(token_bucket_raw_t *bucket,
                              const token_bucket_cfg_t *cfg,
                              const uint32_t elapsed)
{
  const int was_empty = (bucket->bucket <= 0);
  /* The casts here prevent an underflow.
   *
   * Note that even if the bucket value is negative, subtracting it from
   * "burst" will still produce a correct result.  If this result is
   * ridiculously high, then the "elapsed > gap / rate" check below
   * should catch it. */
  const size_t gap = ((size_t)cfg->burst) - ((size_t)bucket->bucket);

  if (elapsed > gap / cfg->rate) {
    bucket->bucket = cfg->burst;
  } else {
    bucket->bucket += cfg->rate * elapsed;
  }

  return was_empty && bucket->bucket > 0;
}

/**
 * Decrement a provided bucket by <b>n</b> units.  Note that <b>n</b>
 * must be nonnegative.
 */
int
token_bucket_raw_dec(token_bucket_raw_t *bucket,
                     ssize_t n)
{
  if (BUG(n < 0))
    return 0;
  const int becomes_empty = bucket->bucket > 0 && n >= bucket->bucket;
  bucket->bucket -= n;
  return becomes_empty;
}

/** Convert a rate in bytes per second to a rate in bytes per step */
STATIC uint32_t
rate_per_sec_to_rate_per_step(uint32_t rate)
{
  /*
    The precise calculation we'd want to do is

    (rate / 1000) * to_approximate_msec(TICKS_PER_STEP).  But to minimize
    rounding error, we do it this way instead, and divide last.
  */
  uint64_t units = (uint64_t) rate * TICKS_PER_STEP;
  uint32_t val = (uint32_t)
    (monotime_coarse_stamp_units_to_approx_msec(units) / 1000);
  return val ? val : 1;
}

/**
 * Initialize a token bucket in *<b>bucket</b>, set up to allow <b>rate</b>
 * bytes per second, with a maximum burst of <b>burst</b> bytes. The bucket
 * is created such that <b>now_ts</b> is the current timestamp.  The bucket
 * starts out full.
 */
void
token_bucket_rw_init(token_bucket_rw_t *bucket,
                     uint32_t rate,
                     uint32_t burst,
                     uint32_t now_ts)
{
  memset(bucket, 0, sizeof(token_bucket_rw_t));
  token_bucket_rw_adjust(bucket, rate, burst);
  token_bucket_rw_reset(bucket, now_ts);
}

/**
 * Change the configured rate (in bytes per second) and burst (in bytes)
 * for the token bucket in *<b>bucket</b>.
 */
void
token_bucket_rw_adjust(token_bucket_rw_t *bucket,
                       uint32_t rate,
                       uint32_t burst)
{
  token_bucket_cfg_init(&bucket->cfg,
                        rate_per_sec_to_rate_per_step(rate),
                        burst);
  token_bucket_raw_adjust(&bucket->read_bucket, &bucket->cfg);
  token_bucket_raw_adjust(&bucket->write_bucket, &bucket->cfg);
}

/**
 * Reset <b>bucket</b> to be full, as of timestamp <b>now_ts</b>.
 */
void
token_bucket_rw_reset(token_bucket_rw_t *bucket,
                      uint32_t now_ts)
{
  token_bucket_raw_reset(&bucket->read_bucket, &bucket->cfg);
  token_bucket_raw_reset(&bucket->write_bucket, &bucket->cfg);
  bucket->last_refilled_at_timestamp = now_ts;
}

/**
 * Refill <b>bucket</b> as appropriate, given that the current timestamp
 * is <b>now_ts</b>.
 *
 * Return a bitmask containing TB_READ iff read bucket was empty and became
 * nonempty, and TB_WRITE iff the write bucket was empty and became nonempty.
 */
int
token_bucket_rw_refill(token_bucket_rw_t *bucket,
                       uint32_t now_ts)
{
  const uint32_t elapsed_ticks =
    (now_ts - bucket->last_refilled_at_timestamp);
  if (elapsed_ticks > UINT32_MAX-(300*1000)) {
    /* Either about 48 days have passed since the last refill, or the
     * monotonic clock has somehow moved backwards. (We're looking at you,
     * Windows.).  We accept up to a 5 minute jump backwards as
     * "unremarkable".
     */
    return 0;
  }
  const uint32_t elapsed_steps = elapsed_ticks / TICKS_PER_STEP;

  if (!elapsed_steps) {
    /* Note that if less than one whole step elapsed, we don't advance the
     * time in last_refilled_at. That's intentional: we want to make sure
     * that we add some bytes to it eventually. */
    return 0;
  }

  int flags = 0;
  if (token_bucket_raw_refill_steps(&bucket->read_bucket,
                                    &bucket->cfg, elapsed_steps))
    flags |= TB_READ;
  if (token_bucket_raw_refill_steps(&bucket->write_bucket,
                                    &bucket->cfg, elapsed_steps))
    flags |= TB_WRITE;

  bucket->last_refilled_at_timestamp = now_ts;
  return flags;
}

/**
 * Decrement the read token bucket in <b>bucket</b> by <b>n</b> bytes.
 *
 * Return true if the bucket was nonempty and became empty; return false
 * otherwise.
 */
int
token_bucket_rw_dec_read(token_bucket_rw_t *bucket,
                         ssize_t n)
{
  return token_bucket_raw_dec(&bucket->read_bucket, n);
}

/**
 * Decrement the write token bucket in <b>bucket</b> by <b>n</b> bytes.
 *
 * Return true if the bucket was nonempty and became empty; return false
 * otherwise.
 */
int
token_bucket_rw_dec_write(token_bucket_rw_t *bucket,
                          ssize_t n)
{
  return token_bucket_raw_dec(&bucket->write_bucket, n);
}

/**
 * As token_bucket_rw_dec_read and token_bucket_rw_dec_write, in a single
 * operation.  Return a bitmask of TB_READ and TB_WRITE to indicate
 * which buckets became empty.
 */
int
token_bucket_rw_dec(token_bucket_rw_t *bucket,
                    ssize_t n_read, ssize_t n_written)
{
  int flags = 0;
  if (token_bucket_rw_dec_read(bucket, n_read))
    flags |= TB_READ;
  if (token_bucket_rw_dec_write(bucket, n_written))
    flags |= TB_WRITE;
  return flags;
}

/** Initialize a token bucket in <b>bucket</b>, set up to allow <b>rate</b>
 * per second, with a maximum burst of <b>burst</b>. The bucket is created
 * such that <b>now_ts</b> is the current timestamp. The bucket starts out
 * full. */
void
token_bucket_ctr_init(token_bucket_ctr_t *bucket, uint32_t rate,
                      uint32_t burst, uint32_t now_ts)
{
  memset(bucket, 0, sizeof(token_bucket_ctr_t));
  token_bucket_ctr_adjust(bucket, rate, burst);
  token_bucket_ctr_reset(bucket, now_ts);
}

/** Change the configured rate and burst of the given token bucket object in
 * <b>bucket</b>. */
void
token_bucket_ctr_adjust(token_bucket_ctr_t *bucket, uint32_t rate,
                        uint32_t burst)
{
  token_bucket_cfg_init(&bucket->cfg, rate, burst);
  token_bucket_raw_adjust(&bucket->counter, &bucket->cfg);
}

/** Reset <b>bucket</b> to be full, as of timestamp <b>now_ts</b>. */
void
token_bucket_ctr_reset(token_bucket_ctr_t *bucket, uint32_t now_ts)
{
  token_bucket_raw_reset(&bucket->counter, &bucket->cfg);
  bucket->last_refilled_at_timestamp = now_ts;
}

/** Refill <b>bucket</b> as appropriate, given that the current timestamp is
 * <b>now_ts</b>. */
void
token_bucket_ctr_refill(token_bucket_ctr_t *bucket, uint32_t now_ts)
{
  const uint32_t elapsed_ticks =
    (now_ts - bucket->last_refilled_at_timestamp);
  if (elapsed_ticks > UINT32_MAX-(300*1000)) {
    /* Either about 48 days have passed since the last refill, or the
     * monotonic clock has somehow moved backwards. (We're looking at you,
     * Windows.).  We accept up to a 5 minute jump backwards as
     * "unremarkable".
     */
    return;
  }

  token_bucket_raw_refill_steps(&bucket->counter, &bucket->cfg,
                                elapsed_ticks);
  bucket->last_refilled_at_timestamp = now_ts;
}