blob: 57dbaf65f6576e30d6bd1278a46c37bb8250d685 [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>
Christopher Fauletd4604ad2017-05-29 10:40:41 +020031
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <types/fd.h>
33
Willy Tarreau7be79a42012-11-11 15:02:54 +010034/* public variables */
Christopher Fauletd4604ad2017-05-29 10:40:41 +020035
Olivier Houchard4815c8c2018-01-24 18:17:56 +010036extern volatile struct fdlist fd_cache;
37extern volatile struct fdlist fd_cache_local[MAX_THREADS];
38
Christopher Faulet69553fe2018-01-15 11:57:03 +010039extern unsigned long fd_cache_mask; // Mask of threads with events in the cache
Christopher Fauletd4604ad2017-05-29 10:40:41 +020040
41extern THREAD_LOCAL int *fd_updt; // FD updates list
42extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
43
Willy Tarreau8b949692017-11-26 11:07:34 +010044__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 +010045
Willy Tarreau173d9952018-01-26 21:48:23 +010046/* Deletes an FD from the fdsets.
Willy Tarreaubaaee002006-06-26 02:48:02 +020047 * The file descriptor is also closed.
48 */
49void fd_delete(int fd);
50
Willy Tarreau173d9952018-01-26 21:48:23 +010051/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +020052 * The file descriptor is kept open.
53 */
54void fd_remove(int fd);
55
Willy Tarreau4f60f162007-04-08 16:39:58 +020056/* disable the specified poller */
57void disable_poller(const char *poller_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau2a429502006-10-15 14:52:29 +020059/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020060 * Initialize the pollers till the best one is found.
61 * If none works, returns 0, otherwise 1.
Willy Tarreauef1d1f82007-04-16 00:25:25 +020062 * The pollers register themselves just before main() is called.
Willy Tarreau2a429502006-10-15 14:52:29 +020063 */
Willy Tarreau4f60f162007-04-08 16:39:58 +020064int init_pollers();
Willy Tarreau2a429502006-10-15 14:52:29 +020065
Willy Tarreau4f60f162007-04-08 16:39:58 +020066/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020067 * Deinitialize the pollers.
68 */
69void deinit_pollers();
70
71/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020072 * Some pollers may lose their connection after a fork(). It may be necessary
73 * to create initialize part of them again. Returns 0 in case of failure,
74 * otherwise 1. The fork() function may be NULL if unused. In case of error,
75 * the the current poller is destroyed and the caller is responsible for trying
76 * another one by calling init_pollers() again.
77 */
78int fork_poller();
79
80/*
81 * Lists the known pollers on <out>.
82 * Should be performed only before initialization.
83 */
84int list_pollers(FILE *out);
85
86/*
Willy Tarreau4f60f162007-04-08 16:39:58 +020087 * Runs the polling loop
88 */
89void run_poller();
Willy Tarreau2a429502006-10-15 14:52:29 +020090
Willy Tarreau033cd9d2014-01-25 19:24:15 +010091/* Scan and process the cached events. This should be called right after
Willy Tarreau09f24562012-11-11 16:43:45 +010092 * the poller.
93 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +010094void fd_process_cached_events();
Willy Tarreau09f24562012-11-11 16:43:45 +010095
Willy Tarreau5be2f352014-11-19 19:43:05 +010096/* Mark fd <fd> as updated for polling and allocate an entry in the update list
97 * for this if it was not already there. This can be done at any time.
Willy Tarreaue8525452014-01-25 09:58:06 +010098 */
Willy Tarreau5be2f352014-11-19 19:43:05 +010099static inline void updt_fd_polling(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100100{
Willy Tarreau4d841862018-01-17 22:57:54 +0100101 unsigned int oldupdt;
102
103 /* note: we don't have a test-and-set yet in hathreads */
104
105 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
Willy Tarreau7be79a42012-11-11 15:02:54 +0100106 return;
Willy Tarreau4d841862018-01-17 22:57:54 +0100107
108 oldupdt = HA_ATOMIC_ADD(&fd_nbupdt, 1) - 1;
109 fd_updt[oldupdt] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100110}
111
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100112static inline void fd_add_to_fd_list(volatile struct fdlist *list, int fd)
113{
114 int next;
115 int new;
116 int old;
117 int last;
118
119redo_next:
Olivier Houchard12568362018-01-31 18:07:29 +0100120 next = fdtab[fd].cache.next;
121 /* Check that we're not already in the cache, and if not, lock us. */
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100122 if (next >= -2)
123 goto done;
Olivier Houchard12568362018-01-31 18:07:29 +0100124 if (!HA_ATOMIC_CAS(&fdtab[fd].cache.next, &next, -2))
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100125 goto redo_next;
126 __ha_barrier_store();
127redo_last:
128 /* First, insert in the linked list */
129 last = list->last;
130 old = -1;
131 new = fd;
132 if (unlikely(last == -1)) {
133 /* list is empty, try to add ourselves alone so that list->last=fd */
134
Olivier Houchard12568362018-01-31 18:07:29 +0100135 fdtab[fd].cache.prev = last;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100136
137 /* Make sure the "prev" store is visible before we update the last entry */
138 __ha_barrier_store();
139 if (unlikely(!HA_ATOMIC_CAS(&list->last, &old, new)))
140 goto redo_last;
141
142 /* list->first was necessary -1, we're guaranteed to be alone here */
143 list->first = fd;
144
145 /* since we're alone at the end of the list and still locked(-2),
146 * we know noone tried to add past us. Mark the end of list.
147 */
Olivier Houchard12568362018-01-31 18:07:29 +0100148 fdtab[fd].cache.next = -1;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100149 goto done; /* We're done ! */
150 } else {
151 /* non-empty list, add past the tail */
152 do {
153 new = fd;
154 old = -1;
Olivier Houchard12568362018-01-31 18:07:29 +0100155 fdtab[fd].cache.prev = last;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100156
157 __ha_barrier_store();
158
159 /* adding ourselves past the last element
160 * The CAS will only succeed if its next is -1,
161 * which means it's in the cache, and the last element.
162 */
Olivier Houchard12568362018-01-31 18:07:29 +0100163 if (likely(HA_ATOMIC_CAS(&fdtab[last].cache.next, &old, new)))
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100164 break;
165 goto redo_last;
166 } while (1);
167 }
168 /* Then, update the last entry */
169redo_fd_cache:
170 last = list->last;
171 __ha_barrier_load();
172
173 if (unlikely(!HA_ATOMIC_CAS(&list->last, &last, fd)))
174 goto redo_fd_cache;
175 __ha_barrier_store();
Olivier Houchard12568362018-01-31 18:07:29 +0100176 fdtab[fd].cache.next = -1;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100177 __ha_barrier_store();
178done:
179 return;
180}
Willy Tarreau7be79a42012-11-11 15:02:54 +0100181
Willy Tarreau899d9572014-01-25 19:20:35 +0100182/* Allocates a cache entry for a file descriptor if it does not yet have one.
183 * This can be done at any time.
184 */
185static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100186{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100187 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
188 fd_add_to_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd);
189 else
190 fd_add_to_fd_list(&fd_cache, fd);
191 }
192
193static inline void fd_rm_from_fd_list(volatile struct fdlist *list, int fd)
194{
195#if defined(HA_HAVE_CAS_DW) || defined(HA_CAS_IS_8B)
196 volatile struct fdlist_entry cur_list, next_list;
197#endif
198 int old;
199 int new = -2;
Olivier Houchard12568362018-01-31 18:07:29 +0100200 int prev;
201 int next;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100202 int last;
203
204lock_self:
205#if (defined(HA_CAS_IS_8B) || defined(HA_HAVE_CAS_DW))
206 next_list.next = next_list.prev = -2;
Olivier Houchard12568362018-01-31 18:07:29 +0100207 cur_list = fdtab[fd].cache;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100208 /* First, attempt to lock our own entries */
209 do {
210 /* The FD is not in the FD cache, give up */
211 if (unlikely(cur_list.next <= -3))
212 return;
213 if (unlikely(cur_list.prev == -2 || cur_list.next == -2))
214 goto lock_self;
215 } while (
216#ifdef HA_CAS_IS_8B
Olivier Houchard12568362018-01-31 18:07:29 +0100217 unlikely(!HA_ATOMIC_CAS(((void **)(void *)&fdtab[fd].cache.next), ((void **)(void *)&cur_list), (*(void **)(void *)&next_list))))
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100218#else
Olivier Houchard12568362018-01-31 18:07:29 +0100219 unlikely(!__ha_cas_dw((void *)&fdtab[fd].cache.next, (void *)&cur_list, (void *)&next_list)))
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100220#endif
221 ;
222 next = cur_list.next;
223 prev = cur_list.prev;
224
225#else
226lock_self_next:
Olivier Houchard12568362018-01-31 18:07:29 +0100227 next = fdtab[fd].cache.next;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100228 if (next == -2)
229 goto lock_self_next;
230 if (next <= -3)
231 goto done;
Olivier Houchard12568362018-01-31 18:07:29 +0100232 if (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].cache.next, &next, -2)))
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100233 goto lock_self_next;
234lock_self_prev:
Olivier Houchard12568362018-01-31 18:07:29 +0100235 prev = fdtab[fd].cache.prev;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100236 if (prev == -2)
237 goto lock_self_prev;
Olivier Houchard12568362018-01-31 18:07:29 +0100238 if (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].cache.prev, &prev, -2)))
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100239 goto lock_self_prev;
240#endif
241 __ha_barrier_store();
242
243 /* Now, lock the entries of our neighbours */
244 if (likely(prev != -1)) {
245redo_prev:
246 old = fd;
247
Olivier Houchard12568362018-01-31 18:07:29 +0100248 if (unlikely(!HA_ATOMIC_CAS(&fdtab[prev].cache.next, &old, new))) {
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100249 if (unlikely(old == -2)) {
250 /* Neighbour already locked, give up and
251 * retry again once he's done
252 */
Olivier Houchard12568362018-01-31 18:07:29 +0100253 fdtab[fd].cache.prev = prev;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100254 __ha_barrier_store();
Olivier Houchard12568362018-01-31 18:07:29 +0100255 fdtab[fd].cache.next = next;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100256 __ha_barrier_store();
257 goto lock_self;
258 }
259 goto redo_prev;
260 }
261 }
262 if (likely(next != -1)) {
263redo_next:
264 old = fd;
Olivier Houchard12568362018-01-31 18:07:29 +0100265 if (unlikely(!HA_ATOMIC_CAS(&fdtab[next].cache.prev, &old, new))) {
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100266 if (unlikely(old == -2)) {
267 /* Neighbour already locked, give up and
268 * retry again once he's done
269 */
270 if (prev != -1) {
Olivier Houchard12568362018-01-31 18:07:29 +0100271 fdtab[prev].cache.next = fd;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100272 __ha_barrier_store();
273 }
Olivier Houchard12568362018-01-31 18:07:29 +0100274 fdtab[fd].cache.prev = prev;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100275 __ha_barrier_store();
Olivier Houchard12568362018-01-31 18:07:29 +0100276 fdtab[fd].cache.next = next;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100277 __ha_barrier_store();
278 goto lock_self;
279 }
280 goto redo_next;
281 }
282 }
283 if (list->first == fd)
284 list->first = next;
285 __ha_barrier_store();
286 last = list->last;
287 while (unlikely(last == fd && (!HA_ATOMIC_CAS(&list->last, &last, prev))))
288 __ha_compiler_barrier();
289 /* Make sure we let other threads know we're no longer in cache,
290 * before releasing our neighbours.
291 */
292 __ha_barrier_store();
293 if (likely(prev != -1))
Olivier Houchard12568362018-01-31 18:07:29 +0100294 fdtab[prev].cache.next = next;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100295 __ha_barrier_store();
296 if (likely(next != -1))
Olivier Houchard12568362018-01-31 18:07:29 +0100297 fdtab[next].cache.prev = prev;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100298 __ha_barrier_store();
299 /* Ok, now we're out of the fd cache */
Olivier Houchard12568362018-01-31 18:07:29 +0100300 fdtab[fd].cache.next = -(next + 4);
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100301 __ha_barrier_store();
302done:
303 return;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100304}
305
Willy Tarreau899d9572014-01-25 19:20:35 +0100306/* Removes entry used by fd <fd> from the FD cache and replaces it with the
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100307 * last one.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100308 * If the fd has no entry assigned, return immediately.
309 */
Willy Tarreau899d9572014-01-25 19:20:35 +0100310static inline void fd_release_cache_entry(int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100311{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100312 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
313 fd_rm_from_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd);
314 else
315 fd_rm_from_fd_list(&fd_cache, fd);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100316}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200317
Willy Tarreau5be2f352014-11-19 19:43:05 +0100318/* This function automatically enables/disables caching for an entry depending
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100319 * on its state. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100320 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100321static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100322{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100323 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
324 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
325 ((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 +0100326 fd_alloc_cache_entry(fd);
327 }
328 else {
329 fd_release_cache_entry(fd);
330 }
331}
332
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100333/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100334 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100335 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100336static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100337{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100338 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100339}
340
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100341/*
342 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100343 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100344static inline int fd_recv_active(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100345{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100346 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100347}
348
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100349/*
350 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100351 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100352static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100353{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100354 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
355}
356
357/*
358 * returns true if the FD is polled for recv
359 */
360static inline int fd_recv_polled(const int fd)
361{
362 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
363}
364
365/*
366 * returns the FD's send state (FD_EV_*)
367 */
368static inline int fd_send_state(const int fd)
369{
370 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
371}
372
373/*
374 * returns true if the FD is active for send
375 */
376static inline int fd_send_active(const int fd)
377{
378 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100379}
380
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100381/*
382 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100383 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100384static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100385{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100386 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
387}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100388
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100389/*
390 * returns true if the FD is polled for send
391 */
392static inline int fd_send_polled(const int fd)
393{
394 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
395}
396
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200397/*
398 * returns true if the FD is active for recv or send
399 */
400static inline int fd_active(const int fd)
401{
402 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
403}
404
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100405/* Disable processing recv events on fd <fd> */
406static inline void fd_stop_recv(int fd)
407{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100408 unsigned char old, new;
409
410 old = fdtab[fd].state;
411 do {
412 if (!(old & FD_EV_ACTIVE_R))
413 return;
414 new = old & ~FD_EV_ACTIVE_R;
415 new &= ~FD_EV_POLLED_R;
416 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
417
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100418 if ((old ^ new) & FD_EV_POLLED_R)
419 updt_fd_polling(fd);
420
Willy Tarreau4d841862018-01-17 22:57:54 +0100421 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100422 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100423 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100424}
425
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100426/* Disable processing send events on fd <fd> */
427static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100428{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100429 unsigned char old, new;
430
431 old = fdtab[fd].state;
432 do {
433 if (!(old & FD_EV_ACTIVE_W))
434 return;
435 new = old & ~FD_EV_ACTIVE_W;
436 new &= ~FD_EV_POLLED_W;
437 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
438
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100439 if ((old ^ new) & FD_EV_POLLED_W)
440 updt_fd_polling(fd);
441
Willy Tarreau4d841862018-01-17 22:57:54 +0100442 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100443 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100444 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100445}
446
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100447/* Disable processing of events on fd <fd> for both directions. */
448static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200449{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100450 unsigned char old, new;
451
452 old = fdtab[fd].state;
453 do {
454 if (!(old & FD_EV_ACTIVE_RW))
455 return;
456 new = old & ~FD_EV_ACTIVE_RW;
457 new &= ~FD_EV_POLLED_RW;
458 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
459
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100460 if ((old ^ new) & FD_EV_POLLED_RW)
461 updt_fd_polling(fd);
462
Willy Tarreau4d841862018-01-17 22:57:54 +0100463 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100464 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100465 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200466}
467
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100468/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
469static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200470{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100471 unsigned char old, new;
472
473 old = fdtab[fd].state;
474 do {
475 if (!(old & FD_EV_READY_R))
476 return;
477 new = old & ~FD_EV_READY_R;
478 if (new & FD_EV_ACTIVE_R)
479 new |= FD_EV_POLLED_R;
480 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
481
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100482 if ((old ^ new) & FD_EV_POLLED_R)
483 updt_fd_polling(fd);
484
Willy Tarreau4d841862018-01-17 22:57:54 +0100485 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100486 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100487 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200488}
489
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100490/* Report that FD <fd> can receive anymore without polling. */
491static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200492{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100493 /* marking ready never changes polled status */
494 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_R);
495
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100496 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100497 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100498 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200499}
500
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100501/* Disable readiness when polled. This is useful to interrupt reading when it
502 * is suspected that the end of data might have been reached (eg: short read).
503 * This can only be done using level-triggered pollers, so if any edge-triggered
504 * is ever implemented, a test will have to be added here.
505 */
506static inline void fd_done_recv(const int fd)
507{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100508 unsigned char old, new;
509
510 old = fdtab[fd].state;
511 do {
512 if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R))
513 return;
514 new = old & ~FD_EV_READY_R;
515 if (new & FD_EV_ACTIVE_R)
516 new |= FD_EV_POLLED_R;
517 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
518
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100519 if ((old ^ new) & FD_EV_POLLED_R)
520 updt_fd_polling(fd);
521
Willy Tarreau4d841862018-01-17 22:57:54 +0100522 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100523 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100524 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100525}
526
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100527/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
528static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200529{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100530 unsigned char old, new;
531
532 old = fdtab[fd].state;
533 do {
534 if (!(old & FD_EV_READY_W))
535 return;
536 new = old & ~FD_EV_READY_W;
537 if (new & FD_EV_ACTIVE_W)
538 new |= FD_EV_POLLED_W;
539 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
540
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100541 if ((old ^ new) & FD_EV_POLLED_W)
542 updt_fd_polling(fd);
543
Willy Tarreau4d841862018-01-17 22:57:54 +0100544 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100545 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100546 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200547}
548
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100549/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
550static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200551{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100552 /* marking ready never changes polled status */
553 HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_W);
554
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100555 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100556 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100557 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200558}
Willy Tarreau2a429502006-10-15 14:52:29 +0200559
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100560/* Prepare FD <fd> to try to receive */
561static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200562{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100563 unsigned char old, new;
564
565 old = fdtab[fd].state;
566 do {
567 if (old & FD_EV_ACTIVE_R)
568 return;
569 new = old | FD_EV_ACTIVE_R;
570 if (!(new & FD_EV_READY_R))
571 new |= FD_EV_POLLED_R;
572 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
573
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100574 if ((old ^ new) & FD_EV_POLLED_R)
575 updt_fd_polling(fd);
576
Willy Tarreau4d841862018-01-17 22:57:54 +0100577 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100578 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100579 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200580}
581
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100582/* Prepare FD <fd> to try to send */
583static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200584{
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100585 unsigned char old, new;
586
587 old = fdtab[fd].state;
588 do {
589 if (old & FD_EV_ACTIVE_W)
590 return;
591 new = old | FD_EV_ACTIVE_W;
592 if (!(new & FD_EV_READY_W))
593 new |= FD_EV_POLLED_W;
594 } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new)));
595
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100596 if ((old ^ new) & FD_EV_POLLED_W)
597 updt_fd_polling(fd);
598
Willy Tarreau4d841862018-01-17 22:57:54 +0100599 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau7ac0e352018-01-17 21:25:57 +0100600 fd_update_cache(fd); /* need an update entry to change the state */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100601 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200602}
Willy Tarreau2a429502006-10-15 14:52:29 +0200603
Christopher Faulet21e92672017-08-30 10:30:04 +0200604/* Update events seen for FD <fd> and its state if needed. This should be called
605 * by the poller to set FD_POLL_* flags. */
606static inline void fd_update_events(int fd, int evts)
607{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100608 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200609 fdtab[fd].ev &= FD_POLL_STICKY;
610 fdtab[fd].ev |= evts;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100611 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200612
613 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
614 fd_may_recv(fd);
615
616 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
617 fd_may_send(fd);
618}
619
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100620/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100621static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200622{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100623 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100624 fdtab[fd].owner = owner;
625 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100626 fdtab[fd].ev = 0;
Willy Tarreauebc78d72018-01-20 23:53:50 +0100627 fdtab[fd].update_mask &= ~tid_bit;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100628 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200629 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100630 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100631 /* note: do not reset polled_mask here as it indicates which poller
632 * still knows this FD from a possible previous round.
633 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100634 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635}
636
Willy Tarreau322e6c72018-01-25 16:37:04 +0100637/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
638static inline void hap_fd_set(int fd, unsigned int *evts)
639{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100640 HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100641}
642
643static inline void hap_fd_clr(int fd, unsigned int *evts)
644{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100645 HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100646}
647
648static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
649{
650 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
651}
652
Willy Tarreaubaaee002006-06-26 02:48:02 +0200653
654#endif /* _PROTO_FD_H */
655
656/*
657 * Local variables:
658 * c-indent-level: 8
659 * c-basic-offset: 8
660 * End:
661 */