blob: bf7f666dc8147213626a6ff206e7dd48a9885a8f [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 Tarreau0c303ee2008-07-07 00:09:58 +020022#include <common/ticks.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020023#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020024#include <common/tools.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020025
Willy Tarreau1e63130a2007-04-09 12:03:06 +020026#include <types/global.h>
27
28#include <proto/fd.h>
Willy Tarreau10146c92015-04-13 20:44:19 +020029
Willy Tarreau1e63130a2007-04-09 12:03:06 +020030
31/* private data */
Willy Tarreau7a2364d2018-01-19 08:56:14 +010032static int kqueue_fd[MAX_THREADS]; // per-thread kqueue_fd
Christopher Fauletd4604ad2017-05-29 10:40:41 +020033static THREAD_LOCAL struct kevent *kev = NULL;
Olivier Houchardebaba752018-04-16 13:24:48 +020034static struct kevent *kev_out = NULL; // Trash buffer for kevent() to write the eventlist in
Willy Tarreau1e63130a2007-04-09 12:03:06 +020035
PiBa-NLc55b88e2018-05-10 01:01:28 +020036static int _update_fd(int fd, int start)
Olivier Houchard6b96f722018-04-25 16:58:25 +020037{
38 int en;
PiBa-NLc55b88e2018-05-10 01:01:28 +020039 int changes = start;
Olivier Houchard6b96f722018-04-25 16:58:25 +020040
41 en = fdtab[fd].state;
42
43 if (!(fdtab[fd].thread_mask & tid_bit) || !(en & FD_EV_POLLED_RW)) {
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020044 if (!(polled_mask[fd] & tid_bit)) {
Olivier Houchard6b96f722018-04-25 16:58:25 +020045 /* fd was not watched, it's still not */
46 return 0;
47 }
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 Houchardcb92f5c2018-04-26 14:23:07 +020051 HA_ATOMIC_AND(&polled_mask[fd], ~tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +020052 }
53 else {
54 /* OK fd has to be monitored, it was either added or changed */
55
56 if (en & FD_EV_POLLED_R)
57 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020058 else if (polled_mask[fd] & tid_bit)
Olivier Houchard6b96f722018-04-25 16:58:25 +020059 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
60
61 if (en & FD_EV_POLLED_W)
62 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020063 else if (polled_mask[fd] & tid_bit)
Olivier Houchard6b96f722018-04-25 16:58:25 +020064 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
65
Olivier Houchardcb92f5c2018-04-26 14:23:07 +020066 HA_ATOMIC_OR(&polled_mask[fd], tid_bit);
Olivier Houchard6b96f722018-04-25 16:58:25 +020067 }
68 return changes;
69}
70
Willy Tarreau1e63130a2007-04-09 12:03:06 +020071/*
Willy Tarreau4a226272012-11-11 20:49:49 +010072 * kqueue() poller
Willy Tarreau1e63130a2007-04-09 12:03:06 +020073 */
Willy Tarreau4a226272012-11-11 20:49:49 +010074REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020075{
Willy Tarreau4a226272012-11-11 20:49:49 +010076 int status;
77 int count, fd, delta_ms;
78 struct timespec timeout;
Olivier Houchard6b96f722018-04-25 16:58:25 +020079 int updt_idx;
Willy Tarreau4a226272012-11-11 20:49:49 +010080 int changes = 0;
Olivier Houchard6b96f722018-04-25 16:58:25 +020081 int old_fd;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020082
Olivier Houchardebaba752018-04-16 13:24:48 +020083 timeout.tv_sec = 0;
84 timeout.tv_nsec = 0;
Willy Tarreau4a226272012-11-11 20:49:49 +010085 /* first, scan the update list to find changes */
86 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
87 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +010088
Olivier Houchard8ef1a6b2018-04-03 19:06:18 +020089 HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010090 if (!fdtab[fd].owner) {
91 activity[tid].poll_drop++;
Willy Tarreauf817e9f2014-01-10 16:58:45 +010092 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010093 }
PiBa-NLc55b88e2018-05-10 01:01:28 +020094 changes = _update_fd(fd, changes);
Olivier Houchard6b96f722018-04-25 16:58:25 +020095 }
96 /* Scan the global update list */
97 for (old_fd = fd = update_list.first; fd != -1; fd = fdtab[fd].update.next) {
98 if (fd == -2) {
99 fd = old_fd;
100 continue;
Willy Tarreau4a226272012-11-11 20:49:49 +0100101 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200102 else if (fd <= -3)
103 fd = -fd -4;
104 if (fd == -1)
105 break;
106 if (fdtab[fd].update_mask & tid_bit)
107 done_update_polling(fd);
108 else
109 continue;
110 if (!fdtab[fd].owner)
111 continue;
PiBa-NLc55b88e2018-05-10 01:01:28 +0200112 changes = _update_fd(fd, changes);
Willy Tarreau4a226272012-11-11 20:49:49 +0100113 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200114
Olivier Houchardebaba752018-04-16 13:24:48 +0200115 if (changes) {
116#ifdef EV_RECEIPT
117 kev[0].flags |= EV_RECEIPT;
118#else
119 /* If EV_RECEIPT isn't defined, just add an invalid entry,
120 * so that we get an error and kevent() stops before scanning
121 * the kqueue.
122 */
123 EV_SET(&kev[changes++], -1, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
124#endif
125 kevent(kqueue_fd[tid], kev, changes, kev_out, changes, &timeout);
126 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100127 fd_nbupdt = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200128
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200129 delta_ms = 0;
Willy Tarreau79b8a622007-05-14 03:15:46 +0200130
Willy Tarreau10146c92015-04-13 20:44:19 +0200131 if (!exp) {
132 delta_ms = MAX_DELAY_MS;
133 timeout.tv_sec = (MAX_DELAY_MS / 1000);
134 timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000;
135 }
136 else if (!tick_is_expired(exp, now_ms)) {
137 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
138 if (delta_ms > MAX_DELAY_MS)
139 delta_ms = MAX_DELAY_MS;
140 timeout.tv_sec = (delta_ms / 1000);
141 timeout.tv_nsec = (delta_ms % 1000) * 1000000;
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200142 }
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100143 else
144 activity[tid].poll_exp++;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200145
Willy Tarreauce036bc2018-01-29 14:58:02 +0100146 fd = global.tune.maxpollevents;
Willy Tarreau45a12512011-09-10 16:56:42 +0200147 gettimeofday(&before_poll, NULL);
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100148 status = kevent(kqueue_fd[tid], // int kq
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200149 NULL, // const struct kevent *changelist
150 0, // int nchanges
151 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200152 fd, // int nevents
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200153 &timeout); // const struct timespec *timeout
154 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200155 measure_idle();
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200156
157 for (count = 0; count < status; count++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200158 unsigned int n = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200159 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200160
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100161 if (!fdtab[fd].owner) {
162 activity[tid].poll_dead++;
Willy Tarreau076be252012-07-06 16:02:29 +0200163 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100164 }
165
166 if (!(fdtab[fd].thread_mask & tid_bit)) {
167 activity[tid].poll_skip++;
168 continue;
169 }
Willy Tarreau076be252012-07-06 16:02:29 +0200170
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200171 if (kev[count].filter == EVFILT_READ) {
Willy Tarreaufa061762017-03-13 20:49:56 +0100172 if (kev[count].data)
Christopher Fauletab62f512017-08-30 10:34:36 +0200173 n |= FD_POLL_IN;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100174 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200175 n |= FD_POLL_HUP;
Willy Tarreau4a226272012-11-11 20:49:49 +0100176 }
177 else if (kev[count].filter == EVFILT_WRITE) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200178 n |= FD_POLL_OUT;
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_ERR;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200181 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100182
Christopher Fauletab62f512017-08-30 10:34:36 +0200183 fd_update_events(fd, n);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200184 }
185}
186
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200187
188static int init_kqueue_per_thread()
189{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100190 int fd;
191
Olivier Houchardebaba752018-04-16 13:24:48 +0200192 /* we can have up to two events per fd, so allocate enough to store
193 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
194 * so that we can add an invalid entry and get an error, to avoid
195 * scanning the kqueue uselessly.
196 */
197 kev = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200198 if (kev == NULL)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100199 goto fail_alloc;
200
Christopher Faulet727c89b2018-01-25 16:40:35 +0100201 if (MAX_THREADS > 1 && tid) {
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100202 kqueue_fd[tid] = kqueue();
203 if (kqueue_fd[tid] < 0)
204 goto fail_fd;
205 }
206
207 /* we may have to unregister some events initially registered on the
208 * original fd when it was alone, and/or to register events on the new
209 * fd for this thread. Let's just mark them as updated, the poller will
210 * do the rest.
211 */
Willy Tarreauce036bc2018-01-29 14:58:02 +0100212 for (fd = 0; fd < global.maxsock; fd++)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100213 updt_fd_polling(fd);
214
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200215 return 1;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100216 fail_fd:
217 free(kev);
218 fail_alloc:
219 return 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200220}
221
222static void deinit_kqueue_per_thread()
223{
Christopher Faulet727c89b2018-01-25 16:40:35 +0100224 if (MAX_THREADS > 1 && tid)
Christopher Faulet13b007d2018-01-25 16:32:18 +0100225 close(kqueue_fd[tid]);
226
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200227 free(kev);
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200228 kev = NULL;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200229}
230
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200231/*
232 * Initialization of the kqueue() poller.
233 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
234 * disables the poller by setting its pref to 0.
235 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200236REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200237{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200238 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200239
Olivier Houchardebaba752018-04-16 13:24:48 +0200240 /* we can have up to two events per fd, so allocate enough to store
241 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
242 * so that we can add an invalid entry and get an error, to avoid
243 * scanning the kqueue uselessly.
244 */
245 kev_out = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
246 if (!kev_out)
247 goto fail_alloc;
248
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100249 kqueue_fd[tid] = kqueue();
250 if (kqueue_fd[tid] < 0)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200251 goto fail_fd;
252
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200253 hap_register_per_thread_init(init_kqueue_per_thread);
254 hap_register_per_thread_deinit(deinit_kqueue_per_thread);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200255 return 1;
256
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200257 fail_fd:
Olivier Houchardebaba752018-04-16 13:24:48 +0200258 free(kev_out);
259 kev_out = NULL;
260fail_alloc:
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200261 p->pref = 0;
262 return 0;
263}
264
265/*
266 * Termination of the kqueue() poller.
267 * Memory is released and the poller is marked as unselectable.
268 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200269REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200270{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100271 if (kqueue_fd[tid] >= 0) {
272 close(kqueue_fd[tid]);
273 kqueue_fd[tid] = -1;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200274 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200275
276 p->private = NULL;
277 p->pref = 0;
Olivier Houchardebaba752018-04-16 13:24:48 +0200278 if (kev_out) {
279 free(kev_out);
280 kev_out = NULL;
281 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200282}
283
284/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200285 * Check that the poller works.
286 * Returns 1 if OK, otherwise 0.
287 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200288REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200289{
290 int fd;
291
292 fd = kqueue();
293 if (fd < 0)
294 return 0;
295 close(fd);
296 return 1;
297}
298
299/*
300 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
301 * otherwise 0. Note that some pollers need to be reopened after a fork()
302 * (such as kqueue), and some others may fail to do so in a chroot.
303 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200304REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200305{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100306 kqueue_fd[tid] = kqueue();
307 if (kqueue_fd[tid] < 0)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200308 return 0;
309 return 1;
310}
311
312/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200313 * It is a constructor, which means that it will automatically be called before
314 * main(). This is GCC-specific but it works at least since 2.95.
315 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200316 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200317__attribute__((constructor))
318static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200319{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200320 struct poller *p;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100321 int i;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200322
323 if (nbpollers >= MAX_POLLERS)
324 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200325
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100326 for (i = 0; i < MAX_THREADS; i++)
327 kqueue_fd[i] = -1;
328
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200329 p = &pollers[nbpollers++];
330
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200331 p->name = "kqueue";
332 p->pref = 300;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100333 p->flags = HAP_POLL_F_RDHUP;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200334 p->private = NULL;
335
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100336 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200337 p->test = _do_test;
338 p->init = _do_init;
339 p->term = _do_term;
340 p->poll = _do_poll;
341 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200342}
343
344
345/*
346 * Local variables:
347 * c-indent-level: 8
348 * c-basic-offset: 8
349 * End:
350 */