1 /* $NetBSD: bufferevent-internal.h,v 1.2 2013/04/11 16:56:41 christos Exp $ */
3 * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #ifndef _BUFFEREVENT_INTERNAL_H_
28 #define _BUFFEREVENT_INTERNAL_H_
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"
42 /* These flags are reasons that we might be declining to actually enable
43 reading or writing on a bufferevent.
46 /* On a all bufferevents, for reading: used when we have read up to the
49 On a filtering bufferevent, for writing: used when the underlying
50 bufferevent's write buffer has been filled up to its watermark
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
65 typedef ev_uint16_t bufferevent_suspend_flags
;
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
;
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
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
87 unsigned pending_unsuspend_write
: 1;
90 /** Total number of bytes read or written in this group since last
92 ev_uint64_t total_read
;
93 ev_uint64_t total_written
;
96 /** The number of bufferevents in the group. */
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
;
104 /** Timeout event that goes off once a tick, when the bucket is ready
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. */
113 /** Fields for rate-limiting a single bufferevent. */
114 struct bufferevent_rate_limit
{
115 /* Linked-list elements for storing this bufferevent_private in a
118 * Note that this field is supposed to be protected by the group
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
;
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
;
131 /* Timeout event used when one this bufferevent's buckets are
133 struct event refill_bucket_event
;
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
;
142 /** Evbuffer callback to enforce watermarks on input. */
143 struct evbuffer_cb_entry
*read_watermarks_cb
;
145 /** If set, we should free the lock when we free the bufferevent. */
146 unsigned own_lock
: 1;
148 /** Flag: set if we have deferred callbacks and a read callback is
150 unsigned readcb_pending
: 1;
151 /** Flag: set if we have deferred callbacks and a write callback is
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
;
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
;
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
;
173 /** Set to the current socket errno if we have deferred callbacks and
174 * an events callback is pending. */
177 /** The DNS error code for bufferevent_socket_connect_hostname */
180 /** Used to implement deferred callbacks */
181 struct deferred_cb deferred
;
183 /** The options this bufferevent was constructed with */
184 enum bufferevent_options options
;
186 /** Current reference count for this bufferevent. */
189 /** Lock for this bufferevent. Shared by the inbuf and the outbuf.
190 * If NULL, locking is disabled. */
193 /** Rate-limiting information for this bufferevent */
194 struct bufferevent_rate_limit
*rate_limiting
;
197 /** Possible operations for a control callback. */
198 enum bufferevent_ctrl_op
{
201 BEV_CTRL_GET_UNDERLYING
,
205 /** Possible data types for a control callback */
206 union bufferevent_ctrl_data
{
212 Implementation table for a bufferevent: holds function pointers and other
213 information to make the various bufferevent types work.
215 struct bufferevent_ops
{
216 /** The name of the bufferevent's type. */
218 /** At what offset into the implementation type will we find a
219 bufferevent structure?
221 Example: if the type is implemented as
222 struct bufferevent_x {
224 struct bufferevent bev;
226 then mem_offset should be offsetof(struct bufferevent_x, bev)
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
234 int (*enable
)(struct bufferevent
*, short);
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
240 int (*disable
)(struct bufferevent
*, short);
242 /** Free any storage and deallocate any extra data or structures used
243 in this implementation.
245 void (*destruct
)(struct bufferevent
*);
247 /** Called when the timeouts on the bufferevent have changed.*/
248 int (*adj_timeouts
)(struct bufferevent
*);
250 /** Called to flush data. */
251 int (*flush
)(struct bufferevent
*, short, enum bufferevent_flush_mode
);
253 /** Called to access miscellaneous fields. */
254 int (*ctrl
)(struct bufferevent
*, enum bufferevent_ctrl_op
, union bufferevent_ctrl_data
*);
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
;
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)
267 extern const struct bufferevent_ops bufferevent_ops_async
;
268 #define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async)
270 #define BEV_IS_ASYNC(bevp) 0
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
);
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
);
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
);
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)
296 Disable a bufferevent. Equivalent to bufferevent_disable(), but
297 first resets 'connecting' flag to force EV_WRITE down for sure.
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.
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()
307 int bufferevent_disable_hard(struct bufferevent
*bufev
, short event
);
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
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
);
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
);
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
);
339 * These next functions implement timeouts for bufferevents that aren't doing
340 * anything else with ev_read and ev_write, to handle timeouts.
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
);
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) \
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) \
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)
375 /** Internal: Given a bufferevent, return its corresponding
376 * bufferevent_private. */
377 #define BEV_UPCAST(b) EVUTIL_UPCAST((b), struct bufferevent_private, bev)
379 #ifdef _EVENT_DISABLE_THREAD_SUPPORT
380 #define BEV_LOCK(b) _EVUTIL_NIL_STMT
381 #define BEV_UNLOCK(b) _EVUTIL_NIL_STMT
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)
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)
397 /* ==== For rate-limiting. */
399 int _bufferevent_decrement_write_buckets(struct bufferevent_private
*bev
,
401 int _bufferevent_decrement_read_buckets(struct bufferevent_private
*bev
,
403 ev_ssize_t
_bufferevent_get_read_max(struct bufferevent_private
*bev
);
404 ev_ssize_t
_bufferevent_get_write_max(struct bufferevent_private
*bev
);
411 #endif /* _BUFFEREVENT_INTERNAL_H_ */