blob: 595af90f2b7ed3d6f93319cb5c36da5913612cfb [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 Tarreauebc78d72018-01-20 23:53:50 +0100101 if (fdtab[fd].update_mask & tid_bit)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100102 /* already scheduled for update */
103 return;
Willy Tarreauebc78d72018-01-20 23:53:50 +0100104 fdtab[fd].update_mask |= tid_bit;
Willy Tarreau4a291442012-12-13 23:34:18 +0100105 fd_updt[fd_nbupdt++] = fd;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100106}
107
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100108
109#define _GET_NEXT(fd) fdtab[fd].fdcache_entry.next
110#define _GET_PREV(fd) fdtab[fd].fdcache_entry.prev
111
112static 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:
120 next = _GET_NEXT(fd);
121 /*
122 * Check that we're not already in the cache, and if not, lock us.
123 * <= -3 means not in the cache, -2 means locked, -1 means we're
124 * in the cache, and the last element, >= 0 gives the FD of the next
125 * in the cache.
126 */
127 if (next >= -2)
128 goto done;
129 if (!HA_ATOMIC_CAS(&_GET_NEXT(fd), &next, -2))
130 goto redo_next;
131 __ha_barrier_store();
132redo_last:
133 /* First, insert in the linked list */
134 last = list->last;
135 old = -1;
136 new = fd;
137 if (unlikely(last == -1)) {
138 /* list is empty, try to add ourselves alone so that list->last=fd */
139
140 _GET_PREV(fd) = last;
141
142 /* Make sure the "prev" store is visible before we update the last entry */
143 __ha_barrier_store();
144 if (unlikely(!HA_ATOMIC_CAS(&list->last, &old, new)))
145 goto redo_last;
146
147 /* list->first was necessary -1, we're guaranteed to be alone here */
148 list->first = fd;
149
150 /* since we're alone at the end of the list and still locked(-2),
151 * we know noone tried to add past us. Mark the end of list.
152 */
153 _GET_NEXT(fd) = -1;
154 goto done; /* We're done ! */
155 } else {
156 /* non-empty list, add past the tail */
157 do {
158 new = fd;
159 old = -1;
160 _GET_PREV(fd) = last;
161
162 __ha_barrier_store();
163
164 /* adding ourselves past the last element
165 * The CAS will only succeed if its next is -1,
166 * which means it's in the cache, and the last element.
167 */
168 if (likely(HA_ATOMIC_CAS(&_GET_NEXT(last), &old, new)))
169 break;
170 goto redo_last;
171 } while (1);
172 }
173 /* Then, update the last entry */
174redo_fd_cache:
175 last = list->last;
176 __ha_barrier_load();
177
178 if (unlikely(!HA_ATOMIC_CAS(&list->last, &last, fd)))
179 goto redo_fd_cache;
180 __ha_barrier_store();
181 _GET_NEXT(fd) = -1;
182 __ha_barrier_store();
183done:
184 return;
185}
Willy Tarreau7be79a42012-11-11 15:02:54 +0100186
Willy Tarreau899d9572014-01-25 19:20:35 +0100187/* Allocates a cache entry for a file descriptor if it does not yet have one.
188 * This can be done at any time.
189 */
190static inline void fd_alloc_cache_entry(const int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100191{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100192 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
193 fd_add_to_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd);
194 else
195 fd_add_to_fd_list(&fd_cache, fd);
196 }
197
198static inline void fd_rm_from_fd_list(volatile struct fdlist *list, int fd)
199{
200#if defined(HA_HAVE_CAS_DW) || defined(HA_CAS_IS_8B)
201 volatile struct fdlist_entry cur_list, next_list;
202#endif
203 int old;
204 int new = -2;
205 volatile int prev;
206 volatile int next;
207 int last;
208
209lock_self:
210#if (defined(HA_CAS_IS_8B) || defined(HA_HAVE_CAS_DW))
211 next_list.next = next_list.prev = -2;
212 cur_list.prev = _GET_PREV(fd);
213 cur_list.next = _GET_NEXT(fd);
214 /* First, attempt to lock our own entries */
215 do {
216 /* The FD is not in the FD cache, give up */
217 if (unlikely(cur_list.next <= -3))
218 return;
219 if (unlikely(cur_list.prev == -2 || cur_list.next == -2))
220 goto lock_self;
221 } while (
222#ifdef HA_CAS_IS_8B
223 unlikely(!HA_ATOMIC_CAS(((void **)(void *)&_GET_NEXT(fd)), ((void **)(void *)&cur_list), (*(void **)(void *)&next_list))))
224#else
225 unlikely(!__ha_cas_dw((void *)&_GET_NEXT(fd), (void *)&cur_list, (void *)&next_list)))
226#endif
227 ;
228 next = cur_list.next;
229 prev = cur_list.prev;
230
231#else
232lock_self_next:
233 next = _GET_NEXT(fd);
234 if (next == -2)
235 goto lock_self_next;
236 if (next <= -3)
237 goto done;
238 if (unlikely(!HA_ATOMIC_CAS(&_GET_NEXT(fd), &next, -2)))
239 goto lock_self_next;
240lock_self_prev:
241 prev = _GET_PREV(fd);
242 if (prev == -2)
243 goto lock_self_prev;
244 if (unlikely(!HA_ATOMIC_CAS(&_GET_PREV(fd), &prev, -2)))
245 goto lock_self_prev;
246#endif
247 __ha_barrier_store();
248
249 /* Now, lock the entries of our neighbours */
250 if (likely(prev != -1)) {
251redo_prev:
252 old = fd;
253
254 if (unlikely(!HA_ATOMIC_CAS(&_GET_NEXT(prev), &old, new))) {
255 if (unlikely(old == -2)) {
256 /* Neighbour already locked, give up and
257 * retry again once he's done
258 */
259 _GET_PREV(fd) = prev;
260 __ha_barrier_store();
261 _GET_NEXT(fd) = next;
262 __ha_barrier_store();
263 goto lock_self;
264 }
265 goto redo_prev;
266 }
267 }
268 if (likely(next != -1)) {
269redo_next:
270 old = fd;
271 if (unlikely(!HA_ATOMIC_CAS(&_GET_PREV(next), &old, new))) {
272 if (unlikely(old == -2)) {
273 /* Neighbour already locked, give up and
274 * retry again once he's done
275 */
276 if (prev != -1) {
277 _GET_NEXT(prev) = fd;
278 __ha_barrier_store();
279 }
280 _GET_PREV(fd) = prev;
281 __ha_barrier_store();
282 _GET_NEXT(fd) = next;
283 __ha_barrier_store();
284 goto lock_self;
285 }
286 goto redo_next;
287 }
288 }
289 if (list->first == fd)
290 list->first = next;
291 __ha_barrier_store();
292 last = list->last;
293 while (unlikely(last == fd && (!HA_ATOMIC_CAS(&list->last, &last, prev))))
294 __ha_compiler_barrier();
295 /* Make sure we let other threads know we're no longer in cache,
296 * before releasing our neighbours.
297 */
298 __ha_barrier_store();
299 if (likely(prev != -1))
300 _GET_NEXT(prev) = next;
301 __ha_barrier_store();
302 if (likely(next != -1))
303 _GET_PREV(next) = prev;
304 __ha_barrier_store();
305 /* Ok, now we're out of the fd cache */
306 _GET_NEXT(fd) = -(next + 4);
307 __ha_barrier_store();
308done:
309 return;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100310}
311
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100312#undef _GET_NEXT
313#undef _GET_PREV
314
315
Willy Tarreau899d9572014-01-25 19:20:35 +0100316/* Removes entry used by fd <fd> from the FD cache and replaces it with the
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100317 * last one.
Willy Tarreau7be79a42012-11-11 15:02:54 +0100318 * If the fd has no entry assigned, return immediately.
319 */
Willy Tarreau899d9572014-01-25 19:20:35 +0100320static inline void fd_release_cache_entry(int fd)
Willy Tarreau7be79a42012-11-11 15:02:54 +0100321{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100322 if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1)))
323 fd_rm_from_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd);
324 else
325 fd_rm_from_fd_list(&fd_cache, fd);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100326}
Willy Tarreau49b046d2012-08-09 12:11:58 +0200327
Willy Tarreau25002d22014-01-25 10:32:56 +0100328/* Computes the new polled status based on the active and ready statuses, for
329 * each direction. This is meant to be used by pollers while processing updates.
330 */
331static inline int fd_compute_new_polled_status(int state)
332{
333 if (state & FD_EV_ACTIVE_R) {
334 if (!(state & FD_EV_READY_R))
335 state |= FD_EV_POLLED_R;
336 }
337 else
338 state &= ~FD_EV_POLLED_R;
339
340 if (state & FD_EV_ACTIVE_W) {
341 if (!(state & FD_EV_READY_W))
342 state |= FD_EV_POLLED_W;
343 }
344 else
345 state &= ~FD_EV_POLLED_W;
346
347 return state;
348}
349
Willy Tarreau5be2f352014-11-19 19:43:05 +0100350/* This function automatically enables/disables caching for an entry depending
351 * on its state, and also possibly creates an update entry so that the poller
352 * does its job as well. It is only called on state changes.
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100353 */
Willy Tarreau5be2f352014-11-19 19:43:05 +0100354static inline void fd_update_cache(int fd)
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100355{
Willy Tarreau5be2f352014-11-19 19:43:05 +0100356 /* 3 states for each direction require a polling update */
357 if ((fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_ACTIVE_R)) == FD_EV_POLLED_R ||
358 (fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_READY_R | FD_EV_ACTIVE_R)) == FD_EV_ACTIVE_R ||
359 (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_ACTIVE_W)) == FD_EV_POLLED_W ||
360 (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_READY_W | FD_EV_ACTIVE_W)) == FD_EV_ACTIVE_W)
361 updt_fd_polling(fd);
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100362
Willy Tarreau5be2f352014-11-19 19:43:05 +0100363 /* only READY and ACTIVE states (the two with both flags set) require a cache entry */
364 if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
365 ((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 +0100366 fd_alloc_cache_entry(fd);
367 }
368 else {
369 fd_release_cache_entry(fd);
370 }
371}
372
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100373/*
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100374 * returns the FD's recv state (FD_EV_*)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100375 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100376static inline int fd_recv_state(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100377{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100378 return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100379}
380
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100381/*
382 * returns true if the FD is active for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100383 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100384static inline int fd_recv_active(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_ACTIVE_R;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100387}
388
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100389/*
390 * returns true if the FD is ready for recv
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100391 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100392static inline int fd_recv_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100393{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100394 return (unsigned)fdtab[fd].state & FD_EV_READY_R;
395}
396
397/*
398 * returns true if the FD is polled for recv
399 */
400static inline int fd_recv_polled(const int fd)
401{
402 return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
403}
404
405/*
406 * returns the FD's send state (FD_EV_*)
407 */
408static inline int fd_send_state(const int fd)
409{
410 return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
411}
412
413/*
414 * returns true if the FD is active for send
415 */
416static inline int fd_send_active(const int fd)
417{
418 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100419}
420
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100421/*
422 * returns true if the FD is ready for send
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100423 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100424static inline int fd_send_ready(const int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100425{
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100426 return (unsigned)fdtab[fd].state & FD_EV_READY_W;
427}
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100428
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100429/*
430 * returns true if the FD is polled for send
431 */
432static inline int fd_send_polled(const int fd)
433{
434 return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
435}
436
Christopher Faulet8db2fdf2017-08-30 09:59:38 +0200437/*
438 * returns true if the FD is active for recv or send
439 */
440static inline int fd_active(const int fd)
441{
442 return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW;
443}
444
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100445/* Disable processing recv events on fd <fd> */
446static inline void fd_stop_recv(int fd)
447{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100448 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200449 if (fd_recv_active(fd)) {
450 fdtab[fd].state &= ~FD_EV_ACTIVE_R;
451 fd_update_cache(fd); /* need an update entry to change the state */
452 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100453 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100454}
455
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100456/* Disable processing send events on fd <fd> */
457static inline void fd_stop_send(int fd)
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100458{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100459 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200460 if (fd_send_active(fd)) {
461 fdtab[fd].state &= ~FD_EV_ACTIVE_W;
462 fd_update_cache(fd); /* need an update entry to change the state */
463 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100464 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100465}
466
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100467/* Disable processing of events on fd <fd> for both directions. */
468static inline void fd_stop_both(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200469{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100470 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200471 if (fd_active(fd)) {
472 fdtab[fd].state &= ~FD_EV_ACTIVE_RW;
473 fd_update_cache(fd); /* need an update entry to change the state */
474 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100475 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200476}
477
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100478/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
479static inline void fd_cant_recv(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200480{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100481 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200482 if (fd_recv_ready(fd)) {
483 fdtab[fd].state &= ~FD_EV_READY_R;
484 fd_update_cache(fd); /* need an update entry to change the state */
485 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100486 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200487}
488
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100489/* Report that FD <fd> can receive anymore without polling. */
490static inline void fd_may_recv(const int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200491{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100492 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200493 if (!fd_recv_ready(fd)) {
494 fdtab[fd].state |= FD_EV_READY_R;
495 fd_update_cache(fd); /* need an update entry to change the state */
496 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100497 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200498}
499
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100500/* Disable readiness when polled. This is useful to interrupt reading when it
501 * is suspected that the end of data might have been reached (eg: short read).
502 * This can only be done using level-triggered pollers, so if any edge-triggered
503 * is ever implemented, a test will have to be added here.
504 */
505static inline void fd_done_recv(const int fd)
506{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100507 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200508 if (fd_recv_polled(fd) && fd_recv_ready(fd)) {
509 fdtab[fd].state &= ~FD_EV_READY_R;
510 fd_update_cache(fd); /* need an update entry to change the state */
511 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100512 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau6c11bd22014-01-24 00:54:27 +0100513}
514
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100515/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
516static inline void fd_cant_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200517{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100518 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200519 if (fd_send_ready(fd)) {
520 fdtab[fd].state &= ~FD_EV_READY_W;
521 fd_update_cache(fd); /* need an update entry to change the state */
522 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100523 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200524}
525
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100526/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
527static inline void fd_may_send(const int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200528{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100529 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200530 if (!fd_send_ready(fd)) {
531 fdtab[fd].state |= FD_EV_READY_W;
532 fd_update_cache(fd); /* need an update entry to change the state */
533 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100534 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200535}
Willy Tarreau2a429502006-10-15 14:52:29 +0200536
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100537/* Prepare FD <fd> to try to receive */
538static inline void fd_want_recv(int fd)
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200539{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100540 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200541 if (!fd_recv_active(fd)) {
542 fdtab[fd].state |= FD_EV_ACTIVE_R;
543 fd_update_cache(fd); /* need an update entry to change the state */
544 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100545 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200546}
547
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100548/* Prepare FD <fd> to try to send */
549static inline void fd_want_send(int fd)
Willy Tarreau49b046d2012-08-09 12:11:58 +0200550{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100551 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd82b1802017-08-30 10:07:47 +0200552 if (!fd_send_active(fd)) {
553 fdtab[fd].state |= FD_EV_ACTIVE_W;
554 fd_update_cache(fd); /* need an update entry to change the state */
555 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100556 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau49b046d2012-08-09 12:11:58 +0200557}
Willy Tarreau2a429502006-10-15 14:52:29 +0200558
Christopher Faulet21e92672017-08-30 10:30:04 +0200559/* Update events seen for FD <fd> and its state if needed. This should be called
560 * by the poller to set FD_POLL_* flags. */
561static inline void fd_update_events(int fd, int evts)
562{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100563 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200564 fdtab[fd].ev &= FD_POLL_STICKY;
565 fdtab[fd].ev |= evts;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100566 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Faulet21e92672017-08-30 10:30:04 +0200567
568 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
569 fd_may_recv(fd);
570
571 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
572 fd_may_send(fd);
573}
574
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100575/* Prepares <fd> for being polled */
Willy Tarreaua9786b62018-01-25 07:22:13 +0100576static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200577{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100578 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100579 fdtab[fd].owner = owner;
580 fdtab[fd].iocb = iocb;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100581 fdtab[fd].ev = 0;
Willy Tarreauebc78d72018-01-20 23:53:50 +0100582 fdtab[fd].update_mask &= ~tid_bit;
Willy Tarreauad38ace2013-12-15 14:19:38 +0100583 fdtab[fd].linger_risk = 0;
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200584 fdtab[fd].cloned = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100585 fdtab[fd].thread_mask = thread_mask;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100586 /* note: do not reset polled_mask here as it indicates which poller
587 * still knows this FD from a possible previous round.
588 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100589 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200590}
591
Willy Tarreau322e6c72018-01-25 16:37:04 +0100592/* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */
593static inline void hap_fd_set(int fd, unsigned int *evts)
594{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100595 HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100596}
597
598static inline void hap_fd_clr(int fd, unsigned int *evts)
599{
Willy Tarreau82b37d72018-01-25 16:59:09 +0100600 HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1))));
Willy Tarreau322e6c72018-01-25 16:37:04 +0100601}
602
603static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
604{
605 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
606}
607
Willy Tarreaubaaee002006-06-26 02:48:02 +0200608
609#endif /* _PROTO_FD_H */
610
611/*
612 * Local variables:
613 * c-indent-level: 8
614 * c-basic-offset: 8
615 * End:
616 */