blob: 7c906911a289b436266c374b1dd6bd9f390f6444 [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 Tarreauf817e9f2014-01-10 16:58:45 +010011 * This code implements an events cache for file descriptors. It remembers the
12 * readiness of a file descriptor after a return from poll() and the fact that
13 * an I/O attempt failed on EAGAIN. Events in the cache which are still marked
14 * ready and active are processed just as if they were reported by poll().
Willy Tarreau7be79a42012-11-11 15:02:54 +010015 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010016 * This serves multiple purposes. First, it significantly improves performance
17 * by avoiding to subscribe to polling unless absolutely necessary, so most
18 * events are processed without polling at all, especially send() which
19 * benefits from the socket buffers. Second, it is the only way to support
20 * edge-triggered pollers (eg: EPOLL_ET). And third, it enables I/O operations
21 * that are backed by invisible buffers. For example, SSL is able to read a
22 * whole socket buffer and not deliver it to the application buffer because
23 * it's full. Unfortunately, it won't be reported by a poller anymore until
24 * some new activity happens. The only way to call it again thus is to keep
25 * this readiness information in the cache and to access it without polling
26 * once the FD is enabled again.
Willy Tarreau7be79a42012-11-11 15:02:54 +010027 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010028 * One interesting feature of the cache is that it maintains the principle
29 * of speculative I/O introduced in haproxy 1.3 : the first time an event is
30 * enabled, the FD is considered as ready so that the I/O attempt is performed
31 * via the cache without polling. And the polling happens only when EAGAIN is
32 * first met. This avoids polling for HTTP requests, especially when the
33 * defer-accept mode is used. It also avoids polling for sending short data
34 * such as requests to servers or short responses to clients.
35 *
36 * The cache consists in a list of active events and a list of updates.
37 * Active events are events that are expected to come and that we must report
38 * to the application until it asks to stop or asks to poll. Updates are new
39 * requests for changing an FD state. Updates are the only way to create new
40 * events. This is important because it means that the number of cached events
41 * cannot increase between updates and will only grow one at a time while
42 * processing updates. All updates must always be processed, though events
43 * might be processed by small batches if required.
Willy Tarreau7be79a42012-11-11 15:02:54 +010044 *
45 * There is no direct link between the FD and the updates list. There is only a
46 * bit in the fdtab[] to indicate than a file descriptor is already present in
47 * the updates list. Once an fd is present in the updates list, it will have to
48 * be considered even if its changes are reverted in the middle or if the fd is
49 * replaced.
50 *
51 * It is important to understand that as long as all expected events are
52 * processed, they might starve the polled events, especially because polled
Willy Tarreauf817e9f2014-01-10 16:58:45 +010053 * I/O starvation quickly induces more cached I/O. One solution to this
Willy Tarreau7be79a42012-11-11 15:02:54 +010054 * consists in only processing a part of the events at once, but one drawback
Willy Tarreauf817e9f2014-01-10 16:58:45 +010055 * is that unhandled events will still wake the poller up. Using an edge-
56 * triggered poller such as EPOLL_ET will solve this issue though.
Willy Tarreau7be79a42012-11-11 15:02:54 +010057 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010058 * Since we do not want to scan all the FD list to find cached I/O events,
59 * we store them in a list consisting in a linear array holding only the FD
60 * indexes right now. Note that a closed FD cannot exist in the cache, because
61 * it is closed by fd_delete() which in turn calls fd_release_cache_entry()
62 * which always removes it from the list.
Willy Tarreau7be79a42012-11-11 15:02:54 +010063 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010064 * The FD array has to hold a back reference to the cache. This reference is
65 * always valid unless the FD is not in the cache and is not updated, in which
66 * case the reference points to index 0.
Willy Tarreau7be79a42012-11-11 15:02:54 +010067 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010068 * The event state for an FD, as found in fdtab[].state, is maintained for each
69 * direction. The state field is built this way, with R bits in the low nibble
70 * and W bits in the high nibble for ease of access and debugging :
Willy Tarreau7be79a42012-11-11 15:02:54 +010071 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010072 * 7 6 5 4 3 2 1 0
73 * [ 0 | PW | RW | AW | 0 | PR | RR | AR ]
74 *
75 * A* = active *R = read
76 * P* = polled *W = write
77 * R* = ready
78 *
79 * An FD is marked "active" when there is a desire to use it.
80 * An FD is marked "polled" when it is registered in the polling.
81 * An FD is marked "ready" when it has not faced a new EAGAIN since last wake-up
82 * (it is a cache of the last EAGAIN regardless of polling changes).
Willy Tarreau7be79a42012-11-11 15:02:54 +010083 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010084 * We have 8 possible states for each direction based on these 3 flags :
Willy Tarreau7be79a42012-11-11 15:02:54 +010085 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010086 * +---+---+---+----------+---------------------------------------------+
87 * | P | R | A | State | Description |
88 * +---+---+---+----------+---------------------------------------------+
89 * | 0 | 0 | 0 | DISABLED | No activity desired, not ready. |
90 * | 0 | 0 | 1 | MUSTPOLL | Activity desired via polling. |
91 * | 0 | 1 | 0 | STOPPED | End of activity without polling. |
92 * | 0 | 1 | 1 | ACTIVE | Activity desired without polling. |
93 * | 1 | 0 | 0 | ABORT | Aborted poll(). Not frequently seen. |
94 * | 1 | 0 | 1 | POLLED | FD is being polled. |
95 * | 1 | 1 | 0 | PAUSED | FD was paused while ready (eg: buffer full) |
96 * | 1 | 1 | 1 | READY | FD was marked ready by poll() |
97 * +---+---+---+----------+---------------------------------------------+
Willy Tarreau7be79a42012-11-11 15:02:54 +010098 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010099 * The transitions are pretty simple :
100 * - fd_want_*() : set flag A
101 * - fd_stop_*() : clear flag A
102 * - fd_cant_*() : clear flag R (when facing EAGAIN)
103 * - fd_may_*() : set flag R (upon return from poll())
104 * - sync() : if (A) { if (!R) P := 1 } else { P := 0 }
105 *
106 * The PAUSED, ABORT and MUSTPOLL states are transient for level-trigerred
107 * pollers and are fixed by the sync() which happens at the beginning of the
108 * poller. For event-triggered pollers, only the MUSTPOLL state will be
109 * transient and ABORT will lead to PAUSED. The ACTIVE state is the only stable
110 * one which has P != A.
111 *
112 * The READY state is a bit special as activity on the FD might be notified
113 * both by the poller or by the cache. But it is needed for some multi-layer
114 * protocols (eg: SSL) where connection activity is not 100% linked to FD
115 * activity. Also some pollers might prefer to implement it as ACTIVE if
116 * enabling/disabling the FD is cheap. The READY and ACTIVE states are the
117 * two states for which a cache entry is allocated.
118 *
119 * The state transitions look like the diagram below. Only the 4 right states
120 * have polling enabled :
121 *
122 * (POLLED=0) (POLLED=1)
123 *
124 * +----------+ sync +-------+
125 * | DISABLED | <----- | ABORT | (READY=0, ACTIVE=0)
126 * +----------+ +-------+
127 * clr | ^ set | ^
128 * | | | |
129 * v | set v | clr
130 * +----------+ sync +--------+
131 * | MUSTPOLL | -----> | POLLED | (READY=0, ACTIVE=1)
132 * +----------+ +--------+
133 * ^ poll | ^
134 * | | |
135 * | EAGAIN v | EAGAIN
136 * +--------+ +-------+
137 * | ACTIVE | | READY | (READY=1, ACTIVE=1)
138 * +--------+ +-------+
139 * clr | ^ set | ^
140 * | | | |
141 * v | set v | clr
142 * +---------+ sync +--------+
143 * | STOPPED | <------ | PAUSED | (READY=1, ACTIVE=0)
144 * +---------+ +--------+
Willy Tarreaubaaee002006-06-26 02:48:02 +0200145 */
146
Willy Tarreau2ff76222007-04-09 19:29:56 +0200147#include <stdio.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +0200148#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149#include <unistd.h>
Olivier Houchard79321b92018-07-26 17:55:11 +0200150#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151#include <sys/types.h>
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100152#include <sys/resource.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200153
Willy Tarreaue5733232019-05-22 19:24:06 +0200154#if defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100155#include <poll.h>
156#include <errno.h>
157#endif
158
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200159#include <common/compat.h>
160#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200161
Willy Tarreau7be79a42012-11-11 15:02:54 +0100162#include <types/global.h>
163
Willy Tarreau2a429502006-10-15 14:52:29 +0200164#include <proto/fd.h>
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200165#include <proto/log.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200166#include <proto/port_range.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167
168struct fdtab *fdtab = NULL; /* array of all the file descriptors */
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200169unsigned long *polled_mask = NULL; /* Array for the polled_mask of each fd */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200170struct fdinfo *fdinfo = NULL; /* less-often used infos for file descriptors */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171int totalconn; /* total # of terminated sessions */
172int actconn; /* # of active sessions */
173
Willy Tarreau4f60f162007-04-08 16:39:58 +0200174struct poller pollers[MAX_POLLERS];
175struct poller cur_poller;
176int nbpollers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200177
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100178volatile struct fdlist fd_cache ; // FD events cache
179volatile struct fdlist fd_cache_local[MAX_THREADS]; // FD events local for each thread
Olivier Houchard6b96f722018-04-25 16:58:25 +0200180volatile struct fdlist update_list; // Global update list
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100181
Christopher Faulet69553fe2018-01-15 11:57:03 +0100182unsigned long fd_cache_mask = 0; // Mask of threads with events in the cache
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200183
184THREAD_LOCAL int *fd_updt = NULL; // FD updates list
185THREAD_LOCAL int fd_nbupdt = 0; // number of updates in the list
Olivier Houchard79321b92018-07-26 17:55:11 +0200186THREAD_LOCAL int poller_rd_pipe = -1; // Pipe to wake the thread
187int poller_wr_pipe[MAX_THREADS]; // Pipe to wake the threads
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200188
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200189volatile int ha_used_fds = 0; // Number of FD we're currently using
190
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200191#define _GET_NEXT(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->next
192#define _GET_PREV(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->prev
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100193/* adds fd <fd> to fd list <list> if it was not yet in it */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200194void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off)
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100195{
196 int next;
197 int new;
198 int old;
199 int last;
200
201redo_next:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200202 next = _GET_NEXT(fd, off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100203 /* Check that we're not already in the cache, and if not, lock us. */
204 if (next >= -2)
205 goto done;
Olivier Houchardd3608792019-03-08 18:47:42 +0100206 if (!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100207 goto redo_next;
Olivier Houchardd2b5d162019-03-08 13:47:21 +0100208 __ha_barrier_atomic_store();
Willy Tarreau11559a72018-02-05 17:52:24 +0100209
210 new = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100211redo_last:
212 /* First, insert in the linked list */
213 last = list->last;
214 old = -1;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100215
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200216 _GET_PREV(fd, off) = -2;
Willy Tarreau11559a72018-02-05 17:52:24 +0100217 /* Make sure the "prev" store is visible before we update the last entry */
218 __ha_barrier_store();
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100219
Willy Tarreau11559a72018-02-05 17:52:24 +0100220 if (unlikely(last == -1)) {
221 /* list is empty, try to add ourselves alone so that list->last=fd */
Olivier Houchardd3608792019-03-08 18:47:42 +0100222 if (unlikely(!_HA_ATOMIC_CAS(&list->last, &old, new)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100223 goto redo_last;
224
225 /* list->first was necessary -1, we're guaranteed to be alone here */
226 list->first = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100227 } else {
Willy Tarreau11559a72018-02-05 17:52:24 +0100228 /* adding ourselves past the last element
229 * The CAS will only succeed if its next is -1,
230 * which means it's in the cache, and the last element.
231 */
Olivier Houchardd3608792019-03-08 18:47:42 +0100232 if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(last, off), &old, new)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100233 goto redo_last;
Willy Tarreau11559a72018-02-05 17:52:24 +0100234
235 /* Then, update the last entry */
236 list->last = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100237 }
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100238 __ha_barrier_store();
Willy Tarreau11559a72018-02-05 17:52:24 +0100239 /* since we're alone at the end of the list and still locked(-2),
240 * we know noone tried to add past us. Mark the end of list.
241 */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200242 _GET_PREV(fd, off) = last;
243 _GET_NEXT(fd, off) = -1;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100244 __ha_barrier_store();
245done:
246 return;
247}
248
249/* removes fd <fd> from fd list <list> */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200250void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off)
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100251{
252#if defined(HA_HAVE_CAS_DW) || defined(HA_CAS_IS_8B)
253 volatile struct fdlist_entry cur_list, next_list;
254#endif
255 int old;
256 int new = -2;
257 int prev;
258 int next;
259 int last;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100260lock_self:
261#if (defined(HA_CAS_IS_8B) || defined(HA_HAVE_CAS_DW))
262 next_list.next = next_list.prev = -2;
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200263 cur_list = *(volatile struct fdlist_entry *)(((char *)&fdtab[fd]) + off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100264 /* First, attempt to lock our own entries */
265 do {
266 /* The FD is not in the FD cache, give up */
267 if (unlikely(cur_list.next <= -3))
268 return;
269 if (unlikely(cur_list.prev == -2 || cur_list.next == -2))
270 goto lock_self;
271 } while (
272#ifdef HA_CAS_IS_8B
Olivier Houchardd3608792019-03-08 18:47:42 +0100273 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 +0100274#else
Willy Tarreauc3b59582019-05-27 17:37:20 +0200275 unlikely(!_HA_ATOMIC_DWCAS(((void *)&_GET_NEXT(fd, off)), ((void *)&cur_list), ((void *)&next_list))))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100276#endif
277 ;
278 next = cur_list.next;
279 prev = cur_list.prev;
280
281#else
282lock_self_next:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200283 next = ({ volatile int *next = &_GET_NEXT(fd, off); *next; });
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100284 if (next == -2)
285 goto lock_self_next;
286 if (next <= -3)
287 goto done;
Olivier Houchardd3608792019-03-08 18:47:42 +0100288 if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100289 goto lock_self_next;
290lock_self_prev:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200291 prev = ({ volatile int *prev = &_GET_PREV(fd, off); *prev; });
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100292 if (prev == -2)
293 goto lock_self_prev;
Olivier Houchardd3608792019-03-08 18:47:42 +0100294 if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(fd, off), &prev, -2)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100295 goto lock_self_prev;
296#endif
Olivier Houchardd2b5d162019-03-08 13:47:21 +0100297 __ha_barrier_atomic_store();
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100298
299 /* Now, lock the entries of our neighbours */
300 if (likely(prev != -1)) {
301redo_prev:
302 old = fd;
303
Olivier Houchardd3608792019-03-08 18:47:42 +0100304 if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(prev, off), &old, new))) {
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100305 if (unlikely(old == -2)) {
306 /* Neighbour already locked, give up and
307 * retry again once he's done
308 */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200309 _GET_PREV(fd, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100310 __ha_barrier_store();
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200311 _GET_NEXT(fd, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100312 __ha_barrier_store();
313 goto lock_self;
314 }
315 goto redo_prev;
316 }
317 }
318 if (likely(next != -1)) {
319redo_next:
320 old = fd;
Olivier Houchardd3608792019-03-08 18:47:42 +0100321 if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(next, off), &old, new))) {
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100322 if (unlikely(old == -2)) {
323 /* Neighbour already locked, give up and
324 * retry again once he's done
325 */
326 if (prev != -1) {
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200327 _GET_NEXT(prev, off) = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100328 __ha_barrier_store();
329 }
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200330 _GET_PREV(fd, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100331 __ha_barrier_store();
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200332 _GET_NEXT(fd, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100333 __ha_barrier_store();
334 goto lock_self;
335 }
336 goto redo_next;
337 }
338 }
339 if (list->first == fd)
340 list->first = next;
341 __ha_barrier_store();
342 last = list->last;
Olivier Houchardd3608792019-03-08 18:47:42 +0100343 while (unlikely(last == fd && (!_HA_ATOMIC_CAS(&list->last, &last, prev))))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100344 __ha_compiler_barrier();
345 /* Make sure we let other threads know we're no longer in cache,
346 * before releasing our neighbours.
347 */
348 __ha_barrier_store();
349 if (likely(prev != -1))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200350 _GET_NEXT(prev, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100351 __ha_barrier_store();
352 if (likely(next != -1))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200353 _GET_PREV(next, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100354 __ha_barrier_store();
355 /* Ok, now we're out of the fd cache */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200356 _GET_NEXT(fd, off) = -(next + 4);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100357 __ha_barrier_store();
358done:
359 return;
360}
361
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200362#undef _GET_NEXT
363#undef _GET_PREV
364
Willy Tarreau173d9952018-01-26 21:48:23 +0100365/* Deletes an FD from the fdsets.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200366 * The file descriptor is also closed.
367 */
Olivier Houchard1fc05162017-04-06 01:05:05 +0200368static void fd_dodelete(int fd, int do_close)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369{
Willy Tarreau87d54a92018-10-15 09:44:46 +0200370 unsigned long locked = atleast2(fdtab[fd].thread_mask);
371
372 if (locked)
373 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreauad38ace2013-12-15 14:19:38 +0100374 if (fdtab[fd].linger_risk) {
375 /* this is generally set when connecting to servers */
376 setsockopt(fd, SOL_SOCKET, SO_LINGER,
377 (struct linger *) &nolinger, sizeof(struct linger));
378 }
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100379 if (cur_poller.clo)
380 cur_poller.clo(fd);
Olivier Houchard15a2e682019-08-05 18:51:52 +0200381 polled_mask[fd] = 0;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100382
Willy Tarreau899d9572014-01-25 19:20:35 +0100383 fd_release_cache_entry(fd);
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100384 fdtab[fd].state = 0;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100385
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200386 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
387 fdinfo[fd].port_range = NULL;
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200388 fdtab[fd].owner = NULL;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100389 fdtab[fd].thread_mask = 0;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100390 if (do_close) {
Christopher Fauletd531f882017-06-01 16:55:03 +0200391 close(fd);
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200392 _HA_ATOMIC_SUB(&ha_used_fds, 1);
Willy Tarreauc9c83782018-01-17 18:44:46 +0100393 }
Willy Tarreau87d54a92018-10-15 09:44:46 +0200394 if (locked)
395 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200396}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200397
Willy Tarreau173d9952018-01-26 21:48:23 +0100398/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200399 * The file descriptor is also closed.
400 */
401void fd_delete(int fd)
402{
403 fd_dodelete(fd, 1);
404}
405
Willy Tarreau173d9952018-01-26 21:48:23 +0100406/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200407 * The file descriptor is kept open.
408 */
409void fd_remove(int fd)
410{
411 fd_dodelete(fd, 0);
412}
413
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100414static inline void fdlist_process_cached_events(volatile struct fdlist *fdlist)
Willy Tarreau09f24562012-11-11 16:43:45 +0100415{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100416 int fd, old_fd, e;
Richard Russobc9d9842019-02-20 12:43:45 -0800417 unsigned long locked;
Willy Tarreau09f24562012-11-11 16:43:45 +0100418
Olivier Houchard12568362018-01-31 18:07:29 +0100419 for (old_fd = fd = fdlist->first; fd != -1; fd = fdtab[fd].cache.next) {
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100420 if (fd == -2) {
421 fd = old_fd;
422 continue;
423 } else if (fd <= -3)
424 fd = -fd - 4;
425 if (fd == -1)
426 break;
427 old_fd = fd;
428 if (!(fdtab[fd].thread_mask & tid_bit))
429 continue;
Olivier Houchard12568362018-01-31 18:07:29 +0100430 if (fdtab[fd].cache.next < -3)
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100431 continue;
Christopher Faulet69553fe2018-01-15 11:57:03 +0100432
Olivier Houchardd3608792019-03-08 18:47:42 +0100433 _HA_ATOMIC_OR(&fd_cache_mask, tid_bit);
Richard Russobc9d9842019-02-20 12:43:45 -0800434 locked = atleast2(fdtab[fd].thread_mask);
435 if (locked && HA_SPIN_TRYLOCK(FD_LOCK, &fdtab[fd].lock)) {
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100436 activity[tid].fd_lock++;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100437 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100438 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200439
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200440 e = fdtab[fd].state;
Willy Tarreau09f24562012-11-11 16:43:45 +0100441 fdtab[fd].ev &= FD_POLL_STICKY;
442
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100443 if ((e & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R))
Willy Tarreau09f24562012-11-11 16:43:45 +0100444 fdtab[fd].ev |= FD_POLL_IN;
445
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100446 if ((e & (FD_EV_READY_W | FD_EV_ACTIVE_W)) == (FD_EV_READY_W | FD_EV_ACTIVE_W))
Willy Tarreau09f24562012-11-11 16:43:45 +0100447 fdtab[fd].ev |= FD_POLL_OUT;
448
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200449 if (fdtab[fd].iocb && fdtab[fd].owner && fdtab[fd].ev) {
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 Tarreau09f24562012-11-11 16:43:45 +0100452 fdtab[fd].iocb(fd);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200453 }
454 else {
Willy Tarreau5be2f352014-11-19 19:43:05 +0100455 fd_release_cache_entry(fd);
Richard Russobc9d9842019-02-20 12:43:45 -0800456 if (locked)
Willy Tarreau87d54a92018-10-15 09:44:46 +0200457 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200458 }
Willy Tarreaue1aed242019-08-16 16:06:14 +0200459 ti->flags &= ~TI_FL_STUCK; // this thread is still running
Willy Tarreau09f24562012-11-11 16:43:45 +0100460 }
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100461}
462
463/* Scan and process the cached events. This should be called right after
464 * the poller. The loop may cause new entries to be created, for example
465 * if a listener causes an accept() to initiate a new incoming connection
466 * wanting to attempt an recv().
467 */
468void fd_process_cached_events()
469{
Olivier Houchardd3608792019-03-08 18:47:42 +0100470 _HA_ATOMIC_AND(&fd_cache_mask, ~tid_bit);
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100471 fdlist_process_cached_events(&fd_cache_local[tid]);
472 fdlist_process_cached_events(&fd_cache);
Willy Tarreau09f24562012-11-11 16:43:45 +0100473}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474
Olivier Houchard2292edf2019-02-25 14:26:54 +0100475#if defined(USE_CLOSEFROM)
476void my_closefrom(int start)
477{
478 closefrom(start);
479}
480
Willy Tarreaue5733232019-05-22 19:24:06 +0200481#elif defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100482/* This is a portable implementation of closefrom(). It closes all open file
483 * descriptors starting at <start> and above. It relies on the fact that poll()
484 * will return POLLNVAL for each invalid (hence close) file descriptor passed
485 * in argument in order to skip them. It acts with batches of FDs and will
486 * typically perform one poll() call per 1024 FDs so the overhead is low in
487 * case all FDs have to be closed.
488 */
489void my_closefrom(int start)
490{
491 struct pollfd poll_events[1024];
492 struct rlimit limit;
493 int nbfds, fd, ret, idx;
494 int step, next;
495
496 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
497 step = nbfds = limit.rlim_cur;
498 else
499 step = nbfds = 0;
500
501 if (nbfds <= 0) {
502 /* set safe limit */
503 nbfds = 1024;
504 step = 256;
505 }
506
507 if (step > sizeof(poll_events) / sizeof(poll_events[0]))
508 step = sizeof(poll_events) / sizeof(poll_events[0]);
509
510 while (start < nbfds) {
511 next = (start / step + 1) * step;
512
513 for (fd = start; fd < next && fd < nbfds; fd++) {
514 poll_events[fd - start].fd = fd;
515 poll_events[fd - start].events = 0;
516 }
517
518 do {
519 ret = poll(poll_events, fd - start, 0);
520 if (ret >= 0)
521 break;
522 } while (errno == EAGAIN || errno == EINTR || errno == ENOMEM);
523
Willy Tarreaub8e602c2019-02-22 09:07:42 +0100524 if (ret)
525 ret = fd - start;
526
Willy Tarreau9188ac62019-02-21 22:12:47 +0100527 for (idx = 0; idx < ret; idx++) {
528 if (poll_events[idx].revents & POLLNVAL)
529 continue; /* already closed */
530
531 fd = poll_events[idx].fd;
532 close(fd);
533 }
534 start = next;
535 }
536}
537
Willy Tarreaue5733232019-05-22 19:24:06 +0200538#else // defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100539
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100540/* This is a portable implementation of closefrom(). It closes all open file
541 * descriptors starting at <start> and above. This is a naive version for use
542 * when the operating system provides no alternative.
543 */
544void my_closefrom(int start)
545{
546 struct rlimit limit;
547 int nbfds;
548
549 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
550 nbfds = limit.rlim_cur;
551 else
552 nbfds = 0;
553
554 if (nbfds <= 0)
555 nbfds = 1024; /* safe limit */
556
557 while (start < nbfds)
558 close(start++);
559}
Willy Tarreaue5733232019-05-22 19:24:06 +0200560#endif // defined(USE_POLL)
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100561
Willy Tarreau4f60f162007-04-08 16:39:58 +0200562/* disable the specified poller */
563void disable_poller(const char *poller_name)
564{
565 int p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566
Willy Tarreau4f60f162007-04-08 16:39:58 +0200567 for (p = 0; p < nbpollers; p++)
568 if (strcmp(pollers[p].name, poller_name) == 0)
569 pollers[p].pref = 0;
570}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571
Olivier Houchard79321b92018-07-26 17:55:11 +0200572void poller_pipe_io_handler(int fd)
573{
574 char buf[1024];
575 /* Flush the pipe */
576 while (read(fd, buf, sizeof(buf)) > 0);
577 fd_cant_recv(fd);
578}
579
Willy Tarreau082b6282019-05-22 14:42:12 +0200580/* allocate the per-thread fd_updt thus needs to be called early after
581 * thread creation.
582 */
583static int alloc_pollers_per_thread()
584{
585 fd_updt = calloc(global.maxsock, sizeof(*fd_updt));
586 return fd_updt != NULL;
587}
588
589/* Initialize the pollers per thread.*/
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200590static int init_pollers_per_thread()
591{
Olivier Houchard79321b92018-07-26 17:55:11 +0200592 int mypipe[2];
Willy Tarreau082b6282019-05-22 14:42:12 +0200593
594 if (pipe(mypipe) < 0)
Olivier Houchard79321b92018-07-26 17:55:11 +0200595 return 0;
Willy Tarreau082b6282019-05-22 14:42:12 +0200596
Olivier Houchard79321b92018-07-26 17:55:11 +0200597 poller_rd_pipe = mypipe[0];
598 poller_wr_pipe[tid] = mypipe[1];
599 fcntl(poller_rd_pipe, F_SETFL, O_NONBLOCK);
600 fd_insert(poller_rd_pipe, poller_pipe_io_handler, poller_pipe_io_handler,
601 tid_bit);
602 fd_want_recv(poller_rd_pipe);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200603 return 1;
604}
605
606/* Deinitialize the pollers per thread */
607static void deinit_pollers_per_thread()
608{
William Lallemand808e1b72018-12-15 22:34:31 +0100609 /* rd and wr are init at the same place, but only rd is init to -1, so
610 we rely to rd to close. */
611 if (poller_rd_pipe > -1) {
612 close(poller_rd_pipe);
613 poller_rd_pipe = -1;
614 close(poller_wr_pipe[tid]);
615 poller_wr_pipe[tid] = -1;
616 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200617}
618
Willy Tarreau082b6282019-05-22 14:42:12 +0200619/* Release the pollers per thread, to be called late */
620static void free_pollers_per_thread()
621{
622 free(fd_updt);
623 fd_updt = NULL;
624}
625
Willy Tarreaubaaee002006-06-26 02:48:02 +0200626/*
Willy Tarreau4f60f162007-04-08 16:39:58 +0200627 * Initialize the pollers till the best one is found.
628 * If none works, returns 0, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200630int init_pollers()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200631{
Willy Tarreau4f60f162007-04-08 16:39:58 +0200632 int p;
633 struct poller *bp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634
Christopher Faulet63fe6522017-08-31 17:52:09 +0200635 if ((fdtab = calloc(global.maxsock, sizeof(struct fdtab))) == NULL)
636 goto fail_tab;
637
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200638 if ((polled_mask = calloc(global.maxsock, sizeof(unsigned long))) == NULL)
639 goto fail_polledmask;
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000640
Christopher Faulet63fe6522017-08-31 17:52:09 +0200641 if ((fdinfo = calloc(global.maxsock, sizeof(struct fdinfo))) == NULL)
642 goto fail_info;
643
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100644 fd_cache.first = fd_cache.last = -1;
Olivier Houchard6b96f722018-04-25 16:58:25 +0200645 update_list.first = update_list.last = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200646
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100647 for (p = 0; p < global.maxsock; p++) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100648 HA_SPIN_INIT(&fdtab[p].lock);
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100649 /* Mark the fd as out of the fd cache */
Olivier Houchard12568362018-01-31 18:07:29 +0100650 fdtab[p].cache.next = -3;
Olivier Houchard6b96f722018-04-25 16:58:25 +0200651 fdtab[p].update.next = -3;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100652 }
653 for (p = 0; p < global.nbthread; p++)
654 fd_cache_local[p].first = fd_cache_local[p].last = -1;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200655
Willy Tarreau4f60f162007-04-08 16:39:58 +0200656 do {
657 bp = NULL;
658 for (p = 0; p < nbpollers; p++)
659 if (!bp || (pollers[p].pref > bp->pref))
660 bp = &pollers[p];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200661
Willy Tarreau4f60f162007-04-08 16:39:58 +0200662 if (!bp || bp->pref == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200663 break;
664
Willy Tarreau4f60f162007-04-08 16:39:58 +0200665 if (bp->init(bp)) {
666 memcpy(&cur_poller, bp, sizeof(*bp));
667 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200668 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200669 } while (!bp || bp->pref == 0);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100670
Christopher Faulet63fe6522017-08-31 17:52:09 +0200671 free(fdinfo);
672 fail_info:
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200673 free(polled_mask);
674 fail_polledmask:
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000675 free(fdtab);
676 fail_tab:
Willy Tarreau7be79a42012-11-11 15:02:54 +0100677 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200678}
679
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200681 * Deinitialize the pollers.
682 */
683void deinit_pollers() {
684
685 struct poller *bp;
686 int p;
687
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200688 for (p = 0; p < global.maxsock; p++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100689 HA_SPIN_DESTROY(&fdtab[p].lock);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200690
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200691 for (p = 0; p < nbpollers; p++) {
692 bp = &pollers[p];
693
694 if (bp && bp->pref)
695 bp->term(bp);
696 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200697
Christopher Faulet63fe6522017-08-31 17:52:09 +0200698 free(fdinfo); fdinfo = NULL;
699 free(fdtab); fdtab = NULL;
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200700 free(polled_mask); polled_mask = NULL;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200701}
702
703/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200704 * Lists the known pollers on <out>.
705 * Should be performed only before initialization.
706 */
707int list_pollers(FILE *out)
708{
709 int p;
710 int last, next;
711 int usable;
712 struct poller *bp;
713
714 fprintf(out, "Available polling systems :\n");
715
716 usable = 0;
717 bp = NULL;
718 last = next = -1;
719 while (1) {
720 for (p = 0; p < nbpollers; p++) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200721 if ((next < 0 || pollers[p].pref > next)
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100722 && (last < 0 || pollers[p].pref < last)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200723 next = pollers[p].pref;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100724 if (!bp || (pollers[p].pref > bp->pref))
725 bp = &pollers[p];
726 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200727 }
728
729 if (next == -1)
730 break;
731
732 for (p = 0; p < nbpollers; p++) {
733 if (pollers[p].pref == next) {
734 fprintf(out, " %10s : ", pollers[p].name);
735 if (pollers[p].pref == 0)
736 fprintf(out, "disabled, ");
737 else
738 fprintf(out, "pref=%3d, ", pollers[p].pref);
739 if (pollers[p].test(&pollers[p])) {
740 fprintf(out, " test result OK");
741 if (next > 0)
742 usable++;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100743 } else {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200744 fprintf(out, " test result FAILED");
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100745 if (bp == &pollers[p])
746 bp = NULL;
747 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200748 fprintf(out, "\n");
749 }
750 }
751 last = next;
752 next = -1;
753 };
754 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
755 return 0;
756}
757
758/*
759 * Some pollers may lose their connection after a fork(). It may be necessary
760 * to create initialize part of them again. Returns 0 in case of failure,
761 * otherwise 1. The fork() function may be NULL if unused. In case of error,
762 * the the current poller is destroyed and the caller is responsible for trying
763 * another one by calling init_pollers() again.
764 */
765int fork_poller()
766{
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200767 int fd;
Willy Tarreauce036bc2018-01-29 14:58:02 +0100768 for (fd = 0; fd < global.maxsock; fd++) {
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200769 if (fdtab[fd].owner) {
770 fdtab[fd].cloned = 1;
771 }
772 }
773
Willy Tarreau2ff76222007-04-09 19:29:56 +0200774 if (cur_poller.fork) {
775 if (cur_poller.fork(&cur_poller))
776 return 1;
777 cur_poller.term(&cur_poller);
778 return 0;
779 }
780 return 1;
781}
782
Willy Tarreau082b6282019-05-22 14:42:12 +0200783REGISTER_PER_THREAD_ALLOC(alloc_pollers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100784REGISTER_PER_THREAD_INIT(init_pollers_per_thread);
785REGISTER_PER_THREAD_DEINIT(deinit_pollers_per_thread);
Willy Tarreau082b6282019-05-22 14:42:12 +0200786REGISTER_PER_THREAD_FREE(free_pollers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100787
Willy Tarreau2ff76222007-04-09 19:29:56 +0200788/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200789 * Local variables:
790 * c-indent-level: 8
791 * c-basic-offset: 8
792 * End:
793 */