blob: 7e626d112db3d019b3a10836fb16d2a6a31e366b [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 Tarreau332740d2009-05-10 09:57:21 +020033#include <proto/signal.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020034#include <proto/task.h>
35
36/* private data */
37static fd_set *fd_evts[2];
38static int kqueue_fd;
39static struct kevent *kev = NULL;
40
41/* speeds up conversion of DIR_RD/DIR_WR to EVFILT* */
42static const int dir2filt[2] = { EVFILT_READ, EVFILT_WRITE };
43
44/* completes a change list for deletion */
45REGPRM3 static int kqev_del(struct kevent *kev, const int fd, const int dir)
46{
47 if (FD_ISSET(fd, fd_evts[dir])) {
48 FD_CLR(fd, fd_evts[dir]);
49 EV_SET(kev, fd, dir2filt[dir], EV_DELETE, 0, 0, NULL);
50 return 1;
51 }
52 return 0;
53}
54
55/*
56 * Returns non-zero if direction <dir> is already set for <fd>.
57 */
Willy Tarreau63455a92007-04-09 15:34:49 +020058REGPRM2 static int __fd_is_set(const int fd, int dir)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020059{
60 return FD_ISSET(fd, fd_evts[dir]);
61}
62
63REGPRM2 static int __fd_set(const int fd, int dir)
64{
65 /* if the value was set, do nothing */
66 if (FD_ISSET(fd, fd_evts[dir]))
67 return 0;
68
69 FD_SET(fd, fd_evts[dir]);
Willy Tarreau87552852007-04-09 17:16:07 +020070 EV_SET(kev, fd, dir2filt[dir], EV_ADD, 0, 0, NULL);
Willy Tarreau1e63130a2007-04-09 12:03:06 +020071 kevent(kqueue_fd, kev, 1, NULL, 0, NULL);
72 return 1;
73}
74
75REGPRM2 static int __fd_clr(const int fd, int dir)
76{
Willy Tarreau1e63130a2007-04-09 12:03:06 +020077 if (!kqev_del(kev, fd, dir))
78 return 0;
79 kevent(kqueue_fd, kev, 1, NULL, 0, NULL);
80 return 1;
81}
82
83REGPRM1 static void __fd_rem(int fd)
84{
85 int changes = 0;
86
Willy Tarreau1e63130a2007-04-09 12:03:06 +020087 changes += kqev_del(&kev[changes], fd, DIR_RD);
88 changes += kqev_del(&kev[changes], fd, DIR_WR);
89
90 if (changes)
91 kevent(kqueue_fd, kev, changes, NULL, 0, NULL);
92}
93
Willy Tarreau87552852007-04-09 17:16:07 +020094REGPRM1 static void __fd_clo(int fd)
95{
96 FD_CLR(fd, fd_evts[DIR_RD]);
97 FD_CLR(fd, fd_evts[DIR_WR]);
98}
99
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200100/*
101 * kqueue() poller
102 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200103REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200104{
105 int status;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200106 int count, fd, delta_ms;
107 struct timespec timeout;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200108
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200109 delta_ms = 0;
110 timeout.tv_sec = 0;
111 timeout.tv_nsec = 0;
Willy Tarreau79b8a622007-05-14 03:15:46 +0200112
Willy Tarreau332740d2009-05-10 09:57:21 +0200113 if (!run_queue && !signal_queue_len) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200114 if (!exp) {
115 delta_ms = MAX_DELAY_MS;
116 timeout.tv_sec = (MAX_DELAY_MS / 1000);
117 timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200118 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200119 else if (!tick_is_expired(exp, now_ms)) {
120 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
121 if (delta_ms > MAX_DELAY_MS)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200122 delta_ms = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200123 timeout.tv_sec = (delta_ms / 1000);
124 timeout.tv_nsec = (delta_ms % 1000) * 1000000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200125 }
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200126 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200127
Willy Tarreau1db37712007-06-03 17:16:49 +0200128 fd = MIN(maxfd, global.tune.maxpollevents);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200129 status = kevent(kqueue_fd, // int kq
130 NULL, // const struct kevent *changelist
131 0, // int nchanges
132 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200133 fd, // int nevents
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200134 &timeout); // const struct timespec *timeout
135 tv_update_date(delta_ms, status);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200136
137 for (count = 0; count < status; count++) {
138 fd = kev[count].ident;
139 if (kev[count].filter == EVFILT_READ) {
140 if (FD_ISSET(fd, fd_evts[DIR_RD])) {
141 if (fdtab[fd].state == FD_STCLOSE)
142 continue;
143 fdtab[fd].cb[DIR_RD].f(fd);
144 }
145 } else if (kev[count].filter == EVFILT_WRITE) {
146 if (FD_ISSET(fd, fd_evts[DIR_WR])) {
147 if (fdtab[fd].state == FD_STCLOSE)
148 continue;
149 fdtab[fd].cb[DIR_WR].f(fd);
150 }
151 }
152 }
153}
154
155/*
156 * Initialization of the kqueue() poller.
157 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
158 * disables the poller by setting its pref to 0.
159 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200160REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200161{
162 __label__ fail_wevt, fail_revt, fail_fd;
163 int fd_set_bytes;
164
165 p->private = NULL;
166 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
167
168 kqueue_fd = kqueue();
169 if (kqueue_fd < 0)
170 goto fail_fd;
171
Willy Tarreau1db37712007-06-03 17:16:49 +0200172 kev = (struct kevent*)calloc(1, sizeof(struct kevent) * global.tune.maxpollevents);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200173
174 if (kev == NULL)
175 goto fail_kev;
176
177 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
178 goto fail_revt;
179
180 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
181 goto fail_wevt;
182
183 return 1;
184
185 fail_wevt:
186 free(fd_evts[DIR_RD]);
187 fail_revt:
188 free(kev);
189 fail_kev:
190 close(kqueue_fd);
191 kqueue_fd = 0;
192 fail_fd:
193 p->pref = 0;
194 return 0;
195}
196
197/*
198 * Termination of the kqueue() poller.
199 * Memory is released and the poller is marked as unselectable.
200 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200201REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200202{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200203 free(fd_evts[DIR_WR]);
204 free(fd_evts[DIR_RD]);
205 free(kev);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200206 close(kqueue_fd);
207 kqueue_fd = 0;
208
209 p->private = NULL;
210 p->pref = 0;
211}
212
213/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200214 * Check that the poller works.
215 * Returns 1 if OK, otherwise 0.
216 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200217REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200218{
219 int fd;
220
221 fd = kqueue();
222 if (fd < 0)
223 return 0;
224 close(fd);
225 return 1;
226}
227
228/*
229 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
230 * otherwise 0. Note that some pollers need to be reopened after a fork()
231 * (such as kqueue), and some others may fail to do so in a chroot.
232 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200233REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200234{
235 close(kqueue_fd);
236 kqueue_fd = kqueue();
237 if (kqueue_fd < 0)
238 return 0;
239 return 1;
240}
241
242/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200243 * It is a constructor, which means that it will automatically be called before
244 * main(). This is GCC-specific but it works at least since 2.95.
245 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200246 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200247__attribute__((constructor))
248static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200249{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200250 struct poller *p;
251
252 if (nbpollers >= MAX_POLLERS)
253 return;
254 p = &pollers[nbpollers++];
255
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200256 p->name = "kqueue";
257 p->pref = 300;
258 p->private = NULL;
259
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200260 p->test = _do_test;
261 p->init = _do_init;
262 p->term = _do_term;
263 p->poll = _do_poll;
264 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200265
Willy Tarreau63455a92007-04-09 15:34:49 +0200266 p->is_set = __fd_is_set;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200267 p->cond_s = p->set = __fd_set;
268 p->cond_c = p->clr = __fd_clr;
Willy Tarreau40562cb2007-04-09 20:38:57 +0200269 p->rem = __fd_rem;
270 p->clo = __fd_clo;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200271}
272
273
274/*
275 * Local variables:
276 * c-indent-level: 8
277 * c-basic-offset: 8
278 * End:
279 */