blob: 745c4fe458a7ea4aa981280bb13a623e9256b92b [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 Tarreauf37ba942018-10-17 11:25:54 +020031#include <common/ticks.h>
32#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <types/fd.h>
Willy Tarreau609aad92018-11-22 08:31:09 +010034#include <proto/activity.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreau7be79a42012-11-11 15:02:54 +010036/* public variables */
Christopher Fauletd4604ad2017-05-29 10:40:41 +020037
Olivier Houchard4815c8c2018-01-24 18:17:56 +010038extern volatile struct fdlist fd_cache;
39extern volatile struct fdlist fd_cache_local[MAX_THREADS];
40
Olivier Houchard6b96f722018-04-25 16:58:25 +020041extern volatile struct fdlist update_list;
42
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020043extern unsigned long *polled_mask;
44
Christopher Faulet69553fe2018-01-15 11:57:03 +010045extern unsigned long fd_cache_mask; // Mask of threads with events in the cache
Christopher Fauletd4604ad2017-05-29 10:40:41 +020046
47extern THREAD_LOCAL int *fd_updt; // FD updates list
48extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
49
Olivier Houchard79321b92018-07-26 17:55:11 +020050extern int poller_wr_pipe[MAX_THREADS];
51
Olivier Houchard7c49d2e2019-04-16 18:37:05 +020052extern volatile int ha_used_fds; // Number of FDs we're currently using
53
Willy Tarreau8b949692017-11-26 11:07:34 +010054__decl_hathreads(extern HA_RWLOCK_T __attribute__((aligned(64))) fdcache_lock); /* global lock to protect fd_cache array */
Willy Tarreau7be79a42012-11-11 15:02:54 +010055
Willy Tarreau173d9952018-01-26 21:48:23 +010056/* Deletes an FD from the fdsets.
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 * The file descriptor is also closed.
58 */
59void fd_delete(int fd);
60
Willy Tarreau173d9952018-01-26 21:48:23 +010061/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +020062 * The file descriptor is kept open.
63 */
64void fd_remove(int fd);
65
Willy Tarreau2d7f81b2019-02-21 22:19:17 +010066/* close all FDs starting from <start> */
67void my_closefrom(int start);
68
Willy Tarreau4f60f162007-04-08 16:39:58 +020069/* disable the specified poller */
70void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020071
Olivier Houchard79321b92018-07-26 17:55:11 +020072void poller_pipe_io_handler(int fd);
73
Willy Tarreau2a429502006-10-15 14:52:29 +020074/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020075 * Initialize the pollers till the best one is found.
76 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020077 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020078 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020079int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020080
Willy Tarreau4f60f162007-04-08 16:39:58 +020081/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020082 * Deinitialize the pollers.
83 */
84void deinit_pollers();
85
86/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020087 * Some pollers may lose their connection after a fork(). It may be necessary
88 * to create initialize part of them again. Returns 0 in case of failure,
89 * otherwise 1. The fork() function may be NULL if unused. In case of error,
90 * the the current poller is destroyed and the caller is responsible for trying
91 * another one by calling init_pollers() again.
92 */
93int fork_poller();
94
95/*
96 * Lists the known pollers on <out>.
97 * Should be performed only before initialization.
98 */
99int list_pollers(FILE *out);
100
101/*
Willy Tarreau4f60f162007-04-08 16:39:58 +0200102 * Runs the polling loop
103 */
104void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +0200105
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100106/* Scan and process the cached events. This should be called right after
Willy Tarreau09f24562012-11-11 16:43:45 +0100107 * the poller.
108 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100109void fd_process_cached_events();
Willy Tarreau09f24562012-11-11 16:43:45 +0100110
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200111void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off);
112void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100113
Willy Tarreau5be2f352014-11-19 19:43:05 +0100114/* Mark fd <fd> as updated for polling and allocate an entry in the update list
115 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +0100116 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100117static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100118{
Olivier Houchard6aab7372018-08-17 13:37:59 +0200119 if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) {
Olivier Houchard6b96f722018-04-25 16:58:25 +0200120 unsigned int oldupdt;
121
122 /* note: we don't have a test-and-set yet in hathreads */
Willy Tarreau4d841862018-01-17 22:57:54 +0100123
Olivier Houchard6b96f722018-04-25 16:58:25 +0200124 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
125 return;
126
Olivier Houchardd3608792019-03-08 18:47:42 +0100127 oldupdt = _HA_ATOMIC_ADD(&fd_nbupdt, 1) - 1;
Olivier Houchard6b96f722018-04-25 16:58:25 +0200128 fd_updt[oldupdt] = fd;
129 } else {
130 unsigned long update_mask = fdtab[fd].update_mask;
131 do {
132 if (update_mask == fdtab[fd].thread_mask)
133 return;
Olivier Houchardd3608792019-03-08 18:47:42 +0100134 } while (!_HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask,
Olivier Houchard6b96f722018-04-25 16:58:25 +0200135 fdtab[fd].thread_mask));
136 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
137 }
138
139}
Willy Tarreau4d841862018-01-17 22:57:54 +0100140
Olivier Houchard6b96f722018-04-25 16:58:25 +0200141/* Called from the poller to acknoledge we read an entry from the global
142 * update list, to remove our bit from the update_mask, and remove it from
143 * the list if we were the last one.
144 */
145static inline void done_update_polling(int fd)
146{
147 unsigned long update_mask;
148
Olivier Houchardd3608792019-03-08 18:47:42 +0100149 update_mask = _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +0200150 while ((update_mask & all_threads_mask)== 0) {
151 /* If we were the last one that had to update that entry, remove it from the list */
152 fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update));
Olivier Houchard6b96f722018-04-25 16:58:25 +0200153 update_mask = (volatile unsigned long)fdtab[fd].update_mask;
154 if ((update_mask & all_threads_mask) != 0) {
155 /* Maybe it's been re-updated in the meanwhile, and we
156 * wrongly removed it from the list, if so, re-add it
157 */
158 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
159 update_mask = (volatile unsigned long)(fdtab[fd].update_mask);
160 /* And then check again, just in case after all it
161 * should be removed, even if it's very unlikely, given
162 * the current thread wouldn't have been able to take
163 * care of it yet */
164 } else
165 break;
Willy Tarreau4d841862018-01-17 22:57:54 +0100166
Olivier Houchard6b96f722018-04-25 16:58:25 +0200167 }
Willy Tarreau7be79a42012-11-11 15:02:54 +0100168}
169
Willy Tarreau899d9572014-01-25 19:20:35 +0100170/* Allocates a cache entry for a file descriptor if it does not yet have one.
171 * This can be done at any time.
172 */
173static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100174{
Olivier Houchardd3608792019-03-08 18:47:42 +0100175 _HA_ATOMIC_OR(&fd_cache_mask, fdtab[fd].thread_mask);
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100176 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200177 fd_add_to_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd, offsetof(struct fdtab, cache));
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100178 else
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200179 fd_add_to_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache));
Willy Tarreau7be79a42012-11-11 15:02:54 +0100180}
181
Willy Tarreau899d9572014-01-25 19:20:35 +0100182/* Removes entry used by fd <fd> from the FD cache and replaces it with the
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100183 * last one.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100184 * If the fd has no entry assigned, return immediately.
185 */
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100186static inline void fd_release_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100187{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100188 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200189 fd_rm_from_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd, offsetof(struct fdtab, cache));
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100190 else
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200191 fd_rm_from_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache));
Willy Tarreau7be79a42012-11-11 15:02:54 +0100192}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200193
Willy Tarreau5be2f352014-11-19 19:43:05 +0100194/* This function automatically enables/disables caching for an entry depending
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100195 * on its state. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100196 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100197static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100198{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100199 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
200 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
201 ((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 +0100202 fd_alloc_cache_entry(fd);
203 }
204 else {
205 fd_release_cache_entry(fd);
206 }
207}
208
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100209/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100210 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100211 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100212static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100213{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100214 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100215}
216
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100217/*
218 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100219 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100220static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100221{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100222 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100223}
224
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100225/*
226 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100227 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100228static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100229{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100230 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
231}
232
233/*
234 * returns true if the FD is polled for recv
235 */
236static inline int fd_recv_polled(const int fd)
237{
238 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
239}
240
241/*
242 * returns the FD's send state (FD_EV_*)
243 */
244static inline int fd_send_state(const int fd)
245{
246 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
247}
248
249/*
250 * returns true if the FD is active for send
251 */
252static inline int fd_send_active(const int fd)
253{
254 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100255}
256
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100257/*
258 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100259 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100260static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100261{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100262 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
263}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100264
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100265/*
266 * returns true if the FD is polled for send
267 */
268static inline int fd_send_polled(const int fd)
269{
270 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
271}
272
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200273/*
274 * returns true if the FD is active for recv or send
275 */
276static inline int fd_active(const int fd)
277{
278 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
279}
280
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100281/* Disable processing recv events on fd <fd> */
282static inline void fd_stop_recv(int fd)
283{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100284 unsigned char old, new;
Richard Russobc9d9842019-02-20 12:43:45 -0800285 unsigned long locked;
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100286
287 old = fdtab[fd].state;
288 do {
289 if (!(old & FD_EV_ACTIVE_R))
290 return;
291 new = old & ~FD_EV_ACTIVE_R;
292 new &= ~FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100293 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100294
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100295 if ((old ^ new) & FD_EV_POLLED_R)
296 updt_fd_polling(fd);
297
Richard Russobc9d9842019-02-20 12:43:45 -0800298 locked = atleast2(fdtab[fd].thread_mask);
299 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200300 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100301 fd_update_cache(fd); /* need an update entry to change the state */
Richard Russobc9d9842019-02-20 12:43:45 -0800302 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200303 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100304}
305
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100306/* Disable processing send events on fd <fd> */
307static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100308{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100309 unsigned char old, new;
Richard Russobc9d9842019-02-20 12:43:45 -0800310 unsigned long locked;
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100311
312 old = fdtab[fd].state;
313 do {
314 if (!(old & FD_EV_ACTIVE_W))
315 return;
316 new = old & ~FD_EV_ACTIVE_W;
317 new &= ~FD_EV_POLLED_W;
Olivier Houchardd3608792019-03-08 18:47:42 +0100318 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100319
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100320 if ((old ^ new) & FD_EV_POLLED_W)
321 updt_fd_polling(fd);
322
Richard Russobc9d9842019-02-20 12:43:45 -0800323 locked = atleast2(fdtab[fd].thread_mask);
324 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200325 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100326 fd_update_cache(fd); /* need an update entry to change the state */
Richard Russobc9d9842019-02-20 12:43:45 -0800327 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200328 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100329}
330
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100331/* Disable processing of events on fd <fd> for both directions. */
332static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200333{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100334 unsigned char old, new;
Richard Russobc9d9842019-02-20 12:43:45 -0800335 unsigned long locked;
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100336
337 old = fdtab[fd].state;
338 do {
339 if (!(old & FD_EV_ACTIVE_RW))
340 return;
341 new = old & ~FD_EV_ACTIVE_RW;
342 new &= ~FD_EV_POLLED_RW;
Olivier Houchardd3608792019-03-08 18:47:42 +0100343 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100344
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100345 if ((old ^ new) & FD_EV_POLLED_RW)
346 updt_fd_polling(fd);
347
Richard Russobc9d9842019-02-20 12:43:45 -0800348 locked = atleast2(fdtab[fd].thread_mask);
349 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200350 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100351 fd_update_cache(fd); /* need an update entry to change the state */
Richard Russobc9d9842019-02-20 12:43:45 -0800352 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200353 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200354}
355
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100356/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
357static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200358{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100359 unsigned char old, new;
Richard Russobc9d9842019-02-20 12:43:45 -0800360 unsigned long locked;
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100361
362 old = fdtab[fd].state;
363 do {
364 if (!(old & FD_EV_READY_R))
365 return;
366 new = old & ~FD_EV_READY_R;
367 if (new & FD_EV_ACTIVE_R)
368 new |= FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100369 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100370
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100371 if ((old ^ new) & FD_EV_POLLED_R)
372 updt_fd_polling(fd);
373
Richard Russobc9d9842019-02-20 12:43:45 -0800374 locked = atleast2(fdtab[fd].thread_mask);
375 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200376 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100377 fd_update_cache(fd); /* need an update entry to change the state */
Richard Russobc9d9842019-02-20 12:43:45 -0800378 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200379 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200380}
381
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100382/* Report that FD <fd> can receive anymore without polling. */
383static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200384{
Richard Russobc9d9842019-02-20 12:43:45 -0800385 unsigned long locked;
386
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100387 /* marking ready never changes polled status */
Olivier Houchardd3608792019-03-08 18:47:42 +0100388 _HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_R);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100389
Richard Russobc9d9842019-02-20 12:43:45 -0800390 locked = atleast2(fdtab[fd].thread_mask);
391 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200392 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100393 fd_update_cache(fd); /* need an update entry to change the state */
Richard Russobc9d9842019-02-20 12:43:45 -0800394 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200395 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200396}
397
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100398/* Disable readiness when polled. This is useful to interrupt reading when it
399 * is suspected that the end of data might have been reached (eg: short read).
400 * This can only be done using level-triggered pollers, so if any edge-triggered
401 * is ever implemented, a test will have to be added here.
402 */
403static inline void fd_done_recv(const int fd)
404{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100405 unsigned char old, new;
Richard Russobc9d9842019-02-20 12:43:45 -0800406 unsigned long locked;
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100407
408 old = fdtab[fd].state;
409 do {
410 if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R))
411 return;
412 new = old & ~FD_EV_READY_R;
413 if (new & FD_EV_ACTIVE_R)
414 new |= FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100415 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100416
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100417 if ((old ^ new) & FD_EV_POLLED_R)
418 updt_fd_polling(fd);
419
Richard Russobc9d9842019-02-20 12:43:45 -0800420 locked = atleast2(fdtab[fd].thread_mask);
421 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200422 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100423 fd_update_cache(fd); /* need an update entry to change the state */
Richard Russobc9d9842019-02-20 12:43:45 -0800424 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200425 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100426}
427
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100428/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
429static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200430{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100431 unsigned char old, new;
Richard Russobc9d9842019-02-20 12:43:45 -0800432 unsigned long locked;
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100433
434 old = fdtab[fd].state;
435 do {
436 if (!(old & FD_EV_READY_W))
437 return;
438 new = old & ~FD_EV_READY_W;
439 if (new & FD_EV_ACTIVE_W)
440 new |= FD_EV_POLLED_W;
Olivier Houchardd3608792019-03-08 18:47:42 +0100441 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100442
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100443 if ((old ^ new) & FD_EV_POLLED_W)
444 updt_fd_polling(fd);
445
Richard Russobc9d9842019-02-20 12:43:45 -0800446 locked = atleast2(fdtab[fd].thread_mask);
447 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200448 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100449 fd_update_cache(fd); /* need an update entry to change the state */
Richard Russobc9d9842019-02-20 12:43:45 -0800450 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200451 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200452}
453
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100454/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
455static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200456{
Richard Russobc9d9842019-02-20 12:43:45 -0800457 unsigned long locked;
458
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100459 /* marking ready never changes polled status */
Olivier Houchardd3608792019-03-08 18:47:42 +0100460 _HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_W);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100461
Richard Russobc9d9842019-02-20 12:43:45 -0800462 locked = atleast2(fdtab[fd].thread_mask);
463 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200464 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100465 fd_update_cache(fd); /* need an update entry to change the state */
Richard Russobc9d9842019-02-20 12:43:45 -0800466 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200467 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200468}
Willy Tarreau2a429502006-10-15 14:52:29 +0200469
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100470/* Prepare FD <fd> to try to receive */
471static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200472{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100473 unsigned char old, new;
Richard Russobc9d9842019-02-20 12:43:45 -0800474 unsigned long locked;
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100475
476 old = fdtab[fd].state;
477 do {
478 if (old & FD_EV_ACTIVE_R)
479 return;
480 new = old | FD_EV_ACTIVE_R;
481 if (!(new & FD_EV_READY_R))
482 new |= FD_EV_POLLED_R;
Olivier Houchardd3608792019-03-08 18:47:42 +0100483 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100484
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100485 if ((old ^ new) & FD_EV_POLLED_R)
486 updt_fd_polling(fd);
487
Richard Russobc9d9842019-02-20 12:43:45 -0800488 locked = atleast2(fdtab[fd].thread_mask);
489 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200490 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100491 fd_update_cache(fd); /* need an update entry to change the state */
Richard Russobc9d9842019-02-20 12:43:45 -0800492 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200493 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200494}
495
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100496/* Prepare FD <fd> to try to send */
497static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200498{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100499 unsigned char old, new;
Richard Russobc9d9842019-02-20 12:43:45 -0800500 unsigned long locked;
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100501
502 old = fdtab[fd].state;
503 do {
504 if (old & FD_EV_ACTIVE_W)
505 return;
506 new = old | FD_EV_ACTIVE_W;
507 if (!(new & FD_EV_READY_W))
508 new |= FD_EV_POLLED_W;
Olivier Houchardd3608792019-03-08 18:47:42 +0100509 } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100510
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100511 if ((old ^ new) & FD_EV_POLLED_W)
512 updt_fd_polling(fd);
513
Richard Russobc9d9842019-02-20 12:43:45 -0800514 locked = atleast2(fdtab[fd].thread_mask);
515 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200516 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100517 fd_update_cache(fd); /* need an update entry to change the state */
Richard Russobc9d9842019-02-20 12:43:45 -0800518 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200519 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200520}
Willy Tarreau2a429502006-10-15 14:52:29 +0200521
Christopher Faulet21e92672017-08-30 10:30:04 +0200522/* Update events seen for FD <fd> and its state if needed. This should be called
523 * by the poller to set FD_POLL_* flags. */
524static inline void fd_update_events(int fd, int evts)
525{
Richard Russobc9d9842019-02-20 12:43:45 -0800526 unsigned long locked = atleast2(fdtab[fd].thread_mask);
527
528 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200529 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200530 fdtab[fd].ev &= FD_POLL_STICKY;
531 fdtab[fd].ev |= evts;
Richard Russobc9d9842019-02-20 12:43:45 -0800532 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200533 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200534
535 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
536 fd_may_recv(fd);
537
538 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
539 fd_may_send(fd);
540}
541
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100542/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100543static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544{
Richard Russobc9d9842019-02-20 12:43:45 -0800545 unsigned long locked = atleast2(thread_mask);
546
547 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200548 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100549 fdtab[fd].owner = owner;
550 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100551 fdtab[fd].ev = 0;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100552 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200553 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100554 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100555 /* note: do not reset polled_mask here as it indicates which poller
556 * still knows this FD from a possible previous round.
557 */
Richard Russobc9d9842019-02-20 12:43:45 -0800558 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200559 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200560 _HA_ATOMIC_ADD(&ha_used_fds, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200561}
562
Willy Tarreauf37ba942018-10-17 11:25:54 +0200563/* Computes the bounded poll() timeout based on the next expiration timer <next>
564 * by bounding it to MAX_DELAY_MS. <next> may equal TICK_ETERNITY. The pollers
565 * just needs to call this function right before polling to get their timeout
566 * value. Timeouts that are already expired (possibly due to a pending event)
567 * are accounted for in activity.poll_exp.
568 */
569static inline int compute_poll_timeout(int next)
570{
571 int wait_time;
572
573 if (!tick_isset(next))
574 wait_time = MAX_DELAY_MS;
575 else if (tick_is_expired(next, now_ms)) {
576 activity[tid].poll_exp++;
577 wait_time = 0;
578 }
579 else {
580 wait_time = TICKS_TO_MS(tick_remain(now_ms, next)) + 1;
581 if (wait_time > MAX_DELAY_MS)
582 wait_time = MAX_DELAY_MS;
583 }
584 return wait_time;
585}
586
Willy Tarreau322e6c72018-01-25 16:37:04 +0100587/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
588static inline void hap_fd_set(int fd, unsigned int *evts)
589{
Olivier Houchardd3608792019-03-08 18:47:42 +0100590 _HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100591}
592
593static inline void hap_fd_clr(int fd, unsigned int *evts)
594{
Olivier Houchardd3608792019-03-08 18:47:42 +0100595 _HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100596}
597
598static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
599{
600 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
601}
602
Olivier Houchard79321b92018-07-26 17:55:11 +0200603static inline void wake_thread(int tid)
604{
605 char c = 'c';
606
607 shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1));
608}
609
Willy Tarreaubaaee002006-06-26 02:48:02 +0200610
611#endif /* _PROTO_FD_H */
612
613/*
614 * Local variables:
615 * c-indent-level: 8
616 * c-basic-offset: 8
617 * End:
618 */