blob: cc96307b0ad94e6903dc56c8d6a67c184be8e905 [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;
Willy Tarreau7d24fad2018-01-25 14:57:25 +010043 int updt_idx, en;
Willy Tarreau4a226272012-11-11 20:49:49 +010044 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
Willy Tarreau7d24fad2018-01-25 14:57:25 +010055 en = fdtab[fd].state;
Willy Tarreau62a627a2018-01-25 18:06:46 +010056 HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Willy Tarreauf817e9f2014-01-10 16:58:45 +010057
Willy Tarreau7a2364d2018-01-19 08:56:14 +010058 if (!(fdtab[fd].thread_mask & tid_bit) || !(en & FD_EV_POLLED_RW)) {
59 if (!(fdtab[fd].polled_mask & tid_bit)) {
60 /* fd was not watched, it's still not */
61 continue;
Willy Tarreau4a226272012-11-11 20:49:49 +010062 }
Willy Tarreau7a2364d2018-01-19 08:56:14 +010063 /* fd totally removed from poll list */
64 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
65 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
66 HA_ATOMIC_AND(&fdtab[fd].polled_mask, ~tid_bit);
67 }
68 else {
69 /* OK fd has to be monitored, it was either added or changed */
Willy Tarreau1e63130a2007-04-09 12:03:06 +020070
Willy Tarreau7a2364d2018-01-19 08:56:14 +010071 if (en & FD_EV_POLLED_R)
72 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
73 else if (fdtab[fd].polled_mask & tid_bit)
74 EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
75
76 if (en & FD_EV_POLLED_W)
77 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
78 else if (fdtab[fd].polled_mask & tid_bit)
79 EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
80
81 HA_ATOMIC_OR(&fdtab[fd].polled_mask, tid_bit);
Willy Tarreau4a226272012-11-11 20:49:49 +010082 }
Willy Tarreau4a226272012-11-11 20:49:49 +010083 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +020084 if (changes)
Willy Tarreau7a2364d2018-01-19 08:56:14 +010085 kevent(kqueue_fd[tid], kev, changes, NULL, 0, NULL);
Willy Tarreau4a226272012-11-11 20:49:49 +010086 fd_nbupdt = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020087
Willy Tarreau0c303ee2008-07-07 00:09:58 +020088 delta_ms = 0;
89 timeout.tv_sec = 0;
90 timeout.tv_nsec = 0;
Willy Tarreau79b8a622007-05-14 03:15:46 +020091
Willy Tarreau10146c92015-04-13 20:44:19 +020092 if (!exp) {
93 delta_ms = MAX_DELAY_MS;
94 timeout.tv_sec = (MAX_DELAY_MS / 1000);
95 timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000;
96 }
97 else if (!tick_is_expired(exp, now_ms)) {
98 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
99 if (delta_ms > MAX_DELAY_MS)
100 delta_ms = MAX_DELAY_MS;
101 timeout.tv_sec = (delta_ms / 1000);
102 timeout.tv_nsec = (delta_ms % 1000) * 1000000;
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200103 }
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100104 else
105 activity[tid].poll_exp++;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200106
Willy Tarreauce036bc2018-01-29 14:58:02 +0100107 fd = global.tune.maxpollevents;
Willy Tarreau45a12512011-09-10 16:56:42 +0200108 gettimeofday(&before_poll, NULL);
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100109 status = kevent(kqueue_fd[tid], // int kq
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200110 NULL, // const struct kevent *changelist
111 0, // int nchanges
112 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200113 fd, // int nevents
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200114 &timeout); // const struct timespec *timeout
115 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200116 measure_idle();
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200117
118 for (count = 0; count < status; count++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200119 unsigned int n = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200120 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200121
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100122 if (!fdtab[fd].owner) {
123 activity[tid].poll_dead++;
Willy Tarreau076be252012-07-06 16:02:29 +0200124 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100125 }
126
127 if (!(fdtab[fd].thread_mask & tid_bit)) {
128 activity[tid].poll_skip++;
129 continue;
130 }
Willy Tarreau076be252012-07-06 16:02:29 +0200131
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200132 if (kev[count].filter == EVFILT_READ) {
Willy Tarreaufa061762017-03-13 20:49:56 +0100133 if (kev[count].data)
Christopher Fauletab62f512017-08-30 10:34:36 +0200134 n |= FD_POLL_IN;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100135 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200136 n |= FD_POLL_HUP;
Willy Tarreau4a226272012-11-11 20:49:49 +0100137 }
138 else if (kev[count].filter == EVFILT_WRITE) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200139 n |= FD_POLL_OUT;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100140 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200141 n |= FD_POLL_ERR;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200142 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100143
Christopher Fauletab62f512017-08-30 10:34:36 +0200144 fd_update_events(fd, n);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200145 }
146}
147
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200148
149static int init_kqueue_per_thread()
150{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100151 int fd;
152
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200153 /* we can have up to two events per fd (*/
154 kev = calloc(1, sizeof(struct kevent) * 2 * global.maxsock);
155 if (kev == NULL)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100156 goto fail_alloc;
157
Christopher Faulet727c89b2018-01-25 16:40:35 +0100158 if (MAX_THREADS > 1 && tid) {
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100159 kqueue_fd[tid] = kqueue();
160 if (kqueue_fd[tid] < 0)
161 goto fail_fd;
162 }
163
164 /* we may have to unregister some events initially registered on the
165 * original fd when it was alone, and/or to register events on the new
166 * fd for this thread. Let's just mark them as updated, the poller will
167 * do the rest.
168 */
Willy Tarreauce036bc2018-01-29 14:58:02 +0100169 for (fd = 0; fd < global.maxsock; fd++)
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100170 updt_fd_polling(fd);
171
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200172 return 1;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100173 fail_fd:
174 free(kev);
175 fail_alloc:
176 return 0;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200177}
178
179static void deinit_kqueue_per_thread()
180{
Christopher Faulet727c89b2018-01-25 16:40:35 +0100181 if (MAX_THREADS > 1 && tid)
Christopher Faulet13b007d2018-01-25 16:32:18 +0100182 close(kqueue_fd[tid]);
183
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200184 free(kev);
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200185 kev = NULL;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200186}
187
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200188/*
189 * Initialization of the kqueue() poller.
190 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
191 * disables the poller by setting its pref to 0.
192 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200193REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200194{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200195 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200196
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100197 kqueue_fd[tid] = kqueue();
198 if (kqueue_fd[tid] < 0)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200199 goto fail_fd;
200
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200201 hap_register_per_thread_init(init_kqueue_per_thread);
202 hap_register_per_thread_deinit(deinit_kqueue_per_thread);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200203 return 1;
204
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200205 fail_fd:
206 p->pref = 0;
207 return 0;
208}
209
210/*
211 * Termination of the kqueue() poller.
212 * Memory is released and the poller is marked as unselectable.
213 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200214REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200215{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100216 if (kqueue_fd[tid] >= 0) {
217 close(kqueue_fd[tid]);
218 kqueue_fd[tid] = -1;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200219 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200220
221 p->private = NULL;
222 p->pref = 0;
223}
224
225/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200226 * Check that the poller works.
227 * Returns 1 if OK, otherwise 0.
228 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200229REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200230{
231 int fd;
232
233 fd = kqueue();
234 if (fd < 0)
235 return 0;
236 close(fd);
237 return 1;
238}
239
240/*
241 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
242 * otherwise 0. Note that some pollers need to be reopened after a fork()
243 * (such as kqueue), and some others may fail to do so in a chroot.
244 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200245REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200246{
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100247 kqueue_fd[tid] = kqueue();
248 if (kqueue_fd[tid] < 0)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200249 return 0;
250 return 1;
251}
252
253/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200254 * It is a constructor, which means that it will automatically be called before
255 * main(). This is GCC-specific but it works at least since 2.95.
256 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200257 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200258__attribute__((constructor))
259static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200260{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200261 struct poller *p;
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100262 int i;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200263
264 if (nbpollers >= MAX_POLLERS)
265 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200266
Willy Tarreau7a2364d2018-01-19 08:56:14 +0100267 for (i = 0; i < MAX_THREADS; i++)
268 kqueue_fd[i] = -1;
269
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200270 p = &pollers[nbpollers++];
271
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200272 p->name = "kqueue";
273 p->pref = 300;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100274 p->flags = HAP_POLL_F_RDHUP;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200275 p->private = NULL;
276
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100277 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200278 p->test = _do_test;
279 p->init = _do_init;
280 p->term = _do_term;
281 p->poll = _do_poll;
282 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200283}
284
285
286/*
287 * Local variables:
288 * c-indent-level: 8
289 * c-basic-offset: 8
290 * End:
291 */