Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * File descriptors management functions. |
| 3 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 4 | * Copyright 2000-2014 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 11 | * This code implements an events cache for file descriptors. It remembers the |
| 12 | * readiness of a file descriptor after a return from poll() and the fact that |
| 13 | * an I/O attempt failed on EAGAIN. Events in the cache which are still marked |
| 14 | * ready and active are processed just as if they were reported by poll(). |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 15 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 16 | * This serves multiple purposes. First, it significantly improves performance |
| 17 | * by avoiding to subscribe to polling unless absolutely necessary, so most |
| 18 | * events are processed without polling at all, especially send() which |
| 19 | * benefits from the socket buffers. Second, it is the only way to support |
| 20 | * edge-triggered pollers (eg: EPOLL_ET). And third, it enables I/O operations |
| 21 | * that are backed by invisible buffers. For example, SSL is able to read a |
| 22 | * whole socket buffer and not deliver it to the application buffer because |
| 23 | * it's full. Unfortunately, it won't be reported by a poller anymore until |
| 24 | * some new activity happens. The only way to call it again thus is to keep |
| 25 | * this readiness information in the cache and to access it without polling |
| 26 | * once the FD is enabled again. |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 27 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 28 | * One interesting feature of the cache is that it maintains the principle |
| 29 | * of speculative I/O introduced in haproxy 1.3 : the first time an event is |
| 30 | * enabled, the FD is considered as ready so that the I/O attempt is performed |
| 31 | * via the cache without polling. And the polling happens only when EAGAIN is |
| 32 | * first met. This avoids polling for HTTP requests, especially when the |
| 33 | * defer-accept mode is used. It also avoids polling for sending short data |
| 34 | * such as requests to servers or short responses to clients. |
| 35 | * |
| 36 | * The cache consists in a list of active events and a list of updates. |
| 37 | * Active events are events that are expected to come and that we must report |
| 38 | * to the application until it asks to stop or asks to poll. Updates are new |
| 39 | * requests for changing an FD state. Updates are the only way to create new |
| 40 | * events. This is important because it means that the number of cached events |
| 41 | * cannot increase between updates and will only grow one at a time while |
| 42 | * processing updates. All updates must always be processed, though events |
| 43 | * might be processed by small batches if required. |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 44 | * |
| 45 | * There is no direct link between the FD and the updates list. There is only a |
| 46 | * bit in the fdtab[] to indicate than a file descriptor is already present in |
| 47 | * the updates list. Once an fd is present in the updates list, it will have to |
| 48 | * be considered even if its changes are reverted in the middle or if the fd is |
| 49 | * replaced. |
| 50 | * |
| 51 | * It is important to understand that as long as all expected events are |
| 52 | * processed, they might starve the polled events, especially because polled |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 53 | * I/O starvation quickly induces more cached I/O. One solution to this |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 54 | * consists in only processing a part of the events at once, but one drawback |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 55 | * is that unhandled events will still wake the poller up. Using an edge- |
| 56 | * triggered poller such as EPOLL_ET will solve this issue though. |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 57 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 58 | * Since we do not want to scan all the FD list to find cached I/O events, |
| 59 | * we store them in a list consisting in a linear array holding only the FD |
| 60 | * indexes right now. Note that a closed FD cannot exist in the cache, because |
| 61 | * it is closed by fd_delete() which in turn calls fd_release_cache_entry() |
| 62 | * which always removes it from the list. |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 63 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 64 | * The FD array has to hold a back reference to the cache. This reference is |
| 65 | * always valid unless the FD is not in the cache and is not updated, in which |
| 66 | * case the reference points to index 0. |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 67 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 68 | * The event state for an FD, as found in fdtab[].state, is maintained for each |
| 69 | * direction. The state field is built this way, with R bits in the low nibble |
| 70 | * and W bits in the high nibble for ease of access and debugging : |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 71 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 72 | * 7 6 5 4 3 2 1 0 |
| 73 | * [ 0 | PW | RW | AW | 0 | PR | RR | AR ] |
| 74 | * |
| 75 | * A* = active *R = read |
| 76 | * P* = polled *W = write |
| 77 | * R* = ready |
| 78 | * |
| 79 | * An FD is marked "active" when there is a desire to use it. |
| 80 | * An FD is marked "polled" when it is registered in the polling. |
| 81 | * An FD is marked "ready" when it has not faced a new EAGAIN since last wake-up |
| 82 | * (it is a cache of the last EAGAIN regardless of polling changes). |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 83 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 84 | * We have 8 possible states for each direction based on these 3 flags : |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 85 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 86 | * +---+---+---+----------+---------------------------------------------+ |
| 87 | * | P | R | A | State | Description | |
| 88 | * +---+---+---+----------+---------------------------------------------+ |
| 89 | * | 0 | 0 | 0 | DISABLED | No activity desired, not ready. | |
| 90 | * | 0 | 0 | 1 | MUSTPOLL | Activity desired via polling. | |
| 91 | * | 0 | 1 | 0 | STOPPED | End of activity without polling. | |
| 92 | * | 0 | 1 | 1 | ACTIVE | Activity desired without polling. | |
| 93 | * | 1 | 0 | 0 | ABORT | Aborted poll(). Not frequently seen. | |
| 94 | * | 1 | 0 | 1 | POLLED | FD is being polled. | |
| 95 | * | 1 | 1 | 0 | PAUSED | FD was paused while ready (eg: buffer full) | |
| 96 | * | 1 | 1 | 1 | READY | FD was marked ready by poll() | |
| 97 | * +---+---+---+----------+---------------------------------------------+ |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 98 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 99 | * The transitions are pretty simple : |
| 100 | * - fd_want_*() : set flag A |
| 101 | * - fd_stop_*() : clear flag A |
| 102 | * - fd_cant_*() : clear flag R (when facing EAGAIN) |
| 103 | * - fd_may_*() : set flag R (upon return from poll()) |
| 104 | * - sync() : if (A) { if (!R) P := 1 } else { P := 0 } |
| 105 | * |
| 106 | * The PAUSED, ABORT and MUSTPOLL states are transient for level-trigerred |
| 107 | * pollers and are fixed by the sync() which happens at the beginning of the |
| 108 | * poller. For event-triggered pollers, only the MUSTPOLL state will be |
| 109 | * transient and ABORT will lead to PAUSED. The ACTIVE state is the only stable |
| 110 | * one which has P != A. |
| 111 | * |
| 112 | * The READY state is a bit special as activity on the FD might be notified |
| 113 | * both by the poller or by the cache. But it is needed for some multi-layer |
| 114 | * protocols (eg: SSL) where connection activity is not 100% linked to FD |
| 115 | * activity. Also some pollers might prefer to implement it as ACTIVE if |
| 116 | * enabling/disabling the FD is cheap. The READY and ACTIVE states are the |
| 117 | * two states for which a cache entry is allocated. |
| 118 | * |
| 119 | * The state transitions look like the diagram below. Only the 4 right states |
| 120 | * have polling enabled : |
| 121 | * |
| 122 | * (POLLED=0) (POLLED=1) |
| 123 | * |
| 124 | * +----------+ sync +-------+ |
| 125 | * | DISABLED | <----- | ABORT | (READY=0, ACTIVE=0) |
| 126 | * +----------+ +-------+ |
| 127 | * clr | ^ set | ^ |
| 128 | * | | | | |
| 129 | * v | set v | clr |
| 130 | * +----------+ sync +--------+ |
| 131 | * | MUSTPOLL | -----> | POLLED | (READY=0, ACTIVE=1) |
| 132 | * +----------+ +--------+ |
| 133 | * ^ poll | ^ |
| 134 | * | | | |
| 135 | * | EAGAIN v | EAGAIN |
| 136 | * +--------+ +-------+ |
| 137 | * | ACTIVE | | READY | (READY=1, ACTIVE=1) |
| 138 | * +--------+ +-------+ |
| 139 | * clr | ^ set | ^ |
| 140 | * | | | | |
| 141 | * v | set v | clr |
| 142 | * +---------+ sync +--------+ |
| 143 | * | STOPPED | <------ | PAUSED | (READY=1, ACTIVE=0) |
| 144 | * +---------+ +--------+ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 145 | */ |
| 146 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 147 | #include <stdio.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 148 | #include <string.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 149 | #include <unistd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 150 | #include <sys/types.h> |
| 151 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 152 | #include <common/compat.h> |
| 153 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 154 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 155 | #include <types/global.h> |
| 156 | |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 157 | #include <proto/fd.h> |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 158 | #include <proto/port_range.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 159 | |
| 160 | struct fdtab *fdtab = NULL; /* array of all the file descriptors */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 161 | struct fdinfo *fdinfo = NULL; /* less-often used infos for file descriptors */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 162 | int maxfd; /* # of the highest fd + 1 */ |
| 163 | int totalconn; /* total # of terminated sessions */ |
| 164 | int actconn; /* # of active sessions */ |
| 165 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 166 | struct poller pollers[MAX_POLLERS]; |
| 167 | struct poller cur_poller; |
| 168 | int nbpollers = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 169 | |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 170 | unsigned int *fd_cache = NULL; // FD events cache |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 171 | unsigned int *fd_updt = NULL; // FD updates list |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 172 | int fd_cache_num = 0; // number of events in the cache |
| 173 | int fd_nbupdt = 0; // number of updates in the list |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 174 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 175 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 176 | * The file descriptor is also closed. |
| 177 | */ |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 178 | static void fd_dodelete(int fd, int do_close) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 179 | { |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 180 | if (fdtab[fd].linger_risk) { |
| 181 | /* this is generally set when connecting to servers */ |
| 182 | setsockopt(fd, SOL_SOCKET, SO_LINGER, |
| 183 | (struct linger *) &nolinger, sizeof(struct linger)); |
| 184 | } |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 185 | if (cur_poller.clo) |
| 186 | cur_poller.clo(fd); |
| 187 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 188 | fd_release_cache_entry(fd); |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 189 | fdtab[fd].state = 0; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 190 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 191 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 192 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | db3b326 | 2012-07-05 23:19:22 +0200 | [diff] [blame] | 193 | fdtab[fd].owner = NULL; |
Christopher Faulet | d531f88 | 2017-06-01 16:55:03 +0200 | [diff] [blame] | 194 | fdtab[fd].updated = 0; |
Willy Tarreau | 1720abd | 2012-11-11 17:08:32 +0100 | [diff] [blame] | 195 | fdtab[fd].new = 0; |
Christopher Faulet | d531f88 | 2017-06-01 16:55:03 +0200 | [diff] [blame] | 196 | if (do_close) |
| 197 | close(fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 198 | |
Willy Tarreau | db3b326 | 2012-07-05 23:19:22 +0200 | [diff] [blame] | 199 | while ((maxfd-1 >= 0) && !fdtab[maxfd-1].owner) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 200 | maxfd--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 201 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 202 | |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 203 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 204 | * The file descriptor is also closed. |
| 205 | */ |
| 206 | void fd_delete(int fd) |
| 207 | { |
| 208 | fd_dodelete(fd, 1); |
| 209 | } |
| 210 | |
| 211 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 212 | * The file descriptor is kept open. |
| 213 | */ |
| 214 | void fd_remove(int fd) |
| 215 | { |
| 216 | fd_dodelete(fd, 0); |
| 217 | } |
| 218 | |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 219 | /* Scan and process the cached events. This should be called right after |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 220 | * the poller. The loop may cause new entries to be created, for example |
| 221 | * if a listener causes an accept() to initiate a new incoming connection |
| 222 | * wanting to attempt an recv(). |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 223 | */ |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 224 | void fd_process_cached_events() |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 225 | { |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 226 | int fd, entry, e; |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 227 | |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 228 | for (entry = 0; entry < fd_cache_num; ) { |
| 229 | fd = fd_cache[entry]; |
Willy Tarreau | 69a41fa | 2014-01-20 11:02:59 +0100 | [diff] [blame] | 230 | e = fdtab[fd].state; |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 231 | |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 232 | fdtab[fd].ev &= FD_POLL_STICKY; |
| 233 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 234 | if ((e & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 235 | fdtab[fd].ev |= FD_POLL_IN; |
| 236 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 237 | if ((e & (FD_EV_READY_W | FD_EV_ACTIVE_W)) == (FD_EV_READY_W | FD_EV_ACTIVE_W)) |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 238 | fdtab[fd].ev |= FD_POLL_OUT; |
| 239 | |
| 240 | if (fdtab[fd].iocb && fdtab[fd].owner && fdtab[fd].ev) |
| 241 | fdtab[fd].iocb(fd); |
Willy Tarreau | fa7fc95 | 2014-01-20 20:18:59 +0100 | [diff] [blame] | 242 | else |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 243 | fd_release_cache_entry(fd); |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 244 | |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 245 | /* If the fd was removed from the cache, it has been |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 246 | * replaced by the next one that we don't want to skip ! |
| 247 | */ |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 248 | if (entry < fd_cache_num && fd_cache[entry] != fd) |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 249 | continue; |
Willy Tarreau | 033cd9d | 2014-01-25 19:24:15 +0100 | [diff] [blame] | 250 | entry++; |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 251 | } |
| 252 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 253 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 254 | /* disable the specified poller */ |
| 255 | void disable_poller(const char *poller_name) |
| 256 | { |
| 257 | int p; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 258 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 259 | for (p = 0; p < nbpollers; p++) |
| 260 | if (strcmp(pollers[p].name, poller_name) == 0) |
| 261 | pollers[p].pref = 0; |
| 262 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 263 | |
| 264 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 265 | * Initialize the pollers till the best one is found. |
| 266 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 267 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 268 | int init_pollers() |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 269 | { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 270 | int p; |
| 271 | struct poller *bp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 272 | |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 273 | if ((fdtab = calloc(global.maxsock, sizeof(struct fdtab))) == NULL) |
| 274 | goto fail_tab; |
| 275 | |
| 276 | if ((fdinfo = calloc(global.maxsock, sizeof(struct fdinfo))) == NULL) |
| 277 | goto fail_info; |
| 278 | |
| 279 | if ((fd_cache = calloc(global.maxsock, sizeof(*fd_cache))) == NULL) |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 280 | goto fail_cache; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 281 | |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 282 | if ((fd_updt = calloc(global.maxsock, sizeof(*fd_updt))) == NULL) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 283 | goto fail_updt; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 284 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 285 | do { |
| 286 | bp = NULL; |
| 287 | for (p = 0; p < nbpollers; p++) |
| 288 | if (!bp || (pollers[p].pref > bp->pref)) |
| 289 | bp = &pollers[p]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 290 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 291 | if (!bp || bp->pref == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 292 | break; |
| 293 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 294 | if (bp->init(bp)) { |
| 295 | memcpy(&cur_poller, bp, sizeof(*bp)); |
| 296 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 297 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 298 | } while (!bp || bp->pref == 0); |
| 299 | return 0; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 300 | |
| 301 | fail_updt: |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 302 | free(fd_cache); |
| 303 | fail_cache: |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 304 | free(fdinfo); |
| 305 | fail_info: |
| 306 | free(fdtab); |
| 307 | fail_tab: |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 308 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 311 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 312 | * Deinitialize the pollers. |
| 313 | */ |
| 314 | void deinit_pollers() { |
| 315 | |
| 316 | struct poller *bp; |
| 317 | int p; |
| 318 | |
| 319 | for (p = 0; p < nbpollers; p++) { |
| 320 | bp = &pollers[p]; |
| 321 | |
| 322 | if (bp && bp->pref) |
| 323 | bp->term(bp); |
| 324 | } |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 325 | free(fd_updt); fd_updt = NULL; |
| 326 | free(fd_cache); fd_cache = NULL; |
| 327 | free(fdinfo); fdinfo = NULL; |
| 328 | free(fdtab); fdtab = NULL; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 332 | * Lists the known pollers on <out>. |
| 333 | * Should be performed only before initialization. |
| 334 | */ |
| 335 | int list_pollers(FILE *out) |
| 336 | { |
| 337 | int p; |
| 338 | int last, next; |
| 339 | int usable; |
| 340 | struct poller *bp; |
| 341 | |
| 342 | fprintf(out, "Available polling systems :\n"); |
| 343 | |
| 344 | usable = 0; |
| 345 | bp = NULL; |
| 346 | last = next = -1; |
| 347 | while (1) { |
| 348 | for (p = 0; p < nbpollers; p++) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 349 | if ((next < 0 || pollers[p].pref > next) |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 350 | && (last < 0 || pollers[p].pref < last)) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 351 | next = pollers[p].pref; |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 352 | if (!bp || (pollers[p].pref > bp->pref)) |
| 353 | bp = &pollers[p]; |
| 354 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | if (next == -1) |
| 358 | break; |
| 359 | |
| 360 | for (p = 0; p < nbpollers; p++) { |
| 361 | if (pollers[p].pref == next) { |
| 362 | fprintf(out, " %10s : ", pollers[p].name); |
| 363 | if (pollers[p].pref == 0) |
| 364 | fprintf(out, "disabled, "); |
| 365 | else |
| 366 | fprintf(out, "pref=%3d, ", pollers[p].pref); |
| 367 | if (pollers[p].test(&pollers[p])) { |
| 368 | fprintf(out, " test result OK"); |
| 369 | if (next > 0) |
| 370 | usable++; |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 371 | } else { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 372 | fprintf(out, " test result FAILED"); |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 373 | if (bp == &pollers[p]) |
| 374 | bp = NULL; |
| 375 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 376 | fprintf(out, "\n"); |
| 377 | } |
| 378 | } |
| 379 | last = next; |
| 380 | next = -1; |
| 381 | }; |
| 382 | fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none"); |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 388 | * to create initialize part of them again. Returns 0 in case of failure, |
| 389 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 390 | * the the current poller is destroyed and the caller is responsible for trying |
| 391 | * another one by calling init_pollers() again. |
| 392 | */ |
| 393 | int fork_poller() |
| 394 | { |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 395 | int fd; |
| 396 | for (fd = 0; fd <= maxfd; fd++) { |
| 397 | if (fdtab[fd].owner) { |
| 398 | fdtab[fd].cloned = 1; |
| 399 | } |
| 400 | } |
| 401 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 402 | if (cur_poller.fork) { |
| 403 | if (cur_poller.fork(&cur_poller)) |
| 404 | return 1; |
| 405 | cur_poller.term(&cur_poller); |
| 406 | return 0; |
| 407 | } |
| 408 | return 1; |
| 409 | } |
| 410 | |
| 411 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 412 | * Local variables: |
| 413 | * c-indent-level: 8 |
| 414 | * c-basic-offset: 8 |
| 415 | * End: |
| 416 | */ |