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