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 | 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 | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 102 | REGPRM2 static void _do_poll(struct poller *p, int exp) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 103 | { |
| 104 | int status; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 105 | int count, fd, delta_ms; |
| 106 | struct timespec timeout; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 107 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 108 | delta_ms = 0; |
| 109 | timeout.tv_sec = 0; |
| 110 | timeout.tv_nsec = 0; |
Willy Tarreau | 79b8a62 | 2007-05-14 03:15:46 +0200 | [diff] [blame] | 111 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 112 | if (!run_queue) { |
| 113 | if (!exp) { |
| 114 | delta_ms = MAX_DELAY_MS; |
| 115 | timeout.tv_sec = (MAX_DELAY_MS / 1000); |
| 116 | timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 117 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 118 | else if (!tick_is_expired(exp, now_ms)) { |
| 119 | delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1; |
| 120 | if (delta_ms > MAX_DELAY_MS) |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 121 | delta_ms = MAX_DELAY_MS; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 122 | timeout.tv_sec = (delta_ms / 1000); |
| 123 | timeout.tv_nsec = (delta_ms % 1000) * 1000000; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 124 | } |
Willy Tarreau | cd5ce2a | 2007-04-09 16:25:46 +0200 | [diff] [blame] | 125 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 126 | |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 127 | fd = MIN(maxfd, global.tune.maxpollevents); |
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 | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 135 | |
| 136 | for (count = 0; count < status; count++) { |
| 137 | fd = kev[count].ident; |
| 138 | if (kev[count].filter == EVFILT_READ) { |
| 139 | if (FD_ISSET(fd, fd_evts[DIR_RD])) { |
| 140 | if (fdtab[fd].state == FD_STCLOSE) |
| 141 | continue; |
| 142 | fdtab[fd].cb[DIR_RD].f(fd); |
| 143 | } |
| 144 | } else if (kev[count].filter == EVFILT_WRITE) { |
| 145 | if (FD_ISSET(fd, fd_evts[DIR_WR])) { |
| 146 | if (fdtab[fd].state == FD_STCLOSE) |
| 147 | continue; |
| 148 | fdtab[fd].cb[DIR_WR].f(fd); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Initialization of the kqueue() poller. |
| 156 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 157 | * disables the poller by setting its pref to 0. |
| 158 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 159 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 160 | { |
| 161 | __label__ fail_wevt, fail_revt, fail_fd; |
| 162 | int fd_set_bytes; |
| 163 | |
| 164 | p->private = NULL; |
| 165 | fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE; |
| 166 | |
| 167 | kqueue_fd = kqueue(); |
| 168 | if (kqueue_fd < 0) |
| 169 | goto fail_fd; |
| 170 | |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 171 | kev = (struct kevent*)calloc(1, sizeof(struct kevent) * global.tune.maxpollevents); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 172 | |
| 173 | if (kev == NULL) |
| 174 | goto fail_kev; |
| 175 | |
| 176 | if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 177 | goto fail_revt; |
| 178 | |
| 179 | if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 180 | goto fail_wevt; |
| 181 | |
| 182 | return 1; |
| 183 | |
| 184 | fail_wevt: |
| 185 | free(fd_evts[DIR_RD]); |
| 186 | fail_revt: |
| 187 | free(kev); |
| 188 | fail_kev: |
| 189 | close(kqueue_fd); |
| 190 | kqueue_fd = 0; |
| 191 | fail_fd: |
| 192 | p->pref = 0; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Termination of the kqueue() poller. |
| 198 | * Memory is released and the poller is marked as unselectable. |
| 199 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 200 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 201 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 202 | free(fd_evts[DIR_WR]); |
| 203 | free(fd_evts[DIR_RD]); |
| 204 | free(kev); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 205 | close(kqueue_fd); |
| 206 | kqueue_fd = 0; |
| 207 | |
| 208 | p->private = NULL; |
| 209 | p->pref = 0; |
| 210 | } |
| 211 | |
| 212 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 213 | * Check that the poller works. |
| 214 | * Returns 1 if OK, otherwise 0. |
| 215 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 216 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 217 | { |
| 218 | int fd; |
| 219 | |
| 220 | fd = kqueue(); |
| 221 | if (fd < 0) |
| 222 | return 0; |
| 223 | close(fd); |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK, |
| 229 | * otherwise 0. Note that some pollers need to be reopened after a fork() |
| 230 | * (such as kqueue), and some others may fail to do so in a chroot. |
| 231 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 232 | REGPRM1 static int _do_fork(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 233 | { |
| 234 | close(kqueue_fd); |
| 235 | kqueue_fd = kqueue(); |
| 236 | if (kqueue_fd < 0) |
| 237 | return 0; |
| 238 | return 1; |
| 239 | } |
| 240 | |
| 241 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 242 | * It is a constructor, which means that it will automatically be called before |
| 243 | * main(). This is GCC-specific but it works at least since 2.95. |
| 244 | * 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] | 245 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 246 | __attribute__((constructor)) |
| 247 | static void _do_register(void) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 248 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 249 | struct poller *p; |
| 250 | |
| 251 | if (nbpollers >= MAX_POLLERS) |
| 252 | return; |
| 253 | p = &pollers[nbpollers++]; |
| 254 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 255 | p->name = "kqueue"; |
| 256 | p->pref = 300; |
| 257 | p->private = NULL; |
| 258 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 259 | p->test = _do_test; |
| 260 | p->init = _do_init; |
| 261 | p->term = _do_term; |
| 262 | p->poll = _do_poll; |
| 263 | p->fork = _do_fork; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 264 | |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 265 | p->is_set = __fd_is_set; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 266 | p->cond_s = p->set = __fd_set; |
| 267 | p->cond_c = p->clr = __fd_clr; |
Willy Tarreau | 40562cb | 2007-04-09 20:38:57 +0200 | [diff] [blame] | 268 | p->rem = __fd_rem; |
| 269 | p->clo = __fd_clo; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | |
| 273 | /* |
| 274 | * Local variables: |
| 275 | * c-indent-level: 8 |
| 276 | * c-basic-offset: 8 |
| 277 | * End: |
| 278 | */ |