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 | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 22 | #include <common/hathreads.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 23 | #include <common/ticks.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 24 | #include <common/time.h> |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 25 | #include <common/tools.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 27 | #include <types/global.h> |
| 28 | |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 29 | #include <proto/activity.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 30 | #include <proto/fd.h> |
Willy Tarreau | beb859a | 2018-11-22 18:07:59 +0100 | [diff] [blame] | 31 | #include <proto/signal.h> |
Willy Tarreau | 10146c9 | 2015-04-13 20:44:19 +0200 | [diff] [blame] | 32 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 33 | |
| 34 | /* private data */ |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 35 | static int kqueue_fd[MAX_THREADS]; // per-thread kqueue_fd |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 36 | static THREAD_LOCAL struct kevent *kev = NULL; |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 37 | static struct kevent *kev_out = NULL; // Trash buffer for kevent() to write the eventlist in |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 38 | |
PiBa-NL | c55b88e | 2018-05-10 01:01:28 +0200 | [diff] [blame] | 39 | static int _update_fd(int fd, int start) |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 40 | { |
| 41 | int en; |
PiBa-NL | c55b88e | 2018-05-10 01:01:28 +0200 | [diff] [blame] | 42 | int changes = start; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 43 | |
| 44 | en = fdtab[fd].state; |
| 45 | |
Willy Tarreau | 5bee3e2 | 2019-09-04 09:52:57 +0200 | [diff] [blame] | 46 | if (!(fdtab[fd].thread_mask & tid_bit) || !(en & FD_EV_ACTIVE_RW)) { |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 47 | if (!(polled_mask[fd].poll_recv & tid_bit) && |
| 48 | !(polled_mask[fd].poll_send & tid_bit)) { |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 49 | /* fd was not watched, it's still not */ |
Olivier Houchard | 5ab3394 | 2018-09-11 14:44:51 +0200 | [diff] [blame] | 50 | return changes; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 51 | } |
| 52 | /* fd totally removed from poll list */ |
| 53 | EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); |
| 54 | EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 55 | if (polled_mask[fd].poll_recv & tid_bit) |
| 56 | _HA_ATOMIC_AND(&polled_mask[fd].poll_recv, ~tid_bit); |
| 57 | if (polled_mask[fd].poll_send & tid_bit) |
| 58 | _HA_ATOMIC_AND(&polled_mask[fd].poll_send, ~tid_bit); |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 59 | } |
| 60 | else { |
| 61 | /* OK fd has to be monitored, it was either added or changed */ |
| 62 | |
Willy Tarreau | 5bee3e2 | 2019-09-04 09:52:57 +0200 | [diff] [blame] | 63 | if (en & FD_EV_ACTIVE_R) { |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 64 | if (!(polled_mask[fd].poll_recv & tid_bit)) { |
| 65 | EV_SET(&kev[changes++], fd, EVFILT_READ, EV_ADD, 0, 0, NULL); |
| 66 | _HA_ATOMIC_OR(&polled_mask[fd].poll_recv, tid_bit); |
| 67 | } |
| 68 | } |
| 69 | else if (polled_mask[fd].poll_recv & tid_bit) { |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 70 | EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 71 | HA_ATOMIC_AND(&polled_mask[fd].poll_recv, ~tid_bit); |
| 72 | } |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 73 | |
Willy Tarreau | 5bee3e2 | 2019-09-04 09:52:57 +0200 | [diff] [blame] | 74 | if (en & FD_EV_ACTIVE_W) { |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 75 | if (!(polled_mask[fd].poll_send & tid_bit)) { |
| 76 | EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); |
| 77 | _HA_ATOMIC_OR(&polled_mask[fd].poll_send, tid_bit); |
| 78 | } |
| 79 | } |
| 80 | else if (polled_mask[fd].poll_send & tid_bit) { |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 81 | EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 82 | _HA_ATOMIC_AND(&polled_mask[fd].poll_send, ~tid_bit); |
| 83 | } |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 84 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 85 | } |
| 86 | return changes; |
| 87 | } |
| 88 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 89 | /* |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 90 | * kqueue() poller |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 91 | */ |
Willy Tarreau | 2ae84e4 | 2019-05-28 16:44:05 +0200 | [diff] [blame] | 92 | REGPRM3 static void _do_poll(struct poller *p, int exp, int wake) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 93 | { |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 94 | int status; |
Willy Tarreau | beb859a | 2018-11-22 18:07:59 +0100 | [diff] [blame] | 95 | int count, fd, wait_time; |
| 96 | struct timespec timeout_ts; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 97 | int updt_idx; |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 98 | int changes = 0; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 99 | int old_fd; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 100 | |
Willy Tarreau | beb859a | 2018-11-22 18:07:59 +0100 | [diff] [blame] | 101 | timeout_ts.tv_sec = 0; |
| 102 | timeout_ts.tv_nsec = 0; |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 103 | /* first, scan the update list to find changes */ |
| 104 | for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) { |
| 105 | fd = fd_updt[updt_idx]; |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 106 | |
Olivier Houchard | cb6c927 | 2019-03-08 18:49:54 +0100 | [diff] [blame] | 107 | _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit); |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 108 | if (!fdtab[fd].owner) { |
| 109 | activity[tid].poll_drop++; |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 110 | continue; |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 111 | } |
PiBa-NL | c55b88e | 2018-05-10 01:01:28 +0200 | [diff] [blame] | 112 | changes = _update_fd(fd, changes); |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 113 | } |
| 114 | /* Scan the global update list */ |
| 115 | for (old_fd = fd = update_list.first; fd != -1; fd = fdtab[fd].update.next) { |
| 116 | if (fd == -2) { |
| 117 | fd = old_fd; |
| 118 | continue; |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 119 | } |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 120 | else if (fd <= -3) |
| 121 | fd = -fd -4; |
| 122 | if (fd == -1) |
| 123 | break; |
| 124 | if (fdtab[fd].update_mask & tid_bit) |
| 125 | done_update_polling(fd); |
| 126 | else |
| 127 | continue; |
| 128 | if (!fdtab[fd].owner) |
| 129 | continue; |
PiBa-NL | c55b88e | 2018-05-10 01:01:28 +0200 | [diff] [blame] | 130 | changes = _update_fd(fd, changes); |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 131 | } |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 132 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 133 | thread_harmless_now(); |
| 134 | |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 135 | if (changes) { |
| 136 | #ifdef EV_RECEIPT |
| 137 | kev[0].flags |= EV_RECEIPT; |
| 138 | #else |
| 139 | /* If EV_RECEIPT isn't defined, just add an invalid entry, |
| 140 | * so that we get an error and kevent() stops before scanning |
| 141 | * the kqueue. |
| 142 | */ |
| 143 | EV_SET(&kev[changes++], -1, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); |
| 144 | #endif |
Willy Tarreau | beb859a | 2018-11-22 18:07:59 +0100 | [diff] [blame] | 145 | kevent(kqueue_fd[tid], kev, changes, kev_out, changes, &timeout_ts); |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 146 | } |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 147 | fd_nbupdt = 0; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 148 | |
Willy Tarreau | f37ba94 | 2018-10-17 11:25:54 +0200 | [diff] [blame] | 149 | /* now let's wait for events */ |
Willy Tarreau | 2ae84e4 | 2019-05-28 16:44:05 +0200 | [diff] [blame] | 150 | wait_time = wake ? 0 : compute_poll_timeout(exp); |
Willy Tarreau | ce036bc | 2018-01-29 14:58:02 +0100 | [diff] [blame] | 151 | fd = global.tune.maxpollevents; |
Willy Tarreau | 7e9c4ae | 2018-10-17 14:31:19 +0200 | [diff] [blame] | 152 | tv_entering_poll(); |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 153 | activity_count_runtime(); |
Willy Tarreau | beb859a | 2018-11-22 18:07:59 +0100 | [diff] [blame] | 154 | |
| 155 | do { |
| 156 | int timeout = (global.tune.options & GTUNE_BUSY_POLLING) ? 0 : wait_time; |
| 157 | |
| 158 | timeout_ts.tv_sec = (timeout / 1000); |
| 159 | timeout_ts.tv_nsec = (timeout % 1000) * 1000000; |
| 160 | |
| 161 | status = kevent(kqueue_fd[tid], // int kq |
| 162 | NULL, // const struct kevent *changelist |
| 163 | 0, // int nchanges |
| 164 | kev, // struct kevent *eventlist |
| 165 | fd, // int nevents |
| 166 | &timeout_ts); // const struct timespec *timeout |
| 167 | tv_update_date(timeout, status); |
| 168 | |
| 169 | if (status) |
| 170 | break; |
| 171 | if (timeout || !wait_time) |
| 172 | break; |
Willy Tarreau | 2ae84e4 | 2019-05-28 16:44:05 +0200 | [diff] [blame] | 173 | if (signal_queue_len || wake) |
Willy Tarreau | beb859a | 2018-11-22 18:07:59 +0100 | [diff] [blame] | 174 | break; |
| 175 | if (tick_isset(exp) && tick_is_expired(exp, now_ms)) |
| 176 | break; |
| 177 | } while (1); |
| 178 | |
| 179 | tv_leaving_poll(wait_time, status); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 180 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 181 | thread_harmless_end(); |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 182 | if (sleeping_thread_mask & tid_bit) |
| 183 | _HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 184 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 185 | for (count = 0; count < status; count++) { |
Christopher Faulet | ab62f51 | 2017-08-30 10:34:36 +0200 | [diff] [blame] | 186 | unsigned int n = 0; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 187 | fd = kev[count].ident; |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 188 | |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 189 | if (!fdtab[fd].owner) { |
| 190 | activity[tid].poll_dead++; |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 191 | continue; |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | if (!(fdtab[fd].thread_mask & tid_bit)) { |
| 195 | activity[tid].poll_skip++; |
| 196 | continue; |
| 197 | } |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 198 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 199 | if (kev[count].filter == EVFILT_READ) { |
Willy Tarreau | fa06176 | 2017-03-13 20:49:56 +0100 | [diff] [blame] | 200 | if (kev[count].data) |
Christopher Faulet | ab62f51 | 2017-08-30 10:34:36 +0200 | [diff] [blame] | 201 | n |= FD_POLL_IN; |
Willy Tarreau | 19c4ab9 | 2017-03-13 20:36:48 +0100 | [diff] [blame] | 202 | if (kev[count].flags & EV_EOF) |
Christopher Faulet | ab62f51 | 2017-08-30 10:34:36 +0200 | [diff] [blame] | 203 | n |= FD_POLL_HUP; |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 204 | } |
| 205 | else if (kev[count].filter == EVFILT_WRITE) { |
Christopher Faulet | ab62f51 | 2017-08-30 10:34:36 +0200 | [diff] [blame] | 206 | n |= FD_POLL_OUT; |
Willy Tarreau | 19c4ab9 | 2017-03-13 20:36:48 +0100 | [diff] [blame] | 207 | if (kev[count].flags & EV_EOF) |
Christopher Faulet | ab62f51 | 2017-08-30 10:34:36 +0200 | [diff] [blame] | 208 | n |= FD_POLL_ERR; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 209 | } |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 210 | |
Christopher Faulet | ab62f51 | 2017-08-30 10:34:36 +0200 | [diff] [blame] | 211 | fd_update_events(fd, n); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 215 | |
| 216 | static int init_kqueue_per_thread() |
| 217 | { |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 218 | int fd; |
| 219 | |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 220 | /* we can have up to two events per fd, so allocate enough to store |
| 221 | * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined, |
| 222 | * so that we can add an invalid entry and get an error, to avoid |
| 223 | * scanning the kqueue uselessly. |
| 224 | */ |
| 225 | kev = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1)); |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 226 | if (kev == NULL) |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 227 | goto fail_alloc; |
| 228 | |
Christopher Faulet | 727c89b | 2018-01-25 16:40:35 +0100 | [diff] [blame] | 229 | if (MAX_THREADS > 1 && tid) { |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 230 | kqueue_fd[tid] = kqueue(); |
| 231 | if (kqueue_fd[tid] < 0) |
| 232 | goto fail_fd; |
| 233 | } |
| 234 | |
| 235 | /* we may have to unregister some events initially registered on the |
| 236 | * original fd when it was alone, and/or to register events on the new |
| 237 | * fd for this thread. Let's just mark them as updated, the poller will |
| 238 | * do the rest. |
| 239 | */ |
Willy Tarreau | ce036bc | 2018-01-29 14:58:02 +0100 | [diff] [blame] | 240 | for (fd = 0; fd < global.maxsock; fd++) |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 241 | updt_fd_polling(fd); |
| 242 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 243 | return 1; |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 244 | fail_fd: |
| 245 | free(kev); |
| 246 | fail_alloc: |
| 247 | return 0; |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static void deinit_kqueue_per_thread() |
| 251 | { |
Christopher Faulet | 727c89b | 2018-01-25 16:40:35 +0100 | [diff] [blame] | 252 | if (MAX_THREADS > 1 && tid) |
Christopher Faulet | 13b007d | 2018-01-25 16:32:18 +0100 | [diff] [blame] | 253 | close(kqueue_fd[tid]); |
| 254 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 255 | free(kev); |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 256 | kev = NULL; |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 259 | /* |
| 260 | * Initialization of the kqueue() poller. |
| 261 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 262 | * disables the poller by setting its pref to 0. |
| 263 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 264 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 265 | { |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 266 | p->private = NULL; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 267 | |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 268 | /* we can have up to two events per fd, so allocate enough to store |
| 269 | * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined, |
| 270 | * so that we can add an invalid entry and get an error, to avoid |
| 271 | * scanning the kqueue uselessly. |
| 272 | */ |
| 273 | kev_out = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1)); |
| 274 | if (!kev_out) |
| 275 | goto fail_alloc; |
| 276 | |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 277 | kqueue_fd[tid] = kqueue(); |
| 278 | if (kqueue_fd[tid] < 0) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 279 | goto fail_fd; |
| 280 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 281 | hap_register_per_thread_init(init_kqueue_per_thread); |
| 282 | hap_register_per_thread_deinit(deinit_kqueue_per_thread); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 283 | return 1; |
| 284 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 285 | fail_fd: |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 286 | free(kev_out); |
| 287 | kev_out = NULL; |
| 288 | fail_alloc: |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 289 | p->pref = 0; |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * Termination of the kqueue() poller. |
| 295 | * Memory is released and the poller is marked as unselectable. |
| 296 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 297 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 298 | { |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 299 | if (kqueue_fd[tid] >= 0) { |
| 300 | close(kqueue_fd[tid]); |
| 301 | kqueue_fd[tid] = -1; |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 302 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 303 | |
| 304 | p->private = NULL; |
| 305 | p->pref = 0; |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 306 | if (kev_out) { |
| 307 | free(kev_out); |
| 308 | kev_out = NULL; |
| 309 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 313 | * Check that the poller works. |
| 314 | * Returns 1 if OK, otherwise 0. |
| 315 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 316 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 317 | { |
| 318 | int fd; |
| 319 | |
| 320 | fd = kqueue(); |
| 321 | if (fd < 0) |
| 322 | return 0; |
| 323 | close(fd); |
| 324 | return 1; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK, |
| 329 | * otherwise 0. Note that some pollers need to be reopened after a fork() |
| 330 | * (such as kqueue), and some others may fail to do so in a chroot. |
| 331 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 332 | REGPRM1 static int _do_fork(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 333 | { |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 334 | kqueue_fd[tid] = kqueue(); |
| 335 | if (kqueue_fd[tid] < 0) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 336 | return 0; |
| 337 | return 1; |
| 338 | } |
| 339 | |
| 340 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 341 | * It is a constructor, which means that it will automatically be called before |
| 342 | * main(). This is GCC-specific but it works at least since 2.95. |
| 343 | * 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] | 344 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 345 | __attribute__((constructor)) |
| 346 | static void _do_register(void) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 347 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 348 | struct poller *p; |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 349 | int i; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 350 | |
| 351 | if (nbpollers >= MAX_POLLERS) |
| 352 | return; |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 353 | |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 354 | for (i = 0; i < MAX_THREADS; i++) |
| 355 | kqueue_fd[i] = -1; |
| 356 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 357 | p = &pollers[nbpollers++]; |
| 358 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 359 | p->name = "kqueue"; |
| 360 | p->pref = 300; |
Willy Tarreau | 19c4ab9 | 2017-03-13 20:36:48 +0100 | [diff] [blame] | 361 | p->flags = HAP_POLL_F_RDHUP; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 362 | p->private = NULL; |
| 363 | |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 364 | p->clo = NULL; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 365 | p->test = _do_test; |
| 366 | p->init = _do_init; |
| 367 | p->term = _do_term; |
| 368 | p->poll = _do_poll; |
| 369 | p->fork = _do_fork; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | |
| 373 | /* |
| 374 | * Local variables: |
| 375 | * c-indent-level: 8 |
| 376 | * c-basic-offset: 8 |
| 377 | * End: |
| 378 | */ |