Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 2 | * include/proto/fd.h |
| 3 | * File descriptors states. |
| 4 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 5 | * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _PROTO_FD_H |
| 23 | #define _PROTO_FD_H |
| 24 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 30 | #include <common/config.h> |
Willy Tarreau | f37ba94 | 2018-10-17 11:25:54 +0200 | [diff] [blame] | 31 | #include <common/ticks.h> |
| 32 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | #include <types/fd.h> |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 34 | #include <proto/activity.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 36 | /* public variables */ |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 37 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 38 | extern volatile struct fdlist update_list; |
| 39 | |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 40 | |
| 41 | extern struct polled_mask { |
| 42 | unsigned long poll_recv; |
| 43 | unsigned long poll_send; |
| 44 | } *polled_mask; |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 45 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 46 | extern THREAD_LOCAL int *fd_updt; // FD updates list |
| 47 | extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list |
| 48 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 49 | extern int poller_wr_pipe[MAX_THREADS]; |
| 50 | |
Olivier Houchard | 7c49d2e | 2019-04-16 18:37:05 +0200 | [diff] [blame] | 51 | extern volatile int ha_used_fds; // Number of FDs we're currently using |
| 52 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 53 | /* Deletes an FD from the fdsets. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | * The file descriptor is also closed. |
| 55 | */ |
| 56 | void fd_delete(int fd); |
| 57 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 58 | /* Deletes an FD from the fdsets. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 59 | * The file descriptor is kept open. |
| 60 | */ |
| 61 | void fd_remove(int fd); |
| 62 | |
Willy Tarreau | 2d7f81b | 2019-02-21 22:19:17 +0100 | [diff] [blame] | 63 | /* close all FDs starting from <start> */ |
| 64 | void my_closefrom(int start); |
| 65 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 66 | /* disable the specified poller */ |
| 67 | void disable_poller(const char *poller_name); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 69 | void poller_pipe_io_handler(int fd); |
| 70 | |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 71 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 72 | * Initialize the pollers till the best one is found. |
| 73 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 74 | * The pollers register themselves just before main() is called. |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 75 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 76 | int init_pollers(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 77 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 78 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 79 | * Deinitialize the pollers. |
| 80 | */ |
| 81 | void deinit_pollers(); |
| 82 | |
| 83 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 84 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 85 | * to create initialize part of them again. Returns 0 in case of failure, |
| 86 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 87 | * the the current poller is destroyed and the caller is responsible for trying |
| 88 | * another one by calling init_pollers() again. |
| 89 | */ |
| 90 | int fork_poller(); |
| 91 | |
| 92 | /* |
| 93 | * Lists the known pollers on <out>. |
| 94 | * Should be performed only before initialization. |
| 95 | */ |
| 96 | int list_pollers(FILE *out); |
| 97 | |
| 98 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 99 | * Runs the polling loop |
| 100 | */ |
| 101 | void run_poller(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 102 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 103 | void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off); |
| 104 | void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 105 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 106 | /* Mark fd <fd> as updated for polling and allocate an entry in the update list |
| 107 | * for this if it was not already there. This can be done at any time. |
Willy Tarreau | e852545 | 2014-01-25 09:58:06 +0100 | [diff] [blame] | 108 | */ |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 109 | static inline void updt_fd_polling(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 110 | { |
Olivier Houchard | 6aab737 | 2018-08-17 13:37:59 +0200 | [diff] [blame] | 111 | if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) { |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 112 | |
| 113 | /* note: we don't have a test-and-set yet in hathreads */ |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 114 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 115 | if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid)) |
| 116 | return; |
| 117 | |
Olivier Houchard | a0fdce3 | 2019-06-12 14:31:08 +0200 | [diff] [blame] | 118 | fd_updt[fd_nbupdt++] = fd; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 119 | } else { |
| 120 | unsigned long update_mask = fdtab[fd].update_mask; |
| 121 | do { |
| 122 | if (update_mask == fdtab[fd].thread_mask) |
| 123 | return; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 124 | } while (!_HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask, |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 125 | fdtab[fd].thread_mask)); |
| 126 | fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update)); |
| 127 | } |
| 128 | |
| 129 | } |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 130 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 131 | /* Called from the poller to acknoledge we read an entry from the global |
| 132 | * update list, to remove our bit from the update_mask, and remove it from |
| 133 | * the list if we were the last one. |
| 134 | */ |
| 135 | static inline void done_update_polling(int fd) |
| 136 | { |
| 137 | unsigned long update_mask; |
| 138 | |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 139 | update_mask = _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit); |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 140 | while ((update_mask & all_threads_mask)== 0) { |
| 141 | /* If we were the last one that had to update that entry, remove it from the list */ |
| 142 | fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update)); |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 143 | update_mask = (volatile unsigned long)fdtab[fd].update_mask; |
| 144 | if ((update_mask & all_threads_mask) != 0) { |
| 145 | /* Maybe it's been re-updated in the meanwhile, and we |
| 146 | * wrongly removed it from the list, if so, re-add it |
| 147 | */ |
| 148 | fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update)); |
| 149 | update_mask = (volatile unsigned long)(fdtab[fd].update_mask); |
| 150 | /* And then check again, just in case after all it |
| 151 | * should be removed, even if it's very unlikely, given |
| 152 | * the current thread wouldn't have been able to take |
| 153 | * care of it yet */ |
| 154 | } else |
| 155 | break; |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 156 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 157 | } |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 160 | /* |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 161 | * returns the FD's recv state (FD_EV_*) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 162 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 163 | static inline int fd_recv_state(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 164 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 165 | return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 168 | /* |
| 169 | * returns true if the FD is active for recv |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 170 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 171 | static inline int fd_recv_active(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 172 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 173 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 176 | /* |
| 177 | * returns true if the FD is ready for recv |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 178 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 179 | static inline int fd_recv_ready(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 180 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 181 | return (unsigned)fdtab[fd].state & FD_EV_READY_R; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * returns true if the FD is polled for recv |
| 186 | */ |
| 187 | static inline int fd_recv_polled(const int fd) |
| 188 | { |
| 189 | return (unsigned)fdtab[fd].state & FD_EV_POLLED_R; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * returns the FD's send state (FD_EV_*) |
| 194 | */ |
| 195 | static inline int fd_send_state(const int fd) |
| 196 | { |
| 197 | return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * returns true if the FD is active for send |
| 202 | */ |
| 203 | static inline int fd_send_active(const int fd) |
| 204 | { |
| 205 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 208 | /* |
| 209 | * returns true if the FD is ready for send |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 210 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 211 | static inline int fd_send_ready(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 212 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 213 | return (unsigned)fdtab[fd].state & FD_EV_READY_W; |
| 214 | } |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 215 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 216 | /* |
| 217 | * returns true if the FD is polled for send |
| 218 | */ |
| 219 | static inline int fd_send_polled(const int fd) |
| 220 | { |
| 221 | return (unsigned)fdtab[fd].state & FD_EV_POLLED_W; |
| 222 | } |
| 223 | |
Christopher Faulet | 8db2fdf | 2017-08-30 09:59:38 +0200 | [diff] [blame] | 224 | /* |
| 225 | * returns true if the FD is active for recv or send |
| 226 | */ |
| 227 | static inline int fd_active(const int fd) |
| 228 | { |
| 229 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW; |
| 230 | } |
| 231 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 232 | /* Disable processing recv events on fd <fd> */ |
| 233 | static inline void fd_stop_recv(int fd) |
| 234 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 235 | unsigned char old, new; |
| 236 | |
| 237 | old = fdtab[fd].state; |
| 238 | do { |
| 239 | if (!(old & FD_EV_ACTIVE_R)) |
| 240 | return; |
| 241 | new = old & ~FD_EV_ACTIVE_R; |
| 242 | new &= ~FD_EV_POLLED_R; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 243 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 244 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 245 | if ((old ^ new) & FD_EV_POLLED_R) |
| 246 | updt_fd_polling(fd); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 249 | /* Disable processing send events on fd <fd> */ |
| 250 | static inline void fd_stop_send(int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 251 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 252 | unsigned char old, new; |
| 253 | |
| 254 | old = fdtab[fd].state; |
| 255 | do { |
| 256 | if (!(old & FD_EV_ACTIVE_W)) |
| 257 | return; |
| 258 | new = old & ~FD_EV_ACTIVE_W; |
| 259 | new &= ~FD_EV_POLLED_W; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 260 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 261 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 262 | if ((old ^ new) & FD_EV_POLLED_W) |
| 263 | updt_fd_polling(fd); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 266 | /* Disable processing of events on fd <fd> for both directions. */ |
| 267 | static inline void fd_stop_both(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 268 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 269 | unsigned char old, new; |
| 270 | |
| 271 | old = fdtab[fd].state; |
| 272 | do { |
| 273 | if (!(old & FD_EV_ACTIVE_RW)) |
| 274 | return; |
| 275 | new = old & ~FD_EV_ACTIVE_RW; |
| 276 | new &= ~FD_EV_POLLED_RW; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 277 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 278 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 279 | if ((old ^ new) & FD_EV_POLLED_RW) |
| 280 | updt_fd_polling(fd); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 283 | /* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */ |
| 284 | static inline void fd_cant_recv(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 285 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 286 | unsigned char old, new; |
| 287 | |
| 288 | old = fdtab[fd].state; |
| 289 | do { |
| 290 | if (!(old & FD_EV_READY_R)) |
| 291 | return; |
| 292 | new = old & ~FD_EV_READY_R; |
| 293 | if (new & FD_EV_ACTIVE_R) |
| 294 | new |= FD_EV_POLLED_R; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 295 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 296 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 297 | if ((old ^ new) & FD_EV_POLLED_R) |
| 298 | updt_fd_polling(fd); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Willy Tarreau | 1dad384 | 2019-07-08 23:09:03 +0200 | [diff] [blame] | 301 | /* Report that FD <fd> may receive again without polling. */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 302 | static inline void fd_may_recv(const int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 303 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 304 | /* marking ready never changes polled status */ |
Willy Tarreau | 1dad384 | 2019-07-08 23:09:03 +0200 | [diff] [blame] | 305 | if ((fdtab[fd].state & FD_EV_READY_R) || |
| 306 | HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_READY_R_BIT)) |
| 307 | return; |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 308 | } |
| 309 | |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 310 | /* Disable readiness when polled. This is useful to interrupt reading when it |
| 311 | * is suspected that the end of data might have been reached (eg: short read). |
| 312 | * This can only be done using level-triggered pollers, so if any edge-triggered |
| 313 | * is ever implemented, a test will have to be added here. |
| 314 | */ |
| 315 | static inline void fd_done_recv(const int fd) |
| 316 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 317 | unsigned char old, new; |
| 318 | |
| 319 | old = fdtab[fd].state; |
| 320 | do { |
| 321 | if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R)) |
| 322 | return; |
| 323 | new = old & ~FD_EV_READY_R; |
| 324 | if (new & FD_EV_ACTIVE_R) |
| 325 | new |= FD_EV_POLLED_R; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 326 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 327 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 328 | if ((old ^ new) & FD_EV_POLLED_R) |
| 329 | updt_fd_polling(fd); |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 332 | /* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */ |
| 333 | static inline void fd_cant_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 334 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 335 | unsigned char old, new; |
| 336 | |
| 337 | old = fdtab[fd].state; |
| 338 | do { |
| 339 | if (!(old & FD_EV_READY_W)) |
| 340 | return; |
| 341 | new = old & ~FD_EV_READY_W; |
| 342 | if (new & FD_EV_ACTIVE_W) |
| 343 | new |= FD_EV_POLLED_W; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 344 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 345 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 346 | if ((old ^ new) & FD_EV_POLLED_W) |
| 347 | updt_fd_polling(fd); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Willy Tarreau | 1dad384 | 2019-07-08 23:09:03 +0200 | [diff] [blame] | 350 | /* Report that FD <fd> may send again without polling (EAGAIN not detected). */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 351 | static inline void fd_may_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 352 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 353 | /* marking ready never changes polled status */ |
Willy Tarreau | 1dad384 | 2019-07-08 23:09:03 +0200 | [diff] [blame] | 354 | if ((fdtab[fd].state & FD_EV_READY_W) || |
| 355 | HA_ATOMIC_BTS(&fdtab[fd].state, FD_EV_READY_W_BIT)) |
| 356 | return; |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 357 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 358 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 359 | /* Prepare FD <fd> to try to receive */ |
| 360 | static inline void fd_want_recv(int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 361 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 362 | unsigned char old, new; |
| 363 | |
| 364 | old = fdtab[fd].state; |
| 365 | do { |
| 366 | if (old & FD_EV_ACTIVE_R) |
| 367 | return; |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 368 | new = old | FD_EV_ACTIVE_R | FD_EV_POLLED_R; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 369 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 370 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 371 | if ((old ^ new) & FD_EV_POLLED_R) |
| 372 | updt_fd_polling(fd); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 375 | /* Prepare FD <fd> to try to send */ |
| 376 | static inline void fd_want_send(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 377 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 378 | unsigned char old, new; |
| 379 | |
| 380 | old = fdtab[fd].state; |
| 381 | do { |
| 382 | if (old & FD_EV_ACTIVE_W) |
| 383 | return; |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 384 | new = old | FD_EV_ACTIVE_W | FD_EV_POLLED_W; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 385 | } while (unlikely(!_HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 386 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 387 | if ((old ^ new) & FD_EV_POLLED_W) |
| 388 | updt_fd_polling(fd); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 389 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 390 | |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 391 | /* Update events seen for FD <fd> and its state if needed. This should be called |
| 392 | * by the poller to set FD_POLL_* flags. */ |
| 393 | static inline void fd_update_events(int fd, int evts) |
| 394 | { |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 395 | unsigned long locked = atleast2(fdtab[fd].thread_mask); |
Willy Tarreau | 1dad384 | 2019-07-08 23:09:03 +0200 | [diff] [blame] | 396 | unsigned char old, new; |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 397 | |
Willy Tarreau | 1dad384 | 2019-07-08 23:09:03 +0200 | [diff] [blame] | 398 | old = fdtab[fd].ev; |
| 399 | new = (old & FD_POLL_STICKY) | evts; |
| 400 | |
| 401 | if (unlikely(locked)) { |
| 402 | /* Locked FDs (those with more than 2 threads) are atomically updated */ |
| 403 | while (unlikely(new != old && !_HA_ATOMIC_CAS(&fdtab[fd].ev, &old, new))) |
| 404 | new = (old & FD_POLL_STICKY) | evts; |
| 405 | } else { |
| 406 | if (new != old) |
| 407 | fdtab[fd].ev = new; |
| 408 | } |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 409 | |
| 410 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) |
| 411 | fd_may_recv(fd); |
| 412 | |
| 413 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) |
| 414 | fd_may_send(fd); |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 415 | |
| 416 | if (fdtab[fd].iocb) |
| 417 | fdtab[fd].iocb(fd); |
Willy Tarreau | f5cab82 | 2019-08-16 16:06:14 +0200 | [diff] [blame] | 418 | |
| 419 | ti->flags &= ~TI_FL_STUCK; // this thread is still running |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 422 | /* Prepares <fd> for being polled */ |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 423 | static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned long thread_mask) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 424 | { |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 425 | unsigned long locked = atleast2(thread_mask); |
| 426 | |
| 427 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 428 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 429 | fdtab[fd].owner = owner; |
| 430 | fdtab[fd].iocb = iocb; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 431 | fdtab[fd].ev = 0; |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 432 | fdtab[fd].linger_risk = 0; |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 433 | fdtab[fd].cloned = 0; |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 434 | fdtab[fd].thread_mask = thread_mask; |
Willy Tarreau | c9c8378 | 2018-01-17 18:44:46 +0100 | [diff] [blame] | 435 | /* note: do not reset polled_mask here as it indicates which poller |
| 436 | * still knows this FD from a possible previous round. |
| 437 | */ |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 438 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 439 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Olivier Houchard | 7c49d2e | 2019-04-16 18:37:05 +0200 | [diff] [blame] | 440 | _HA_ATOMIC_ADD(&ha_used_fds, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Willy Tarreau | f37ba94 | 2018-10-17 11:25:54 +0200 | [diff] [blame] | 443 | /* Computes the bounded poll() timeout based on the next expiration timer <next> |
| 444 | * by bounding it to MAX_DELAY_MS. <next> may equal TICK_ETERNITY. The pollers |
| 445 | * just needs to call this function right before polling to get their timeout |
| 446 | * value. Timeouts that are already expired (possibly due to a pending event) |
| 447 | * are accounted for in activity.poll_exp. |
| 448 | */ |
| 449 | static inline int compute_poll_timeout(int next) |
| 450 | { |
| 451 | int wait_time; |
| 452 | |
| 453 | if (!tick_isset(next)) |
| 454 | wait_time = MAX_DELAY_MS; |
| 455 | else if (tick_is_expired(next, now_ms)) { |
| 456 | activity[tid].poll_exp++; |
| 457 | wait_time = 0; |
| 458 | } |
| 459 | else { |
| 460 | wait_time = TICKS_TO_MS(tick_remain(now_ms, next)) + 1; |
| 461 | if (wait_time > MAX_DELAY_MS) |
| 462 | wait_time = MAX_DELAY_MS; |
| 463 | } |
| 464 | return wait_time; |
| 465 | } |
| 466 | |
Willy Tarreau | 322e6c7 | 2018-01-25 16:37:04 +0100 | [diff] [blame] | 467 | /* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */ |
| 468 | static inline void hap_fd_set(int fd, unsigned int *evts) |
| 469 | { |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 470 | _HA_ATOMIC_OR(&evts[fd / (8*sizeof(*evts))], 1U << (fd & (8*sizeof(*evts) - 1))); |
Willy Tarreau | 322e6c7 | 2018-01-25 16:37:04 +0100 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static inline void hap_fd_clr(int fd, unsigned int *evts) |
| 474 | { |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 475 | _HA_ATOMIC_AND(&evts[fd / (8*sizeof(*evts))], ~(1U << (fd & (8*sizeof(*evts) - 1)))); |
Willy Tarreau | 322e6c7 | 2018-01-25 16:37:04 +0100 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static inline unsigned int hap_fd_isset(int fd, unsigned int *evts) |
| 479 | { |
| 480 | return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1))); |
| 481 | } |
| 482 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 483 | static inline void wake_thread(int tid) |
| 484 | { |
| 485 | char c = 'c'; |
| 486 | |
| 487 | shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1)); |
| 488 | } |
| 489 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 490 | |
| 491 | #endif /* _PROTO_FD_H */ |
| 492 | |
| 493 | /* |
| 494 | * Local variables: |
| 495 | * c-indent-level: 8 |
| 496 | * c-basic-offset: 8 |
| 497 | * End: |
| 498 | */ |