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