blob: 43643fb38754d46435ae100f2e2c6eb7ac1bdd94 [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 Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/activity.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreau55542642021-10-08 09:33:24 +020022#include <haproxy/clock.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/fd.h>
24#include <haproxy/global.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020025#include <haproxy/signal.h>
Willy Tarreau6dfab112021-09-30 17:53:22 +020026#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020027#include <haproxy/ticks.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020028
Willy Tarreau1e63130a2007-04-09 12:03:06 +020029
30/* private data */
Willy Tarreau8209c9a2021-04-10 17:09:53 +020031static int kqueue_fd[MAX_THREADS] __read_mostly; // per-thread kqueue_fd
Christopher Fauletd4604ad2017-05-29 10:40:41 +020032static THREAD_LOCAL struct kevent *kev = NULL;
Olivier Houchardebaba752018-04-16 13:24:48 +020033static struct kevent *kev_out = NULL; // Trash buffer for kevent() to write the eventlist in
Willy Tarreau1e63130a2007-04-09 12:03:06 +020034
PiBa-NLc55b88e2018-05-10 01:01:28 +020035static int _update_fd(int fd, int start)
Olivier Houchard6b96f722018-04-25 16:58:25 +020036{
37 int en;
PiBa-NLc55b88e2018-05-10 01:01:28 +020038 int changes = start;
Olivier Houchard6b96f722018-04-25 16:58:25 +020039
40 en = fdtab[fd].state;
41
Willy Tarreau5bee3e22019-09-04 09:52:57 +020042 if (!(fdtab[fd].thread_mask & tid_bit) || !(en & FD_EV_ACTIVE_RW)) {
Olivier Houchard53055052019-07-25 14:00:18 +000043 if (!(polled_mask[fd].poll_recv & tid_bit) &&
44 !(polled_mask[fd].poll_send & tid_bit)) {
Olivier Houchard6b96f722018-04-25 16:58:25 +020045 /* fd was not watched, it's still not */
Olivier Houchard5ab33942018-09-11 14:44:51 +020046 return changes;
Olivier Houchard6b96f722018-04-25 16:58:25 +020047 }
48 /* fd totally removed from poll list */
49 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
50 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
Olivier Houchard53055052019-07-25 14:00:18 +000051 if (polled_mask[fd].poll_recv & tid_bit)
52 _HA_ATOMIC_AND(&polled_mask[fd].poll_recv, ~tid_bit);
53 if (polled_mask[fd].poll_send & tid_bit)
54 _HA_ATOMIC_AND(&polled_mask[fd].poll_send, ~tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +020055 }
56 else {
57 /* OK fd has to be monitored, it was either added or changed */
58
Willy Tarreau5bee3e22019-09-04 09:52:57 +020059 if (en & FD_EV_ACTIVE_R) {
Olivier Houchard53055052019-07-25 14:00:18 +000060 if (!(polled_mask[fd].poll_recv & tid_bit)) {
61 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
62 _HA_ATOMIC_OR(&polled_mask[fd].poll_recv, tid_bit);
63 }
64 }
65 else if (polled_mask[fd].poll_recv & tid_bit) {
Olivier Houchard6b96f722018-04-25 16:58:25 +020066 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
Olivier Houchard53055052019-07-25 14:00:18 +000067 HA_ATOMIC_AND(&polled_mask[fd].poll_recv, ~tid_bit);
68 }
Olivier Houchard6b96f722018-04-25 16:58:25 +020069
Willy Tarreau5bee3e22019-09-04 09:52:57 +020070 if (en & FD_EV_ACTIVE_W) {
Olivier Houchard53055052019-07-25 14:00:18 +000071 if (!(polled_mask[fd].poll_send & tid_bit)) {
72 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
73 _HA_ATOMIC_OR(&polled_mask[fd].poll_send, tid_bit);
74 }
75 }
76 else if (polled_mask[fd].poll_send & tid_bit) {
Olivier Houchard6b96f722018-04-25 16:58:25 +020077 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
Olivier Houchard53055052019-07-25 14:00:18 +000078 _HA_ATOMIC_AND(&polled_mask[fd].poll_send, ~tid_bit);
79 }
Olivier Houchard6b96f722018-04-25 16:58:25 +020080
Olivier Houchard6b96f722018-04-25 16:58:25 +020081 }
82 return changes;
83}
84
Willy Tarreau1e63130a2007-04-09 12:03:06 +020085/*
Willy Tarreau4a226272012-11-11 20:49:49 +010086 * kqueue() poller
Willy Tarreau1e63130a2007-04-09 12:03:06 +020087 */
Willy Tarreau03e78532020-02-25 07:38:05 +010088static void _do_poll(struct poller *p, int exp, int wake)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020089{
Willy Tarreau4a226272012-11-11 20:49:49 +010090 int status;
Willy Tarreaubeb859a2018-11-22 18:07:59 +010091 int count, fd, wait_time;
92 struct timespec timeout_ts;
Olivier Houchard6b96f722018-04-25 16:58:25 +020093 int updt_idx;
Willy Tarreau4a226272012-11-11 20:49:49 +010094 int changes = 0;
Olivier Houchard6b96f722018-04-25 16:58:25 +020095 int old_fd;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020096
Willy Tarreaubeb859a2018-11-22 18:07:59 +010097 timeout_ts.tv_sec = 0;
98 timeout_ts.tv_nsec = 0;
Willy Tarreau4a226272012-11-11 20:49:49 +010099 /* first, scan the update list to find changes */
100 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
101 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100102
Olivier Houchardcb6c9272019-03-08 18:49:54 +0100103 _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100104 if (!fdtab[fd].owner) {
Willy Tarreaue4063862020-06-17 20:35:33 +0200105 activity[tid].poll_drop_fd++;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100106 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100107 }
PiBa-NLc55b88e2018-05-10 01:01:28 +0200108 changes = _update_fd(fd, changes);
Olivier Houchard6b96f722018-04-25 16:58:25 +0200109 }
110 /* Scan the global update list */
111 for (old_fd = fd = update_list.first; fd != -1; fd = fdtab[fd].update.next) {
112 if (fd == -2) {
113 fd = old_fd;
114 continue;
Willy Tarreau4a226272012-11-11 20:49:49 +0100115 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200116 else if (fd <= -3)
117 fd = -fd -4;
118 if (fd == -1)
119 break;
120 if (fdtab[fd].update_mask & tid_bit)
121 done_update_polling(fd);
122 else
123 continue;
124 if (!fdtab[fd].owner)
125 continue;
PiBa-NLc55b88e2018-05-10 01:01:28 +0200126 changes = _update_fd(fd, changes);
Willy Tarreau4a226272012-11-11 20:49:49 +0100127 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200128
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200129 thread_idle_now();
Willy Tarreau60b639c2018-08-02 10:16:17 +0200130 thread_harmless_now();
131
Olivier Houchardebaba752018-04-16 13:24:48 +0200132 if (changes) {
133#ifdef EV_RECEIPT
134 kev[0].flags |= EV_RECEIPT;
135#else
136 /* If EV_RECEIPT isn't defined, just add an invalid entry,
137 * so that we get an error and kevent() stops before scanning
138 * the kqueue.
139 */
140 EV_SET(&kev[changes++], -1, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
141#endif
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100142 kevent(kqueue_fd[tid], kev, changes, kev_out, changes, &timeout_ts);
Olivier Houchardebaba752018-04-16 13:24:48 +0200143 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100144 fd_nbupdt = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200145
Willy Tarreauf37ba942018-10-17 11:25:54 +0200146 /* now let's wait for events */
Willy Tarreau2ae84e42019-05-28 16:44:05 +0200147 wait_time = wake ? 0 : compute_poll_timeout(exp);
Willy Tarreauce036bc2018-01-29 14:58:02 +0100148 fd = global.tune.maxpollevents;
Willy Tarreauf9d5e102021-10-08 10:43:59 +0200149 clock_entering_poll();
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100150
151 do {
152 int timeout = (global.tune.options & GTUNE_BUSY_POLLING) ? 0 : wait_time;
153
154 timeout_ts.tv_sec = (timeout / 1000);
155 timeout_ts.tv_nsec = (timeout % 1000) * 1000000;
156
157 status = kevent(kqueue_fd[tid], // int kq
158 NULL, // const struct kevent *changelist
159 0, // int nchanges
160 kev, // struct kevent *eventlist
161 fd, // int nevents
162 &timeout_ts); // const struct timespec *timeout
Willy Tarreau55542642021-10-08 09:33:24 +0200163 clock_update_date(timeout, status);
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100164
Willy Tarreaue5451532020-06-17 20:25:18 +0200165 if (status) {
166 activity[tid].poll_io++;
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100167 break;
Willy Tarreaue5451532020-06-17 20:25:18 +0200168 }
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100169 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
Willy Tarreau058b2c12022-06-22 15:21:34 +0200177 fd_leaving_poll(wait_time, status);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200178
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200179 for (count = 0; count < status; count++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200180 unsigned int n = 0;
Willy Tarreau200bd502021-07-29 16:57:19 +0200181 int ret;
182
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200183 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200184
Willy Tarreau38e8a1c2020-06-23 10:04:54 +0200185#ifdef DEBUG_FD
Willy Tarreau4781b152021-04-06 13:53:36 +0200186 _HA_ATOMIC_INC(&fdtab[fd].event_count);
Willy Tarreau38e8a1c2020-06-23 10:04:54 +0200187#endif
Willy Tarreau6b308982019-09-06 19:05:50 +0200188 if (kev[count].filter == EVFILT_READ) {
Olivier Houchardeaefc3c2019-12-10 18:22:55 +0100189 if (kev[count].data || !(kev[count].flags & EV_EOF))
Willy Tarreau6b308982019-09-06 19:05:50 +0200190 n |= FD_EV_READY_R;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100191 if (kev[count].flags & EV_EOF)
Willy Tarreau6b308982019-09-06 19:05:50 +0200192 n |= FD_EV_SHUT_R;
Willy Tarreau4a226272012-11-11 20:49:49 +0100193 }
Willy Tarreau6b308982019-09-06 19:05:50 +0200194 else if (kev[count].filter == EVFILT_WRITE) {
195 n |= FD_EV_READY_W;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100196 if (kev[count].flags & EV_EOF)
Willy Tarreau6b308982019-09-06 19:05:50 +0200197 n |= FD_EV_ERR_RW;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200198 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100199
Willy Tarreau200bd502021-07-29 16:57:19 +0200200 ret = fd_update_events(fd, n);
201
202 if (ret == FD_UPDT_MIGRATED) {
203 /* FD was migrated, let's stop polling it */
204 if (!HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid))
205 fd_updt[fd_nbupdt++] = fd;
206 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200207 }
208}
209
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200210
211static int init_kqueue_per_thread()
212{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100213 int fd;
214
Olivier Houchardebaba752018-04-16 13:24:48 +0200215 /* we can have up to two events per fd, so allocate enough to store
216 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
217 * so that we can add an invalid entry and get an error, to avoid
218 * scanning the kqueue uselessly.
219 */
220 kev = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200221 if (kev == NULL)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100222 goto fail_alloc;
223
Christopher Faulet727c89b2018-01-25 16:40:35 +0100224 if (MAX_THREADS > 1 && tid) {
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100225 kqueue_fd[tid] = kqueue();
226 if (kqueue_fd[tid] < 0)
227 goto fail_fd;
228 }
229
230 /* we may have to unregister some events initially registered on the
231 * original fd when it was alone, and/or to register events on the new
232 * fd for this thread. Let's just mark them as updated, the poller will
233 * do the rest.
234 */
Willy Tarreauce036bc2018-01-29 14:58:02 +0100235 for (fd = 0; fd < global.maxsock; fd++)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100236 updt_fd_polling(fd);
237
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200238 return 1;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100239 fail_fd:
240 free(kev);
241 fail_alloc:
242 return 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200243}
244
245static void deinit_kqueue_per_thread()
246{
Christopher Faulet727c89b2018-01-25 16:40:35 +0100247 if (MAX_THREADS > 1 && tid)
Christopher Faulet13b007d2018-01-25 16:32:18 +0100248 close(kqueue_fd[tid]);
249
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100250 ha_free(&kev);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200251}
252
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200253/*
254 * Initialization of the kqueue() poller.
255 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
256 * disables the poller by setting its pref to 0.
257 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100258static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200259{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200260 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200261
Olivier Houchardebaba752018-04-16 13:24:48 +0200262 /* we can have up to two events per fd, so allocate enough to store
263 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
264 * so that we can add an invalid entry and get an error, to avoid
265 * scanning the kqueue uselessly.
266 */
267 kev_out = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
268 if (!kev_out)
269 goto fail_alloc;
270
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100271 kqueue_fd[tid] = kqueue();
272 if (kqueue_fd[tid] < 0)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200273 goto fail_fd;
274
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200275 hap_register_per_thread_init(init_kqueue_per_thread);
276 hap_register_per_thread_deinit(deinit_kqueue_per_thread);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200277 return 1;
278
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200279 fail_fd:
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100280 ha_free(&kev_out);
Olivier Houchardebaba752018-04-16 13:24:48 +0200281fail_alloc:
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200282 p->pref = 0;
283 return 0;
284}
285
286/*
287 * Termination of the kqueue() poller.
288 * Memory is released and the poller is marked as unselectable.
289 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100290static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200291{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100292 if (kqueue_fd[tid] >= 0) {
293 close(kqueue_fd[tid]);
294 kqueue_fd[tid] = -1;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200295 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200296
297 p->private = NULL;
298 p->pref = 0;
Olivier Houchardebaba752018-04-16 13:24:48 +0200299 if (kev_out) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100300 ha_free(&kev_out);
Olivier Houchardebaba752018-04-16 13:24:48 +0200301 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200302}
303
304/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200305 * Check that the poller works.
306 * Returns 1 if OK, otherwise 0.
307 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100308static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200309{
310 int fd;
311
312 fd = kqueue();
313 if (fd < 0)
314 return 0;
315 close(fd);
316 return 1;
317}
318
319/*
320 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
321 * otherwise 0. Note that some pollers need to be reopened after a fork()
322 * (such as kqueue), and some others may fail to do so in a chroot.
323 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100324static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200325{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100326 kqueue_fd[tid] = kqueue();
327 if (kqueue_fd[tid] < 0)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200328 return 0;
329 return 1;
330}
331
332/*
Willy Tarreau740d7492022-04-25 19:00:55 +0200333 * Registers the poller.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200334 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200335static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200336{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200337 struct poller *p;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100338 int i;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200339
340 if (nbpollers >= MAX_POLLERS)
341 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200342
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100343 for (i = 0; i < MAX_THREADS; i++)
344 kqueue_fd[i] = -1;
345
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200346 p = &pollers[nbpollers++];
347
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200348 p->name = "kqueue";
349 p->pref = 300;
Willy Tarreau11ef0832019-11-28 18:17:33 +0100350 p->flags = HAP_POLL_F_RDHUP | HAP_POLL_F_ERRHUP;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200351 p->private = NULL;
352
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100353 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200354 p->test = _do_test;
355 p->init = _do_init;
356 p->term = _do_term;
357 p->poll = _do_poll;
358 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200359}
360
Willy Tarreau740d7492022-04-25 19:00:55 +0200361INITCALL0(STG_REGISTER, _do_register);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200362
363/*
364 * Local variables:
365 * c-indent-level: 8
366 * c-basic-offset: 8
367 * End:
368 */