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