blob: 56e20827caafcdf3682cbad1dd55b2941b9559a9 [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
Olivier Houchard1fc05162017-04-06 01:05:05 +020044/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
45 * The file descriptor is kept open.
46 */
47void fd_remove(int fd);
48
Willy Tarreau4f60f162007-04-08 16:39:58 +020049/* disable the specified poller */
50void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020051
Willy Tarreau2a429502006-10-15 14:52:29 +020052/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020053 * Initialize the pollers till the best one is found.
54 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020055 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020056 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020057int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020058
Willy Tarreau4f60f162007-04-08 16:39:58 +020059/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020060 * Deinitialize the pollers.
61 */
62void deinit_pollers();
63
64/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020065 * Some pollers may lose their connection after a fork(). It may be necessary
66 * to create initialize part of them again. Returns 0 in case of failure,
67 * otherwise 1. The fork() function may be NULL if unused. In case of error,
68 * the the current poller is destroyed and the caller is responsible for trying
69 * another one by calling init_pollers() again.
70 */
71int fork_poller();
72
73/*
74 * Lists the known pollers on <out>.
75 * Should be performed only before initialization.
76 */
77int list_pollers(FILE *out);
78
79/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020080 * Runs the polling loop
81 */
82void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +020083
Willy Tarreau033cd9d2014-01-25 19:24:15 +010084/* Scan and process the cached events. This should be called right after
Willy Tarreau09f24562012-11-11 16:43:45 +010085 * the poller.
86 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +010087void fd_process_cached_events();
Willy Tarreau09f24562012-11-11 16:43:45 +010088
Willy Tarreau5be2f352014-11-19 19:43:05 +010089/* Mark fd <fd> as updated for polling and allocate an entry in the update list
90 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +010091 */
Willy Tarreau5be2f352014-11-19 19:43:05 +010092static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +010093{
94 if (fdtab[fd].updated)
95 /* already scheduled for update */
96 return;
Willy Tarreau7be79a42012-11-11 15:02:54 +010097 fdtab[fd].updated = 1;
Willy Tarreau4a291442012-12-13 23:34:18 +010098 fd_updt[fd_nbupdt++] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +010099}
100
101
Willy Tarreau899d9572014-01-25 19:20:35 +0100102/* Allocates a cache entry for a file descriptor if it does not yet have one.
103 * This can be done at any time.
104 */
105static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100106{
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100107 if (fdtab[fd].cache)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100108 return;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100109 fd_cache_num++;
110 fdtab[fd].cache = fd_cache_num;
111 fd_cache[fd_cache_num-1] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100112}
113
Willy Tarreau899d9572014-01-25 19:20:35 +0100114/* Removes entry used by fd <fd> from the FD cache and replaces it with the
115 * last one. The fdtab.cache is adjusted to match the back reference if needed.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100116 * If the fd has no entry assigned, return immediately.
117 */
Willy Tarreau899d9572014-01-25 19:20:35 +0100118static inline void fd_release_cache_entry(int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100119{
120 unsigned int pos;
121
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100122 pos = fdtab[fd].cache;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100123 if (!pos)
124 return;
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100125 fdtab[fd].cache = 0;
Willy Tarreau16f649c2014-01-25 19:10:48 +0100126 fd_cache_num--;
127 if (likely(pos <= fd_cache_num)) {
Willy Tarreau7be79a42012-11-11 15:02:54 +0100128 /* was not the last entry */
Willy Tarreau16f649c2014-01-25 19:10:48 +0100129 fd = fd_cache[fd_cache_num];
130 fd_cache[pos - 1] = fd;
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100131 fdtab[fd].cache = pos;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100132 }
133}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200134
Willy Tarreau25002d22014-01-25 10:32:56 +0100135/* Computes the new polled status based on the active and ready statuses, for
136 * each direction. This is meant to be used by pollers while processing updates.
137 */
138static inline int fd_compute_new_polled_status(int state)
139{
140 if (state & FD_EV_ACTIVE_R) {
141 if (!(state & FD_EV_READY_R))
142 state |= FD_EV_POLLED_R;
143 }
144 else
145 state &= ~FD_EV_POLLED_R;
146
147 if (state & FD_EV_ACTIVE_W) {
148 if (!(state & FD_EV_READY_W))
149 state |= FD_EV_POLLED_W;
150 }
151 else
152 state &= ~FD_EV_POLLED_W;
153
154 return state;
155}
156
Willy Tarreau5be2f352014-11-19 19:43:05 +0100157/* This function automatically enables/disables caching for an entry depending
158 * on its state, and also possibly creates an update entry so that the poller
159 * does its job as well. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100160 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100161static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100162{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100163 /* 3 states for each direction require a polling update */
164 if ((fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_ACTIVE_R)) == FD_EV_POLLED_R ||
165 (fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_READY_R | FD_EV_ACTIVE_R)) == FD_EV_ACTIVE_R ||
166 (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_ACTIVE_W)) == FD_EV_POLLED_W ||
167 (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_READY_W | FD_EV_ACTIVE_W)) == FD_EV_ACTIVE_W)
168 updt_fd_polling(fd);
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100169
Willy Tarreau5be2f352014-11-19 19:43:05 +0100170 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
171 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
172 ((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 +0100173 fd_alloc_cache_entry(fd);
174 }
175 else {
176 fd_release_cache_entry(fd);
177 }
178}
179
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100180/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100181 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100182 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100183static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100184{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100185 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100186}
187
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100188/*
189 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100190 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100191static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100192{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100193 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100194}
195
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100196/*
197 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100198 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100199static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100200{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100201 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
202}
203
204/*
205 * returns true if the FD is polled for recv
206 */
207static inline int fd_recv_polled(const int fd)
208{
209 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
210}
211
212/*
213 * returns the FD's send state (FD_EV_*)
214 */
215static inline int fd_send_state(const int fd)
216{
217 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
218}
219
220/*
221 * returns true if the FD is active for send
222 */
223static inline int fd_send_active(const int fd)
224{
225 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100226}
227
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100228/*
229 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100230 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100231static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100232{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100233 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
234}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100235
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100236/*
237 * returns true if the FD is polled for send
238 */
239static inline int fd_send_polled(const int fd)
240{
241 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
242}
243
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200244/*
245 * returns true if the FD is active for recv or send
246 */
247static inline int fd_active(const int fd)
248{
249 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
250}
251
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100252/* Disable processing recv events on fd <fd> */
253static inline void fd_stop_recv(int fd)
254{
Christopher Fauletd82b1802017-08-30 10:07:47 +0200255 if (fd_recv_active(fd)) {
256 fdtab[fd].state &= ~FD_EV_ACTIVE_R;
257 fd_update_cache(fd); /* need an update entry to change the state */
258 }
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100259}
260
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100261/* Disable processing send events on fd <fd> */
262static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100263{
Christopher Fauletd82b1802017-08-30 10:07:47 +0200264 if (fd_send_active(fd)) {
265 fdtab[fd].state &= ~FD_EV_ACTIVE_W;
266 fd_update_cache(fd); /* need an update entry to change the state */
267 }
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100268}
269
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100270/* Disable processing of events on fd <fd> for both directions. */
271static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200272{
Christopher Fauletd82b1802017-08-30 10:07:47 +0200273 if (fd_active(fd)) {
274 fdtab[fd].state &= ~FD_EV_ACTIVE_RW;
275 fd_update_cache(fd); /* need an update entry to change the state */
276 }
Willy Tarreau49b046d2012-08-09 12:11:58 +0200277}
278
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100279/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
280static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200281{
Christopher Fauletd82b1802017-08-30 10:07:47 +0200282 if (fd_recv_ready(fd)) {
283 fdtab[fd].state &= ~FD_EV_READY_R;
284 fd_update_cache(fd); /* need an update entry to change the state */
285 }
Willy Tarreau49b046d2012-08-09 12:11:58 +0200286}
287
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100288/* Report that FD <fd> can receive anymore without polling. */
289static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200290{
Christopher Fauletd82b1802017-08-30 10:07:47 +0200291 if (!fd_recv_ready(fd)) {
292 fdtab[fd].state |= FD_EV_READY_R;
293 fd_update_cache(fd); /* need an update entry to change the state */
294 }
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200295}
296
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100297/* Disable readiness when polled. This is useful to interrupt reading when it
298 * is suspected that the end of data might have been reached (eg: short read).
299 * This can only be done using level-triggered pollers, so if any edge-triggered
300 * is ever implemented, a test will have to be added here.
301 */
302static inline void fd_done_recv(const int fd)
303{
Christopher Fauletd82b1802017-08-30 10:07:47 +0200304 if (fd_recv_polled(fd) && fd_recv_ready(fd)) {
305 fdtab[fd].state &= ~FD_EV_READY_R;
306 fd_update_cache(fd); /* need an update entry to change the state */
307 }
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100308}
309
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100310/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
311static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200312{
Christopher Fauletd82b1802017-08-30 10:07:47 +0200313 if (fd_send_ready(fd)) {
314 fdtab[fd].state &= ~FD_EV_READY_W;
315 fd_update_cache(fd); /* need an update entry to change the state */
316 }
Willy Tarreau49b046d2012-08-09 12:11:58 +0200317}
318
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100319/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
320static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200321{
Christopher Fauletd82b1802017-08-30 10:07:47 +0200322 if (!fd_send_ready(fd)) {
323 fdtab[fd].state |= FD_EV_READY_W;
324 fd_update_cache(fd); /* need an update entry to change the state */
325 }
Willy Tarreau49b046d2012-08-09 12:11:58 +0200326}
Willy Tarreau2a429502006-10-15 14:52:29 +0200327
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100328/* Prepare FD <fd> to try to receive */
329static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200330{
Christopher Fauletd82b1802017-08-30 10:07:47 +0200331 if (!fd_recv_active(fd)) {
332 fdtab[fd].state |= FD_EV_ACTIVE_R;
333 fd_update_cache(fd); /* need an update entry to change the state */
334 }
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200335}
336
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100337/* Prepare FD <fd> to try to send */
338static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200339{
Christopher Fauletd82b1802017-08-30 10:07:47 +0200340 if (!fd_send_active(fd)) {
341 fdtab[fd].state |= FD_EV_ACTIVE_W;
342 fd_update_cache(fd); /* need an update entry to change the state */
343 }
Willy Tarreau49b046d2012-08-09 12:11:58 +0200344}
Willy Tarreau2a429502006-10-15 14:52:29 +0200345
Christopher Faulet21e92672017-08-30 10:30:04 +0200346/* Update events seen for FD <fd> and its state if needed. This should be called
347 * by the poller to set FD_POLL_* flags. */
348static inline void fd_update_events(int fd, int evts)
349{
350 fdtab[fd].ev &= FD_POLL_STICKY;
351 fdtab[fd].ev |= evts;
352
353 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
354 fd_may_recv(fd);
355
356 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
357 fd_may_send(fd);
358}
359
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100360/* Prepares <fd> for being polled */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361static inline void fd_insert(int fd)
362{
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100363 fdtab[fd].ev = 0;
Willy Tarreau037d2c12012-11-06 02:34:46 +0100364 fdtab[fd].new = 1;
Christopher Fauletd531f882017-06-01 16:55:03 +0200365 fdtab[fd].updated = 0;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100366 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200367 fdtab[fd].cloned = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368 if (fd + 1 > maxfd)
369 maxfd = fd + 1;
370}
371
372
373#endif /* _PROTO_FD_H */
374
375/*
376 * Local variables:
377 * c-indent-level: 8
378 * c-basic-offset: 8
379 * End:
380 */