2018-04-10 17:23:14 +02:00
|
|
|
/* Copyright (c) 2018, The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
2018-04-13 16:51:59 +02:00
|
|
|
* \file token_bucket_rw.h
|
|
|
|
* \brief Headers for token_bucket_rw.c
|
2018-04-10 17:23:14 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#ifndef TOR_TOKEN_BUCKET_H
|
|
|
|
#define TOR_TOKEN_BUCKET_H
|
|
|
|
|
|
|
|
#include "torint.h"
|
|
|
|
|
2018-04-13 17:30:53 +02:00
|
|
|
/** Largest allowable burst value for a token buffer. */
|
|
|
|
#define TOKEN_BUCKET_MAX_BURST INT32_MAX
|
|
|
|
|
|
|
|
/** A generic token buffer configuration: determines the number of tokens
|
|
|
|
* added to the bucket in each time unit (the "rate"), and the maximum number
|
|
|
|
* of tokens in the bucket (the "burst") */
|
|
|
|
typedef struct token_bucket_cfg_t {
|
2018-04-10 17:23:14 +02:00
|
|
|
uint32_t rate;
|
|
|
|
int32_t burst;
|
2018-04-13 17:30:53 +02:00
|
|
|
} token_bucket_cfg_t;
|
|
|
|
|
|
|
|
/** A raw token bucket, decoupled from its configuration and timestamp. */
|
|
|
|
typedef struct token_bucket_raw_t {
|
|
|
|
int32_t bucket;
|
|
|
|
} token_bucket_raw_t;
|
|
|
|
|
|
|
|
void token_bucket_cfg_init(token_bucket_cfg_t *cfg,
|
|
|
|
uint32_t rate,
|
|
|
|
uint32_t burst);
|
|
|
|
|
|
|
|
void token_bucket_raw_adjust(token_bucket_raw_t *bucket,
|
|
|
|
const token_bucket_cfg_t *cfg);
|
|
|
|
|
|
|
|
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-10 17:23:14 +02:00
|
|
|
|
2018-04-13 17:30:53 +02:00
|
|
|
int token_bucket_raw_dec(token_bucket_raw_t *bucket,
|
|
|
|
ssize_t n);
|
|
|
|
|
|
|
|
int token_bucket_raw_refill_steps(token_bucket_raw_t *bucket,
|
|
|
|
const token_bucket_cfg_t *cfg,
|
|
|
|
const uint32_t elapsed_steps);
|
|
|
|
|
|
|
|
static inline size_t token_bucket_raw_get(const token_bucket_raw_t *bucket);
|
|
|
|
/** Return the current number of bytes set in a token bucket. */
|
|
|
|
static inline size_t
|
|
|
|
token_bucket_raw_get(const token_bucket_raw_t *bucket)
|
|
|
|
{
|
|
|
|
return bucket->bucket >= 0 ? bucket->bucket : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A convenience type containing all the pieces needed for a coupled
|
|
|
|
* read-bucket and write-bucket that have the same rate limit, and which use
|
|
|
|
* "timestamp units" (see compat_time.h) for their time. */
|
|
|
|
typedef struct token_bucket_rw_t {
|
|
|
|
token_bucket_cfg_t cfg;
|
|
|
|
token_bucket_raw_t read_bucket;
|
|
|
|
token_bucket_raw_t write_bucket;
|
2018-04-13 18:03:29 +02:00
|
|
|
uint32_t last_refilled_at_timestamp;
|
2018-04-13 17:30:53 +02:00
|
|
|
} token_bucket_rw_t;
|
2018-04-10 17:23:14 +02:00
|
|
|
|
2018-04-13 16:51:59 +02:00
|
|
|
void token_bucket_rw_init(token_bucket_rw_t *bucket,
|
2018-04-10 17:23:14 +02:00
|
|
|
uint32_t rate,
|
|
|
|
uint32_t burst,
|
|
|
|
uint32_t now_ts);
|
|
|
|
|
2018-04-13 16:51:59 +02:00
|
|
|
void token_bucket_rw_adjust(token_bucket_rw_t *bucket,
|
2018-04-10 17:23:14 +02:00
|
|
|
uint32_t rate, uint32_t burst);
|
|
|
|
|
2018-04-13 16:51:59 +02:00
|
|
|
void token_bucket_rw_reset(token_bucket_rw_t *bucket,
|
2018-04-10 17:23:14 +02:00
|
|
|
uint32_t now_ts);
|
|
|
|
|
|
|
|
#define TB_READ 1
|
|
|
|
#define TB_WRITE 2
|
|
|
|
|
2018-04-13 16:51:59 +02:00
|
|
|
int token_bucket_rw_refill(token_bucket_rw_t *bucket,
|
2018-04-10 17:23:14 +02:00
|
|
|
uint32_t now_ts);
|
|
|
|
|
2018-04-13 16:51:59 +02:00
|
|
|
int token_bucket_rw_dec_read(token_bucket_rw_t *bucket,
|
2018-04-10 17:23:14 +02:00
|
|
|
ssize_t n);
|
2018-04-13 16:51:59 +02:00
|
|
|
int token_bucket_rw_dec_write(token_bucket_rw_t *bucket,
|
2018-04-10 17:23:14 +02:00
|
|
|
ssize_t n);
|
|
|
|
|
2018-04-13 16:51:59 +02:00
|
|
|
void token_bucket_rw_dec(token_bucket_rw_t *bucket,
|
2018-04-10 18:33:30 +02:00
|
|
|
ssize_t n_read, ssize_t n_written);
|
|
|
|
|
2018-04-13 16:51:59 +02:00
|
|
|
static inline size_t token_bucket_rw_get_read(const token_bucket_rw_t *bucket);
|
2018-04-10 17:23:14 +02:00
|
|
|
static inline size_t
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_get_read(const token_bucket_rw_t *bucket)
|
2018-04-10 17:23:14 +02:00
|
|
|
{
|
2018-04-13 17:30:53 +02:00
|
|
|
return token_bucket_raw_get(&bucket->read_bucket);
|
2018-04-10 17:23:14 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 16:51:59 +02:00
|
|
|
static inline size_t token_bucket_rw_get_write(
|
|
|
|
const token_bucket_rw_t *bucket);
|
2018-04-10 17:23:14 +02:00
|
|
|
static inline size_t
|
2018-04-13 16:51:59 +02:00
|
|
|
token_bucket_rw_get_write(const token_bucket_rw_t *bucket)
|
2018-04-10 17:23:14 +02:00
|
|
|
{
|
2018-04-13 17:30:53 +02:00
|
|
|
return token_bucket_raw_get(&bucket->write_bucket);
|
2018-04-10 17:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TOKEN_BUCKET_PRIVATE
|
|
|
|
|
|
|
|
/* To avoid making the rates too small, we consider units of "steps",
|
|
|
|
* where a "step" is defined as this many timestamp ticks. Keep this
|
|
|
|
* a power of two if you can. */
|
|
|
|
#define TICKS_PER_STEP 16
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* TOR_TOKEN_BUCKET_H */
|
|
|
|
|