blob: 87309bf0a38a24790c9a3237bf2a21905f4cd9eb [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 Tarreau5be2f352014-11-19 19:43:05 +010084/* Mark fd <fd> as updated for polling and allocate an entry in the update list
85 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +010086 */
Willy Tarreau5be2f352014-11-19 19:43:05 +010087static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +010088{
89 if (fdtab[fd].updated)
90 /* already scheduled for update */
91 return;
Willy Tarreau7be79a42012-11-11 15:02:54 +010092 fdtab[fd].updated = 1;
Willy Tarreau4a291442012-12-13 23:34:18 +010093 fd_updt[fd_nbupdt++] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +010094}
95
96
Willy Tarreau899d9572014-01-25 19:20:35 +010097/* Allocates a cache entry for a file descriptor if it does not yet have one.
98 * This can be done at any time.
99 */
100static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100101{
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100102 if (fdtab[fd].cache)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100103 return;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100104 fd_cache_num++;
105 fdtab[fd].cache = fd_cache_num;
106 fd_cache[fd_cache_num-1] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100107}
108
Willy Tarreau899d9572014-01-25 19:20:35 +0100109/* Removes entry used by fd <fd> from the FD cache and replaces it with the
110 * last one. The fdtab.cache is adjusted to match the back reference if needed.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100111 * If the fd has no entry assigned, return immediately.
112 */
Willy Tarreau899d9572014-01-25 19:20:35 +0100113static inline void fd_release_cache_entry(int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100114{
115 unsigned int pos;
116
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100117 pos = fdtab[fd].cache;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100118 if (!pos)
119 return;
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100120 fdtab[fd].cache = 0;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100121 fd_cache_num--;
122 if (likely(pos <= fd_cache_num)) {
Willy Tarreau7be79a42012-11-11 15:02:54 +0100123 /* was not the last entry */
Willy Tarreau16f649c2014-01-25 19:10:48 +0100124 fd = fd_cache[fd_cache_num];
125 fd_cache[pos - 1] = fd;
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100126 fdtab[fd].cache = pos;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100127 }
128}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200129
Willy Tarreau25002d22014-01-25 10:32:56 +0100130/* Computes the new polled status based on the active and ready statuses, for
131 * each direction. This is meant to be used by pollers while processing updates.
132 */
133static inline int fd_compute_new_polled_status(int state)
134{
135 if (state & FD_EV_ACTIVE_R) {
136 if (!(state & FD_EV_READY_R))
137 state |= FD_EV_POLLED_R;
138 }
139 else
140 state &= ~FD_EV_POLLED_R;
141
142 if (state & FD_EV_ACTIVE_W) {
143 if (!(state & FD_EV_READY_W))
144 state |= FD_EV_POLLED_W;
145 }
146 else
147 state &= ~FD_EV_POLLED_W;
148
149 return state;
150}
151
Willy Tarreau5be2f352014-11-19 19:43:05 +0100152/* This function automatically enables/disables caching for an entry depending
153 * on its state, and also possibly creates an update entry so that the poller
154 * does its job as well. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100155 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100156static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100157{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100158 /* 3 states for each direction require a polling update */
159 if ((fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_ACTIVE_R)) == FD_EV_POLLED_R ||
160 (fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_READY_R | FD_EV_ACTIVE_R)) == FD_EV_ACTIVE_R ||
161 (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_ACTIVE_W)) == FD_EV_POLLED_W ||
162 (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_READY_W | FD_EV_ACTIVE_W)) == FD_EV_ACTIVE_W)
163 updt_fd_polling(fd);
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100164
Willy Tarreau5be2f352014-11-19 19:43:05 +0100165 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
166 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
167 ((fdtab[fd].state & (FD_EV_READY_W | FD_EV_ACTIVE_W)) == (FD_EV_READY_W | FD_EV_ACTIVE_W))) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100168 fd_alloc_cache_entry(fd);
169 }
170 else {
171 fd_release_cache_entry(fd);
172 }
173}
174
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100175/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100176 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100177 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100178static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100179{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100180 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100181}
182
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100183/*
184 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100185 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100186static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100187{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100188 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100189}
190
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100191/*
192 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100193 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100194static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100195{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100196 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
197}
198
199/*
200 * returns true if the FD is polled for recv
201 */
202static inline int fd_recv_polled(const int fd)
203{
204 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
205}
206
207/*
208 * returns the FD's send state (FD_EV_*)
209 */
210static inline int fd_send_state(const int fd)
211{
212 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
213}
214
215/*
216 * returns true if the FD is active for send
217 */
218static inline int fd_send_active(const int fd)
219{
220 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100221}
222
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100223/*
224 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100225 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100226static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100227{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100228 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
229}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100230
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100231/*
232 * returns true if the FD is polled for send
233 */
234static inline int fd_send_polled(const int fd)
235{
236 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
237}
238
239/* Disable processing recv events on fd <fd> */
240static inline void fd_stop_recv(int fd)
241{
242 if (!((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_R))
243 return; /* already disabled */
244 fdtab[fd].state &= ~FD_EV_ACTIVE_R;
Willy Tarreau5be2f352014-11-19 19:43:05 +0100245 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100246}
247
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100248/* Disable processing send events on fd <fd> */
249static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100250{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100251 if (!((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_W))
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100252 return; /* already disabled */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100253 fdtab[fd].state &= ~FD_EV_ACTIVE_W;
Willy Tarreau5be2f352014-11-19 19:43:05 +0100254 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100255}
256
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100257/* Disable processing of events on fd <fd> for both directions. */
258static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200259{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100260 if (!((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_RW))
261 return; /* already disabled */
262 fdtab[fd].state &= ~FD_EV_ACTIVE_RW;
Willy Tarreau5be2f352014-11-19 19:43:05 +0100263 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau49b046d2012-08-09 12:11:58 +0200264}
265
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100266/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
267static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200268{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100269 if (!(((unsigned int)fdtab[fd].state) & FD_EV_READY_R))
270 return; /* already marked as blocked */
271 fdtab[fd].state &= ~FD_EV_READY_R;
Willy Tarreau5be2f352014-11-19 19:43:05 +0100272 fd_update_cache(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200273}
274
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100275/* Report that FD <fd> can receive anymore without polling. */
276static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200277{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100278 if (((unsigned int)fdtab[fd].state) & FD_EV_READY_R)
279 return; /* already marked as blocked */
280 fdtab[fd].state |= FD_EV_READY_R;
Willy Tarreau5be2f352014-11-19 19:43:05 +0100281 fd_update_cache(fd);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200282}
283
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100284/* Disable readiness when polled. This is useful to interrupt reading when it
285 * is suspected that the end of data might have been reached (eg: short read).
286 * This can only be done using level-triggered pollers, so if any edge-triggered
287 * is ever implemented, a test will have to be added here.
288 */
289static inline void fd_done_recv(const int fd)
290{
291 if (fd_recv_polled(fd))
292 fd_cant_recv(fd);
293}
294
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100295/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
296static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200297{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100298 if (!(((unsigned int)fdtab[fd].state) & FD_EV_READY_W))
299 return; /* already marked as blocked */
300 fdtab[fd].state &= ~FD_EV_READY_W;
Willy Tarreau5be2f352014-11-19 19:43:05 +0100301 fd_update_cache(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200302}
303
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100304/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
305static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200306{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100307 if (((unsigned int)fdtab[fd].state) & FD_EV_READY_W)
308 return; /* already marked as blocked */
309 fdtab[fd].state |= FD_EV_READY_W;
Willy Tarreau5be2f352014-11-19 19:43:05 +0100310 fd_update_cache(fd);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200311}
Willy Tarreau2a429502006-10-15 14:52:29 +0200312
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100313/* Prepare FD <fd> to try to receive */
314static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200315{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100316 if (((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_R))
317 return; /* already enabled */
318 fdtab[fd].state |= FD_EV_ACTIVE_R;
Willy Tarreau5be2f352014-11-19 19:43:05 +0100319 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200320}
321
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100322/* Prepare FD <fd> to try to send */
323static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200324{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100325 if (((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_W))
326 return; /* already enabled */
327 fdtab[fd].state |= FD_EV_ACTIVE_W;
Willy Tarreau5be2f352014-11-19 19:43:05 +0100328 fd_update_cache(fd); /* need an update entry to change the state */
Willy Tarreau49b046d2012-08-09 12:11:58 +0200329}
Willy Tarreau2a429502006-10-15 14:52:29 +0200330
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100331/* Prepares <fd> for being polled */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332static inline void fd_insert(int fd)
333{
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100334 fdtab[fd].ev = 0;
Willy Tarreau037d2c12012-11-06 02:34:46 +0100335 fdtab[fd].new = 1;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100336 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200337 fdtab[fd].cloned = 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 */