blob: 1e1c0cbc5222a353d610647ae75c9c38397906bc [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 Houchardfc51f0f52019-12-19 18:33:08 +0100128 if (next > -2)
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100129 goto done;
Olivier Houchardfc51f0f52019-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{
Willy Tarreauc460c912020-06-18 07:28:09 +0200346 int ret = -1;
Olivier Houchard88516642020-03-05 18:10:51 +0100347
Willy Tarreauf1cad382020-06-18 08:14:59 +0200348#ifndef HA_HAVE_CAS_DW
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 }
Olivier Houchard88516642020-03-05 18:10:51 +0100357#else
358 unsigned long old_masks[2];
359 unsigned long new_masks[2];
360
Olivier Houchard88516642020-03-05 18:10:51 +0100361 new_masks[0] = new_masks[1] = tid_bit;
Willy Tarreau42973632020-06-18 08:05:15 +0200362
363 old_masks[0] = _HA_ATOMIC_OR(&fdtab[fd].running_mask, tid_bit);
364 old_masks[1] = fdtab[fd].thread_mask;
365
Olivier Houchard88516642020-03-05 18:10:51 +0100366 /* protect ourself against a delete then an insert for the same fd,
367 * if it happens, then the owner will no longer be the expected
368 * connection.
369 */
Willy Tarreauf1cad382020-06-18 08:14:59 +0200370 if (fdtab[fd].owner == expected_owner) {
371 while (old_masks[0] == tid_bit && old_masks[1]) {
372 if (_HA_ATOMIC_DWCAS(&fdtab[fd].running_mask, &old_masks, &new_masks)) {
373 ret = 0;
374 break;
375 }
Olivier Houchard88516642020-03-05 18:10:51 +0100376 }
Willy Tarreauf1cad382020-06-18 08:14:59 +0200377 }
378#endif /* HW_HAVE_CAS_DW */
379
Olivier Houchard88516642020-03-05 18:10:51 +0100380 _HA_ATOMIC_AND(&fdtab[fd].running_mask, ~tid_bit);
Willy Tarreauf1cad382020-06-18 08:14:59 +0200381
Olivier Houchardddc874c2020-06-17 20:34:05 +0200382 /* Make sure the FD doesn't have the active bit. It is possible that
383 * the fd is polled by the thread that used to own it, the new thread
384 * is supposed to call subscribe() later, to activate polling.
385 */
Willy Tarreauf1cad382020-06-18 08:14:59 +0200386 if (likely(ret == 0))
387 fd_stop_recv(fd);
388 return ret;
Olivier Houchard88516642020-03-05 18:10:51 +0100389}
390
Willy Tarreau173d9952018-01-26 21:48:23 +0100391/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200392 * The file descriptor is also closed.
393 */
394void fd_delete(int fd)
395{
396 fd_dodelete(fd, 1);
397}
398
Willy Tarreau173d9952018-01-26 21:48:23 +0100399/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200400 * The file descriptor is kept open.
401 */
402void fd_remove(int fd)
403{
404 fd_dodelete(fd, 0);
405}
406
Willy Tarreaudbe30602019-09-04 13:25:41 +0200407void updt_fd_polling(const int fd)
408{
409 if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) {
410
411 /* note: we don't have a test-and-set yet in hathreads */
412
413 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
414 return;
415
416 fd_updt[fd_nbupdt++] = fd;
417 } else {
418 unsigned long update_mask = fdtab[fd].update_mask;
419 do {
420 if (update_mask == fdtab[fd].thread_mask)
421 return;
422 } while (!_HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask,
423 fdtab[fd].thread_mask));
424 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
425 }
426}
427
Olivier Houchard7fa55622020-02-27 17:25:43 +0100428__decl_spinlock(log_lock);
Willy Tarreau931d8b72019-08-27 11:08:17 +0200429/* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg>
430 * optionally followed by a newline if <nl> is non-null, to file descriptor
431 * <fd>. The message is sent atomically using writev(). It may be truncated to
432 * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the
433 * two lists, it's just a convenience to help the caller prepend some prefixes
434 * when necessary. It takes the fd's lock to make sure no other thread will
435 * write to the same fd in parallel. Returns the number of bytes sent, or <=0
436 * on failure. A limit to 31 total non-empty segments is enforced. The caller
437 * is responsible for taking care of making the fd non-blocking.
438 */
439ssize_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)
440{
441 struct iovec iovec[32];
442 size_t totlen = 0;
443 size_t sent = 0;
444 int vec = 0;
Willy Tarreaudf187872020-06-11 14:25:47 +0200445 int attempts = 0;
Willy Tarreau931d8b72019-08-27 11:08:17 +0200446
447 if (!maxlen)
448 maxlen = ~0;
449
450 /* keep one char for a possible trailing '\n' in any case */
451 maxlen--;
452
453 /* make an iovec from the concatenation of all parts of the original
454 * message. Skip empty fields and truncate the whole message to maxlen,
455 * leaving one spare iovec for the '\n'.
456 */
457 while (vec < (sizeof(iovec) / sizeof(iovec[0]) - 1)) {
458 if (!npfx) {
459 pfx = msg;
460 npfx = nmsg;
461 nmsg = 0;
462 if (!npfx)
463 break;
464 }
465
466 iovec[vec].iov_base = pfx->ptr;
467 iovec[vec].iov_len = MIN(maxlen, pfx->len);
468 maxlen -= iovec[vec].iov_len;
469 totlen += iovec[vec].iov_len;
470 if (iovec[vec].iov_len)
471 vec++;
472 pfx++; npfx--;
473 };
474
475 if (nl) {
476 iovec[vec].iov_base = "\n";
477 iovec[vec].iov_len = 1;
478 vec++;
479 }
480
Willy Tarreaudf187872020-06-11 14:25:47 +0200481 /* make sure we never interleave writes and we never block. This means
482 * we prefer to fail on collision than to block. But we don't want to
483 * lose too many logs so we just perform a few lock attempts then give
484 * up.
485 */
486
487 while (HA_SPIN_TRYLOCK(OTHER_LOCK, &log_lock) != 0) {
488 if (++attempts >= 200) {
489 /* so that the caller knows the message couldn't be delivered */
490 sent = -1;
491 errno = EAGAIN;
492 goto leave;
493 }
494 ha_thread_relax();
495 }
496
Willy Tarreau7e9776a2019-08-30 14:41:47 +0200497 if (unlikely(!fdtab[fd].initialized)) {
498 fdtab[fd].initialized = 1;
499 if (!isatty(fd))
500 fcntl(fd, F_SETFL, O_NONBLOCK);
501 }
Willy Tarreau931d8b72019-08-27 11:08:17 +0200502 sent = writev(fd, iovec, vec);
Olivier Houchard7fa55622020-02-27 17:25:43 +0100503 HA_SPIN_UNLOCK(OTHER_LOCK, &log_lock);
Willy Tarreau931d8b72019-08-27 11:08:17 +0200504
Willy Tarreaudf187872020-06-11 14:25:47 +0200505 leave:
Willy Tarreau931d8b72019-08-27 11:08:17 +0200506 /* sent > 0 if the message was delivered */
507 return sent;
508}
509
Olivier Houchard2292edf2019-02-25 14:26:54 +0100510#if defined(USE_CLOSEFROM)
511void my_closefrom(int start)
512{
513 closefrom(start);
514}
515
Willy Tarreaue5733232019-05-22 19:24:06 +0200516#elif defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100517/* This is a portable implementation of closefrom(). It closes all open file
518 * descriptors starting at <start> and above. It relies on the fact that poll()
519 * will return POLLNVAL for each invalid (hence close) file descriptor passed
520 * in argument in order to skip them. It acts with batches of FDs and will
521 * typically perform one poll() call per 1024 FDs so the overhead is low in
522 * case all FDs have to be closed.
523 */
524void my_closefrom(int start)
525{
526 struct pollfd poll_events[1024];
527 struct rlimit limit;
528 int nbfds, fd, ret, idx;
529 int step, next;
530
531 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
532 step = nbfds = limit.rlim_cur;
533 else
534 step = nbfds = 0;
535
536 if (nbfds <= 0) {
537 /* set safe limit */
538 nbfds = 1024;
539 step = 256;
540 }
541
542 if (step > sizeof(poll_events) / sizeof(poll_events[0]))
543 step = sizeof(poll_events) / sizeof(poll_events[0]);
544
545 while (start < nbfds) {
546 next = (start / step + 1) * step;
547
548 for (fd = start; fd < next && fd < nbfds; fd++) {
549 poll_events[fd - start].fd = fd;
550 poll_events[fd - start].events = 0;
551 }
552
553 do {
554 ret = poll(poll_events, fd - start, 0);
555 if (ret >= 0)
556 break;
557 } while (errno == EAGAIN || errno == EINTR || errno == ENOMEM);
558
Willy Tarreaub8e602c2019-02-22 09:07:42 +0100559 if (ret)
560 ret = fd - start;
561
Willy Tarreau9188ac62019-02-21 22:12:47 +0100562 for (idx = 0; idx < ret; idx++) {
563 if (poll_events[idx].revents & POLLNVAL)
564 continue; /* already closed */
565
566 fd = poll_events[idx].fd;
567 close(fd);
568 }
569 start = next;
570 }
571}
572
Willy Tarreaue5733232019-05-22 19:24:06 +0200573#else // defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100574
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100575/* This is a portable implementation of closefrom(). It closes all open file
576 * descriptors starting at <start> and above. This is a naive version for use
577 * when the operating system provides no alternative.
578 */
579void my_closefrom(int start)
580{
581 struct rlimit limit;
582 int nbfds;
583
584 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
585 nbfds = limit.rlim_cur;
586 else
587 nbfds = 0;
588
589 if (nbfds <= 0)
590 nbfds = 1024; /* safe limit */
591
592 while (start < nbfds)
593 close(start++);
594}
Willy Tarreaue5733232019-05-22 19:24:06 +0200595#endif // defined(USE_POLL)
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100596
Willy Tarreau4f60f162007-04-08 16:39:58 +0200597/* disable the specified poller */
598void disable_poller(const char *poller_name)
599{
600 int p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200601
Willy Tarreau4f60f162007-04-08 16:39:58 +0200602 for (p = 0; p < nbpollers; p++)
603 if (strcmp(pollers[p].name, poller_name) == 0)
604 pollers[p].pref = 0;
605}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200606
Olivier Houchard79321b92018-07-26 17:55:11 +0200607void poller_pipe_io_handler(int fd)
608{
609 char buf[1024];
610 /* Flush the pipe */
611 while (read(fd, buf, sizeof(buf)) > 0);
612 fd_cant_recv(fd);
613}
614
Willy Tarreau082b6282019-05-22 14:42:12 +0200615/* allocate the per-thread fd_updt thus needs to be called early after
616 * thread creation.
617 */
618static int alloc_pollers_per_thread()
619{
620 fd_updt = calloc(global.maxsock, sizeof(*fd_updt));
621 return fd_updt != NULL;
622}
623
624/* Initialize the pollers per thread.*/
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200625static int init_pollers_per_thread()
626{
Olivier Houchard79321b92018-07-26 17:55:11 +0200627 int mypipe[2];
Willy Tarreau082b6282019-05-22 14:42:12 +0200628
629 if (pipe(mypipe) < 0)
Olivier Houchard79321b92018-07-26 17:55:11 +0200630 return 0;
Willy Tarreau082b6282019-05-22 14:42:12 +0200631
Olivier Houchard79321b92018-07-26 17:55:11 +0200632 poller_rd_pipe = mypipe[0];
633 poller_wr_pipe[tid] = mypipe[1];
634 fcntl(poller_rd_pipe, F_SETFL, O_NONBLOCK);
635 fd_insert(poller_rd_pipe, poller_pipe_io_handler, poller_pipe_io_handler,
636 tid_bit);
637 fd_want_recv(poller_rd_pipe);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200638 return 1;
639}
640
641/* Deinitialize the pollers per thread */
642static void deinit_pollers_per_thread()
643{
William Lallemand808e1b72018-12-15 22:34:31 +0100644 /* rd and wr are init at the same place, but only rd is init to -1, so
645 we rely to rd to close. */
646 if (poller_rd_pipe > -1) {
647 close(poller_rd_pipe);
648 poller_rd_pipe = -1;
649 close(poller_wr_pipe[tid]);
650 poller_wr_pipe[tid] = -1;
651 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200652}
653
Willy Tarreau082b6282019-05-22 14:42:12 +0200654/* Release the pollers per thread, to be called late */
655static void free_pollers_per_thread()
656{
657 free(fd_updt);
658 fd_updt = NULL;
659}
660
Willy Tarreaubaaee002006-06-26 02:48:02 +0200661/*
Willy Tarreau4f60f162007-04-08 16:39:58 +0200662 * Initialize the pollers till the best one is found.
663 * If none works, returns 0, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200664 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200665int init_pollers()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200666{
Willy Tarreau4f60f162007-04-08 16:39:58 +0200667 int p;
668 struct poller *bp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669
Christopher Faulet63fe6522017-08-31 17:52:09 +0200670 if ((fdtab = calloc(global.maxsock, sizeof(struct fdtab))) == NULL)
671 goto fail_tab;
672
Olivier Houchard53055052019-07-25 14:00:18 +0000673 if ((polled_mask = calloc(global.maxsock, sizeof(*polled_mask))) == NULL)
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200674 goto fail_polledmask;
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000675
Christopher Faulet63fe6522017-08-31 17:52:09 +0200676 if ((fdinfo = calloc(global.maxsock, sizeof(struct fdinfo))) == NULL)
677 goto fail_info;
678
Olivier Houchard6b96f722018-04-25 16:58:25 +0200679 update_list.first = update_list.last = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100681 for (p = 0; p < global.maxsock; p++) {
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100682 /* Mark the fd as out of the fd cache */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200683 fdtab[p].update.next = -3;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100684 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200685
Willy Tarreau4f60f162007-04-08 16:39:58 +0200686 do {
687 bp = NULL;
688 for (p = 0; p < nbpollers; p++)
689 if (!bp || (pollers[p].pref > bp->pref))
690 bp = &pollers[p];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200691
Willy Tarreau4f60f162007-04-08 16:39:58 +0200692 if (!bp || bp->pref == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200693 break;
694
Willy Tarreau4f60f162007-04-08 16:39:58 +0200695 if (bp->init(bp)) {
696 memcpy(&cur_poller, bp, sizeof(*bp));
697 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200698 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200699 } while (!bp || bp->pref == 0);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100700
Christopher Faulet63fe6522017-08-31 17:52:09 +0200701 free(fdinfo);
702 fail_info:
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200703 free(polled_mask);
704 fail_polledmask:
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000705 free(fdtab);
706 fail_tab:
Willy Tarreau7be79a42012-11-11 15:02:54 +0100707 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708}
709
Willy Tarreaubaaee002006-06-26 02:48:02 +0200710/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200711 * Deinitialize the pollers.
712 */
713void deinit_pollers() {
714
715 struct poller *bp;
716 int p;
717
718 for (p = 0; p < nbpollers; p++) {
719 bp = &pollers[p];
720
721 if (bp && bp->pref)
722 bp->term(bp);
723 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200724
Christopher Faulet63fe6522017-08-31 17:52:09 +0200725 free(fdinfo); fdinfo = NULL;
726 free(fdtab); fdtab = NULL;
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200727 free(polled_mask); polled_mask = NULL;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200728}
729
730/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200731 * Lists the known pollers on <out>.
732 * Should be performed only before initialization.
733 */
734int list_pollers(FILE *out)
735{
736 int p;
737 int last, next;
738 int usable;
739 struct poller *bp;
740
741 fprintf(out, "Available polling systems :\n");
742
743 usable = 0;
744 bp = NULL;
745 last = next = -1;
746 while (1) {
747 for (p = 0; p < nbpollers; p++) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200748 if ((next < 0 || pollers[p].pref > next)
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100749 && (last < 0 || pollers[p].pref < last)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200750 next = pollers[p].pref;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100751 if (!bp || (pollers[p].pref > bp->pref))
752 bp = &pollers[p];
753 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200754 }
755
756 if (next == -1)
757 break;
758
759 for (p = 0; p < nbpollers; p++) {
760 if (pollers[p].pref == next) {
761 fprintf(out, " %10s : ", pollers[p].name);
762 if (pollers[p].pref == 0)
763 fprintf(out, "disabled, ");
764 else
765 fprintf(out, "pref=%3d, ", pollers[p].pref);
766 if (pollers[p].test(&pollers[p])) {
767 fprintf(out, " test result OK");
768 if (next > 0)
769 usable++;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100770 } else {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200771 fprintf(out, " test result FAILED");
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100772 if (bp == &pollers[p])
773 bp = NULL;
774 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200775 fprintf(out, "\n");
776 }
777 }
778 last = next;
779 next = -1;
780 };
781 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
782 return 0;
783}
784
785/*
786 * Some pollers may lose their connection after a fork(). It may be necessary
787 * to create initialize part of them again. Returns 0 in case of failure,
788 * otherwise 1. The fork() function may be NULL if unused. In case of error,
789 * the the current poller is destroyed and the caller is responsible for trying
790 * another one by calling init_pollers() again.
791 */
792int fork_poller()
793{
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200794 int fd;
Willy Tarreauce036bc2018-01-29 14:58:02 +0100795 for (fd = 0; fd < global.maxsock; fd++) {
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200796 if (fdtab[fd].owner) {
797 fdtab[fd].cloned = 1;
798 }
799 }
800
Willy Tarreau2ff76222007-04-09 19:29:56 +0200801 if (cur_poller.fork) {
802 if (cur_poller.fork(&cur_poller))
803 return 1;
804 cur_poller.term(&cur_poller);
805 return 0;
806 }
807 return 1;
808}
809
Willy Tarreau082b6282019-05-22 14:42:12 +0200810REGISTER_PER_THREAD_ALLOC(alloc_pollers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100811REGISTER_PER_THREAD_INIT(init_pollers_per_thread);
812REGISTER_PER_THREAD_DEINIT(deinit_pollers_per_thread);
Willy Tarreau082b6282019-05-22 14:42:12 +0200813REGISTER_PER_THREAD_FREE(free_pollers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100814
Willy Tarreau2ff76222007-04-09 19:29:56 +0200815/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200816 * Local variables:
817 * c-indent-level: 8
818 * c-basic-offset: 8
819 * End:
820 */