Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * FD polling functions for FreeBSD kqueue() |
| 3 | * |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 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> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 26 | #include <common/ticks.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 27 | #include <common/time.h> |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 28 | #include <common/tools.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 30 | #include <types/global.h> |
| 31 | |
| 32 | #include <proto/fd.h> |
Willy Tarreau | 332740d | 2009-05-10 09:57:21 +0200 | [diff] [blame] | 33 | #include <proto/signal.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 34 | #include <proto/task.h> |
| 35 | |
| 36 | /* private data */ |
| 37 | static fd_set *fd_evts[2]; |
| 38 | static int kqueue_fd; |
| 39 | static struct kevent *kev = NULL; |
| 40 | |
| 41 | /* speeds up conversion of DIR_RD/DIR_WR to EVFILT* */ |
| 42 | static const int dir2filt[2] = { EVFILT_READ, EVFILT_WRITE }; |
| 43 | |
| 44 | /* completes a change list for deletion */ |
| 45 | REGPRM3 static int kqev_del(struct kevent *kev, const int fd, const int dir) |
| 46 | { |
| 47 | if (FD_ISSET(fd, fd_evts[dir])) { |
| 48 | FD_CLR(fd, fd_evts[dir]); |
| 49 | EV_SET(kev, fd, dir2filt[dir], EV_DELETE, 0, 0, NULL); |
| 50 | return 1; |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Returns non-zero if direction <dir> is already set for <fd>. |
| 57 | */ |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 58 | REGPRM2 static int __fd_is_set(const int fd, int dir) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 59 | { |
| 60 | return FD_ISSET(fd, fd_evts[dir]); |
| 61 | } |
| 62 | |
Willy Tarreau | 3788e4c | 2012-07-30 14:29:35 +0200 | [diff] [blame] | 63 | REGPRM2 static void __fd_set(const int fd, int dir) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 64 | { |
| 65 | /* if the value was set, do nothing */ |
| 66 | if (FD_ISSET(fd, fd_evts[dir])) |
Willy Tarreau | 3788e4c | 2012-07-30 14:29:35 +0200 | [diff] [blame] | 67 | return; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 68 | |
| 69 | FD_SET(fd, fd_evts[dir]); |
Willy Tarreau | 8755285 | 2007-04-09 17:16:07 +0200 | [diff] [blame] | 70 | EV_SET(kev, fd, dir2filt[dir], EV_ADD, 0, 0, NULL); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 71 | kevent(kqueue_fd, kev, 1, NULL, 0, NULL); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Willy Tarreau | 3788e4c | 2012-07-30 14:29:35 +0200 | [diff] [blame] | 74 | REGPRM2 static void __fd_clr(const int fd, int dir) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 75 | { |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 76 | if (!kqev_del(kev, fd, dir)) |
Willy Tarreau | 3788e4c | 2012-07-30 14:29:35 +0200 | [diff] [blame] | 77 | return; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 78 | kevent(kqueue_fd, kev, 1, NULL, 0, NULL); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 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 | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 101 | REGPRM2 static void _do_poll(struct poller *p, int exp) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 102 | { |
| 103 | int status; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 104 | int count, fd, delta_ms; |
| 105 | struct timespec timeout; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 106 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 107 | delta_ms = 0; |
| 108 | timeout.tv_sec = 0; |
| 109 | timeout.tv_nsec = 0; |
Willy Tarreau | 79b8a62 | 2007-05-14 03:15:46 +0200 | [diff] [blame] | 110 | |
Willy Tarreau | 332740d | 2009-05-10 09:57:21 +0200 | [diff] [blame] | 111 | if (!run_queue && !signal_queue_len) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 112 | if (!exp) { |
| 113 | delta_ms = MAX_DELAY_MS; |
| 114 | timeout.tv_sec = (MAX_DELAY_MS / 1000); |
| 115 | timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 116 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 117 | else if (!tick_is_expired(exp, now_ms)) { |
| 118 | delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1; |
| 119 | if (delta_ms > MAX_DELAY_MS) |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 120 | delta_ms = MAX_DELAY_MS; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 121 | timeout.tv_sec = (delta_ms / 1000); |
| 122 | timeout.tv_nsec = (delta_ms % 1000) * 1000000; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 123 | } |
Willy Tarreau | cd5ce2a | 2007-04-09 16:25:46 +0200 | [diff] [blame] | 124 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 125 | |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 126 | fd = MIN(maxfd, global.tune.maxpollevents); |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 127 | gettimeofday(&before_poll, NULL); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 128 | status = kevent(kqueue_fd, // int kq |
| 129 | NULL, // const struct kevent *changelist |
| 130 | 0, // int nchanges |
| 131 | kev, // struct kevent *eventlist |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 132 | fd, // int nevents |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 133 | &timeout); // const struct timespec *timeout |
| 134 | tv_update_date(delta_ms, status); |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 135 | measure_idle(); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 136 | |
| 137 | for (count = 0; count < status; count++) { |
| 138 | fd = kev[count].ident; |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 139 | |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 140 | if (!fdtab[fd].owner) |
| 141 | continue; |
| 142 | |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 143 | fdtab[fd].ev &= FD_POLL_STICKY; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 144 | if (kev[count].filter == EVFILT_READ) { |
| 145 | if (FD_ISSET(fd, fd_evts[DIR_RD])) { |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 146 | fdtab[fd].ev |= FD_POLL_IN; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 147 | } |
| 148 | } else if (kev[count].filter == EVFILT_WRITE) { |
| 149 | if (FD_ISSET(fd, fd_evts[DIR_WR])) { |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 150 | fdtab[fd].ev |= FD_POLL_OUT; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 151 | } |
| 152 | } |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 153 | if (fdtab[fd].iocb && fdtab[fd].ev) |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 154 | fdtab[fd].iocb(fd); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Initialization of the kqueue() poller. |
| 160 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 161 | * disables the poller by setting its pref to 0. |
| 162 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 163 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 164 | { |
| 165 | __label__ fail_wevt, fail_revt, fail_fd; |
| 166 | int fd_set_bytes; |
| 167 | |
| 168 | p->private = NULL; |
| 169 | fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE; |
| 170 | |
| 171 | kqueue_fd = kqueue(); |
| 172 | if (kqueue_fd < 0) |
| 173 | goto fail_fd; |
| 174 | |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 175 | kev = (struct kevent*)calloc(1, sizeof(struct kevent) * global.tune.maxpollevents); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 176 | |
| 177 | if (kev == NULL) |
| 178 | goto fail_kev; |
| 179 | |
| 180 | if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 181 | goto fail_revt; |
| 182 | |
| 183 | if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 184 | goto fail_wevt; |
| 185 | |
| 186 | return 1; |
| 187 | |
| 188 | fail_wevt: |
| 189 | free(fd_evts[DIR_RD]); |
| 190 | fail_revt: |
| 191 | free(kev); |
| 192 | fail_kev: |
| 193 | close(kqueue_fd); |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 194 | kqueue_fd = -1; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 195 | fail_fd: |
| 196 | p->pref = 0; |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Termination of the kqueue() poller. |
| 202 | * Memory is released and the poller is marked as unselectable. |
| 203 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 204 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 205 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 206 | free(fd_evts[DIR_WR]); |
| 207 | free(fd_evts[DIR_RD]); |
| 208 | free(kev); |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 209 | |
| 210 | if (kqueue_fd >= 0) { |
| 211 | close(kqueue_fd); |
| 212 | kqueue_fd = -1; |
| 213 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 214 | |
| 215 | p->private = NULL; |
| 216 | p->pref = 0; |
| 217 | } |
| 218 | |
| 219 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 220 | * Check that the poller works. |
| 221 | * Returns 1 if OK, otherwise 0. |
| 222 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 223 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 224 | { |
| 225 | int fd; |
| 226 | |
| 227 | fd = kqueue(); |
| 228 | if (fd < 0) |
| 229 | return 0; |
| 230 | close(fd); |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK, |
| 236 | * otherwise 0. Note that some pollers need to be reopened after a fork() |
| 237 | * (such as kqueue), and some others may fail to do so in a chroot. |
| 238 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 239 | REGPRM1 static int _do_fork(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 240 | { |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 241 | if (kqueue_fd >= 0) |
| 242 | close(kqueue_fd); |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 243 | kqueue_fd = kqueue(); |
| 244 | if (kqueue_fd < 0) |
| 245 | return 0; |
| 246 | return 1; |
| 247 | } |
| 248 | |
| 249 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 250 | * It is a constructor, which means that it will automatically be called before |
| 251 | * main(). This is GCC-specific but it works at least since 2.95. |
| 252 | * 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] | 253 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 254 | __attribute__((constructor)) |
| 255 | static void _do_register(void) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 256 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 257 | struct poller *p; |
| 258 | |
| 259 | if (nbpollers >= MAX_POLLERS) |
| 260 | return; |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 261 | |
| 262 | kqueue_fd = -1; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 263 | p = &pollers[nbpollers++]; |
| 264 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 265 | p->name = "kqueue"; |
| 266 | p->pref = 300; |
| 267 | p->private = NULL; |
| 268 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 269 | p->test = _do_test; |
| 270 | p->init = _do_init; |
| 271 | p->term = _do_term; |
| 272 | p->poll = _do_poll; |
| 273 | p->fork = _do_fork; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 274 | |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 275 | p->is_set = __fd_is_set; |
Willy Tarreau | 3788e4c | 2012-07-30 14:29:35 +0200 | [diff] [blame] | 276 | p->set = __fd_set; |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 277 | p->wai = __fd_set; |
Willy Tarreau | 3788e4c | 2012-07-30 14:29:35 +0200 | [diff] [blame] | 278 | p->clr = __fd_clr; |
Willy Tarreau | 40562cb | 2007-04-09 20:38:57 +0200 | [diff] [blame] | 279 | p->rem = __fd_rem; |
| 280 | p->clo = __fd_clo; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
| 284 | /* |
| 285 | * Local variables: |
| 286 | * c-indent-level: 8 |
| 287 | * c-basic-offset: 8 |
| 288 | * End: |
| 289 | */ |