blob: f5201060502aa11d25b47bf967a6932eea94f628 [file] [log] [blame]
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001/*
2 * FD polling functions for FreeBSD kqueue()
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 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 *
11 * Note: not knowing much about kqueue, I had to rely on OpenBSD's detailed man
12 * page and to check how it was implemented in lighttpd to understand it better.
13 * But it is possible that I got things wrong.
14 *
15 */
16
Willy Tarreau1e63130a2007-04-09 12:03:06 +020017#include <unistd.h>
18#include <sys/time.h>
19#include <sys/types.h>
20
21#include <sys/event.h>
22#include <sys/time.h>
23
24#include <common/compat.h>
25#include <common/config.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020026#include <common/ticks.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020027#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020028#include <common/tools.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020029
Willy Tarreau1e63130a2007-04-09 12:03:06 +020030#include <types/global.h>
31
32#include <proto/fd.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020033#include <proto/task.h>
34
35/* private data */
36static fd_set *fd_evts[2];
37static int kqueue_fd;
38static struct kevent *kev = NULL;
39
40/* speeds up conversion of DIR_RD/DIR_WR to EVFILT* */
41static const int dir2filt[2] = { EVFILT_READ, EVFILT_WRITE };
42
43/* completes a change list for deletion */
44REGPRM3 static int kqev_del(struct kevent *kev, const int fd, const int dir)
45{
46 if (FD_ISSET(fd, fd_evts[dir])) {
47 FD_CLR(fd, fd_evts[dir]);
48 EV_SET(kev, fd, dir2filt[dir], EV_DELETE, 0, 0, NULL);
49 return 1;
50 }
51 return 0;
52}
53
54/*
55 * Returns non-zero if direction <dir> is already set for <fd>.
56 */
Willy Tarreau63455a92007-04-09 15:34:49 +020057REGPRM2 static int __fd_is_set(const int fd, int dir)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020058{
59 return FD_ISSET(fd, fd_evts[dir]);
60}
61
62REGPRM2 static int __fd_set(const int fd, int dir)
63{
64 /* if the value was set, do nothing */
65 if (FD_ISSET(fd, fd_evts[dir]))
66 return 0;
67
68 FD_SET(fd, fd_evts[dir]);
Willy Tarreau87552852007-04-09 17:16:07 +020069 EV_SET(kev, fd, dir2filt[dir], EV_ADD, 0, 0, NULL);
Willy Tarreau1e63130a2007-04-09 12:03:06 +020070 kevent(kqueue_fd, kev, 1, NULL, 0, NULL);
71 return 1;
72}
73
74REGPRM2 static int __fd_clr(const int fd, int dir)
75{
Willy Tarreau1e63130a2007-04-09 12:03:06 +020076 if (!kqev_del(kev, fd, dir))
77 return 0;
78 kevent(kqueue_fd, kev, 1, NULL, 0, NULL);
79 return 1;
80}
81
82REGPRM1 static void __fd_rem(int fd)
83{
84 int changes = 0;
85
Willy Tarreau1e63130a2007-04-09 12:03:06 +020086 changes += kqev_del(&kev[changes], fd, DIR_RD);
87 changes += kqev_del(&kev[changes], fd, DIR_WR);
88
89 if (changes)
90 kevent(kqueue_fd, kev, changes, NULL, 0, NULL);
91}
92
Willy Tarreau87552852007-04-09 17:16:07 +020093REGPRM1 static void __fd_clo(int fd)
94{
95 FD_CLR(fd, fd_evts[DIR_RD]);
96 FD_CLR(fd, fd_evts[DIR_WR]);
97}
98
Willy Tarreau1e63130a2007-04-09 12:03:06 +020099/*
100 * kqueue() poller
101 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200102REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200103{
104 int status;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200105 int count, fd, delta_ms;
106 struct timespec timeout;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200107
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200108 delta_ms = 0;
109 timeout.tv_sec = 0;
110 timeout.tv_nsec = 0;
Willy Tarreau79b8a622007-05-14 03:15:46 +0200111
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200112 if (!run_queue) {
113 if (!exp) {
114 delta_ms = MAX_DELAY_MS;
115 timeout.tv_sec = (MAX_DELAY_MS / 1000);
116 timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200117 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200118 else if (!tick_is_expired(exp, now_ms)) {
119 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
120 if (delta_ms > MAX_DELAY_MS)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200121 delta_ms = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200122 timeout.tv_sec = (delta_ms / 1000);
123 timeout.tv_nsec = (delta_ms % 1000) * 1000000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200124 }
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200125 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200126
Willy Tarreau1db37712007-06-03 17:16:49 +0200127 fd = MIN(maxfd, global.tune.maxpollevents);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200128 status = kevent(kqueue_fd, // int kq
129 NULL, // const struct kevent *changelist
130 0, // int nchanges
131 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200132 fd, // int nevents
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200133 &timeout); // const struct timespec *timeout
134 tv_update_date(delta_ms, status);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200135
136 for (count = 0; count < status; count++) {
137 fd = kev[count].ident;
138 if (kev[count].filter == EVFILT_READ) {
139 if (FD_ISSET(fd, fd_evts[DIR_RD])) {
140 if (fdtab[fd].state == FD_STCLOSE)
141 continue;
142 fdtab[fd].cb[DIR_RD].f(fd);
143 }
144 } else if (kev[count].filter == EVFILT_WRITE) {
145 if (FD_ISSET(fd, fd_evts[DIR_WR])) {
146 if (fdtab[fd].state == FD_STCLOSE)
147 continue;
148 fdtab[fd].cb[DIR_WR].f(fd);
149 }
150 }
151 }
152}
153
154/*
155 * Initialization of the kqueue() poller.
156 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
157 * disables the poller by setting its pref to 0.
158 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200159REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200160{
161 __label__ fail_wevt, fail_revt, fail_fd;
162 int fd_set_bytes;
163
164 p->private = NULL;
165 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
166
167 kqueue_fd = kqueue();
168 if (kqueue_fd < 0)
169 goto fail_fd;
170
Willy Tarreau1db37712007-06-03 17:16:49 +0200171 kev = (struct kevent*)calloc(1, sizeof(struct kevent) * global.tune.maxpollevents);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200172
173 if (kev == NULL)
174 goto fail_kev;
175
176 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
177 goto fail_revt;
178
179 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
180 goto fail_wevt;
181
182 return 1;
183
184 fail_wevt:
185 free(fd_evts[DIR_RD]);
186 fail_revt:
187 free(kev);
188 fail_kev:
189 close(kqueue_fd);
190 kqueue_fd = 0;
191 fail_fd:
192 p->pref = 0;
193 return 0;
194}
195
196/*
197 * Termination of the kqueue() poller.
198 * Memory is released and the poller is marked as unselectable.
199 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200200REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200201{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200202 free(fd_evts[DIR_WR]);
203 free(fd_evts[DIR_RD]);
204 free(kev);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200205 close(kqueue_fd);
206 kqueue_fd = 0;
207
208 p->private = NULL;
209 p->pref = 0;
210}
211
212/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200213 * Check that the poller works.
214 * Returns 1 if OK, otherwise 0.
215 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200216REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200217{
218 int fd;
219
220 fd = kqueue();
221 if (fd < 0)
222 return 0;
223 close(fd);
224 return 1;
225}
226
227/*
228 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
229 * otherwise 0. Note that some pollers need to be reopened after a fork()
230 * (such as kqueue), and some others may fail to do so in a chroot.
231 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200232REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200233{
234 close(kqueue_fd);
235 kqueue_fd = kqueue();
236 if (kqueue_fd < 0)
237 return 0;
238 return 1;
239}
240
241/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200242 * It is a constructor, which means that it will automatically be called before
243 * main(). This is GCC-specific but it works at least since 2.95.
244 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200245 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200246__attribute__((constructor))
247static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200248{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200249 struct poller *p;
250
251 if (nbpollers >= MAX_POLLERS)
252 return;
253 p = &pollers[nbpollers++];
254
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200255 p->name = "kqueue";
256 p->pref = 300;
257 p->private = NULL;
258
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200259 p->test = _do_test;
260 p->init = _do_init;
261 p->term = _do_term;
262 p->poll = _do_poll;
263 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200264
Willy Tarreau63455a92007-04-09 15:34:49 +0200265 p->is_set = __fd_is_set;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200266 p->cond_s = p->set = __fd_set;
267 p->cond_c = p->clr = __fd_clr;
Willy Tarreau40562cb2007-04-09 20:38:57 +0200268 p->rem = __fd_rem;
269 p->clo = __fd_clo;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200270}
271
272
273/*
274 * Local variables:
275 * c-indent-level: 8
276 * c-basic-offset: 8
277 * End:
278 */