blob: 8740217b854e945e9e763e08dbd07fe97fb8766b [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>
27
28#include <types/fd.h>
29#include <types/global.h>
30
31#include <proto/fd.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020032#include <proto/task.h>
33
34/* private data */
35static fd_set *fd_evts[2];
36static int kqueue_fd;
37static struct kevent *kev = NULL;
38
39/* speeds up conversion of DIR_RD/DIR_WR to EVFILT* */
40static const int dir2filt[2] = { EVFILT_READ, EVFILT_WRITE };
41
42/* completes a change list for deletion */
43REGPRM3 static int kqev_del(struct kevent *kev, const int fd, const int dir)
44{
45 if (FD_ISSET(fd, fd_evts[dir])) {
46 FD_CLR(fd, fd_evts[dir]);
47 EV_SET(kev, fd, dir2filt[dir], EV_DELETE, 0, 0, NULL);
48 return 1;
49 }
50 return 0;
51}
52
53/*
54 * Returns non-zero if direction <dir> is already set for <fd>.
55 */
Willy Tarreau63455a92007-04-09 15:34:49 +020056REGPRM2 static int __fd_is_set(const int fd, int dir)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020057{
58 return FD_ISSET(fd, fd_evts[dir]);
59}
60
61REGPRM2 static int __fd_set(const int fd, int dir)
62{
63 /* if the value was set, do nothing */
64 if (FD_ISSET(fd, fd_evts[dir]))
65 return 0;
66
67 FD_SET(fd, fd_evts[dir]);
Willy Tarreau87552852007-04-09 17:16:07 +020068 EV_SET(kev, fd, dir2filt[dir], EV_ADD, 0, 0, NULL);
Willy Tarreau1e63130a2007-04-09 12:03:06 +020069 kevent(kqueue_fd, kev, 1, NULL, 0, NULL);
70 return 1;
71}
72
73REGPRM2 static int __fd_clr(const int fd, int dir)
74{
Willy Tarreau1e63130a2007-04-09 12:03:06 +020075 if (!kqev_del(kev, fd, dir))
76 return 0;
77 kevent(kqueue_fd, kev, 1, NULL, 0, NULL);
78 return 1;
79}
80
81REGPRM1 static void __fd_rem(int fd)
82{
83 int changes = 0;
84
Willy Tarreau1e63130a2007-04-09 12:03:06 +020085 changes += kqev_del(&kev[changes], fd, DIR_RD);
86 changes += kqev_del(&kev[changes], fd, DIR_WR);
87
88 if (changes)
89 kevent(kqueue_fd, kev, changes, NULL, 0, NULL);
90}
91
Willy Tarreau87552852007-04-09 17:16:07 +020092REGPRM1 static void __fd_clo(int fd)
93{
94 FD_CLR(fd, fd_evts[DIR_RD]);
95 FD_CLR(fd, fd_evts[DIR_WR]);
96}
97
Willy Tarreau1e63130a2007-04-09 12:03:06 +020098/*
99 * kqueue() poller
100 */
Willy Tarreau79b8a622007-05-14 03:15:46 +0200101REGPRM2 static void _do_poll(struct poller *p, struct timeval *exp)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200102{
103 int status;
104 int count, fd;
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200105 struct timespec timeout, *to_ptr;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200106
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200107 to_ptr = NULL; // no timeout
Willy Tarreau79b8a622007-05-14 03:15:46 +0200108 if (tv_isset(exp)) {
109 struct timeval delta;
110
111 if (tv_isge(&now, exp))
112 delta.tv_sec = delta.tv_usec = 0;
113 else
114 tv_remain(&now, exp, &delta);
115
116 timeout.tv_sec = delta.tv_sec;
117 timeout.tv_nsec = delta.tv_usec * 1000;
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200118 to_ptr = &timeout;
119 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200120
121 status = kevent(kqueue_fd, // int kq
122 NULL, // const struct kevent *changelist
123 0, // int nchanges
124 kev, // struct kevent *eventlist
125 maxfd, // int nevents
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200126 to_ptr); // const struct timespec *timeout
Willy Tarreau258696f2007-04-10 02:31:54 +0200127 tv_now(&now);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200128
129 for (count = 0; count < status; count++) {
130 fd = kev[count].ident;
131 if (kev[count].filter == EVFILT_READ) {
132 if (FD_ISSET(fd, fd_evts[DIR_RD])) {
133 if (fdtab[fd].state == FD_STCLOSE)
134 continue;
135 fdtab[fd].cb[DIR_RD].f(fd);
136 }
137 } else if (kev[count].filter == EVFILT_WRITE) {
138 if (FD_ISSET(fd, fd_evts[DIR_WR])) {
139 if (fdtab[fd].state == FD_STCLOSE)
140 continue;
141 fdtab[fd].cb[DIR_WR].f(fd);
142 }
143 }
144 }
145}
146
147/*
148 * Initialization of the kqueue() poller.
149 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
150 * disables the poller by setting its pref to 0.
151 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200152REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200153{
154 __label__ fail_wevt, fail_revt, fail_fd;
155 int fd_set_bytes;
156
157 p->private = NULL;
158 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
159
160 kqueue_fd = kqueue();
161 if (kqueue_fd < 0)
162 goto fail_fd;
163
164 kev = (struct kevent*)calloc(1, sizeof(struct kevent) * global.maxsock);
165
166 if (kev == NULL)
167 goto fail_kev;
168
169 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
170 goto fail_revt;
171
172 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
173 goto fail_wevt;
174
175 return 1;
176
177 fail_wevt:
178 free(fd_evts[DIR_RD]);
179 fail_revt:
180 free(kev);
181 fail_kev:
182 close(kqueue_fd);
183 kqueue_fd = 0;
184 fail_fd:
185 p->pref = 0;
186 return 0;
187}
188
189/*
190 * Termination of the kqueue() poller.
191 * Memory is released and the poller is marked as unselectable.
192 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200193REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200194{
195 if (fd_evts[DIR_WR])
196 free(fd_evts[DIR_WR]);
197 if (fd_evts[DIR_RD])
198 free(fd_evts[DIR_RD]);
199 if (kev)
200 free(kev);
201 close(kqueue_fd);
202 kqueue_fd = 0;
203
204 p->private = NULL;
205 p->pref = 0;
206}
207
208/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200209 * Check that the poller works.
210 * Returns 1 if OK, otherwise 0.
211 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200212REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200213{
214 int fd;
215
216 fd = kqueue();
217 if (fd < 0)
218 return 0;
219 close(fd);
220 return 1;
221}
222
223/*
224 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
225 * otherwise 0. Note that some pollers need to be reopened after a fork()
226 * (such as kqueue), and some others may fail to do so in a chroot.
227 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200228REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200229{
230 close(kqueue_fd);
231 kqueue_fd = kqueue();
232 if (kqueue_fd < 0)
233 return 0;
234 return 1;
235}
236
237/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200238 * It is a constructor, which means that it will automatically be called before
239 * main(). This is GCC-specific but it works at least since 2.95.
240 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200241 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200242__attribute__((constructor))
243static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200244{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200245 struct poller *p;
246
247 if (nbpollers >= MAX_POLLERS)
248 return;
249 p = &pollers[nbpollers++];
250
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200251 p->name = "kqueue";
252 p->pref = 300;
253 p->private = NULL;
254
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200255 p->test = _do_test;
256 p->init = _do_init;
257 p->term = _do_term;
258 p->poll = _do_poll;
259 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200260
Willy Tarreau63455a92007-04-09 15:34:49 +0200261 p->is_set = __fd_is_set;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200262 p->cond_s = p->set = __fd_set;
263 p->cond_c = p->clr = __fd_clr;
Willy Tarreau40562cb2007-04-09 20:38:57 +0200264 p->rem = __fd_rem;
265 p->clo = __fd_clo;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200266}
267
268
269/*
270 * Local variables:
271 * c-indent-level: 8
272 * c-basic-offset: 8
273 * End:
274 */