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