blob: a4c3bce9901ead9181dbd466a3c3a8ef9760971b [file] [log] [blame]
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001/*
2 * FD polling functions for FreeBSD kqueue()
3 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +01004 * Copyright 2000-2014 Willy Tarreau <w@1wt.eu>
Willy Tarreau1e63130a2007-04-09 12:03:06 +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 Tarreau1e63130a2007-04-09 12:03:06 +020011 */
12
Willy Tarreau1e63130a2007-04-09 12:03:06 +020013#include <unistd.h>
14#include <sys/time.h>
15#include <sys/types.h>
16
17#include <sys/event.h>
18#include <sys/time.h>
19
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020020#include <haproxy/api.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020021#include <haproxy/thread-t.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020022#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020023#include <haproxy/time.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020024
Willy Tarreauf268ee82020-06-04 17:05:57 +020025#include <haproxy/global.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020026
Willy Tarreaua04ded52020-06-02 10:29:48 +020027#include <haproxy/activity.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020028#include <haproxy/fd.h>
Willy Tarreaubeb859a2018-11-22 18:07:59 +010029#include <proto/signal.h>
Willy Tarreau10146c92015-04-13 20:44:19 +020030
Willy Tarreau1e63130a2007-04-09 12:03:06 +020031
32/* private data */
Willy Tarreau7a2364d2018-01-19 08:56:14 +010033static int kqueue_fd[MAX_THREADS]; // per-thread kqueue_fd
Christopher Fauletd4604ad2017-05-29 10:40:41 +020034static THREAD_LOCAL struct kevent *kev = NULL;
Olivier Houchardebaba752018-04-16 13:24:48 +020035static struct kevent *kev_out = NULL; // Trash buffer for kevent() to write the eventlist in
Willy Tarreau1e63130a2007-04-09 12:03:06 +020036
PiBa-NLc55b88e2018-05-10 01:01:28 +020037static int _update_fd(int fd, int start)
Olivier Houchard6b96f722018-04-25 16:58:25 +020038{
39 int en;
PiBa-NLc55b88e2018-05-10 01:01:28 +020040 int changes = start;
Olivier Houchard6b96f722018-04-25 16:58:25 +020041
42 en = fdtab[fd].state;
43
Willy Tarreau5bee3e22019-09-04 09:52:57 +020044 if (!(fdtab[fd].thread_mask & tid_bit) || !(en & FD_EV_ACTIVE_RW)) {
Olivier Houchard53055052019-07-25 14:00:18 +000045 if (!(polled_mask[fd].poll_recv & tid_bit) &&
46 !(polled_mask[fd].poll_send & tid_bit)) {
Olivier Houchard6b96f722018-04-25 16:58:25 +020047 /* fd was not watched, it's still not */
Olivier Houchard5ab33942018-09-11 14:44:51 +020048 return changes;
Olivier Houchard6b96f722018-04-25 16:58:25 +020049 }
50 /* fd totally removed from poll list */
51 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
52 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
Olivier Houchard53055052019-07-25 14:00:18 +000053 if (polled_mask[fd].poll_recv & tid_bit)
54 _HA_ATOMIC_AND(&polled_mask[fd].poll_recv, ~tid_bit);
55 if (polled_mask[fd].poll_send & tid_bit)
56 _HA_ATOMIC_AND(&polled_mask[fd].poll_send, ~tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +020057 }
58 else {
59 /* OK fd has to be monitored, it was either added or changed */
60
Willy Tarreau5bee3e22019-09-04 09:52:57 +020061 if (en & FD_EV_ACTIVE_R) {
Olivier Houchard53055052019-07-25 14:00:18 +000062 if (!(polled_mask[fd].poll_recv & tid_bit)) {
63 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
64 _HA_ATOMIC_OR(&polled_mask[fd].poll_recv, tid_bit);
65 }
66 }
67 else if (polled_mask[fd].poll_recv & tid_bit) {
Olivier Houchard6b96f722018-04-25 16:58:25 +020068 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
Olivier Houchard53055052019-07-25 14:00:18 +000069 HA_ATOMIC_AND(&polled_mask[fd].poll_recv, ~tid_bit);
70 }
Olivier Houchard6b96f722018-04-25 16:58:25 +020071
Willy Tarreau5bee3e22019-09-04 09:52:57 +020072 if (en & FD_EV_ACTIVE_W) {
Olivier Houchard53055052019-07-25 14:00:18 +000073 if (!(polled_mask[fd].poll_send & tid_bit)) {
74 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
75 _HA_ATOMIC_OR(&polled_mask[fd].poll_send, tid_bit);
76 }
77 }
78 else if (polled_mask[fd].poll_send & tid_bit) {
Olivier Houchard6b96f722018-04-25 16:58:25 +020079 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
Olivier Houchard53055052019-07-25 14:00:18 +000080 _HA_ATOMIC_AND(&polled_mask[fd].poll_send, ~tid_bit);
81 }
Olivier Houchard6b96f722018-04-25 16:58:25 +020082
Olivier Houchard6b96f722018-04-25 16:58:25 +020083 }
84 return changes;
85}
86
Willy Tarreau1e63130a2007-04-09 12:03:06 +020087/*
Willy Tarreau4a226272012-11-11 20:49:49 +010088 * kqueue() poller
Willy Tarreau1e63130a2007-04-09 12:03:06 +020089 */
Willy Tarreau03e78532020-02-25 07:38:05 +010090static void _do_poll(struct poller *p, int exp, int wake)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020091{
Willy Tarreau4a226272012-11-11 20:49:49 +010092 int status;
Willy Tarreaubeb859a2018-11-22 18:07:59 +010093 int count, fd, wait_time;
94 struct timespec timeout_ts;
Olivier Houchard6b96f722018-04-25 16:58:25 +020095 int updt_idx;
Willy Tarreau4a226272012-11-11 20:49:49 +010096 int changes = 0;
Olivier Houchard6b96f722018-04-25 16:58:25 +020097 int old_fd;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020098
Willy Tarreaubeb859a2018-11-22 18:07:59 +010099 timeout_ts.tv_sec = 0;
100 timeout_ts.tv_nsec = 0;
Willy Tarreau4a226272012-11-11 20:49:49 +0100101 /* first, scan the update list to find changes */
102 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
103 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100104
Olivier Houchardcb6c9272019-03-08 18:49:54 +0100105 _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100106 if (!fdtab[fd].owner) {
107 activity[tid].poll_drop++;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100108 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100109 }
PiBa-NLc55b88e2018-05-10 01:01:28 +0200110 changes = _update_fd(fd, changes);
Olivier Houchard6b96f722018-04-25 16:58:25 +0200111 }
112 /* Scan the global update list */
113 for (old_fd = fd = update_list.first; fd != -1; fd = fdtab[fd].update.next) {
114 if (fd == -2) {
115 fd = old_fd;
116 continue;
Willy Tarreau4a226272012-11-11 20:49:49 +0100117 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200118 else if (fd <= -3)
119 fd = -fd -4;
120 if (fd == -1)
121 break;
122 if (fdtab[fd].update_mask & tid_bit)
123 done_update_polling(fd);
124 else
125 continue;
126 if (!fdtab[fd].owner)
127 continue;
PiBa-NLc55b88e2018-05-10 01:01:28 +0200128 changes = _update_fd(fd, changes);
Willy Tarreau4a226272012-11-11 20:49:49 +0100129 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200130
Willy Tarreau60b639c2018-08-02 10:16:17 +0200131 thread_harmless_now();
132
Olivier Houchardebaba752018-04-16 13:24:48 +0200133 if (changes) {
134#ifdef EV_RECEIPT
135 kev[0].flags |= EV_RECEIPT;
136#else
137 /* If EV_RECEIPT isn't defined, just add an invalid entry,
138 * so that we get an error and kevent() stops before scanning
139 * the kqueue.
140 */
141 EV_SET(&kev[changes++], -1, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
142#endif
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100143 kevent(kqueue_fd[tid], kev, changes, kev_out, changes, &timeout_ts);
Olivier Houchardebaba752018-04-16 13:24:48 +0200144 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100145 fd_nbupdt = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200146
Willy Tarreauf37ba942018-10-17 11:25:54 +0200147 /* now let's wait for events */
Willy Tarreau2ae84e42019-05-28 16:44:05 +0200148 wait_time = wake ? 0 : compute_poll_timeout(exp);
Willy Tarreauce036bc2018-01-29 14:58:02 +0100149 fd = global.tune.maxpollevents;
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200150 tv_entering_poll();
Willy Tarreau609aad92018-11-22 08:31:09 +0100151 activity_count_runtime();
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100152
153 do {
154 int timeout = (global.tune.options & GTUNE_BUSY_POLLING) ? 0 : wait_time;
155
156 timeout_ts.tv_sec = (timeout / 1000);
157 timeout_ts.tv_nsec = (timeout % 1000) * 1000000;
158
159 status = kevent(kqueue_fd[tid], // int kq
160 NULL, // const struct kevent *changelist
161 0, // int nchanges
162 kev, // struct kevent *eventlist
163 fd, // int nevents
164 &timeout_ts); // const struct timespec *timeout
165 tv_update_date(timeout, status);
166
167 if (status)
168 break;
169 if (timeout || !wait_time)
170 break;
Willy Tarreau2ae84e42019-05-28 16:44:05 +0200171 if (signal_queue_len || wake)
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100172 break;
173 if (tick_isset(exp) && tick_is_expired(exp, now_ms))
174 break;
175 } while (1);
176
177 tv_leaving_poll(wait_time, status);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200178
Willy Tarreau60b639c2018-08-02 10:16:17 +0200179 thread_harmless_end();
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200180 if (sleeping_thread_mask & tid_bit)
181 _HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200182
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200183 for (count = 0; count < status; count++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200184 unsigned int n = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200185 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200186
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100187 if (!fdtab[fd].owner) {
188 activity[tid].poll_dead++;
Willy Tarreau076be252012-07-06 16:02:29 +0200189 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100190 }
191
192 if (!(fdtab[fd].thread_mask & tid_bit)) {
193 activity[tid].poll_skip++;
194 continue;
195 }
Willy Tarreau076be252012-07-06 16:02:29 +0200196
Willy Tarreau6b308982019-09-06 19:05:50 +0200197 if (kev[count].filter == EVFILT_READ) {
Olivier Houchardeaefc3c2019-12-10 18:22:55 +0100198 if (kev[count].data || !(kev[count].flags & EV_EOF))
Willy Tarreau6b308982019-09-06 19:05:50 +0200199 n |= FD_EV_READY_R;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100200 if (kev[count].flags & EV_EOF)
Willy Tarreau6b308982019-09-06 19:05:50 +0200201 n |= FD_EV_SHUT_R;
Willy Tarreau4a226272012-11-11 20:49:49 +0100202 }
Willy Tarreau6b308982019-09-06 19:05:50 +0200203 else if (kev[count].filter == EVFILT_WRITE) {
204 n |= FD_EV_READY_W;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100205 if (kev[count].flags & EV_EOF)
Willy Tarreau6b308982019-09-06 19:05:50 +0200206 n |= FD_EV_ERR_RW;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200207 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100208
Christopher Fauletab62f512017-08-30 10:34:36 +0200209 fd_update_events(fd, n);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200210 }
211}
212
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200213
214static int init_kqueue_per_thread()
215{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100216 int fd;
217
Olivier Houchardebaba752018-04-16 13:24:48 +0200218 /* we can have up to two events per fd, so allocate enough to store
219 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
220 * so that we can add an invalid entry and get an error, to avoid
221 * scanning the kqueue uselessly.
222 */
223 kev = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200224 if (kev == NULL)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100225 goto fail_alloc;
226
Christopher Faulet727c89b2018-01-25 16:40:35 +0100227 if (MAX_THREADS > 1 && tid) {
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100228 kqueue_fd[tid] = kqueue();
229 if (kqueue_fd[tid] < 0)
230 goto fail_fd;
231 }
232
233 /* we may have to unregister some events initially registered on the
234 * original fd when it was alone, and/or to register events on the new
235 * fd for this thread. Let's just mark them as updated, the poller will
236 * do the rest.
237 */
Willy Tarreauce036bc2018-01-29 14:58:02 +0100238 for (fd = 0; fd < global.maxsock; fd++)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100239 updt_fd_polling(fd);
240
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200241 return 1;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100242 fail_fd:
243 free(kev);
244 fail_alloc:
245 return 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200246}
247
248static void deinit_kqueue_per_thread()
249{
Christopher Faulet727c89b2018-01-25 16:40:35 +0100250 if (MAX_THREADS > 1 && tid)
Christopher Faulet13b007d2018-01-25 16:32:18 +0100251 close(kqueue_fd[tid]);
252
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200253 free(kev);
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200254 kev = NULL;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200255}
256
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200257/*
258 * Initialization of the kqueue() poller.
259 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
260 * disables the poller by setting its pref to 0.
261 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100262static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200263{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200264 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200265
Olivier Houchardebaba752018-04-16 13:24:48 +0200266 /* we can have up to two events per fd, so allocate enough to store
267 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
268 * so that we can add an invalid entry and get an error, to avoid
269 * scanning the kqueue uselessly.
270 */
271 kev_out = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
272 if (!kev_out)
273 goto fail_alloc;
274
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100275 kqueue_fd[tid] = kqueue();
276 if (kqueue_fd[tid] < 0)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200277 goto fail_fd;
278
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200279 hap_register_per_thread_init(init_kqueue_per_thread);
280 hap_register_per_thread_deinit(deinit_kqueue_per_thread);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200281 return 1;
282
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200283 fail_fd:
Olivier Houchardebaba752018-04-16 13:24:48 +0200284 free(kev_out);
285 kev_out = NULL;
286fail_alloc:
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200287 p->pref = 0;
288 return 0;
289}
290
291/*
292 * Termination of the kqueue() poller.
293 * Memory is released and the poller is marked as unselectable.
294 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100295static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200296{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100297 if (kqueue_fd[tid] >= 0) {
298 close(kqueue_fd[tid]);
299 kqueue_fd[tid] = -1;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200300 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200301
302 p->private = NULL;
303 p->pref = 0;
Olivier Houchardebaba752018-04-16 13:24:48 +0200304 if (kev_out) {
305 free(kev_out);
306 kev_out = NULL;
307 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200308}
309
310/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200311 * Check that the poller works.
312 * Returns 1 if OK, otherwise 0.
313 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100314static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200315{
316 int fd;
317
318 fd = kqueue();
319 if (fd < 0)
320 return 0;
321 close(fd);
322 return 1;
323}
324
325/*
326 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
327 * otherwise 0. Note that some pollers need to be reopened after a fork()
328 * (such as kqueue), and some others may fail to do so in a chroot.
329 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100330static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200331{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100332 kqueue_fd[tid] = kqueue();
333 if (kqueue_fd[tid] < 0)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200334 return 0;
335 return 1;
336}
337
338/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200339 * It is a constructor, which means that it will automatically be called before
340 * main(). This is GCC-specific but it works at least since 2.95.
341 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200342 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200343__attribute__((constructor))
344static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200345{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200346 struct poller *p;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100347 int i;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200348
349 if (nbpollers >= MAX_POLLERS)
350 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200351
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100352 for (i = 0; i < MAX_THREADS; i++)
353 kqueue_fd[i] = -1;
354
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200355 p = &pollers[nbpollers++];
356
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200357 p->name = "kqueue";
358 p->pref = 300;
Willy Tarreau11ef0832019-11-28 18:17:33 +0100359 p->flags = HAP_POLL_F_RDHUP | HAP_POLL_F_ERRHUP;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200360 p->private = NULL;
361
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100362 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200363 p->test = _do_test;
364 p->init = _do_init;
365 p->term = _do_term;
366 p->poll = _do_poll;
367 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200368}
369
370
371/*
372 * Local variables:
373 * c-indent-level: 8
374 * c-basic-offset: 8
375 * End:
376 */