blob: 326d616078336caf06422597620a06d2660640bd [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 Tarreau1e63130a2007-04-09 12:03:06 +020032static int 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
50 if (!fdtab[fd].owner)
51 continue;
52
Christopher Fauletd4604ad2017-05-29 10:40:41 +020053 SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
54 fdtab[fd].updated = 0;
55 fdtab[fd].new = 0;
56
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;
60 SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreauf817e9f2014-01-10 16:58:45 +010061
62 if ((eo ^ en) & FD_EV_POLLED_RW) {
63 /* poll status changed */
Willy Tarreau4a226272012-11-11 20:49:49 +010064 if ((eo ^ en) & FD_EV_POLLED_R) {
65 /* read poll status changed */
66 if (en & FD_EV_POLLED_R) {
67 EV_SET(&kev[changes], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
68 changes++;
69 }
70 else {
71 EV_SET(&kev[changes], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
72 changes++;
73 }
74 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +020075
Willy Tarreau4a226272012-11-11 20:49:49 +010076 if ((eo ^ en) & FD_EV_POLLED_W) {
77 /* write poll status changed */
78 if (en & FD_EV_POLLED_W) {
79 EV_SET(&kev[changes], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
80 changes++;
81 }
82 else {
83 EV_SET(&kev[changes], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
84 changes++;
85 }
86 }
Willy Tarreau4a226272012-11-11 20:49:49 +010087 }
Willy Tarreau4a226272012-11-11 20:49:49 +010088 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +020089 if (changes)
90 kevent(kqueue_fd, kev, changes, NULL, 0, NULL);
Willy Tarreau4a226272012-11-11 20:49:49 +010091 fd_nbupdt = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020092
Willy Tarreau0c303ee2008-07-07 00:09:58 +020093 delta_ms = 0;
94 timeout.tv_sec = 0;
95 timeout.tv_nsec = 0;
Willy Tarreau79b8a622007-05-14 03:15:46 +020096
Willy Tarreau10146c92015-04-13 20:44:19 +020097 if (!exp) {
98 delta_ms = MAX_DELAY_MS;
99 timeout.tv_sec = (MAX_DELAY_MS / 1000);
100 timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000;
101 }
102 else if (!tick_is_expired(exp, now_ms)) {
103 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
104 if (delta_ms > MAX_DELAY_MS)
105 delta_ms = MAX_DELAY_MS;
106 timeout.tv_sec = (delta_ms / 1000);
107 timeout.tv_nsec = (delta_ms % 1000) * 1000000;
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200108 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200109
Willy Tarreau1db37712007-06-03 17:16:49 +0200110 fd = MIN(maxfd, global.tune.maxpollevents);
Willy Tarreau45a12512011-09-10 16:56:42 +0200111 gettimeofday(&before_poll, NULL);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200112 status = kevent(kqueue_fd, // int kq
113 NULL, // const struct kevent *changelist
114 0, // int nchanges
115 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200116 fd, // int nevents
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200117 &timeout); // const struct timespec *timeout
118 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200119 measure_idle();
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200120
121 for (count = 0; count < status; count++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200122 unsigned int n = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200123 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200124
Willy Tarreau076be252012-07-06 16:02:29 +0200125 if (!fdtab[fd].owner)
126 continue;
127
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200128 if (kev[count].filter == EVFILT_READ) {
Willy Tarreaufa061762017-03-13 20:49:56 +0100129 if (kev[count].data)
Christopher Fauletab62f512017-08-30 10:34:36 +0200130 n |= FD_POLL_IN;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100131 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200132 n |= FD_POLL_HUP;
Willy Tarreau4a226272012-11-11 20:49:49 +0100133 }
134 else if (kev[count].filter == EVFILT_WRITE) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200135 n |= FD_POLL_OUT;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100136 if (kev[count].flags & EV_EOF)
Christopher Fauletab62f512017-08-30 10:34:36 +0200137 n |= FD_POLL_ERR;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200138 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100139
Christopher Fauletab62f512017-08-30 10:34:36 +0200140 fd_update_events(fd, n);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200141 }
142}
143
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200144
145static int init_kqueue_per_thread()
146{
147 /* we can have up to two events per fd (*/
148 kev = calloc(1, sizeof(struct kevent) * 2 * global.maxsock);
149 if (kev == NULL)
150 return 0;
151 return 1;
152}
153
154static void deinit_kqueue_per_thread()
155{
156 free(kev);
157}
158
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200159/*
160 * Initialization of the kqueue() poller.
161 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
162 * disables the poller by setting its pref to 0.
163 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200164REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200165{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200166 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200167
168 kqueue_fd = kqueue();
169 if (kqueue_fd < 0)
170 goto fail_fd;
171
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200172 if (global.nbthread > 1) {
173 hap_register_per_thread_init(init_kqueue_per_thread);
174 hap_register_per_thread_deinit(deinit_kqueue_per_thread);
175 }
176 else if (!init_kqueue_per_thread())
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200177 goto fail_kev;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200178
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200179 return 1;
180
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200181 fail_kev:
182 close(kqueue_fd);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200183 kqueue_fd = -1;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200184 fail_fd:
185 p->pref = 0;
186 return 0;
187}
188
189/*
190 * Termination of the kqueue() poller.
191 * Memory is released and the poller is marked as unselectable.
192 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200193REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200194{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200195 free(kev);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200196
197 if (kqueue_fd >= 0) {
198 close(kqueue_fd);
199 kqueue_fd = -1;
200 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200201
202 p->private = NULL;
203 p->pref = 0;
204}
205
206/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200207 * Check that the poller works.
208 * Returns 1 if OK, otherwise 0.
209 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200210REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200211{
212 int fd;
213
214 fd = kqueue();
215 if (fd < 0)
216 return 0;
217 close(fd);
218 return 1;
219}
220
221/*
222 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
223 * otherwise 0. Note that some pollers need to be reopened after a fork()
224 * (such as kqueue), and some others may fail to do so in a chroot.
225 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200226REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200227{
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200228 if (kqueue_fd >= 0)
229 close(kqueue_fd);
Willy Tarreau2ff76222007-04-09 19:29:56 +0200230 kqueue_fd = kqueue();
231 if (kqueue_fd < 0)
232 return 0;
233 return 1;
234}
235
236/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200237 * It is a constructor, which means that it will automatically be called before
238 * main(). This is GCC-specific but it works at least since 2.95.
239 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200240 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200241__attribute__((constructor))
242static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200243{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200244 struct poller *p;
245
246 if (nbpollers >= MAX_POLLERS)
247 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200248
249 kqueue_fd = -1;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200250 p = &pollers[nbpollers++];
251
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200252 p->name = "kqueue";
253 p->pref = 300;
Willy Tarreau19c4ab92017-03-13 20:36:48 +0100254 p->flags = HAP_POLL_F_RDHUP;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200255 p->private = NULL;
256
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100257 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200258 p->test = _do_test;
259 p->init = _do_init;
260 p->term = _do_term;
261 p->poll = _do_poll;
262 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200263}
264
265
266/*
267 * Local variables:
268 * c-indent-level: 8
269 * c-basic-offset: 8
270 * End:
271 */