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