blob: 692437731b0c9551d6aec6dcf8dcb1407060d559 [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
20#include <common/compat.h>
21#include <common/config.h>
Willy Tarreau60b639c2018-08-02 10:16:17 +020022#include <common/hathreads.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020023#include <common/ticks.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020024#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020025#include <common/tools.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020026
Willy Tarreau1e63130a2007-04-09 12:03:06 +020027#include <types/global.h>
28
Willy Tarreau609aad92018-11-22 08:31:09 +010029#include <proto/activity.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020030#include <proto/fd.h>
Willy Tarreaubeb859a2018-11-22 18:07:59 +010031#include <proto/signal.h>
Willy Tarreau10146c92015-04-13 20:44:19 +020032
Willy Tarreau1e63130a2007-04-09 12:03:06 +020033
34/* private data */
Willy Tarreau7a2364d2018-01-19 08:56:14 +010035static int kqueue_fd[MAX_THREADS]; // per-thread kqueue_fd
Christopher Fauletd4604ad2017-05-29 10:40:41 +020036static THREAD_LOCAL struct kevent *kev = NULL;
Olivier Houchardebaba752018-04-16 13:24:48 +020037static struct kevent *kev_out = NULL; // Trash buffer for kevent() to write the eventlist in
Willy Tarreau1e63130a2007-04-09 12:03:06 +020038
PiBa-NLc55b88e2018-05-10 01:01:28 +020039static int _update_fd(int fd, int start)
Olivier Houchard6b96f722018-04-25 16:58:25 +020040{
41 int en;
PiBa-NLc55b88e2018-05-10 01:01:28 +020042 int changes = start;
Olivier Houchard6b96f722018-04-25 16:58:25 +020043
44 en = fdtab[fd].state;
45
46 if (!(fdtab[fd].thread_mask & tid_bit) || !(en & FD_EV_POLLED_RW)) {
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020047 if (!(polled_mask[fd] & tid_bit)) {
Olivier Houchard6b96f722018-04-25 16:58:25 +020048 /* fd was not watched, it's still not */
Olivier Houchard5ab33942018-09-11 14:44:51 +020049 return changes;
Olivier Houchard6b96f722018-04-25 16:58:25 +020050 }
51 /* fd totally removed from poll list */
52 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
53 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
Olivier Houchardcb6c9272019-03-08 18:49:54 +010054 _HA_ATOMIC_AND(&polled_mask[fd], ~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
59 if (en & FD_EV_POLLED_R)
60 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020061 else if (polled_mask[fd] & tid_bit)
Olivier Houchard6b96f722018-04-25 16:58:25 +020062 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
63
64 if (en & FD_EV_POLLED_W)
65 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020066 else if (polled_mask[fd] & tid_bit)
Olivier Houchard6b96f722018-04-25 16:58:25 +020067 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
68
Olivier Houchardcb6c9272019-03-08 18:49:54 +010069 _HA_ATOMIC_OR(&polled_mask[fd], tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +020070 }
71 return changes;
72}
73
Willy Tarreau1e63130a2007-04-09 12:03:06 +020074/*
Willy Tarreau4a226272012-11-11 20:49:49 +010075 * kqueue() poller
Willy Tarreau1e63130a2007-04-09 12:03:06 +020076 */
Willy Tarreau2ae84e42019-05-28 16:44:05 +020077REGPRM3 static void _do_poll(struct poller *p, int exp, int wake)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020078{
Willy Tarreau4a226272012-11-11 20:49:49 +010079 int status;
Willy Tarreaubeb859a2018-11-22 18:07:59 +010080 int count, fd, wait_time;
81 struct timespec timeout_ts;
Olivier Houchard6b96f722018-04-25 16:58:25 +020082 int updt_idx;
Willy Tarreau4a226272012-11-11 20:49:49 +010083 int changes = 0;
Olivier Houchard6b96f722018-04-25 16:58:25 +020084 int old_fd;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020085
Willy Tarreaubeb859a2018-11-22 18:07:59 +010086 timeout_ts.tv_sec = 0;
87 timeout_ts.tv_nsec = 0;
Willy Tarreau4a226272012-11-11 20:49:49 +010088 /* first, scan the update list to find changes */
89 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
90 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +010091
Olivier Houchardcb6c9272019-03-08 18:49:54 +010092 _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010093 if (!fdtab[fd].owner) {
94 activity[tid].poll_drop++;
Willy Tarreauf817e9f2014-01-10 16:58:45 +010095 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010096 }
PiBa-NLc55b88e2018-05-10 01:01:28 +020097 changes = _update_fd(fd, changes);
Olivier Houchard6b96f722018-04-25 16:58:25 +020098 }
99 /* Scan the global update list */
100 for (old_fd = fd = update_list.first; fd != -1; fd = fdtab[fd].update.next) {
101 if (fd == -2) {
102 fd = old_fd;
103 continue;
Willy Tarreau4a226272012-11-11 20:49:49 +0100104 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200105 else if (fd <= -3)
106 fd = -fd -4;
107 if (fd == -1)
108 break;
109 if (fdtab[fd].update_mask & tid_bit)
110 done_update_polling(fd);
111 else
112 continue;
113 if (!fdtab[fd].owner)
114 continue;
PiBa-NLc55b88e2018-05-10 01:01:28 +0200115 changes = _update_fd(fd, changes);
Willy Tarreau4a226272012-11-11 20:49:49 +0100116 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200117
Willy Tarreau60b639c2018-08-02 10:16:17 +0200118 thread_harmless_now();
119
Olivier Houchardebaba752018-04-16 13:24:48 +0200120 if (changes) {
121#ifdef EV_RECEIPT
122 kev[0].flags |= EV_RECEIPT;
123#else
124 /* If EV_RECEIPT isn't defined, just add an invalid entry,
125 * so that we get an error and kevent() stops before scanning
126 * the kqueue.
127 */
128 EV_SET(&kev[changes++], -1, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
129#endif
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100130 kevent(kqueue_fd[tid], kev, changes, kev_out, changes, &timeout_ts);
Olivier Houchardebaba752018-04-16 13:24:48 +0200131 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100132 fd_nbupdt = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200133
Willy Tarreauf37ba942018-10-17 11:25:54 +0200134 /* now let's wait for events */
Willy Tarreau2ae84e42019-05-28 16:44:05 +0200135 wait_time = wake ? 0 : compute_poll_timeout(exp);
Willy Tarreauce036bc2018-01-29 14:58:02 +0100136 fd = global.tune.maxpollevents;
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200137 tv_entering_poll();
Willy Tarreau609aad92018-11-22 08:31:09 +0100138 activity_count_runtime();
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100139
140 do {
141 int timeout = (global.tune.options & GTUNE_BUSY_POLLING) ? 0 : wait_time;
142
143 timeout_ts.tv_sec = (timeout / 1000);
144 timeout_ts.tv_nsec = (timeout % 1000) * 1000000;
145
146 status = kevent(kqueue_fd[tid], // int kq
147 NULL, // const struct kevent *changelist
148 0, // int nchanges
149 kev, // struct kevent *eventlist
150 fd, // int nevents
151 &timeout_ts); // const struct timespec *timeout
152 tv_update_date(timeout, status);
153
154 if (status)
155 break;
156 if (timeout || !wait_time)
157 break;
Willy Tarreau2ae84e42019-05-28 16:44:05 +0200158 if (signal_queue_len || wake)
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100159 break;
160 if (tick_isset(exp) && tick_is_expired(exp, now_ms))
161 break;
162 } while (1);
163
164 tv_leaving_poll(wait_time, status);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200165
Willy Tarreau60b639c2018-08-02 10:16:17 +0200166 thread_harmless_end();
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200167 if (sleeping_thread_mask & tid_bit)
168 _HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200169
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200170 for (count = 0; count < status; count++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200171 unsigned int n = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200172 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200173
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100174 if (!fdtab[fd].owner) {
175 activity[tid].poll_dead++;
Willy Tarreau076be252012-07-06 16:02:29 +0200176 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100177 }
178
179 if (!(fdtab[fd].thread_mask & tid_bit)) {
180 activity[tid].poll_skip++;
181 continue;
182 }
Willy Tarreau076be252012-07-06 16:02:29 +0200183
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200184 if (kev[count].filter == EVFILT_READ) {
Willy Tarreaufa061762017-03-13 20:49:56 +0100185 if (kev[count].data)
Christopher Fauletab62f512017-08-30 10:34:36 +0200186 n |= FD_POLL_IN;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100187 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200188 n |= FD_POLL_HUP;
Willy Tarreau4a226272012-11-11 20:49:49 +0100189 }
190 else if (kev[count].filter == EVFILT_WRITE) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200191 n |= FD_POLL_OUT;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100192 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200193 n |= FD_POLL_ERR;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200194 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100195
Christopher Fauletab62f512017-08-30 10:34:36 +0200196 fd_update_events(fd, n);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200197 }
198}
199
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200200
201static int init_kqueue_per_thread()
202{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100203 int fd;
204
Olivier Houchardebaba752018-04-16 13:24:48 +0200205 /* we can have up to two events per fd, so allocate enough to store
206 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
207 * so that we can add an invalid entry and get an error, to avoid
208 * scanning the kqueue uselessly.
209 */
210 kev = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200211 if (kev == NULL)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100212 goto fail_alloc;
213
Christopher Faulet727c89b2018-01-25 16:40:35 +0100214 if (MAX_THREADS > 1 && tid) {
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100215 kqueue_fd[tid] = kqueue();
216 if (kqueue_fd[tid] < 0)
217 goto fail_fd;
218 }
219
220 /* we may have to unregister some events initially registered on the
221 * original fd when it was alone, and/or to register events on the new
222 * fd for this thread. Let's just mark them as updated, the poller will
223 * do the rest.
224 */
Willy Tarreauce036bc2018-01-29 14:58:02 +0100225 for (fd = 0; fd < global.maxsock; fd++)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100226 updt_fd_polling(fd);
227
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200228 return 1;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100229 fail_fd:
230 free(kev);
231 fail_alloc:
232 return 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200233}
234
235static void deinit_kqueue_per_thread()
236{
Christopher Faulet727c89b2018-01-25 16:40:35 +0100237 if (MAX_THREADS > 1 && tid)
Christopher Faulet13b007d2018-01-25 16:32:18 +0100238 close(kqueue_fd[tid]);
239
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200240 free(kev);
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200241 kev = NULL;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200242}
243
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200244/*
245 * Initialization of the kqueue() poller.
246 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
247 * disables the poller by setting its pref to 0.
248 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200249REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200250{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200251 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200252
Olivier Houchardebaba752018-04-16 13:24:48 +0200253 /* we can have up to two events per fd, so allocate enough to store
254 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
255 * so that we can add an invalid entry and get an error, to avoid
256 * scanning the kqueue uselessly.
257 */
258 kev_out = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
259 if (!kev_out)
260 goto fail_alloc;
261
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100262 kqueue_fd[tid] = kqueue();
263 if (kqueue_fd[tid] < 0)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200264 goto fail_fd;
265
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200266 hap_register_per_thread_init(init_kqueue_per_thread);
267 hap_register_per_thread_deinit(deinit_kqueue_per_thread);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200268 return 1;
269
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200270 fail_fd:
Olivier Houchardebaba752018-04-16 13:24:48 +0200271 free(kev_out);
272 kev_out = NULL;
273fail_alloc:
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200274 p->pref = 0;
275 return 0;
276}
277
278/*
279 * Termination of the kqueue() poller.
280 * Memory is released and the poller is marked as unselectable.
281 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200282REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200283{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100284 if (kqueue_fd[tid] >= 0) {
285 close(kqueue_fd[tid]);
286 kqueue_fd[tid] = -1;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200287 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200288
289 p->private = NULL;
290 p->pref = 0;
Olivier Houchardebaba752018-04-16 13:24:48 +0200291 if (kev_out) {
292 free(kev_out);
293 kev_out = NULL;
294 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200295}
296
297/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200298 * Check that the poller works.
299 * Returns 1 if OK, otherwise 0.
300 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200301REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200302{
303 int fd;
304
305 fd = kqueue();
306 if (fd < 0)
307 return 0;
308 close(fd);
309 return 1;
310}
311
312/*
313 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
314 * otherwise 0. Note that some pollers need to be reopened after a fork()
315 * (such as kqueue), and some others may fail to do so in a chroot.
316 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200317REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200318{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100319 kqueue_fd[tid] = kqueue();
320 if (kqueue_fd[tid] < 0)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200321 return 0;
322 return 1;
323}
324
325/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200326 * It is a constructor, which means that it will automatically be called before
327 * main(). This is GCC-specific but it works at least since 2.95.
328 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200329 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200330__attribute__((constructor))
331static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200332{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200333 struct poller *p;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100334 int i;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200335
336 if (nbpollers >= MAX_POLLERS)
337 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200338
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100339 for (i = 0; i < MAX_THREADS; i++)
340 kqueue_fd[i] = -1;
341
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200342 p = &pollers[nbpollers++];
343
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200344 p->name = "kqueue";
345 p->pref = 300;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100346 p->flags = HAP_POLL_F_RDHUP;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200347 p->private = NULL;
348
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100349 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200350 p->test = _do_test;
351 p->init = _do_init;
352 p->term = _do_term;
353 p->poll = _do_poll;
354 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200355}
356
357
358/*
359 * Local variables:
360 * c-indent-level: 8
361 * c-basic-offset: 8
362 * End:
363 */