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