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 | |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 36 | extern unsigned int *fd_cache; // FD events cache |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 37 | extern int fd_cache_num; // number of events in the cache |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 38 | |
| 39 | extern THREAD_LOCAL int *fd_updt; // FD updates list |
| 40 | extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list |
| 41 | |
| 42 | #ifdef USE_THREAD |
| 43 | HA_SPINLOCK_T fdtab_lock; /* global lock to protect fdtab array */ |
| 44 | HA_RWLOCK_T fdcache_lock; /* global lock to protect fd_cache array */ |
| 45 | HA_SPINLOCK_T poll_lock; /* global lock to protect poll info */ |
| 46 | #endif |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 47 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 49 | * The file descriptor is also closed. |
| 50 | */ |
| 51 | void fd_delete(int fd); |
| 52 | |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 53 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 54 | * The file descriptor is kept open. |
| 55 | */ |
| 56 | void fd_remove(int fd); |
| 57 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 58 | /* disable the specified poller */ |
| 59 | void disable_poller(const char *poller_name); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 61 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 62 | * Initialize the pollers till the best one is found. |
| 63 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 64 | * The pollers register themselves just before main() is called. |
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 | int init_pollers(); |
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 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 69 | * Deinitialize the pollers. |
| 70 | */ |
| 71 | void deinit_pollers(); |
| 72 | |
| 73 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 74 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 75 | * to create initialize part of them again. Returns 0 in case of failure, |
| 76 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 77 | * the the current poller is destroyed and the caller is responsible for trying |
| 78 | * another one by calling init_pollers() again. |
| 79 | */ |
| 80 | int fork_poller(); |
| 81 | |
| 82 | /* |
| 83 | * Lists the known pollers on <out>. |
| 84 | * Should be performed only before initialization. |
| 85 | */ |
| 86 | int list_pollers(FILE *out); |
| 87 | |
| 88 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 89 | * Runs the polling loop |
| 90 | */ |
| 91 | void run_poller(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 92 | |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 93 | /* Scan and process the cached events. This should be called right after |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 94 | * the poller. |
| 95 | */ |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 96 | void fd_process_cached_events(); |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 97 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 98 | /* Mark fd <fd> as updated for polling and allocate an entry in the update list |
| 99 | * 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] | 100 | */ |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 101 | static inline void updt_fd_polling(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 102 | { |
| 103 | if (fdtab[fd].updated) |
| 104 | /* already scheduled for update */ |
| 105 | return; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 106 | fdtab[fd].updated = 1; |
Willy Tarreau | 4a29144 | 2012-12-13 23:34:18 +0100 | [diff] [blame] | 107 | fd_updt[fd_nbupdt++] = fd; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 111 | /* Allocates a cache entry for a file descriptor if it does not yet have one. |
| 112 | * This can be done at any time. |
| 113 | */ |
| 114 | static inline void fd_alloc_cache_entry(const int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 115 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 116 | RWLOCK_WRLOCK(FDCACHE_LOCK, &fdcache_lock); |
Willy Tarreau | 15a4dec | 2014-01-20 11:09:39 +0100 | [diff] [blame] | 117 | if (fdtab[fd].cache) |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 118 | goto end; |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 119 | fd_cache_num++; |
| 120 | fdtab[fd].cache = fd_cache_num; |
| 121 | fd_cache[fd_cache_num-1] = fd; |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 122 | end: |
| 123 | RWLOCK_WRUNLOCK(FDCACHE_LOCK, &fdcache_lock); |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 126 | /* Removes entry used by fd <fd> from the FD cache and replaces it with the |
| 127 | * last one. The fdtab.cache is adjusted to match the back reference if needed. |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 128 | * If the fd has no entry assigned, return immediately. |
| 129 | */ |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 130 | static inline void fd_release_cache_entry(int fd) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 131 | { |
| 132 | unsigned int pos; |
| 133 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 134 | RWLOCK_WRLOCK(FDCACHE_LOCK, &fdcache_lock); |
Willy Tarreau | 15a4dec | 2014-01-20 11:09:39 +0100 | [diff] [blame] | 135 | pos = fdtab[fd].cache; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 136 | if (!pos) |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 137 | goto end; |
Willy Tarreau | 15a4dec | 2014-01-20 11:09:39 +0100 | [diff] [blame] | 138 | fdtab[fd].cache = 0; |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 139 | fd_cache_num--; |
| 140 | if (likely(pos <= fd_cache_num)) { |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 141 | /* was not the last entry */ |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 142 | fd = fd_cache[fd_cache_num]; |
| 143 | fd_cache[pos - 1] = fd; |
Willy Tarreau | 15a4dec | 2014-01-20 11:09:39 +0100 | [diff] [blame] | 144 | fdtab[fd].cache = pos; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 145 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 146 | end: |
| 147 | RWLOCK_WRUNLOCK(FDCACHE_LOCK, &fdcache_lock); |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 148 | } |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 149 | |
Willy Tarreau | 25002d2 | 2014-01-25 10:32:56 +0100 | [diff] [blame] | 150 | /* Computes the new polled status based on the active and ready statuses, for |
| 151 | * each direction. This is meant to be used by pollers while processing updates. |
| 152 | */ |
| 153 | static inline int fd_compute_new_polled_status(int state) |
| 154 | { |
| 155 | if (state & FD_EV_ACTIVE_R) { |
| 156 | if (!(state & FD_EV_READY_R)) |
| 157 | state |= FD_EV_POLLED_R; |
| 158 | } |
| 159 | else |
| 160 | state &= ~FD_EV_POLLED_R; |
| 161 | |
| 162 | if (state & FD_EV_ACTIVE_W) { |
| 163 | if (!(state & FD_EV_READY_W)) |
| 164 | state |= FD_EV_POLLED_W; |
| 165 | } |
| 166 | else |
| 167 | state &= ~FD_EV_POLLED_W; |
| 168 | |
| 169 | return state; |
| 170 | } |
| 171 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 172 | /* This function automatically enables/disables caching for an entry depending |
| 173 | * on its state, and also possibly creates an update entry so that the poller |
| 174 | * does its job as well. It is only called on state changes. |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 175 | */ |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 176 | static inline void fd_update_cache(int fd) |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 177 | { |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 178 | /* 3 states for each direction require a polling update */ |
| 179 | if ((fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_ACTIVE_R)) == FD_EV_POLLED_R || |
| 180 | (fdtab[fd].state & (FD_EV_POLLED_R | FD_EV_READY_R | FD_EV_ACTIVE_R)) == FD_EV_ACTIVE_R || |
| 181 | (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_ACTIVE_W)) == FD_EV_POLLED_W || |
| 182 | (fdtab[fd].state & (FD_EV_POLLED_W | FD_EV_READY_W | FD_EV_ACTIVE_W)) == FD_EV_ACTIVE_W) |
| 183 | updt_fd_polling(fd); |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 184 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 185 | /* only READY and ACTIVE states (the two with both flags set) require a cache entry */ |
| 186 | if (((fdtab[fd].state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) || |
| 187 | ((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] | 188 | fd_alloc_cache_entry(fd); |
| 189 | } |
| 190 | else { |
| 191 | fd_release_cache_entry(fd); |
| 192 | } |
| 193 | } |
| 194 | |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 195 | /* |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 196 | * returns the FD's recv state (FD_EV_*) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 197 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 198 | static inline int fd_recv_state(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 199 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 200 | return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 203 | /* |
| 204 | * returns true if the FD is active for recv |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 205 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 206 | static inline int fd_recv_active(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 207 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 208 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 211 | /* |
| 212 | * returns true if the FD is ready for recv |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 213 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 214 | static inline int fd_recv_ready(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 215 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 216 | return (unsigned)fdtab[fd].state & FD_EV_READY_R; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * returns true if the FD is polled for recv |
| 221 | */ |
| 222 | static inline int fd_recv_polled(const int fd) |
| 223 | { |
| 224 | return (unsigned)fdtab[fd].state & FD_EV_POLLED_R; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * returns the FD's send state (FD_EV_*) |
| 229 | */ |
| 230 | static inline int fd_send_state(const int fd) |
| 231 | { |
| 232 | return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * returns true if the FD is active for send |
| 237 | */ |
| 238 | static inline int fd_send_active(const int fd) |
| 239 | { |
| 240 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 243 | /* |
| 244 | * returns true if the FD is ready for send |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 245 | */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 246 | static inline int fd_send_ready(const int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 247 | { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 248 | return (unsigned)fdtab[fd].state & FD_EV_READY_W; |
| 249 | } |
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 | /* |
| 252 | * returns true if the FD is polled for send |
| 253 | */ |
| 254 | static inline int fd_send_polled(const int fd) |
| 255 | { |
| 256 | return (unsigned)fdtab[fd].state & FD_EV_POLLED_W; |
| 257 | } |
| 258 | |
Christopher Faulet | 8db2fdf | 2017-08-30 09:59:38 +0200 | [diff] [blame] | 259 | /* |
| 260 | * returns true if the FD is active for recv or send |
| 261 | */ |
| 262 | static inline int fd_active(const int fd) |
| 263 | { |
| 264 | return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_RW; |
| 265 | } |
| 266 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 267 | /* Disable processing recv events on fd <fd> */ |
| 268 | static inline void fd_stop_recv(int fd) |
| 269 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 270 | SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 271 | if (fd_recv_active(fd)) { |
| 272 | fdtab[fd].state &= ~FD_EV_ACTIVE_R; |
| 273 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 274 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 275 | SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 278 | /* Disable processing send events on fd <fd> */ |
| 279 | static inline void fd_stop_send(int fd) |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 280 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 281 | SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 282 | if (fd_send_active(fd)) { |
| 283 | fdtab[fd].state &= ~FD_EV_ACTIVE_W; |
| 284 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 285 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 286 | SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 289 | /* Disable processing of events on fd <fd> for both directions. */ |
| 290 | static inline void fd_stop_both(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 291 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 292 | SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 293 | if (fd_active(fd)) { |
| 294 | fdtab[fd].state &= ~FD_EV_ACTIVE_RW; |
| 295 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 296 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 297 | SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 300 | /* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */ |
| 301 | static inline void fd_cant_recv(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 302 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 303 | SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 304 | if (fd_recv_ready(fd)) { |
| 305 | fdtab[fd].state &= ~FD_EV_READY_R; |
| 306 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 307 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 308 | 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 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 314 | SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 315 | if (!fd_recv_ready(fd)) { |
| 316 | fdtab[fd].state |= FD_EV_READY_R; |
| 317 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 318 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 319 | 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 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 329 | SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 330 | if (fd_recv_polled(fd) && fd_recv_ready(fd)) { |
| 331 | fdtab[fd].state &= ~FD_EV_READY_R; |
| 332 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 333 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 334 | SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 335 | } |
| 336 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 337 | /* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */ |
| 338 | static inline void fd_cant_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 339 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 340 | SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 341 | if (fd_send_ready(fd)) { |
| 342 | fdtab[fd].state &= ~FD_EV_READY_W; |
| 343 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 344 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 345 | SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 348 | /* Report that FD <fd> can send anymore without polling (EAGAIN detected). */ |
| 349 | static inline void fd_may_send(const int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 350 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 351 | SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 352 | if (!fd_send_ready(fd)) { |
| 353 | fdtab[fd].state |= FD_EV_READY_W; |
| 354 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 355 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 356 | SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 357 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 358 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 359 | /* Prepare FD <fd> to try to receive */ |
| 360 | static inline void fd_want_recv(int fd) |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 361 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 362 | SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 363 | if (!fd_recv_active(fd)) { |
| 364 | fdtab[fd].state |= FD_EV_ACTIVE_R; |
| 365 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 366 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 367 | SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 368 | } |
| 369 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 370 | /* Prepare FD <fd> to try to send */ |
| 371 | static inline void fd_want_send(int fd) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 372 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 373 | SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d82b180 | 2017-08-30 10:07:47 +0200 | [diff] [blame] | 374 | if (!fd_send_active(fd)) { |
| 375 | fdtab[fd].state |= FD_EV_ACTIVE_W; |
| 376 | fd_update_cache(fd); /* need an update entry to change the state */ |
| 377 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 378 | 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 | |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 381 | /* Update events seen for FD <fd> and its state if needed. This should be called |
| 382 | * by the poller to set FD_POLL_* flags. */ |
| 383 | static inline void fd_update_events(int fd, int evts) |
| 384 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 385 | SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 386 | fdtab[fd].ev &= FD_POLL_STICKY; |
| 387 | fdtab[fd].ev |= evts; |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 388 | SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | 21e9267 | 2017-08-30 10:30:04 +0200 | [diff] [blame] | 389 | |
| 390 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) |
| 391 | fd_may_recv(fd); |
| 392 | |
| 393 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) |
| 394 | fd_may_send(fd); |
| 395 | } |
| 396 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 397 | /* Prepares <fd> for being polled */ |
Christopher Faulet | 36716a7 | 2017-05-30 11:07:16 +0200 | [diff] [blame] | 398 | static inline void fd_insert(int fd, unsigned long thread_mask) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 399 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 400 | SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 401 | fdtab[fd].ev = 0; |
Willy Tarreau | 037d2c1 | 2012-11-06 02:34:46 +0100 | [diff] [blame] | 402 | fdtab[fd].new = 1; |
Christopher Faulet | d531f88 | 2017-06-01 16:55:03 +0200 | [diff] [blame] | 403 | fdtab[fd].updated = 0; |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 404 | fdtab[fd].linger_risk = 0; |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 405 | fdtab[fd].cloned = 0; |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 406 | fdtab[fd].cache = 0; |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 407 | fdtab[fd].thread_mask = thread_mask; |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 408 | SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
| 409 | |
| 410 | SPIN_LOCK(FDTAB_LOCK, &fdtab_lock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | if (fd + 1 > maxfd) |
| 412 | maxfd = fd + 1; |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 413 | SPIN_UNLOCK(FDTAB_LOCK, &fdtab_lock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | |
| 417 | #endif /* _PROTO_FD_H */ |
| 418 | |
| 419 | /* |
| 420 | * Local variables: |
| 421 | * c-indent-level: 8 |
| 422 | * c-basic-offset: 8 |
| 423 | * End: |
| 424 | */ |