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> |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 158 | #include <proto/log.h> |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 159 | #include <proto/port_range.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 160 | |
| 161 | struct fdtab *fdtab = NULL; /* array of all the file descriptors */ |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 162 | unsigned long *polled_mask = NULL; /* Array for the polled_mask of each fd */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 163 | struct fdinfo *fdinfo = NULL; /* less-often used infos for file descriptors */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 164 | int totalconn; /* total # of terminated sessions */ |
| 165 | int actconn; /* # of active sessions */ |
| 166 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 167 | struct poller pollers[MAX_POLLERS]; |
| 168 | struct poller cur_poller; |
| 169 | int nbpollers = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 171 | volatile struct fdlist fd_cache ; // FD events cache |
| 172 | volatile struct fdlist fd_cache_local[MAX_THREADS]; // FD events local for each thread |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 173 | volatile struct fdlist update_list; // Global update list |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 174 | |
Christopher Faulet | 69553fe | 2018-01-15 11:57:03 +0100 | [diff] [blame] | 175 | unsigned long fd_cache_mask = 0; // Mask of threads with events in the cache |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 176 | |
| 177 | THREAD_LOCAL int *fd_updt = NULL; // FD updates list |
| 178 | THREAD_LOCAL int fd_nbupdt = 0; // number of updates in the list |
| 179 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 180 | #define _GET_NEXT(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->next |
| 181 | #define _GET_PREV(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->prev |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 182 | /* adds fd <fd> to fd list <list> if it was not yet in it */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 183 | void fd_add_to_fd_list(volatile struct fdlist *list, int fd, int off) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 184 | { |
| 185 | int next; |
| 186 | int new; |
| 187 | int old; |
| 188 | int last; |
| 189 | |
| 190 | redo_next: |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 191 | next = _GET_NEXT(fd, off); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 192 | /* Check that we're not already in the cache, and if not, lock us. */ |
| 193 | if (next >= -2) |
| 194 | goto done; |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 195 | if (!HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2)) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 196 | goto redo_next; |
| 197 | __ha_barrier_store(); |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 198 | |
| 199 | new = fd; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 200 | redo_last: |
| 201 | /* First, insert in the linked list */ |
| 202 | last = list->last; |
| 203 | old = -1; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 204 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 205 | _GET_PREV(fd, off) = -2; |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 206 | /* Make sure the "prev" store is visible before we update the last entry */ |
| 207 | __ha_barrier_store(); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 208 | |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 209 | if (unlikely(last == -1)) { |
| 210 | /* list is empty, try to add ourselves alone so that list->last=fd */ |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 211 | if (unlikely(!HA_ATOMIC_CAS(&list->last, &old, new))) |
| 212 | goto redo_last; |
| 213 | |
| 214 | /* list->first was necessary -1, we're guaranteed to be alone here */ |
| 215 | list->first = fd; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 216 | } else { |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 217 | /* adding ourselves past the last element |
| 218 | * The CAS will only succeed if its next is -1, |
| 219 | * which means it's in the cache, and the last element. |
| 220 | */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 221 | if (unlikely(!HA_ATOMIC_CAS(&_GET_NEXT(last, off), &old, new))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 222 | goto redo_last; |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 223 | |
| 224 | /* Then, update the last entry */ |
| 225 | list->last = fd; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 226 | } |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 227 | __ha_barrier_store(); |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 228 | /* since we're alone at the end of the list and still locked(-2), |
| 229 | * we know noone tried to add past us. Mark the end of list. |
| 230 | */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 231 | _GET_PREV(fd, off) = last; |
| 232 | _GET_NEXT(fd, off) = -1; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 233 | __ha_barrier_store(); |
| 234 | done: |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | /* removes fd <fd> from fd list <list> */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 239 | void fd_rm_from_fd_list(volatile struct fdlist *list, int fd, int off) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 240 | { |
| 241 | #if defined(HA_HAVE_CAS_DW) || defined(HA_CAS_IS_8B) |
| 242 | volatile struct fdlist_entry cur_list, next_list; |
| 243 | #endif |
| 244 | int old; |
| 245 | int new = -2; |
| 246 | int prev; |
| 247 | int next; |
| 248 | int last; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 249 | lock_self: |
| 250 | #if (defined(HA_CAS_IS_8B) || defined(HA_HAVE_CAS_DW)) |
| 251 | next_list.next = next_list.prev = -2; |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 252 | cur_list = *(volatile struct fdlist_entry *)(((char *)&fdtab[fd]) + off); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 253 | /* First, attempt to lock our own entries */ |
| 254 | do { |
| 255 | /* The FD is not in the FD cache, give up */ |
| 256 | if (unlikely(cur_list.next <= -3)) |
| 257 | return; |
| 258 | if (unlikely(cur_list.prev == -2 || cur_list.next == -2)) |
| 259 | goto lock_self; |
| 260 | } while ( |
| 261 | #ifdef HA_CAS_IS_8B |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 262 | unlikely(!HA_ATOMIC_CAS(((void **)(void *)&_GET_NEXT(fd, off)), ((void **)(void *)&cur_list), (*(void **)(void *)&next_list)))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 263 | #else |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 264 | unlikely(!__ha_cas_dw((void *)&_GET_NEXT(fd, off), (void *)&cur_list, (void *)&next_list))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 265 | #endif |
| 266 | ; |
| 267 | next = cur_list.next; |
| 268 | prev = cur_list.prev; |
| 269 | |
| 270 | #else |
| 271 | lock_self_next: |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 272 | next = ({ volatile int *next = &_GET_NEXT(fd, off); *next; }); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 273 | if (next == -2) |
| 274 | goto lock_self_next; |
| 275 | if (next <= -3) |
| 276 | goto done; |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 277 | if (unlikely(!HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 278 | goto lock_self_next; |
| 279 | lock_self_prev: |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 280 | prev = ({ volatile int *prev = &_GET_PREV(fd, off); *prev; }); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 281 | if (prev == -2) |
| 282 | goto lock_self_prev; |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 283 | if (unlikely(!HA_ATOMIC_CAS(&_GET_PREV(fd, off), &prev, -2))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 284 | goto lock_self_prev; |
| 285 | #endif |
| 286 | __ha_barrier_store(); |
| 287 | |
| 288 | /* Now, lock the entries of our neighbours */ |
| 289 | if (likely(prev != -1)) { |
| 290 | redo_prev: |
| 291 | old = fd; |
| 292 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 293 | if (unlikely(!HA_ATOMIC_CAS(&_GET_NEXT(prev, off), &old, new))) { |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 294 | if (unlikely(old == -2)) { |
| 295 | /* Neighbour already locked, give up and |
| 296 | * retry again once he's done |
| 297 | */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 298 | _GET_PREV(fd, off) = prev; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 299 | __ha_barrier_store(); |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 300 | _GET_NEXT(fd, off) = next; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 301 | __ha_barrier_store(); |
| 302 | goto lock_self; |
| 303 | } |
| 304 | goto redo_prev; |
| 305 | } |
| 306 | } |
| 307 | if (likely(next != -1)) { |
| 308 | redo_next: |
| 309 | old = fd; |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 310 | if (unlikely(!HA_ATOMIC_CAS(&_GET_PREV(next, off), &old, new))) { |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 311 | if (unlikely(old == -2)) { |
| 312 | /* Neighbour already locked, give up and |
| 313 | * retry again once he's done |
| 314 | */ |
| 315 | if (prev != -1) { |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 316 | _GET_NEXT(prev, off) = fd; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 317 | __ha_barrier_store(); |
| 318 | } |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 319 | _GET_PREV(fd, off) = prev; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 320 | __ha_barrier_store(); |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 321 | _GET_NEXT(fd, off) = next; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 322 | __ha_barrier_store(); |
| 323 | goto lock_self; |
| 324 | } |
| 325 | goto redo_next; |
| 326 | } |
| 327 | } |
| 328 | if (list->first == fd) |
| 329 | list->first = next; |
| 330 | __ha_barrier_store(); |
| 331 | last = list->last; |
| 332 | while (unlikely(last == fd && (!HA_ATOMIC_CAS(&list->last, &last, prev)))) |
| 333 | __ha_compiler_barrier(); |
| 334 | /* Make sure we let other threads know we're no longer in cache, |
| 335 | * before releasing our neighbours. |
| 336 | */ |
| 337 | __ha_barrier_store(); |
| 338 | if (likely(prev != -1)) |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 339 | _GET_NEXT(prev, off) = next; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 340 | __ha_barrier_store(); |
| 341 | if (likely(next != -1)) |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 342 | _GET_PREV(next, off) = prev; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 343 | __ha_barrier_store(); |
| 344 | /* Ok, now we're out of the fd cache */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 345 | _GET_NEXT(fd, off) = -(next + 4); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 346 | __ha_barrier_store(); |
| 347 | done: |
| 348 | return; |
| 349 | } |
| 350 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 351 | #undef _GET_NEXT |
| 352 | #undef _GET_PREV |
| 353 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 354 | /* Deletes an FD from the fdsets. |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 355 | * The file descriptor is also closed. |
| 356 | */ |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 357 | static void fd_dodelete(int fd, int do_close) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 358 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 359 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 360 | if (fdtab[fd].linger_risk) { |
| 361 | /* this is generally set when connecting to servers */ |
| 362 | setsockopt(fd, SOL_SOCKET, SO_LINGER, |
| 363 | (struct linger *) &nolinger, sizeof(struct linger)); |
| 364 | } |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 365 | if (cur_poller.clo) |
| 366 | cur_poller.clo(fd); |
| 367 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 368 | fd_release_cache_entry(fd); |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 369 | fdtab[fd].state = 0; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 370 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 371 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 372 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | db3b326 | 2012-07-05 23:19:22 +0200 | [diff] [blame] | 373 | fdtab[fd].owner = NULL; |
Willy Tarreau | ebc78d7 | 2018-01-20 23:53:50 +0100 | [diff] [blame] | 374 | fdtab[fd].update_mask &= ~tid_bit; |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 375 | fdtab[fd].thread_mask = 0; |
Willy Tarreau | c9c8378 | 2018-01-17 18:44:46 +0100 | [diff] [blame] | 376 | if (do_close) { |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 377 | polled_mask[fd] = 0; |
Christopher Faulet | d531f88 | 2017-06-01 16:55:03 +0200 | [diff] [blame] | 378 | close(fd); |
Willy Tarreau | c9c8378 | 2018-01-17 18:44:46 +0100 | [diff] [blame] | 379 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 380 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 381 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 382 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 383 | /* Deletes an FD from the fdsets. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 384 | * The file descriptor is also closed. |
| 385 | */ |
| 386 | void fd_delete(int fd) |
| 387 | { |
| 388 | fd_dodelete(fd, 1); |
| 389 | } |
| 390 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 391 | /* Deletes an FD from the fdsets. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 392 | * The file descriptor is kept open. |
| 393 | */ |
| 394 | void fd_remove(int fd) |
| 395 | { |
| 396 | fd_dodelete(fd, 0); |
| 397 | } |
| 398 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 399 | static inline void fdlist_process_cached_events(volatile struct fdlist *fdlist) |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 400 | { |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 401 | int fd, old_fd, e; |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 402 | |
Olivier Houchard | 1256836 | 2018-01-31 18:07:29 +0100 | [diff] [blame] | 403 | for (old_fd = fd = fdlist->first; fd != -1; fd = fdtab[fd].cache.next) { |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 404 | if (fd == -2) { |
| 405 | fd = old_fd; |
| 406 | continue; |
| 407 | } else if (fd <= -3) |
| 408 | fd = -fd - 4; |
| 409 | if (fd == -1) |
| 410 | break; |
| 411 | old_fd = fd; |
| 412 | if (!(fdtab[fd].thread_mask & tid_bit)) |
| 413 | continue; |
Olivier Houchard | 1256836 | 2018-01-31 18:07:29 +0100 | [diff] [blame] | 414 | if (fdtab[fd].cache.next < -3) |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 415 | continue; |
Christopher Faulet | 69553fe | 2018-01-15 11:57:03 +0100 | [diff] [blame] | 416 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 417 | HA_ATOMIC_OR(&fd_cache_mask, tid_bit); |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 418 | if (HA_SPIN_TRYLOCK(FD_LOCK, &fdtab[fd].lock)) { |
| 419 | activity[tid].fd_lock++; |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 420 | continue; |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 421 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 422 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 423 | e = fdtab[fd].state; |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 424 | fdtab[fd].ev &= FD_POLL_STICKY; |
| 425 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 426 | 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] | 427 | fdtab[fd].ev |= FD_POLL_IN; |
| 428 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 429 | 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] | 430 | fdtab[fd].ev |= FD_POLL_OUT; |
| 431 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 432 | if (fdtab[fd].iocb && fdtab[fd].owner && fdtab[fd].ev) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 433 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 434 | fdtab[fd].iocb(fd); |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 435 | } |
| 436 | else { |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 437 | fd_release_cache_entry(fd); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 438 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 439 | } |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 440 | } |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* Scan and process the cached events. This should be called right after |
| 444 | * the poller. The loop may cause new entries to be created, for example |
| 445 | * if a listener causes an accept() to initiate a new incoming connection |
| 446 | * wanting to attempt an recv(). |
| 447 | */ |
| 448 | void fd_process_cached_events() |
| 449 | { |
| 450 | HA_ATOMIC_AND(&fd_cache_mask, ~tid_bit); |
| 451 | fdlist_process_cached_events(&fd_cache_local[tid]); |
| 452 | fdlist_process_cached_events(&fd_cache); |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 453 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 454 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 455 | /* disable the specified poller */ |
| 456 | void disable_poller(const char *poller_name) |
| 457 | { |
| 458 | int p; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 459 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 460 | for (p = 0; p < nbpollers; p++) |
| 461 | if (strcmp(pollers[p].name, poller_name) == 0) |
| 462 | pollers[p].pref = 0; |
| 463 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 464 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 465 | /* Initialize the pollers per thread */ |
| 466 | static int init_pollers_per_thread() |
| 467 | { |
| 468 | if ((fd_updt = calloc(global.maxsock, sizeof(*fd_updt))) == NULL) |
| 469 | return 0; |
| 470 | return 1; |
| 471 | } |
| 472 | |
| 473 | /* Deinitialize the pollers per thread */ |
| 474 | static void deinit_pollers_per_thread() |
| 475 | { |
| 476 | free(fd_updt); |
| 477 | fd_updt = NULL; |
| 478 | } |
| 479 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 480 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 481 | * Initialize the pollers till the best one is found. |
| 482 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 483 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 484 | int init_pollers() |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 485 | { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 486 | int p; |
| 487 | struct poller *bp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 488 | |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 489 | if ((fdtab = calloc(global.maxsock, sizeof(struct fdtab))) == NULL) |
| 490 | goto fail_tab; |
| 491 | |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 492 | if ((polled_mask = calloc(global.maxsock, sizeof(unsigned long))) == NULL) |
| 493 | goto fail_polledmask; |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 494 | if ((fdinfo = calloc(global.maxsock, sizeof(struct fdinfo))) == NULL) |
| 495 | goto fail_info; |
| 496 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 497 | fd_cache.first = fd_cache.last = -1; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 498 | update_list.first = update_list.last = -1; |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 499 | hap_register_per_thread_init(init_pollers_per_thread); |
| 500 | hap_register_per_thread_deinit(deinit_pollers_per_thread); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 501 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 502 | for (p = 0; p < global.maxsock; p++) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 503 | HA_SPIN_INIT(&fdtab[p].lock); |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 504 | /* Mark the fd as out of the fd cache */ |
Olivier Houchard | 1256836 | 2018-01-31 18:07:29 +0100 | [diff] [blame] | 505 | fdtab[p].cache.next = -3; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 506 | fdtab[p].update.next = -3; |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 507 | } |
| 508 | for (p = 0; p < global.nbthread; p++) |
| 509 | fd_cache_local[p].first = fd_cache_local[p].last = -1; |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 510 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 511 | do { |
| 512 | bp = NULL; |
| 513 | for (p = 0; p < nbpollers; p++) |
| 514 | if (!bp || (pollers[p].pref > bp->pref)) |
| 515 | bp = &pollers[p]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 516 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 517 | if (!bp || bp->pref == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 518 | break; |
| 519 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 520 | if (bp->init(bp)) { |
| 521 | memcpy(&cur_poller, bp, sizeof(*bp)); |
| 522 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 523 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 524 | } while (!bp || bp->pref == 0); |
| 525 | return 0; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 526 | |
Willy Tarreau | 16f649c | 2014-01-25 19:10:48 +0100 | [diff] [blame] | 527 | fail_cache: |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 528 | free(fdinfo); |
| 529 | fail_info: |
| 530 | free(fdtab); |
| 531 | fail_tab: |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 532 | free(polled_mask); |
| 533 | fail_polledmask: |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 534 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 537 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 538 | * Deinitialize the pollers. |
| 539 | */ |
| 540 | void deinit_pollers() { |
| 541 | |
| 542 | struct poller *bp; |
| 543 | int p; |
| 544 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 545 | for (p = 0; p < global.maxsock; p++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 546 | HA_SPIN_DESTROY(&fdtab[p].lock); |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 547 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 548 | for (p = 0; p < nbpollers; p++) { |
| 549 | bp = &pollers[p]; |
| 550 | |
| 551 | if (bp && bp->pref) |
| 552 | bp->term(bp); |
| 553 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 554 | |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 555 | free(fdinfo); fdinfo = NULL; |
| 556 | free(fdtab); fdtab = NULL; |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 557 | free(polled_mask); polled_mask = NULL; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 561 | * Lists the known pollers on <out>. |
| 562 | * Should be performed only before initialization. |
| 563 | */ |
| 564 | int list_pollers(FILE *out) |
| 565 | { |
| 566 | int p; |
| 567 | int last, next; |
| 568 | int usable; |
| 569 | struct poller *bp; |
| 570 | |
| 571 | fprintf(out, "Available polling systems :\n"); |
| 572 | |
| 573 | usable = 0; |
| 574 | bp = NULL; |
| 575 | last = next = -1; |
| 576 | while (1) { |
| 577 | for (p = 0; p < nbpollers; p++) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 578 | if ((next < 0 || pollers[p].pref > next) |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 579 | && (last < 0 || pollers[p].pref < last)) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 580 | next = pollers[p].pref; |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 581 | if (!bp || (pollers[p].pref > bp->pref)) |
| 582 | bp = &pollers[p]; |
| 583 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | if (next == -1) |
| 587 | break; |
| 588 | |
| 589 | for (p = 0; p < nbpollers; p++) { |
| 590 | if (pollers[p].pref == next) { |
| 591 | fprintf(out, " %10s : ", pollers[p].name); |
| 592 | if (pollers[p].pref == 0) |
| 593 | fprintf(out, "disabled, "); |
| 594 | else |
| 595 | fprintf(out, "pref=%3d, ", pollers[p].pref); |
| 596 | if (pollers[p].test(&pollers[p])) { |
| 597 | fprintf(out, " test result OK"); |
| 598 | if (next > 0) |
| 599 | usable++; |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 600 | } else { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 601 | fprintf(out, " test result FAILED"); |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 602 | if (bp == &pollers[p]) |
| 603 | bp = NULL; |
| 604 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 605 | fprintf(out, "\n"); |
| 606 | } |
| 607 | } |
| 608 | last = next; |
| 609 | next = -1; |
| 610 | }; |
| 611 | fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none"); |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 617 | * to create initialize part of them again. Returns 0 in case of failure, |
| 618 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 619 | * the the current poller is destroyed and the caller is responsible for trying |
| 620 | * another one by calling init_pollers() again. |
| 621 | */ |
| 622 | int fork_poller() |
| 623 | { |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 624 | int fd; |
Willy Tarreau | ce036bc | 2018-01-29 14:58:02 +0100 | [diff] [blame] | 625 | for (fd = 0; fd < global.maxsock; fd++) { |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 626 | if (fdtab[fd].owner) { |
| 627 | fdtab[fd].cloned = 1; |
| 628 | } |
| 629 | } |
| 630 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 631 | if (cur_poller.fork) { |
| 632 | if (cur_poller.fork(&cur_poller)) |
| 633 | return 1; |
| 634 | cur_poller.term(&cur_poller); |
| 635 | return 0; |
| 636 | } |
| 637 | return 1; |
| 638 | } |
| 639 | |
| 640 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 641 | * Local variables: |
| 642 | * c-indent-level: 8 |
| 643 | * c-basic-offset: 8 |
| 644 | * End: |
| 645 | */ |