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