blob: 71e9ecf614fd5c2ac922fbc333898888e3d63748 [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>
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;
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 Tarreau3a628112008-06-13 21:06:56 +0200108 if (run_queue) {
109 timeout.tv_sec = timeout.tv_nsec = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200110 delta_ms = 0;
Willy Tarreau3a628112008-06-13 21:06:56 +0200111 }
112 else if (tv_isset(exp)) {
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200113 const struct timeval max_delay = {
114 .tv_sec = MAX_DELAY_MS / 1000,
115 .tv_usec = (MAX_DELAY_MS % 1000) * 1000
116 };
Willy Tarreau79b8a622007-05-14 03:15:46 +0200117 struct timeval delta;
118
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200119 if (tv_isge(&now, exp)) {
Willy Tarreau79b8a622007-05-14 03:15:46 +0200120 delta.tv_sec = delta.tv_usec = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200121 delta_ms = 0;
122 }
123 else {
Willy Tarreau79b8a622007-05-14 03:15:46 +0200124 tv_remain(&now, exp, &delta);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200125 if (__tv_isgt(&delta, &max_delay)) {
126 delta = max_delay;
127 delta_ms = MAX_DELAY_MS;
128 } else {
129 delta_ms = delta.tv_sec * 1000 + delta.tv_usec / 1000;
130 }
131 }
Willy Tarreau79b8a622007-05-14 03:15:46 +0200132
133 timeout.tv_sec = delta.tv_sec;
134 timeout.tv_nsec = delta.tv_usec * 1000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200135 }
136 else {
137 delta_ms = MAX_DELAY_MS;
138 timeout.tv_sec = MAX_DELAY_MS / 1000;
139 timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000;
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200140 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200141
Willy Tarreau1db37712007-06-03 17:16:49 +0200142 fd = MIN(maxfd, global.tune.maxpollevents);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200143 status = kevent(kqueue_fd, // int kq
144 NULL, // const struct kevent *changelist
145 0, // int nchanges
146 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200147 fd, // int nevents
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200148 &timeout); // const struct timespec *timeout
149 tv_update_date(delta_ms, status);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200150
151 for (count = 0; count < status; count++) {
152 fd = kev[count].ident;
153 if (kev[count].filter == EVFILT_READ) {
154 if (FD_ISSET(fd, fd_evts[DIR_RD])) {
155 if (fdtab[fd].state == FD_STCLOSE)
156 continue;
157 fdtab[fd].cb[DIR_RD].f(fd);
158 }
159 } else if (kev[count].filter == EVFILT_WRITE) {
160 if (FD_ISSET(fd, fd_evts[DIR_WR])) {
161 if (fdtab[fd].state == FD_STCLOSE)
162 continue;
163 fdtab[fd].cb[DIR_WR].f(fd);
164 }
165 }
166 }
167}
168
169/*
170 * Initialization of the kqueue() poller.
171 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
172 * disables the poller by setting its pref to 0.
173 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200174REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200175{
176 __label__ fail_wevt, fail_revt, fail_fd;
177 int fd_set_bytes;
178
179 p->private = NULL;
180 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
181
182 kqueue_fd = kqueue();
183 if (kqueue_fd < 0)
184 goto fail_fd;
185
Willy Tarreau1db37712007-06-03 17:16:49 +0200186 kev = (struct kevent*)calloc(1, sizeof(struct kevent) * global.tune.maxpollevents);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200187
188 if (kev == NULL)
189 goto fail_kev;
190
191 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
192 goto fail_revt;
193
194 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
195 goto fail_wevt;
196
197 return 1;
198
199 fail_wevt:
200 free(fd_evts[DIR_RD]);
201 fail_revt:
202 free(kev);
203 fail_kev:
204 close(kqueue_fd);
205 kqueue_fd = 0;
206 fail_fd:
207 p->pref = 0;
208 return 0;
209}
210
211/*
212 * Termination of the kqueue() poller.
213 * Memory is released and the poller is marked as unselectable.
214 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200215REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200216{
217 if (fd_evts[DIR_WR])
218 free(fd_evts[DIR_WR]);
219 if (fd_evts[DIR_RD])
220 free(fd_evts[DIR_RD]);
221 if (kev)
222 free(kev);
223 close(kqueue_fd);
224 kqueue_fd = 0;
225
226 p->private = NULL;
227 p->pref = 0;
228}
229
230/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200231 * Check that the poller works.
232 * Returns 1 if OK, otherwise 0.
233 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200234REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200235{
236 int fd;
237
238 fd = kqueue();
239 if (fd < 0)
240 return 0;
241 close(fd);
242 return 1;
243}
244
245/*
246 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
247 * otherwise 0. Note that some pollers need to be reopened after a fork()
248 * (such as kqueue), and some others may fail to do so in a chroot.
249 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200250REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200251{
252 close(kqueue_fd);
253 kqueue_fd = kqueue();
254 if (kqueue_fd < 0)
255 return 0;
256 return 1;
257}
258
259/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200260 * It is a constructor, which means that it will automatically be called before
261 * main(). This is GCC-specific but it works at least since 2.95.
262 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200263 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200264__attribute__((constructor))
265static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200266{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200267 struct poller *p;
268
269 if (nbpollers >= MAX_POLLERS)
270 return;
271 p = &pollers[nbpollers++];
272
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200273 p->name = "kqueue";
274 p->pref = 300;
275 p->private = NULL;
276
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200277 p->test = _do_test;
278 p->init = _do_init;
279 p->term = _do_term;
280 p->poll = _do_poll;
281 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200282
Willy Tarreau63455a92007-04-09 15:34:49 +0200283 p->is_set = __fd_is_set;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200284 p->cond_s = p->set = __fd_set;
285 p->cond_c = p->clr = __fd_clr;
Willy Tarreau40562cb2007-04-09 20:38:57 +0200286 p->rem = __fd_rem;
287 p->clo = __fd_clo;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200288}
289
290
291/*
292 * Local variables:
293 * c-indent-level: 8
294 * c-basic-offset: 8
295 * End:
296 */