xref: /freebsd/contrib/ntp/sntp/libevent/ratelim-internal.h (revision 416ba5c74546f32a993436a99516d35008e9f384)
1*2b15cb3dSCy Schubert /*
2*2b15cb3dSCy Schubert  * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
3*2b15cb3dSCy Schubert  *
4*2b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
5*2b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
6*2b15cb3dSCy Schubert  * are met:
7*2b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
8*2b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
9*2b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
10*2b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
11*2b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
12*2b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
13*2b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
14*2b15cb3dSCy Schubert  *
15*2b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*2b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*2b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*2b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*2b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*2b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*2b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*2b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*2b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*2b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*2b15cb3dSCy Schubert  */
26*2b15cb3dSCy Schubert #ifndef RATELIM_INTERNAL_H_INCLUDED_
27*2b15cb3dSCy Schubert #define RATELIM_INTERNAL_H_INCLUDED_
28*2b15cb3dSCy Schubert 
29*2b15cb3dSCy Schubert #ifdef __cplusplus
30*2b15cb3dSCy Schubert extern "C" {
31*2b15cb3dSCy Schubert #endif
32*2b15cb3dSCy Schubert 
33*2b15cb3dSCy Schubert #include "event2/util.h"
34*2b15cb3dSCy Schubert 
35*2b15cb3dSCy Schubert /** A token bucket is an internal structure that tracks how many bytes we are
36*2b15cb3dSCy Schubert  * currently willing to read or write on a given bufferevent or group of
37*2b15cb3dSCy Schubert  * bufferevents */
38*2b15cb3dSCy Schubert struct ev_token_bucket {
39*2b15cb3dSCy Schubert 	/** How many bytes are we willing to read or write right now? These
40*2b15cb3dSCy Schubert 	 * values are signed so that we can do "defecit spending" */
41*2b15cb3dSCy Schubert 	ev_ssize_t read_limit, write_limit;
42*2b15cb3dSCy Schubert 	/** When was this bucket last updated?  Measured in abstract 'ticks'
43*2b15cb3dSCy Schubert 	 * relative to the token bucket configuration. */
44*2b15cb3dSCy Schubert 	ev_uint32_t last_updated;
45*2b15cb3dSCy Schubert };
46*2b15cb3dSCy Schubert 
47*2b15cb3dSCy Schubert /** Configuration info for a token bucket or set of token buckets. */
48*2b15cb3dSCy Schubert struct ev_token_bucket_cfg {
49*2b15cb3dSCy Schubert 	/** How many bytes are we willing to read on average per tick? */
50*2b15cb3dSCy Schubert 	size_t read_rate;
51*2b15cb3dSCy Schubert 	/** How many bytes are we willing to read at most in any one tick? */
52*2b15cb3dSCy Schubert 	size_t read_maximum;
53*2b15cb3dSCy Schubert 	/** How many bytes are we willing to write on average per tick? */
54*2b15cb3dSCy Schubert 	size_t write_rate;
55*2b15cb3dSCy Schubert 	/** How many bytes are we willing to write at most in any one tick? */
56*2b15cb3dSCy Schubert 	size_t write_maximum;
57*2b15cb3dSCy Schubert 
58*2b15cb3dSCy Schubert 	/* How long is a tick?  Note that fractions of a millisecond are
59*2b15cb3dSCy Schubert 	 * ignored. */
60*2b15cb3dSCy Schubert 	struct timeval tick_timeout;
61*2b15cb3dSCy Schubert 
62*2b15cb3dSCy Schubert 	/* How long is a tick, in milliseconds?  Derived from tick_timeout. */
63*2b15cb3dSCy Schubert 	unsigned msec_per_tick;
64*2b15cb3dSCy Schubert };
65*2b15cb3dSCy Schubert 
66*2b15cb3dSCy Schubert /** The current tick is 'current_tick': add bytes to 'bucket' as specified in
67*2b15cb3dSCy Schubert  * 'cfg'. */
68*2b15cb3dSCy Schubert int ev_token_bucket_update_(struct ev_token_bucket *bucket,
69*2b15cb3dSCy Schubert     const struct ev_token_bucket_cfg *cfg,
70*2b15cb3dSCy Schubert     ev_uint32_t current_tick);
71*2b15cb3dSCy Schubert 
72*2b15cb3dSCy Schubert /** In which tick does 'tv' fall according to 'cfg'?  Note that ticks can
73*2b15cb3dSCy Schubert  * overflow easily; your code needs to handle this. */
74*2b15cb3dSCy Schubert ev_uint32_t ev_token_bucket_get_tick_(const struct timeval *tv,
75*2b15cb3dSCy Schubert     const struct ev_token_bucket_cfg *cfg);
76*2b15cb3dSCy Schubert 
77*2b15cb3dSCy Schubert /** Adjust 'bucket' to respect 'cfg', and note that it was last updated in
78*2b15cb3dSCy Schubert  * 'current_tick'.  If 'reinitialize' is true, we are changing the
79*2b15cb3dSCy Schubert  * configuration of 'bucket'; otherwise, we are setting it up for the first
80*2b15cb3dSCy Schubert  * time.
81*2b15cb3dSCy Schubert  */
82*2b15cb3dSCy Schubert int ev_token_bucket_init_(struct ev_token_bucket *bucket,
83*2b15cb3dSCy Schubert     const struct ev_token_bucket_cfg *cfg,
84*2b15cb3dSCy Schubert     ev_uint32_t current_tick,
85*2b15cb3dSCy Schubert     int reinitialize);
86*2b15cb3dSCy Schubert 
87*2b15cb3dSCy Schubert int bufferevent_remove_from_rate_limit_group_internal_(struct bufferevent *bev,
88*2b15cb3dSCy Schubert     int unsuspend);
89*2b15cb3dSCy Schubert 
90*2b15cb3dSCy Schubert /** Decrease the read limit of 'b' by 'n' bytes */
91*2b15cb3dSCy Schubert #define ev_token_bucket_decrement_read(b,n)	\
92*2b15cb3dSCy Schubert 	do {					\
93*2b15cb3dSCy Schubert 		(b)->read_limit -= (n);		\
94*2b15cb3dSCy Schubert 	} while (0)
95*2b15cb3dSCy Schubert /** Decrease the write limit of 'b' by 'n' bytes */
96*2b15cb3dSCy Schubert #define ev_token_bucket_decrement_write(b,n)	\
97*2b15cb3dSCy Schubert 	do {					\
98*2b15cb3dSCy Schubert 		(b)->write_limit -= (n);	\
99*2b15cb3dSCy Schubert 	} while (0)
100*2b15cb3dSCy Schubert 
101*2b15cb3dSCy Schubert #ifdef __cplusplus
102*2b15cb3dSCy Schubert }
103*2b15cb3dSCy Schubert #endif
104*2b15cb3dSCy Schubert 
105*2b15cb3dSCy Schubert #endif
106