Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * FD polling functions for FreeBSD kqueue() |
| 3 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 4 | * Copyright 2000-2014 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 | * |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 13 | #include <unistd.h> |
| 14 | #include <sys/time.h> |
| 15 | #include <sys/types.h> |
| 16 | |
| 17 | #include <sys/event.h> |
| 18 | #include <sys/time.h> |
| 19 | |
| 20 | #include <common/compat.h> |
| 21 | #include <common/config.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 22 | #include <common/ticks.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 23 | #include <common/time.h> |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 24 | #include <common/tools.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 25 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 26 | #include <types/global.h> |
| 27 | |
| 28 | #include <proto/fd.h> |
Willy Tarreau | 10146c9 | 2015-04-13 20:44:19 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 30 | |
| 31 | /* private data */ |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 32 | static int kqueue_fd; |
| 33 | static struct kevent *kev = NULL; |
| 34 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 35 | /* |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 36 | * kqueue() poller |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 37 | */ |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 38 | REGPRM2 static void _do_poll(struct poller *p, int exp) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 39 | { |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 40 | int status; |
| 41 | int count, fd, delta_ms; |
| 42 | struct timespec timeout; |
| 43 | int updt_idx, en, eo; |
| 44 | int changes = 0; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 46 | /* first, scan the update list to find changes */ |
| 47 | for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) { |
| 48 | fd = fd_updt[updt_idx]; |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 49 | fdtab[fd].updated = 0; |
| 50 | fdtab[fd].new = 0; |
| 51 | |
| 52 | if (!fdtab[fd].owner) |
| 53 | continue; |
| 54 | |
Willy Tarreau | 25002d2 | 2014-01-25 10:32:56 +0100 | [diff] [blame] | 55 | eo = fdtab[fd].state; |
| 56 | en = fd_compute_new_polled_status(eo); |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 57 | |
| 58 | if ((eo ^ en) & FD_EV_POLLED_RW) { |
| 59 | /* poll status changed */ |
| 60 | fdtab[fd].state = en; |
| 61 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 62 | if ((eo ^ en) & FD_EV_POLLED_R) { |
| 63 | /* read poll status changed */ |
| 64 | if (en & FD_EV_POLLED_R) { |
| 65 | EV_SET(&kev[changes], fd, EVFILT_READ, EV_ADD, 0, 0, NULL); |
| 66 | changes++; |
| 67 | } |
| 68 | else { |
| 69 | EV_SET(&kev[changes], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); |
| 70 | changes++; |
| 71 | } |
| 72 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 73 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 74 | if ((eo ^ en) & FD_EV_POLLED_W) { |
| 75 | /* write poll status changed */ |
| 76 | if (en & FD_EV_POLLED_W) { |
| 77 | EV_SET(&kev[changes], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); |
| 78 | changes++; |
| 79 | } |
| 80 | else { |
| 81 | EV_SET(&kev[changes], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); |
| 82 | changes++; |
| 83 | } |
| 84 | } |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 85 | } |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 86 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 87 | if (changes) |
| 88 | kevent(kqueue_fd, kev, changes, NULL, 0, NULL); |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 89 | fd_nbupdt = 0; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 90 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 91 | delta_ms = 0; |
| 92 | timeout.tv_sec = 0; |
| 93 | timeout.tv_nsec = 0; |
Willy Tarreau | 79b8a62 | 2007-05-14 03:15:46 +0200 | [diff] [blame] | 94 | |
Willy Tarreau | 10146c9 | 2015-04-13 20:44:19 +0200 | [diff] [blame] | 95 | if (!exp) { |
| 96 | delta_ms = MAX_DELAY_MS; |
| 97 | timeout.tv_sec = (MAX_DELAY_MS / 1000); |
| 98 | timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000; |
| 99 | } |
| 100 | else if (!tick_is_expired(exp, now_ms)) { |
| 101 | delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1; |
| 102 | if (delta_ms > MAX_DELAY_MS) |
| 103 | delta_ms = MAX_DELAY_MS; |
| 104 | timeout.tv_sec = (delta_ms / 1000); |
| 105 | timeout.tv_nsec = (delta_ms % 1000) * 1000000; |
Willy Tarreau | cd5ce2a | 2007-04-09 16:25:46 +0200 | [diff] [blame] | 106 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 107 | |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 108 | fd = MIN(maxfd, global.tune.maxpollevents); |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 109 | gettimeofday(&before_poll, NULL); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 110 | status = kevent(kqueue_fd, // int kq |
| 111 | NULL, // const struct kevent *changelist |
| 112 | 0, // int nchanges |
| 113 | kev, // struct kevent *eventlist |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 114 | fd, // int nevents |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 115 | &timeout); // const struct timespec *timeout |
| 116 | tv_update_date(delta_ms, status); |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 117 | measure_idle(); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 118 | |
| 119 | for (count = 0; count < status; count++) { |
| 120 | fd = kev[count].ident; |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 121 | |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 122 | if (!fdtab[fd].owner) |
| 123 | continue; |
| 124 | |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 125 | fdtab[fd].ev &= FD_POLL_STICKY; |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 126 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 127 | if (kev[count].filter == EVFILT_READ) { |
Willy Tarreau | 69a41fa | 2014-01-20 11:02:59 +0100 | [diff] [blame] | 128 | if ((fdtab[fd].state & FD_EV_STATUS_R)) |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 129 | fdtab[fd].ev |= FD_POLL_IN; |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 130 | } |
| 131 | else if (kev[count].filter == EVFILT_WRITE) { |
Willy Tarreau | 69a41fa | 2014-01-20 11:02:59 +0100 | [diff] [blame] | 132 | if ((fdtab[fd].state & FD_EV_STATUS_W)) |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 133 | fdtab[fd].ev |= FD_POLL_OUT; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 134 | } |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 135 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 136 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) |
| 137 | fd_may_recv(fd); |
| 138 | |
| 139 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) |
| 140 | fd_may_send(fd); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Initialization of the kqueue() poller. |
| 146 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 147 | * disables the poller by setting its pref to 0. |
| 148 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 149 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 150 | { |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 151 | p->private = NULL; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 152 | |
| 153 | kqueue_fd = kqueue(); |
| 154 | if (kqueue_fd < 0) |
| 155 | goto fail_fd; |
| 156 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 157 | /* we can have up to two events per fd (*/ |
| 158 | kev = (struct kevent*)calloc(1, sizeof(struct kevent) * 2 * global.maxsock); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 159 | if (kev == NULL) |
| 160 | goto fail_kev; |
| 161 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 162 | return 1; |
| 163 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 164 | fail_kev: |
| 165 | close(kqueue_fd); |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 166 | kqueue_fd = -1; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 167 | fail_fd: |
| 168 | p->pref = 0; |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Termination of the kqueue() poller. |
| 174 | * Memory is released and the poller is marked as unselectable. |
| 175 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 176 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 177 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 178 | free(kev); |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 179 | |
| 180 | if (kqueue_fd >= 0) { |
| 181 | close(kqueue_fd); |
| 182 | kqueue_fd = -1; |
| 183 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 184 | |
| 185 | p->private = NULL; |
| 186 | p->pref = 0; |
| 187 | } |
| 188 | |
| 189 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 190 | * Check that the poller works. |
| 191 | * Returns 1 if OK, otherwise 0. |
| 192 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 193 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 194 | { |
| 195 | int fd; |
| 196 | |
| 197 | fd = kqueue(); |
| 198 | if (fd < 0) |
| 199 | return 0; |
| 200 | close(fd); |
| 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK, |
| 206 | * otherwise 0. Note that some pollers need to be reopened after a fork() |
| 207 | * (such as kqueue), and some others may fail to do so in a chroot. |
| 208 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 209 | REGPRM1 static int _do_fork(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 210 | { |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 211 | if (kqueue_fd >= 0) |
| 212 | close(kqueue_fd); |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 213 | kqueue_fd = kqueue(); |
| 214 | if (kqueue_fd < 0) |
| 215 | return 0; |
| 216 | return 1; |
| 217 | } |
| 218 | |
| 219 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 220 | * It is a constructor, which means that it will automatically be called before |
| 221 | * main(). This is GCC-specific but it works at least since 2.95. |
| 222 | * 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] | 223 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 224 | __attribute__((constructor)) |
| 225 | static void _do_register(void) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 226 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 227 | struct poller *p; |
| 228 | |
| 229 | if (nbpollers >= MAX_POLLERS) |
| 230 | return; |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 231 | |
| 232 | kqueue_fd = -1; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 233 | p = &pollers[nbpollers++]; |
| 234 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 235 | p->name = "kqueue"; |
| 236 | p->pref = 300; |
| 237 | p->private = NULL; |
| 238 | |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 239 | p->clo = NULL; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 240 | p->test = _do_test; |
| 241 | p->init = _do_init; |
| 242 | p->term = _do_term; |
| 243 | p->poll = _do_poll; |
| 244 | p->fork = _do_fork; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
| 248 | /* |
| 249 | * Local variables: |
| 250 | * c-indent-level: 8 |
| 251 | * c-basic-offset: 8 |
| 252 | * End: |
| 253 | */ |