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