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 | * |
| 5 | * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu |
| 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> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <types/fd.h> |
| 32 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 33 | /* public variables */ |
| 34 | extern int fd_nbspec; // number of speculative events in the list |
| 35 | extern int fd_nbupdt; // number of updates in the list |
| 36 | extern unsigned int *fd_spec; // speculative I/O list |
| 37 | extern unsigned int *fd_updt; // FD updates list |
| 38 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 40 | * The file descriptor is also closed. |
| 41 | */ |
| 42 | void fd_delete(int fd); |
| 43 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 44 | /* disable the specified poller */ |
| 45 | void disable_poller(const char *poller_name); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 47 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 48 | * Initialize the pollers till the best one is found. |
| 49 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 50 | * The pollers register themselves just before main() is called. |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 51 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 52 | int init_pollers(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 54 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 55 | * Deinitialize the pollers. |
| 56 | */ |
| 57 | void deinit_pollers(); |
| 58 | |
| 59 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 60 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 61 | * to create initialize part of them again. Returns 0 in case of failure, |
| 62 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 63 | * the the current poller is destroyed and the caller is responsible for trying |
| 64 | * another one by calling init_pollers() again. |
| 65 | */ |
| 66 | int fork_poller(); |
| 67 | |
| 68 | /* |
| 69 | * Lists the known pollers on <out>. |
| 70 | * Should be performed only before initialization. |
| 71 | */ |
| 72 | int list_pollers(FILE *out); |
| 73 | |
| 74 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 75 | * Runs the polling loop |
| 76 | */ |
| 77 | void run_poller(); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 78 | |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 79 | /* Scan and process the speculative events. This should be called right after |
| 80 | * the poller. |
| 81 | */ |
| 82 | void fd_process_spec_events(); |
| 83 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 84 | /* Mark fd <fd> as updated and allocate an entry in the update list for this if |
| 85 | * it was not already there. This can be done at any time. |
| 86 | */ |
| 87 | static inline void updt_fd(const int fd) |
| 88 | { |
| 89 | if (fdtab[fd].updated) |
| 90 | /* already scheduled for update */ |
| 91 | return; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 92 | fdtab[fd].updated = 1; |
Willy Tarreau | 4a29144 | 2012-12-13 23:34:18 +0100 | [diff] [blame] | 93 | fd_updt[fd_nbupdt++] = fd; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
| 97 | /* allocate an entry for a speculative event. This can be done at any time. */ |
| 98 | static inline void alloc_spec_entry(const int fd) |
| 99 | { |
| 100 | if (fdtab[fd].spec_p) |
| 101 | /* FD already in speculative I/O list */ |
| 102 | return; |
Willy Tarreau | 4a29144 | 2012-12-13 23:34:18 +0100 | [diff] [blame] | 103 | fd_nbspec++; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 104 | fdtab[fd].spec_p = fd_nbspec; |
Willy Tarreau | 4a29144 | 2012-12-13 23:34:18 +0100 | [diff] [blame] | 105 | fd_spec[fd_nbspec-1] = fd; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* Removes entry used by fd <fd> from the spec list and replaces it with the |
| 109 | * last one. The fdtab.spec is adjusted to match the back reference if needed. |
| 110 | * If the fd has no entry assigned, return immediately. |
| 111 | */ |
| 112 | static inline void release_spec_entry(int fd) |
| 113 | { |
| 114 | unsigned int pos; |
| 115 | |
| 116 | pos = fdtab[fd].spec_p; |
| 117 | if (!pos) |
| 118 | return; |
| 119 | fdtab[fd].spec_p = 0; |
| 120 | fd_nbspec--; |
Willy Tarreau | 4a29144 | 2012-12-13 23:34:18 +0100 | [diff] [blame] | 121 | if (likely(pos <= fd_nbspec)) { |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 122 | /* was not the last entry */ |
| 123 | fd = fd_spec[fd_nbspec]; |
| 124 | fd_spec[pos - 1] = fd; |
| 125 | fdtab[fd].spec_p = pos; |
| 126 | } |
| 127 | } |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 128 | |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 129 | /* |
| 130 | * Returns non-zero if <fd> is already monitored for events in direction <dir>. |
| 131 | */ |
| 132 | static inline int fd_ev_is_set(const int fd, int dir) |
| 133 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 134 | return ((unsigned)fdtab[fd].spec_e >> dir) & FD_EV_STATUS; |
| 135 | } |
| 136 | |
| 137 | /* Disable processing of events on fd <fd> for direction <dir>. Note: this |
| 138 | * function was optimized to be used with a constant for <dir>. |
| 139 | */ |
| 140 | static inline void fd_ev_clr(const int fd, int dir) |
| 141 | { |
| 142 | unsigned int i = ((unsigned int)fdtab[fd].spec_e) & (FD_EV_STATUS << dir); |
| 143 | if (i == 0) |
| 144 | return; /* already disabled */ |
| 145 | fdtab[fd].spec_e ^= i; |
| 146 | updt_fd(fd); /* need an update entry to change the state */ |
| 147 | } |
| 148 | |
| 149 | /* Enable polling for events on fd <fd> for direction <dir>. Note: this |
| 150 | * function was optimized to be used with a constant for <dir>. |
| 151 | */ |
| 152 | static inline void fd_ev_wai(const int fd, int dir) |
| 153 | { |
| 154 | unsigned int i = ((unsigned int)fdtab[fd].spec_e) & (FD_EV_STATUS << dir); |
| 155 | if (i == (FD_EV_POLLED << dir)) |
| 156 | return; /* already in desired state */ |
| 157 | fdtab[fd].spec_e ^= i ^ (FD_EV_POLLED << dir); |
| 158 | updt_fd(fd); /* need an update entry to change the state */ |
| 159 | } |
| 160 | |
| 161 | /* Enable processing of events on fd <fd> for direction <dir>. Note: this |
| 162 | * function was optimized to be used with a constant for <dir>. |
| 163 | */ |
| 164 | static inline void fd_ev_set(int fd, int dir) |
| 165 | { |
| 166 | unsigned int i = ((unsigned int)fdtab[fd].spec_e) & (FD_EV_STATUS << dir); |
| 167 | |
| 168 | /* note that we don't care about disabling the polled state when |
| 169 | * enabling the active state, since it brings no benefit but costs |
| 170 | * some syscalls. |
| 171 | */ |
| 172 | if (i & (FD_EV_ACTIVE << dir)) |
| 173 | return; /* already in desired state */ |
Willy Tarreau | 1208266 | 2013-12-27 20:11:50 +0100 | [diff] [blame] | 174 | |
| 175 | /* If we're touching an FD which is still being polled, and was |
| 176 | * recently disabled, we re-enable polling in order not to perform |
| 177 | * a syscall dance and to avoid a missed speculative event. |
| 178 | */ |
| 179 | if ((((unsigned int)fdtab[fd].spec_e) >> 4) & (FD_EV_POLLED << dir)) |
| 180 | fdtab[fd].spec_e ^= i ^ (FD_EV_POLLED << dir); |
| 181 | else |
| 182 | fdtab[fd].spec_e |= (FD_EV_ACTIVE << dir); |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 183 | updt_fd(fd); /* need an update entry to change the state */ |
| 184 | } |
| 185 | |
| 186 | /* Disable processing of events on fd <fd> for both directions. */ |
| 187 | static inline void fd_ev_rem(const int fd) |
| 188 | { |
| 189 | unsigned int i = ((unsigned int)fdtab[fd].spec_e) & FD_EV_CURR_MASK; |
| 190 | if (i == 0) |
| 191 | return; /* already disabled */ |
| 192 | fdtab[fd].spec_e ^= i; |
| 193 | updt_fd(fd); /* need an update entry to change the state */ |
| 194 | } |
| 195 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 196 | /* event manipulation primitives for use by I/O callbacks */ |
| 197 | static inline void fd_want_recv(int fd) |
| 198 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 199 | return fd_ev_set(fd, DIR_RD); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static inline void fd_stop_recv(int fd) |
| 203 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 204 | return fd_ev_clr(fd, DIR_RD); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 207 | static inline void fd_poll_recv(int fd) |
| 208 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 209 | return fd_ev_wai(fd, DIR_RD); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 212 | static inline void fd_want_send(int fd) |
| 213 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 214 | return fd_ev_set(fd, DIR_WR); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static inline void fd_stop_send(int fd) |
| 218 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 219 | return fd_ev_clr(fd, DIR_WR); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 220 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 221 | |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 222 | static inline void fd_poll_send(int fd) |
| 223 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 224 | return fd_ev_wai(fd, DIR_WR); |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 227 | static inline void fd_stop_both(int fd) |
| 228 | { |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 229 | return fd_ev_rem(fd); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 230 | } |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 231 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 232 | /* Prepares <fd> for being polled */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 233 | static inline void fd_insert(int fd) |
| 234 | { |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 235 | fdtab[fd].ev = 0; |
Willy Tarreau | 037d2c1 | 2012-11-06 02:34:46 +0100 | [diff] [blame] | 236 | fdtab[fd].new = 1; |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 237 | fdtab[fd].linger_risk = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 238 | if (fd + 1 > maxfd) |
| 239 | maxfd = fd + 1; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | #endif /* _PROTO_FD_H */ |
| 244 | |
| 245 | /* |
| 246 | * Local variables: |
| 247 | * c-indent-level: 8 |
| 248 | * c-basic-offset: 8 |
| 249 | * End: |
| 250 | */ |