blob: dc1310cff8234153af4e386ac0b1b93f81e0293b [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 Tarreau10146c92015-04-13 20:44:19 +020031
Willy Tarreau1e63130a2007-04-09 12:03:06 +020032
33/* private data */
Willy Tarreau7a2364d2018-01-19 08:56:14 +010034static int kqueue_fd[MAX_THREADS]; // per-thread kqueue_fd
Christopher Fauletd4604ad2017-05-29 10:40:41 +020035static THREAD_LOCAL struct kevent *kev = NULL;
Olivier Houchardebaba752018-04-16 13:24:48 +020036static struct kevent *kev_out = NULL; // Trash buffer for kevent() to write the eventlist in
Willy Tarreau1e63130a2007-04-09 12:03:06 +020037
PiBa-NLc55b88e2018-05-10 01:01:28 +020038static int _update_fd(int fd, int start)
Olivier Houchard6b96f722018-04-25 16:58:25 +020039{
40 int en;
PiBa-NLc55b88e2018-05-10 01:01:28 +020041 int changes = start;
Olivier Houchard6b96f722018-04-25 16:58:25 +020042
43 en = fdtab[fd].state;
44
45 if (!(fdtab[fd].thread_mask & tid_bit) || !(en & FD_EV_POLLED_RW)) {
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020046 if (!(polled_mask[fd] & 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 Houchardcb92f5c2018-04-26 14:23:07 +020053 HA_ATOMIC_AND(&polled_mask[fd], ~tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +020054 }
55 else {
56 /* OK fd has to be monitored, it was either added or changed */
57
58 if (en & FD_EV_POLLED_R)
59 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020060 else if (polled_mask[fd] & tid_bit)
Olivier Houchard6b96f722018-04-25 16:58:25 +020061 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
62
63 if (en & FD_EV_POLLED_W)
64 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020065 else if (polled_mask[fd] & tid_bit)
Olivier Houchard6b96f722018-04-25 16:58:25 +020066 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
67
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020068 HA_ATOMIC_OR(&polled_mask[fd], tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +020069 }
70 return changes;
71}
72
Willy Tarreau1e63130a2007-04-09 12:03:06 +020073/*
Willy Tarreau4a226272012-11-11 20:49:49 +010074 * kqueue() poller
Willy Tarreau1e63130a2007-04-09 12:03:06 +020075 */
Willy Tarreau4a226272012-11-11 20:49:49 +010076REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020077{
Willy Tarreau4a226272012-11-11 20:49:49 +010078 int status;
79 int count, fd, delta_ms;
80 struct timespec timeout;
Olivier Houchard6b96f722018-04-25 16:58:25 +020081 int updt_idx;
Willy Tarreau4a226272012-11-11 20:49:49 +010082 int changes = 0;
Olivier Houchard6b96f722018-04-25 16:58:25 +020083 int old_fd;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020084
Olivier Houchardebaba752018-04-16 13:24:48 +020085 timeout.tv_sec = 0;
86 timeout.tv_nsec = 0;
Willy Tarreau4a226272012-11-11 20:49:49 +010087 /* first, scan the update list to find changes */
88 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
89 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +010090
Olivier Houchard8ef1a6b2018-04-03 19:06:18 +020091 HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010092 if (!fdtab[fd].owner) {
93 activity[tid].poll_drop++;
Willy Tarreauf817e9f2014-01-10 16:58:45 +010094 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010095 }
PiBa-NLc55b88e2018-05-10 01:01:28 +020096 changes = _update_fd(fd, changes);
Olivier Houchard6b96f722018-04-25 16:58:25 +020097 }
98 /* Scan the global update list */
99 for (old_fd = fd = update_list.first; fd != -1; fd = fdtab[fd].update.next) {
100 if (fd == -2) {
101 fd = old_fd;
102 continue;
Willy Tarreau4a226272012-11-11 20:49:49 +0100103 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200104 else if (fd <= -3)
105 fd = -fd -4;
106 if (fd == -1)
107 break;
108 if (fdtab[fd].update_mask & tid_bit)
109 done_update_polling(fd);
110 else
111 continue;
112 if (!fdtab[fd].owner)
113 continue;
PiBa-NLc55b88e2018-05-10 01:01:28 +0200114 changes = _update_fd(fd, changes);
Willy Tarreau4a226272012-11-11 20:49:49 +0100115 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200116
Willy Tarreau60b639c2018-08-02 10:16:17 +0200117 thread_harmless_now();
118
Olivier Houchardebaba752018-04-16 13:24:48 +0200119 if (changes) {
120#ifdef EV_RECEIPT
121 kev[0].flags |= EV_RECEIPT;
122#else
123 /* If EV_RECEIPT isn't defined, just add an invalid entry,
124 * so that we get an error and kevent() stops before scanning
125 * the kqueue.
126 */
127 EV_SET(&kev[changes++], -1, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
128#endif
129 kevent(kqueue_fd[tid], kev, changes, kev_out, changes, &timeout);
130 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100131 fd_nbupdt = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200132
Willy Tarreauf37ba942018-10-17 11:25:54 +0200133 /* now let's wait for events */
134 delta_ms = compute_poll_timeout(exp);
135 timeout.tv_sec = (delta_ms / 1000);
136 timeout.tv_nsec = (delta_ms % 1000) * 1000000;
Willy Tarreauce036bc2018-01-29 14:58:02 +0100137 fd = global.tune.maxpollevents;
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200138 tv_entering_poll();
Willy Tarreau609aad92018-11-22 08:31:09 +0100139 activity_count_runtime();
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100140 status = kevent(kqueue_fd[tid], // int kq
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200141 NULL, // const struct kevent *changelist
142 0, // int nchanges
143 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200144 fd, // int nevents
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200145 &timeout); // const struct timespec *timeout
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200146 tv_leaving_poll(delta_ms, status);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200147
Willy Tarreau60b639c2018-08-02 10:16:17 +0200148 thread_harmless_end();
149
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200150 for (count = 0; count < status; count++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200151 unsigned int n = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200152 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200153
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100154 if (!fdtab[fd].owner) {
155 activity[tid].poll_dead++;
Willy Tarreau076be252012-07-06 16:02:29 +0200156 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100157 }
158
159 if (!(fdtab[fd].thread_mask & tid_bit)) {
160 activity[tid].poll_skip++;
161 continue;
162 }
Willy Tarreau076be252012-07-06 16:02:29 +0200163
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200164 if (kev[count].filter == EVFILT_READ) {
Willy Tarreaufa061762017-03-13 20:49:56 +0100165 if (kev[count].data)
Christopher Fauletab62f512017-08-30 10:34:36 +0200166 n |= FD_POLL_IN;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100167 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200168 n |= FD_POLL_HUP;
Willy Tarreau4a226272012-11-11 20:49:49 +0100169 }
170 else if (kev[count].filter == EVFILT_WRITE) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200171 n |= FD_POLL_OUT;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100172 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200173 n |= FD_POLL_ERR;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200174 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100175
Christopher Fauletab62f512017-08-30 10:34:36 +0200176 fd_update_events(fd, n);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200177 }
178}
179
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200180
181static int init_kqueue_per_thread()
182{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100183 int fd;
184
Olivier Houchardebaba752018-04-16 13:24:48 +0200185 /* we can have up to two events per fd, so allocate enough to store
186 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
187 * so that we can add an invalid entry and get an error, to avoid
188 * scanning the kqueue uselessly.
189 */
190 kev = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200191 if (kev == NULL)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100192 goto fail_alloc;
193
Christopher Faulet727c89b2018-01-25 16:40:35 +0100194 if (MAX_THREADS > 1 && tid) {
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100195 kqueue_fd[tid] = kqueue();
196 if (kqueue_fd[tid] < 0)
197 goto fail_fd;
198 }
199
200 /* we may have to unregister some events initially registered on the
201 * original fd when it was alone, and/or to register events on the new
202 * fd for this thread. Let's just mark them as updated, the poller will
203 * do the rest.
204 */
Willy Tarreauce036bc2018-01-29 14:58:02 +0100205 for (fd = 0; fd < global.maxsock; fd++)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100206 updt_fd_polling(fd);
207
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200208 return 1;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100209 fail_fd:
210 free(kev);
211 fail_alloc:
212 return 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200213}
214
215static void deinit_kqueue_per_thread()
216{
Christopher Faulet727c89b2018-01-25 16:40:35 +0100217 if (MAX_THREADS > 1 && tid)
Christopher Faulet13b007d2018-01-25 16:32:18 +0100218 close(kqueue_fd[tid]);
219
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200220 free(kev);
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200221 kev = NULL;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200222}
223
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200224/*
225 * Initialization of the kqueue() poller.
226 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
227 * disables the poller by setting its pref to 0.
228 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200229REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200230{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200231 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200232
Olivier Houchardebaba752018-04-16 13:24:48 +0200233 /* we can have up to two events per fd, so allocate enough to store
234 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
235 * so that we can add an invalid entry and get an error, to avoid
236 * scanning the kqueue uselessly.
237 */
238 kev_out = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
239 if (!kev_out)
240 goto fail_alloc;
241
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100242 kqueue_fd[tid] = kqueue();
243 if (kqueue_fd[tid] < 0)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200244 goto fail_fd;
245
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200246 hap_register_per_thread_init(init_kqueue_per_thread);
247 hap_register_per_thread_deinit(deinit_kqueue_per_thread);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200248 return 1;
249
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200250 fail_fd:
Olivier Houchardebaba752018-04-16 13:24:48 +0200251 free(kev_out);
252 kev_out = NULL;
253fail_alloc:
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200254 p->pref = 0;
255 return 0;
256}
257
258/*
259 * Termination of the kqueue() poller.
260 * Memory is released and the poller is marked as unselectable.
261 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200262REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200263{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100264 if (kqueue_fd[tid] >= 0) {
265 close(kqueue_fd[tid]);
266 kqueue_fd[tid] = -1;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200267 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200268
269 p->private = NULL;
270 p->pref = 0;
Olivier Houchardebaba752018-04-16 13:24:48 +0200271 if (kev_out) {
272 free(kev_out);
273 kev_out = NULL;
274 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200275}
276
277/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200278 * Check that the poller works.
279 * Returns 1 if OK, otherwise 0.
280 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200281REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200282{
283 int fd;
284
285 fd = kqueue();
286 if (fd < 0)
287 return 0;
288 close(fd);
289 return 1;
290}
291
292/*
293 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
294 * otherwise 0. Note that some pollers need to be reopened after a fork()
295 * (such as kqueue), and some others may fail to do so in a chroot.
296 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200297REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200298{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100299 kqueue_fd[tid] = kqueue();
300 if (kqueue_fd[tid] < 0)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200301 return 0;
302 return 1;
303}
304
305/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200306 * It is a constructor, which means that it will automatically be called before
307 * main(). This is GCC-specific but it works at least since 2.95.
308 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200309 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200310__attribute__((constructor))
311static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200312{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200313 struct poller *p;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100314 int i;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200315
316 if (nbpollers >= MAX_POLLERS)
317 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200318
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100319 for (i = 0; i < MAX_THREADS; i++)
320 kqueue_fd[i] = -1;
321
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200322 p = &pollers[nbpollers++];
323
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200324 p->name = "kqueue";
325 p->pref = 300;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100326 p->flags = HAP_POLL_F_RDHUP;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200327 p->private = NULL;
328
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100329 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200330 p->test = _do_test;
331 p->init = _do_init;
332 p->term = _do_term;
333 p->poll = _do_poll;
334 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200335}
336
337
338/*
339 * Local variables:
340 * c-indent-level: 8
341 * c-basic-offset: 8
342 * End:
343 */