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> |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | #include <types/fd.h> |
| 33 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 34 | /* public variables */ |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 35 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 36 | extern volatile struct fdlist fd_cache; |
| 37 | extern volatile struct fdlist fd_cache_local[MAX_THREADS]; |
| 38 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 39 | extern volatile struct fdlist update_list; |
| 40 | |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 41 | extern unsigned long *polled_mask; |
| 42 | |
Christopher Faulet | 69553fe | 2018-01-15 11:57:03 +0100 | [diff] [blame] | 43 | extern unsigned long fd_cache_mask; // Mask of threads with events in the cache |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 44 | |
| 45 | extern THREAD_LOCAL int *fd_updt; // FD updates list |
| 46 | extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list |
| 47 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 48 | extern int poller_wr_pipe[MAX_THREADS]; |
| 49 | |
Willy Tarreau | 8b94969 | 2017-11-26 11:07:34 +0100 | [diff] [blame] | 50 | __decl_hathreads(extern HA_RWLOCK_T __attribute__((aligned(64))) fdcache_lock); /* global lock to protect fd_cache array */ |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 51 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 52 | /* Deletes an FD from the fdsets. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | * The file descriptor is also closed. |
| 54 | */ |
| 55 | void fd_delete(int fd); |
| 56 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 57 | /* Deletes an FD from the fdsets. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 58 | * The file descriptor is kept open. |
| 59 | */ |
| 60 | void fd_remove(int fd); |
| 61 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 62 | /* disable the specified poller */ |
| 63 | void disable_poller(const char *poller_name); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 65 | void poller_pipe_io_handler(int fd); |
| 66 | |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 67 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 68 | * Initialize the pollers till the best one is found. |
| 69 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 70 | * The pollers register themselves just before main() is called. |
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 | int init_pollers(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 73 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 74 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 75 | * Deinitialize the pollers. |
| 76 | */ |
| 77 | void deinit_pollers(); |
| 78 | |
| 79 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 80 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 81 | * to create initialize part of them again. Returns 0 in case of failure, |
| 82 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 83 | * the the current poller is destroyed and the caller is responsible for trying |
| 84 | * another one by calling init_pollers() again. |
| 85 | */ |
| 86 | int fork_poller(); |
| 87 | |
| 88 | /* |
| 89 | * Lists the known pollers on <out>. |
| 90 | * Should be performed only before initialization. |
| 91 | */ |
| 92 | int list_pollers(FILE *out); |
| 93 | |
| 94 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 95 | * Runs the polling loop |
| 96 | */ |
| 97 | void run_poller(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 98 | |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 99 | /* Scan and process the cached events. This should be called right after |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 100 | * the poller. |
| 101 | */ |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 102 | void fd_process_cached_events(); |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 103 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 104 | void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off); |
| 105 | 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] | 106 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 107 | /* Mark fd <fd> as updated for polling and allocate an entry in the update list |
| 108 | * 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] | 109 | */ |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 110 | static inline void updt_fd_polling(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 111 | { |
Olivier Houchard | 6aab737 | 2018-08-17 13:37:59 +0200 | [diff] [blame] | 112 | if ((fdtab[fd].thread_mask & all_threads_mask) == tid_bit) { |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 113 | unsigned int oldupdt; |
| 114 | |
| 115 | /* note: we don't have a test-and-set yet in hathreads */ |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 116 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 117 | if (HA_ATOMIC_BTS(&fdtab[fd].update_mask, tid)) |
| 118 | return; |
| 119 | |
| 120 | oldupdt = HA_ATOMIC_ADD(&fd_nbupdt, 1) - 1; |
| 121 | fd_updt[oldupdt] = fd; |
| 122 | } else { |
| 123 | unsigned long update_mask = fdtab[fd].update_mask; |
| 124 | do { |
| 125 | if (update_mask == fdtab[fd].thread_mask) |
| 126 | return; |
| 127 | } while (!HA_ATOMIC_CAS(&fdtab[fd].update_mask, &update_mask, |
| 128 | fdtab[fd].thread_mask)); |
| 129 | fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update)); |
| 130 | } |
| 131 | |
| 132 | } |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 133 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 134 | /* Called from the poller to acknoledge we read an entry from the global |
| 135 | * update list, to remove our bit from the update_mask, and remove it from |
| 136 | * the list if we were the last one. |
| 137 | */ |
| 138 | static inline void done_update_polling(int fd) |
| 139 | { |
| 140 | unsigned long update_mask; |
| 141 | |
| 142 | update_mask = HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit); |
| 143 | while ((update_mask & all_threads_mask)== 0) { |
| 144 | /* If we were the last one that had to update that entry, remove it from the list */ |
| 145 | fd_rm_from_fd_list(&update_list, fd, offsetof(struct fdtab, update)); |
| 146 | if (update_list.first == fd) |
| 147 | abort(); |
| 148 | update_mask = (volatile unsigned long)fdtab[fd].update_mask; |
| 149 | if ((update_mask & all_threads_mask) != 0) { |
| 150 | /* Maybe it's been re-updated in the meanwhile, and we |
| 151 | * wrongly removed it from the list, if so, re-add it |
| 152 | */ |
| 153 | fd_add_to_fd_list(&update_list, fd, offsetof(struct fdtab, update)); |
| 154 | update_mask = (volatile unsigned long)(fdtab[fd].update_mask); |
| 155 | /* And then check again, just in case after all it |
| 156 | * should be removed, even if it's very unlikely, given |
| 157 | * the current thread wouldn't have been able to take |
| 158 | * care of it yet */ |
| 159 | } else |
| 160 | break; |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 161 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 162 | } |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 165 | /* Allocates a cache entry for a file descriptor if it does not yet have one. |
| 166 | * This can be done at any time. |
| 167 | */ |
| 168 | static inline void fd_alloc_cache_entry(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 169 | { |
Willy Tarreau | 26fb5d8 | 2018-03-20 19:06:52 +0100 | [diff] [blame] | 170 | HA_ATOMIC_OR(&fd_cache_mask, fdtab[fd].thread_mask); |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 171 | if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1))) |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 172 | fd_add_to_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd, offsetof(struct fdtab, cache)); |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 173 | else |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 174 | fd_add_to_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache)); |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 177 | /* Removes entry used by fd <fd> from the FD cache and replaces it with the |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 178 | * last one. |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 179 | * If the fd has no entry assigned, return immediately. |
| 180 | */ |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 181 | static inline void fd_release_cache_entry(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 182 | { |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 183 | if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1))) |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 184 | fd_rm_from_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd, offsetof(struct fdtab, cache)); |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 185 | else |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 186 | fd_rm_from_fd_list(&fd_cache, fd, offsetof(struct fdtab, cache)); |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 187 | } |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 188 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 189 | /* This function automatically enables/disables caching for an entry depending |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 190 | * on its state. It is only called on state changes. |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 191 | */ |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 192 | static inline void fd_update_cache(int fd) |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 193 | { |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 194 | /* only READY and ACTIVE states (the two with both flags set) require a cache entry */ |
| 195 | if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) || |
| 196 | ((fdtab[fd].state & (FD_EV_READY_W | FD_EV_ACTIVE_W)) == (FD_EV_READY_W | FD_EV_ACTIVE_W))) { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 197 | fd_alloc_cache_entry(fd); |
| 198 | } |
| 199 | else { |
| 200 | fd_release_cache_entry(fd); |
| 201 | } |
| 202 | } |
| 203 | |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 204 | /* |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 205 | * returns the FD's recv state (FD_EV_*) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 206 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 207 | static inline int fd_recv_state(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 208 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 209 | return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 212 | /* |
| 213 | * returns true if the FD is active for recv |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 214 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 215 | static inline int fd_recv_active(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 216 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 217 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 220 | /* |
| 221 | * returns true if the FD is ready for recv |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 222 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 223 | static inline int fd_recv_ready(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 224 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 225 | return (unsigned)fdtab[fd].state & FD_EV_READY_R; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * returns true if the FD is polled for recv |
| 230 | */ |
| 231 | static inline int fd_recv_polled(const int fd) |
| 232 | { |
| 233 | return (unsigned)fdtab[fd].state & FD_EV_POLLED_R; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * returns the FD's send state (FD_EV_*) |
| 238 | */ |
| 239 | static inline int fd_send_state(const int fd) |
| 240 | { |
| 241 | return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * returns true if the FD is active for send |
| 246 | */ |
| 247 | static inline int fd_send_active(const int fd) |
| 248 | { |
| 249 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 252 | /* |
| 253 | * returns true if the FD is ready for send |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 254 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 255 | static inline int fd_send_ready(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 256 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 257 | return (unsigned)fdtab[fd].state & FD_EV_READY_W; |
| 258 | } |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 259 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 260 | /* |
| 261 | * returns true if the FD is polled for send |
| 262 | */ |
| 263 | static inline int fd_send_polled(const int fd) |
| 264 | { |
| 265 | return (unsigned)fdtab[fd].state & FD_EV_POLLED_W; |
| 266 | } |
| 267 | |
Christopher Faulet | 8db2fdf | 2017-08-30 09:59:38 +0200 | [diff] [blame] | 268 | /* |
| 269 | * returns true if the FD is active for recv or send |
| 270 | */ |
| 271 | static inline int fd_active(const int fd) |
| 272 | { |
| 273 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW; |
| 274 | } |
| 275 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 276 | /* Disable processing recv events on fd <fd> */ |
| 277 | static inline void fd_stop_recv(int fd) |
| 278 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 279 | unsigned char old, new; |
| 280 | |
| 281 | old = fdtab[fd].state; |
| 282 | do { |
| 283 | if (!(old & FD_EV_ACTIVE_R)) |
| 284 | return; |
| 285 | new = old & ~FD_EV_ACTIVE_R; |
| 286 | new &= ~FD_EV_POLLED_R; |
| 287 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 288 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 289 | if ((old ^ new) & FD_EV_POLLED_R) |
| 290 | updt_fd_polling(fd); |
| 291 | |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 292 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 293 | fd_update_cache(fd); /* need an update entry to change the state */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 294 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 297 | /* Disable processing send events on fd <fd> */ |
| 298 | static inline void fd_stop_send(int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 299 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 300 | unsigned char old, new; |
| 301 | |
| 302 | old = fdtab[fd].state; |
| 303 | do { |
| 304 | if (!(old & FD_EV_ACTIVE_W)) |
| 305 | return; |
| 306 | new = old & ~FD_EV_ACTIVE_W; |
| 307 | new &= ~FD_EV_POLLED_W; |
| 308 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 309 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 310 | if ((old ^ new) & FD_EV_POLLED_W) |
| 311 | updt_fd_polling(fd); |
| 312 | |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 313 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 314 | fd_update_cache(fd); /* need an update entry to change the state */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 315 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 318 | /* Disable processing of events on fd <fd> for both directions. */ |
| 319 | static inline void fd_stop_both(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 320 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 321 | unsigned char old, new; |
| 322 | |
| 323 | old = fdtab[fd].state; |
| 324 | do { |
| 325 | if (!(old & FD_EV_ACTIVE_RW)) |
| 326 | return; |
| 327 | new = old & ~FD_EV_ACTIVE_RW; |
| 328 | new &= ~FD_EV_POLLED_RW; |
| 329 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 330 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 331 | if ((old ^ new) & FD_EV_POLLED_RW) |
| 332 | updt_fd_polling(fd); |
| 333 | |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 334 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 335 | fd_update_cache(fd); /* need an update entry to change the state */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 336 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 337 | } |
| 338 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 339 | /* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */ |
| 340 | static inline void fd_cant_recv(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 341 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 342 | unsigned char old, new; |
| 343 | |
| 344 | old = fdtab[fd].state; |
| 345 | do { |
| 346 | if (!(old & FD_EV_READY_R)) |
| 347 | return; |
| 348 | new = old & ~FD_EV_READY_R; |
| 349 | if (new & FD_EV_ACTIVE_R) |
| 350 | new |= FD_EV_POLLED_R; |
| 351 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 352 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 353 | if ((old ^ new) & FD_EV_POLLED_R) |
| 354 | updt_fd_polling(fd); |
| 355 | |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 356 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 357 | fd_update_cache(fd); /* need an update entry to change the state */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 358 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 361 | /* Report that FD <fd> can receive anymore without polling. */ |
| 362 | static inline void fd_may_recv(const int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 363 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 364 | /* marking ready never changes polled status */ |
| 365 | HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_R); |
| 366 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 367 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 368 | fd_update_cache(fd); /* need an update entry to change the state */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 369 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 372 | /* Disable readiness when polled. This is useful to interrupt reading when it |
| 373 | * is suspected that the end of data might have been reached (eg: short read). |
| 374 | * This can only be done using level-triggered pollers, so if any edge-triggered |
| 375 | * is ever implemented, a test will have to be added here. |
| 376 | */ |
| 377 | static inline void fd_done_recv(const int fd) |
| 378 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 379 | unsigned char old, new; |
| 380 | |
| 381 | old = fdtab[fd].state; |
| 382 | do { |
| 383 | if ((old & (FD_EV_POLLED_R|FD_EV_READY_R)) != (FD_EV_POLLED_R|FD_EV_READY_R)) |
| 384 | return; |
| 385 | new = old & ~FD_EV_READY_R; |
| 386 | if (new & FD_EV_ACTIVE_R) |
| 387 | new |= FD_EV_POLLED_R; |
| 388 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 389 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 390 | if ((old ^ new) & FD_EV_POLLED_R) |
| 391 | updt_fd_polling(fd); |
| 392 | |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 393 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 394 | fd_update_cache(fd); /* need an update entry to change the state */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 395 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 396 | } |
| 397 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 398 | /* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */ |
| 399 | static inline void fd_cant_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 400 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 401 | unsigned char old, new; |
| 402 | |
| 403 | old = fdtab[fd].state; |
| 404 | do { |
| 405 | if (!(old & FD_EV_READY_W)) |
| 406 | return; |
| 407 | new = old & ~FD_EV_READY_W; |
| 408 | if (new & FD_EV_ACTIVE_W) |
| 409 | new |= FD_EV_POLLED_W; |
| 410 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 411 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 412 | if ((old ^ new) & FD_EV_POLLED_W) |
| 413 | updt_fd_polling(fd); |
| 414 | |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 415 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 416 | fd_update_cache(fd); /* need an update entry to change the state */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 417 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 420 | /* Report that FD <fd> can send anymore without polling (EAGAIN detected). */ |
| 421 | static inline void fd_may_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 422 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 423 | /* marking ready never changes polled status */ |
| 424 | HA_ATOMIC_OR(&fdtab[fd].state, FD_EV_READY_W); |
| 425 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 426 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 427 | fd_update_cache(fd); /* need an update entry to change the state */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 428 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 429 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 430 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 431 | /* Prepare FD <fd> to try to receive */ |
| 432 | static inline void fd_want_recv(int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 433 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 434 | unsigned char old, new; |
| 435 | |
| 436 | old = fdtab[fd].state; |
| 437 | do { |
| 438 | if (old & FD_EV_ACTIVE_R) |
| 439 | return; |
| 440 | new = old | FD_EV_ACTIVE_R; |
| 441 | if (!(new & FD_EV_READY_R)) |
| 442 | new |= FD_EV_POLLED_R; |
| 443 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 444 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 445 | if ((old ^ new) & FD_EV_POLLED_R) |
| 446 | updt_fd_polling(fd); |
| 447 | |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 448 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 449 | fd_update_cache(fd); /* need an update entry to change the state */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 450 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 453 | /* Prepare FD <fd> to try to send */ |
| 454 | static inline void fd_want_send(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 455 | { |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 456 | unsigned char old, new; |
| 457 | |
| 458 | old = fdtab[fd].state; |
| 459 | do { |
| 460 | if (old & FD_EV_ACTIVE_W) |
| 461 | return; |
| 462 | new = old | FD_EV_ACTIVE_W; |
| 463 | if (!(new & FD_EV_READY_W)) |
| 464 | new |= FD_EV_POLLED_W; |
| 465 | } while (unlikely(!HA_ATOMIC_CAS(&fdtab[fd].state, &old, new))); |
| 466 | |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 467 | if ((old ^ new) & FD_EV_POLLED_W) |
| 468 | updt_fd_polling(fd); |
| 469 | |
Willy Tarreau | 4d84186 | 2018-01-17 22:57:54 +0100 | [diff] [blame] | 470 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 7ac0e35 | 2018-01-17 21:25:57 +0100 | [diff] [blame] | 471 | fd_update_cache(fd); /* need an update entry to change the state */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 472 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 473 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 474 | |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 475 | /* Update events seen for FD <fd> and its state if needed. This should be called |
| 476 | * by the poller to set FD_POLL_* flags. */ |
| 477 | static inline void fd_update_events(int fd, int evts) |
| 478 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 479 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 480 | fdtab[fd].ev &= FD_POLL_STICKY; |
| 481 | fdtab[fd].ev |= evts; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 482 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 483 | |
| 484 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) |
| 485 | fd_may_recv(fd); |
| 486 | |
| 487 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) |
| 488 | fd_may_send(fd); |
| 489 | } |
| 490 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 491 | /* Prepares <fd> for being polled */ |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 492 | 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] | 493 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 494 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 495 | fdtab[fd].owner = owner; |
| 496 | fdtab[fd].iocb = iocb; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 497 | fdtab[fd].ev = 0; |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 498 | fdtab[fd].linger_risk = 0; |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 499 | fdtab[fd].cloned = 0; |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 500 | fdtab[fd].thread_mask = thread_mask; |
Willy Tarreau | c9c8378 | 2018-01-17 18:44:46 +0100 | [diff] [blame] | 501 | /* note: do not reset polled_mask here as it indicates which poller |
| 502 | * still knows this FD from a possible previous round. |
| 503 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 504 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Willy Tarreau | 322e6c7 | 2018-01-25 16:37:04 +0100 | [diff] [blame] | 507 | /* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */ |
| 508 | static inline void hap_fd_set(int fd, unsigned int *evts) |
| 509 | { |
Willy Tarreau | 82b37d7 | 2018-01-25 16:59:09 +0100 | [diff] [blame] | 510 | 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] | 511 | } |
| 512 | |
| 513 | static inline void hap_fd_clr(int fd, unsigned int *evts) |
| 514 | { |
Willy Tarreau | 82b37d7 | 2018-01-25 16:59:09 +0100 | [diff] [blame] | 515 | 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] | 516 | } |
| 517 | |
| 518 | static inline unsigned int hap_fd_isset(int fd, unsigned int *evts) |
| 519 | { |
| 520 | return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1))); |
| 521 | } |
| 522 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 523 | static inline void wake_thread(int tid) |
| 524 | { |
| 525 | char c = 'c'; |
| 526 | |
| 527 | shut_your_big_mouth_gcc(write(poller_wr_pipe[tid], &c, 1)); |
| 528 | } |
| 529 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 530 | |
| 531 | #endif /* _PROTO_FD_H */ |
| 532 | |
| 533 | /* |
| 534 | * Local variables: |
| 535 | * c-indent-level: 8 |
| 536 | * c-basic-offset: 8 |
| 537 | * End: |
| 538 | */ |