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 test_bwmgt.c
|
|
|
|
* \brief tests for bandwidth management / token bucket functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define TOKEN_BUCKET_PRIVATE
|
|
|
|
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/or.h"
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "test/test.h"
|
2018-04-10 17:23:14 +02:00
|
|
|
|
2018-07-05 21:14:04 +02:00
|
|
|
#include "lib/evloop/token_bucket.h"
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
// an imaginary time, in timestamp units. Chosen so it will roll over.
|
|
|
|
static const uint32_t START_TS = UINT32_MAX-10;
|
|
|
|
static const int32_t KB = 1024;
|
2018-07-03 16:33:50 +02:00
|
|
|
static const uint32_t GB = (UINT64_C(1) << 30);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
test_bwmgt_token_buf_init(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_t b;
|
2018-04-10 17:23:14 +02:00
|
|
|
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_init(&b, 16*KB, 64*KB, START_TS);
|
2018-04-10 17:23:14 +02:00
|
|
|
// Burst is correct
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_uint_op(b.cfg.burst, OP_EQ, 64*KB);
|
2018-04-10 17:23:14 +02:00
|
|
|
// Rate is correct, within 1 percent.
|
|
|
|
{
|
|
|
|
uint32_t ticks_per_sec =
|
|
|
|
(uint32_t) monotime_msec_to_approx_coarse_stamp_units(1000);
|
2018-04-13 17:30:53 +02:00
|
|
|
uint32_t rate_per_sec = (b.cfg.rate * ticks_per_sec / TICKS_PER_STEP);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
tt_uint_op(rate_per_sec, OP_GT, 16*KB-160);
|
|
|
|
tt_uint_op(rate_per_sec, OP_LT, 16*KB+160);
|
|
|
|
}
|
|
|
|
// Bucket starts out full:
|
2018-04-13 18:03:29 +02:00
|
|
|
tt_uint_op(b.last_refilled_at_timestamp, OP_EQ, START_TS);
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_EQ, 64*KB);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_bwmgt_token_buf_adjust(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_t b;
|
2018-04-10 17:23:14 +02:00
|
|
|
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_init(&b, 16*KB, 64*KB, START_TS);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
2018-04-13 17:30:53 +02:00
|
|
|
uint32_t rate_orig = b.cfg.rate;
|
2018-04-10 17:23:14 +02:00
|
|
|
// Increasing burst
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_adjust(&b, 16*KB, 128*KB);
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_uint_op(b.cfg.rate, OP_EQ, rate_orig);
|
|
|
|
tt_uint_op(b.read_bucket.bucket, OP_EQ, 64*KB);
|
|
|
|
tt_uint_op(b.cfg.burst, OP_EQ, 128*KB);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
// Decreasing burst but staying above bucket
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_adjust(&b, 16*KB, 96*KB);
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_uint_op(b.cfg.rate, OP_EQ, rate_orig);
|
|
|
|
tt_uint_op(b.read_bucket.bucket, OP_EQ, 64*KB);
|
|
|
|
tt_uint_op(b.cfg.burst, OP_EQ, 96*KB);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
// Decreasing burst below bucket,
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_adjust(&b, 16*KB, 48*KB);
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_uint_op(b.cfg.rate, OP_EQ, rate_orig);
|
|
|
|
tt_uint_op(b.read_bucket.bucket, OP_EQ, 48*KB);
|
|
|
|
tt_uint_op(b.cfg.burst, OP_EQ, 48*KB);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
// Changing rate.
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_adjust(&b, 32*KB, 48*KB);
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_uint_op(b.cfg.rate, OP_GE, rate_orig*2 - 10);
|
|
|
|
tt_uint_op(b.cfg.rate, OP_LE, rate_orig*2 + 10);
|
|
|
|
tt_uint_op(b.read_bucket.bucket, OP_EQ, 48*KB);
|
|
|
|
tt_uint_op(b.cfg.burst, OP_EQ, 48*KB);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_bwmgt_token_buf_dec(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_t b;
|
|
|
|
token_bucket_rw_init(&b, 16*KB, 64*KB, START_TS);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
// full-to-not-full.
|
2018-04-13 16:51:59 +02:00
|
|
|
tt_int_op(0, OP_EQ, token_bucket_rw_dec_read(&b, KB));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_EQ, 63*KB);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
// Full to almost-not-full
|
2018-04-13 16:51:59 +02:00
|
|
|
tt_int_op(0, OP_EQ, token_bucket_rw_dec_read(&b, 63*KB - 1));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_EQ, 1);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
// almost-not-full to empty.
|
2018-04-13 16:51:59 +02:00
|
|
|
tt_int_op(1, OP_EQ, token_bucket_rw_dec_read(&b, 1));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_EQ, 0);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
// reset bucket, try full-to-empty
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_init(&b, 16*KB, 64*KB, START_TS);
|
|
|
|
tt_int_op(1, OP_EQ, token_bucket_rw_dec_read(&b, 64*KB));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_EQ, 0);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
// reset bucket, try underflow.
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_init(&b, 16*KB, 64*KB, START_TS);
|
|
|
|
tt_int_op(1, OP_EQ, token_bucket_rw_dec_read(&b, 64*KB + 1));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_EQ, -1);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
// A second underflow does not make the bucket empty.
|
2018-04-13 16:51:59 +02:00
|
|
|
tt_int_op(0, OP_EQ, token_bucket_rw_dec_read(&b, 1000));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_EQ, -1001);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_bwmgt_token_buf_refill(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_t b;
|
2018-08-06 16:39:14 +02:00
|
|
|
const uint32_t BW_SEC =
|
2018-04-10 17:23:14 +02:00
|
|
|
(uint32_t)monotime_msec_to_approx_coarse_stamp_units(1000);
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_init(&b, 16*KB, 64*KB, START_TS);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
/* Make the buffer much emptier, then let one second elapse. */
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_dec_read(&b, 48*KB);
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_EQ, 16*KB);
|
2018-08-06 16:39:14 +02:00
|
|
|
tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b, START_TS + BW_SEC));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_GT, 32*KB - 300);
|
|
|
|
tt_int_op(b.read_bucket.bucket, OP_LT, 32*KB + 300);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
/* Another half second. */
|
2018-08-06 16:39:14 +02:00
|
|
|
tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b, START_TS + BW_SEC*3/2));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_GT, 40*KB - 400);
|
|
|
|
tt_int_op(b.read_bucket.bucket, OP_LT, 40*KB + 400);
|
2018-08-06 16:39:14 +02:00
|
|
|
tt_uint_op(b.last_refilled_at_timestamp, OP_EQ, START_TS + BW_SEC*3/2);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
/* No time: nothing happens. */
|
|
|
|
{
|
2018-04-13 17:30:53 +02:00
|
|
|
const uint32_t bucket_orig = b.read_bucket.bucket;
|
2018-08-06 16:39:14 +02:00
|
|
|
tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b, START_TS + BW_SEC*3/2));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_EQ, bucket_orig);
|
2018-04-10 17:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Another 30 seconds: fill the bucket. */
|
2018-08-06 16:39:14 +02:00
|
|
|
tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b,
|
|
|
|
START_TS + BW_SEC*3/2 + BW_SEC*30));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_EQ, b.cfg.burst);
|
2018-08-06 16:39:14 +02:00
|
|
|
tt_uint_op(b.last_refilled_at_timestamp, OP_EQ,
|
|
|
|
START_TS + BW_SEC*3/2 + BW_SEC*30);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
/* Another 30 seconds: nothing happens. */
|
2018-08-06 16:39:14 +02:00
|
|
|
tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b,
|
|
|
|
START_TS + BW_SEC*3/2 + BW_SEC*60));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_EQ, b.cfg.burst);
|
2018-08-06 16:39:14 +02:00
|
|
|
tt_uint_op(b.last_refilled_at_timestamp, OP_EQ,
|
|
|
|
START_TS + BW_SEC*3/2 + BW_SEC*60);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
/* Empty the bucket, let two seconds pass, and make sure that a refill is
|
|
|
|
* noticed. */
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(1, OP_EQ, token_bucket_rw_dec_read(&b, b.cfg.burst));
|
|
|
|
tt_int_op(0, OP_EQ, b.read_bucket.bucket);
|
2018-08-06 16:39:14 +02:00
|
|
|
tt_int_op(1, OP_EQ, token_bucket_rw_refill(&b,
|
|
|
|
START_TS + BW_SEC*3/2 + BW_SEC*61));
|
|
|
|
tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b,
|
|
|
|
START_TS + BW_SEC*3/2 + BW_SEC*62));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_GT, 32*KB-400);
|
|
|
|
tt_int_op(b.read_bucket.bucket, OP_LT, 32*KB+400);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
/* Underflow the bucket, make sure we detect when it has tokens again. */
|
2018-04-13 18:04:54 +02:00
|
|
|
tt_int_op(1, OP_EQ,
|
|
|
|
token_bucket_rw_dec_read(&b, b.read_bucket.bucket+16*KB));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(-16*KB, OP_EQ, b.read_bucket.bucket);
|
2018-04-10 17:23:14 +02:00
|
|
|
// half a second passes...
|
2018-08-06 16:39:14 +02:00
|
|
|
tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b, START_TS + BW_SEC*64));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_GT, -8*KB-300);
|
|
|
|
tt_int_op(b.read_bucket.bucket, OP_LT, -8*KB+300);
|
2018-04-10 17:23:14 +02:00
|
|
|
// a second passes
|
2018-08-06 16:39:14 +02:00
|
|
|
tt_int_op(1, OP_EQ, token_bucket_rw_refill(&b, START_TS + BW_SEC*65));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_GT, 8*KB-400);
|
|
|
|
tt_int_op(b.read_bucket.bucket, OP_LT, 8*KB+400);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
2018-04-12 19:11:35 +02:00
|
|
|
// We step a second backwards, and nothing happens.
|
2018-08-06 16:39:14 +02:00
|
|
|
tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b, START_TS + BW_SEC*64));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_GT, 8*KB-400);
|
|
|
|
tt_int_op(b.read_bucket.bucket, OP_LT, 8*KB+400);
|
2018-04-12 19:11:35 +02:00
|
|
|
|
|
|
|
// A ridiculous amount of time passes.
|
2018-04-13 16:51:59 +02:00
|
|
|
tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b, INT32_MAX));
|
2018-04-13 17:30:53 +02:00
|
|
|
tt_int_op(b.read_bucket.bucket, OP_EQ, b.cfg.burst);
|
2018-04-10 17:23:14 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2018-04-16 21:02:51 +02:00
|
|
|
/* Test some helper functions we use within the token bucket interface. */
|
|
|
|
static void
|
|
|
|
test_bwmgt_token_buf_helpers(void *arg)
|
|
|
|
{
|
|
|
|
uint32_t ret;
|
|
|
|
|
|
|
|
(void) arg;
|
|
|
|
|
|
|
|
/* The returned value will be OS specific but in any case, it should be
|
|
|
|
* greater than 1 since we are passing 1GB/sec rate. */
|
|
|
|
ret = rate_per_sec_to_rate_per_step(1 * GB);
|
|
|
|
tt_u64_op(ret, OP_GT, 1);
|
|
|
|
|
|
|
|
/* We default to 1 in case rate is 0. */
|
|
|
|
ret = rate_per_sec_to_rate_per_step(0);
|
|
|
|
tt_u64_op(ret, OP_EQ, 1);
|
|
|
|
|
|
|
|
done:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:23:14 +02:00
|
|
|
#define BWMGT(name) \
|
|
|
|
{ #name, test_bwmgt_ ## name , 0, NULL, NULL }
|
|
|
|
|
|
|
|
struct testcase_t bwmgt_tests[] = {
|
|
|
|
BWMGT(token_buf_init),
|
|
|
|
BWMGT(token_buf_adjust),
|
|
|
|
BWMGT(token_buf_dec),
|
|
|
|
BWMGT(token_buf_refill),
|
2018-04-16 21:02:51 +02:00
|
|
|
BWMGT(token_buf_helpers),
|
2018-04-10 17:23:14 +02:00
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|