blob: 674551e4d79acb7f3412e356559b6e4b6f4bba29 [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 Tarreau4a226272012-11-11 20:49:49 +010077REGPRM2 static void _do_poll(struct poller *p, int exp)
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 Tarreaubeb859a2018-11-22 18:07:59 +0100135 wait_time = 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;
158 if (signal_queue_len)
159 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();
167
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200168 for (count = 0; count < status; count++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200169 unsigned int n = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200170 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200171
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100172 if (!fdtab[fd].owner) {
173 activity[tid].poll_dead++;
Willy Tarreau076be252012-07-06 16:02:29 +0200174 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100175 }
176
177 if (!(fdtab[fd].thread_mask & tid_bit)) {
178 activity[tid].poll_skip++;
179 continue;
180 }
Willy Tarreau076be252012-07-06 16:02:29 +0200181
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200182 if (kev[count].filter == EVFILT_READ) {
Willy Tarreaufa061762017-03-13 20:49:56 +0100183 if (kev[count].data)
Christopher Fauletab62f512017-08-30 10:34:36 +0200184 n |= FD_POLL_IN;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100185 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200186 n |= FD_POLL_HUP;
Willy Tarreau4a226272012-11-11 20:49:49 +0100187 }
188 else if (kev[count].filter == EVFILT_WRITE) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200189 n |= FD_POLL_OUT;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100190 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200191 n |= FD_POLL_ERR;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200192 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100193
Christopher Fauletab62f512017-08-30 10:34:36 +0200194 fd_update_events(fd, n);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200195 }
196}
197
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200198
199static int init_kqueue_per_thread()
200{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100201 int fd;
202
Olivier Houchardebaba752018-04-16 13:24:48 +0200203 /* we can have up to two events per fd, so allocate enough to store
204 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
205 * so that we can add an invalid entry and get an error, to avoid
206 * scanning the kqueue uselessly.
207 */
208 kev = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200209 if (kev == NULL)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100210 goto fail_alloc;
211
Christopher Faulet727c89b2018-01-25 16:40:35 +0100212 if (MAX_THREADS > 1 && tid) {
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100213 kqueue_fd[tid] = kqueue();
214 if (kqueue_fd[tid] < 0)
215 goto fail_fd;
216 }
217
218 /* we may have to unregister some events initially registered on the
219 * original fd when it was alone, and/or to register events on the new
220 * fd for this thread. Let's just mark them as updated, the poller will
221 * do the rest.
222 */
Willy Tarreauce036bc2018-01-29 14:58:02 +0100223 for (fd = 0; fd < global.maxsock; fd++)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100224 updt_fd_polling(fd);
225
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200226 return 1;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100227 fail_fd:
228 free(kev);
229 fail_alloc:
230 return 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200231}
232
233static void deinit_kqueue_per_thread()
234{
Christopher Faulet727c89b2018-01-25 16:40:35 +0100235 if (MAX_THREADS > 1 && tid)
Christopher Faulet13b007d2018-01-25 16:32:18 +0100236 close(kqueue_fd[tid]);
237
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200238 free(kev);
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200239 kev = NULL;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200240}
241
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200242/*
243 * Initialization of the kqueue() poller.
244 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
245 * disables the poller by setting its pref to 0.
246 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200247REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200248{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200249 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200250
Olivier Houchardebaba752018-04-16 13:24:48 +0200251 /* we can have up to two events per fd, so allocate enough to store
252 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
253 * so that we can add an invalid entry and get an error, to avoid
254 * scanning the kqueue uselessly.
255 */
256 kev_out = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
257 if (!kev_out)
258 goto fail_alloc;
259
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100260 kqueue_fd[tid] = kqueue();
261 if (kqueue_fd[tid] < 0)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200262 goto fail_fd;
263
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200264 hap_register_per_thread_init(init_kqueue_per_thread);
265 hap_register_per_thread_deinit(deinit_kqueue_per_thread);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200266 return 1;
267
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200268 fail_fd:
Olivier Houchardebaba752018-04-16 13:24:48 +0200269 free(kev_out);
270 kev_out = NULL;
271fail_alloc:
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200272 p->pref = 0;
273 return 0;
274}
275
276/*
277 * Termination of the kqueue() poller.
278 * Memory is released and the poller is marked as unselectable.
279 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200280REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200281{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100282 if (kqueue_fd[tid] >= 0) {
283 close(kqueue_fd[tid]);
284 kqueue_fd[tid] = -1;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200285 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200286
287 p->private = NULL;
288 p->pref = 0;
Olivier Houchardebaba752018-04-16 13:24:48 +0200289 if (kev_out) {
290 free(kev_out);
291 kev_out = NULL;
292 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200293}
294
295/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200296 * Check that the poller works.
297 * Returns 1 if OK, otherwise 0.
298 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200299REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200300{
301 int fd;
302
303 fd = kqueue();
304 if (fd < 0)
305 return 0;
306 close(fd);
307 return 1;
308}
309
310/*
311 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
312 * otherwise 0. Note that some pollers need to be reopened after a fork()
313 * (such as kqueue), and some others may fail to do so in a chroot.
314 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200315REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200316{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100317 kqueue_fd[tid] = kqueue();
318 if (kqueue_fd[tid] < 0)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200319 return 0;
320 return 1;
321}
322
323/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200324 * It is a constructor, which means that it will automatically be called before
325 * main(). This is GCC-specific but it works at least since 2.95.
326 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200327 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200328__attribute__((constructor))
329static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200330{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200331 struct poller *p;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100332 int i;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200333
334 if (nbpollers >= MAX_POLLERS)
335 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200336
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100337 for (i = 0; i < MAX_THREADS; i++)
338 kqueue_fd[i] = -1;
339
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200340 p = &pollers[nbpollers++];
341
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200342 p->name = "kqueue";
343 p->pref = 300;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100344 p->flags = HAP_POLL_F_RDHUP;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200345 p->private = NULL;
346
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100347 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200348 p->test = _do_test;
349 p->init = _do_init;
350 p->term = _do_term;
351 p->poll = _do_poll;
352 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200353}
354
355
356/*
357 * Local variables:
358 * c-indent-level: 8
359 * c-basic-offset: 8
360 * End:
361 */