2019-01-16 18:33:22 +01:00
|
|
|
/* Copyright (c) 2018-2019, The Tor Project, Inc. */
|
2018-04-10 17:23:14 +02:00
|
|
|
/* 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.
|
|
|
|
*
|
2018-04-13 17:30:53 +02:00
|
|
|
* 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.
|
2018-04-10 17:23:14 +02:00
|
|
|
*
|
|
|
|
* Token buckets may become negative.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#define TOKEN_BUCKET_PRIVATE
|
|
|
|
|
2018-07-05 21:14:04 +02:00
|
|
|
#include "lib/evloop/token_bucket.h"
|
2018-06-22 17:54:38 +02:00
|
|
|
#include "lib/log/util_bug.h"
|
2018-06-29 17:35:49 +02:00
|
|
|
#include "lib/intmath/cmp.h"
|
|
|
|
#include "lib/time/compat_time.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2018-04-10 17:23:14 +02:00
|
|
|
|
2018-04-13 17:30:53 +02:00
|
|
|
/**
|
|
|
|
* 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,
|
2018-04-13 18:03:29 +02:00
|
|
|
const token_bucket_cfg_t *cfg)
|
2018-04-13 17:30:53 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-04-13 17:42:54 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:23:14 +02:00
|
|
|
/** Convert a rate in bytes per second to a rate in bytes per step */
|
2018-04-16 21:02:51 +02:00
|
|
|
STATIC uint32_t
|
2018-04-10 17:23:14 +02:00
|
|
|
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.
|
|
|
|
*/
|
2018-04-16 21:02:51 +02:00
|
|
|
uint64_t units = (uint64_t) rate * TICKS_PER_STEP;
|
2018-04-13 17:38:57 +02:00
|
|
|
uint32_t val = (uint32_t)
|
2018-04-17 17:09:55 +02:00
|
|
|
(monotime_coarse_stamp_units_to_approx_msec(units) / 1000);
|
2018-04-13 17:38:57 +02:00
|
|
|
return val ? val : 1;
|
2018-04-10 17:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_init(token_bucket_rw_t *bucket,
|
2018-04-13 22:58:49 +02:00
|
|
|
uint32_t rate,
|
|
|
|
uint32_t burst,
|
|
|
|
uint32_t now_ts)
|
2018-04-10 17:23:14 +02:00
|
|
|
{
|
2018-04-13 16:51:59 +02:00
|
|
|
memset(bucket, 0, sizeof(token_bucket_rw_t));
|
|
|
|
token_bucket_rw_adjust(bucket, rate, burst);
|
|
|
|
token_bucket_rw_reset(bucket, now_ts);
|
2018-04-10 17:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the configured rate (in bytes per second) and burst (in bytes)
|
|
|
|
* for the token bucket in *<b>bucket</b>.
|
|
|
|
*/
|
|
|
|
void
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_adjust(token_bucket_rw_t *bucket,
|
2018-04-13 17:30:53 +02:00
|
|
|
uint32_t rate,
|
|
|
|
uint32_t burst)
|
2018-04-10 17:23:14 +02:00
|
|
|
{
|
2018-04-13 17:30:53 +02:00
|
|
|
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);
|
2018-04-10 17:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset <b>bucket</b> to be full, as of timestamp <b>now_ts</b>.
|
|
|
|
*/
|
|
|
|
void
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_reset(token_bucket_rw_t *bucket,
|
2018-04-13 17:30:53 +02:00
|
|
|
uint32_t now_ts)
|
2018-04-10 17:23:14 +02:00
|
|
|
{
|
2018-04-13 18:03:29 +02:00
|
|
|
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;
|
2018-04-10 17:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_refill(token_bucket_rw_t *bucket,
|
2018-04-13 18:03:29 +02:00
|
|
|
uint32_t now_ts)
|
2018-04-10 17:23:14 +02:00
|
|
|
{
|
2018-04-13 18:03:29 +02:00
|
|
|
const uint32_t elapsed_ticks =
|
|
|
|
(now_ts - bucket->last_refilled_at_timestamp);
|
2018-04-12 19:11:35 +02:00
|
|
|
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;
|
|
|
|
}
|
2018-04-10 17:23:14 +02:00
|
|
|
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
|
2018-04-13 17:30:53 +02:00
|
|
|
* time in last_refilled_at. That's intentional: we want to make sure
|
2018-04-10 17:23:14 +02:00
|
|
|
* that we add some bytes to it eventually. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flags = 0;
|
2018-04-13 17:30:53 +02:00
|
|
|
if (token_bucket_raw_refill_steps(&bucket->read_bucket,
|
|
|
|
&bucket->cfg, elapsed_steps))
|
2018-04-10 17:23:14 +02:00
|
|
|
flags |= TB_READ;
|
2018-04-13 17:30:53 +02:00
|
|
|
if (token_bucket_raw_refill_steps(&bucket->write_bucket,
|
|
|
|
&bucket->cfg, elapsed_steps))
|
2018-04-10 17:23:14 +02:00
|
|
|
flags |= TB_WRITE;
|
|
|
|
|
2018-04-13 18:03:29 +02:00
|
|
|
bucket->last_refilled_at_timestamp = now_ts;
|
2018-04-10 17:23:14 +02:00
|
|
|
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
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_dec_read(token_bucket_rw_t *bucket,
|
2018-04-13 22:58:49 +02:00
|
|
|
ssize_t n)
|
2018-04-10 17:23:14 +02:00
|
|
|
{
|
2018-04-13 17:30:53 +02:00
|
|
|
return token_bucket_raw_dec(&bucket->read_bucket, n);
|
2018-04-10 17:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_dec_write(token_bucket_rw_t *bucket,
|
2018-04-13 22:58:49 +02:00
|
|
|
ssize_t n)
|
2018-04-10 17:23:14 +02:00
|
|
|
{
|
2018-04-13 17:30:53 +02:00
|
|
|
return token_bucket_raw_dec(&bucket->write_bucket, n);
|
2018-04-10 17:23:14 +02:00
|
|
|
}
|
|
|
|
|
2018-04-10 18:33:30 +02:00
|
|
|
/**
|
2018-04-13 16:51:59 +02:00
|
|
|
* As token_bucket_rw_dec_read and token_bucket_rw_dec_write, in a single
|
2018-04-17 18:02:49 +02:00
|
|
|
* operation. Return a bitmask of TB_READ and TB_WRITE to indicate
|
|
|
|
* which buckets became empty.
|
2018-04-10 18:33:30 +02:00
|
|
|
*/
|
2018-04-17 18:02:49 +02:00
|
|
|
int
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_dec(token_bucket_rw_t *bucket,
|
2018-04-13 17:30:53 +02:00
|
|
|
ssize_t n_read, ssize_t n_written)
|
2018-04-10 18:33:30 +02:00
|
|
|
{
|
2018-04-17 18:02:49 +02:00
|
|
|
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;
|
2018-04-10 18:33:30 +02:00
|
|
|
}
|