1/*
2 * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26#ifndef _BUFFEREVENT_INTERNAL_H_
27#define _BUFFEREVENT_INTERNAL_H_
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#include "event2/event-config.h"
34#include "event2/util.h"
35#include "defer-internal.h"
36#include "evthread-internal.h"
37#include "event2/thread.h"
38#include "ratelim-internal.h"
39#include "event2/bufferevent_struct.h"
40
41/* These flags are reasons that we might be declining to actually enable
42   reading or writing on a bufferevent.
43 */
44
45/* On a all bufferevents, for reading: used when we have read up to the
46   watermark value.
47
48   On a filtering bufferevent, for writing: used when the underlying
49   bufferevent's write buffer has been filled up to its watermark
50   value.
51*/
52#define BEV_SUSPEND_WM 0x01
53/* On a base bufferevent: when we have emptied a bandwidth buckets */
54#define BEV_SUSPEND_BW 0x02
55/* On a base bufferevent: when we have emptied the group's bandwidth bucket. */
56#define BEV_SUSPEND_BW_GROUP 0x04
57/* On a socket bufferevent: can't do any operations while we're waiting for
58 * name lookup to finish. */
59#define BEV_SUSPEND_LOOKUP 0x08
60/* On a base bufferevent, for reading: used when a filter has choked this
61 * (underlying) bufferevent because it has stopped reading from it. */
62#define BEV_SUSPEND_FILT_READ 0x10
63
64typedef ev_uint16_t bufferevent_suspend_flags;
65
66struct bufferevent_rate_limit_group {
67	/** List of all members in the group */
68	TAILQ_HEAD(rlim_group_member_list, bufferevent_private) members;
69	/** Current limits for the group. */
70	struct ev_token_bucket rate_limit;
71	struct ev_token_bucket_cfg rate_limit_cfg;
72
73	/** True iff we don't want to read from any member of the group.until
74	 * the token bucket refills.  */
75	unsigned read_suspended : 1;
76	/** True iff we don't want to write from any member of the group.until
77	 * the token bucket refills.  */
78	unsigned write_suspended : 1;
79	/** True iff we were unable to suspend one of the bufferevents in the
80	 * group for reading the last time we tried, and we should try
81	 * again. */
82	unsigned pending_unsuspend_read : 1;
83	/** True iff we were unable to suspend one of the bufferevents in the
84	 * group for writing the last time we tried, and we should try
85	 * again. */
86	unsigned pending_unsuspend_write : 1;
87
88	/*@{*/
89	/** Total number of bytes read or written in this group since last
90	 * reset. */
91	ev_uint64_t total_read;
92	ev_uint64_t total_written;
93	/*@}*/
94
95	/** The number of bufferevents in the group. */
96	int n_members;
97
98	/** The smallest number of bytes that any member of the group should
99	 * be limited to read or write at a time. */
100	ev_ssize_t min_share;
101	ev_ssize_t configured_min_share;
102
103	/** Timeout event that goes off once a tick, when the bucket is ready
104	 * to refill. */
105	struct event master_refill_event;
106	/** Lock to protect the members of this group.  This lock should nest
107	 * within every bufferevent lock: if you are holding this lock, do
108	 * not assume you can lock another bufferevent. */
109	void *lock;
110};
111
112/** Fields for rate-limiting a single bufferevent. */
113struct bufferevent_rate_limit {
114	/* Linked-list elements for storing this bufferevent_private in a
115	 * group.
116	 *
117	 * Note that this field is supposed to be protected by the group
118	 * lock */
119	TAILQ_ENTRY(bufferevent_private) next_in_group;
120	/** The rate-limiting group for this bufferevent, or NULL if it is
121	 * only rate-limited on its own. */
122	struct bufferevent_rate_limit_group *group;
123
124	/* This bufferevent's current limits. */
125	struct ev_token_bucket limit;
126	/* Pointer to the rate-limit configuration for this bufferevent.
127	 * Can be shared.  XXX reference-count this? */
128	struct ev_token_bucket_cfg *cfg;
129
130	/* Timeout event used when one this bufferevent's buckets are
131	 * empty. */
132	struct event refill_bucket_event;
133};
134
135/** Parts of the bufferevent structure that are shared among all bufferevent
136 * types, but not exposed in bufferevent_struct.h. */
137struct bufferevent_private {
138	/** The underlying bufferevent structure. */
139	struct bufferevent bev;
140
141	/** Evbuffer callback to enforce watermarks on input. */
142	struct evbuffer_cb_entry *read_watermarks_cb;
143
144	/** If set, we should free the lock when we free the bufferevent. */
145	unsigned own_lock : 1;
146
147	/** Flag: set if we have deferred callbacks and a read callback is
148	 * pending. */
149	unsigned readcb_pending : 1;
150	/** Flag: set if we have deferred callbacks and a write callback is
151	 * pending. */
152	unsigned writecb_pending : 1;
153	/** Flag: set if we are currently busy connecting. */
154	unsigned connecting : 1;
155	/** Flag: set if a connect failed prematurely; this is a hack for
156	 * getting around the bufferevent abstraction. */
157	unsigned connection_refused : 1;
158	/** Set to the events pending if we have deferred callbacks and
159	 * an events callback is pending. */
160	short eventcb_pending;
161
162	/** If set, read is suspended until one or more conditions are over.
163	 * The actual value here is a bitfield of those conditions; see the
164	 * BEV_SUSPEND_* flags above. */
165	bufferevent_suspend_flags read_suspended;
166
167	/** If set, writing is suspended until one or more conditions are over.
168	 * The actual value here is a bitfield of those conditions; see the
169	 * BEV_SUSPEND_* flags above. */
170	bufferevent_suspend_flags write_suspended;
171
172	/** Set to the current socket errno if we have deferred callbacks and
173	 * an events callback is pending. */
174	int errno_pending;
175
176	/** The DNS error code for bufferevent_socket_connect_hostname */
177	int dns_error;
178
179	/** Used to implement deferred callbacks */
180	struct deferred_cb deferred;
181
182	/** The options this bufferevent was constructed with */
183	enum bufferevent_options options;
184
185	/** Current reference count for this bufferevent. */
186	int refcnt;
187
188	/** Lock for this bufferevent.  Shared by the inbuf and the outbuf.
189	 * If NULL, locking is disabled. */
190	void *lock;
191
192	/** Rate-limiting information for this bufferevent */
193	struct bufferevent_rate_limit *rate_limiting;
194};
195
196/** Possible operations for a control callback. */
197enum bufferevent_ctrl_op {
198	BEV_CTRL_SET_FD,
199	BEV_CTRL_GET_FD,
200	BEV_CTRL_GET_UNDERLYING,
201	BEV_CTRL_CANCEL_ALL
202};
203
204/** Possible data types for a control callback */
205union bufferevent_ctrl_data {
206	void *ptr;
207	evutil_socket_t fd;
208};
209
210/**
211   Implementation table for a bufferevent: holds function pointers and other
212   information to make the various bufferevent types work.
213*/
214struct bufferevent_ops {
215	/** The name of the bufferevent's type. */
216	const char *type;
217	/** At what offset into the implementation type will we find a
218	    bufferevent structure?
219
220	    Example: if the type is implemented as
221	    struct bufferevent_x {
222	       int extra_data;
223	       struct bufferevent bev;
224	    }
225	    then mem_offset should be offsetof(struct bufferevent_x, bev)
226	*/
227	off_t mem_offset;
228
229	/** Enables one or more of EV_READ|EV_WRITE on a bufferevent.  Does
230	    not need to adjust the 'enabled' field.  Returns 0 on success, -1
231	    on failure.
232	 */
233	int (*enable)(struct bufferevent *, short);
234
235	/** Disables one or more of EV_READ|EV_WRITE on a bufferevent.  Does
236	    not need to adjust the 'enabled' field.  Returns 0 on success, -1
237	    on failure.
238	 */
239	int (*disable)(struct bufferevent *, short);
240
241	/** Free any storage and deallocate any extra data or structures used
242	    in this implementation.
243	 */
244	void (*destruct)(struct bufferevent *);
245
246	/** Called when the timeouts on the bufferevent have changed.*/
247	int (*adj_timeouts)(struct bufferevent *);
248
249	/** Called to flush data. */
250	int (*flush)(struct bufferevent *, short, enum bufferevent_flush_mode);
251
252	/** Called to access miscellaneous fields. */
253	int (*ctrl)(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
254
255};
256
257extern const struct bufferevent_ops bufferevent_ops_socket;
258extern const struct bufferevent_ops bufferevent_ops_filter;
259extern const struct bufferevent_ops bufferevent_ops_pair;
260
261#define BEV_IS_SOCKET(bevp) ((bevp)->be_ops == &bufferevent_ops_socket)
262#define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter)
263#define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair)
264
265#ifdef WIN32
266extern const struct bufferevent_ops bufferevent_ops_async;
267#define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async)
268#else
269#define BEV_IS_ASYNC(bevp) 0
270#endif
271
272/** Initialize the shared parts of a bufferevent. */
273int bufferevent_init_common(struct bufferevent_private *, struct event_base *, const struct bufferevent_ops *, enum bufferevent_options options);
274
275/** For internal use: temporarily stop all reads on bufev, until the conditions
276 * in 'what' are over. */
277void bufferevent_suspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what);
278/** For internal use: clear the conditions 'what' on bufev, and re-enable
279 * reading if there are no conditions left. */
280void bufferevent_unsuspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what);
281
282/** For internal use: temporarily stop all writes on bufev, until the conditions
283 * in 'what' are over. */
284void bufferevent_suspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what);
285/** For internal use: clear the conditions 'what' on bufev, and re-enable
286 * writing if there are no conditions left. */
287void bufferevent_unsuspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what);
288
289#define bufferevent_wm_suspend_read(b) \
290	bufferevent_suspend_read((b), BEV_SUSPEND_WM)
291#define bufferevent_wm_unsuspend_read(b) \
292	bufferevent_unsuspend_read((b), BEV_SUSPEND_WM)
293
294/*
295  Disable a bufferevent.  Equivalent to bufferevent_disable(), but
296  first resets 'connecting' flag to force EV_WRITE down for sure.
297
298  XXXX this method will go away in the future; try not to add new users.
299    See comment in evhttp_connection_reset() for discussion.
300
301  @param bufev the bufferevent to be disabled
302  @param event any combination of EV_READ | EV_WRITE.
303  @return 0 if successful, or -1 if an error occurred
304  @see bufferevent_disable()
305 */
306int bufferevent_disable_hard(struct bufferevent *bufev, short event);
307
308/** Internal: Set up locking on a bufferevent.  If lock is set, use it.
309 * Otherwise, use a new lock. */
310int bufferevent_enable_locking(struct bufferevent *bufev, void *lock);
311/** Internal: Increment the reference count on bufev. */
312void bufferevent_incref(struct bufferevent *bufev);
313/** Internal: Lock bufev and increase its reference count.
314 * unlocking it otherwise. */
315void _bufferevent_incref_and_lock(struct bufferevent *bufev);
316/** Internal: Decrement the reference count on bufev.  Returns 1 if it freed
317 * the bufferevent.*/
318int bufferevent_decref(struct bufferevent *bufev);
319/** Internal: Drop the reference count on bufev, freeing as necessary, and
320 * unlocking it otherwise.  Returns 1 if it freed the bufferevent. */
321int _bufferevent_decref_and_unlock(struct bufferevent *bufev);
322
323/** Internal: If callbacks are deferred and we have a read callback, schedule
324 * a readcb.  Otherwise just run the readcb. */
325void _bufferevent_run_readcb(struct bufferevent *bufev);
326/** Internal: If callbacks are deferred and we have a write callback, schedule
327 * a writecb.  Otherwise just run the writecb. */
328void _bufferevent_run_writecb(struct bufferevent *bufev);
329/** Internal: If callbacks are deferred and we have an eventcb, schedule
330 * it to run with events "what".  Otherwise just run the eventcb. */
331void _bufferevent_run_eventcb(struct bufferevent *bufev, short what);
332
333/** Internal: Add the event 'ev' with timeout tv, unless tv is set to 0, in
334 * which case add ev with no timeout. */
335int _bufferevent_add_event(struct event *ev, const struct timeval *tv);
336
337/* =========
338 * These next functions implement timeouts for bufferevents that aren't doing
339 * anything else with ev_read and ev_write, to handle timeouts.
340 * ========= */
341/** Internal use: Set up the ev_read and ev_write callbacks so that
342 * the other "generic_timeout" functions will work on it.  Call this from
343 * the constructor function. */
344void _bufferevent_init_generic_timeout_cbs(struct bufferevent *bev);
345/** Internal use: Delete the ev_read and ev_write callbacks if they're pending.
346 * Call this from the destructor function. */
347int _bufferevent_del_generic_timeout_cbs(struct bufferevent *bev);
348/** Internal use: Add or delete the generic timeout events as appropriate.
349 * (If an event is enabled and a timeout is set, we add the event.  Otherwise
350 * we delete it.)  Call this from anything that changes the timeout values,
351 * that enabled EV_READ or EV_WRITE, or that disables EV_READ or EV_WRITE. */
352int _bufferevent_generic_adj_timeouts(struct bufferevent *bev);
353
354/** Internal use: We have just successfully read data into an inbuf, so
355 * reset the read timeout (if any). */
356#define BEV_RESET_GENERIC_READ_TIMEOUT(bev)				\
357	do {								\
358		if (evutil_timerisset(&(bev)->timeout_read))		\
359			event_add(&(bev)->ev_read, &(bev)->timeout_read); \
360	} while (0)
361/** Internal use: We have just successfully written data from an inbuf, so
362 * reset the read timeout (if any). */
363#define BEV_RESET_GENERIC_WRITE_TIMEOUT(bev)				\
364	do {								\
365		if (evutil_timerisset(&(bev)->timeout_write))		\
366			event_add(&(bev)->ev_write, &(bev)->timeout_write); \
367	} while (0)
368#define BEV_DEL_GENERIC_READ_TIMEOUT(bev)	\
369		event_del(&(bev)->ev_read)
370#define BEV_DEL_GENERIC_WRITE_TIMEOUT(bev)	\
371		event_del(&(bev)->ev_write)
372
373
374/** Internal: Given a bufferevent, return its corresponding
375 * bufferevent_private. */
376#define BEV_UPCAST(b) EVUTIL_UPCAST((b), struct bufferevent_private, bev)
377
378#ifdef _EVENT_DISABLE_THREAD_SUPPORT
379#define BEV_LOCK(b) _EVUTIL_NIL_STMT
380#define BEV_UNLOCK(b) _EVUTIL_NIL_STMT
381#else
382/** Internal: Grab the lock (if any) on a bufferevent */
383#define BEV_LOCK(b) do {						\
384		struct bufferevent_private *locking =  BEV_UPCAST(b);	\
385		EVLOCK_LOCK(locking->lock, 0);				\
386	} while (0)
387
388/** Internal: Release the lock (if any) on a bufferevent */
389#define BEV_UNLOCK(b) do {						\
390		struct bufferevent_private *locking =  BEV_UPCAST(b);	\
391		EVLOCK_UNLOCK(locking->lock, 0);			\
392	} while (0)
393#endif
394
395
396/* ==== For rate-limiting. */
397
398int _bufferevent_decrement_write_buckets(struct bufferevent_private *bev,
399    ev_ssize_t bytes);
400int _bufferevent_decrement_read_buckets(struct bufferevent_private *bev,
401    ev_ssize_t bytes);
402ev_ssize_t _bufferevent_get_read_max(struct bufferevent_private *bev);
403ev_ssize_t _bufferevent_get_write_max(struct bufferevent_private *bev);
404
405#ifdef __cplusplus
406}
407#endif
408
409
410#endif /* _BUFFEREVENT_INTERNAL_H_ */
411