blob: ebfd5d210b9de7b1f6b6a333289529b63ebe599c [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
Willy Tarreau1e63130a2007-04-09 12:03:06 +020036/*
Willy Tarreau4a226272012-11-11 20:49:49 +010037 * kqueue() poller
Willy Tarreau1e63130a2007-04-09 12:03:06 +020038 */
Willy Tarreau4a226272012-11-11 20:49:49 +010039REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020040{
Willy Tarreau4a226272012-11-11 20:49:49 +010041 int status;
42 int count, fd, delta_ms;
43 struct timespec timeout;
Willy Tarreau7d24fad2018-01-25 14:57:25 +010044 int updt_idx, en;
Willy Tarreau4a226272012-11-11 20:49:49 +010045 int changes = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020046
Olivier Houchardebaba752018-04-16 13:24:48 +020047 timeout.tv_sec = 0;
48 timeout.tv_nsec = 0;
Willy Tarreau4a226272012-11-11 20:49:49 +010049 /* first, scan the update list to find changes */
50 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
51 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +010052
Olivier Houchard8ef1a6b2018-04-03 19:06:18 +020053 HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010054 if (!fdtab[fd].owner) {
55 activity[tid].poll_drop++;
Willy Tarreauf817e9f2014-01-10 16:58:45 +010056 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010057 }
Willy Tarreauf817e9f2014-01-10 16:58:45 +010058
Willy Tarreau7d24fad2018-01-25 14:57:25 +010059 en = fdtab[fd].state;
Willy Tarreauf817e9f2014-01-10 16:58:45 +010060
Willy Tarreau7a2364d2018-01-19 08:56:14 +010061 if (!(fdtab[fd].thread_mask & tid_bit) || !(en & FD_EV_POLLED_RW)) {
62 if (!(fdtab[fd].polled_mask & tid_bit)) {
63 /* fd was not watched, it's still not */
64 continue;
Willy Tarreau4a226272012-11-11 20:49:49 +010065 }
Willy Tarreau7a2364d2018-01-19 08:56:14 +010066 /* fd totally removed from poll list */
67 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
68 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
69 HA_ATOMIC_AND(&fdtab[fd].polled_mask, ~tid_bit);
70 }
71 else {
72 /* OK fd has to be monitored, it was either added or changed */
Willy Tarreau1e63130a2007-04-09 12:03:06 +020073
Willy Tarreau7a2364d2018-01-19 08:56:14 +010074 if (en & FD_EV_POLLED_R)
75 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
76 else if (fdtab[fd].polled_mask & tid_bit)
77 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
78
79 if (en & FD_EV_POLLED_W)
80 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
81 else if (fdtab[fd].polled_mask & tid_bit)
82 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
83
84 HA_ATOMIC_OR(&fdtab[fd].polled_mask, tid_bit);
Willy Tarreau4a226272012-11-11 20:49:49 +010085 }
Willy Tarreau4a226272012-11-11 20:49:49 +010086 }
Olivier Houchardebaba752018-04-16 13:24:48 +020087 if (changes) {
88#ifdef EV_RECEIPT
89 kev[0].flags |= EV_RECEIPT;
90#else
91 /* If EV_RECEIPT isn't defined, just add an invalid entry,
92 * so that we get an error and kevent() stops before scanning
93 * the kqueue.
94 */
95 EV_SET(&kev[changes++], -1, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
96#endif
97 kevent(kqueue_fd[tid], kev, changes, kev_out, changes, &timeout);
98 }
Willy Tarreau4a226272012-11-11 20:49:49 +010099 fd_nbupdt = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200100
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200101 delta_ms = 0;
Willy Tarreau79b8a622007-05-14 03:15:46 +0200102
Willy Tarreau10146c92015-04-13 20:44:19 +0200103 if (!exp) {
104 delta_ms = MAX_DELAY_MS;
105 timeout.tv_sec = (MAX_DELAY_MS / 1000);
106 timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000;
107 }
108 else if (!tick_is_expired(exp, now_ms)) {
109 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
110 if (delta_ms > MAX_DELAY_MS)
111 delta_ms = MAX_DELAY_MS;
112 timeout.tv_sec = (delta_ms / 1000);
113 timeout.tv_nsec = (delta_ms % 1000) * 1000000;
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200114 }
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100115 else
116 activity[tid].poll_exp++;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200117
Willy Tarreauce036bc2018-01-29 14:58:02 +0100118 fd = global.tune.maxpollevents;
Willy Tarreau45a12512011-09-10 16:56:42 +0200119 gettimeofday(&before_poll, NULL);
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100120 status = kevent(kqueue_fd[tid], // int kq
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200121 NULL, // const struct kevent *changelist
122 0, // int nchanges
123 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200124 fd, // int nevents
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200125 &timeout); // const struct timespec *timeout
126 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200127 measure_idle();
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200128
129 for (count = 0; count < status; count++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200130 unsigned int n = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200131 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200132
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100133 if (!fdtab[fd].owner) {
134 activity[tid].poll_dead++;
Willy Tarreau076be252012-07-06 16:02:29 +0200135 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100136 }
137
138 if (!(fdtab[fd].thread_mask & tid_bit)) {
139 activity[tid].poll_skip++;
140 continue;
141 }
Willy Tarreau076be252012-07-06 16:02:29 +0200142
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200143 if (kev[count].filter == EVFILT_READ) {
Willy Tarreaufa061762017-03-13 20:49:56 +0100144 if (kev[count].data)
Christopher Fauletab62f512017-08-30 10:34:36 +0200145 n |= FD_POLL_IN;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100146 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200147 n |= FD_POLL_HUP;
Willy Tarreau4a226272012-11-11 20:49:49 +0100148 }
149 else if (kev[count].filter == EVFILT_WRITE) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200150 n |= FD_POLL_OUT;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100151 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200152 n |= FD_POLL_ERR;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200153 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100154
Christopher Fauletab62f512017-08-30 10:34:36 +0200155 fd_update_events(fd, n);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200156 }
157}
158
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200159
160static int init_kqueue_per_thread()
161{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100162 int fd;
163
Olivier Houchardebaba752018-04-16 13:24:48 +0200164 /* we can have up to two events per fd, so allocate enough to store
165 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
166 * so that we can add an invalid entry and get an error, to avoid
167 * scanning the kqueue uselessly.
168 */
169 kev = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200170 if (kev == NULL)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100171 goto fail_alloc;
172
Christopher Faulet727c89b2018-01-25 16:40:35 +0100173 if (MAX_THREADS > 1 && tid) {
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100174 kqueue_fd[tid] = kqueue();
175 if (kqueue_fd[tid] < 0)
176 goto fail_fd;
177 }
178
179 /* we may have to unregister some events initially registered on the
180 * original fd when it was alone, and/or to register events on the new
181 * fd for this thread. Let's just mark them as updated, the poller will
182 * do the rest.
183 */
Willy Tarreauce036bc2018-01-29 14:58:02 +0100184 for (fd = 0; fd < global.maxsock; fd++)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100185 updt_fd_polling(fd);
186
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200187 return 1;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100188 fail_fd:
189 free(kev);
190 fail_alloc:
191 return 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200192}
193
194static void deinit_kqueue_per_thread()
195{
Christopher Faulet727c89b2018-01-25 16:40:35 +0100196 if (MAX_THREADS > 1 && tid)
Christopher Faulet13b007d2018-01-25 16:32:18 +0100197 close(kqueue_fd[tid]);
198
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200199 free(kev);
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200200 kev = NULL;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200201}
202
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200203/*
204 * Initialization of the kqueue() poller.
205 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
206 * disables the poller by setting its pref to 0.
207 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200208REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200209{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200210 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200211
Olivier Houchardebaba752018-04-16 13:24:48 +0200212 /* we can have up to two events per fd, so allocate enough to store
213 * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined,
214 * so that we can add an invalid entry and get an error, to avoid
215 * scanning the kqueue uselessly.
216 */
217 kev_out = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1));
218 if (!kev_out)
219 goto fail_alloc;
220
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100221 kqueue_fd[tid] = kqueue();
222 if (kqueue_fd[tid] < 0)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200223 goto fail_fd;
224
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200225 hap_register_per_thread_init(init_kqueue_per_thread);
226 hap_register_per_thread_deinit(deinit_kqueue_per_thread);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200227 return 1;
228
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200229 fail_fd:
Olivier Houchardebaba752018-04-16 13:24:48 +0200230 free(kev_out);
231 kev_out = NULL;
232fail_alloc:
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200233 p->pref = 0;
234 return 0;
235}
236
237/*
238 * Termination of the kqueue() poller.
239 * Memory is released and the poller is marked as unselectable.
240 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200241REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200242{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100243 if (kqueue_fd[tid] >= 0) {
244 close(kqueue_fd[tid]);
245 kqueue_fd[tid] = -1;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200246 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200247
248 p->private = NULL;
249 p->pref = 0;
Olivier Houchardebaba752018-04-16 13:24:48 +0200250 if (kev_out) {
251 free(kev_out);
252 kev_out = NULL;
253 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200254}
255
256/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200257 * Check that the poller works.
258 * Returns 1 if OK, otherwise 0.
259 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200260REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200261{
262 int fd;
263
264 fd = kqueue();
265 if (fd < 0)
266 return 0;
267 close(fd);
268 return 1;
269}
270
271/*
272 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
273 * otherwise 0. Note that some pollers need to be reopened after a fork()
274 * (such as kqueue), and some others may fail to do so in a chroot.
275 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200276REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200277{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100278 kqueue_fd[tid] = kqueue();
279 if (kqueue_fd[tid] < 0)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200280 return 0;
281 return 1;
282}
283
284/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200285 * It is a constructor, which means that it will automatically be called before
286 * main(). This is GCC-specific but it works at least since 2.95.
287 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200288 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200289__attribute__((constructor))
290static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200291{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200292 struct poller *p;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100293 int i;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200294
295 if (nbpollers >= MAX_POLLERS)
296 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200297
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100298 for (i = 0; i < MAX_THREADS; i++)
299 kqueue_fd[i] = -1;
300
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200301 p = &pollers[nbpollers++];
302
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200303 p->name = "kqueue";
304 p->pref = 300;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100305 p->flags = HAP_POLL_F_RDHUP;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200306 p->private = NULL;
307
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100308 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200309 p->test = _do_test;
310 p->init = _do_init;
311 p->term = _do_term;
312 p->poll = _do_poll;
313 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200314}
315
316
317/*
318 * Local variables:
319 * c-indent-level: 8
320 * c-basic-offset: 8
321 * End:
322 */