blob: 6169e63ceaed2aa1ccaf89d86df5ccf6a2668b64 [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;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020034
Willy Tarreau1e63130a2007-04-09 12:03:06 +020035/*
Willy Tarreau4a226272012-11-11 20:49:49 +010036 * kqueue() poller
Willy Tarreau1e63130a2007-04-09 12:03:06 +020037 */
Willy Tarreau4a226272012-11-11 20:49:49 +010038REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020039{
Willy Tarreau4a226272012-11-11 20:49:49 +010040 int status;
41 int count, fd, delta_ms;
42 struct timespec timeout;
43 int updt_idx, en, eo;
44 int changes = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020045
Willy Tarreau4a226272012-11-11 20:49:49 +010046 /* first, scan the update list to find changes */
47 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
48 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +010049
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010050 if (!fdtab[fd].owner) {
51 activity[tid].poll_drop++;
Willy Tarreauf817e9f2014-01-10 16:58:45 +010052 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010053 }
Willy Tarreauf817e9f2014-01-10 16:58:45 +010054
Christopher Faulet2a944ee2017-11-07 10:42:54 +010055 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreauebc78d72018-01-20 23:53:50 +010056 fdtab[fd].update_mask &= ~tid_bit;
Willy Tarreau25002d22014-01-25 10:32:56 +010057 eo = fdtab[fd].state;
58 en = fd_compute_new_polled_status(eo);
Christopher Fauletd4604ad2017-05-29 10:40:41 +020059 fdtab[fd].state = en;
Christopher Faulet2a944ee2017-11-07 10:42:54 +010060 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreauf817e9f2014-01-10 16:58:45 +010061
Willy Tarreau7a2364d2018-01-19 08:56:14 +010062 if (!(fdtab[fd].thread_mask & tid_bit) || !(en & FD_EV_POLLED_RW)) {
63 if (!(fdtab[fd].polled_mask & tid_bit)) {
64 /* fd was not watched, it's still not */
65 continue;
Willy Tarreau4a226272012-11-11 20:49:49 +010066 }
Willy Tarreau7a2364d2018-01-19 08:56:14 +010067 /* fd totally removed from poll list */
68 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
69 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
70 HA_ATOMIC_AND(&fdtab[fd].polled_mask, ~tid_bit);
71 }
72 else {
73 /* OK fd has to be monitored, it was either added or changed */
Willy Tarreau1e63130a2007-04-09 12:03:06 +020074
Willy Tarreau7a2364d2018-01-19 08:56:14 +010075 if (en & FD_EV_POLLED_R)
76 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
77 else if (fdtab[fd].polled_mask & tid_bit)
78 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
79
80 if (en & FD_EV_POLLED_W)
81 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
82 else if (fdtab[fd].polled_mask & tid_bit)
83 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
84
85 HA_ATOMIC_OR(&fdtab[fd].polled_mask, tid_bit);
Willy Tarreau4a226272012-11-11 20:49:49 +010086 }
Willy Tarreau4a226272012-11-11 20:49:49 +010087 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +020088 if (changes)
Willy Tarreau7a2364d2018-01-19 08:56:14 +010089 kevent(kqueue_fd[tid], kev, changes, NULL, 0, NULL);
Willy Tarreau4a226272012-11-11 20:49:49 +010090 fd_nbupdt = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020091
Willy Tarreau0c303ee2008-07-07 00:09:58 +020092 delta_ms = 0;
93 timeout.tv_sec = 0;
94 timeout.tv_nsec = 0;
Willy Tarreau79b8a622007-05-14 03:15:46 +020095
Willy Tarreau10146c92015-04-13 20:44:19 +020096 if (!exp) {
97 delta_ms = MAX_DELAY_MS;
98 timeout.tv_sec = (MAX_DELAY_MS / 1000);
99 timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000;
100 }
101 else if (!tick_is_expired(exp, now_ms)) {
102 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
103 if (delta_ms > MAX_DELAY_MS)
104 delta_ms = MAX_DELAY_MS;
105 timeout.tv_sec = (delta_ms / 1000);
106 timeout.tv_nsec = (delta_ms % 1000) * 1000000;
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200107 }
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100108 else
109 activity[tid].poll_exp++;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200110
Willy Tarreauce036bc2018-01-29 14:58:02 +0100111 fd = global.tune.maxpollevents;
Willy Tarreau45a12512011-09-10 16:56:42 +0200112 gettimeofday(&before_poll, NULL);
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100113 status = kevent(kqueue_fd[tid], // int kq
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200114 NULL, // const struct kevent *changelist
115 0, // int nchanges
116 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200117 fd, // int nevents
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200118 &timeout); // const struct timespec *timeout
119 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200120 measure_idle();
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200121
122 for (count = 0; count < status; count++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200123 unsigned int n = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200124 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200125
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100126 if (!fdtab[fd].owner) {
127 activity[tid].poll_dead++;
Willy Tarreau076be252012-07-06 16:02:29 +0200128 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100129 }
130
131 if (!(fdtab[fd].thread_mask & tid_bit)) {
132 activity[tid].poll_skip++;
133 continue;
134 }
Willy Tarreau076be252012-07-06 16:02:29 +0200135
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200136 if (kev[count].filter == EVFILT_READ) {
Willy Tarreaufa061762017-03-13 20:49:56 +0100137 if (kev[count].data)
Christopher Fauletab62f512017-08-30 10:34:36 +0200138 n |= FD_POLL_IN;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100139 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200140 n |= FD_POLL_HUP;
Willy Tarreau4a226272012-11-11 20:49:49 +0100141 }
142 else if (kev[count].filter == EVFILT_WRITE) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200143 n |= FD_POLL_OUT;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100144 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200145 n |= FD_POLL_ERR;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200146 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100147
Christopher Fauletab62f512017-08-30 10:34:36 +0200148 fd_update_events(fd, n);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200149 }
150}
151
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200152
153static int init_kqueue_per_thread()
154{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100155 int fd;
156
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200157 /* we can have up to two events per fd (*/
158 kev = calloc(1, sizeof(struct kevent) * 2 * global.maxsock);
159 if (kev == NULL)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100160 goto fail_alloc;
161
Christopher Faulet727c89b2018-01-25 16:40:35 +0100162 if (MAX_THREADS > 1 && tid) {
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100163 kqueue_fd[tid] = kqueue();
164 if (kqueue_fd[tid] < 0)
165 goto fail_fd;
166 }
167
168 /* we may have to unregister some events initially registered on the
169 * original fd when it was alone, and/or to register events on the new
170 * fd for this thread. Let's just mark them as updated, the poller will
171 * do the rest.
172 */
Willy Tarreauce036bc2018-01-29 14:58:02 +0100173 for (fd = 0; fd < global.maxsock; fd++)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100174 updt_fd_polling(fd);
175
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200176 return 1;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100177 fail_fd:
178 free(kev);
179 fail_alloc:
180 return 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200181}
182
183static void deinit_kqueue_per_thread()
184{
Christopher Faulet727c89b2018-01-25 16:40:35 +0100185 if (MAX_THREADS > 1 && tid)
Christopher Faulet13b007d2018-01-25 16:32:18 +0100186 close(kqueue_fd[tid]);
187
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200188 free(kev);
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200189 kev = NULL;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200190}
191
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200192/*
193 * Initialization of the kqueue() poller.
194 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
195 * disables the poller by setting its pref to 0.
196 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200197REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200198{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200199 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200200
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100201 kqueue_fd[tid] = kqueue();
202 if (kqueue_fd[tid] < 0)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200203 goto fail_fd;
204
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200205 hap_register_per_thread_init(init_kqueue_per_thread);
206 hap_register_per_thread_deinit(deinit_kqueue_per_thread);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200207 return 1;
208
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200209 fail_fd:
210 p->pref = 0;
211 return 0;
212}
213
214/*
215 * Termination of the kqueue() poller.
216 * Memory is released and the poller is marked as unselectable.
217 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200218REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200219{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100220 if (kqueue_fd[tid] >= 0) {
221 close(kqueue_fd[tid]);
222 kqueue_fd[tid] = -1;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200223 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200224
225 p->private = NULL;
226 p->pref = 0;
227}
228
229/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200230 * Check that the poller works.
231 * Returns 1 if OK, otherwise 0.
232 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200233REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200234{
235 int fd;
236
237 fd = kqueue();
238 if (fd < 0)
239 return 0;
240 close(fd);
241 return 1;
242}
243
244/*
245 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
246 * otherwise 0. Note that some pollers need to be reopened after a fork()
247 * (such as kqueue), and some others may fail to do so in a chroot.
248 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200249REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200250{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100251 kqueue_fd[tid] = kqueue();
252 if (kqueue_fd[tid] < 0)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200253 return 0;
254 return 1;
255}
256
257/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200258 * It is a constructor, which means that it will automatically be called before
259 * main(). This is GCC-specific but it works at least since 2.95.
260 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200261 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200262__attribute__((constructor))
263static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200264{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200265 struct poller *p;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100266 int i;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200267
268 if (nbpollers >= MAX_POLLERS)
269 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200270
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100271 for (i = 0; i < MAX_THREADS; i++)
272 kqueue_fd[i] = -1;
273
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200274 p = &pollers[nbpollers++];
275
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200276 p->name = "kqueue";
277 p->pref = 300;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100278 p->flags = HAP_POLL_F_RDHUP;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200279 p->private = NULL;
280
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100281 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200282 p->test = _do_test;
283 p->init = _do_init;
284 p->term = _do_term;
285 p->poll = _do_poll;
286 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200287}
288
289
290/*
291 * Local variables:
292 * c-indent-level: 8
293 * c-basic-offset: 8
294 * End:
295 */