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_INCLUDED_
27 #define BUFFEREVENT_INTERNAL_H_INCLUDED_
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include "event2/event-config.h"
34 #include "event2/event_struct.h"
35 #include "evconfig-private.h"
36 #include "event2/util.h"
37 #include "defer-internal.h"
38 #include "evthread-internal.h"
39 #include "event2/thread.h"
40 #include "ratelim-internal.h"
41 #include "event2/bufferevent_struct.h"
42 
43 /* These flags are reasons that we might be declining to actually enable
44    reading or writing on a bufferevent.
45  */
46 
47 /* On a all bufferevents, for reading: used when we have read up to the
48    watermark value.
49 
50    On a filtering bufferevent, for writing: used when the underlying
51    bufferevent's write buffer has been filled up to its watermark
52    value.
53 */
54 #define BEV_SUSPEND_WM 0x01
55 /* On a base bufferevent: when we have emptied a bandwidth buckets */
56 #define BEV_SUSPEND_BW 0x02
57 /* On a base bufferevent: when we have emptied the group's bandwidth bucket. */
58 #define BEV_SUSPEND_BW_GROUP 0x04
59 /* On a socket bufferevent: can't do any operations while we're waiting for
60  * name lookup to finish. */
61 #define BEV_SUSPEND_LOOKUP 0x08
62 /* On a base bufferevent, for reading: used when a filter has choked this
63  * (underlying) bufferevent because it has stopped reading from it. */
64 #define BEV_SUSPEND_FILT_READ 0x10
65 
66 typedef ev_uint16_t bufferevent_suspend_flags;
67 
68 struct bufferevent_rate_limit_group {
69 	/** List of all members in the group */
70 	LIST_HEAD(rlim_group_member_list, bufferevent_private) members;
71 	/** Current limits for the group. */
72 	struct ev_token_bucket rate_limit;
73 	struct ev_token_bucket_cfg rate_limit_cfg;
74 
75 	/** True iff we don't want to read from any member of the group.until
76 	 * the token bucket refills.  */
77 	unsigned read_suspended : 1;
78 	/** True iff we don't want to write from any member of the group.until
79 	 * the token bucket refills.  */
80 	unsigned write_suspended : 1;
81 	/** True iff we were unable to suspend one of the bufferevents in the
82 	 * group for reading the last time we tried, and we should try
83 	 * again. */
84 	unsigned pending_unsuspend_read : 1;
85 	/** True iff we were unable to suspend one of the bufferevents in the
86 	 * group for writing the last time we tried, and we should try
87 	 * again. */
88 	unsigned pending_unsuspend_write : 1;
89 
90 	/*@{*/
91 	/** Total number of bytes read or written in this group since last
92 	 * reset. */
93 	ev_uint64_t total_read;
94 	ev_uint64_t total_written;
95 	/*@}*/
96 
97 	/** The number of bufferevents in the group. */
98 	int n_members;
99 
100 	/** The smallest number of bytes that any member of the group should
101 	 * be limited to read or write at a time. */
102 	ev_ssize_t min_share;
103 	ev_ssize_t configured_min_share;
104 
105 	/** Timeout event that goes off once a tick, when the bucket is ready
106 	 * to refill. */
107 	struct event master_refill_event;
108 
109 	/** Seed for weak random number generator. Protected by 'lock' */
110 	struct evutil_weakrand_state weakrand_seed;
111 
112 	/** Lock to protect the members of this group.  This lock should nest
113 	 * within every bufferevent lock: if you are holding this lock, do
114 	 * not assume you can lock another bufferevent. */
115 	void *lock;
116 };
117 
118 /** Fields for rate-limiting a single bufferevent. */
119 struct bufferevent_rate_limit {
120 	/* Linked-list elements for storing this bufferevent_private in a
121 	 * group.
122 	 *
123 	 * Note that this field is supposed to be protected by the group
124 	 * lock */
125 	LIST_ENTRY(bufferevent_private) next_in_group;
126 	/** The rate-limiting group for this bufferevent, or NULL if it is
127 	 * only rate-limited on its own. */
128 	struct bufferevent_rate_limit_group *group;
129 
130 	/* This bufferevent's current limits. */
131 	struct ev_token_bucket limit;
132 	/* Pointer to the rate-limit configuration for this bufferevent.
133 	 * Can be shared.  XXX reference-count this? */
134 	struct ev_token_bucket_cfg *cfg;
135 
136 	/* Timeout event used when one this bufferevent's buckets are
137 	 * empty. */
138 	struct event refill_bucket_event;
139 };
140 
141 /** Parts of the bufferevent structure that are shared among all bufferevent
142  * types, but not exposed in bufferevent_struct.h. */
143 struct bufferevent_private {
144 	/** The underlying bufferevent structure. */
145 	struct bufferevent bev;
146 
147 	/** Evbuffer callback to enforce watermarks on input. */
148 	struct evbuffer_cb_entry *read_watermarks_cb;
149 
150 	/** If set, we should free the lock when we free the bufferevent. */
151 	unsigned own_lock : 1;
152 
153 	/** Flag: set if we have deferred callbacks and a read callback is
154 	 * pending. */
155 	unsigned readcb_pending : 1;
156 	/** Flag: set if we have deferred callbacks and a write callback is
157 	 * pending. */
158 	unsigned writecb_pending : 1;
159 	/** Flag: set if we are currently busy connecting. */
160 	unsigned connecting : 1;
161 	/** Flag: set if a connect failed prematurely; this is a hack for
162 	 * getting around the bufferevent abstraction. */
163 	unsigned connection_refused : 1;
164 	/** Set to the events pending if we have deferred callbacks and
165 	 * an events callback is pending. */
166 	short eventcb_pending;
167 
168 	/** If set, read 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 read_suspended;
172 
173 	/** If set, writing is suspended until one or more conditions are over.
174 	 * The actual value here is a bitfield of those conditions; see the
175 	 * BEV_SUSPEND_* flags above. */
176 	bufferevent_suspend_flags write_suspended;
177 
178 	/** Set to the current socket errno if we have deferred callbacks and
179 	 * an events callback is pending. */
180 	int errno_pending;
181 
182 	/** The DNS error code for bufferevent_socket_connect_hostname */
183 	int dns_error;
184 
185 	/** Used to implement deferred callbacks */
186 	struct event_callback deferred;
187 
188 	/** The options this bufferevent was constructed with */
189 	enum bufferevent_options options;
190 
191 	/** Current reference count for this bufferevent. */
192 	int refcnt;
193 
194 	/** Lock for this bufferevent.  Shared by the inbuf and the outbuf.
195 	 * If NULL, locking is disabled. */
196 	void *lock;
197 
198 	/** No matter how big our bucket gets, don't try to read more than this
199 	 * much in a single read operation. */
200 	ev_ssize_t max_single_read;
201 
202 	/** No matter how big our bucket gets, don't try to write more than this
203 	 * much in a single write operation. */
204 	ev_ssize_t max_single_write;
205 
206 	/** Rate-limiting information for this bufferevent */
207 	struct bufferevent_rate_limit *rate_limiting;
208 };
209 
210 /** Possible operations for a control callback. */
211 enum bufferevent_ctrl_op {
212 	BEV_CTRL_SET_FD,
213 	BEV_CTRL_GET_FD,
214 	BEV_CTRL_GET_UNDERLYING,
215 	BEV_CTRL_CANCEL_ALL
216 };
217 
218 /** Possible data types for a control callback */
219 union bufferevent_ctrl_data {
220 	void *ptr;
221 	evutil_socket_t fd;
222 };
223 
224 /**
225    Implementation table for a bufferevent: holds function pointers and other
226    information to make the various bufferevent types work.
227 */
228 struct bufferevent_ops {
229 	/** The name of the bufferevent's type. */
230 	const char *type;
231 	/** At what offset into the implementation type will we find a
232 	    bufferevent structure?
233 
234 	    Example: if the type is implemented as
235 	    struct bufferevent_x {
236 	       int extra_data;
237 	       struct bufferevent bev;
238 	    }
239 	    then mem_offset should be offsetof(struct bufferevent_x, bev)
240 	*/
241 	off_t mem_offset;
242 
243 	/** Enables one or more of EV_READ|EV_WRITE on a bufferevent.  Does
244 	    not need to adjust the 'enabled' field.  Returns 0 on success, -1
245 	    on failure.
246 	 */
247 	int (*enable)(struct bufferevent *, short);
248 
249 	/** Disables one or more of EV_READ|EV_WRITE on a bufferevent.  Does
250 	    not need to adjust the 'enabled' field.  Returns 0 on success, -1
251 	    on failure.
252 	 */
253 	int (*disable)(struct bufferevent *, short);
254 
255 	/** Detatches the bufferevent from related data structures. Called as
256 	 * soon as its reference count reaches 0. */
257 	void (*unlink)(struct bufferevent *);
258 
259 	/** Free any storage and deallocate any extra data or structures used
260 	    in this implementation. Called when the bufferevent is
261 	    finalized.
262 	 */
263 	void (*destruct)(struct bufferevent *);
264 
265 	/** Called when the timeouts on the bufferevent have changed.*/
266 	int (*adj_timeouts)(struct bufferevent *);
267 
268 	/** Called to flush data. */
269 	int (*flush)(struct bufferevent *, short, enum bufferevent_flush_mode);
270 
271 	/** Called to access miscellaneous fields. */
272 	int (*ctrl)(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
273 
274 };
275 
276 extern const struct bufferevent_ops bufferevent_ops_socket;
277 extern const struct bufferevent_ops bufferevent_ops_filter;
278 extern const struct bufferevent_ops bufferevent_ops_pair;
279 
280 #define BEV_IS_SOCKET(bevp) ((bevp)->be_ops == &bufferevent_ops_socket)
281 #define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter)
282 #define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair)
283 
284 #ifdef _WIN32
285 extern const struct bufferevent_ops bufferevent_ops_async;
286 #define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async)
287 #else
288 #define BEV_IS_ASYNC(bevp) 0
289 #endif
290 
291 /** Initialize the shared parts of a bufferevent. */
292 int bufferevent_init_common_(struct bufferevent_private *, struct event_base *, const struct bufferevent_ops *, enum bufferevent_options options);
293 
294 /** For internal use: temporarily stop all reads on bufev, until the conditions
295  * in 'what' are over. */
296 void bufferevent_suspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what);
297 /** For internal use: clear the conditions 'what' on bufev, and re-enable
298  * reading if there are no conditions left. */
299 void bufferevent_unsuspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what);
300 
301 /** For internal use: temporarily stop all writes on bufev, until the conditions
302  * in 'what' are over. */
303 void bufferevent_suspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what);
304 /** For internal use: clear the conditions 'what' on bufev, and re-enable
305  * writing if there are no conditions left. */
306 void bufferevent_unsuspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what);
307 
308 #define bufferevent_wm_suspend_read(b) \
309 	bufferevent_suspend_read_((b), BEV_SUSPEND_WM)
310 #define bufferevent_wm_unsuspend_read(b) \
311 	bufferevent_unsuspend_read_((b), BEV_SUSPEND_WM)
312 
313 /*
314   Disable a bufferevent.  Equivalent to bufferevent_disable(), but
315   first resets 'connecting' flag to force EV_WRITE down for sure.
316 
317   XXXX this method will go away in the future; try not to add new users.
318     See comment in evhttp_connection_reset_() for discussion.
319 
320   @param bufev the bufferevent to be disabled
321   @param event any combination of EV_READ | EV_WRITE.
322   @return 0 if successful, or -1 if an error occurred
323   @see bufferevent_disable()
324  */
325 int bufferevent_disable_hard_(struct bufferevent *bufev, short event);
326 
327 /** Internal: Set up locking on a bufferevent.  If lock is set, use it.
328  * Otherwise, use a new lock. */
329 int bufferevent_enable_locking_(struct bufferevent *bufev, void *lock);
330 /** Internal: Increment the reference count on bufev. */
331 void bufferevent_incref_(struct bufferevent *bufev);
332 /** Internal: Lock bufev and increase its reference count.
333  * unlocking it otherwise. */
334 void bufferevent_incref_and_lock_(struct bufferevent *bufev);
335 /** Internal: Decrement the reference count on bufev.  Returns 1 if it freed
336  * the bufferevent.*/
337 int bufferevent_decref_(struct bufferevent *bufev);
338 /** Internal: Drop the reference count on bufev, freeing as necessary, and
339  * unlocking it otherwise.  Returns 1 if it freed the bufferevent. */
340 int bufferevent_decref_and_unlock_(struct bufferevent *bufev);
341 
342 /** Internal: If callbacks are deferred and we have a read callback, schedule
343  * a readcb.  Otherwise just run the readcb. Ignores watermarks. */
344 void bufferevent_run_readcb_(struct bufferevent *bufev, int options);
345 /** Internal: If callbacks are deferred and we have a write callback, schedule
346  * a writecb.  Otherwise just run the writecb. Ignores watermarks. */
347 void bufferevent_run_writecb_(struct bufferevent *bufev, int options);
348 /** Internal: If callbacks are deferred and we have an eventcb, schedule
349  * it to run with events "what".  Otherwise just run the eventcb.
350  * See bufferevent_trigger_event for meaning of "options". */
351 void bufferevent_run_eventcb_(struct bufferevent *bufev, short what, int options);
352 
353 /** Internal: Run or schedule (if deferred or options contain
354  * BEV_TRIG_DEFER_CALLBACKS) I/O callbacks specified in iotype.
355  * Must already hold the bufev lock. Honors watermarks unless
356  * BEV_TRIG_IGNORE_WATERMARKS is in options. */
357 static inline void bufferevent_trigger_nolock_(struct bufferevent *bufev, short iotype, int options);
358 
359 /* Making this inline since all of the common-case calls to this function in
360  * libevent use constant arguments. */
361 static inline void
362 bufferevent_trigger_nolock_(struct bufferevent *bufev, short iotype, int options)
363 {
364 	if ((iotype & EV_READ) && ((options & BEV_TRIG_IGNORE_WATERMARKS) ||
365 	    evbuffer_get_length(bufev->input) >= bufev->wm_read.low))
366 		bufferevent_run_readcb_(bufev, options);
367 	if ((iotype & EV_WRITE) && ((options & BEV_TRIG_IGNORE_WATERMARKS) ||
368 	    evbuffer_get_length(bufev->output) <= bufev->wm_write.low))
369 		bufferevent_run_writecb_(bufev, options);
370 }
371 
372 /** Internal: Add the event 'ev' with timeout tv, unless tv is set to 0, in
373  * which case add ev with no timeout. */
374 int bufferevent_add_event_(struct event *ev, const struct timeval *tv);
375 
376 /* =========
377  * These next functions implement timeouts for bufferevents that aren't doing
378  * anything else with ev_read and ev_write, to handle timeouts.
379  * ========= */
380 /** Internal use: Set up the ev_read and ev_write callbacks so that
381  * the other "generic_timeout" functions will work on it.  Call this from
382  * the constructor function. */
383 void bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev);
384 /** Internal use: Add or delete the generic timeout events as appropriate.
385  * (If an event is enabled and a timeout is set, we add the event.  Otherwise
386  * we delete it.)  Call this from anything that changes the timeout values,
387  * that enabled EV_READ or EV_WRITE, or that disables EV_READ or EV_WRITE. */
388 int bufferevent_generic_adj_timeouts_(struct bufferevent *bev);
389 
390 enum bufferevent_options bufferevent_get_options_(struct bufferevent *bev);
391 
392 /** Internal use: We have just successfully read data into an inbuf, so
393  * reset the read timeout (if any). */
394 #define BEV_RESET_GENERIC_READ_TIMEOUT(bev)				\
395 	do {								\
396 		if (evutil_timerisset(&(bev)->timeout_read))		\
397 			event_add(&(bev)->ev_read, &(bev)->timeout_read); \
398 	} while (0)
399 /** Internal use: We have just successfully written data from an inbuf, so
400  * reset the read timeout (if any). */
401 #define BEV_RESET_GENERIC_WRITE_TIMEOUT(bev)				\
402 	do {								\
403 		if (evutil_timerisset(&(bev)->timeout_write))		\
404 			event_add(&(bev)->ev_write, &(bev)->timeout_write); \
405 	} while (0)
406 #define BEV_DEL_GENERIC_READ_TIMEOUT(bev)	\
407 		event_del(&(bev)->ev_read)
408 #define BEV_DEL_GENERIC_WRITE_TIMEOUT(bev)	\
409 		event_del(&(bev)->ev_write)
410 
411 
412 /** Internal: Given a bufferevent, return its corresponding
413  * bufferevent_private. */
414 #define BEV_UPCAST(b) EVUTIL_UPCAST((b), struct bufferevent_private, bev)
415 
416 #ifdef EVENT__DISABLE_THREAD_SUPPORT
417 #define BEV_LOCK(b) EVUTIL_NIL_STMT_
418 #define BEV_UNLOCK(b) EVUTIL_NIL_STMT_
419 #else
420 /** Internal: Grab the lock (if any) on a bufferevent */
421 #define BEV_LOCK(b) do {						\
422 		struct bufferevent_private *locking =  BEV_UPCAST(b);	\
423 		EVLOCK_LOCK(locking->lock, 0);				\
424 	} while (0)
425 
426 /** Internal: Release the lock (if any) on a bufferevent */
427 #define BEV_UNLOCK(b) do {						\
428 		struct bufferevent_private *locking =  BEV_UPCAST(b);	\
429 		EVLOCK_UNLOCK(locking->lock, 0);			\
430 	} while (0)
431 #endif
432 
433 
434 /* ==== For rate-limiting. */
435 
436 int bufferevent_decrement_write_buckets_(struct bufferevent_private *bev,
437     ev_ssize_t bytes);
438 int bufferevent_decrement_read_buckets_(struct bufferevent_private *bev,
439     ev_ssize_t bytes);
440 ev_ssize_t bufferevent_get_read_max_(struct bufferevent_private *bev);
441 ev_ssize_t bufferevent_get_write_max_(struct bufferevent_private *bev);
442 
443 int bufferevent_ratelim_init_(struct bufferevent_private *bev);
444 
445 #ifdef __cplusplus
446 }
447 #endif
448 
449 
450 #endif /* BUFFEREVENT_INTERNAL_H_INCLUDED_ */
451