blob: 919e4335141e5594e4df58ea1ff0fce11e752904 [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()
303 * here, not against fd_takeother(), because either we're called
304 * 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
347 int ret;
348
349 HA_RWLOCK_WRLOCK(OTHER_LOCK, &fd_mig_lock);
350 _HA_ATOMIC_OR(&fdtab[fd].running_mask, tid_bit);
351 if (fdtab[fd].running_mask != tid_bit || fdtab[fd].owner != expected_owner) {
352 ret = -1;
353 _HA_ATOMIC_AND(&fdtab[fd].running_mask, ~tid_bit);
354 goto end;
355 }
356 fdtab[fd].thread_mask = tid_bit;
357 _HA_ATOMIC_AND(&fdtab[fd].running_mask, ~tid_bit);
358 ret = 0;
359end:
360 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &fd_mig_lock);
361 /* Make sure the FD doesn't have the active bit. It is possible that
362 * the fd is polled by the thread that used to own it, the new thread
363 * is supposed to call subscribe() later, to activate polling.
364 */
365 fd_stop_recv(fd);
366 return ret;
367#else
368 unsigned long old_masks[2];
369 unsigned long new_masks[2];
370
371 old_masks[0] = tid_bit;
372 old_masks[1] = fdtab[fd].thread_mask;
373 new_masks[0] = new_masks[1] = tid_bit;
374 /* protect ourself against a delete then an insert for the same fd,
375 * if it happens, then the owner will no longer be the expected
376 * connection.
377 */
378 _HA_ATOMIC_OR(&fdtab[fd].running_mask, tid_bit);
379 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);
391 return 0;
392#endif /* HW_HAVE_CAS_DW */
393}
394
Willy Tarreau173d9952018-01-26 21:48:23 +0100395/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200396 * The file descriptor is also closed.
397 */
398void fd_delete(int fd)
399{
400 fd_dodelete(fd, 1);
401}
402
Willy Tarreau173d9952018-01-26 21:48:23 +0100403/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200404 * The file descriptor is kept open.
405 */
406void fd_remove(int fd)
407{
408 fd_dodelete(fd, 0);
409}
410
Willy Tarreaudbe30602019-09-04 13:25:41 +0200411void updt_fd_polling(const int fd)
412{
413 if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) {
414
415 /* note: we don't have a test-and-set yet in hathreads */
416
417 if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
418 return;
419
420 fd_updt[fd_nbupdt++] = fd;
421 } else {
422 unsigned long update_mask = fdtab[fd].update_mask;
423 do {
424 if (update_mask == fdtab[fd].thread_mask)
425 return;
426 } while (!_HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask,
427 fdtab[fd].thread_mask));
428 fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update));
429 }
430}
431
Olivier Houchard7fa55622020-02-27 17:25:43 +0100432__decl_spinlock(log_lock);
Willy Tarreau931d8b72019-08-27 11:08:17 +0200433/* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg>
434 * optionally followed by a newline if <nl> is non-null, to file descriptor
435 * <fd>. The message is sent atomically using writev(). It may be truncated to
436 * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the
437 * two lists, it's just a convenience to help the caller prepend some prefixes
438 * when necessary. It takes the fd's lock to make sure no other thread will
439 * write to the same fd in parallel. Returns the number of bytes sent, or <=0
440 * on failure. A limit to 31 total non-empty segments is enforced. The caller
441 * is responsible for taking care of making the fd non-blocking.
442 */
443ssize_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)
444{
445 struct iovec iovec[32];
446 size_t totlen = 0;
447 size_t sent = 0;
448 int vec = 0;
Willy Tarreaudf187872020-06-11 14:25:47 +0200449 int attempts = 0;
Willy Tarreau931d8b72019-08-27 11:08:17 +0200450
451 if (!maxlen)
452 maxlen = ~0;
453
454 /* keep one char for a possible trailing '\n' in any case */
455 maxlen--;
456
457 /* make an iovec from the concatenation of all parts of the original
458 * message. Skip empty fields and truncate the whole message to maxlen,
459 * leaving one spare iovec for the '\n'.
460 */
461 while (vec < (sizeof(iovec) / sizeof(iovec[0]) - 1)) {
462 if (!npfx) {
463 pfx = msg;
464 npfx = nmsg;
465 nmsg = 0;
466 if (!npfx)
467 break;
468 }
469
470 iovec[vec].iov_base = pfx->ptr;
471 iovec[vec].iov_len = MIN(maxlen, pfx->len);
472 maxlen -= iovec[vec].iov_len;
473 totlen += iovec[vec].iov_len;
474 if (iovec[vec].iov_len)
475 vec++;
476 pfx++; npfx--;
477 };
478
479 if (nl) {
480 iovec[vec].iov_base = "\n";
481 iovec[vec].iov_len = 1;
482 vec++;
483 }
484
Willy Tarreaudf187872020-06-11 14:25:47 +0200485 /* make sure we never interleave writes and we never block. This means
486 * we prefer to fail on collision than to block. But we don't want to
487 * lose too many logs so we just perform a few lock attempts then give
488 * up.
489 */
490
491 while (HA_SPIN_TRYLOCK(OTHER_LOCK, &log_lock) != 0) {
492 if (++attempts >= 200) {
493 /* so that the caller knows the message couldn't be delivered */
494 sent = -1;
495 errno = EAGAIN;
496 goto leave;
497 }
498 ha_thread_relax();
499 }
500
Willy Tarreau7e9776a2019-08-30 14:41:47 +0200501 if (unlikely(!fdtab[fd].initialized)) {
502 fdtab[fd].initialized = 1;
503 if (!isatty(fd))
504 fcntl(fd, F_SETFL, O_NONBLOCK);
505 }
Willy Tarreau931d8b72019-08-27 11:08:17 +0200506 sent = writev(fd, iovec, vec);
Olivier Houchard7fa55622020-02-27 17:25:43 +0100507 HA_SPIN_UNLOCK(OTHER_LOCK, &log_lock);
Willy Tarreau931d8b72019-08-27 11:08:17 +0200508
Willy Tarreaudf187872020-06-11 14:25:47 +0200509 leave:
Willy Tarreau931d8b72019-08-27 11:08:17 +0200510 /* sent > 0 if the message was delivered */
511 return sent;
512}
513
Olivier Houchard2292edf2019-02-25 14:26:54 +0100514#if defined(USE_CLOSEFROM)
515void my_closefrom(int start)
516{
517 closefrom(start);
518}
519
Willy Tarreaue5733232019-05-22 19:24:06 +0200520#elif defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100521/* This is a portable implementation of closefrom(). It closes all open file
522 * descriptors starting at <start> and above. It relies on the fact that poll()
523 * will return POLLNVAL for each invalid (hence close) file descriptor passed
524 * in argument in order to skip them. It acts with batches of FDs and will
525 * typically perform one poll() call per 1024 FDs so the overhead is low in
526 * case all FDs have to be closed.
527 */
528void my_closefrom(int start)
529{
530 struct pollfd poll_events[1024];
531 struct rlimit limit;
532 int nbfds, fd, ret, idx;
533 int step, next;
534
535 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
536 step = nbfds = limit.rlim_cur;
537 else
538 step = nbfds = 0;
539
540 if (nbfds <= 0) {
541 /* set safe limit */
542 nbfds = 1024;
543 step = 256;
544 }
545
546 if (step > sizeof(poll_events) / sizeof(poll_events[0]))
547 step = sizeof(poll_events) / sizeof(poll_events[0]);
548
549 while (start < nbfds) {
550 next = (start / step + 1) * step;
551
552 for (fd = start; fd < next && fd < nbfds; fd++) {
553 poll_events[fd - start].fd = fd;
554 poll_events[fd - start].events = 0;
555 }
556
557 do {
558 ret = poll(poll_events, fd - start, 0);
559 if (ret >= 0)
560 break;
561 } while (errno == EAGAIN || errno == EINTR || errno == ENOMEM);
562
Willy Tarreaub8e602c2019-02-22 09:07:42 +0100563 if (ret)
564 ret = fd - start;
565
Willy Tarreau9188ac62019-02-21 22:12:47 +0100566 for (idx = 0; idx < ret; idx++) {
567 if (poll_events[idx].revents & POLLNVAL)
568 continue; /* already closed */
569
570 fd = poll_events[idx].fd;
571 close(fd);
572 }
573 start = next;
574 }
575}
576
Willy Tarreaue5733232019-05-22 19:24:06 +0200577#else // defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100578
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100579/* This is a portable implementation of closefrom(). It closes all open file
580 * descriptors starting at <start> and above. This is a naive version for use
581 * when the operating system provides no alternative.
582 */
583void my_closefrom(int start)
584{
585 struct rlimit limit;
586 int nbfds;
587
588 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
589 nbfds = limit.rlim_cur;
590 else
591 nbfds = 0;
592
593 if (nbfds <= 0)
594 nbfds = 1024; /* safe limit */
595
596 while (start < nbfds)
597 close(start++);
598}
Willy Tarreaue5733232019-05-22 19:24:06 +0200599#endif // defined(USE_POLL)
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100600
Willy Tarreau4f60f162007-04-08 16:39:58 +0200601/* disable the specified poller */
602void disable_poller(const char *poller_name)
603{
604 int p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200605
Willy Tarreau4f60f162007-04-08 16:39:58 +0200606 for (p = 0; p < nbpollers; p++)
607 if (strcmp(pollers[p].name, poller_name) == 0)
608 pollers[p].pref = 0;
609}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200610
Olivier Houchard79321b92018-07-26 17:55:11 +0200611void poller_pipe_io_handler(int fd)
612{
613 char buf[1024];
614 /* Flush the pipe */
615 while (read(fd, buf, sizeof(buf)) > 0);
616 fd_cant_recv(fd);
617}
618
Willy Tarreau082b6282019-05-22 14:42:12 +0200619/* allocate the per-thread fd_updt thus needs to be called early after
620 * thread creation.
621 */
622static int alloc_pollers_per_thread()
623{
624 fd_updt = calloc(global.maxsock, sizeof(*fd_updt));
625 return fd_updt != NULL;
626}
627
628/* Initialize the pollers per thread.*/
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200629static int init_pollers_per_thread()
630{
Olivier Houchard79321b92018-07-26 17:55:11 +0200631 int mypipe[2];
Willy Tarreau082b6282019-05-22 14:42:12 +0200632
633 if (pipe(mypipe) < 0)
Olivier Houchard79321b92018-07-26 17:55:11 +0200634 return 0;
Willy Tarreau082b6282019-05-22 14:42:12 +0200635
Olivier Houchard79321b92018-07-26 17:55:11 +0200636 poller_rd_pipe = mypipe[0];
637 poller_wr_pipe[tid] = mypipe[1];
638 fcntl(poller_rd_pipe, F_SETFL, O_NONBLOCK);
639 fd_insert(poller_rd_pipe, poller_pipe_io_handler, poller_pipe_io_handler,
640 tid_bit);
641 fd_want_recv(poller_rd_pipe);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200642 return 1;
643}
644
645/* Deinitialize the pollers per thread */
646static void deinit_pollers_per_thread()
647{
William Lallemand808e1b72018-12-15 22:34:31 +0100648 /* rd and wr are init at the same place, but only rd is init to -1, so
649 we rely to rd to close. */
650 if (poller_rd_pipe > -1) {
651 close(poller_rd_pipe);
652 poller_rd_pipe = -1;
653 close(poller_wr_pipe[tid]);
654 poller_wr_pipe[tid] = -1;
655 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200656}
657
Willy Tarreau082b6282019-05-22 14:42:12 +0200658/* Release the pollers per thread, to be called late */
659static void free_pollers_per_thread()
660{
661 free(fd_updt);
662 fd_updt = NULL;
663}
664
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665/*
Willy Tarreau4f60f162007-04-08 16:39:58 +0200666 * Initialize the pollers till the best one is found.
667 * If none works, returns 0, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200668 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200669int init_pollers()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670{
Willy Tarreau4f60f162007-04-08 16:39:58 +0200671 int p;
672 struct poller *bp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200673
Christopher Faulet63fe6522017-08-31 17:52:09 +0200674 if ((fdtab = calloc(global.maxsock, sizeof(struct fdtab))) == NULL)
675 goto fail_tab;
676
Olivier Houchard53055052019-07-25 14:00:18 +0000677 if ((polled_mask = calloc(global.maxsock, sizeof(*polled_mask))) == NULL)
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200678 goto fail_polledmask;
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000679
Christopher Faulet63fe6522017-08-31 17:52:09 +0200680 if ((fdinfo = calloc(global.maxsock, sizeof(struct fdinfo))) == NULL)
681 goto fail_info;
682
Olivier Houchard6b96f722018-04-25 16:58:25 +0200683 update_list.first = update_list.last = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100685 for (p = 0; p < global.maxsock; p++) {
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100686 /* Mark the fd as out of the fd cache */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200687 fdtab[p].update.next = -3;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100688 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200689
Willy Tarreau4f60f162007-04-08 16:39:58 +0200690 do {
691 bp = NULL;
692 for (p = 0; p < nbpollers; p++)
693 if (!bp || (pollers[p].pref > bp->pref))
694 bp = &pollers[p];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695
Willy Tarreau4f60f162007-04-08 16:39:58 +0200696 if (!bp || bp->pref == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200697 break;
698
Willy Tarreau4f60f162007-04-08 16:39:58 +0200699 if (bp->init(bp)) {
700 memcpy(&cur_poller, bp, sizeof(*bp));
701 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200702 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200703 } while (!bp || bp->pref == 0);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100704
Christopher Faulet63fe6522017-08-31 17:52:09 +0200705 free(fdinfo);
706 fail_info:
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200707 free(polled_mask);
708 fail_polledmask:
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000709 free(fdtab);
710 fail_tab:
Willy Tarreau7be79a42012-11-11 15:02:54 +0100711 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200712}
713
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200715 * Deinitialize the pollers.
716 */
717void deinit_pollers() {
718
719 struct poller *bp;
720 int p;
721
722 for (p = 0; p < nbpollers; p++) {
723 bp = &pollers[p];
724
725 if (bp && bp->pref)
726 bp->term(bp);
727 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200728
Christopher Faulet63fe6522017-08-31 17:52:09 +0200729 free(fdinfo); fdinfo = NULL;
730 free(fdtab); fdtab = NULL;
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200731 free(polled_mask); polled_mask = NULL;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200732}
733
734/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200735 * Lists the known pollers on <out>.
736 * Should be performed only before initialization.
737 */
738int list_pollers(FILE *out)
739{
740 int p;
741 int last, next;
742 int usable;
743 struct poller *bp;
744
745 fprintf(out, "Available polling systems :\n");
746
747 usable = 0;
748 bp = NULL;
749 last = next = -1;
750 while (1) {
751 for (p = 0; p < nbpollers; p++) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200752 if ((next < 0 || pollers[p].pref > next)
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100753 && (last < 0 || pollers[p].pref < last)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200754 next = pollers[p].pref;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100755 if (!bp || (pollers[p].pref > bp->pref))
756 bp = &pollers[p];
757 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200758 }
759
760 if (next == -1)
761 break;
762
763 for (p = 0; p < nbpollers; p++) {
764 if (pollers[p].pref == next) {
765 fprintf(out, " %10s : ", pollers[p].name);
766 if (pollers[p].pref == 0)
767 fprintf(out, "disabled, ");
768 else
769 fprintf(out, "pref=%3d, ", pollers[p].pref);
770 if (pollers[p].test(&pollers[p])) {
771 fprintf(out, " test result OK");
772 if (next > 0)
773 usable++;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100774 } else {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200775 fprintf(out, " test result FAILED");
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100776 if (bp == &pollers[p])
777 bp = NULL;
778 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200779 fprintf(out, "\n");
780 }
781 }
782 last = next;
783 next = -1;
784 };
785 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
786 return 0;
787}
788
789/*
790 * Some pollers may lose their connection after a fork(). It may be necessary
791 * to create initialize part of them again. Returns 0 in case of failure,
792 * otherwise 1. The fork() function may be NULL if unused. In case of error,
793 * the the current poller is destroyed and the caller is responsible for trying
794 * another one by calling init_pollers() again.
795 */
796int fork_poller()
797{
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200798 int fd;
Willy Tarreauce036bc2018-01-29 14:58:02 +0100799 for (fd = 0; fd < global.maxsock; fd++) {
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200800 if (fdtab[fd].owner) {
801 fdtab[fd].cloned = 1;
802 }
803 }
804
Willy Tarreau2ff76222007-04-09 19:29:56 +0200805 if (cur_poller.fork) {
806 if (cur_poller.fork(&cur_poller))
807 return 1;
808 cur_poller.term(&cur_poller);
809 return 0;
810 }
811 return 1;
812}
813
Willy Tarreau082b6282019-05-22 14:42:12 +0200814REGISTER_PER_THREAD_ALLOC(alloc_pollers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100815REGISTER_PER_THREAD_INIT(init_pollers_per_thread);
816REGISTER_PER_THREAD_DEINIT(deinit_pollers_per_thread);
Willy Tarreau082b6282019-05-22 14:42:12 +0200817REGISTER_PER_THREAD_FREE(free_pollers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100818
Willy Tarreau2ff76222007-04-09 19:29:56 +0200819/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200820 * Local variables:
821 * c-indent-level: 8
822 * c-basic-offset: 8
823 * End:
824 */