1275970Scy/*
2275970Scy * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
3275970Scy *
4275970Scy * Redistribution and use in source and binary forms, with or without
5275970Scy * modification, are permitted provided that the following conditions
6275970Scy * are met:
7275970Scy * 1. Redistributions of source code must retain the above copyright
8275970Scy *    notice, this list of conditions and the following disclaimer.
9275970Scy * 2. Redistributions in binary form must reproduce the above copyright
10275970Scy *    notice, this list of conditions and the following disclaimer in the
11275970Scy *    documentation and/or other materials provided with the distribution.
12275970Scy * 3. The name of the author may not be used to endorse or promote products
13275970Scy *    derived from this software without specific prior written permission.
14275970Scy *
15275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25275970Scy */
26275970Scy#ifndef RATELIM_INTERNAL_H_INCLUDED_
27275970Scy#define RATELIM_INTERNAL_H_INCLUDED_
28275970Scy
29275970Scy#ifdef __cplusplus
30275970Scyextern "C" {
31275970Scy#endif
32275970Scy
33275970Scy#include "event2/util.h"
34275970Scy
35275970Scy/** A token bucket is an internal structure that tracks how many bytes we are
36275970Scy * currently willing to read or write on a given bufferevent or group of
37275970Scy * bufferevents */
38275970Scystruct ev_token_bucket {
39275970Scy	/** How many bytes are we willing to read or write right now? These
40275970Scy	 * values are signed so that we can do "defecit spending" */
41275970Scy	ev_ssize_t read_limit, write_limit;
42275970Scy	/** When was this bucket last updated?  Measured in abstract 'ticks'
43275970Scy	 * relative to the token bucket configuration. */
44275970Scy	ev_uint32_t last_updated;
45275970Scy};
46275970Scy
47275970Scy/** Configuration info for a token bucket or set of token buckets. */
48275970Scystruct ev_token_bucket_cfg {
49275970Scy	/** How many bytes are we willing to read on average per tick? */
50275970Scy	size_t read_rate;
51275970Scy	/** How many bytes are we willing to read at most in any one tick? */
52275970Scy	size_t read_maximum;
53275970Scy	/** How many bytes are we willing to write on average per tick? */
54275970Scy	size_t write_rate;
55275970Scy	/** How many bytes are we willing to write at most in any one tick? */
56275970Scy	size_t write_maximum;
57275970Scy
58275970Scy	/* How long is a tick?  Note that fractions of a millisecond are
59275970Scy	 * ignored. */
60275970Scy	struct timeval tick_timeout;
61275970Scy
62275970Scy	/* How long is a tick, in milliseconds?  Derived from tick_timeout. */
63275970Scy	unsigned msec_per_tick;
64275970Scy};
65275970Scy
66275970Scy/** The current tick is 'current_tick': add bytes to 'bucket' as specified in
67275970Scy * 'cfg'. */
68275970Scyint ev_token_bucket_update_(struct ev_token_bucket *bucket,
69275970Scy    const struct ev_token_bucket_cfg *cfg,
70275970Scy    ev_uint32_t current_tick);
71275970Scy
72275970Scy/** In which tick does 'tv' fall according to 'cfg'?  Note that ticks can
73275970Scy * overflow easily; your code needs to handle this. */
74275970Scyev_uint32_t ev_token_bucket_get_tick_(const struct timeval *tv,
75275970Scy    const struct ev_token_bucket_cfg *cfg);
76275970Scy
77275970Scy/** Adjust 'bucket' to respect 'cfg', and note that it was last updated in
78275970Scy * 'current_tick'.  If 'reinitialize' is true, we are changing the
79275970Scy * configuration of 'bucket'; otherwise, we are setting it up for the first
80275970Scy * time.
81275970Scy */
82275970Scyint ev_token_bucket_init_(struct ev_token_bucket *bucket,
83275970Scy    const struct ev_token_bucket_cfg *cfg,
84275970Scy    ev_uint32_t current_tick,
85275970Scy    int reinitialize);
86275970Scy
87275970Scyint bufferevent_remove_from_rate_limit_group_internal_(struct bufferevent *bev,
88275970Scy    int unsuspend);
89275970Scy
90275970Scy/** Decrease the read limit of 'b' by 'n' bytes */
91275970Scy#define ev_token_bucket_decrement_read(b,n)	\
92275970Scy	do {					\
93275970Scy		(b)->read_limit -= (n);		\
94275970Scy	} while (0)
95275970Scy/** Decrease the write limit of 'b' by 'n' bytes */
96275970Scy#define ev_token_bucket_decrement_write(b,n)	\
97275970Scy	do {					\
98275970Scy		(b)->write_limit -= (n);	\
99275970Scy	} while (0)
100275970Scy
101275970Scy#ifdef __cplusplus
102275970Scy}
103275970Scy#endif
104275970Scy
105275970Scy#endif
106