blob: f34cdc3c1bcb33b53fafee5a12990adb0a08b9d0 [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 Tarreau3a628112008-06-13 21:06:56 +0200109 if (run_queue) {
110 timeout.tv_sec = timeout.tv_nsec = 0;
111 to_ptr = &timeout;
112 }
113 else if (tv_isset(exp)) {
Willy Tarreau79b8a622007-05-14 03:15:46 +0200114 struct timeval delta;
115
116 if (tv_isge(&now, exp))
117 delta.tv_sec = delta.tv_usec = 0;
118 else
119 tv_remain(&now, exp, &delta);
120
121 timeout.tv_sec = delta.tv_sec;
122 timeout.tv_nsec = delta.tv_usec * 1000;
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200123 to_ptr = &timeout;
124 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200125
Willy Tarreau1db37712007-06-03 17:16:49 +0200126 fd = MIN(maxfd, global.tune.maxpollevents);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200127 status = kevent(kqueue_fd, // int kq
128 NULL, // const struct kevent *changelist
129 0, // int nchanges
130 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200131 fd, // int nevents
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200132 to_ptr); // const struct timespec *timeout
Willy Tarreau258696f2007-04-10 02:31:54 +0200133 tv_now(&now);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200134
135 for (count = 0; count < status; count++) {
136 fd = kev[count].ident;
137 if (kev[count].filter == EVFILT_READ) {
138 if (FD_ISSET(fd, fd_evts[DIR_RD])) {
139 if (fdtab[fd].state == FD_STCLOSE)
140 continue;
141 fdtab[fd].cb[DIR_RD].f(fd);
142 }
143 } else if (kev[count].filter == EVFILT_WRITE) {
144 if (FD_ISSET(fd, fd_evts[DIR_WR])) {
145 if (fdtab[fd].state == FD_STCLOSE)
146 continue;
147 fdtab[fd].cb[DIR_WR].f(fd);
148 }
149 }
150 }
151}
152
153/*
154 * Initialization of the kqueue() poller.
155 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
156 * disables the poller by setting its pref to 0.
157 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200158REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200159{
160 __label__ fail_wevt, fail_revt, fail_fd;
161 int fd_set_bytes;
162
163 p->private = NULL;
164 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
165
166 kqueue_fd = kqueue();
167 if (kqueue_fd < 0)
168 goto fail_fd;
169
Willy Tarreau1db37712007-06-03 17:16:49 +0200170 kev = (struct kevent*)calloc(1, sizeof(struct kevent) * global.tune.maxpollevents);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200171
172 if (kev == NULL)
173 goto fail_kev;
174
175 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
176 goto fail_revt;
177
178 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
179 goto fail_wevt;
180
181 return 1;
182
183 fail_wevt:
184 free(fd_evts[DIR_RD]);
185 fail_revt:
186 free(kev);
187 fail_kev:
188 close(kqueue_fd);
189 kqueue_fd = 0;
190 fail_fd:
191 p->pref = 0;
192 return 0;
193}
194
195/*
196 * Termination of the kqueue() poller.
197 * Memory is released and the poller is marked as unselectable.
198 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200199REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200200{
201 if (fd_evts[DIR_WR])
202 free(fd_evts[DIR_WR]);
203 if (fd_evts[DIR_RD])
204 free(fd_evts[DIR_RD]);
205 if (kev)
206 free(kev);
207 close(kqueue_fd);
208 kqueue_fd = 0;
209
210 p->private = NULL;
211 p->pref = 0;
212}
213
214/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200215 * Check that the poller works.
216 * Returns 1 if OK, otherwise 0.
217 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200218REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200219{
220 int fd;
221
222 fd = kqueue();
223 if (fd < 0)
224 return 0;
225 close(fd);
226 return 1;
227}
228
229/*
230 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
231 * otherwise 0. Note that some pollers need to be reopened after a fork()
232 * (such as kqueue), and some others may fail to do so in a chroot.
233 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200234REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200235{
236 close(kqueue_fd);
237 kqueue_fd = kqueue();
238 if (kqueue_fd < 0)
239 return 0;
240 return 1;
241}
242
243/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200244 * It is a constructor, which means that it will automatically be called before
245 * main(). This is GCC-specific but it works at least since 2.95.
246 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200247 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200248__attribute__((constructor))
249static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200250{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200251 struct poller *p;
252
253 if (nbpollers >= MAX_POLLERS)
254 return;
255 p = &pollers[nbpollers++];
256
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200257 p->name = "kqueue";
258 p->pref = 300;
259 p->private = NULL;
260
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200261 p->test = _do_test;
262 p->init = _do_init;
263 p->term = _do_term;
264 p->poll = _do_poll;
265 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200266
Willy Tarreau63455a92007-04-09 15:34:49 +0200267 p->is_set = __fd_is_set;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200268 p->cond_s = p->set = __fd_set;
269 p->cond_c = p->clr = __fd_clr;
Willy Tarreau40562cb2007-04-09 20:38:57 +0200270 p->rem = __fd_rem;
271 p->clo = __fd_clo;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200272}
273
274
275/*
276 * Local variables:
277 * c-indent-level: 8
278 * c-basic-offset: 8
279 * End:
280 */