blob: b028b4cadc43ba0915e2be51d6bf8c7f6484b83d [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 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010017 * The event state for an FD, as found in fdtab[].state, is maintained for each
18 * direction. The state field is built this way, with R bits in the low nibble
19 * and W bits in the high nibble for ease of access and debugging :
Willy Tarreau7be79a42012-11-11 15:02:54 +010020 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010021 * 7 6 5 4 3 2 1 0
Willy Tarreau5bee3e22019-09-04 09:52:57 +020022 * [ 0 | 0 | RW | AW | 0 | 0 | RR | AR ]
Willy Tarreauf817e9f2014-01-10 16:58:45 +010023 *
24 * A* = active *R = read
Willy Tarreau5bee3e22019-09-04 09:52:57 +020025 * R* = ready *W = write
Willy Tarreauf817e9f2014-01-10 16:58:45 +010026 *
27 * An FD is marked "active" when there is a desire to use it.
Willy Tarreauf817e9f2014-01-10 16:58:45 +010028 * An FD is marked "ready" when it has not faced a new EAGAIN since last wake-up
Willy Tarreau5bee3e22019-09-04 09:52:57 +020029 * (it is a cache of the last EAGAIN regardless of polling changes). Each poller
30 * has its own "polled" state for the same fd, as stored in the polled_mask.
Willy Tarreau7be79a42012-11-11 15:02:54 +010031 *
Willy Tarreau5bee3e22019-09-04 09:52:57 +020032 * We have 4 possible states for each direction based on these 2 flags :
Willy Tarreau7be79a42012-11-11 15:02:54 +010033 *
Willy Tarreau5bee3e22019-09-04 09:52:57 +020034 * +---+---+----------+---------------------------------------------+
35 * | R | A | State | Description |
36 * +---+---+----------+---------------------------------------------+
37 * | 0 | 0 | DISABLED | No activity desired, not ready. |
38 * | 0 | 1 | ACTIVE | Activity desired. |
39 * | 1 | 0 | STOPPED | End of activity. |
40 * | 1 | 1 | READY | Activity desired and reported. |
41 * +---+---+----------+---------------------------------------------+
Willy Tarreau7be79a42012-11-11 15:02:54 +010042 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010043 * The transitions are pretty simple :
44 * - fd_want_*() : set flag A
45 * - fd_stop_*() : clear flag A
46 * - fd_cant_*() : clear flag R (when facing EAGAIN)
47 * - fd_may_*() : set flag R (upon return from poll())
Willy Tarreauf817e9f2014-01-10 16:58:45 +010048 *
Willy Tarreau5bee3e22019-09-04 09:52:57 +020049 * Each poller then computes its own polled state :
50 * if (A) { if (!R) P := 1 } else { P := 0 }
Willy Tarreauf817e9f2014-01-10 16:58:45 +010051 *
Willy Tarreau5bee3e22019-09-04 09:52:57 +020052 * The state transitions look like the diagram below.
Willy Tarreauf817e9f2014-01-10 16:58:45 +010053 *
Willy Tarreau5bee3e22019-09-04 09:52:57 +020054 * may +----------+
55 * ,----| DISABLED | (READY=0, ACTIVE=0)
56 * | +----------+
57 * | want | ^
58 * | | |
59 * | v | stop
60 * | +----------+
61 * | | ACTIVE | (READY=0, ACTIVE=1)
62 * | +----------+
63 * | | ^
64 * | may | |
65 * | v | EAGAIN (cant)
66 * | +--------+
67 * | | READY | (READY=1, ACTIVE=1)
68 * | +--------+
69 * | stop | ^
70 * | | |
71 * | v | want
72 * | +---------+
73 * `--->| STOPPED | (READY=1, ACTIVE=0)
74 * +---------+
Willy Tarreaubaaee002006-06-26 02:48:02 +020075 */
76
Willy Tarreau2ff76222007-04-09 19:29:56 +020077#include <stdio.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020078#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020079#include <unistd.h>
Olivier Houchard79321b92018-07-26 17:55:11 +020080#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020081#include <sys/types.h>
Willy Tarreau2d7f81b2019-02-21 22:19:17 +010082#include <sys/resource.h>
Willy Tarreau931d8b72019-08-27 11:08:17 +020083#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020084
Willy Tarreaue5733232019-05-22 19:24:06 +020085#if defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +010086#include <poll.h>
87#include <errno.h>
88#endif
89
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020090#include <haproxy/api.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020091#include <haproxy/fd.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020092#include <haproxy/global.h>
Willy Tarreaufc8f6a82020-06-03 19:20:59 +020093#include <haproxy/port_range.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020094
Willy Tarreaub2551052020-06-09 09:07:15 +020095
Willy Tarreaubaaee002006-06-26 02:48:02 +020096struct fdtab *fdtab = NULL; /* array of all the file descriptors */
Olivier Houchard53055052019-07-25 14:00:18 +000097struct polled_mask *polled_mask = NULL; /* Array for the polled_mask of each fd */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +020098struct fdinfo *fdinfo = NULL; /* less-often used infos for file descriptors */
Willy Tarreaubaaee002006-06-26 02:48:02 +020099int totalconn; /* total # of terminated sessions */
100int actconn; /* # of active sessions */
101
Willy Tarreau4f60f162007-04-08 16:39:58 +0200102struct poller pollers[MAX_POLLERS];
103struct poller cur_poller;
104int nbpollers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105
Olivier Houchard6b96f722018-04-25 16:58:25 +0200106volatile struct fdlist update_list; // Global update list
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100107
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200108THREAD_LOCAL int *fd_updt = NULL; // FD updates list
109THREAD_LOCAL int fd_nbupdt = 0; // number of updates in the list
Olivier Houchard79321b92018-07-26 17:55:11 +0200110THREAD_LOCAL int poller_rd_pipe = -1; // Pipe to wake the thread
111int poller_wr_pipe[MAX_THREADS]; // Pipe to wake the threads
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200112
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200113volatile int ha_used_fds = 0; // Number of FD we're currently using
114
Willy Tarreau337fb712019-12-20 07:20:00 +0100115#define _GET_NEXT(fd, off) ((volatile struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->next
116#define _GET_PREV(fd, off) ((volatile struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->prev
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100117/* adds fd <fd> to fd list <list> if it was not yet in it */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200118void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off)
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100119{
120 int next;
121 int new;
122 int old;
123 int last;
124
125redo_next:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200126 next = _GET_NEXT(fd, off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100127 /* Check that we're not already in the cache, and if not, lock us. */
Olivier Houchardfc51f0f2019-12-19 18:33:08 +0100128 if (next > -2)
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100129 goto done;
Olivier Houchardfc51f0f2019-12-19 18:33:08 +0100130 if (next == -2)
131 goto redo_next;
Olivier Houchardd3608792019-03-08 18:47:42 +0100132 if (!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100133 goto redo_next;
Olivier Houchardd2b5d162019-03-08 13:47:21 +0100134 __ha_barrier_atomic_store();
Willy Tarreau11559a72018-02-05 17:52:24 +0100135
136 new = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100137redo_last:
138 /* First, insert in the linked list */
139 last = list->last;
140 old = -1;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100141
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200142 _GET_PREV(fd, off) = -2;
Willy Tarreau11559a72018-02-05 17:52:24 +0100143 /* Make sure the "prev" store is visible before we update the last entry */
144 __ha_barrier_store();
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100145
Willy Tarreau11559a72018-02-05 17:52:24 +0100146 if (unlikely(last == -1)) {
147 /* list is empty, try to add ourselves alone so that list->last=fd */
Olivier Houchardd3608792019-03-08 18:47:42 +0100148 if (unlikely(!_HA_ATOMIC_CAS(&list->last, &old, new)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100149 goto redo_last;
150
151 /* list->first was necessary -1, we're guaranteed to be alone here */
152 list->first = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100153 } else {
Willy Tarreau11559a72018-02-05 17:52:24 +0100154 /* adding ourselves past the last element
155 * The CAS will only succeed if its next is -1,
156 * which means it's in the cache, and the last element.
157 */
Olivier Houchardd3608792019-03-08 18:47:42 +0100158 if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(last, off), &old, new)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100159 goto redo_last;
Willy Tarreau11559a72018-02-05 17:52:24 +0100160
161 /* Then, update the last entry */
162 list->last = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100163 }
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100164 __ha_barrier_store();
Willy Tarreau11559a72018-02-05 17:52:24 +0100165 /* since we're alone at the end of the list and still locked(-2),
166 * we know noone tried to add past us. Mark the end of list.
167 */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200168 _GET_PREV(fd, off) = last;
169 _GET_NEXT(fd, off) = -1;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100170 __ha_barrier_store();
171done:
172 return;
173}
174
175/* removes fd <fd> from fd list <list> */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200176void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off)
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100177{
178#if defined(HA_HAVE_CAS_DW) || defined(HA_CAS_IS_8B)
Willy Tarreau2b9f0662020-02-25 09:25:53 +0100179 volatile union {
180 struct fdlist_entry ent;
181 uint64_t u64;
182 uint32_t u32[2];
183 } cur_list, next_list;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100184#endif
185 int old;
186 int new = -2;
187 int prev;
188 int next;
189 int last;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100190lock_self:
191#if (defined(HA_CAS_IS_8B) || defined(HA_HAVE_CAS_DW))
Willy Tarreau2b9f0662020-02-25 09:25:53 +0100192 next_list.ent.next = next_list.ent.prev = -2;
193 cur_list.ent = *(volatile struct fdlist_entry *)(((char *)&fdtab[fd]) + off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100194 /* First, attempt to lock our own entries */
195 do {
196 /* The FD is not in the FD cache, give up */
Willy Tarreau2b9f0662020-02-25 09:25:53 +0100197 if (unlikely(cur_list.ent.next <= -3))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100198 return;
Willy Tarreau2b9f0662020-02-25 09:25:53 +0100199 if (unlikely(cur_list.ent.prev == -2 || cur_list.ent.next == -2))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100200 goto lock_self;
201 } while (
202#ifdef HA_CAS_IS_8B
Willy Tarreau2b9f0662020-02-25 09:25:53 +0100203 unlikely(!_HA_ATOMIC_CAS(((uint64_t *)&_GET_NEXT(fd, off)), (uint64_t *)&cur_list.u64, next_list.u64))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100204#else
Willy Tarreau2b9f0662020-02-25 09:25:53 +0100205 unlikely(!_HA_ATOMIC_DWCAS(((long *)&_GET_NEXT(fd, off)), (uint32_t *)&cur_list.u32, &next_list.u32))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100206#endif
Willy Tarreau2b9f0662020-02-25 09:25:53 +0100207 );
208 next = cur_list.ent.next;
209 prev = cur_list.ent.prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100210
211#else
212lock_self_next:
Willy Tarreau337fb712019-12-20 07:20:00 +0100213 next = _GET_NEXT(fd, off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100214 if (next == -2)
215 goto lock_self_next;
216 if (next <= -3)
217 goto done;
Olivier Houchardd3608792019-03-08 18:47:42 +0100218 if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100219 goto lock_self_next;
220lock_self_prev:
Willy Tarreau337fb712019-12-20 07:20:00 +0100221 prev = _GET_PREV(fd, off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100222 if (prev == -2)
223 goto lock_self_prev;
Olivier Houchardd3608792019-03-08 18:47:42 +0100224 if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(fd, off), &prev, -2)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100225 goto lock_self_prev;
226#endif
Olivier Houchardd2b5d162019-03-08 13:47:21 +0100227 __ha_barrier_atomic_store();
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100228
229 /* Now, lock the entries of our neighbours */
230 if (likely(prev != -1)) {
231redo_prev:
232 old = fd;
233
Olivier Houchardd3608792019-03-08 18:47:42 +0100234 if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(prev, off), &old, new))) {
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100235 if (unlikely(old == -2)) {
236 /* Neighbour already locked, give up and
237 * retry again once he's done
238 */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200239 _GET_PREV(fd, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100240 __ha_barrier_store();
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200241 _GET_NEXT(fd, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100242 __ha_barrier_store();
243 goto lock_self;
244 }
245 goto redo_prev;
246 }
247 }
248 if (likely(next != -1)) {
249redo_next:
250 old = fd;
Olivier Houchardd3608792019-03-08 18:47:42 +0100251 if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(next, off), &old, new))) {
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100252 if (unlikely(old == -2)) {
253 /* Neighbour already locked, give up and
254 * retry again once he's done
255 */
256 if (prev != -1) {
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200257 _GET_NEXT(prev, off) = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100258 __ha_barrier_store();
259 }
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200260 _GET_PREV(fd, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100261 __ha_barrier_store();
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200262 _GET_NEXT(fd, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100263 __ha_barrier_store();
264 goto lock_self;
265 }
266 goto redo_next;
267 }
268 }
269 if (list->first == fd)
270 list->first = next;
271 __ha_barrier_store();
272 last = list->last;
Olivier Houchardd3608792019-03-08 18:47:42 +0100273 while (unlikely(last == fd && (!_HA_ATOMIC_CAS(&list->last, &last, prev))))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100274 __ha_compiler_barrier();
275 /* Make sure we let other threads know we're no longer in cache,
276 * before releasing our neighbours.
277 */
278 __ha_barrier_store();
279 if (likely(prev != -1))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200280 _GET_NEXT(prev, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100281 __ha_barrier_store();
282 if (likely(next != -1))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200283 _GET_PREV(next, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100284 __ha_barrier_store();
285 /* Ok, now we're out of the fd cache */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200286 _GET_NEXT(fd, off) = -(next + 4);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100287 __ha_barrier_store();
288done:
289 return;
290}
291
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200292#undef _GET_NEXT
293#undef _GET_PREV
294
Willy Tarreau173d9952018-01-26 21:48:23 +0100295/* Deletes an FD from the fdsets.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200296 * The file descriptor is also closed.
297 */
Olivier Houchard1fc05162017-04-06 01:05:05 +0200298static void fd_dodelete(int fd, int do_close)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299{
Olivier Houcharda7bf5732020-02-27 17:26:13 +0100300 int locked = fdtab[fd].running_mask != tid_bit;
Willy Tarreau87d54a92018-10-15 09:44:46 +0200301
Olivier Houchard88516642020-03-05 18:10:51 +0100302 /* We're just trying to protect against a concurrent fd_insert()
Olivier Houchardf86a1062020-06-17 20:28:27 +0200303 * here, not against fd_takeover(), because either we're called
Olivier Houchard88516642020-03-05 18:10:51 +0100304 * directly from the iocb(), and we're already locked, or we're
305 * called from the mux tasklet, but then the mux is responsible for
306 * making sure the tasklet does nothing, and the connection is never
307 * destroyed.
308 */
Willy Tarreau87d54a92018-10-15 09:44:46 +0200309 if (locked)
Olivier Houcharda7bf5732020-02-27 17:26:13 +0100310 fd_set_running_excl(fd);
311
Willy Tarreauad38ace2013-12-15 14:19:38 +0100312 if (fdtab[fd].linger_risk) {
313 /* this is generally set when connecting to servers */
Ilya Shipitsinb7e43f02020-04-02 15:02:08 +0500314 DISGUISE(setsockopt(fd, SOL_SOCKET, SO_LINGER,
315 (struct linger *) &nolinger, sizeof(struct linger)));
Willy Tarreauad38ace2013-12-15 14:19:38 +0100316 }
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100317 if (cur_poller.clo)
318 cur_poller.clo(fd);
Olivier Houchardc22580c2019-08-05 18:51:52 +0200319 polled_mask[fd].poll_recv = polled_mask[fd].poll_send = 0;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100320
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100321 fdtab[fd].state = 0;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100322
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200323 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
324 fdinfo[fd].port_range = NULL;
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200325 fdtab[fd].owner = NULL;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100326 fdtab[fd].thread_mask = 0;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100327 if (do_close) {
Christopher Fauletd531f882017-06-01 16:55:03 +0200328 close(fd);
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200329 _HA_ATOMIC_SUB(&ha_used_fds, 1);
Willy Tarreauc9c83782018-01-17 18:44:46 +0100330 }
Willy Tarreau87d54a92018-10-15 09:44:46 +0200331 if (locked)
Olivier Houcharda7bf5732020-02-27 17:26:13 +0100332 fd_clr_running(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334
Olivier Houchard88516642020-03-05 18:10:51 +0100335#ifndef HA_HAVE_CAS_DW
Willy Tarreauaf613e82020-06-05 08:40:51 +0200336__decl_thread(__decl_rwlock(fd_mig_lock));
Olivier Houchard88516642020-03-05 18:10:51 +0100337#endif
338
339/*
340 * Take over a FD belonging to another thread.
341 * unexpected_conn is the expected owner of the fd.
342 * Returns 0 on success, and -1 on failure.
343 */
344int fd_takeover(int fd, void *expected_owner)
345{
346#ifndef HA_HAVE_CAS_DW
Willy Tarreauc460c912020-06-18 07:28:09 +0200347 int ret = -1;
Olivier Houchard88516642020-03-05 18:10:51 +0100348
Willy Tarreauc460c912020-06-18 07:28:09 +0200349 if (_HA_ATOMIC_OR(&fdtab[fd].running_mask, tid_bit) == tid_bit) {
350 HA_RWLOCK_WRLOCK(OTHER_LOCK, &fd_mig_lock);
351 if (fdtab[fd].owner == expected_owner) {
352 fdtab[fd].thread_mask = tid_bit;
353 ret = 0;
354 }
355 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &fd_mig_lock);
Olivier Houchard88516642020-03-05 18:10:51 +0100356 }
Willy Tarreauc460c912020-06-18 07:28:09 +0200357
Olivier Houchard88516642020-03-05 18:10:51 +0100358 _HA_ATOMIC_AND(&fdtab[fd].running_mask, ~tid_bit);
Olivier Houchard88516642020-03-05 18:10:51 +0100359 /* Make sure the FD doesn't have the active bit. It is possible that
360 * the fd is polled by the thread that used to own it, the new thread
361 * is supposed to call subscribe() later, to activate polling.
362 */
Olivier Houchard8d7b5172020-06-17 20:32:34 +0200363 if (ret != -1)
364 fd_stop_recv(fd);
Olivier Houchard88516642020-03-05 18:10:51 +0100365 return ret;
366#else
367 unsigned long old_masks[2];
368 unsigned long new_masks[2];
369
Olivier Houchard88516642020-03-05 18:10:51 +0100370 new_masks[0] = new_masks[1] = tid_bit;
Willy Tarreau42973632020-06-18 08:05:15 +0200371
372 old_masks[0] = _HA_ATOMIC_OR(&fdtab[fd].running_mask, tid_bit);
373 old_masks[1] = fdtab[fd].thread_mask;
374
Olivier Houchard88516642020-03-05 18:10:51 +0100375 /* protect ourself against a delete then an insert for the same fd,
376 * if it happens, then the owner will no longer be the expected
377 * connection.
378 */
Olivier Houchard88516642020-03-05 18:10:51 +0100379 if (fdtab[fd].owner != expected_owner) {
380 _HA_ATOMIC_AND(&fdtab[fd].running_mask, ~tid_bit);
381 return -1;
382 }
383 do {
384 if (old_masks[0] != tid_bit || !old_masks[1]) {
385 _HA_ATOMIC_AND(&fdtab[fd].running_mask, ~tid_bit);
386 return -1;
387 }
388 } while (!(_HA_ATOMIC_DWCAS(&fdtab[fd].running_mask, &old_masks,
389 &new_masks)));
390 _HA_ATOMIC_AND(&fdtab[fd].running_mask, ~tid_bit);
Olivier Houchardddc874c2020-06-17 20:34:05 +0200391 /* Make sure the FD doesn't have the active bit. It is possible that
392 * the fd is polled by the thread that used to own it, the new thread
393 * is supposed to call subscribe() later, to activate polling.
394 */
395 fd_stop_recv(fd);
396
Olivier Houchard88516642020-03-05 18:10:51 +0100397 return 0;
398#endif /* HW_HAVE_CAS_DW */
399}
400
Willy Tarreau173d9952018-01-26 21:48:23 +0100401/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200402 * The file descriptor is also closed.
403 */
404void fd_delete(int fd)
405{
406 fd_dodelete(fd, 1);
407}
408
Willy Tarreau173d9952018-01-26 21:48:23 +0100409/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200410 * The file descriptor is kept open.
411 */
412void fd_remove(int fd)
413{
414 fd_dodelete(fd, 0);
415}
416
Willy Tarreaudbe30602019-09-04 13:25:41 +0200417void updt_fd_polling(const int fd)
418{
419 if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) {
420
421 /* note: we don't have a test-and-set yet in hathreads */
422
423 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
424 return;
425
426 fd_updt[fd_nbupdt++] = fd;
427 } else {
428 unsigned long update_mask = fdtab[fd].update_mask;
429 do {
430 if (update_mask == fdtab[fd].thread_mask)
431 return;
432 } while (!_HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask,
433 fdtab[fd].thread_mask));
434 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
435 }
436}
437
Olivier Houchard7fa55622020-02-27 17:25:43 +0100438__decl_spinlock(log_lock);
Willy Tarreau931d8b72019-08-27 11:08:17 +0200439/* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg>
440 * optionally followed by a newline if <nl> is non-null, to file descriptor
441 * <fd>. The message is sent atomically using writev(). It may be truncated to
442 * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the
443 * two lists, it's just a convenience to help the caller prepend some prefixes
444 * when necessary. It takes the fd's lock to make sure no other thread will
445 * write to the same fd in parallel. Returns the number of bytes sent, or <=0
446 * on failure. A limit to 31 total non-empty segments is enforced. The caller
447 * is responsible for taking care of making the fd non-blocking.
448 */
449ssize_t fd_write_frag_line(int fd, size_t maxlen, const struct ist pfx[], size_t npfx, const struct ist msg[], size_t nmsg, int nl)
450{
451 struct iovec iovec[32];
452 size_t totlen = 0;
453 size_t sent = 0;
454 int vec = 0;
Willy Tarreaudf187872020-06-11 14:25:47 +0200455 int attempts = 0;
Willy Tarreau931d8b72019-08-27 11:08:17 +0200456
457 if (!maxlen)
458 maxlen = ~0;
459
460 /* keep one char for a possible trailing '\n' in any case */
461 maxlen--;
462
463 /* make an iovec from the concatenation of all parts of the original
464 * message. Skip empty fields and truncate the whole message to maxlen,
465 * leaving one spare iovec for the '\n'.
466 */
467 while (vec < (sizeof(iovec) / sizeof(iovec[0]) - 1)) {
468 if (!npfx) {
469 pfx = msg;
470 npfx = nmsg;
471 nmsg = 0;
472 if (!npfx)
473 break;
474 }
475
476 iovec[vec].iov_base = pfx->ptr;
477 iovec[vec].iov_len = MIN(maxlen, pfx->len);
478 maxlen -= iovec[vec].iov_len;
479 totlen += iovec[vec].iov_len;
480 if (iovec[vec].iov_len)
481 vec++;
482 pfx++; npfx--;
483 };
484
485 if (nl) {
486 iovec[vec].iov_base = "\n";
487 iovec[vec].iov_len = 1;
488 vec++;
489 }
490
Willy Tarreaudf187872020-06-11 14:25:47 +0200491 /* make sure we never interleave writes and we never block. This means
492 * we prefer to fail on collision than to block. But we don't want to
493 * lose too many logs so we just perform a few lock attempts then give
494 * up.
495 */
496
497 while (HA_SPIN_TRYLOCK(OTHER_LOCK, &log_lock) != 0) {
498 if (++attempts >= 200) {
499 /* so that the caller knows the message couldn't be delivered */
500 sent = -1;
501 errno = EAGAIN;
502 goto leave;
503 }
504 ha_thread_relax();
505 }
506
Willy Tarreau7e9776a2019-08-30 14:41:47 +0200507 if (unlikely(!fdtab[fd].initialized)) {
508 fdtab[fd].initialized = 1;
509 if (!isatty(fd))
510 fcntl(fd, F_SETFL, O_NONBLOCK);
511 }
Willy Tarreau931d8b72019-08-27 11:08:17 +0200512 sent = writev(fd, iovec, vec);
Olivier Houchard7fa55622020-02-27 17:25:43 +0100513 HA_SPIN_UNLOCK(OTHER_LOCK, &log_lock);
Willy Tarreau931d8b72019-08-27 11:08:17 +0200514
Willy Tarreaudf187872020-06-11 14:25:47 +0200515 leave:
Willy Tarreau931d8b72019-08-27 11:08:17 +0200516 /* sent > 0 if the message was delivered */
517 return sent;
518}
519
Olivier Houchard2292edf2019-02-25 14:26:54 +0100520#if defined(USE_CLOSEFROM)
521void my_closefrom(int start)
522{
523 closefrom(start);
524}
525
Willy Tarreaue5733232019-05-22 19:24:06 +0200526#elif defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100527/* This is a portable implementation of closefrom(). It closes all open file
528 * descriptors starting at <start> and above. It relies on the fact that poll()
529 * will return POLLNVAL for each invalid (hence close) file descriptor passed
530 * in argument in order to skip them. It acts with batches of FDs and will
531 * typically perform one poll() call per 1024 FDs so the overhead is low in
532 * case all FDs have to be closed.
533 */
534void my_closefrom(int start)
535{
536 struct pollfd poll_events[1024];
537 struct rlimit limit;
538 int nbfds, fd, ret, idx;
539 int step, next;
540
541 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
542 step = nbfds = limit.rlim_cur;
543 else
544 step = nbfds = 0;
545
546 if (nbfds <= 0) {
547 /* set safe limit */
548 nbfds = 1024;
549 step = 256;
550 }
551
552 if (step > sizeof(poll_events) / sizeof(poll_events[0]))
553 step = sizeof(poll_events) / sizeof(poll_events[0]);
554
555 while (start < nbfds) {
556 next = (start / step + 1) * step;
557
558 for (fd = start; fd < next && fd < nbfds; fd++) {
559 poll_events[fd - start].fd = fd;
560 poll_events[fd - start].events = 0;
561 }
562
563 do {
564 ret = poll(poll_events, fd - start, 0);
565 if (ret >= 0)
566 break;
567 } while (errno == EAGAIN || errno == EINTR || errno == ENOMEM);
568
Willy Tarreaub8e602c2019-02-22 09:07:42 +0100569 if (ret)
570 ret = fd - start;
571
Willy Tarreau9188ac62019-02-21 22:12:47 +0100572 for (idx = 0; idx < ret; idx++) {
573 if (poll_events[idx].revents & POLLNVAL)
574 continue; /* already closed */
575
576 fd = poll_events[idx].fd;
577 close(fd);
578 }
579 start = next;
580 }
581}
582
Willy Tarreaue5733232019-05-22 19:24:06 +0200583#else // defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100584
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100585/* This is a portable implementation of closefrom(). It closes all open file
586 * descriptors starting at <start> and above. This is a naive version for use
587 * when the operating system provides no alternative.
588 */
589void my_closefrom(int start)
590{
591 struct rlimit limit;
592 int nbfds;
593
594 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
595 nbfds = limit.rlim_cur;
596 else
597 nbfds = 0;
598
599 if (nbfds <= 0)
600 nbfds = 1024; /* safe limit */
601
602 while (start < nbfds)
603 close(start++);
604}
Willy Tarreaue5733232019-05-22 19:24:06 +0200605#endif // defined(USE_POLL)
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100606
Willy Tarreau4f60f162007-04-08 16:39:58 +0200607/* disable the specified poller */
608void disable_poller(const char *poller_name)
609{
610 int p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611
Willy Tarreau4f60f162007-04-08 16:39:58 +0200612 for (p = 0; p < nbpollers; p++)
613 if (strcmp(pollers[p].name, poller_name) == 0)
614 pollers[p].pref = 0;
615}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200616
Olivier Houchard79321b92018-07-26 17:55:11 +0200617void poller_pipe_io_handler(int fd)
618{
619 char buf[1024];
620 /* Flush the pipe */
621 while (read(fd, buf, sizeof(buf)) > 0);
622 fd_cant_recv(fd);
623}
624
Willy Tarreau082b6282019-05-22 14:42:12 +0200625/* allocate the per-thread fd_updt thus needs to be called early after
626 * thread creation.
627 */
628static int alloc_pollers_per_thread()
629{
630 fd_updt = calloc(global.maxsock, sizeof(*fd_updt));
631 return fd_updt != NULL;
632}
633
634/* Initialize the pollers per thread.*/
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200635static int init_pollers_per_thread()
636{
Olivier Houchard79321b92018-07-26 17:55:11 +0200637 int mypipe[2];
Willy Tarreau082b6282019-05-22 14:42:12 +0200638
639 if (pipe(mypipe) < 0)
Olivier Houchard79321b92018-07-26 17:55:11 +0200640 return 0;
Willy Tarreau082b6282019-05-22 14:42:12 +0200641
Olivier Houchard79321b92018-07-26 17:55:11 +0200642 poller_rd_pipe = mypipe[0];
643 poller_wr_pipe[tid] = mypipe[1];
644 fcntl(poller_rd_pipe, F_SETFL, O_NONBLOCK);
645 fd_insert(poller_rd_pipe, poller_pipe_io_handler, poller_pipe_io_handler,
646 tid_bit);
647 fd_want_recv(poller_rd_pipe);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200648 return 1;
649}
650
651/* Deinitialize the pollers per thread */
652static void deinit_pollers_per_thread()
653{
William Lallemand808e1b72018-12-15 22:34:31 +0100654 /* rd and wr are init at the same place, but only rd is init to -1, so
655 we rely to rd to close. */
656 if (poller_rd_pipe > -1) {
657 close(poller_rd_pipe);
658 poller_rd_pipe = -1;
659 close(poller_wr_pipe[tid]);
660 poller_wr_pipe[tid] = -1;
661 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200662}
663
Willy Tarreau082b6282019-05-22 14:42:12 +0200664/* Release the pollers per thread, to be called late */
665static void free_pollers_per_thread()
666{
667 free(fd_updt);
668 fd_updt = NULL;
669}
670
Willy Tarreaubaaee002006-06-26 02:48:02 +0200671/*
Willy Tarreau4f60f162007-04-08 16:39:58 +0200672 * Initialize the pollers till the best one is found.
673 * If none works, returns 0, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200675int init_pollers()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676{
Willy Tarreau4f60f162007-04-08 16:39:58 +0200677 int p;
678 struct poller *bp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679
Christopher Faulet63fe6522017-08-31 17:52:09 +0200680 if ((fdtab = calloc(global.maxsock, sizeof(struct fdtab))) == NULL)
681 goto fail_tab;
682
Olivier Houchard53055052019-07-25 14:00:18 +0000683 if ((polled_mask = calloc(global.maxsock, sizeof(*polled_mask))) == NULL)
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200684 goto fail_polledmask;
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000685
Christopher Faulet63fe6522017-08-31 17:52:09 +0200686 if ((fdinfo = calloc(global.maxsock, sizeof(struct fdinfo))) == NULL)
687 goto fail_info;
688
Olivier Houchard6b96f722018-04-25 16:58:25 +0200689 update_list.first = update_list.last = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100691 for (p = 0; p < global.maxsock; p++) {
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100692 /* Mark the fd as out of the fd cache */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200693 fdtab[p].update.next = -3;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100694 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200695
Willy Tarreau4f60f162007-04-08 16:39:58 +0200696 do {
697 bp = NULL;
698 for (p = 0; p < nbpollers; p++)
699 if (!bp || (pollers[p].pref > bp->pref))
700 bp = &pollers[p];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701
Willy Tarreau4f60f162007-04-08 16:39:58 +0200702 if (!bp || bp->pref == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200703 break;
704
Willy Tarreau4f60f162007-04-08 16:39:58 +0200705 if (bp->init(bp)) {
706 memcpy(&cur_poller, bp, sizeof(*bp));
707 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200709 } while (!bp || bp->pref == 0);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100710
Christopher Faulet63fe6522017-08-31 17:52:09 +0200711 free(fdinfo);
712 fail_info:
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200713 free(polled_mask);
714 fail_polledmask:
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000715 free(fdtab);
716 fail_tab:
Willy Tarreau7be79a42012-11-11 15:02:54 +0100717 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200718}
719
Willy Tarreaubaaee002006-06-26 02:48:02 +0200720/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200721 * Deinitialize the pollers.
722 */
723void deinit_pollers() {
724
725 struct poller *bp;
726 int p;
727
728 for (p = 0; p < nbpollers; p++) {
729 bp = &pollers[p];
730
731 if (bp && bp->pref)
732 bp->term(bp);
733 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200734
Christopher Faulet63fe6522017-08-31 17:52:09 +0200735 free(fdinfo); fdinfo = NULL;
736 free(fdtab); fdtab = NULL;
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200737 free(polled_mask); polled_mask = NULL;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200738}
739
740/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200741 * Lists the known pollers on <out>.
742 * Should be performed only before initialization.
743 */
744int list_pollers(FILE *out)
745{
746 int p;
747 int last, next;
748 int usable;
749 struct poller *bp;
750
751 fprintf(out, "Available polling systems :\n");
752
753 usable = 0;
754 bp = NULL;
755 last = next = -1;
756 while (1) {
757 for (p = 0; p < nbpollers; p++) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200758 if ((next < 0 || pollers[p].pref > next)
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100759 && (last < 0 || pollers[p].pref < last)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200760 next = pollers[p].pref;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100761 if (!bp || (pollers[p].pref > bp->pref))
762 bp = &pollers[p];
763 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200764 }
765
766 if (next == -1)
767 break;
768
769 for (p = 0; p < nbpollers; p++) {
770 if (pollers[p].pref == next) {
771 fprintf(out, " %10s : ", pollers[p].name);
772 if (pollers[p].pref == 0)
773 fprintf(out, "disabled, ");
774 else
775 fprintf(out, "pref=%3d, ", pollers[p].pref);
776 if (pollers[p].test(&pollers[p])) {
777 fprintf(out, " test result OK");
778 if (next > 0)
779 usable++;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100780 } else {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200781 fprintf(out, " test result FAILED");
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100782 if (bp == &pollers[p])
783 bp = NULL;
784 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200785 fprintf(out, "\n");
786 }
787 }
788 last = next;
789 next = -1;
790 };
791 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
792 return 0;
793}
794
795/*
796 * Some pollers may lose their connection after a fork(). It may be necessary
797 * to create initialize part of them again. Returns 0 in case of failure,
798 * otherwise 1. The fork() function may be NULL if unused. In case of error,
799 * the the current poller is destroyed and the caller is responsible for trying
800 * another one by calling init_pollers() again.
801 */
802int fork_poller()
803{
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200804 int fd;
Willy Tarreauce036bc2018-01-29 14:58:02 +0100805 for (fd = 0; fd < global.maxsock; fd++) {
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200806 if (fdtab[fd].owner) {
807 fdtab[fd].cloned = 1;
808 }
809 }
810
Willy Tarreau2ff76222007-04-09 19:29:56 +0200811 if (cur_poller.fork) {
812 if (cur_poller.fork(&cur_poller))
813 return 1;
814 cur_poller.term(&cur_poller);
815 return 0;
816 }
817 return 1;
818}
819
Willy Tarreau082b6282019-05-22 14:42:12 +0200820REGISTER_PER_THREAD_ALLOC(alloc_pollers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100821REGISTER_PER_THREAD_INIT(init_pollers_per_thread);
822REGISTER_PER_THREAD_DEINIT(deinit_pollers_per_thread);
Willy Tarreau082b6282019-05-22 14:42:12 +0200823REGISTER_PER_THREAD_FREE(free_pollers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100824
Willy Tarreau2ff76222007-04-09 19:29:56 +0200825/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200826 * Local variables:
827 * c-indent-level: 8
828 * c-basic-offset: 8
829 * End:
830 */