blob: 773db74a3bfa8dd13661b2f00330fce24df46cb6 [file] [log] [blame]
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001/*
2 * FD polling functions for FreeBSD kqueue()
3 *
4 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
5 *
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>
26#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020027#include <common/tools.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020028
29#include <types/fd.h>
30#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 Tarreau79b8a622007-05-14 03:15:46 +0200102REGPRM2 static void _do_poll(struct poller *p, struct timeval *exp)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200103{
104 int status;
105 int count, fd;
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200106 struct timespec timeout, *to_ptr;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200107
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200108 to_ptr = NULL; // no timeout
Willy Tarreau79b8a622007-05-14 03:15:46 +0200109 if (tv_isset(exp)) {
110 struct timeval delta;
111
112 if (tv_isge(&now, exp))
113 delta.tv_sec = delta.tv_usec = 0;
114 else
115 tv_remain(&now, exp, &delta);
116
117 timeout.tv_sec = delta.tv_sec;
118 timeout.tv_nsec = delta.tv_usec * 1000;
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200119 to_ptr = &timeout;
120 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200121
Willy Tarreau1db37712007-06-03 17:16:49 +0200122 fd = MIN(maxfd, global.tune.maxpollevents);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200123 status = kevent(kqueue_fd, // int kq
124 NULL, // const struct kevent *changelist
125 0, // int nchanges
126 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200127 fd, // int nevents
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200128 to_ptr); // const struct timespec *timeout
Willy Tarreau258696f2007-04-10 02:31:54 +0200129 tv_now(&now);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200130
131 for (count = 0; count < status; count++) {
132 fd = kev[count].ident;
133 if (kev[count].filter == EVFILT_READ) {
134 if (FD_ISSET(fd, fd_evts[DIR_RD])) {
135 if (fdtab[fd].state == FD_STCLOSE)
136 continue;
137 fdtab[fd].cb[DIR_RD].f(fd);
138 }
139 } else if (kev[count].filter == EVFILT_WRITE) {
140 if (FD_ISSET(fd, fd_evts[DIR_WR])) {
141 if (fdtab[fd].state == FD_STCLOSE)
142 continue;
143 fdtab[fd].cb[DIR_WR].f(fd);
144 }
145 }
146 }
147}
148
149/*
150 * Initialization of the kqueue() poller.
151 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
152 * disables the poller by setting its pref to 0.
153 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200154REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200155{
156 __label__ fail_wevt, fail_revt, fail_fd;
157 int fd_set_bytes;
158
159 p->private = NULL;
160 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
161
162 kqueue_fd = kqueue();
163 if (kqueue_fd < 0)
164 goto fail_fd;
165
Willy Tarreau1db37712007-06-03 17:16:49 +0200166 kev = (struct kevent*)calloc(1, sizeof(struct kevent) * global.tune.maxpollevents);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200167
168 if (kev == NULL)
169 goto fail_kev;
170
171 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
172 goto fail_revt;
173
174 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
175 goto fail_wevt;
176
177 return 1;
178
179 fail_wevt:
180 free(fd_evts[DIR_RD]);
181 fail_revt:
182 free(kev);
183 fail_kev:
184 close(kqueue_fd);
185 kqueue_fd = 0;
186 fail_fd:
187 p->pref = 0;
188 return 0;
189}
190
191/*
192 * Termination of the kqueue() poller.
193 * Memory is released and the poller is marked as unselectable.
194 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200195REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200196{
197 if (fd_evts[DIR_WR])
198 free(fd_evts[DIR_WR]);
199 if (fd_evts[DIR_RD])
200 free(fd_evts[DIR_RD]);
201 if (kev)
202 free(kev);
203 close(kqueue_fd);
204 kqueue_fd = 0;
205
206 p->private = NULL;
207 p->pref = 0;
208}
209
210/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200211 * Check that the poller works.
212 * Returns 1 if OK, otherwise 0.
213 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200214REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200215{
216 int fd;
217
218 fd = kqueue();
219 if (fd < 0)
220 return 0;
221 close(fd);
222 return 1;
223}
224
225/*
226 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
227 * otherwise 0. Note that some pollers need to be reopened after a fork()
228 * (such as kqueue), and some others may fail to do so in a chroot.
229 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200230REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200231{
232 close(kqueue_fd);
233 kqueue_fd = kqueue();
234 if (kqueue_fd < 0)
235 return 0;
236 return 1;
237}
238
239/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200240 * It is a constructor, which means that it will automatically be called before
241 * main(). This is GCC-specific but it works at least since 2.95.
242 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200243 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200244__attribute__((constructor))
245static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200246{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200247 struct poller *p;
248
249 if (nbpollers >= MAX_POLLERS)
250 return;
251 p = &pollers[nbpollers++];
252
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200253 p->name = "kqueue";
254 p->pref = 300;
255 p->private = NULL;
256
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200257 p->test = _do_test;
258 p->init = _do_init;
259 p->term = _do_term;
260 p->poll = _do_poll;
261 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200262
Willy Tarreau63455a92007-04-09 15:34:49 +0200263 p->is_set = __fd_is_set;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200264 p->cond_s = p->set = __fd_set;
265 p->cond_c = p->clr = __fd_clr;
Willy Tarreau40562cb2007-04-09 20:38:57 +0200266 p->rem = __fd_rem;
267 p->clo = __fd_clo;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200268}
269
270
271/*
272 * Local variables:
273 * c-indent-level: 8
274 * c-basic-offset: 8
275 * End:
276 */