blob: c4155aa86fc9153fff81d14f7fc25924e8970e65 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * File descriptors management functions.
3 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +01004 * Copyright 2000-2014 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
Willy Tarreau7be79a42012-11-11 15:02:54 +010011 * There is no direct link between the FD and the updates list. There is only a
12 * bit in the fdtab[] to indicate than a file descriptor is already present in
13 * the updates list. Once an fd is present in the updates list, it will have to
14 * be considered even if its changes are reverted in the middle or if the fd is
15 * replaced.
16 *
17 * It is important to understand that as long as all expected events are
18 * processed, they might starve the polled events, especially because polled
Willy Tarreauf817e9f2014-01-10 16:58:45 +010019 * I/O starvation quickly induces more cached I/O. One solution to this
Willy Tarreau7be79a42012-11-11 15:02:54 +010020 * consists in only processing a part of the events at once, but one drawback
Willy Tarreauf817e9f2014-01-10 16:58:45 +010021 * is that unhandled events will still wake the poller up. Using an edge-
22 * triggered poller such as EPOLL_ET will solve this issue though.
Willy Tarreau7be79a42012-11-11 15:02:54 +010023 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010024 * The event state for an FD, as found in fdtab[].state, is maintained for each
25 * direction. The state field is built this way, with R bits in the low nibble
26 * and W bits in the high nibble for ease of access and debugging :
Willy Tarreau7be79a42012-11-11 15:02:54 +010027 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010028 * 7 6 5 4 3 2 1 0
29 * [ 0 | PW | RW | AW | 0 | PR | RR | AR ]
30 *
31 * A* = active *R = read
32 * P* = polled *W = write
33 * R* = ready
34 *
35 * An FD is marked "active" when there is a desire to use it.
36 * An FD is marked "polled" when it is registered in the polling.
37 * An FD is marked "ready" when it has not faced a new EAGAIN since last wake-up
38 * (it is a cache of the last EAGAIN regardless of polling changes).
Willy Tarreau7be79a42012-11-11 15:02:54 +010039 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010040 * We have 8 possible states for each direction based on these 3 flags :
Willy Tarreau7be79a42012-11-11 15:02:54 +010041 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010042 * +---+---+---+----------+---------------------------------------------+
43 * | P | R | A | State | Description |
44 * +---+---+---+----------+---------------------------------------------+
45 * | 0 | 0 | 0 | DISABLED | No activity desired, not ready. |
46 * | 0 | 0 | 1 | MUSTPOLL | Activity desired via polling. |
47 * | 0 | 1 | 0 | STOPPED | End of activity without polling. |
48 * | 0 | 1 | 1 | ACTIVE | Activity desired without polling. |
49 * | 1 | 0 | 0 | ABORT | Aborted poll(). Not frequently seen. |
50 * | 1 | 0 | 1 | POLLED | FD is being polled. |
51 * | 1 | 1 | 0 | PAUSED | FD was paused while ready (eg: buffer full) |
52 * | 1 | 1 | 1 | READY | FD was marked ready by poll() |
53 * +---+---+---+----------+---------------------------------------------+
Willy Tarreau7be79a42012-11-11 15:02:54 +010054 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010055 * The transitions are pretty simple :
56 * - fd_want_*() : set flag A
57 * - fd_stop_*() : clear flag A
58 * - fd_cant_*() : clear flag R (when facing EAGAIN)
59 * - fd_may_*() : set flag R (upon return from poll())
60 * - sync() : if (A) { if (!R) P := 1 } else { P := 0 }
61 *
62 * The PAUSED, ABORT and MUSTPOLL states are transient for level-trigerred
63 * pollers and are fixed by the sync() which happens at the beginning of the
64 * poller. For event-triggered pollers, only the MUSTPOLL state will be
65 * transient and ABORT will lead to PAUSED. The ACTIVE state is the only stable
66 * one which has P != A.
67 *
68 * The READY state is a bit special as activity on the FD might be notified
69 * both by the poller or by the cache. But it is needed for some multi-layer
70 * protocols (eg: SSL) where connection activity is not 100% linked to FD
71 * activity. Also some pollers might prefer to implement it as ACTIVE if
72 * enabling/disabling the FD is cheap. The READY and ACTIVE states are the
73 * two states for which a cache entry is allocated.
74 *
75 * The state transitions look like the diagram below. Only the 4 right states
76 * have polling enabled :
77 *
78 * (POLLED=0) (POLLED=1)
79 *
80 * +----------+ sync +-------+
81 * | DISABLED | <----- | ABORT | (READY=0, ACTIVE=0)
82 * +----------+ +-------+
83 * clr | ^ set | ^
84 * | | | |
85 * v | set v | clr
86 * +----------+ sync +--------+
87 * | MUSTPOLL | -----> | POLLED | (READY=0, ACTIVE=1)
88 * +----------+ +--------+
89 * ^ poll | ^
90 * | | |
91 * | EAGAIN v | EAGAIN
92 * +--------+ +-------+
93 * | ACTIVE | | READY | (READY=1, ACTIVE=1)
94 * +--------+ +-------+
95 * clr | ^ set | ^
96 * | | | |
97 * v | set v | clr
98 * +---------+ sync +--------+
99 * | STOPPED | <------ | PAUSED | (READY=1, ACTIVE=0)
100 * +---------+ +--------+
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 */
102
Willy Tarreau2ff76222007-04-09 19:29:56 +0200103#include <stdio.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +0200104#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105#include <unistd.h>
Olivier Houchard79321b92018-07-26 17:55:11 +0200106#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107#include <sys/types.h>
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100108#include <sys/resource.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109
Willy Tarreaue5733232019-05-22 19:24:06 +0200110#if defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100111#include <poll.h>
112#include <errno.h>
113#endif
114
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200115#include <common/compat.h>
116#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117
Willy Tarreau7be79a42012-11-11 15:02:54 +0100118#include <types/global.h>
119
Willy Tarreau2a429502006-10-15 14:52:29 +0200120#include <proto/fd.h>
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200121#include <proto/log.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200122#include <proto/port_range.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123
124struct fdtab *fdtab = NULL; /* array of all the file descriptors */
Olivier Houchard53055052019-07-25 14:00:18 +0000125struct polled_mask *polled_mask = NULL; /* Array for the polled_mask of each fd */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200126struct fdinfo *fdinfo = NULL; /* less-often used infos for file descriptors */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127int totalconn; /* total # of terminated sessions */
128int actconn; /* # of active sessions */
129
Willy Tarreau4f60f162007-04-08 16:39:58 +0200130struct poller pollers[MAX_POLLERS];
131struct poller cur_poller;
132int nbpollers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133
Olivier Houchard6b96f722018-04-25 16:58:25 +0200134volatile struct fdlist update_list; // Global update list
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100135
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200136THREAD_LOCAL int *fd_updt = NULL; // FD updates list
137THREAD_LOCAL int fd_nbupdt = 0; // number of updates in the list
Olivier Houchard79321b92018-07-26 17:55:11 +0200138THREAD_LOCAL int poller_rd_pipe = -1; // Pipe to wake the thread
139int poller_wr_pipe[MAX_THREADS]; // Pipe to wake the threads
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200140
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200141volatile int ha_used_fds = 0; // Number of FD we're currently using
142
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200143#define _GET_NEXT(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->next
144#define _GET_PREV(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->prev
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100145/* adds fd <fd> to fd list <list> if it was not yet in it */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200146void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off)
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100147{
148 int next;
149 int new;
150 int old;
151 int last;
152
153redo_next:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200154 next = _GET_NEXT(fd, off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100155 /* Check that we're not already in the cache, and if not, lock us. */
156 if (next >= -2)
157 goto done;
Olivier Houchardd3608792019-03-08 18:47:42 +0100158 if (!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100159 goto redo_next;
Olivier Houchardd2b5d162019-03-08 13:47:21 +0100160 __ha_barrier_atomic_store();
Willy Tarreau11559a72018-02-05 17:52:24 +0100161
162 new = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100163redo_last:
164 /* First, insert in the linked list */
165 last = list->last;
166 old = -1;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100167
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200168 _GET_PREV(fd, off) = -2;
Willy Tarreau11559a72018-02-05 17:52:24 +0100169 /* Make sure the "prev" store is visible before we update the last entry */
170 __ha_barrier_store();
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100171
Willy Tarreau11559a72018-02-05 17:52:24 +0100172 if (unlikely(last == -1)) {
173 /* list is empty, try to add ourselves alone so that list->last=fd */
Olivier Houchardd3608792019-03-08 18:47:42 +0100174 if (unlikely(!_HA_ATOMIC_CAS(&list->last, &old, new)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100175 goto redo_last;
176
177 /* list->first was necessary -1, we're guaranteed to be alone here */
178 list->first = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100179 } else {
Willy Tarreau11559a72018-02-05 17:52:24 +0100180 /* adding ourselves past the last element
181 * The CAS will only succeed if its next is -1,
182 * which means it's in the cache, and the last element.
183 */
Olivier Houchardd3608792019-03-08 18:47:42 +0100184 if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(last, off), &old, new)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100185 goto redo_last;
Willy Tarreau11559a72018-02-05 17:52:24 +0100186
187 /* Then, update the last entry */
188 list->last = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100189 }
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100190 __ha_barrier_store();
Willy Tarreau11559a72018-02-05 17:52:24 +0100191 /* since we're alone at the end of the list and still locked(-2),
192 * we know noone tried to add past us. Mark the end of list.
193 */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200194 _GET_PREV(fd, off) = last;
195 _GET_NEXT(fd, off) = -1;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100196 __ha_barrier_store();
197done:
198 return;
199}
200
201/* removes fd <fd> from fd list <list> */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200202void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off)
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100203{
204#if defined(HA_HAVE_CAS_DW) || defined(HA_CAS_IS_8B)
205 volatile struct fdlist_entry cur_list, next_list;
206#endif
207 int old;
208 int new = -2;
209 int prev;
210 int next;
211 int last;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100212lock_self:
213#if (defined(HA_CAS_IS_8B) || defined(HA_HAVE_CAS_DW))
214 next_list.next = next_list.prev = -2;
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200215 cur_list = *(volatile struct fdlist_entry *)(((char *)&fdtab[fd]) + off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100216 /* First, attempt to lock our own entries */
217 do {
218 /* The FD is not in the FD cache, give up */
219 if (unlikely(cur_list.next <= -3))
220 return;
221 if (unlikely(cur_list.prev == -2 || cur_list.next == -2))
222 goto lock_self;
223 } while (
224#ifdef HA_CAS_IS_8B
Olivier Houchardd3608792019-03-08 18:47:42 +0100225 unlikely(!_HA_ATOMIC_CAS(((void **)(void *)&_GET_NEXT(fd, off)), ((void **)(void *)&cur_list), (*(void **)(void *)&next_list))))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100226#else
Willy Tarreauc3b59582019-05-27 17:37:20 +0200227 unlikely(!_HA_ATOMIC_DWCAS(((void *)&_GET_NEXT(fd, off)), ((void *)&cur_list), ((void *)&next_list))))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100228#endif
229 ;
230 next = cur_list.next;
231 prev = cur_list.prev;
232
233#else
234lock_self_next:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200235 next = ({ volatile int *next = &_GET_NEXT(fd, off); *next; });
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100236 if (next == -2)
237 goto lock_self_next;
238 if (next <= -3)
239 goto done;
Olivier Houchardd3608792019-03-08 18:47:42 +0100240 if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100241 goto lock_self_next;
242lock_self_prev:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200243 prev = ({ volatile int *prev = &_GET_PREV(fd, off); *prev; });
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100244 if (prev == -2)
245 goto lock_self_prev;
Olivier Houchardd3608792019-03-08 18:47:42 +0100246 if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(fd, off), &prev, -2)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100247 goto lock_self_prev;
248#endif
Olivier Houchardd2b5d162019-03-08 13:47:21 +0100249 __ha_barrier_atomic_store();
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100250
251 /* Now, lock the entries of our neighbours */
252 if (likely(prev != -1)) {
253redo_prev:
254 old = fd;
255
Olivier Houchardd3608792019-03-08 18:47:42 +0100256 if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(prev, off), &old, new))) {
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100257 if (unlikely(old == -2)) {
258 /* Neighbour already locked, give up and
259 * retry again once he's done
260 */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200261 _GET_PREV(fd, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100262 __ha_barrier_store();
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200263 _GET_NEXT(fd, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100264 __ha_barrier_store();
265 goto lock_self;
266 }
267 goto redo_prev;
268 }
269 }
270 if (likely(next != -1)) {
271redo_next:
272 old = fd;
Olivier Houchardd3608792019-03-08 18:47:42 +0100273 if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(next, off), &old, new))) {
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100274 if (unlikely(old == -2)) {
275 /* Neighbour already locked, give up and
276 * retry again once he's done
277 */
278 if (prev != -1) {
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200279 _GET_NEXT(prev, off) = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100280 __ha_barrier_store();
281 }
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200282 _GET_PREV(fd, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100283 __ha_barrier_store();
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200284 _GET_NEXT(fd, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100285 __ha_barrier_store();
286 goto lock_self;
287 }
288 goto redo_next;
289 }
290 }
291 if (list->first == fd)
292 list->first = next;
293 __ha_barrier_store();
294 last = list->last;
Olivier Houchardd3608792019-03-08 18:47:42 +0100295 while (unlikely(last == fd && (!_HA_ATOMIC_CAS(&list->last, &last, prev))))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100296 __ha_compiler_barrier();
297 /* Make sure we let other threads know we're no longer in cache,
298 * before releasing our neighbours.
299 */
300 __ha_barrier_store();
301 if (likely(prev != -1))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200302 _GET_NEXT(prev, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100303 __ha_barrier_store();
304 if (likely(next != -1))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200305 _GET_PREV(next, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100306 __ha_barrier_store();
307 /* Ok, now we're out of the fd cache */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200308 _GET_NEXT(fd, off) = -(next + 4);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100309 __ha_barrier_store();
310done:
311 return;
312}
313
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200314#undef _GET_NEXT
315#undef _GET_PREV
316
Willy Tarreau173d9952018-01-26 21:48:23 +0100317/* Deletes an FD from the fdsets.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200318 * The file descriptor is also closed.
319 */
Olivier Houchard1fc05162017-04-06 01:05:05 +0200320static void fd_dodelete(int fd, int do_close)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321{
Willy Tarreau87d54a92018-10-15 09:44:46 +0200322 unsigned long locked = atleast2(fdtab[fd].thread_mask);
323
324 if (locked)
325 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreauad38ace2013-12-15 14:19:38 +0100326 if (fdtab[fd].linger_risk) {
327 /* this is generally set when connecting to servers */
328 setsockopt(fd, SOL_SOCKET, SO_LINGER,
329 (struct linger *) &nolinger, sizeof(struct linger));
330 }
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100331 if (cur_poller.clo)
332 cur_poller.clo(fd);
Olivier Houchardc22580c2019-08-05 18:51:52 +0200333 polled_mask[fd].poll_recv = polled_mask[fd].poll_send = 0;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100334
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100335 fdtab[fd].state = 0;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100336
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200337 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
338 fdinfo[fd].port_range = NULL;
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200339 fdtab[fd].owner = NULL;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100340 fdtab[fd].thread_mask = 0;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100341 if (do_close) {
Christopher Fauletd531f882017-06-01 16:55:03 +0200342 close(fd);
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200343 _HA_ATOMIC_SUB(&ha_used_fds, 1);
Willy Tarreauc9c83782018-01-17 18:44:46 +0100344 }
Willy Tarreau87d54a92018-10-15 09:44:46 +0200345 if (locked)
346 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200347}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348
Willy Tarreau173d9952018-01-26 21:48:23 +0100349/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200350 * The file descriptor is also closed.
351 */
352void fd_delete(int fd)
353{
354 fd_dodelete(fd, 1);
355}
356
Willy Tarreau173d9952018-01-26 21:48:23 +0100357/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200358 * The file descriptor is kept open.
359 */
360void fd_remove(int fd)
361{
362 fd_dodelete(fd, 0);
363}
364
Olivier Houchard2292edf2019-02-25 14:26:54 +0100365#if defined(USE_CLOSEFROM)
366void my_closefrom(int start)
367{
368 closefrom(start);
369}
370
Willy Tarreaue5733232019-05-22 19:24:06 +0200371#elif defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100372/* This is a portable implementation of closefrom(). It closes all open file
373 * descriptors starting at <start> and above. It relies on the fact that poll()
374 * will return POLLNVAL for each invalid (hence close) file descriptor passed
375 * in argument in order to skip them. It acts with batches of FDs and will
376 * typically perform one poll() call per 1024 FDs so the overhead is low in
377 * case all FDs have to be closed.
378 */
379void my_closefrom(int start)
380{
381 struct pollfd poll_events[1024];
382 struct rlimit limit;
383 int nbfds, fd, ret, idx;
384 int step, next;
385
386 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
387 step = nbfds = limit.rlim_cur;
388 else
389 step = nbfds = 0;
390
391 if (nbfds <= 0) {
392 /* set safe limit */
393 nbfds = 1024;
394 step = 256;
395 }
396
397 if (step > sizeof(poll_events) / sizeof(poll_events[0]))
398 step = sizeof(poll_events) / sizeof(poll_events[0]);
399
400 while (start < nbfds) {
401 next = (start / step + 1) * step;
402
403 for (fd = start; fd < next && fd < nbfds; fd++) {
404 poll_events[fd - start].fd = fd;
405 poll_events[fd - start].events = 0;
406 }
407
408 do {
409 ret = poll(poll_events, fd - start, 0);
410 if (ret >= 0)
411 break;
412 } while (errno == EAGAIN || errno == EINTR || errno == ENOMEM);
413
Willy Tarreaub8e602c2019-02-22 09:07:42 +0100414 if (ret)
415 ret = fd - start;
416
Willy Tarreau9188ac62019-02-21 22:12:47 +0100417 for (idx = 0; idx < ret; idx++) {
418 if (poll_events[idx].revents & POLLNVAL)
419 continue; /* already closed */
420
421 fd = poll_events[idx].fd;
422 close(fd);
423 }
424 start = next;
425 }
426}
427
Willy Tarreaue5733232019-05-22 19:24:06 +0200428#else // defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100429
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100430/* This is a portable implementation of closefrom(). It closes all open file
431 * descriptors starting at <start> and above. This is a naive version for use
432 * when the operating system provides no alternative.
433 */
434void my_closefrom(int start)
435{
436 struct rlimit limit;
437 int nbfds;
438
439 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
440 nbfds = limit.rlim_cur;
441 else
442 nbfds = 0;
443
444 if (nbfds <= 0)
445 nbfds = 1024; /* safe limit */
446
447 while (start < nbfds)
448 close(start++);
449}
Willy Tarreaue5733232019-05-22 19:24:06 +0200450#endif // defined(USE_POLL)
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100451
Willy Tarreau4f60f162007-04-08 16:39:58 +0200452/* disable the specified poller */
453void disable_poller(const char *poller_name)
454{
455 int p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200456
Willy Tarreau4f60f162007-04-08 16:39:58 +0200457 for (p = 0; p < nbpollers; p++)
458 if (strcmp(pollers[p].name, poller_name) == 0)
459 pollers[p].pref = 0;
460}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461
Olivier Houchard79321b92018-07-26 17:55:11 +0200462void poller_pipe_io_handler(int fd)
463{
464 char buf[1024];
465 /* Flush the pipe */
466 while (read(fd, buf, sizeof(buf)) > 0);
467 fd_cant_recv(fd);
468}
469
Willy Tarreau082b6282019-05-22 14:42:12 +0200470/* allocate the per-thread fd_updt thus needs to be called early after
471 * thread creation.
472 */
473static int alloc_pollers_per_thread()
474{
475 fd_updt = calloc(global.maxsock, sizeof(*fd_updt));
476 return fd_updt != NULL;
477}
478
479/* Initialize the pollers per thread.*/
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200480static int init_pollers_per_thread()
481{
Olivier Houchard79321b92018-07-26 17:55:11 +0200482 int mypipe[2];
Willy Tarreau082b6282019-05-22 14:42:12 +0200483
484 if (pipe(mypipe) < 0)
Olivier Houchard79321b92018-07-26 17:55:11 +0200485 return 0;
Willy Tarreau082b6282019-05-22 14:42:12 +0200486
Olivier Houchard79321b92018-07-26 17:55:11 +0200487 poller_rd_pipe = mypipe[0];
488 poller_wr_pipe[tid] = mypipe[1];
489 fcntl(poller_rd_pipe, F_SETFL, O_NONBLOCK);
490 fd_insert(poller_rd_pipe, poller_pipe_io_handler, poller_pipe_io_handler,
491 tid_bit);
492 fd_want_recv(poller_rd_pipe);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200493 return 1;
494}
495
496/* Deinitialize the pollers per thread */
497static void deinit_pollers_per_thread()
498{
William Lallemand808e1b72018-12-15 22:34:31 +0100499 /* rd and wr are init at the same place, but only rd is init to -1, so
500 we rely to rd to close. */
501 if (poller_rd_pipe > -1) {
502 close(poller_rd_pipe);
503 poller_rd_pipe = -1;
504 close(poller_wr_pipe[tid]);
505 poller_wr_pipe[tid] = -1;
506 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200507}
508
Willy Tarreau082b6282019-05-22 14:42:12 +0200509/* Release the pollers per thread, to be called late */
510static void free_pollers_per_thread()
511{
512 free(fd_updt);
513 fd_updt = NULL;
514}
515
Willy Tarreaubaaee002006-06-26 02:48:02 +0200516/*
Willy Tarreau4f60f162007-04-08 16:39:58 +0200517 * Initialize the pollers till the best one is found.
518 * If none works, returns 0, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200519 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200520int init_pollers()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521{
Willy Tarreau4f60f162007-04-08 16:39:58 +0200522 int p;
523 struct poller *bp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200524
Christopher Faulet63fe6522017-08-31 17:52:09 +0200525 if ((fdtab = calloc(global.maxsock, sizeof(struct fdtab))) == NULL)
526 goto fail_tab;
527
Olivier Houchard53055052019-07-25 14:00:18 +0000528 if ((polled_mask = calloc(global.maxsock, sizeof(*polled_mask))) == NULL)
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200529 goto fail_polledmask;
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000530
Christopher Faulet63fe6522017-08-31 17:52:09 +0200531 if ((fdinfo = calloc(global.maxsock, sizeof(struct fdinfo))) == NULL)
532 goto fail_info;
533
Olivier Houchard6b96f722018-04-25 16:58:25 +0200534 update_list.first = update_list.last = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200535
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100536 for (p = 0; p < global.maxsock; p++) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100537 HA_SPIN_INIT(&fdtab[p].lock);
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100538 /* Mark the fd as out of the fd cache */
Olivier Houchard12568362018-01-31 18:07:29 +0100539 fdtab[p].cache.next = -3;
Olivier Houchard6b96f722018-04-25 16:58:25 +0200540 fdtab[p].update.next = -3;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100541 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200542
Willy Tarreau4f60f162007-04-08 16:39:58 +0200543 do {
544 bp = NULL;
545 for (p = 0; p < nbpollers; p++)
546 if (!bp || (pollers[p].pref > bp->pref))
547 bp = &pollers[p];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200548
Willy Tarreau4f60f162007-04-08 16:39:58 +0200549 if (!bp || bp->pref == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550 break;
551
Willy Tarreau4f60f162007-04-08 16:39:58 +0200552 if (bp->init(bp)) {
553 memcpy(&cur_poller, bp, sizeof(*bp));
554 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200556 } while (!bp || bp->pref == 0);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100557
Christopher Faulet63fe6522017-08-31 17:52:09 +0200558 free(fdinfo);
559 fail_info:
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200560 free(polled_mask);
561 fail_polledmask:
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000562 free(fdtab);
563 fail_tab:
Willy Tarreau7be79a42012-11-11 15:02:54 +0100564 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200565}
566
Willy Tarreaubaaee002006-06-26 02:48:02 +0200567/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200568 * Deinitialize the pollers.
569 */
570void deinit_pollers() {
571
572 struct poller *bp;
573 int p;
574
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200575 for (p = 0; p < global.maxsock; p++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100576 HA_SPIN_DESTROY(&fdtab[p].lock);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200577
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200578 for (p = 0; p < nbpollers; p++) {
579 bp = &pollers[p];
580
581 if (bp && bp->pref)
582 bp->term(bp);
583 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200584
Christopher Faulet63fe6522017-08-31 17:52:09 +0200585 free(fdinfo); fdinfo = NULL;
586 free(fdtab); fdtab = NULL;
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200587 free(polled_mask); polled_mask = NULL;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200588}
589
590/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200591 * Lists the known pollers on <out>.
592 * Should be performed only before initialization.
593 */
594int list_pollers(FILE *out)
595{
596 int p;
597 int last, next;
598 int usable;
599 struct poller *bp;
600
601 fprintf(out, "Available polling systems :\n");
602
603 usable = 0;
604 bp = NULL;
605 last = next = -1;
606 while (1) {
607 for (p = 0; p < nbpollers; p++) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200608 if ((next < 0 || pollers[p].pref > next)
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100609 && (last < 0 || pollers[p].pref < last)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200610 next = pollers[p].pref;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100611 if (!bp || (pollers[p].pref > bp->pref))
612 bp = &pollers[p];
613 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200614 }
615
616 if (next == -1)
617 break;
618
619 for (p = 0; p < nbpollers; p++) {
620 if (pollers[p].pref == next) {
621 fprintf(out, " %10s : ", pollers[p].name);
622 if (pollers[p].pref == 0)
623 fprintf(out, "disabled, ");
624 else
625 fprintf(out, "pref=%3d, ", pollers[p].pref);
626 if (pollers[p].test(&pollers[p])) {
627 fprintf(out, " test result OK");
628 if (next > 0)
629 usable++;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100630 } else {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200631 fprintf(out, " test result FAILED");
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100632 if (bp == &pollers[p])
633 bp = NULL;
634 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200635 fprintf(out, "\n");
636 }
637 }
638 last = next;
639 next = -1;
640 };
641 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
642 return 0;
643}
644
645/*
646 * Some pollers may lose their connection after a fork(). It may be necessary
647 * to create initialize part of them again. Returns 0 in case of failure,
648 * otherwise 1. The fork() function may be NULL if unused. In case of error,
649 * the the current poller is destroyed and the caller is responsible for trying
650 * another one by calling init_pollers() again.
651 */
652int fork_poller()
653{
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200654 int fd;
Willy Tarreauce036bc2018-01-29 14:58:02 +0100655 for (fd = 0; fd < global.maxsock; fd++) {
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200656 if (fdtab[fd].owner) {
657 fdtab[fd].cloned = 1;
658 }
659 }
660
Willy Tarreau2ff76222007-04-09 19:29:56 +0200661 if (cur_poller.fork) {
662 if (cur_poller.fork(&cur_poller))
663 return 1;
664 cur_poller.term(&cur_poller);
665 return 0;
666 }
667 return 1;
668}
669
Willy Tarreau082b6282019-05-22 14:42:12 +0200670REGISTER_PER_THREAD_ALLOC(alloc_pollers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100671REGISTER_PER_THREAD_INIT(init_pollers_per_thread);
672REGISTER_PER_THREAD_DEINIT(deinit_pollers_per_thread);
Willy Tarreau082b6282019-05-22 14:42:12 +0200673REGISTER_PER_THREAD_FREE(free_pollers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100674
Willy Tarreau2ff76222007-04-09 19:29:56 +0200675/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676 * Local variables:
677 * c-indent-level: 8
678 * c-basic-offset: 8
679 * End:
680 */