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