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> |
| 27 | |
| 28 | #include <types/fd.h> |
| 29 | #include <types/global.h> |
| 30 | |
| 31 | #include <proto/fd.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 32 | #include <proto/task.h> |
| 33 | |
| 34 | /* private data */ |
| 35 | static fd_set *fd_evts[2]; |
| 36 | static int kqueue_fd; |
| 37 | static struct kevent *kev = NULL; |
| 38 | |
| 39 | /* speeds up conversion of DIR_RD/DIR_WR to EVFILT* */ |
| 40 | static const int dir2filt[2] = { EVFILT_READ, EVFILT_WRITE }; |
| 41 | |
| 42 | /* completes a change list for deletion */ |
| 43 | REGPRM3 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 Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 56 | REGPRM2 static int __fd_is_set(const int fd, int dir) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 57 | { |
| 58 | return FD_ISSET(fd, fd_evts[dir]); |
| 59 | } |
| 60 | |
| 61 | REGPRM2 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 Tarreau | 8755285 | 2007-04-09 17:16:07 +0200 | [diff] [blame] | 68 | EV_SET(kev, fd, dir2filt[dir], EV_ADD, 0, 0, NULL); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 69 | kevent(kqueue_fd, kev, 1, NULL, 0, NULL); |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | REGPRM2 static int __fd_clr(const int fd, int dir) |
| 74 | { |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 75 | if (!kqev_del(kev, fd, dir)) |
| 76 | return 0; |
| 77 | kevent(kqueue_fd, kev, 1, NULL, 0, NULL); |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | REGPRM1 static void __fd_rem(int fd) |
| 82 | { |
| 83 | int changes = 0; |
| 84 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 85 | 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 Tarreau | 8755285 | 2007-04-09 17:16:07 +0200 | [diff] [blame] | 92 | REGPRM1 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 Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 98 | /* |
| 99 | * kqueue() poller |
| 100 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 101 | REGPRM2 static void _do_poll(struct poller *p, int wait_time) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 102 | { |
| 103 | int status; |
| 104 | int count, fd; |
Willy Tarreau | cd5ce2a | 2007-04-09 16:25:46 +0200 | [diff] [blame] | 105 | struct timespec timeout, *to_ptr; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 106 | |
Willy Tarreau | cd5ce2a | 2007-04-09 16:25:46 +0200 | [diff] [blame] | 107 | 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 Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 113 | |
| 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 Tarreau | cd5ce2a | 2007-04-09 16:25:46 +0200 | [diff] [blame] | 119 | to_ptr); // const struct timespec *timeout |
Willy Tarreau | 258696f | 2007-04-10 02:31:54 +0200 | [diff] [blame] | 120 | tv_now(&now); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 121 | |
| 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 Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 145 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 146 | { |
| 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 Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 186 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 187 | { |
| 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 Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 202 | * Check that the poller works. |
| 203 | * Returns 1 if OK, otherwise 0. |
| 204 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 205 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 206 | { |
| 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 Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 221 | REGPRM1 static int _do_fork(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 222 | { |
| 223 | close(kqueue_fd); |
| 224 | kqueue_fd = kqueue(); |
| 225 | if (kqueue_fd < 0) |
| 226 | return 0; |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 231 | * 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 Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 234 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 235 | __attribute__((constructor)) |
| 236 | static void _do_register(void) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 237 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 238 | struct poller *p; |
| 239 | |
| 240 | if (nbpollers >= MAX_POLLERS) |
| 241 | return; |
| 242 | p = &pollers[nbpollers++]; |
| 243 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 244 | p->name = "kqueue"; |
| 245 | p->pref = 300; |
| 246 | p->private = NULL; |
| 247 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 248 | 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 Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 253 | |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 254 | p->is_set = __fd_is_set; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 255 | p->cond_s = p->set = __fd_set; |
| 256 | p->cond_c = p->clr = __fd_clr; |
Willy Tarreau | 40562cb | 2007-04-09 20:38:57 +0200 | [diff] [blame] | 257 | p->rem = __fd_rem; |
| 258 | p->clo = __fd_clo; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | /* |
| 263 | * Local variables: |
| 264 | * c-indent-level: 8 |
| 265 | * c-basic-offset: 8 |
| 266 | * End: |
| 267 | */ |