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