blob: 58d73d4ab471a99f38ab047446c420ad09b8555f [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 *
17 * It is important to understand that as long as all expected events are
18 * processed, they might starve the polled events, especially because polled
Willy Tarreauf817e9f2014-01-10 16:58:45 +010019 * I/O starvation quickly induces more cached I/O. One solution to this
Willy Tarreau7be79a42012-11-11 15:02:54 +010020 * consists in only processing a part of the events at once, but one drawback
Willy Tarreauf817e9f2014-01-10 16:58:45 +010021 * is that unhandled events will still wake the poller up. Using an edge-
22 * triggered poller such as EPOLL_ET will solve this issue though.
Willy Tarreau7be79a42012-11-11 15:02:54 +010023 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010024 * The event state for an FD, as found in fdtab[].state, is maintained for each
25 * direction. The state field is built this way, with R bits in the low nibble
26 * and W bits in the high nibble for ease of access and debugging :
Willy Tarreau7be79a42012-11-11 15:02:54 +010027 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010028 * 7 6 5 4 3 2 1 0
29 * [ 0 | PW | RW | AW | 0 | PR | RR | AR ]
30 *
31 * A* = active *R = read
32 * P* = polled *W = write
33 * R* = ready
34 *
35 * An FD is marked "active" when there is a desire to use it.
36 * An FD is marked "polled" when it is registered in the polling.
37 * An FD is marked "ready" when it has not faced a new EAGAIN since last wake-up
38 * (it is a cache of the last EAGAIN regardless of polling changes).
Willy Tarreau7be79a42012-11-11 15:02:54 +010039 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010040 * We have 8 possible states for each direction based on these 3 flags :
Willy Tarreau7be79a42012-11-11 15:02:54 +010041 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010042 * +---+---+---+----------+---------------------------------------------+
43 * | P | R | A | State | Description |
44 * +---+---+---+----------+---------------------------------------------+
45 * | 0 | 0 | 0 | DISABLED | No activity desired, not ready. |
46 * | 0 | 0 | 1 | MUSTPOLL | Activity desired via polling. |
47 * | 0 | 1 | 0 | STOPPED | End of activity without polling. |
48 * | 0 | 1 | 1 | ACTIVE | Activity desired without polling. |
49 * | 1 | 0 | 0 | ABORT | Aborted poll(). Not frequently seen. |
50 * | 1 | 0 | 1 | POLLED | FD is being polled. |
51 * | 1 | 1 | 0 | PAUSED | FD was paused while ready (eg: buffer full) |
52 * | 1 | 1 | 1 | READY | FD was marked ready by poll() |
53 * +---+---+---+----------+---------------------------------------------+
Willy Tarreau7be79a42012-11-11 15:02:54 +010054 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010055 * The transitions are pretty simple :
56 * - fd_want_*() : set flag A
57 * - fd_stop_*() : clear flag A
58 * - fd_cant_*() : clear flag R (when facing EAGAIN)
59 * - fd_may_*() : set flag R (upon return from poll())
60 * - sync() : if (A) { if (!R) P := 1 } else { P := 0 }
61 *
62 * The PAUSED, ABORT and MUSTPOLL states are transient for level-trigerred
63 * pollers and are fixed by the sync() which happens at the beginning of the
64 * poller. For event-triggered pollers, only the MUSTPOLL state will be
65 * transient and ABORT will lead to PAUSED. The ACTIVE state is the only stable
66 * one which has P != A.
67 *
68 * The READY state is a bit special as activity on the FD might be notified
69 * both by the poller or by the cache. But it is needed for some multi-layer
70 * protocols (eg: SSL) where connection activity is not 100% linked to FD
71 * activity. Also some pollers might prefer to implement it as ACTIVE if
72 * enabling/disabling the FD is cheap. The READY and ACTIVE states are the
73 * two states for which a cache entry is allocated.
74 *
75 * The state transitions look like the diagram below. Only the 4 right states
76 * have polling enabled :
77 *
78 * (POLLED=0) (POLLED=1)
79 *
80 * +----------+ sync +-------+
81 * | DISABLED | <----- | ABORT | (READY=0, ACTIVE=0)
82 * +----------+ +-------+
83 * clr | ^ set | ^
84 * | | | |
85 * v | set v | clr
86 * +----------+ sync +--------+
87 * | MUSTPOLL | -----> | POLLED | (READY=0, ACTIVE=1)
88 * +----------+ +--------+
89 * ^ poll | ^
90 * | | |
91 * | EAGAIN v | EAGAIN
92 * +--------+ +-------+
93 * | ACTIVE | | READY | (READY=1, ACTIVE=1)
94 * +--------+ +-------+
95 * clr | ^ set | ^
96 * | | | |
97 * v | set v | clr
98 * +---------+ sync +--------+
99 * | STOPPED | <------ | PAUSED | (READY=1, ACTIVE=0)
100 * +---------+ +--------+
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 */
102
Willy Tarreau2ff76222007-04-09 19:29:56 +0200103#include <stdio.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +0200104#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105#include <unistd.h>
Olivier Houchard79321b92018-07-26 17:55:11 +0200106#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107#include <sys/types.h>
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100108#include <sys/resource.h>
Willy Tarreau931d8b72019-08-27 11:08:17 +0200109#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110
Willy Tarreaue5733232019-05-22 19:24:06 +0200111#if defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100112#include <poll.h>
113#include <errno.h>
114#endif
115
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200116#include <common/compat.h>
117#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118
Willy Tarreau7be79a42012-11-11 15:02:54 +0100119#include <types/global.h>
120
Willy Tarreau2a429502006-10-15 14:52:29 +0200121#include <proto/fd.h>
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200122#include <proto/log.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200123#include <proto/port_range.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124
125struct fdtab *fdtab = NULL; /* array of all the file descriptors */
Olivier Houchard53055052019-07-25 14:00:18 +0000126struct polled_mask *polled_mask = NULL; /* Array for the polled_mask of each fd */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200127struct fdinfo *fdinfo = NULL; /* less-often used infos for file descriptors */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128int totalconn; /* total # of terminated sessions */
129int actconn; /* # of active sessions */
130
Willy Tarreau4f60f162007-04-08 16:39:58 +0200131struct poller pollers[MAX_POLLERS];
132struct poller cur_poller;
133int nbpollers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134
Olivier Houchard6b96f722018-04-25 16:58:25 +0200135volatile struct fdlist update_list; // Global update list
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100136
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200137THREAD_LOCAL int *fd_updt = NULL; // FD updates list
138THREAD_LOCAL int fd_nbupdt = 0; // number of updates in the list
Olivier Houchard79321b92018-07-26 17:55:11 +0200139THREAD_LOCAL int poller_rd_pipe = -1; // Pipe to wake the thread
140int poller_wr_pipe[MAX_THREADS]; // Pipe to wake the threads
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200141
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200142volatile int ha_used_fds = 0; // Number of FD we're currently using
143
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200144#define _GET_NEXT(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->next
145#define _GET_PREV(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->prev
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100146/* adds fd <fd> to fd list <list> if it was not yet in it */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200147void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off)
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100148{
149 int next;
150 int new;
151 int old;
152 int last;
153
154redo_next:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200155 next = _GET_NEXT(fd, off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100156 /* Check that we're not already in the cache, and if not, lock us. */
157 if (next >= -2)
158 goto done;
Olivier Houchardd3608792019-03-08 18:47:42 +0100159 if (!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100160 goto redo_next;
Olivier Houchardd2b5d162019-03-08 13:47:21 +0100161 __ha_barrier_atomic_store();
Willy Tarreau11559a72018-02-05 17:52:24 +0100162
163 new = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100164redo_last:
165 /* First, insert in the linked list */
166 last = list->last;
167 old = -1;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100168
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200169 _GET_PREV(fd, off) = -2;
Willy Tarreau11559a72018-02-05 17:52:24 +0100170 /* Make sure the "prev" store is visible before we update the last entry */
171 __ha_barrier_store();
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100172
Willy Tarreau11559a72018-02-05 17:52:24 +0100173 if (unlikely(last == -1)) {
174 /* list is empty, try to add ourselves alone so that list->last=fd */
Olivier Houchardd3608792019-03-08 18:47:42 +0100175 if (unlikely(!_HA_ATOMIC_CAS(&list->last, &old, new)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100176 goto redo_last;
177
178 /* list->first was necessary -1, we're guaranteed to be alone here */
179 list->first = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100180 } else {
Willy Tarreau11559a72018-02-05 17:52:24 +0100181 /* adding ourselves past the last element
182 * The CAS will only succeed if its next is -1,
183 * which means it's in the cache, and the last element.
184 */
Olivier Houchardd3608792019-03-08 18:47:42 +0100185 if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(last, off), &old, new)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100186 goto redo_last;
Willy Tarreau11559a72018-02-05 17:52:24 +0100187
188 /* Then, update the last entry */
189 list->last = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100190 }
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100191 __ha_barrier_store();
Willy Tarreau11559a72018-02-05 17:52:24 +0100192 /* since we're alone at the end of the list and still locked(-2),
193 * we know noone tried to add past us. Mark the end of list.
194 */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200195 _GET_PREV(fd, off) = last;
196 _GET_NEXT(fd, off) = -1;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100197 __ha_barrier_store();
198done:
199 return;
200}
201
202/* removes fd <fd> from fd list <list> */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200203void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off)
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100204{
205#if defined(HA_HAVE_CAS_DW) || defined(HA_CAS_IS_8B)
206 volatile struct fdlist_entry cur_list, next_list;
207#endif
208 int old;
209 int new = -2;
210 int prev;
211 int next;
212 int last;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100213lock_self:
214#if (defined(HA_CAS_IS_8B) || defined(HA_HAVE_CAS_DW))
215 next_list.next = next_list.prev = -2;
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200216 cur_list = *(volatile struct fdlist_entry *)(((char *)&fdtab[fd]) + off);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100217 /* First, attempt to lock our own entries */
218 do {
219 /* The FD is not in the FD cache, give up */
220 if (unlikely(cur_list.next <= -3))
221 return;
222 if (unlikely(cur_list.prev == -2 || cur_list.next == -2))
223 goto lock_self;
224 } while (
225#ifdef HA_CAS_IS_8B
Olivier Houchardd3608792019-03-08 18:47:42 +0100226 unlikely(!_HA_ATOMIC_CAS(((void **)(void *)&_GET_NEXT(fd, off)), ((void **)(void *)&cur_list), (*(void **)(void *)&next_list))))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100227#else
Willy Tarreauc3b59582019-05-27 17:37:20 +0200228 unlikely(!_HA_ATOMIC_DWCAS(((void *)&_GET_NEXT(fd, off)), ((void *)&cur_list), ((void *)&next_list))))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100229#endif
230 ;
231 next = cur_list.next;
232 prev = cur_list.prev;
233
234#else
235lock_self_next:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200236 next = ({ volatile int *next = &_GET_NEXT(fd, off); *next; });
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100237 if (next == -2)
238 goto lock_self_next;
239 if (next <= -3)
240 goto done;
Olivier Houchardd3608792019-03-08 18:47:42 +0100241 if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100242 goto lock_self_next;
243lock_self_prev:
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200244 prev = ({ volatile int *prev = &_GET_PREV(fd, off); *prev; });
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100245 if (prev == -2)
246 goto lock_self_prev;
Olivier Houchardd3608792019-03-08 18:47:42 +0100247 if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(fd, off), &prev, -2)))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100248 goto lock_self_prev;
249#endif
Olivier Houchardd2b5d162019-03-08 13:47:21 +0100250 __ha_barrier_atomic_store();
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100251
252 /* Now, lock the entries of our neighbours */
253 if (likely(prev != -1)) {
254redo_prev:
255 old = fd;
256
Olivier Houchardd3608792019-03-08 18:47:42 +0100257 if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(prev, off), &old, new))) {
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100258 if (unlikely(old == -2)) {
259 /* Neighbour already locked, give up and
260 * retry again once he's done
261 */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200262 _GET_PREV(fd, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100263 __ha_barrier_store();
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200264 _GET_NEXT(fd, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100265 __ha_barrier_store();
266 goto lock_self;
267 }
268 goto redo_prev;
269 }
270 }
271 if (likely(next != -1)) {
272redo_next:
273 old = fd;
Olivier Houchardd3608792019-03-08 18:47:42 +0100274 if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(next, off), &old, new))) {
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100275 if (unlikely(old == -2)) {
276 /* Neighbour already locked, give up and
277 * retry again once he's done
278 */
279 if (prev != -1) {
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200280 _GET_NEXT(prev, off) = fd;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100281 __ha_barrier_store();
282 }
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200283 _GET_PREV(fd, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100284 __ha_barrier_store();
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200285 _GET_NEXT(fd, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100286 __ha_barrier_store();
287 goto lock_self;
288 }
289 goto redo_next;
290 }
291 }
292 if (list->first == fd)
293 list->first = next;
294 __ha_barrier_store();
295 last = list->last;
Olivier Houchardd3608792019-03-08 18:47:42 +0100296 while (unlikely(last == fd && (!_HA_ATOMIC_CAS(&list->last, &last, prev))))
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100297 __ha_compiler_barrier();
298 /* Make sure we let other threads know we're no longer in cache,
299 * before releasing our neighbours.
300 */
301 __ha_barrier_store();
302 if (likely(prev != -1))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200303 _GET_NEXT(prev, off) = next;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100304 __ha_barrier_store();
305 if (likely(next != -1))
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200306 _GET_PREV(next, off) = prev;
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100307 __ha_barrier_store();
308 /* Ok, now we're out of the fd cache */
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200309 _GET_NEXT(fd, off) = -(next + 4);
Willy Tarreau4cc67a22018-02-05 17:14:55 +0100310 __ha_barrier_store();
311done:
312 return;
313}
314
Olivier Houchard6a2cf872018-04-25 15:10:30 +0200315#undef _GET_NEXT
316#undef _GET_PREV
317
Willy Tarreau173d9952018-01-26 21:48:23 +0100318/* Deletes an FD from the fdsets.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200319 * The file descriptor is also closed.
320 */
Olivier Houchard1fc05162017-04-06 01:05:05 +0200321static void fd_dodelete(int fd, int do_close)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200322{
Willy Tarreau87d54a92018-10-15 09:44:46 +0200323 unsigned long locked = atleast2(fdtab[fd].thread_mask);
324
325 if (locked)
326 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreauad38ace2013-12-15 14:19:38 +0100327 if (fdtab[fd].linger_risk) {
328 /* this is generally set when connecting to servers */
329 setsockopt(fd, SOL_SOCKET, SO_LINGER,
330 (struct linger *) &nolinger, sizeof(struct linger));
331 }
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100332 if (cur_poller.clo)
333 cur_poller.clo(fd);
Olivier Houchardc22580c2019-08-05 18:51:52 +0200334 polled_mask[fd].poll_recv = polled_mask[fd].poll_send = 0;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100335
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100336 fdtab[fd].state = 0;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100337
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200338 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
339 fdinfo[fd].port_range = NULL;
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200340 fdtab[fd].owner = NULL;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100341 fdtab[fd].thread_mask = 0;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100342 if (do_close) {
Christopher Fauletd531f882017-06-01 16:55:03 +0200343 close(fd);
Olivier Houchard7c49d2e2019-04-16 18:37:05 +0200344 _HA_ATOMIC_SUB(&ha_used_fds, 1);
Willy Tarreauc9c83782018-01-17 18:44:46 +0100345 }
Willy Tarreau87d54a92018-10-15 09:44:46 +0200346 if (locked)
347 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349
Willy Tarreau173d9952018-01-26 21:48:23 +0100350/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200351 * The file descriptor is also closed.
352 */
353void fd_delete(int fd)
354{
355 fd_dodelete(fd, 1);
356}
357
Willy Tarreau173d9952018-01-26 21:48:23 +0100358/* Deletes an FD from the fdsets.
Olivier Houchard1fc05162017-04-06 01:05:05 +0200359 * The file descriptor is kept open.
360 */
361void fd_remove(int fd)
362{
363 fd_dodelete(fd, 0);
364}
365
Willy Tarreau931d8b72019-08-27 11:08:17 +0200366/* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg>
367 * optionally followed by a newline if <nl> is non-null, to file descriptor
368 * <fd>. The message is sent atomically using writev(). It may be truncated to
369 * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the
370 * two lists, it's just a convenience to help the caller prepend some prefixes
371 * when necessary. It takes the fd's lock to make sure no other thread will
372 * write to the same fd in parallel. Returns the number of bytes sent, or <=0
373 * on failure. A limit to 31 total non-empty segments is enforced. The caller
374 * is responsible for taking care of making the fd non-blocking.
375 */
376ssize_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)
377{
378 struct iovec iovec[32];
379 size_t totlen = 0;
380 size_t sent = 0;
381 int vec = 0;
382
383 if (!maxlen)
384 maxlen = ~0;
385
386 /* keep one char for a possible trailing '\n' in any case */
387 maxlen--;
388
389 /* make an iovec from the concatenation of all parts of the original
390 * message. Skip empty fields and truncate the whole message to maxlen,
391 * leaving one spare iovec for the '\n'.
392 */
393 while (vec < (sizeof(iovec) / sizeof(iovec[0]) - 1)) {
394 if (!npfx) {
395 pfx = msg;
396 npfx = nmsg;
397 nmsg = 0;
398 if (!npfx)
399 break;
400 }
401
402 iovec[vec].iov_base = pfx->ptr;
403 iovec[vec].iov_len = MIN(maxlen, pfx->len);
404 maxlen -= iovec[vec].iov_len;
405 totlen += iovec[vec].iov_len;
406 if (iovec[vec].iov_len)
407 vec++;
408 pfx++; npfx--;
409 };
410
411 if (nl) {
412 iovec[vec].iov_base = "\n";
413 iovec[vec].iov_len = 1;
414 vec++;
415 }
416
Willy Tarreau7e9776a2019-08-30 14:41:47 +0200417 if (unlikely(!fdtab[fd].initialized)) {
418 fdtab[fd].initialized = 1;
419 if (!isatty(fd))
420 fcntl(fd, F_SETFL, O_NONBLOCK);
421 }
422
Willy Tarreau931d8b72019-08-27 11:08:17 +0200423 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
424 sent = writev(fd, iovec, vec);
425 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
426
427 /* sent > 0 if the message was delivered */
428 return sent;
429}
430
Olivier Houchard2292edf2019-02-25 14:26:54 +0100431#if defined(USE_CLOSEFROM)
432void my_closefrom(int start)
433{
434 closefrom(start);
435}
436
Willy Tarreaue5733232019-05-22 19:24:06 +0200437#elif defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100438/* This is a portable implementation of closefrom(). It closes all open file
439 * descriptors starting at <start> and above. It relies on the fact that poll()
440 * will return POLLNVAL for each invalid (hence close) file descriptor passed
441 * in argument in order to skip them. It acts with batches of FDs and will
442 * typically perform one poll() call per 1024 FDs so the overhead is low in
443 * case all FDs have to be closed.
444 */
445void my_closefrom(int start)
446{
447 struct pollfd poll_events[1024];
448 struct rlimit limit;
449 int nbfds, fd, ret, idx;
450 int step, next;
451
452 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
453 step = nbfds = limit.rlim_cur;
454 else
455 step = nbfds = 0;
456
457 if (nbfds <= 0) {
458 /* set safe limit */
459 nbfds = 1024;
460 step = 256;
461 }
462
463 if (step > sizeof(poll_events) / sizeof(poll_events[0]))
464 step = sizeof(poll_events) / sizeof(poll_events[0]);
465
466 while (start < nbfds) {
467 next = (start / step + 1) * step;
468
469 for (fd = start; fd < next && fd < nbfds; fd++) {
470 poll_events[fd - start].fd = fd;
471 poll_events[fd - start].events = 0;
472 }
473
474 do {
475 ret = poll(poll_events, fd - start, 0);
476 if (ret >= 0)
477 break;
478 } while (errno == EAGAIN || errno == EINTR || errno == ENOMEM);
479
Willy Tarreaub8e602c2019-02-22 09:07:42 +0100480 if (ret)
481 ret = fd - start;
482
Willy Tarreau9188ac62019-02-21 22:12:47 +0100483 for (idx = 0; idx < ret; idx++) {
484 if (poll_events[idx].revents & POLLNVAL)
485 continue; /* already closed */
486
487 fd = poll_events[idx].fd;
488 close(fd);
489 }
490 start = next;
491 }
492}
493
Willy Tarreaue5733232019-05-22 19:24:06 +0200494#else // defined(USE_POLL)
Willy Tarreau9188ac62019-02-21 22:12:47 +0100495
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100496/* This is a portable implementation of closefrom(). It closes all open file
497 * descriptors starting at <start> and above. This is a naive version for use
498 * when the operating system provides no alternative.
499 */
500void my_closefrom(int start)
501{
502 struct rlimit limit;
503 int nbfds;
504
505 if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
506 nbfds = limit.rlim_cur;
507 else
508 nbfds = 0;
509
510 if (nbfds <= 0)
511 nbfds = 1024; /* safe limit */
512
513 while (start < nbfds)
514 close(start++);
515}
Willy Tarreaue5733232019-05-22 19:24:06 +0200516#endif // defined(USE_POLL)
Willy Tarreau2d7f81b2019-02-21 22:19:17 +0100517
Willy Tarreau4f60f162007-04-08 16:39:58 +0200518/* disable the specified poller */
519void disable_poller(const char *poller_name)
520{
521 int p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522
Willy Tarreau4f60f162007-04-08 16:39:58 +0200523 for (p = 0; p < nbpollers; p++)
524 if (strcmp(pollers[p].name, poller_name) == 0)
525 pollers[p].pref = 0;
526}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200527
Olivier Houchard79321b92018-07-26 17:55:11 +0200528void poller_pipe_io_handler(int fd)
529{
530 char buf[1024];
531 /* Flush the pipe */
532 while (read(fd, buf, sizeof(buf)) > 0);
533 fd_cant_recv(fd);
534}
535
Willy Tarreau082b6282019-05-22 14:42:12 +0200536/* allocate the per-thread fd_updt thus needs to be called early after
537 * thread creation.
538 */
539static int alloc_pollers_per_thread()
540{
541 fd_updt = calloc(global.maxsock, sizeof(*fd_updt));
542 return fd_updt != NULL;
543}
544
545/* Initialize the pollers per thread.*/
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200546static int init_pollers_per_thread()
547{
Olivier Houchard79321b92018-07-26 17:55:11 +0200548 int mypipe[2];
Willy Tarreau082b6282019-05-22 14:42:12 +0200549
550 if (pipe(mypipe) < 0)
Olivier Houchard79321b92018-07-26 17:55:11 +0200551 return 0;
Willy Tarreau082b6282019-05-22 14:42:12 +0200552
Olivier Houchard79321b92018-07-26 17:55:11 +0200553 poller_rd_pipe = mypipe[0];
554 poller_wr_pipe[tid] = mypipe[1];
555 fcntl(poller_rd_pipe, F_SETFL, O_NONBLOCK);
556 fd_insert(poller_rd_pipe, poller_pipe_io_handler, poller_pipe_io_handler,
557 tid_bit);
558 fd_want_recv(poller_rd_pipe);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200559 return 1;
560}
561
562/* Deinitialize the pollers per thread */
563static void deinit_pollers_per_thread()
564{
William Lallemand808e1b72018-12-15 22:34:31 +0100565 /* rd and wr are init at the same place, but only rd is init to -1, so
566 we rely to rd to close. */
567 if (poller_rd_pipe > -1) {
568 close(poller_rd_pipe);
569 poller_rd_pipe = -1;
570 close(poller_wr_pipe[tid]);
571 poller_wr_pipe[tid] = -1;
572 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200573}
574
Willy Tarreau082b6282019-05-22 14:42:12 +0200575/* Release the pollers per thread, to be called late */
576static void free_pollers_per_thread()
577{
578 free(fd_updt);
579 fd_updt = NULL;
580}
581
Willy Tarreaubaaee002006-06-26 02:48:02 +0200582/*
Willy Tarreau4f60f162007-04-08 16:39:58 +0200583 * Initialize the pollers till the best one is found.
584 * If none works, returns 0, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200585 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200586int init_pollers()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200587{
Willy Tarreau4f60f162007-04-08 16:39:58 +0200588 int p;
589 struct poller *bp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200590
Christopher Faulet63fe6522017-08-31 17:52:09 +0200591 if ((fdtab = calloc(global.maxsock, sizeof(struct fdtab))) == NULL)
592 goto fail_tab;
593
Olivier Houchard53055052019-07-25 14:00:18 +0000594 if ((polled_mask = calloc(global.maxsock, sizeof(*polled_mask))) == NULL)
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200595 goto fail_polledmask;
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000596
Christopher Faulet63fe6522017-08-31 17:52:09 +0200597 if ((fdinfo = calloc(global.maxsock, sizeof(struct fdinfo))) == NULL)
598 goto fail_info;
599
Olivier Houchard6b96f722018-04-25 16:58:25 +0200600 update_list.first = update_list.last = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200601
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100602 for (p = 0; p < global.maxsock; p++) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100603 HA_SPIN_INIT(&fdtab[p].lock);
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100604 /* Mark the fd as out of the fd cache */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200605 fdtab[p].update.next = -3;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100606 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200607
Willy Tarreau4f60f162007-04-08 16:39:58 +0200608 do {
609 bp = NULL;
610 for (p = 0; p < nbpollers; p++)
611 if (!bp || (pollers[p].pref > bp->pref))
612 bp = &pollers[p];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200613
Willy Tarreau4f60f162007-04-08 16:39:58 +0200614 if (!bp || bp->pref == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200615 break;
616
Willy Tarreau4f60f162007-04-08 16:39:58 +0200617 if (bp->init(bp)) {
618 memcpy(&cur_poller, bp, sizeof(*bp));
619 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200620 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200621 } while (!bp || bp->pref == 0);
Willy Tarreau7be79a42012-11-11 15:02:54 +0100622
Christopher Faulet63fe6522017-08-31 17:52:09 +0200623 free(fdinfo);
624 fail_info:
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200625 free(polled_mask);
626 fail_polledmask:
Uman Shahzadda7eeed2019-01-17 08:21:39 +0000627 free(fdtab);
628 fail_tab:
Willy Tarreau7be79a42012-11-11 15:02:54 +0100629 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200630}
631
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200633 * Deinitialize the pollers.
634 */
635void deinit_pollers() {
636
637 struct poller *bp;
638 int p;
639
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200640 for (p = 0; p < global.maxsock; p++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100641 HA_SPIN_DESTROY(&fdtab[p].lock);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200642
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200643 for (p = 0; p < nbpollers; p++) {
644 bp = &pollers[p];
645
646 if (bp && bp->pref)
647 bp->term(bp);
648 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200649
Christopher Faulet63fe6522017-08-31 17:52:09 +0200650 free(fdinfo); fdinfo = NULL;
651 free(fdtab); fdtab = NULL;
Olivier Houchardcb92f5c2018-04-26 14:23:07 +0200652 free(polled_mask); polled_mask = NULL;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200653}
654
655/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200656 * Lists the known pollers on <out>.
657 * Should be performed only before initialization.
658 */
659int list_pollers(FILE *out)
660{
661 int p;
662 int last, next;
663 int usable;
664 struct poller *bp;
665
666 fprintf(out, "Available polling systems :\n");
667
668 usable = 0;
669 bp = NULL;
670 last = next = -1;
671 while (1) {
672 for (p = 0; p < nbpollers; p++) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200673 if ((next < 0 || pollers[p].pref > next)
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100674 && (last < 0 || pollers[p].pref < last)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200675 next = pollers[p].pref;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100676 if (!bp || (pollers[p].pref > bp->pref))
677 bp = &pollers[p];
678 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200679 }
680
681 if (next == -1)
682 break;
683
684 for (p = 0; p < nbpollers; p++) {
685 if (pollers[p].pref == next) {
686 fprintf(out, " %10s : ", pollers[p].name);
687 if (pollers[p].pref == 0)
688 fprintf(out, "disabled, ");
689 else
690 fprintf(out, "pref=%3d, ", pollers[p].pref);
691 if (pollers[p].test(&pollers[p])) {
692 fprintf(out, " test result OK");
693 if (next > 0)
694 usable++;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100695 } else {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200696 fprintf(out, " test result FAILED");
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100697 if (bp == &pollers[p])
698 bp = NULL;
699 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200700 fprintf(out, "\n");
701 }
702 }
703 last = next;
704 next = -1;
705 };
706 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
707 return 0;
708}
709
710/*
711 * Some pollers may lose their connection after a fork(). It may be necessary
712 * to create initialize part of them again. Returns 0 in case of failure,
713 * otherwise 1. The fork() function may be NULL if unused. In case of error,
714 * the the current poller is destroyed and the caller is responsible for trying
715 * another one by calling init_pollers() again.
716 */
717int fork_poller()
718{
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200719 int fd;
Willy Tarreauce036bc2018-01-29 14:58:02 +0100720 for (fd = 0; fd < global.maxsock; fd++) {
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200721 if (fdtab[fd].owner) {
722 fdtab[fd].cloned = 1;
723 }
724 }
725
Willy Tarreau2ff76222007-04-09 19:29:56 +0200726 if (cur_poller.fork) {
727 if (cur_poller.fork(&cur_poller))
728 return 1;
729 cur_poller.term(&cur_poller);
730 return 0;
731 }
732 return 1;
733}
734
Willy Tarreau082b6282019-05-22 14:42:12 +0200735REGISTER_PER_THREAD_ALLOC(alloc_pollers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100736REGISTER_PER_THREAD_INIT(init_pollers_per_thread);
737REGISTER_PER_THREAD_DEINIT(deinit_pollers_per_thread);
Willy Tarreau082b6282019-05-22 14:42:12 +0200738REGISTER_PER_THREAD_FREE(free_pollers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100739
Willy Tarreau2ff76222007-04-09 19:29:56 +0200740/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741 * Local variables:
742 * c-indent-level: 8
743 * c-basic-offset: 8
744 * End:
745 */