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> |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 150 | #include <fcntl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 151 | #include <sys/types.h> |
Willy Tarreau | 2d7f81b | 2019-02-21 22:19:17 +0100 | [diff] [blame] | 152 | #include <sys/resource.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 153 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 154 | #if defined(USE_POLL) |
Willy Tarreau | 9188ac6 | 2019-02-21 22:12:47 +0100 | [diff] [blame] | 155 | #include <poll.h> |
| 156 | #include <errno.h> |
| 157 | #endif |
| 158 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 159 | #include <common/compat.h> |
| 160 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 161 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 162 | #include <types/global.h> |
| 163 | |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 164 | #include <proto/fd.h> |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 165 | #include <proto/log.h> |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 166 | #include <proto/port_range.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 167 | |
| 168 | struct fdtab *fdtab = NULL; /* array of all the file descriptors */ |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 169 | 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] | 170 | struct fdinfo *fdinfo = NULL; /* less-often used infos for file descriptors */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 171 | int totalconn; /* total # of terminated sessions */ |
| 172 | int actconn; /* # of active sessions */ |
| 173 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 174 | struct poller pollers[MAX_POLLERS]; |
| 175 | struct poller cur_poller; |
| 176 | int nbpollers = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 177 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 178 | volatile struct fdlist fd_cache ; // FD events cache |
| 179 | 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] | 180 | volatile struct fdlist update_list; // Global update list |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 181 | |
Christopher Faulet | 69553fe | 2018-01-15 11:57:03 +0100 | [diff] [blame] | 182 | 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] | 183 | |
| 184 | THREAD_LOCAL int *fd_updt = NULL; // FD updates list |
| 185 | THREAD_LOCAL int fd_nbupdt = 0; // number of updates in the list |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 186 | THREAD_LOCAL int poller_rd_pipe = -1; // Pipe to wake the thread |
| 187 | int poller_wr_pipe[MAX_THREADS]; // Pipe to wake the threads |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 188 | |
Olivier Houchard | 7c49d2e | 2019-04-16 18:37:05 +0200 | [diff] [blame] | 189 | volatile int ha_used_fds = 0; // Number of FD we're currently using |
| 190 | |
Willy Tarreau | 1c90f32 | 2019-12-20 07:20:00 +0100 | [diff] [blame] | 191 | #define _GET_NEXT(fd, off) ((volatile struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->next |
| 192 | #define _GET_PREV(fd, off) ((volatile struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->prev |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 193 | /* 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] | 194 | 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] | 195 | { |
| 196 | int next; |
| 197 | int new; |
| 198 | int old; |
| 199 | int last; |
| 200 | |
| 201 | redo_next: |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 202 | next = _GET_NEXT(fd, off); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 203 | /* Check that we're not already in the cache, and if not, lock us. */ |
Olivier Houchard | a29cdde | 2019-12-19 18:33:08 +0100 | [diff] [blame] | 204 | if (next > -2) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 205 | goto done; |
Olivier Houchard | a29cdde | 2019-12-19 18:33:08 +0100 | [diff] [blame] | 206 | if (next == -2) |
| 207 | goto redo_next; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 208 | if (!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2)) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 209 | goto redo_next; |
Olivier Houchard | d2b5d16 | 2019-03-08 13:47:21 +0100 | [diff] [blame] | 210 | __ha_barrier_atomic_store(); |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 211 | |
| 212 | new = fd; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 213 | redo_last: |
| 214 | /* First, insert in the linked list */ |
| 215 | last = list->last; |
| 216 | old = -1; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 217 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 218 | _GET_PREV(fd, off) = -2; |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 219 | /* Make sure the "prev" store is visible before we update the last entry */ |
| 220 | __ha_barrier_store(); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 221 | |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 222 | if (unlikely(last == -1)) { |
| 223 | /* list is empty, try to add ourselves alone so that list->last=fd */ |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 224 | if (unlikely(!_HA_ATOMIC_CAS(&list->last, &old, new))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 225 | goto redo_last; |
| 226 | |
| 227 | /* list->first was necessary -1, we're guaranteed to be alone here */ |
| 228 | list->first = fd; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 229 | } else { |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 230 | /* adding ourselves past the last element |
| 231 | * The CAS will only succeed if its next is -1, |
| 232 | * which means it's in the cache, and the last element. |
| 233 | */ |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 234 | if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(last, off), &old, new))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 235 | goto redo_last; |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 236 | |
| 237 | /* Then, update the last entry */ |
| 238 | list->last = fd; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 239 | } |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 240 | __ha_barrier_store(); |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 241 | /* since we're alone at the end of the list and still locked(-2), |
| 242 | * we know noone tried to add past us. Mark the end of list. |
| 243 | */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 244 | _GET_PREV(fd, off) = last; |
| 245 | _GET_NEXT(fd, off) = -1; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 246 | __ha_barrier_store(); |
| 247 | done: |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | /* removes fd <fd> from fd list <list> */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 252 | 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] | 253 | { |
| 254 | #if defined(HA_HAVE_CAS_DW) || defined(HA_CAS_IS_8B) |
| 255 | volatile struct fdlist_entry cur_list, next_list; |
| 256 | #endif |
| 257 | int old; |
| 258 | int new = -2; |
| 259 | int prev; |
| 260 | int next; |
| 261 | int last; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 262 | lock_self: |
| 263 | #if (defined(HA_CAS_IS_8B) || defined(HA_HAVE_CAS_DW)) |
| 264 | next_list.next = next_list.prev = -2; |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 265 | cur_list = *(volatile struct fdlist_entry *)(((char *)&fdtab[fd]) + off); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 266 | /* First, attempt to lock our own entries */ |
| 267 | do { |
| 268 | /* The FD is not in the FD cache, give up */ |
| 269 | if (unlikely(cur_list.next <= -3)) |
| 270 | return; |
| 271 | if (unlikely(cur_list.prev == -2 || cur_list.next == -2)) |
| 272 | goto lock_self; |
| 273 | } while ( |
| 274 | #ifdef HA_CAS_IS_8B |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 275 | 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] | 276 | #else |
Willy Tarreau | c3b5958 | 2019-05-27 17:37:20 +0200 | [diff] [blame] | 277 | unlikely(!_HA_ATOMIC_DWCAS(((void *)&_GET_NEXT(fd, off)), ((void *)&cur_list), ((void *)&next_list)))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 278 | #endif |
| 279 | ; |
| 280 | next = cur_list.next; |
| 281 | prev = cur_list.prev; |
| 282 | |
| 283 | #else |
| 284 | lock_self_next: |
Willy Tarreau | 1c90f32 | 2019-12-20 07:20:00 +0100 | [diff] [blame] | 285 | next = _GET_NEXT(fd, off); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 286 | if (next == -2) |
| 287 | goto lock_self_next; |
| 288 | if (next <= -3) |
| 289 | goto done; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 290 | if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 291 | goto lock_self_next; |
| 292 | lock_self_prev: |
Willy Tarreau | 1c90f32 | 2019-12-20 07:20:00 +0100 | [diff] [blame] | 293 | prev = _GET_PREV(fd, off); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 294 | if (prev == -2) |
| 295 | goto lock_self_prev; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 296 | if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(fd, off), &prev, -2))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 297 | goto lock_self_prev; |
| 298 | #endif |
Olivier Houchard | d2b5d16 | 2019-03-08 13:47:21 +0100 | [diff] [blame] | 299 | __ha_barrier_atomic_store(); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 300 | |
| 301 | /* Now, lock the entries of our neighbours */ |
| 302 | if (likely(prev != -1)) { |
| 303 | redo_prev: |
| 304 | old = fd; |
| 305 | |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 306 | if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(prev, off), &old, new))) { |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 307 | if (unlikely(old == -2)) { |
| 308 | /* Neighbour already locked, give up and |
| 309 | * retry again once he's done |
| 310 | */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 311 | _GET_PREV(fd, off) = prev; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 312 | __ha_barrier_store(); |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 313 | _GET_NEXT(fd, off) = next; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 314 | __ha_barrier_store(); |
| 315 | goto lock_self; |
| 316 | } |
| 317 | goto redo_prev; |
| 318 | } |
| 319 | } |
| 320 | if (likely(next != -1)) { |
| 321 | redo_next: |
| 322 | old = fd; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 323 | if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(next, off), &old, new))) { |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 324 | if (unlikely(old == -2)) { |
| 325 | /* Neighbour already locked, give up and |
| 326 | * retry again once he's done |
| 327 | */ |
| 328 | if (prev != -1) { |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 329 | _GET_NEXT(prev, off) = fd; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 330 | __ha_barrier_store(); |
| 331 | } |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 332 | _GET_PREV(fd, off) = prev; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 333 | __ha_barrier_store(); |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 334 | _GET_NEXT(fd, off) = next; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 335 | __ha_barrier_store(); |
| 336 | goto lock_self; |
| 337 | } |
| 338 | goto redo_next; |
| 339 | } |
| 340 | } |
| 341 | if (list->first == fd) |
| 342 | list->first = next; |
| 343 | __ha_barrier_store(); |
| 344 | last = list->last; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 345 | while (unlikely(last == fd && (!_HA_ATOMIC_CAS(&list->last, &last, prev)))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 346 | __ha_compiler_barrier(); |
| 347 | /* Make sure we let other threads know we're no longer in cache, |
| 348 | * before releasing our neighbours. |
| 349 | */ |
| 350 | __ha_barrier_store(); |
| 351 | if (likely(prev != -1)) |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 352 | _GET_NEXT(prev, off) = next; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 353 | __ha_barrier_store(); |
| 354 | if (likely(next != -1)) |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 355 | _GET_PREV(next, off) = prev; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 356 | __ha_barrier_store(); |
| 357 | /* Ok, now we're out of the fd cache */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 358 | _GET_NEXT(fd, off) = -(next + 4); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 359 | __ha_barrier_store(); |
| 360 | done: |
| 361 | return; |
| 362 | } |
| 363 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 364 | #undef _GET_NEXT |
| 365 | #undef _GET_PREV |
| 366 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 367 | /* Deletes an FD from the fdsets. |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 368 | * The file descriptor is also closed. |
| 369 | */ |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 370 | static void fd_dodelete(int fd, int do_close) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 371 | { |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 372 | unsigned long locked = atleast2(fdtab[fd].thread_mask); |
| 373 | |
| 374 | if (locked) |
| 375 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 376 | if (fdtab[fd].linger_risk) { |
| 377 | /* this is generally set when connecting to servers */ |
| 378 | setsockopt(fd, SOL_SOCKET, SO_LINGER, |
| 379 | (struct linger *) &nolinger, sizeof(struct linger)); |
| 380 | } |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 381 | if (cur_poller.clo) |
| 382 | cur_poller.clo(fd); |
Olivier Houchard | 15a2e68 | 2019-08-05 18:51:52 +0200 | [diff] [blame] | 383 | polled_mask[fd] = 0; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 384 | |
Willy Tarreau | 899d957 | 2014-01-25 19:20:35 +0100 | [diff] [blame] | 385 | fd_release_cache_entry(fd); |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 386 | fdtab[fd].state = 0; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 387 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 388 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 389 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | db3b326 | 2012-07-05 23:19:22 +0200 | [diff] [blame] | 390 | fdtab[fd].owner = NULL; |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 391 | fdtab[fd].thread_mask = 0; |
Willy Tarreau | c9c8378 | 2018-01-17 18:44:46 +0100 | [diff] [blame] | 392 | if (do_close) { |
Christopher Faulet | d531f88 | 2017-06-01 16:55:03 +0200 | [diff] [blame] | 393 | close(fd); |
Olivier Houchard | 7c49d2e | 2019-04-16 18:37:05 +0200 | [diff] [blame] | 394 | _HA_ATOMIC_SUB(&ha_used_fds, 1); |
Willy Tarreau | c9c8378 | 2018-01-17 18:44:46 +0100 | [diff] [blame] | 395 | } |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 396 | if (locked) |
| 397 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 398 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 399 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 400 | /* Deletes an FD from the fdsets. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 401 | * The file descriptor is also closed. |
| 402 | */ |
| 403 | void fd_delete(int fd) |
| 404 | { |
| 405 | fd_dodelete(fd, 1); |
| 406 | } |
| 407 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 408 | /* Deletes an FD from the fdsets. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 409 | * The file descriptor is kept open. |
| 410 | */ |
| 411 | void fd_remove(int fd) |
| 412 | { |
| 413 | fd_dodelete(fd, 0); |
| 414 | } |
| 415 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 416 | static inline void fdlist_process_cached_events(volatile struct fdlist *fdlist) |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 417 | { |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 418 | int fd, old_fd, e; |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 419 | unsigned long locked; |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 420 | |
Olivier Houchard | 1256836 | 2018-01-31 18:07:29 +0100 | [diff] [blame] | 421 | 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] | 422 | if (fd == -2) { |
| 423 | fd = old_fd; |
| 424 | continue; |
| 425 | } else if (fd <= -3) |
| 426 | fd = -fd - 4; |
| 427 | if (fd == -1) |
| 428 | break; |
| 429 | old_fd = fd; |
| 430 | if (!(fdtab[fd].thread_mask & tid_bit)) |
| 431 | continue; |
Olivier Houchard | 1256836 | 2018-01-31 18:07:29 +0100 | [diff] [blame] | 432 | if (fdtab[fd].cache.next < -3) |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 433 | continue; |
Christopher Faulet | 69553fe | 2018-01-15 11:57:03 +0100 | [diff] [blame] | 434 | |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 435 | _HA_ATOMIC_OR(&fd_cache_mask, tid_bit); |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 436 | locked = atleast2(fdtab[fd].thread_mask); |
| 437 | if (locked && HA_SPIN_TRYLOCK(FD_LOCK, &fdtab[fd].lock)) { |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 438 | activity[tid].fd_lock++; |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 439 | continue; |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 440 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 441 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 442 | e = fdtab[fd].state; |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 443 | fdtab[fd].ev &= FD_POLL_STICKY; |
| 444 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 445 | 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] | 446 | fdtab[fd].ev |= FD_POLL_IN; |
| 447 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 448 | 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] | 449 | fdtab[fd].ev |= FD_POLL_OUT; |
| 450 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 451 | if (fdtab[fd].iocb && fdtab[fd].owner && fdtab[fd].ev) { |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 452 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 453 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 454 | fdtab[fd].iocb(fd); |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 455 | } |
| 456 | else { |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 457 | fd_release_cache_entry(fd); |
Richard Russo | bc9d984 | 2019-02-20 12:43:45 -0800 | [diff] [blame] | 458 | if (locked) |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 459 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 460 | } |
Willy Tarreau | e1aed24 | 2019-08-16 16:06:14 +0200 | [diff] [blame] | 461 | ti->flags &= ~TI_FL_STUCK; // this thread is still running |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 462 | } |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* Scan and process the cached events. This should be called right after |
| 466 | * the poller. The loop may cause new entries to be created, for example |
| 467 | * if a listener causes an accept() to initiate a new incoming connection |
| 468 | * wanting to attempt an recv(). |
| 469 | */ |
| 470 | void fd_process_cached_events() |
| 471 | { |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 472 | _HA_ATOMIC_AND(&fd_cache_mask, ~tid_bit); |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 473 | fdlist_process_cached_events(&fd_cache_local[tid]); |
| 474 | fdlist_process_cached_events(&fd_cache); |
Willy Tarreau | 09f2456 | 2012-11-11 16:43:45 +0100 | [diff] [blame] | 475 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 476 | |
Olivier Houchard | 2292edf | 2019-02-25 14:26:54 +0100 | [diff] [blame] | 477 | #if defined(USE_CLOSEFROM) |
| 478 | void my_closefrom(int start) |
| 479 | { |
| 480 | closefrom(start); |
| 481 | } |
| 482 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 483 | #elif defined(USE_POLL) |
Willy Tarreau | 9188ac6 | 2019-02-21 22:12:47 +0100 | [diff] [blame] | 484 | /* This is a portable implementation of closefrom(). It closes all open file |
| 485 | * descriptors starting at <start> and above. It relies on the fact that poll() |
| 486 | * will return POLLNVAL for each invalid (hence close) file descriptor passed |
| 487 | * in argument in order to skip them. It acts with batches of FDs and will |
| 488 | * typically perform one poll() call per 1024 FDs so the overhead is low in |
| 489 | * case all FDs have to be closed. |
| 490 | */ |
| 491 | void my_closefrom(int start) |
| 492 | { |
| 493 | struct pollfd poll_events[1024]; |
| 494 | struct rlimit limit; |
| 495 | int nbfds, fd, ret, idx; |
| 496 | int step, next; |
| 497 | |
| 498 | if (getrlimit(RLIMIT_NOFILE, &limit) == 0) |
| 499 | step = nbfds = limit.rlim_cur; |
| 500 | else |
| 501 | step = nbfds = 0; |
| 502 | |
| 503 | if (nbfds <= 0) { |
| 504 | /* set safe limit */ |
| 505 | nbfds = 1024; |
| 506 | step = 256; |
| 507 | } |
| 508 | |
| 509 | if (step > sizeof(poll_events) / sizeof(poll_events[0])) |
| 510 | step = sizeof(poll_events) / sizeof(poll_events[0]); |
| 511 | |
| 512 | while (start < nbfds) { |
| 513 | next = (start / step + 1) * step; |
| 514 | |
| 515 | for (fd = start; fd < next && fd < nbfds; fd++) { |
| 516 | poll_events[fd - start].fd = fd; |
| 517 | poll_events[fd - start].events = 0; |
| 518 | } |
| 519 | |
| 520 | do { |
| 521 | ret = poll(poll_events, fd - start, 0); |
| 522 | if (ret >= 0) |
| 523 | break; |
| 524 | } while (errno == EAGAIN || errno == EINTR || errno == ENOMEM); |
| 525 | |
Willy Tarreau | b8e602c | 2019-02-22 09:07:42 +0100 | [diff] [blame] | 526 | if (ret) |
| 527 | ret = fd - start; |
| 528 | |
Willy Tarreau | 9188ac6 | 2019-02-21 22:12:47 +0100 | [diff] [blame] | 529 | for (idx = 0; idx < ret; idx++) { |
| 530 | if (poll_events[idx].revents & POLLNVAL) |
| 531 | continue; /* already closed */ |
| 532 | |
| 533 | fd = poll_events[idx].fd; |
| 534 | close(fd); |
| 535 | } |
| 536 | start = next; |
| 537 | } |
| 538 | } |
| 539 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 540 | #else // defined(USE_POLL) |
Willy Tarreau | 9188ac6 | 2019-02-21 22:12:47 +0100 | [diff] [blame] | 541 | |
Willy Tarreau | 2d7f81b | 2019-02-21 22:19:17 +0100 | [diff] [blame] | 542 | /* This is a portable implementation of closefrom(). It closes all open file |
| 543 | * descriptors starting at <start> and above. This is a naive version for use |
| 544 | * when the operating system provides no alternative. |
| 545 | */ |
| 546 | void my_closefrom(int start) |
| 547 | { |
| 548 | struct rlimit limit; |
| 549 | int nbfds; |
| 550 | |
| 551 | if (getrlimit(RLIMIT_NOFILE, &limit) == 0) |
| 552 | nbfds = limit.rlim_cur; |
| 553 | else |
| 554 | nbfds = 0; |
| 555 | |
| 556 | if (nbfds <= 0) |
| 557 | nbfds = 1024; /* safe limit */ |
| 558 | |
| 559 | while (start < nbfds) |
| 560 | close(start++); |
| 561 | } |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 562 | #endif // defined(USE_POLL) |
Willy Tarreau | 2d7f81b | 2019-02-21 22:19:17 +0100 | [diff] [blame] | 563 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 564 | /* disable the specified poller */ |
| 565 | void disable_poller(const char *poller_name) |
| 566 | { |
| 567 | int p; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 568 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 569 | for (p = 0; p < nbpollers; p++) |
| 570 | if (strcmp(pollers[p].name, poller_name) == 0) |
| 571 | pollers[p].pref = 0; |
| 572 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 573 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 574 | void poller_pipe_io_handler(int fd) |
| 575 | { |
| 576 | char buf[1024]; |
| 577 | /* Flush the pipe */ |
| 578 | while (read(fd, buf, sizeof(buf)) > 0); |
| 579 | fd_cant_recv(fd); |
| 580 | } |
| 581 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 582 | /* allocate the per-thread fd_updt thus needs to be called early after |
| 583 | * thread creation. |
| 584 | */ |
| 585 | static int alloc_pollers_per_thread() |
| 586 | { |
| 587 | fd_updt = calloc(global.maxsock, sizeof(*fd_updt)); |
| 588 | return fd_updt != NULL; |
| 589 | } |
| 590 | |
| 591 | /* Initialize the pollers per thread.*/ |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 592 | static int init_pollers_per_thread() |
| 593 | { |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 594 | int mypipe[2]; |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 595 | |
| 596 | if (pipe(mypipe) < 0) |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 597 | return 0; |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 598 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 599 | poller_rd_pipe = mypipe[0]; |
| 600 | poller_wr_pipe[tid] = mypipe[1]; |
| 601 | fcntl(poller_rd_pipe, F_SETFL, O_NONBLOCK); |
| 602 | fd_insert(poller_rd_pipe, poller_pipe_io_handler, poller_pipe_io_handler, |
| 603 | tid_bit); |
| 604 | fd_want_recv(poller_rd_pipe); |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 605 | return 1; |
| 606 | } |
| 607 | |
| 608 | /* Deinitialize the pollers per thread */ |
| 609 | static void deinit_pollers_per_thread() |
| 610 | { |
William Lallemand | 808e1b7 | 2018-12-15 22:34:31 +0100 | [diff] [blame] | 611 | /* rd and wr are init at the same place, but only rd is init to -1, so |
| 612 | we rely to rd to close. */ |
| 613 | if (poller_rd_pipe > -1) { |
| 614 | close(poller_rd_pipe); |
| 615 | poller_rd_pipe = -1; |
| 616 | close(poller_wr_pipe[tid]); |
| 617 | poller_wr_pipe[tid] = -1; |
| 618 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 621 | /* Release the pollers per thread, to be called late */ |
| 622 | static void free_pollers_per_thread() |
| 623 | { |
| 624 | free(fd_updt); |
| 625 | fd_updt = NULL; |
| 626 | } |
| 627 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 628 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 629 | * Initialize the pollers till the best one is found. |
| 630 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 631 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 632 | int init_pollers() |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 633 | { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 634 | int p; |
| 635 | struct poller *bp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 636 | |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 637 | if ((fdtab = calloc(global.maxsock, sizeof(struct fdtab))) == NULL) |
| 638 | goto fail_tab; |
| 639 | |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 640 | if ((polled_mask = calloc(global.maxsock, sizeof(unsigned long))) == NULL) |
| 641 | goto fail_polledmask; |
Uman Shahzad | da7eeed | 2019-01-17 08:21:39 +0000 | [diff] [blame] | 642 | |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 643 | if ((fdinfo = calloc(global.maxsock, sizeof(struct fdinfo))) == NULL) |
| 644 | goto fail_info; |
| 645 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 646 | fd_cache.first = fd_cache.last = -1; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 647 | update_list.first = update_list.last = -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 648 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 649 | for (p = 0; p < global.maxsock; p++) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 650 | HA_SPIN_INIT(&fdtab[p].lock); |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 651 | /* Mark the fd as out of the fd cache */ |
Olivier Houchard | 1256836 | 2018-01-31 18:07:29 +0100 | [diff] [blame] | 652 | fdtab[p].cache.next = -3; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 653 | fdtab[p].update.next = -3; |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 654 | } |
| 655 | for (p = 0; p < global.nbthread; p++) |
| 656 | fd_cache_local[p].first = fd_cache_local[p].last = -1; |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 657 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 658 | do { |
| 659 | bp = NULL; |
| 660 | for (p = 0; p < nbpollers; p++) |
| 661 | if (!bp || (pollers[p].pref > bp->pref)) |
| 662 | bp = &pollers[p]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 663 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 664 | if (!bp || bp->pref == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 665 | break; |
| 666 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 667 | if (bp->init(bp)) { |
| 668 | memcpy(&cur_poller, bp, sizeof(*bp)); |
| 669 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 670 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 671 | } while (!bp || bp->pref == 0); |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 672 | |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 673 | free(fdinfo); |
| 674 | fail_info: |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 675 | free(polled_mask); |
| 676 | fail_polledmask: |
Uman Shahzad | da7eeed | 2019-01-17 08:21:39 +0000 | [diff] [blame] | 677 | free(fdtab); |
| 678 | fail_tab: |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 679 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 682 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 683 | * Deinitialize the pollers. |
| 684 | */ |
| 685 | void deinit_pollers() { |
| 686 | |
| 687 | struct poller *bp; |
| 688 | int p; |
| 689 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 690 | for (p = 0; p < global.maxsock; p++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 691 | HA_SPIN_DESTROY(&fdtab[p].lock); |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 692 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 693 | for (p = 0; p < nbpollers; p++) { |
| 694 | bp = &pollers[p]; |
| 695 | |
| 696 | if (bp && bp->pref) |
| 697 | bp->term(bp); |
| 698 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 699 | |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 700 | free(fdinfo); fdinfo = NULL; |
| 701 | free(fdtab); fdtab = NULL; |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 702 | free(polled_mask); polled_mask = NULL; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 706 | * Lists the known pollers on <out>. |
| 707 | * Should be performed only before initialization. |
| 708 | */ |
| 709 | int list_pollers(FILE *out) |
| 710 | { |
| 711 | int p; |
| 712 | int last, next; |
| 713 | int usable; |
| 714 | struct poller *bp; |
| 715 | |
| 716 | fprintf(out, "Available polling systems :\n"); |
| 717 | |
| 718 | usable = 0; |
| 719 | bp = NULL; |
| 720 | last = next = -1; |
| 721 | while (1) { |
| 722 | for (p = 0; p < nbpollers; p++) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 723 | if ((next < 0 || pollers[p].pref > next) |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 724 | && (last < 0 || pollers[p].pref < last)) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 725 | next = pollers[p].pref; |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 726 | if (!bp || (pollers[p].pref > bp->pref)) |
| 727 | bp = &pollers[p]; |
| 728 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | if (next == -1) |
| 732 | break; |
| 733 | |
| 734 | for (p = 0; p < nbpollers; p++) { |
| 735 | if (pollers[p].pref == next) { |
| 736 | fprintf(out, " %10s : ", pollers[p].name); |
| 737 | if (pollers[p].pref == 0) |
| 738 | fprintf(out, "disabled, "); |
| 739 | else |
| 740 | fprintf(out, "pref=%3d, ", pollers[p].pref); |
| 741 | if (pollers[p].test(&pollers[p])) { |
| 742 | fprintf(out, " test result OK"); |
| 743 | if (next > 0) |
| 744 | usable++; |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 745 | } else { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 746 | fprintf(out, " test result FAILED"); |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 747 | if (bp == &pollers[p]) |
| 748 | bp = NULL; |
| 749 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 750 | fprintf(out, "\n"); |
| 751 | } |
| 752 | } |
| 753 | last = next; |
| 754 | next = -1; |
| 755 | }; |
| 756 | fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none"); |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 762 | * to create initialize part of them again. Returns 0 in case of failure, |
| 763 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 764 | * the the current poller is destroyed and the caller is responsible for trying |
| 765 | * another one by calling init_pollers() again. |
| 766 | */ |
| 767 | int fork_poller() |
| 768 | { |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 769 | int fd; |
Willy Tarreau | ce036bc | 2018-01-29 14:58:02 +0100 | [diff] [blame] | 770 | for (fd = 0; fd < global.maxsock; fd++) { |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 771 | if (fdtab[fd].owner) { |
| 772 | fdtab[fd].cloned = 1; |
| 773 | } |
| 774 | } |
| 775 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 776 | if (cur_poller.fork) { |
| 777 | if (cur_poller.fork(&cur_poller)) |
| 778 | return 1; |
| 779 | cur_poller.term(&cur_poller); |
| 780 | return 0; |
| 781 | } |
| 782 | return 1; |
| 783 | } |
| 784 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 785 | REGISTER_PER_THREAD_ALLOC(alloc_pollers_per_thread); |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 786 | REGISTER_PER_THREAD_INIT(init_pollers_per_thread); |
| 787 | REGISTER_PER_THREAD_DEINIT(deinit_pollers_per_thread); |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 788 | REGISTER_PER_THREAD_FREE(free_pollers_per_thread); |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 789 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 790 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 791 | * Local variables: |
| 792 | * c-indent-level: 8 |
| 793 | * c-basic-offset: 8 |
| 794 | * End: |
| 795 | */ |