Tor 0.4.9.0-alpha-dev
Data Structures | Macros | Functions
token_bucket.h File Reference

Headers for token_bucket.c. More...

#include "lib/cc/torint.h"
#include "lib/testsupport/testsupport.h"

Go to the source code of this file.

Data Structures

struct  token_bucket_cfg_t
 
struct  token_bucket_raw_t
 
struct  token_bucket_rw_t
 
struct  token_bucket_ctr_t
 

Macros

#define TOKEN_BUCKET_MAX_BURST   INT32_MAX
 
#define TB_READ   1
 
#define TB_WRITE   2
 

Functions

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, const token_bucket_cfg_t *cfg)
 
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 size_t token_bucket_raw_get (const token_bucket_raw_t *bucket)
 
void token_bucket_rw_init (token_bucket_rw_t *bucket, uint32_t rate, uint32_t burst, uint32_t now_ts_stamp)
 
void token_bucket_rw_adjust (token_bucket_rw_t *bucket, uint32_t rate, uint32_t burst)
 
void token_bucket_rw_reset (token_bucket_rw_t *bucket, uint32_t now_ts_stamp)
 
int token_bucket_rw_refill (token_bucket_rw_t *bucket, uint32_t now_ts_stamp)
 
int token_bucket_rw_dec_read (token_bucket_rw_t *bucket, ssize_t n)
 
int token_bucket_rw_dec_write (token_bucket_rw_t *bucket, ssize_t n)
 
int token_bucket_rw_dec (token_bucket_rw_t *bucket, ssize_t n_read, ssize_t n_written)
 
static size_t token_bucket_rw_get_read (const token_bucket_rw_t *bucket)
 
static size_t token_bucket_rw_get_write (const token_bucket_rw_t *bucket)
 
void token_bucket_ctr_init (token_bucket_ctr_t *bucket, uint32_t rate, uint32_t burst, uint32_t now_ts_sec)
 
void token_bucket_ctr_adjust (token_bucket_ctr_t *bucket, uint32_t rate, uint32_t burst)
 
void token_bucket_ctr_reset (token_bucket_ctr_t *bucket, uint32_t now_ts_sec)
 
void token_bucket_ctr_refill (token_bucket_ctr_t *bucket, uint32_t now_ts_sec)
 
static bool token_bucket_ctr_dec (token_bucket_ctr_t *bucket, ssize_t n)
 
static size_t token_bucket_ctr_get (const token_bucket_ctr_t *bucket)
 

Detailed Description

Headers for token_bucket.c.

Definition in file token_bucket.h.

Macro Definition Documentation

◆ TB_READ

#define TB_READ   1

Definition at line 77 of file token_bucket.h.

◆ TB_WRITE

#define TB_WRITE   2

Definition at line 78 of file token_bucket.h.

◆ TOKEN_BUCKET_MAX_BURST

#define TOKEN_BUCKET_MAX_BURST   INT32_MAX

Largest allowable burst value for a token buffer.

Definition at line 16 of file token_bucket.h.

Function Documentation

◆ token_bucket_cfg_init()

void token_bucket_cfg_init ( token_bucket_cfg_t cfg,
uint32_t  rate,
uint32_t  burst 
)

Set the rate and burst value in a token_bucket_cfg.

Note that the rate value is in arbitrary units, but those units will determine the units of token_bucket_raw_dec(), token_bucket_raw_refill, and so on.

Definition at line 36 of file token_bucket.c.

Referenced by token_bucket_ctr_adjust(), and token_bucket_rw_adjust().

◆ token_bucket_ctr_adjust()

void token_bucket_ctr_adjust ( token_bucket_ctr_t bucket,
uint32_t  rate,
uint32_t  burst 
)

Change the configured rate and burst of the given token bucket object in bucket.

Definition at line 278 of file token_bucket.c.

Referenced by token_bucket_ctr_init().

◆ token_bucket_ctr_dec()

static bool token_bucket_ctr_dec ( token_bucket_ctr_t bucket,
ssize_t  n 
)
inlinestatic

Definition at line 124 of file token_bucket.h.

◆ token_bucket_ctr_get()

static size_t token_bucket_ctr_get ( const token_bucket_ctr_t bucket)
inlinestatic

Definition at line 130 of file token_bucket.h.

◆ token_bucket_ctr_init()

void token_bucket_ctr_init ( token_bucket_ctr_t bucket,
uint32_t  rate,
uint32_t  burst,
uint32_t  now_ts_sec 
)

Initialize a token bucket in bucket, set up to allow rate per second, with a maximum burst of burst. The bucket is created such that now_ts_sec is the current timestamp. The bucket starts out full. Note that these counters use seconds instead of approximate milliseconds, in order to allow a lower minimum rate than the rw counters.

Definition at line 267 of file token_bucket.c.

◆ token_bucket_ctr_refill()

void token_bucket_ctr_refill ( token_bucket_ctr_t bucket,
uint32_t  now_ts_sec 
)

Refill bucket as appropriate, given that the current timestamp is now_ts_sec in seconds.

Definition at line 296 of file token_bucket.c.

◆ token_bucket_ctr_reset()

void token_bucket_ctr_reset ( token_bucket_ctr_t bucket,
uint32_t  now_ts_sec 
)

Reset bucket to be full, as of timestamp now_ts_sec.

Definition at line 287 of file token_bucket.c.

Referenced by token_bucket_ctr_init().

◆ token_bucket_raw_adjust()

void token_bucket_raw_adjust ( token_bucket_raw_t bucket,
const token_bucket_cfg_t cfg 
)

Adust a preexisting token bucket to respect the new configuration cfg, by decreasing its current level if needed.

Definition at line 64 of file token_bucket.c.

Referenced by token_bucket_ctr_adjust(), and token_bucket_rw_adjust().

◆ token_bucket_raw_dec()

int token_bucket_raw_dec ( token_bucket_raw_t bucket,
ssize_t  n 
)

Decrement a provided bucket by n units. Note that n must be nonnegative.

Definition at line 104 of file token_bucket.c.

Referenced by token_bucket_rw_dec_read(), and token_bucket_rw_dec_write().

◆ token_bucket_raw_get()

static size_t token_bucket_raw_get ( const token_bucket_raw_t bucket)
inlinestatic

Return the current number of bytes set in a token bucket.

Definition at line 51 of file token_bucket.h.

◆ token_bucket_raw_refill_steps()

int token_bucket_raw_refill_steps ( token_bucket_raw_t bucket,
const token_bucket_cfg_t cfg,
const uint32_t  elapsed 
)

Given an amount of elapsed time units, and a bucket configuration cfg, refill the level of bucket accordingly. Note that the units of time in elapsed must correspond to those used to set the rate in cfg, or the result will be illogical.

Definition at line 77 of file token_bucket.c.

Referenced by token_bucket_ctr_refill(), and token_bucket_rw_refill().

◆ token_bucket_raw_reset()

void token_bucket_raw_reset ( token_bucket_raw_t bucket,
const token_bucket_cfg_t cfg 
)

Initialize a raw token bucket and its associated timestamp to the "full" state, according to cfg.

Definition at line 54 of file token_bucket.c.

Referenced by token_bucket_ctr_reset(), and token_bucket_rw_reset().

◆ token_bucket_rw_adjust()

void token_bucket_rw_adjust ( token_bucket_rw_t bucket,
uint32_t  rate,
uint32_t  burst 
)

Change the configured rate (in bytes per second) and burst (in bytes) for the token bucket in *bucket.

Definition at line 154 of file token_bucket.c.

Referenced by connection_bucket_adjust(), connection_or_update_token_buckets_helper(), and token_bucket_rw_init().

◆ token_bucket_rw_dec()

int token_bucket_rw_dec ( token_bucket_rw_t bucket,
ssize_t  n_read,
ssize_t  n_written 
)

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.

Definition at line 249 of file token_bucket.c.

◆ token_bucket_rw_dec_read()

int token_bucket_rw_dec_read ( token_bucket_rw_t bucket,
ssize_t  n 
)

Decrement the read token bucket in bucket by n bytes.

Return true if the bucket was nonempty and became empty; return false otherwise.

Definition at line 224 of file token_bucket.c.

Referenced by token_bucket_rw_dec().

◆ token_bucket_rw_dec_write()

int token_bucket_rw_dec_write ( token_bucket_rw_t bucket,
ssize_t  n 
)

Decrement the write token bucket in bucket by n bytes.

Return true if the bucket was nonempty and became empty; return false otherwise.

Definition at line 237 of file token_bucket.c.

◆ token_bucket_rw_get_read()

static size_t token_bucket_rw_get_read ( const token_bucket_rw_t bucket)
inlinestatic

Definition at line 93 of file token_bucket.h.

◆ token_bucket_rw_get_write()

static size_t token_bucket_rw_get_write ( const token_bucket_rw_t bucket)
inlinestatic

Definition at line 101 of file token_bucket.h.

◆ token_bucket_rw_init()

void token_bucket_rw_init ( token_bucket_rw_t bucket,
uint32_t  rate,
uint32_t  burst,
uint32_t  now_ts_stamp 
)

Initialize a token bucket in *bucket, set up to allow rate bytes per second, with a maximum burst of burst bytes. The bucket is created such that now_ts_stamp is the current time in coarse stamp units. The bucket starts out full.

Definition at line 139 of file token_bucket.c.

Referenced by connection_bucket_init().

◆ token_bucket_rw_refill()

int token_bucket_rw_refill ( token_bucket_rw_t bucket,
uint32_t  now_ts_stamp 
)

Refill bucket as appropriate, given that the current timestamp is now_ts_stamp in coarse timestamp units.

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.

Definition at line 185 of file token_bucket.c.

Referenced by connection_bucket_refill_single().

◆ token_bucket_rw_reset()

void token_bucket_rw_reset ( token_bucket_rw_t bucket,
uint32_t  now_ts_stamp 
)

Reset bucket to be full, as of timestamp now_ts_stamp.

Definition at line 169 of file token_bucket.c.

Referenced by connection_or_update_token_buckets_helper(), and token_bucket_rw_init().