blob: 55419f6f02b3e385b62cacef894516c0bf510cbf [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 Tarreauef1d1f82007-04-16 00:25:25 +0200101REGPRM2 static void _do_poll(struct poller *p, int wait_time)
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
108 if (wait_time >= 0) {
109 timeout.tv_sec = wait_time / 1000;
110 timeout.tv_nsec = (wait_time % 1000) * 1000000;
111 to_ptr = &timeout;
112 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200113
114 status = kevent(kqueue_fd, // int kq
115 NULL, // const struct kevent *changelist
116 0, // int nchanges
117 kev, // struct kevent *eventlist
118 maxfd, // int nevents
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200119 to_ptr); // const struct timespec *timeout
Willy Tarreau258696f2007-04-10 02:31:54 +0200120 tv_now(&now);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200121
122 for (count = 0; count < status; count++) {
123 fd = kev[count].ident;
124 if (kev[count].filter == EVFILT_READ) {
125 if (FD_ISSET(fd, fd_evts[DIR_RD])) {
126 if (fdtab[fd].state == FD_STCLOSE)
127 continue;
128 fdtab[fd].cb[DIR_RD].f(fd);
129 }
130 } else if (kev[count].filter == EVFILT_WRITE) {
131 if (FD_ISSET(fd, fd_evts[DIR_WR])) {
132 if (fdtab[fd].state == FD_STCLOSE)
133 continue;
134 fdtab[fd].cb[DIR_WR].f(fd);
135 }
136 }
137 }
138}
139
140/*
141 * Initialization of the kqueue() poller.
142 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
143 * disables the poller by setting its pref to 0.
144 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200145REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200146{
147 __label__ fail_wevt, fail_revt, fail_fd;
148 int fd_set_bytes;
149
150 p->private = NULL;
151 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
152
153 kqueue_fd = kqueue();
154 if (kqueue_fd < 0)
155 goto fail_fd;
156
157 kev = (struct kevent*)calloc(1, sizeof(struct kevent) * global.maxsock);
158
159 if (kev == NULL)
160 goto fail_kev;
161
162 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
163 goto fail_revt;
164
165 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
166 goto fail_wevt;
167
168 return 1;
169
170 fail_wevt:
171 free(fd_evts[DIR_RD]);
172 fail_revt:
173 free(kev);
174 fail_kev:
175 close(kqueue_fd);
176 kqueue_fd = 0;
177 fail_fd:
178 p->pref = 0;
179 return 0;
180}
181
182/*
183 * Termination of the kqueue() poller.
184 * Memory is released and the poller is marked as unselectable.
185 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200186REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200187{
188 if (fd_evts[DIR_WR])
189 free(fd_evts[DIR_WR]);
190 if (fd_evts[DIR_RD])
191 free(fd_evts[DIR_RD]);
192 if (kev)
193 free(kev);
194 close(kqueue_fd);
195 kqueue_fd = 0;
196
197 p->private = NULL;
198 p->pref = 0;
199}
200
201/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200202 * Check that the poller works.
203 * Returns 1 if OK, otherwise 0.
204 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200205REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200206{
207 int fd;
208
209 fd = kqueue();
210 if (fd < 0)
211 return 0;
212 close(fd);
213 return 1;
214}
215
216/*
217 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
218 * otherwise 0. Note that some pollers need to be reopened after a fork()
219 * (such as kqueue), and some others may fail to do so in a chroot.
220 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200221REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200222{
223 close(kqueue_fd);
224 kqueue_fd = kqueue();
225 if (kqueue_fd < 0)
226 return 0;
227 return 1;
228}
229
230/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200231 * It is a constructor, which means that it will automatically be called before
232 * main(). This is GCC-specific but it works at least since 2.95.
233 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200234 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200235__attribute__((constructor))
236static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200237{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200238 struct poller *p;
239
240 if (nbpollers >= MAX_POLLERS)
241 return;
242 p = &pollers[nbpollers++];
243
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200244 p->name = "kqueue";
245 p->pref = 300;
246 p->private = NULL;
247
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200248 p->test = _do_test;
249 p->init = _do_init;
250 p->term = _do_term;
251 p->poll = _do_poll;
252 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200253
Willy Tarreau63455a92007-04-09 15:34:49 +0200254 p->is_set = __fd_is_set;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200255 p->cond_s = p->set = __fd_set;
256 p->cond_c = p->clr = __fd_clr;
Willy Tarreau40562cb2007-04-09 20:38:57 +0200257 p->rem = __fd_rem;
258 p->clo = __fd_clo;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200259}
260
261
262/*
263 * Local variables:
264 * c-indent-level: 8
265 * c-basic-offset: 8
266 * End:
267 */