Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 17 | #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 Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 27 | #include <common/tools.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 28 | |
| 29 | #include <types/fd.h> |
| 30 | #include <types/global.h> |
| 31 | |
| 32 | #include <proto/fd.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 33 | #include <proto/task.h> |
| 34 | |
| 35 | /* private data */ |
| 36 | static fd_set *fd_evts[2]; |
| 37 | static int kqueue_fd; |
| 38 | static struct kevent *kev = NULL; |
| 39 | |
| 40 | /* speeds up conversion of DIR_RD/DIR_WR to EVFILT* */ |
| 41 | static const int dir2filt[2] = { EVFILT_READ, EVFILT_WRITE }; |
| 42 | |
| 43 | /* completes a change list for deletion */ |
| 44 | REGPRM3 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 Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 57 | REGPRM2 static int __fd_is_set(const int fd, int dir) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 58 | { |
| 59 | return FD_ISSET(fd, fd_evts[dir]); |
| 60 | } |
| 61 | |
| 62 | REGPRM2 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 Tarreau | 8755285 | 2007-04-09 17:16:07 +0200 | [diff] [blame] | 69 | EV_SET(kev, fd, dir2filt[dir], EV_ADD, 0, 0, NULL); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 70 | kevent(kqueue_fd, kev, 1, NULL, 0, NULL); |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | REGPRM2 static int __fd_clr(const int fd, int dir) |
| 75 | { |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 76 | if (!kqev_del(kev, fd, dir)) |
| 77 | return 0; |
| 78 | kevent(kqueue_fd, kev, 1, NULL, 0, NULL); |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | REGPRM1 static void __fd_rem(int fd) |
| 83 | { |
| 84 | int changes = 0; |
| 85 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 86 | 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 Tarreau | 8755285 | 2007-04-09 17:16:07 +0200 | [diff] [blame] | 93 | REGPRM1 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 Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 99 | /* |
| 100 | * kqueue() poller |
| 101 | */ |
Willy Tarreau | 79b8a62 | 2007-05-14 03:15:46 +0200 | [diff] [blame] | 102 | REGPRM2 static void _do_poll(struct poller *p, struct timeval *exp) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 103 | { |
| 104 | int status; |
| 105 | int count, fd; |
Willy Tarreau | cd5ce2a | 2007-04-09 16:25:46 +0200 | [diff] [blame] | 106 | struct timespec timeout, *to_ptr; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 107 | |
Willy Tarreau | cd5ce2a | 2007-04-09 16:25:46 +0200 | [diff] [blame] | 108 | to_ptr = NULL; // no timeout |
Willy Tarreau | 79b8a62 | 2007-05-14 03:15:46 +0200 | [diff] [blame] | 109 | 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 Tarreau | cd5ce2a | 2007-04-09 16:25:46 +0200 | [diff] [blame] | 119 | to_ptr = &timeout; |
| 120 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 121 | |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 122 | fd = MIN(maxfd, global.tune.maxpollevents); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 123 | status = kevent(kqueue_fd, // int kq |
| 124 | NULL, // const struct kevent *changelist |
| 125 | 0, // int nchanges |
| 126 | kev, // struct kevent *eventlist |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 127 | fd, // int nevents |
Willy Tarreau | cd5ce2a | 2007-04-09 16:25:46 +0200 | [diff] [blame] | 128 | to_ptr); // const struct timespec *timeout |
Willy Tarreau | 258696f | 2007-04-10 02:31:54 +0200 | [diff] [blame] | 129 | tv_now(&now); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 130 | |
| 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 Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 154 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 155 | { |
| 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 Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 166 | kev = (struct kevent*)calloc(1, sizeof(struct kevent) * global.tune.maxpollevents); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 167 | |
| 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 Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 195 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 196 | { |
| 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 Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 211 | * Check that the poller works. |
| 212 | * Returns 1 if OK, otherwise 0. |
| 213 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 214 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 215 | { |
| 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 Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 230 | REGPRM1 static int _do_fork(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 231 | { |
| 232 | close(kqueue_fd); |
| 233 | kqueue_fd = kqueue(); |
| 234 | if (kqueue_fd < 0) |
| 235 | return 0; |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 240 | * 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 Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 243 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 244 | __attribute__((constructor)) |
| 245 | static void _do_register(void) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 246 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 247 | struct poller *p; |
| 248 | |
| 249 | if (nbpollers >= MAX_POLLERS) |
| 250 | return; |
| 251 | p = &pollers[nbpollers++]; |
| 252 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 253 | p->name = "kqueue"; |
| 254 | p->pref = 300; |
| 255 | p->private = NULL; |
| 256 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 257 | 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 Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 262 | |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 263 | p->is_set = __fd_is_set; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 264 | p->cond_s = p->set = __fd_set; |
| 265 | p->cond_c = p->clr = __fd_clr; |
Willy Tarreau | 40562cb | 2007-04-09 20:38:57 +0200 | [diff] [blame] | 266 | p->rem = __fd_rem; |
| 267 | p->clo = __fd_clo; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
| 271 | /* |
| 272 | * Local variables: |
| 273 | * c-indent-level: 8 |
| 274 | * c-basic-offset: 8 |
| 275 | * End: |
| 276 | */ |