blob: 92d093cb633fb51d7247c8c1a73789c6fac07a0e [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 Tarreau2dd0d472006-06-29 17:53:05 +0200154#include <common/compat.h>
155#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156
Willy Tarreau7be79a42012-11-11 15:02:54 +0100157#include <types/global.h>
158
Willy Tarreau2a429502006-10-15 14:52:29 +0200159#include <proto/fd.h>
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200160#include <proto/log.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200161#include <proto/port_range.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200162
163struct fdtab *fdtab = NULL; /* array of all the file descriptors */
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200164unsigned long *polled_mask = NULL; /* Array for the polled_mask of each fd */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200165struct fdinfo *fdinfo = NULL; /* less-often used infos for file descriptors */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166int totalconn; /* total # of terminated sessions */
167int actconn; /* # of active sessions */
168
Willy Tarreau4f60f162007-04-08 16:39:58 +0200169struct poller pollers[MAX_POLLERS];
170struct poller cur_poller;
171int nbpollers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100173volatile struct fdlist fd_cache ; // FD events cache
174volatile struct fdlist fd_cache_local[MAX_THREADS]; // FD events local for each thread
Olivier Houchard6b96f722018-04-25 16:58:25 +0200175volatile struct fdlist update_list; // Global update list
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100176
Christopher Faulet69553fe2018-01-15 11:57:03 +0100177unsigned long fd_cache_mask = 0; // Mask of threads with events in the cache
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200178
179THREAD_LOCAL int *fd_updt = NULL; // FD updates list
180THREAD_LOCAL int fd_nbupdt = 0; // number of updates in the list
Olivier Houchard79321b92018-07-26 17:55:11 +0200181THREAD_LOCAL int poller_rd_pipe = -1; // Pipe to wake the thread
182int poller_wr_pipe[MAX_THREADS]; // Pipe to wake the threads
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200183
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200184#define _GET_NEXT(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->next
185#define _GET_PREV(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->prev
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100186/* adds fd <fd> to fd list <list> if it was not yet in it */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200187void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off)
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100188{
189 int next;
190 int new;
191 int old;
192 int last;
193
194redo_next:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200195 next = _GET_NEXT(fd, off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100196 /* Check that we're not already in the cache, and if not, lock us. */
197 if (next >= -2)
198 goto done;
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200199 if (!HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100200 goto redo_next;
201 __ha_barrier_store();
Willy Tarreau11559a72018-02-05 17:52:24 +0100202
203 new = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100204redo_last:
205 /* First, insert in the linked list */
206 last = list->last;
207 old = -1;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100208
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200209 _GET_PREV(fd, off) = -2;
Willy Tarreau11559a72018-02-05 17:52:24 +0100210 /* Make sure the "prev" store is visible before we update the last entry */
211 __ha_barrier_store();
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100212
Willy Tarreau11559a72018-02-05 17:52:24 +0100213 if (unlikely(last == -1)) {
214 /* list is empty, try to add ourselves alone so that list->last=fd */
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100215 if (unlikely(!HA_ATOMIC_CAS(&list->last, &old, new)))
216 goto redo_last;
217
218 /* list->first was necessary -1, we're guaranteed to be alone here */
219 list->first = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100220 } else {
Willy Tarreau11559a72018-02-05 17:52:24 +0100221 /* adding ourselves past the last element
222 * The CAS will only succeed if its next is -1,
223 * which means it's in the cache, and the last element.
224 */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200225 if (unlikely(!HA_ATOMIC_CAS(&_GET_NEXT(last, off), &old, new)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100226 goto redo_last;
Willy Tarreau11559a72018-02-05 17:52:24 +0100227
228 /* Then, update the last entry */
229 list->last = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100230 }
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100231 __ha_barrier_store();
Willy Tarreau11559a72018-02-05 17:52:24 +0100232 /* since we're alone at the end of the list and still locked(-2),
233 * we know noone tried to add past us. Mark the end of list.
234 */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200235 _GET_PREV(fd, off) = last;
236 _GET_NEXT(fd, off) = -1;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100237 __ha_barrier_store();
238done:
239 return;
240}
241
242/* removes fd <fd> from fd list <list> */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200243void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off)
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100244{
245#if defined(HA_HAVE_CAS_DW) || defined(HA_CAS_IS_8B)
246 volatile struct fdlist_entry cur_list, next_list;
247#endif
248 int old;
249 int new = -2;
250 int prev;
251 int next;
252 int last;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100253lock_self:
254#if (defined(HA_CAS_IS_8B) || defined(HA_HAVE_CAS_DW))
255 next_list.next = next_list.prev = -2;
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200256 cur_list = *(volatile struct fdlist_entry *)(((char *)&fdtab[fd]) + off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100257 /* First, attempt to lock our own entries */
258 do {
259 /* The FD is not in the FD cache, give up */
260 if (unlikely(cur_list.next <= -3))
261 return;
262 if (unlikely(cur_list.prev == -2 || cur_list.next == -2))
263 goto lock_self;
264 } while (
265#ifdef HA_CAS_IS_8B
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200266 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 +0100267#else
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200268 unlikely(!__ha_cas_dw((void *)&_GET_NEXT(fd, off), (void *)&cur_list, (void *)&next_list)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100269#endif
270 ;
271 next = cur_list.next;
272 prev = cur_list.prev;
273
274#else
275lock_self_next:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200276 next = ({ volatile int *next = &_GET_NEXT(fd, off); *next; });
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100277 if (next == -2)
278 goto lock_self_next;
279 if (next <= -3)
280 goto done;
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200281 if (unlikely(!HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100282 goto lock_self_next;
283lock_self_prev:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200284 prev = ({ volatile int *prev = &_GET_PREV(fd, off); *prev; });
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100285 if (prev == -2)
286 goto lock_self_prev;
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200287 if (unlikely(!HA_ATOMIC_CAS(&_GET_PREV(fd, off), &prev, -2)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100288 goto lock_self_prev;
289#endif
290 __ha_barrier_store();
291
292 /* Now, lock the entries of our neighbours */
293 if (likely(prev != -1)) {
294redo_prev:
295 old = fd;
296
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200297 if (unlikely(!HA_ATOMIC_CAS(&_GET_NEXT(prev, off), &old, new))) {
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100298 if (unlikely(old == -2)) {
299 /* Neighbour already locked, give up and
300 * retry again once he's done
301 */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200302 _GET_PREV(fd, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100303 __ha_barrier_store();
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200304 _GET_NEXT(fd, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100305 __ha_barrier_store();
306 goto lock_self;
307 }
308 goto redo_prev;
309 }
310 }
311 if (likely(next != -1)) {
312redo_next:
313 old = fd;
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200314 if (unlikely(!HA_ATOMIC_CAS(&_GET_PREV(next, off), &old, new))) {
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100315 if (unlikely(old == -2)) {
316 /* Neighbour already locked, give up and
317 * retry again once he's done
318 */
319 if (prev != -1) {
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200320 _GET_NEXT(prev, off) = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100321 __ha_barrier_store();
322 }
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200323 _GET_PREV(fd, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100324 __ha_barrier_store();
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200325 _GET_NEXT(fd, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100326 __ha_barrier_store();
327 goto lock_self;
328 }
329 goto redo_next;
330 }
331 }
332 if (list->first == fd)
333 list->first = next;
334 __ha_barrier_store();
335 last = list->last;
336 while (unlikely(last == fd && (!HA_ATOMIC_CAS(&list->last, &last, prev))))
337 __ha_compiler_barrier();
338 /* Make sure we let other threads know we're no longer in cache,
339 * before releasing our neighbours.
340 */
341 __ha_barrier_store();
342 if (likely(prev != -1))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200343 _GET_NEXT(prev, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100344 __ha_barrier_store();
345 if (likely(next != -1))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200346 _GET_PREV(next, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100347 __ha_barrier_store();
348 /* Ok, now we're out of the fd cache */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200349 _GET_NEXT(fd, off) = -(next + 4);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100350 __ha_barrier_store();
351done:
352 return;
353}
354
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200355#undef _GET_NEXT
356#undef _GET_PREV
357
Willy Tarreau173d9952018-01-26 21:48:23 +0100358/* Deletes an FD from the fdsets.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200359 * The file descriptor is also closed.
360 */
Olivier Houchard1fc05162017-04-06 01:05:05 +0200361static void fd_dodelete(int fd, int do_close)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362{
Willy Tarreau87d54a92018-10-15 09:44:46 +0200363 unsigned long locked = atleast2(fdtab[fd].thread_mask);
364
365 if (locked)
366 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreauad38ace2013-12-15 14:19:38 +0100367 if (fdtab[fd].linger_risk) {
368 /* this is generally set when connecting to servers */
369 setsockopt(fd, SOL_SOCKET, SO_LINGER,
370 (struct linger *) &nolinger, sizeof(struct linger));
371 }
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100372 if (cur_poller.clo)
373 cur_poller.clo(fd);
374
Willy Tarreau899d9572014-01-25 19:20:35 +0100375 fd_release_cache_entry(fd);
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100376 fdtab[fd].state = 0;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100377
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200378 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
379 fdinfo[fd].port_range = NULL;
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200380 fdtab[fd].owner = NULL;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100381 fdtab[fd].thread_mask = 0;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100382 if (do_close) {
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200383 polled_mask[fd] = 0;
Christopher Fauletd531f882017-06-01 16:55:03 +0200384 close(fd);
Willy Tarreauc9c83782018-01-17 18:44:46 +0100385 }
Willy Tarreau87d54a92018-10-15 09:44:46 +0200386 if (locked)
387 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389
Willy Tarreau173d9952018-01-26 21:48:23 +0100390/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200391 * The file descriptor is also closed.
392 */
393void fd_delete(int fd)
394{
395 fd_dodelete(fd, 1);
396}
397
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 kept open.
400 */
401void fd_remove(int fd)
402{
403 fd_dodelete(fd, 0);
404}
405
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100406static inline void fdlist_process_cached_events(volatile struct fdlist *fdlist)
Willy Tarreau09f24562012-11-11 16:43:45 +0100407{
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100408 int fd, old_fd, e;
Willy Tarreau09f24562012-11-11 16:43:45 +0100409
Olivier Houchard12568362018-01-31 18:07:29 +0100410 for (old_fd = fd = fdlist->first; fd != -1; fd = fdtab[fd].cache.next) {
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100411 if (fd == -2) {
412 fd = old_fd;
413 continue;
414 } else if (fd <= -3)
415 fd = -fd - 4;
416 if (fd == -1)
417 break;
418 old_fd = fd;
419 if (!(fdtab[fd].thread_mask & tid_bit))
420 continue;
Olivier Houchard12568362018-01-31 18:07:29 +0100421 if (fdtab[fd].cache.next < -3)
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100422 continue;
Christopher Faulet69553fe2018-01-15 11:57:03 +0100423
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100424 HA_ATOMIC_OR(&fd_cache_mask, tid_bit);
Willy Tarreau87d54a92018-10-15 09:44:46 +0200425 if (atleast2(fdtab[fd].thread_mask) && HA_SPIN_TRYLOCK(FD_LOCK, &fdtab[fd].lock)) {
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100426 activity[tid].fd_lock++;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100427 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100428 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200429
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200430 e = fdtab[fd].state;
Willy Tarreau09f24562012-11-11 16:43:45 +0100431 fdtab[fd].ev &= FD_POLL_STICKY;
432
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100433 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 +0100434 fdtab[fd].ev |= FD_POLL_IN;
435
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100436 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 +0100437 fdtab[fd].ev |= FD_POLL_OUT;
438
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200439 if (fdtab[fd].iocb && fdtab[fd].owner && fdtab[fd].ev) {
Willy Tarreau87d54a92018-10-15 09:44:46 +0200440 if (atleast2(fdtab[fd].thread_mask))
441 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau09f24562012-11-11 16:43:45 +0100442 fdtab[fd].iocb(fd);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200443 }
444 else {
Willy Tarreau5be2f352014-11-19 19:43:05 +0100445 fd_release_cache_entry(fd);
Willy Tarreau87d54a92018-10-15 09:44:46 +0200446 if (atleast2(fdtab[fd].thread_mask))
447 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200448 }
Willy Tarreau09f24562012-11-11 16:43:45 +0100449 }
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100450}
451
452/* Scan and process the cached events. This should be called right after
453 * the poller. The loop may cause new entries to be created, for example
454 * if a listener causes an accept() to initiate a new incoming connection
455 * wanting to attempt an recv().
456 */
457void fd_process_cached_events()
458{
459 HA_ATOMIC_AND(&fd_cache_mask, ~tid_bit);
460 fdlist_process_cached_events(&fd_cache_local[tid]);
461 fdlist_process_cached_events(&fd_cache);
Willy Tarreau09f24562012-11-11 16:43:45 +0100462}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100464/* This is a portable implementation of closefrom(). It closes all open file
465 * descriptors starting at <start> and above. This is a naive version for use
466 * when the operating system provides no alternative.
467 */
468void my_closefrom(int start)
469{
470 struct rlimit limit;
471 int nbfds;
472
473 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
474 nbfds = limit.rlim_cur;
475 else
476 nbfds = 0;
477
478 if (nbfds <= 0)
479 nbfds = 1024; /* safe limit */
480
481 while (start < nbfds)
482 close(start++);
483}
484
Willy Tarreau4f60f162007-04-08 16:39:58 +0200485/* disable the specified poller */
486void disable_poller(const char *poller_name)
487{
488 int p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200489
Willy Tarreau4f60f162007-04-08 16:39:58 +0200490 for (p = 0; p < nbpollers; p++)
491 if (strcmp(pollers[p].name, poller_name) == 0)
492 pollers[p].pref = 0;
493}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494
Olivier Houchard79321b92018-07-26 17:55:11 +0200495void poller_pipe_io_handler(int fd)
496{
497 char buf[1024];
498 /* Flush the pipe */
499 while (read(fd, buf, sizeof(buf)) > 0);
500 fd_cant_recv(fd);
501}
502
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200503/* Initialize the pollers per thread */
504static int init_pollers_per_thread()
505{
Olivier Houchard79321b92018-07-26 17:55:11 +0200506 int mypipe[2];
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200507 if ((fd_updt = calloc(global.maxsock, sizeof(*fd_updt))) == NULL)
508 return 0;
Olivier Houchard79321b92018-07-26 17:55:11 +0200509 if (pipe(mypipe) < 0) {
510 free(fd_updt);
511 fd_updt = NULL;
512 return 0;
513 }
514 poller_rd_pipe = mypipe[0];
515 poller_wr_pipe[tid] = mypipe[1];
516 fcntl(poller_rd_pipe, F_SETFL, O_NONBLOCK);
517 fd_insert(poller_rd_pipe, poller_pipe_io_handler, poller_pipe_io_handler,
518 tid_bit);
519 fd_want_recv(poller_rd_pipe);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200520 return 1;
521}
522
523/* Deinitialize the pollers per thread */
524static void deinit_pollers_per_thread()
525{
526 free(fd_updt);
527 fd_updt = NULL;
William Lallemand808e1b72018-12-15 22:34:31 +0100528
529 /* rd and wr are init at the same place, but only rd is init to -1, so
530 we rely to rd to close. */
531 if (poller_rd_pipe > -1) {
532 close(poller_rd_pipe);
533 poller_rd_pipe = -1;
534 close(poller_wr_pipe[tid]);
535 poller_wr_pipe[tid] = -1;
536 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200537}
538
Willy Tarreaubaaee002006-06-26 02:48:02 +0200539/*
Willy Tarreau4f60f162007-04-08 16:39:58 +0200540 * Initialize the pollers till the best one is found.
541 * If none works, returns 0, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200543int init_pollers()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544{
Willy Tarreau4f60f162007-04-08 16:39:58 +0200545 int p;
546 struct poller *bp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547
Christopher Faulet63fe6522017-08-31 17:52:09 +0200548 if ((fdtab = calloc(global.maxsock, sizeof(struct fdtab))) == NULL)
549 goto fail_tab;
550
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200551 if ((polled_mask = calloc(global.maxsock, sizeof(unsigned long))) == NULL)
552 goto fail_polledmask;
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000553
Christopher Faulet63fe6522017-08-31 17:52:09 +0200554 if ((fdinfo = calloc(global.maxsock, sizeof(struct fdinfo))) == NULL)
555 goto fail_info;
556
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100557 fd_cache.first = fd_cache.last = -1;
Olivier Houchard6b96f722018-04-25 16:58:25 +0200558 update_list.first = update_list.last = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200559
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100560 for (p = 0; p < global.maxsock; p++) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100561 HA_SPIN_INIT(&fdtab[p].lock);
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100562 /* Mark the fd as out of the fd cache */
Olivier Houchard12568362018-01-31 18:07:29 +0100563 fdtab[p].cache.next = -3;
Olivier Houchard6b96f722018-04-25 16:58:25 +0200564 fdtab[p].update.next = -3;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100565 }
566 for (p = 0; p < global.nbthread; p++)
567 fd_cache_local[p].first = fd_cache_local[p].last = -1;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200568
Willy Tarreau4f60f162007-04-08 16:39:58 +0200569 do {
570 bp = NULL;
571 for (p = 0; p < nbpollers; p++)
572 if (!bp || (pollers[p].pref > bp->pref))
573 bp = &pollers[p];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200574
Willy Tarreau4f60f162007-04-08 16:39:58 +0200575 if (!bp || bp->pref == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200576 break;
577
Willy Tarreau4f60f162007-04-08 16:39:58 +0200578 if (bp->init(bp)) {
579 memcpy(&cur_poller, bp, sizeof(*bp));
580 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200581 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200582 } while (!bp || bp->pref == 0);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100583
Christopher Faulet63fe6522017-08-31 17:52:09 +0200584 free(fdinfo);
585 fail_info:
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200586 free(polled_mask);
587 fail_polledmask:
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000588 free(fdtab);
589 fail_tab:
Willy Tarreau7be79a42012-11-11 15:02:54 +0100590 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200591}
592
Willy Tarreaubaaee002006-06-26 02:48:02 +0200593/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200594 * Deinitialize the pollers.
595 */
596void deinit_pollers() {
597
598 struct poller *bp;
599 int p;
600
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200601 for (p = 0; p < global.maxsock; p++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100602 HA_SPIN_DESTROY(&fdtab[p].lock);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200603
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200604 for (p = 0; p < nbpollers; p++) {
605 bp = &pollers[p];
606
607 if (bp && bp->pref)
608 bp->term(bp);
609 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200610
Christopher Faulet63fe6522017-08-31 17:52:09 +0200611 free(fdinfo); fdinfo = NULL;
612 free(fdtab); fdtab = NULL;
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200613 free(polled_mask); polled_mask = NULL;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200614}
615
616/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200617 * Lists the known pollers on <out>.
618 * Should be performed only before initialization.
619 */
620int list_pollers(FILE *out)
621{
622 int p;
623 int last, next;
624 int usable;
625 struct poller *bp;
626
627 fprintf(out, "Available polling systems :\n");
628
629 usable = 0;
630 bp = NULL;
631 last = next = -1;
632 while (1) {
633 for (p = 0; p < nbpollers; p++) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200634 if ((next < 0 || pollers[p].pref > next)
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100635 && (last < 0 || pollers[p].pref < last)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200636 next = pollers[p].pref;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100637 if (!bp || (pollers[p].pref > bp->pref))
638 bp = &pollers[p];
639 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200640 }
641
642 if (next == -1)
643 break;
644
645 for (p = 0; p < nbpollers; p++) {
646 if (pollers[p].pref == next) {
647 fprintf(out, " %10s : ", pollers[p].name);
648 if (pollers[p].pref == 0)
649 fprintf(out, "disabled, ");
650 else
651 fprintf(out, "pref=%3d, ", pollers[p].pref);
652 if (pollers[p].test(&pollers[p])) {
653 fprintf(out, " test result OK");
654 if (next > 0)
655 usable++;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100656 } else {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200657 fprintf(out, " test result FAILED");
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100658 if (bp == &pollers[p])
659 bp = NULL;
660 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200661 fprintf(out, "\n");
662 }
663 }
664 last = next;
665 next = -1;
666 };
667 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
668 return 0;
669}
670
671/*
672 * Some pollers may lose their connection after a fork(). It may be necessary
673 * to create initialize part of them again. Returns 0 in case of failure,
674 * otherwise 1. The fork() function may be NULL if unused. In case of error,
675 * the the current poller is destroyed and the caller is responsible for trying
676 * another one by calling init_pollers() again.
677 */
678int fork_poller()
679{
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200680 int fd;
Willy Tarreauce036bc2018-01-29 14:58:02 +0100681 for (fd = 0; fd < global.maxsock; fd++) {
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200682 if (fdtab[fd].owner) {
683 fdtab[fd].cloned = 1;
684 }
685 }
686
Willy Tarreau2ff76222007-04-09 19:29:56 +0200687 if (cur_poller.fork) {
688 if (cur_poller.fork(&cur_poller))
689 return 1;
690 cur_poller.term(&cur_poller);
691 return 0;
692 }
693 return 1;
694}
695
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100696REGISTER_PER_THREAD_INIT(init_pollers_per_thread);
697REGISTER_PER_THREAD_DEINIT(deinit_pollers_per_thread);
698
Willy Tarreau2ff76222007-04-09 19:29:56 +0200699/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700 * Local variables:
701 * c-indent-level: 8
702 * c-basic-offset: 8
703 * End:
704 */