blob: 605dc215ff2758e8722a12f91ad307e9e2a07e5a [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau49b046d2012-08-09 12:11:58 +02002 * include/proto/fd.h
3 * File descriptors states.
4 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +01005 * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu
Willy Tarreau49b046d2012-08-09 12:11:58 +02006 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#ifndef _PROTO_FD_H
23#define _PROTO_FD_H
24
Willy Tarreau2ff76222007-04-09 19:29:56 +020025#include <stdio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <sys/time.h>
27#include <sys/types.h>
28#include <unistd.h>
29
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020030#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/fd.h>
32
Willy Tarreau7be79a42012-11-11 15:02:54 +010033/* public variables */
Willy Tarreau16f649c2014-01-25 19:10:48 +010034extern unsigned int *fd_cache; // FD events cache
35extern unsigned int *fd_updt; // FD updates list
36extern int fd_cache_num; // number of events in the cache
37extern int fd_nbupdt; // number of updates in the list
Willy Tarreau7be79a42012-11-11 15:02:54 +010038
Willy Tarreaubaaee002006-06-26 02:48:02 +020039/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
40 * The file descriptor is also closed.
41 */
42void fd_delete(int fd);
43
Willy Tarreau4f60f162007-04-08 16:39:58 +020044/* disable the specified poller */
45void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau2a429502006-10-15 14:52:29 +020047/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020048 * Initialize the pollers till the best one is found.
49 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020050 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020051 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020052int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020053
Willy Tarreau4f60f162007-04-08 16:39:58 +020054/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020055 * Deinitialize the pollers.
56 */
57void deinit_pollers();
58
59/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020060 * Some pollers may lose their connection after a fork(). It may be necessary
61 * to create initialize part of them again. Returns 0 in case of failure,
62 * otherwise 1. The fork() function may be NULL if unused. In case of error,
63 * the the current poller is destroyed and the caller is responsible for trying
64 * another one by calling init_pollers() again.
65 */
66int fork_poller();
67
68/*
69 * Lists the known pollers on <out>.
70 * Should be performed only before initialization.
71 */
72int list_pollers(FILE *out);
73
74/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020075 * Runs the polling loop
76 */
77void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +020078
Willy Tarreau033cd9d2014-01-25 19:24:15 +010079/* Scan and process the cached events. This should be called right after
Willy Tarreau09f24562012-11-11 16:43:45 +010080 * the poller.
81 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +010082void fd_process_cached_events();
Willy Tarreau09f24562012-11-11 16:43:45 +010083
Willy Tarreaue8525452014-01-25 09:58:06 +010084/* Check the events attached to a file descriptor, update its cache
85 * accordingly, and call the associated I/O callback. If new updates are
86 * detected, the function tries to process them as well in order to save
87 * wakeups after accept().
88 */
89void fd_process_polled_events(int fd);
90
91
Willy Tarreau7be79a42012-11-11 15:02:54 +010092/* Mark fd <fd> as updated and allocate an entry in the update list for this if
93 * it was not already there. This can be done at any time.
94 */
95static inline void updt_fd(const int fd)
96{
97 if (fdtab[fd].updated)
98 /* already scheduled for update */
99 return;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100100 fdtab[fd].updated = 1;
Willy Tarreau4a291442012-12-13 23:34:18 +0100101 fd_updt[fd_nbupdt++] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100102}
103
104
Willy Tarreau899d9572014-01-25 19:20:35 +0100105/* Allocates a cache entry for a file descriptor if it does not yet have one.
106 * This can be done at any time.
107 */
108static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100109{
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100110 if (fdtab[fd].cache)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100111 return;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100112 fd_cache_num++;
113 fdtab[fd].cache = fd_cache_num;
114 fd_cache[fd_cache_num-1] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100115}
116
Willy Tarreau899d9572014-01-25 19:20:35 +0100117/* Removes entry used by fd <fd> from the FD cache and replaces it with the
118 * last one. The fdtab.cache is adjusted to match the back reference if needed.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100119 * If the fd has no entry assigned, return immediately.
120 */
Willy Tarreau899d9572014-01-25 19:20:35 +0100121static inline void fd_release_cache_entry(int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100122{
123 unsigned int pos;
124
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100125 pos = fdtab[fd].cache;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100126 if (!pos)
127 return;
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100128 fdtab[fd].cache = 0;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100129 fd_cache_num--;
130 if (likely(pos <= fd_cache_num)) {
Willy Tarreau7be79a42012-11-11 15:02:54 +0100131 /* was not the last entry */
Willy Tarreau16f649c2014-01-25 19:10:48 +0100132 fd = fd_cache[fd_cache_num];
133 fd_cache[pos - 1] = fd;
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100134 fdtab[fd].cache = pos;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100135 }
136}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200137
Willy Tarreau25002d22014-01-25 10:32:56 +0100138/* Computes the new polled status based on the active and ready statuses, for
139 * each direction. This is meant to be used by pollers while processing updates.
140 */
141static inline int fd_compute_new_polled_status(int state)
142{
143 if (state & FD_EV_ACTIVE_R) {
144 if (!(state & FD_EV_READY_R))
145 state |= FD_EV_POLLED_R;
146 }
147 else
148 state &= ~FD_EV_POLLED_R;
149
150 if (state & FD_EV_ACTIVE_W) {
151 if (!(state & FD_EV_READY_W))
152 state |= FD_EV_POLLED_W;
153 }
154 else
155 state &= ~FD_EV_POLLED_W;
156
157 return state;
158}
159
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100160/* Automatically allocates or releases a cache entry for fd <fd> depending on
161 * its new state. This is meant to be used by pollers while processing updates.
162 */
163static inline void fd_alloc_or_release_cache_entry(int fd, int new_state)
164{
165 /* READY and ACTIVE states (the two with both flags set) require a cache entry */
166
167 if (((new_state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
168 ((new_state & (FD_EV_READY_W | FD_EV_ACTIVE_W)) == (FD_EV_READY_W | FD_EV_ACTIVE_W))) {
169 fd_alloc_cache_entry(fd);
170 }
171 else {
172 fd_release_cache_entry(fd);
173 }
174}
175
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100176/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100177 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100178 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100179static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100180{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100181 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100182}
183
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100184/*
185 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100186 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100187static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100188{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100189 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100190}
191
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100192/*
193 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100194 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100195static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100196{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100197 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
198}
199
200/*
201 * returns true if the FD is polled for recv
202 */
203static inline int fd_recv_polled(const int fd)
204{
205 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
206}
207
208/*
209 * returns the FD's send state (FD_EV_*)
210 */
211static inline int fd_send_state(const int fd)
212{
213 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
214}
215
216/*
217 * returns true if the FD is active for send
218 */
219static inline int fd_send_active(const int fd)
220{
221 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100222}
223
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100224/*
225 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100226 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100227static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100228{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100229 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
230}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100231
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100232/*
233 * returns true if the FD is polled for send
234 */
235static inline int fd_send_polled(const int fd)
236{
237 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
238}
239
240/* Disable processing recv events on fd <fd> */
241static inline void fd_stop_recv(int fd)
242{
243 if (!((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_R))
244 return; /* already disabled */
245 fdtab[fd].state &= ~FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100246 updt_fd(fd); /* need an update entry to change the state */
247}
248
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100249/* Disable processing send events on fd <fd> */
250static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100251{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100252 if (!((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_W))
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100253 return; /* already disabled */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100254 fdtab[fd].state &= ~FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100255 updt_fd(fd); /* need an update entry to change the state */
256}
257
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100258/* Disable processing of events on fd <fd> for both directions. */
259static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200260{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100261 if (!((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_RW))
262 return; /* already disabled */
263 fdtab[fd].state &= ~FD_EV_ACTIVE_RW;
264 updt_fd(fd); /* need an update entry to change the state */
Willy Tarreau49b046d2012-08-09 12:11:58 +0200265}
266
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100267/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
268static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200269{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100270 if (!(((unsigned int)fdtab[fd].state) & FD_EV_READY_R))
271 return; /* already marked as blocked */
272 fdtab[fd].state &= ~FD_EV_READY_R;
273 updt_fd(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200274}
275
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100276/* Report that FD <fd> can receive anymore without polling. */
277static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200278{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100279 if (((unsigned int)fdtab[fd].state) & FD_EV_READY_R)
280 return; /* already marked as blocked */
281 fdtab[fd].state |= FD_EV_READY_R;
282 updt_fd(fd);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200283}
284
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100285/* Disable readiness when polled. This is useful to interrupt reading when it
286 * is suspected that the end of data might have been reached (eg: short read).
287 * This can only be done using level-triggered pollers, so if any edge-triggered
288 * is ever implemented, a test will have to be added here.
289 */
290static inline void fd_done_recv(const int fd)
291{
292 if (fd_recv_polled(fd))
293 fd_cant_recv(fd);
294}
295
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100296/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
297static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200298{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100299 if (!(((unsigned int)fdtab[fd].state) & FD_EV_READY_W))
300 return; /* already marked as blocked */
301 fdtab[fd].state &= ~FD_EV_READY_W;
302 updt_fd(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200303}
304
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100305/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
306static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200307{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100308 if (((unsigned int)fdtab[fd].state) & FD_EV_READY_W)
309 return; /* already marked as blocked */
310 fdtab[fd].state |= FD_EV_READY_W;
311 updt_fd(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200312}
Willy Tarreau2a429502006-10-15 14:52:29 +0200313
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100314/* Prepare FD <fd> to try to receive */
315static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200316{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100317 if (((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_R))
318 return; /* already enabled */
319 fdtab[fd].state |= FD_EV_ACTIVE_R;
320 updt_fd(fd); /* need an update entry to change the state */
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200321}
322
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100323/* Prepare FD <fd> to try to send */
324static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200325{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100326 if (((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_W))
327 return; /* already enabled */
328 fdtab[fd].state |= FD_EV_ACTIVE_W;
329 updt_fd(fd); /* need an update entry to change the state */
Willy Tarreau49b046d2012-08-09 12:11:58 +0200330}
Willy Tarreau2a429502006-10-15 14:52:29 +0200331
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100332/* Prepares <fd> for being polled */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333static inline void fd_insert(int fd)
334{
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100335 fdtab[fd].ev = 0;
Willy Tarreau037d2c12012-11-06 02:34:46 +0100336 fdtab[fd].new = 1;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100337 fdtab[fd].linger_risk = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200338 if (fd + 1 > maxfd)
339 maxfd = fd + 1;
340}
341
342
343#endif /* _PROTO_FD_H */
344
345/*
346 * Local variables:
347 * c-indent-level: 8
348 * c-basic-offset: 8
349 * End:
350 */