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 | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 96 | /* Mark fd <fd> as updated for polling and allocate an entry in the update list |
| 97 | * 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] | 98 | */ |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 99 | static inline void updt_fd_polling(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 100 | { |
Willy Tarreau | ebc78d7 | 2018-01-20 23:53:50 +0100 | [diff] [blame] | 101 | if (fdtab[fd].update_mask & tid_bit) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 102 | /* already scheduled for update */ |
| 103 | return; |
Willy Tarreau | ebc78d7 | 2018-01-20 23:53:50 +0100 | [diff] [blame] | 104 | fdtab[fd].update_mask |= tid_bit; |
Willy Tarreau | 4a29144 | 2012-12-13 23:34:18 +0100 | [diff] [blame] | 105 | fd_updt[fd_nbupdt++] = fd; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame^] | 108 | |
| 109 | #define _GET_NEXT(fd) fdtab[fd].fdcache_entry.next |
| 110 | #define _GET_PREV(fd) fdtab[fd].fdcache_entry.prev |
| 111 | |
| 112 | static inline void fd_add_to_fd_list(volatile struct fdlist *list, int fd) |
| 113 | { |
| 114 | int next; |
| 115 | int new; |
| 116 | int old; |
| 117 | int last; |
| 118 | |
| 119 | redo_next: |
| 120 | next = _GET_NEXT(fd); |
| 121 | /* |
| 122 | * Check that we're not already in the cache, and if not, lock us. |
| 123 | * <= -3 means not in the cache, -2 means locked, -1 means we're |
| 124 | * in the cache, and the last element, >= 0 gives the FD of the next |
| 125 | * in the cache. |
| 126 | */ |
| 127 | if (next >= -2) |
| 128 | goto done; |
| 129 | if (!HA_ATOMIC_CAS(&_GET_NEXT(fd), &next, -2)) |
| 130 | goto redo_next; |
| 131 | __ha_barrier_store(); |
| 132 | redo_last: |
| 133 | /* First, insert in the linked list */ |
| 134 | last = list->last; |
| 135 | old = -1; |
| 136 | new = fd; |
| 137 | if (unlikely(last == -1)) { |
| 138 | /* list is empty, try to add ourselves alone so that list->last=fd */ |
| 139 | |
| 140 | _GET_PREV(fd) = last; |
| 141 | |
| 142 | /* Make sure the "prev" store is visible before we update the last entry */ |
| 143 | __ha_barrier_store(); |
| 144 | if (unlikely(!HA_ATOMIC_CAS(&list->last, &old, new))) |
| 145 | goto redo_last; |
| 146 | |
| 147 | /* list->first was necessary -1, we're guaranteed to be alone here */ |
| 148 | list->first = fd; |
| 149 | |
| 150 | /* since we're alone at the end of the list and still locked(-2), |
| 151 | * we know noone tried to add past us. Mark the end of list. |
| 152 | */ |
| 153 | _GET_NEXT(fd) = -1; |
| 154 | goto done; /* We're done ! */ |
| 155 | } else { |
| 156 | /* non-empty list, add past the tail */ |
| 157 | do { |
| 158 | new = fd; |
| 159 | old = -1; |
| 160 | _GET_PREV(fd) = last; |
| 161 | |
| 162 | __ha_barrier_store(); |
| 163 | |
| 164 | /* adding ourselves past the last element |
| 165 | * The CAS will only succeed if its next is -1, |
| 166 | * which means it's in the cache, and the last element. |
| 167 | */ |
| 168 | if (likely(HA_ATOMIC_CAS(&_GET_NEXT(last), &old, new))) |
| 169 | break; |
| 170 | goto redo_last; |
| 171 | } while (1); |
| 172 | } |
| 173 | /* Then, update the last entry */ |
| 174 | redo_fd_cache: |
| 175 | last = list->last; |
| 176 | __ha_barrier_load(); |
| 177 | |
| 178 | if (unlikely(!HA_ATOMIC_CAS(&list->last, &last, fd))) |
| 179 | goto redo_fd_cache; |
| 180 | __ha_barrier_store(); |
| 181 | _GET_NEXT(fd) = -1; |
| 182 | __ha_barrier_store(); |
| 183 | done: |
| 184 | return; |
| 185 | } |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 186 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 187 | /* Allocates a cache entry for a file descriptor if it does not yet have one. |
| 188 | * This can be done at any time. |
| 189 | */ |
| 190 | static inline void fd_alloc_cache_entry(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 191 | { |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame^] | 192 | if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1))) |
| 193 | fd_add_to_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd); |
| 194 | else |
| 195 | fd_add_to_fd_list(&fd_cache, fd); |
| 196 | } |
| 197 | |
| 198 | static inline void fd_rm_from_fd_list(volatile struct fdlist *list, int fd) |
| 199 | { |
| 200 | #if defined(HA_HAVE_CAS_DW) || defined(HA_CAS_IS_8B) |
| 201 | volatile struct fdlist_entry cur_list, next_list; |
| 202 | #endif |
| 203 | int old; |
| 204 | int new = -2; |
| 205 | volatile int prev; |
| 206 | volatile int next; |
| 207 | int last; |
| 208 | |
| 209 | lock_self: |
| 210 | #if (defined(HA_CAS_IS_8B) || defined(HA_HAVE_CAS_DW)) |
| 211 | next_list.next = next_list.prev = -2; |
| 212 | cur_list.prev = _GET_PREV(fd); |
| 213 | cur_list.next = _GET_NEXT(fd); |
| 214 | /* First, attempt to lock our own entries */ |
| 215 | do { |
| 216 | /* The FD is not in the FD cache, give up */ |
| 217 | if (unlikely(cur_list.next <= -3)) |
| 218 | return; |
| 219 | if (unlikely(cur_list.prev == -2 || cur_list.next == -2)) |
| 220 | goto lock_self; |
| 221 | } while ( |
| 222 | #ifdef HA_CAS_IS_8B |
| 223 | unlikely(!HA_ATOMIC_CAS(((void **)(void *)&_GET_NEXT(fd)), ((void **)(void *)&cur_list), (*(void **)(void *)&next_list)))) |
| 224 | #else |
| 225 | unlikely(!__ha_cas_dw((void *)&_GET_NEXT(fd), (void *)&cur_list, (void *)&next_list))) |
| 226 | #endif |
| 227 | ; |
| 228 | next = cur_list.next; |
| 229 | prev = cur_list.prev; |
| 230 | |
| 231 | #else |
| 232 | lock_self_next: |
| 233 | next = _GET_NEXT(fd); |
| 234 | if (next == -2) |
| 235 | goto lock_self_next; |
| 236 | if (next <= -3) |
| 237 | goto done; |
| 238 | if (unlikely(!HA_ATOMIC_CAS(&_GET_NEXT(fd), &next, -2))) |
| 239 | goto lock_self_next; |
| 240 | lock_self_prev: |
| 241 | prev = _GET_PREV(fd); |
| 242 | if (prev == -2) |
| 243 | goto lock_self_prev; |
| 244 | if (unlikely(!HA_ATOMIC_CAS(&_GET_PREV(fd), &prev, -2))) |
| 245 | goto lock_self_prev; |
| 246 | #endif |
| 247 | __ha_barrier_store(); |
| 248 | |
| 249 | /* Now, lock the entries of our neighbours */ |
| 250 | if (likely(prev != -1)) { |
| 251 | redo_prev: |
| 252 | old = fd; |
| 253 | |
| 254 | if (unlikely(!HA_ATOMIC_CAS(&_GET_NEXT(prev), &old, new))) { |
| 255 | if (unlikely(old == -2)) { |
| 256 | /* Neighbour already locked, give up and |
| 257 | * retry again once he's done |
| 258 | */ |
| 259 | _GET_PREV(fd) = prev; |
| 260 | __ha_barrier_store(); |
| 261 | _GET_NEXT(fd) = next; |
| 262 | __ha_barrier_store(); |
| 263 | goto lock_self; |
| 264 | } |
| 265 | goto redo_prev; |
| 266 | } |
| 267 | } |
| 268 | if (likely(next != -1)) { |
| 269 | redo_next: |
| 270 | old = fd; |
| 271 | if (unlikely(!HA_ATOMIC_CAS(&_GET_PREV(next), &old, new))) { |
| 272 | if (unlikely(old == -2)) { |
| 273 | /* Neighbour already locked, give up and |
| 274 | * retry again once he's done |
| 275 | */ |
| 276 | if (prev != -1) { |
| 277 | _GET_NEXT(prev) = fd; |
| 278 | __ha_barrier_store(); |
| 279 | } |
| 280 | _GET_PREV(fd) = prev; |
| 281 | __ha_barrier_store(); |
| 282 | _GET_NEXT(fd) = next; |
| 283 | __ha_barrier_store(); |
| 284 | goto lock_self; |
| 285 | } |
| 286 | goto redo_next; |
| 287 | } |
| 288 | } |
| 289 | if (list->first == fd) |
| 290 | list->first = next; |
| 291 | __ha_barrier_store(); |
| 292 | last = list->last; |
| 293 | while (unlikely(last == fd && (!HA_ATOMIC_CAS(&list->last, &last, prev)))) |
| 294 | __ha_compiler_barrier(); |
| 295 | /* Make sure we let other threads know we're no longer in cache, |
| 296 | * before releasing our neighbours. |
| 297 | */ |
| 298 | __ha_barrier_store(); |
| 299 | if (likely(prev != -1)) |
| 300 | _GET_NEXT(prev) = next; |
| 301 | __ha_barrier_store(); |
| 302 | if (likely(next != -1)) |
| 303 | _GET_PREV(next) = prev; |
| 304 | __ha_barrier_store(); |
| 305 | /* Ok, now we're out of the fd cache */ |
| 306 | _GET_NEXT(fd) = -(next + 4); |
| 307 | __ha_barrier_store(); |
| 308 | done: |
| 309 | return; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame^] | 312 | #undef _GET_NEXT |
| 313 | #undef _GET_PREV |
| 314 | |
| 315 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 316 | /* 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^] | 317 | * last one. |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 318 | * If the fd has no entry assigned, return immediately. |
| 319 | */ |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 320 | static inline void fd_release_cache_entry(int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 321 | { |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame^] | 322 | if (!(fdtab[fd].thread_mask & (fdtab[fd].thread_mask - 1))) |
| 323 | fd_rm_from_fd_list(&fd_cache_local[my_ffsl(fdtab[fd].thread_mask) - 1], fd); |
| 324 | else |
| 325 | fd_rm_from_fd_list(&fd_cache, fd); |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 326 | } |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 327 | |
Willy Tarreau | 25002d2 | 2014-01-25 10:32:56 +0100 | [diff] [blame] | 328 | /* Computes the new polled status based on the active and ready statuses, for |
| 329 | * each direction. This is meant to be used by pollers while processing updates. |
| 330 | */ |
| 331 | static inline int fd_compute_new_polled_status(int state) |
| 332 | { |
| 333 | if (state & FD_EV_ACTIVE_R) { |
| 334 | if (!(state & FD_EV_READY_R)) |
| 335 | state |= FD_EV_POLLED_R; |
| 336 | } |
| 337 | else |
| 338 | state &= ~FD_EV_POLLED_R; |
| 339 | |
| 340 | if (state & FD_EV_ACTIVE_W) { |
| 341 | if (!(state & FD_EV_READY_W)) |
| 342 | state |= FD_EV_POLLED_W; |
| 343 | } |
| 344 | else |
| 345 | state &= ~FD_EV_POLLED_W; |
| 346 | |
| 347 | return state; |
| 348 | } |
| 349 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 350 | /* This function automatically enables/disables caching for an entry depending |
| 351 | * on its state, and also possibly creates an update entry so that the poller |
| 352 | * does its job as well. It is only called on state changes. |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 353 | */ |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 354 | static inline void fd_update_cache(int fd) |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 355 | { |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 356 | /* 3 states for each direction require a polling update */ |
| 357 | if ((fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_ACTIVE_R)) == FD_EV_POLLED_R || |
| 358 | (fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_READY_R | FD_EV_ACTIVE_R)) == FD_EV_ACTIVE_R || |
| 359 | (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_ACTIVE_W)) == FD_EV_POLLED_W || |
| 360 | (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_READY_W | FD_EV_ACTIVE_W)) == FD_EV_ACTIVE_W) |
| 361 | updt_fd_polling(fd); |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 362 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 363 | /* only READY and ACTIVE states (the two with both flags set) require a cache entry */ |
| 364 | if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) || |
| 365 | ((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] | 366 | fd_alloc_cache_entry(fd); |
| 367 | } |
| 368 | else { |
| 369 | fd_release_cache_entry(fd); |
| 370 | } |
| 371 | } |
| 372 | |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 373 | /* |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 374 | * returns the FD's recv state (FD_EV_*) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 375 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 376 | static inline int fd_recv_state(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 377 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 378 | return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 379 | } |
| 380 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 381 | /* |
| 382 | * returns true if the FD is active for recv |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 383 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 384 | static inline int fd_recv_active(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 385 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 386 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 387 | } |
| 388 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 389 | /* |
| 390 | * returns true if the FD is ready for recv |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 391 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 392 | static inline int fd_recv_ready(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 393 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 394 | return (unsigned)fdtab[fd].state & FD_EV_READY_R; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * returns true if the FD is polled for recv |
| 399 | */ |
| 400 | static inline int fd_recv_polled(const int fd) |
| 401 | { |
| 402 | return (unsigned)fdtab[fd].state & FD_EV_POLLED_R; |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * returns the FD's send state (FD_EV_*) |
| 407 | */ |
| 408 | static inline int fd_send_state(const int fd) |
| 409 | { |
| 410 | return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * returns true if the FD is active for send |
| 415 | */ |
| 416 | static inline int fd_send_active(const int fd) |
| 417 | { |
| 418 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 421 | /* |
| 422 | * returns true if the FD is ready for send |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 423 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 424 | static inline int fd_send_ready(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 425 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 426 | return (unsigned)fdtab[fd].state & FD_EV_READY_W; |
| 427 | } |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 428 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 429 | /* |
| 430 | * returns true if the FD is polled for send |
| 431 | */ |
| 432 | static inline int fd_send_polled(const int fd) |
| 433 | { |
| 434 | return (unsigned)fdtab[fd].state & FD_EV_POLLED_W; |
| 435 | } |
| 436 | |
Christopher Faulet | 8db2fdf | 2017-08-30 09:59:38 +0200 | [diff] [blame] | 437 | /* |
| 438 | * returns true if the FD is active for recv or send |
| 439 | */ |
| 440 | static inline int fd_active(const int fd) |
| 441 | { |
| 442 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW; |
| 443 | } |
| 444 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 445 | /* Disable processing recv events on fd <fd> */ |
| 446 | static inline void fd_stop_recv(int fd) |
| 447 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 448 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 449 | if (fd_recv_active(fd)) { |
| 450 | fdtab[fd].state &= ~FD_EV_ACTIVE_R; |
| 451 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 452 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 453 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 456 | /* Disable processing send events on fd <fd> */ |
| 457 | static inline void fd_stop_send(int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 458 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 459 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 460 | if (fd_send_active(fd)) { |
| 461 | fdtab[fd].state &= ~FD_EV_ACTIVE_W; |
| 462 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 463 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 464 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 467 | /* Disable processing of events on fd <fd> for both directions. */ |
| 468 | static inline void fd_stop_both(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 469 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 470 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 471 | if (fd_active(fd)) { |
| 472 | fdtab[fd].state &= ~FD_EV_ACTIVE_RW; |
| 473 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 474 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 475 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 478 | /* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */ |
| 479 | static inline void fd_cant_recv(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 480 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 481 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 482 | if (fd_recv_ready(fd)) { |
| 483 | fdtab[fd].state &= ~FD_EV_READY_R; |
| 484 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 485 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 486 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 487 | } |
| 488 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 489 | /* Report that FD <fd> can receive anymore without polling. */ |
| 490 | static inline void fd_may_recv(const int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 491 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 492 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 493 | if (!fd_recv_ready(fd)) { |
| 494 | fdtab[fd].state |= FD_EV_READY_R; |
| 495 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 496 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 497 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 498 | } |
| 499 | |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 500 | /* Disable readiness when polled. This is useful to interrupt reading when it |
| 501 | * is suspected that the end of data might have been reached (eg: short read). |
| 502 | * This can only be done using level-triggered pollers, so if any edge-triggered |
| 503 | * is ever implemented, a test will have to be added here. |
| 504 | */ |
| 505 | static inline void fd_done_recv(const int fd) |
| 506 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 507 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 508 | if (fd_recv_polled(fd) && fd_recv_ready(fd)) { |
| 509 | fdtab[fd].state &= ~FD_EV_READY_R; |
| 510 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 511 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 512 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 515 | /* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */ |
| 516 | static inline void fd_cant_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 517 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 518 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 519 | if (fd_send_ready(fd)) { |
| 520 | fdtab[fd].state &= ~FD_EV_READY_W; |
| 521 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 522 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 523 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 526 | /* Report that FD <fd> can send anymore without polling (EAGAIN detected). */ |
| 527 | static inline void fd_may_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 528 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 529 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 530 | if (!fd_send_ready(fd)) { |
| 531 | fdtab[fd].state |= FD_EV_READY_W; |
| 532 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 533 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 534 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 535 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 536 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 537 | /* Prepare FD <fd> to try to receive */ |
| 538 | static inline void fd_want_recv(int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 539 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 540 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 541 | if (!fd_recv_active(fd)) { |
| 542 | fdtab[fd].state |= FD_EV_ACTIVE_R; |
| 543 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 544 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 545 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 546 | } |
| 547 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 548 | /* Prepare FD <fd> to try to send */ |
| 549 | static inline void fd_want_send(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 550 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 551 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 552 | if (!fd_send_active(fd)) { |
| 553 | fdtab[fd].state |= FD_EV_ACTIVE_W; |
| 554 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 555 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 556 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 557 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 558 | |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 559 | /* Update events seen for FD <fd> and its state if needed. This should be called |
| 560 | * by the poller to set FD_POLL_* flags. */ |
| 561 | static inline void fd_update_events(int fd, int evts) |
| 562 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 563 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 564 | fdtab[fd].ev &= FD_POLL_STICKY; |
| 565 | fdtab[fd].ev |= evts; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 566 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 567 | |
| 568 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) |
| 569 | fd_may_recv(fd); |
| 570 | |
| 571 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) |
| 572 | fd_may_send(fd); |
| 573 | } |
| 574 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 575 | /* Prepares <fd> for being polled */ |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 576 | 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] | 577 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 578 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 579 | fdtab[fd].owner = owner; |
| 580 | fdtab[fd].iocb = iocb; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 581 | fdtab[fd].ev = 0; |
Willy Tarreau | ebc78d7 | 2018-01-20 23:53:50 +0100 | [diff] [blame] | 582 | fdtab[fd].update_mask &= ~tid_bit; |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 583 | fdtab[fd].linger_risk = 0; |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 584 | fdtab[fd].cloned = 0; |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 585 | fdtab[fd].thread_mask = thread_mask; |
Willy Tarreau | c9c8378 | 2018-01-17 18:44:46 +0100 | [diff] [blame] | 586 | /* note: do not reset polled_mask here as it indicates which poller |
| 587 | * still knows this FD from a possible previous round. |
| 588 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 589 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Willy Tarreau | 322e6c7 | 2018-01-25 16:37:04 +0100 | [diff] [blame] | 592 | /* These are replacements for FD_SET, FD_CLR, FD_ISSET, working on uints */ |
| 593 | static inline void hap_fd_set(int fd, unsigned int *evts) |
| 594 | { |
Willy Tarreau | 82b37d7 | 2018-01-25 16:59:09 +0100 | [diff] [blame] | 595 | 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] | 596 | } |
| 597 | |
| 598 | static inline void hap_fd_clr(int fd, unsigned int *evts) |
| 599 | { |
Willy Tarreau | 82b37d7 | 2018-01-25 16:59:09 +0100 | [diff] [blame] | 600 | 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] | 601 | } |
| 602 | |
| 603 | static inline unsigned int hap_fd_isset(int fd, unsigned int *evts) |
| 604 | { |
| 605 | return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1))); |
| 606 | } |
| 607 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 608 | |
| 609 | #endif /* _PROTO_FD_H */ |
| 610 | |
| 611 | /* |
| 612 | * Local variables: |
| 613 | * c-indent-level: 8 |
| 614 | * c-basic-offset: 8 |
| 615 | * End: |
| 616 | */ |