1290001Sglebius/* 2290001Sglebius * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson 3290001Sglebius * 4290001Sglebius * Redistribution and use in source and binary forms, with or without 5290001Sglebius * modification, are permitted provided that the following conditions 6290001Sglebius * are met: 7290001Sglebius * 1. Redistributions of source code must retain the above copyright 8290001Sglebius * notice, this list of conditions and the following disclaimer. 9290001Sglebius * 2. Redistributions in binary form must reproduce the above copyright 10290001Sglebius * notice, this list of conditions and the following disclaimer in the 11290001Sglebius * documentation and/or other materials provided with the distribution. 12290001Sglebius * 3. The name of the author may not be used to endorse or promote products 13290001Sglebius * derived from this software without specific prior written permission. 14290001Sglebius * 15290001Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16290001Sglebius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17290001Sglebius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18290001Sglebius * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19290001Sglebius * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20290001Sglebius * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21290001Sglebius * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22290001Sglebius * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23290001Sglebius * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24290001Sglebius * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25290001Sglebius */ 26290001Sglebius#ifndef RATELIM_INTERNAL_H_INCLUDED_ 27290001Sglebius#define RATELIM_INTERNAL_H_INCLUDED_ 28290001Sglebius 29290001Sglebius#ifdef __cplusplus 30290001Sglebiusextern "C" { 31290001Sglebius#endif 32290001Sglebius 33290001Sglebius#include "event2/util.h" 34290001Sglebius 35290001Sglebius/** A token bucket is an internal structure that tracks how many bytes we are 36290001Sglebius * currently willing to read or write on a given bufferevent or group of 37290001Sglebius * bufferevents */ 38290001Sglebiusstruct ev_token_bucket { 39290001Sglebius /** How many bytes are we willing to read or write right now? These 40290001Sglebius * values are signed so that we can do "defecit spending" */ 41290001Sglebius ev_ssize_t read_limit, write_limit; 42290001Sglebius /** When was this bucket last updated? Measured in abstract 'ticks' 43290001Sglebius * relative to the token bucket configuration. */ 44290001Sglebius ev_uint32_t last_updated; 45290001Sglebius}; 46290001Sglebius 47290001Sglebius/** Configuration info for a token bucket or set of token buckets. */ 48290001Sglebiusstruct ev_token_bucket_cfg { 49290001Sglebius /** How many bytes are we willing to read on average per tick? */ 50290001Sglebius size_t read_rate; 51290001Sglebius /** How many bytes are we willing to read at most in any one tick? */ 52290001Sglebius size_t read_maximum; 53290001Sglebius /** How many bytes are we willing to write on average per tick? */ 54290001Sglebius size_t write_rate; 55290001Sglebius /** How many bytes are we willing to write at most in any one tick? */ 56290001Sglebius size_t write_maximum; 57290001Sglebius 58290001Sglebius /* How long is a tick? Note that fractions of a millisecond are 59290001Sglebius * ignored. */ 60290001Sglebius struct timeval tick_timeout; 61290001Sglebius 62290001Sglebius /* How long is a tick, in milliseconds? Derived from tick_timeout. */ 63290001Sglebius unsigned msec_per_tick; 64290001Sglebius}; 65290001Sglebius 66290001Sglebius/** The current tick is 'current_tick': add bytes to 'bucket' as specified in 67290001Sglebius * 'cfg'. */ 68290001Sglebiusint ev_token_bucket_update_(struct ev_token_bucket *bucket, 69290001Sglebius const struct ev_token_bucket_cfg *cfg, 70290001Sglebius ev_uint32_t current_tick); 71290001Sglebius 72290001Sglebius/** In which tick does 'tv' fall according to 'cfg'? Note that ticks can 73290001Sglebius * overflow easily; your code needs to handle this. */ 74290001Sglebiusev_uint32_t ev_token_bucket_get_tick_(const struct timeval *tv, 75290001Sglebius const struct ev_token_bucket_cfg *cfg); 76290001Sglebius 77290001Sglebius/** Adjust 'bucket' to respect 'cfg', and note that it was last updated in 78290001Sglebius * 'current_tick'. If 'reinitialize' is true, we are changing the 79290001Sglebius * configuration of 'bucket'; otherwise, we are setting it up for the first 80290001Sglebius * time. 81290001Sglebius */ 82290001Sglebiusint ev_token_bucket_init_(struct ev_token_bucket *bucket, 83290001Sglebius const struct ev_token_bucket_cfg *cfg, 84290001Sglebius ev_uint32_t current_tick, 85290001Sglebius int reinitialize); 86290001Sglebius 87290001Sglebiusint bufferevent_remove_from_rate_limit_group_internal_(struct bufferevent *bev, 88290001Sglebius int unsuspend); 89290001Sglebius 90290001Sglebius/** Decrease the read limit of 'b' by 'n' bytes */ 91290001Sglebius#define ev_token_bucket_decrement_read(b,n) \ 92290001Sglebius do { \ 93290001Sglebius (b)->read_limit -= (n); \ 94290001Sglebius } while (0) 95290001Sglebius/** Decrease the write limit of 'b' by 'n' bytes */ 96290001Sglebius#define ev_token_bucket_decrement_write(b,n) \ 97290001Sglebius do { \ 98290001Sglebius (b)->write_limit -= (n); \ 99290001Sglebius } while (0) 100290001Sglebius 101290001Sglebius#ifdef __cplusplus 102290001Sglebius} 103290001Sglebius#endif 104290001Sglebius 105290001Sglebius#endif 106