michael@0: /* michael@0: * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #ifndef _BUFFEREVENT_INTERNAL_H_ michael@0: #define _BUFFEREVENT_INTERNAL_H_ michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: #include "event2/event-config.h" michael@0: #include "event2/util.h" michael@0: #include "defer-internal.h" michael@0: #include "evthread-internal.h" michael@0: #include "event2/thread.h" michael@0: #include "ratelim-internal.h" michael@0: #include "event2/bufferevent_struct.h" michael@0: michael@0: /* These flags are reasons that we might be declining to actually enable michael@0: reading or writing on a bufferevent. michael@0: */ michael@0: michael@0: /* On a all bufferevents, for reading: used when we have read up to the michael@0: watermark value. michael@0: michael@0: On a filtering bufferevent, for writing: used when the underlying michael@0: bufferevent's write buffer has been filled up to its watermark michael@0: value. michael@0: */ michael@0: #define BEV_SUSPEND_WM 0x01 michael@0: /* On a base bufferevent: when we have emptied a bandwidth buckets */ michael@0: #define BEV_SUSPEND_BW 0x02 michael@0: /* On a base bufferevent: when we have emptied the group's bandwidth bucket. */ michael@0: #define BEV_SUSPEND_BW_GROUP 0x04 michael@0: /* On a socket bufferevent: can't do any operations while we're waiting for michael@0: * name lookup to finish. */ michael@0: #define BEV_SUSPEND_LOOKUP 0x08 michael@0: /* On a base bufferevent, for reading: used when a filter has choked this michael@0: * (underlying) bufferevent because it has stopped reading from it. */ michael@0: #define BEV_SUSPEND_FILT_READ 0x10 michael@0: michael@0: typedef ev_uint16_t bufferevent_suspend_flags; michael@0: michael@0: struct bufferevent_rate_limit_group { michael@0: /** List of all members in the group */ michael@0: TAILQ_HEAD(rlim_group_member_list, bufferevent_private) members; michael@0: /** Current limits for the group. */ michael@0: struct ev_token_bucket rate_limit; michael@0: struct ev_token_bucket_cfg rate_limit_cfg; michael@0: michael@0: /** True iff we don't want to read from any member of the group.until michael@0: * the token bucket refills. */ michael@0: unsigned read_suspended : 1; michael@0: /** True iff we don't want to write from any member of the group.until michael@0: * the token bucket refills. */ michael@0: unsigned write_suspended : 1; michael@0: /** True iff we were unable to suspend one of the bufferevents in the michael@0: * group for reading the last time we tried, and we should try michael@0: * again. */ michael@0: unsigned pending_unsuspend_read : 1; michael@0: /** True iff we were unable to suspend one of the bufferevents in the michael@0: * group for writing the last time we tried, and we should try michael@0: * again. */ michael@0: unsigned pending_unsuspend_write : 1; michael@0: michael@0: /*@{*/ michael@0: /** Total number of bytes read or written in this group since last michael@0: * reset. */ michael@0: ev_uint64_t total_read; michael@0: ev_uint64_t total_written; michael@0: /*@}*/ michael@0: michael@0: /** The number of bufferevents in the group. */ michael@0: int n_members; michael@0: michael@0: /** The smallest number of bytes that any member of the group should michael@0: * be limited to read or write at a time. */ michael@0: ev_ssize_t min_share; michael@0: ev_ssize_t configured_min_share; michael@0: michael@0: /** Timeout event that goes off once a tick, when the bucket is ready michael@0: * to refill. */ michael@0: struct event master_refill_event; michael@0: /** Lock to protect the members of this group. This lock should nest michael@0: * within every bufferevent lock: if you are holding this lock, do michael@0: * not assume you can lock another bufferevent. */ michael@0: void *lock; michael@0: }; michael@0: michael@0: /** Fields for rate-limiting a single bufferevent. */ michael@0: struct bufferevent_rate_limit { michael@0: /* Linked-list elements for storing this bufferevent_private in a michael@0: * group. michael@0: * michael@0: * Note that this field is supposed to be protected by the group michael@0: * lock */ michael@0: TAILQ_ENTRY(bufferevent_private) next_in_group; michael@0: /** The rate-limiting group for this bufferevent, or NULL if it is michael@0: * only rate-limited on its own. */ michael@0: struct bufferevent_rate_limit_group *group; michael@0: michael@0: /* This bufferevent's current limits. */ michael@0: struct ev_token_bucket limit; michael@0: /* Pointer to the rate-limit configuration for this bufferevent. michael@0: * Can be shared. XXX reference-count this? */ michael@0: struct ev_token_bucket_cfg *cfg; michael@0: michael@0: /* Timeout event used when one this bufferevent's buckets are michael@0: * empty. */ michael@0: struct event refill_bucket_event; michael@0: }; michael@0: michael@0: /** Parts of the bufferevent structure that are shared among all bufferevent michael@0: * types, but not exposed in bufferevent_struct.h. */ michael@0: struct bufferevent_private { michael@0: /** The underlying bufferevent structure. */ michael@0: struct bufferevent bev; michael@0: michael@0: /** Evbuffer callback to enforce watermarks on input. */ michael@0: struct evbuffer_cb_entry *read_watermarks_cb; michael@0: michael@0: /** If set, we should free the lock when we free the bufferevent. */ michael@0: unsigned own_lock : 1; michael@0: michael@0: /** Flag: set if we have deferred callbacks and a read callback is michael@0: * pending. */ michael@0: unsigned readcb_pending : 1; michael@0: /** Flag: set if we have deferred callbacks and a write callback is michael@0: * pending. */ michael@0: unsigned writecb_pending : 1; michael@0: /** Flag: set if we are currently busy connecting. */ michael@0: unsigned connecting : 1; michael@0: /** Flag: set if a connect failed prematurely; this is a hack for michael@0: * getting around the bufferevent abstraction. */ michael@0: unsigned connection_refused : 1; michael@0: /** Set to the events pending if we have deferred callbacks and michael@0: * an events callback is pending. */ michael@0: short eventcb_pending; michael@0: michael@0: /** If set, read is suspended until one or more conditions are over. michael@0: * The actual value here is a bitfield of those conditions; see the michael@0: * BEV_SUSPEND_* flags above. */ michael@0: bufferevent_suspend_flags read_suspended; michael@0: michael@0: /** If set, writing is suspended until one or more conditions are over. michael@0: * The actual value here is a bitfield of those conditions; see the michael@0: * BEV_SUSPEND_* flags above. */ michael@0: bufferevent_suspend_flags write_suspended; michael@0: michael@0: /** Set to the current socket errno if we have deferred callbacks and michael@0: * an events callback is pending. */ michael@0: int errno_pending; michael@0: michael@0: /** The DNS error code for bufferevent_socket_connect_hostname */ michael@0: int dns_error; michael@0: michael@0: /** Used to implement deferred callbacks */ michael@0: struct deferred_cb deferred; michael@0: michael@0: /** The options this bufferevent was constructed with */ michael@0: enum bufferevent_options options; michael@0: michael@0: /** Current reference count for this bufferevent. */ michael@0: int refcnt; michael@0: michael@0: /** Lock for this bufferevent. Shared by the inbuf and the outbuf. michael@0: * If NULL, locking is disabled. */ michael@0: void *lock; michael@0: michael@0: /** Rate-limiting information for this bufferevent */ michael@0: struct bufferevent_rate_limit *rate_limiting; michael@0: }; michael@0: michael@0: /** Possible operations for a control callback. */ michael@0: enum bufferevent_ctrl_op { michael@0: BEV_CTRL_SET_FD, michael@0: BEV_CTRL_GET_FD, michael@0: BEV_CTRL_GET_UNDERLYING, michael@0: BEV_CTRL_CANCEL_ALL michael@0: }; michael@0: michael@0: /** Possible data types for a control callback */ michael@0: union bufferevent_ctrl_data { michael@0: void *ptr; michael@0: evutil_socket_t fd; michael@0: }; michael@0: michael@0: /** michael@0: Implementation table for a bufferevent: holds function pointers and other michael@0: information to make the various bufferevent types work. michael@0: */ michael@0: struct bufferevent_ops { michael@0: /** The name of the bufferevent's type. */ michael@0: const char *type; michael@0: /** At what offset into the implementation type will we find a michael@0: bufferevent structure? michael@0: michael@0: Example: if the type is implemented as michael@0: struct bufferevent_x { michael@0: int extra_data; michael@0: struct bufferevent bev; michael@0: } michael@0: then mem_offset should be offsetof(struct bufferevent_x, bev) michael@0: */ michael@0: off_t mem_offset; michael@0: michael@0: /** Enables one or more of EV_READ|EV_WRITE on a bufferevent. Does michael@0: not need to adjust the 'enabled' field. Returns 0 on success, -1 michael@0: on failure. michael@0: */ michael@0: int (*enable)(struct bufferevent *, short); michael@0: michael@0: /** Disables one or more of EV_READ|EV_WRITE on a bufferevent. Does michael@0: not need to adjust the 'enabled' field. Returns 0 on success, -1 michael@0: on failure. michael@0: */ michael@0: int (*disable)(struct bufferevent *, short); michael@0: michael@0: /** Free any storage and deallocate any extra data or structures used michael@0: in this implementation. michael@0: */ michael@0: void (*destruct)(struct bufferevent *); michael@0: michael@0: /** Called when the timeouts on the bufferevent have changed.*/ michael@0: int (*adj_timeouts)(struct bufferevent *); michael@0: michael@0: /** Called to flush data. */ michael@0: int (*flush)(struct bufferevent *, short, enum bufferevent_flush_mode); michael@0: michael@0: /** Called to access miscellaneous fields. */ michael@0: int (*ctrl)(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *); michael@0: michael@0: }; michael@0: michael@0: extern const struct bufferevent_ops bufferevent_ops_socket; michael@0: extern const struct bufferevent_ops bufferevent_ops_filter; michael@0: extern const struct bufferevent_ops bufferevent_ops_pair; michael@0: michael@0: #define BEV_IS_SOCKET(bevp) ((bevp)->be_ops == &bufferevent_ops_socket) michael@0: #define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter) michael@0: #define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair) michael@0: michael@0: #ifdef WIN32 michael@0: extern const struct bufferevent_ops bufferevent_ops_async; michael@0: #define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async) michael@0: #else michael@0: #define BEV_IS_ASYNC(bevp) 0 michael@0: #endif michael@0: michael@0: /** Initialize the shared parts of a bufferevent. */ michael@0: int bufferevent_init_common(struct bufferevent_private *, struct event_base *, const struct bufferevent_ops *, enum bufferevent_options options); michael@0: michael@0: /** For internal use: temporarily stop all reads on bufev, until the conditions michael@0: * in 'what' are over. */ michael@0: void bufferevent_suspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what); michael@0: /** For internal use: clear the conditions 'what' on bufev, and re-enable michael@0: * reading if there are no conditions left. */ michael@0: void bufferevent_unsuspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what); michael@0: michael@0: /** For internal use: temporarily stop all writes on bufev, until the conditions michael@0: * in 'what' are over. */ michael@0: void bufferevent_suspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what); michael@0: /** For internal use: clear the conditions 'what' on bufev, and re-enable michael@0: * writing if there are no conditions left. */ michael@0: void bufferevent_unsuspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what); michael@0: michael@0: #define bufferevent_wm_suspend_read(b) \ michael@0: bufferevent_suspend_read((b), BEV_SUSPEND_WM) michael@0: #define bufferevent_wm_unsuspend_read(b) \ michael@0: bufferevent_unsuspend_read((b), BEV_SUSPEND_WM) michael@0: michael@0: /* michael@0: Disable a bufferevent. Equivalent to bufferevent_disable(), but michael@0: first resets 'connecting' flag to force EV_WRITE down for sure. michael@0: michael@0: XXXX this method will go away in the future; try not to add new users. michael@0: See comment in evhttp_connection_reset() for discussion. michael@0: michael@0: @param bufev the bufferevent to be disabled michael@0: @param event any combination of EV_READ | EV_WRITE. michael@0: @return 0 if successful, or -1 if an error occurred michael@0: @see bufferevent_disable() michael@0: */ michael@0: int bufferevent_disable_hard(struct bufferevent *bufev, short event); michael@0: michael@0: /** Internal: Set up locking on a bufferevent. If lock is set, use it. michael@0: * Otherwise, use a new lock. */ michael@0: int bufferevent_enable_locking(struct bufferevent *bufev, void *lock); michael@0: /** Internal: Increment the reference count on bufev. */ michael@0: void bufferevent_incref(struct bufferevent *bufev); michael@0: /** Internal: Lock bufev and increase its reference count. michael@0: * unlocking it otherwise. */ michael@0: void _bufferevent_incref_and_lock(struct bufferevent *bufev); michael@0: /** Internal: Decrement the reference count on bufev. Returns 1 if it freed michael@0: * the bufferevent.*/ michael@0: int bufferevent_decref(struct bufferevent *bufev); michael@0: /** Internal: Drop the reference count on bufev, freeing as necessary, and michael@0: * unlocking it otherwise. Returns 1 if it freed the bufferevent. */ michael@0: int _bufferevent_decref_and_unlock(struct bufferevent *bufev); michael@0: michael@0: /** Internal: If callbacks are deferred and we have a read callback, schedule michael@0: * a readcb. Otherwise just run the readcb. */ michael@0: void _bufferevent_run_readcb(struct bufferevent *bufev); michael@0: /** Internal: If callbacks are deferred and we have a write callback, schedule michael@0: * a writecb. Otherwise just run the writecb. */ michael@0: void _bufferevent_run_writecb(struct bufferevent *bufev); michael@0: /** Internal: If callbacks are deferred and we have an eventcb, schedule michael@0: * it to run with events "what". Otherwise just run the eventcb. */ michael@0: void _bufferevent_run_eventcb(struct bufferevent *bufev, short what); michael@0: michael@0: /** Internal: Add the event 'ev' with timeout tv, unless tv is set to 0, in michael@0: * which case add ev with no timeout. */ michael@0: int _bufferevent_add_event(struct event *ev, const struct timeval *tv); michael@0: michael@0: /* ========= michael@0: * These next functions implement timeouts for bufferevents that aren't doing michael@0: * anything else with ev_read and ev_write, to handle timeouts. michael@0: * ========= */ michael@0: /** Internal use: Set up the ev_read and ev_write callbacks so that michael@0: * the other "generic_timeout" functions will work on it. Call this from michael@0: * the constructor function. */ michael@0: void _bufferevent_init_generic_timeout_cbs(struct bufferevent *bev); michael@0: /** Internal use: Delete the ev_read and ev_write callbacks if they're pending. michael@0: * Call this from the destructor function. */ michael@0: int _bufferevent_del_generic_timeout_cbs(struct bufferevent *bev); michael@0: /** Internal use: Add or delete the generic timeout events as appropriate. michael@0: * (If an event is enabled and a timeout is set, we add the event. Otherwise michael@0: * we delete it.) Call this from anything that changes the timeout values, michael@0: * that enabled EV_READ or EV_WRITE, or that disables EV_READ or EV_WRITE. */ michael@0: int _bufferevent_generic_adj_timeouts(struct bufferevent *bev); michael@0: michael@0: /** Internal use: We have just successfully read data into an inbuf, so michael@0: * reset the read timeout (if any). */ michael@0: #define BEV_RESET_GENERIC_READ_TIMEOUT(bev) \ michael@0: do { \ michael@0: if (evutil_timerisset(&(bev)->timeout_read)) \ michael@0: event_add(&(bev)->ev_read, &(bev)->timeout_read); \ michael@0: } while (0) michael@0: /** Internal use: We have just successfully written data from an inbuf, so michael@0: * reset the read timeout (if any). */ michael@0: #define BEV_RESET_GENERIC_WRITE_TIMEOUT(bev) \ michael@0: do { \ michael@0: if (evutil_timerisset(&(bev)->timeout_write)) \ michael@0: event_add(&(bev)->ev_write, &(bev)->timeout_write); \ michael@0: } while (0) michael@0: #define BEV_DEL_GENERIC_READ_TIMEOUT(bev) \ michael@0: event_del(&(bev)->ev_read) michael@0: #define BEV_DEL_GENERIC_WRITE_TIMEOUT(bev) \ michael@0: event_del(&(bev)->ev_write) michael@0: michael@0: michael@0: /** Internal: Given a bufferevent, return its corresponding michael@0: * bufferevent_private. */ michael@0: #define BEV_UPCAST(b) EVUTIL_UPCAST((b), struct bufferevent_private, bev) michael@0: michael@0: #ifdef _EVENT_DISABLE_THREAD_SUPPORT michael@0: #define BEV_LOCK(b) _EVUTIL_NIL_STMT michael@0: #define BEV_UNLOCK(b) _EVUTIL_NIL_STMT michael@0: #else michael@0: /** Internal: Grab the lock (if any) on a bufferevent */ michael@0: #define BEV_LOCK(b) do { \ michael@0: struct bufferevent_private *locking = BEV_UPCAST(b); \ michael@0: EVLOCK_LOCK(locking->lock, 0); \ michael@0: } while (0) michael@0: michael@0: /** Internal: Release the lock (if any) on a bufferevent */ michael@0: #define BEV_UNLOCK(b) do { \ michael@0: struct bufferevent_private *locking = BEV_UPCAST(b); \ michael@0: EVLOCK_UNLOCK(locking->lock, 0); \ michael@0: } while (0) michael@0: #endif michael@0: michael@0: michael@0: /* ==== For rate-limiting. */ michael@0: michael@0: int _bufferevent_decrement_write_buckets(struct bufferevent_private *bev, michael@0: ev_ssize_t bytes); michael@0: int _bufferevent_decrement_read_buckets(struct bufferevent_private *bev, michael@0: ev_ssize_t bytes); michael@0: ev_ssize_t _bufferevent_get_read_max(struct bufferevent_private *bev); michael@0: ev_ssize_t _bufferevent_get_write_max(struct bufferevent_private *bev); michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: michael@0: #endif /* _BUFFEREVENT_INTERNAL_H_ */