blob: e2f04f7025ad3a5ea44baae4a4188359f9c22bbb [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
29#include <proto/fd.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
44 if (!(fdtab[fd].thread_mask & tid_bit) || !(en & FD_EV_POLLED_RW)) {
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020045 if (!(polled_mask[fd] & tid_bit)) {
Olivier Houchard6b96f722018-04-25 16:58:25 +020046 /* fd was not watched, it's still not */
Olivier Houchard5ab33942018-09-11 14:44:51 +020047 return changes;
Olivier Houchard6b96f722018-04-25 16:58:25 +020048 }
49 /* fd totally removed from poll list */
50 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
51 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020052 HA_ATOMIC_AND(&polled_mask[fd], ~tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +020053 }
54 else {
55 /* OK fd has to be monitored, it was either added or changed */
56
57 if (en & FD_EV_POLLED_R)
58 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020059 else if (polled_mask[fd] & tid_bit)
Olivier Houchard6b96f722018-04-25 16:58:25 +020060 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
61
62 if (en & FD_EV_POLLED_W)
63 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020064 else if (polled_mask[fd] & tid_bit)
Olivier Houchard6b96f722018-04-25 16:58:25 +020065 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
66
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020067 HA_ATOMIC_OR(&polled_mask[fd], tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +020068 }
69 return changes;
70}
71
Willy Tarreau1e63130a2007-04-09 12:03:06 +020072/*
Willy Tarreau4a226272012-11-11 20:49:49 +010073 * kqueue() poller
Willy Tarreau1e63130a2007-04-09 12:03:06 +020074 */
Willy Tarreau4a226272012-11-11 20:49:49 +010075REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020076{
Willy Tarreau4a226272012-11-11 20:49:49 +010077 int status;
78 int count, fd, delta_ms;
79 struct timespec timeout;
Olivier Houchard6b96f722018-04-25 16:58:25 +020080 int updt_idx;
Willy Tarreau4a226272012-11-11 20:49:49 +010081 int changes = 0;
Olivier Houchard6b96f722018-04-25 16:58:25 +020082 int old_fd;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020083
Olivier Houchardebaba752018-04-16 13:24:48 +020084 timeout.tv_sec = 0;
85 timeout.tv_nsec = 0;
Willy Tarreau4a226272012-11-11 20:49:49 +010086 /* first, scan the update list to find changes */
87 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
88 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +010089
Olivier Houchard8ef1a6b2018-04-03 19:06:18 +020090 HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010091 if (!fdtab[fd].owner) {
92 activity[tid].poll_drop++;
Willy Tarreauf817e9f2014-01-10 16:58:45 +010093 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010094 }
PiBa-NLc55b88e2018-05-10 01:01:28 +020095 changes = _update_fd(fd, changes);
Olivier Houchard6b96f722018-04-25 16:58:25 +020096 }
97 /* Scan the global update list */
98 for (old_fd = fd = update_list.first; fd != -1; fd = fdtab[fd].update.next) {
99 if (fd == -2) {
100 fd = old_fd;
101 continue;
Willy Tarreau4a226272012-11-11 20:49:49 +0100102 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200103 else if (fd <= -3)
104 fd = -fd -4;
105 if (fd == -1)
106 break;
107 if (fdtab[fd].update_mask & tid_bit)
108 done_update_polling(fd);
109 else
110 continue;
111 if (!fdtab[fd].owner)
112 continue;
PiBa-NLc55b88e2018-05-10 01:01:28 +0200113 changes = _update_fd(fd, changes);
Willy Tarreau4a226272012-11-11 20:49:49 +0100114 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200115
Willy Tarreau60b639c2018-08-02 10:16:17 +0200116 thread_harmless_now();
117
Olivier Houchardebaba752018-04-16 13:24:48 +0200118 if (changes) {
119#ifdef EV_RECEIPT
120 kev[0].flags |= EV_RECEIPT;
121#else
122 /* If EV_RECEIPT isn't defined, just add an invalid entry,
123 * so that we get an error and kevent() stops before scanning
124 * the kqueue.
125 */
126 EV_SET(&kev[changes++], -1, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
127#endif
128 kevent(kqueue_fd[tid], kev, changes, kev_out, changes, &timeout);
129 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100130 fd_nbupdt = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200131
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200132 delta_ms = 0;
Willy Tarreau79b8a622007-05-14 03:15:46 +0200133
Willy Tarreau10146c92015-04-13 20:44:19 +0200134 if (!exp) {
135 delta_ms = MAX_DELAY_MS;
136 timeout.tv_sec = (MAX_DELAY_MS / 1000);
137 timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000;
138 }
139 else if (!tick_is_expired(exp, now_ms)) {
140 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
141 if (delta_ms > MAX_DELAY_MS)
142 delta_ms = MAX_DELAY_MS;
143 timeout.tv_sec = (delta_ms / 1000);
144 timeout.tv_nsec = (delta_ms % 1000) * 1000000;
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200145 }
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100146 else
147 activity[tid].poll_exp++;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200148
Willy Tarreauce036bc2018-01-29 14:58:02 +0100149 fd = global.tune.maxpollevents;
Willy Tarreau45a12512011-09-10 16:56:42 +0200150 gettimeofday(&before_poll, NULL);
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100151 status = kevent(kqueue_fd[tid], // int kq
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200152 NULL, // const struct kevent *changelist
153 0, // int nchanges
154 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200155 fd, // int nevents
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200156 &timeout); // const struct timespec *timeout
157 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200158 measure_idle();
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200159
Willy Tarreau60b639c2018-08-02 10:16:17 +0200160 thread_harmless_end();
161
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200162 for (count = 0; count < status; count++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200163 unsigned int n = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200164 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200165
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100166 if (!fdtab[fd].owner) {
167 activity[tid].poll_dead++;
Willy Tarreau076be252012-07-06 16:02:29 +0200168 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100169 }
170
171 if (!(fdtab[fd].thread_mask & tid_bit)) {
172 activity[tid].poll_skip++;
173 continue;
174 }
Willy Tarreau076be252012-07-06 16:02:29 +0200175
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200176 if (kev[count].filter == EVFILT_READ) {
Willy Tarreaufa061762017-03-13 20:49:56 +0100177 if (kev[count].data)
Christopher Fauletab62f512017-08-30 10:34:36 +0200178 n |= FD_POLL_IN;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100179 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200180 n |= FD_POLL_HUP;
Willy Tarreau4a226272012-11-11 20:49:49 +0100181 }
182 else if (kev[count].filter == EVFILT_WRITE) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200183 n |= FD_POLL_OUT;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100184 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200185 n |= FD_POLL_ERR;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200186 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100187
Christopher Fauletab62f512017-08-30 10:34:36 +0200188 fd_update_events(fd, n);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200189 }
190}
191
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200192
193static int init_kqueue_per_thread()
194{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100195 int fd;
196
Olivier Houchardebaba752018-04-16 13:24:48 +0200197 /* we can have up to two events per fd, so allocate enough to store
198 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
199 * so that we can add an invalid entry and get an error, to avoid
200 * scanning the kqueue uselessly.
201 */
202 kev = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200203 if (kev == NULL)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100204 goto fail_alloc;
205
Christopher Faulet727c89b2018-01-25 16:40:35 +0100206 if (MAX_THREADS > 1 && tid) {
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100207 kqueue_fd[tid] = kqueue();
208 if (kqueue_fd[tid] < 0)
209 goto fail_fd;
210 }
211
212 /* we may have to unregister some events initially registered on the
213 * original fd when it was alone, and/or to register events on the new
214 * fd for this thread. Let's just mark them as updated, the poller will
215 * do the rest.
216 */
Willy Tarreauce036bc2018-01-29 14:58:02 +0100217 for (fd = 0; fd < global.maxsock; fd++)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100218 updt_fd_polling(fd);
219
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200220 return 1;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100221 fail_fd:
222 free(kev);
223 fail_alloc:
224 return 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200225}
226
227static void deinit_kqueue_per_thread()
228{
Christopher Faulet727c89b2018-01-25 16:40:35 +0100229 if (MAX_THREADS > 1 && tid)
Christopher Faulet13b007d2018-01-25 16:32:18 +0100230 close(kqueue_fd[tid]);
231
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200232 free(kev);
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200233 kev = NULL;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200234}
235
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200236/*
237 * Initialization of the kqueue() poller.
238 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
239 * disables the poller by setting its pref to 0.
240 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200241REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200242{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200243 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200244
Olivier Houchardebaba752018-04-16 13:24:48 +0200245 /* we can have up to two events per fd, so allocate enough to store
246 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
247 * so that we can add an invalid entry and get an error, to avoid
248 * scanning the kqueue uselessly.
249 */
250 kev_out = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
251 if (!kev_out)
252 goto fail_alloc;
253
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100254 kqueue_fd[tid] = kqueue();
255 if (kqueue_fd[tid] < 0)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200256 goto fail_fd;
257
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200258 hap_register_per_thread_init(init_kqueue_per_thread);
259 hap_register_per_thread_deinit(deinit_kqueue_per_thread);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200260 return 1;
261
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200262 fail_fd:
Olivier Houchardebaba752018-04-16 13:24:48 +0200263 free(kev_out);
264 kev_out = NULL;
265fail_alloc:
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200266 p->pref = 0;
267 return 0;
268}
269
270/*
271 * Termination of the kqueue() poller.
272 * Memory is released and the poller is marked as unselectable.
273 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200274REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200275{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100276 if (kqueue_fd[tid] >= 0) {
277 close(kqueue_fd[tid]);
278 kqueue_fd[tid] = -1;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200279 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200280
281 p->private = NULL;
282 p->pref = 0;
Olivier Houchardebaba752018-04-16 13:24:48 +0200283 if (kev_out) {
284 free(kev_out);
285 kev_out = NULL;
286 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200287}
288
289/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200290 * Check that the poller works.
291 * Returns 1 if OK, otherwise 0.
292 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200293REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200294{
295 int fd;
296
297 fd = kqueue();
298 if (fd < 0)
299 return 0;
300 close(fd);
301 return 1;
302}
303
304/*
305 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
306 * otherwise 0. Note that some pollers need to be reopened after a fork()
307 * (such as kqueue), and some others may fail to do so in a chroot.
308 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200309REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200310{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100311 kqueue_fd[tid] = kqueue();
312 if (kqueue_fd[tid] < 0)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200313 return 0;
314 return 1;
315}
316
317/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200318 * It is a constructor, which means that it will automatically be called before
319 * main(). This is GCC-specific but it works at least since 2.95.
320 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200321 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200322__attribute__((constructor))
323static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200324{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200325 struct poller *p;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100326 int i;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200327
328 if (nbpollers >= MAX_POLLERS)
329 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200330
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100331 for (i = 0; i < MAX_THREADS; i++)
332 kqueue_fd[i] = -1;
333
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200334 p = &pollers[nbpollers++];
335
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200336 p->name = "kqueue";
337 p->pref = 300;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100338 p->flags = HAP_POLL_F_RDHUP;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200339 p->private = NULL;
340
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100341 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200342 p->test = _do_test;
343 p->init = _do_init;
344 p->term = _do_term;
345 p->poll = _do_poll;
346 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200347}
348
349
350/*
351 * Local variables:
352 * c-indent-level: 8
353 * c-basic-offset: 8
354 * End:
355 */