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 | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 11 | * There is no direct link between the FD and the updates list. There is only a |
| 12 | * bit in the fdtab[] to indicate than a file descriptor is already present in |
| 13 | * the updates list. Once an fd is present in the updates list, it will have to |
| 14 | * be considered even if its changes are reverted in the middle or if the fd is |
| 15 | * replaced. |
| 16 | * |
| 17 | * It is important to understand that as long as all expected events are |
| 18 | * processed, they might starve the polled events, especially because polled |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 19 | * 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] | 20 | * 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] | 21 | * is that unhandled events will still wake the poller up. Using an edge- |
| 22 | * triggered poller such as EPOLL_ET will solve this issue though. |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 23 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 24 | * The event state for an FD, as found in fdtab[].state, is maintained for each |
| 25 | * direction. The state field is built this way, with R bits in the low nibble |
| 26 | * 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] | 27 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 28 | * 7 6 5 4 3 2 1 0 |
| 29 | * [ 0 | PW | RW | AW | 0 | PR | RR | AR ] |
| 30 | * |
| 31 | * A* = active *R = read |
| 32 | * P* = polled *W = write |
| 33 | * R* = ready |
| 34 | * |
| 35 | * An FD is marked "active" when there is a desire to use it. |
| 36 | * An FD is marked "polled" when it is registered in the polling. |
| 37 | * An FD is marked "ready" when it has not faced a new EAGAIN since last wake-up |
| 38 | * (it is a cache of the last EAGAIN regardless of polling changes). |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 39 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 40 | * 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] | 41 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 42 | * +---+---+---+----------+---------------------------------------------+ |
| 43 | * | P | R | A | State | Description | |
| 44 | * +---+---+---+----------+---------------------------------------------+ |
| 45 | * | 0 | 0 | 0 | DISABLED | No activity desired, not ready. | |
| 46 | * | 0 | 0 | 1 | MUSTPOLL | Activity desired via polling. | |
| 47 | * | 0 | 1 | 0 | STOPPED | End of activity without polling. | |
| 48 | * | 0 | 1 | 1 | ACTIVE | Activity desired without polling. | |
| 49 | * | 1 | 0 | 0 | ABORT | Aborted poll(). Not frequently seen. | |
| 50 | * | 1 | 0 | 1 | POLLED | FD is being polled. | |
| 51 | * | 1 | 1 | 0 | PAUSED | FD was paused while ready (eg: buffer full) | |
| 52 | * | 1 | 1 | 1 | READY | FD was marked ready by poll() | |
| 53 | * +---+---+---+----------+---------------------------------------------+ |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 54 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 55 | * The transitions are pretty simple : |
| 56 | * - fd_want_*() : set flag A |
| 57 | * - fd_stop_*() : clear flag A |
| 58 | * - fd_cant_*() : clear flag R (when facing EAGAIN) |
| 59 | * - fd_may_*() : set flag R (upon return from poll()) |
| 60 | * - sync() : if (A) { if (!R) P := 1 } else { P := 0 } |
| 61 | * |
| 62 | * The PAUSED, ABORT and MUSTPOLL states are transient for level-trigerred |
| 63 | * pollers and are fixed by the sync() which happens at the beginning of the |
| 64 | * poller. For event-triggered pollers, only the MUSTPOLL state will be |
| 65 | * transient and ABORT will lead to PAUSED. The ACTIVE state is the only stable |
| 66 | * one which has P != A. |
| 67 | * |
| 68 | * The READY state is a bit special as activity on the FD might be notified |
| 69 | * both by the poller or by the cache. But it is needed for some multi-layer |
| 70 | * protocols (eg: SSL) where connection activity is not 100% linked to FD |
| 71 | * activity. Also some pollers might prefer to implement it as ACTIVE if |
| 72 | * enabling/disabling the FD is cheap. The READY and ACTIVE states are the |
| 73 | * two states for which a cache entry is allocated. |
| 74 | * |
| 75 | * The state transitions look like the diagram below. Only the 4 right states |
| 76 | * have polling enabled : |
| 77 | * |
| 78 | * (POLLED=0) (POLLED=1) |
| 79 | * |
| 80 | * +----------+ sync +-------+ |
| 81 | * | DISABLED | <----- | ABORT | (READY=0, ACTIVE=0) |
| 82 | * +----------+ +-------+ |
| 83 | * clr | ^ set | ^ |
| 84 | * | | | | |
| 85 | * v | set v | clr |
| 86 | * +----------+ sync +--------+ |
| 87 | * | MUSTPOLL | -----> | POLLED | (READY=0, ACTIVE=1) |
| 88 | * +----------+ +--------+ |
| 89 | * ^ poll | ^ |
| 90 | * | | | |
| 91 | * | EAGAIN v | EAGAIN |
| 92 | * +--------+ +-------+ |
| 93 | * | ACTIVE | | READY | (READY=1, ACTIVE=1) |
| 94 | * +--------+ +-------+ |
| 95 | * clr | ^ set | ^ |
| 96 | * | | | | |
| 97 | * v | set v | clr |
| 98 | * +---------+ sync +--------+ |
| 99 | * | STOPPED | <------ | PAUSED | (READY=1, ACTIVE=0) |
| 100 | * +---------+ +--------+ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | */ |
| 102 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 103 | #include <stdio.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 104 | #include <string.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | #include <unistd.h> |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 106 | #include <fcntl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | #include <sys/types.h> |
Willy Tarreau | 2d7f81b | 2019-02-21 22:19:17 +0100 | [diff] [blame] | 108 | #include <sys/resource.h> |
Willy Tarreau | 931d8b7 | 2019-08-27 11:08:17 +0200 | [diff] [blame] | 109 | #include <sys/uio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 111 | #if defined(USE_POLL) |
Willy Tarreau | 9188ac6 | 2019-02-21 22:12:47 +0100 | [diff] [blame] | 112 | #include <poll.h> |
| 113 | #include <errno.h> |
| 114 | #endif |
| 115 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 116 | #include <common/compat.h> |
| 117 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 118 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 119 | #include <types/global.h> |
| 120 | |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 121 | #include <proto/fd.h> |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 122 | #include <proto/log.h> |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 123 | #include <proto/port_range.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 124 | |
| 125 | struct fdtab *fdtab = NULL; /* array of all the file descriptors */ |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 126 | struct polled_mask *polled_mask = NULL; /* Array for the polled_mask of each fd */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 127 | struct fdinfo *fdinfo = NULL; /* less-often used infos for file descriptors */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 128 | int totalconn; /* total # of terminated sessions */ |
| 129 | int actconn; /* # of active sessions */ |
| 130 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 131 | struct poller pollers[MAX_POLLERS]; |
| 132 | struct poller cur_poller; |
| 133 | int nbpollers = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 134 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 135 | volatile struct fdlist update_list; // Global update list |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 136 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 137 | THREAD_LOCAL int *fd_updt = NULL; // FD updates list |
| 138 | THREAD_LOCAL int fd_nbupdt = 0; // number of updates in the list |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 139 | THREAD_LOCAL int poller_rd_pipe = -1; // Pipe to wake the thread |
| 140 | int poller_wr_pipe[MAX_THREADS]; // Pipe to wake the threads |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 141 | |
Olivier Houchard | 7c49d2e | 2019-04-16 18:37:05 +0200 | [diff] [blame] | 142 | volatile int ha_used_fds = 0; // Number of FD we're currently using |
| 143 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 144 | #define _GET_NEXT(fd, off) ((struct fdlist_entry *)(void *)((char *)(&fdtab[fd]) + off))->next |
| 145 | #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] | 146 | /* 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] | 147 | 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] | 148 | { |
| 149 | int next; |
| 150 | int new; |
| 151 | int old; |
| 152 | int last; |
| 153 | |
| 154 | redo_next: |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 155 | next = _GET_NEXT(fd, off); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 156 | /* Check that we're not already in the cache, and if not, lock us. */ |
| 157 | if (next >= -2) |
| 158 | goto done; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 159 | if (!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2)) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 160 | goto redo_next; |
Olivier Houchard | d2b5d16 | 2019-03-08 13:47:21 +0100 | [diff] [blame] | 161 | __ha_barrier_atomic_store(); |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 162 | |
| 163 | new = fd; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 164 | redo_last: |
| 165 | /* First, insert in the linked list */ |
| 166 | last = list->last; |
| 167 | old = -1; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 168 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 169 | _GET_PREV(fd, off) = -2; |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 170 | /* Make sure the "prev" store is visible before we update the last entry */ |
| 171 | __ha_barrier_store(); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 172 | |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 173 | if (unlikely(last == -1)) { |
| 174 | /* 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] | 175 | if (unlikely(!_HA_ATOMIC_CAS(&list->last, &old, new))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 176 | goto redo_last; |
| 177 | |
| 178 | /* list->first was necessary -1, we're guaranteed to be alone here */ |
| 179 | list->first = fd; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 180 | } else { |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 181 | /* adding ourselves past the last element |
| 182 | * The CAS will only succeed if its next is -1, |
| 183 | * which means it's in the cache, and the last element. |
| 184 | */ |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 185 | if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(last, off), &old, new))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 186 | goto redo_last; |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 187 | |
| 188 | /* Then, update the last entry */ |
| 189 | list->last = fd; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 190 | } |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 191 | __ha_barrier_store(); |
Willy Tarreau | 11559a7 | 2018-02-05 17:52:24 +0100 | [diff] [blame] | 192 | /* since we're alone at the end of the list and still locked(-2), |
| 193 | * we know noone tried to add past us. Mark the end of list. |
| 194 | */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 195 | _GET_PREV(fd, off) = last; |
| 196 | _GET_NEXT(fd, off) = -1; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 197 | __ha_barrier_store(); |
| 198 | done: |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | /* removes fd <fd> from fd list <list> */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 203 | 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] | 204 | { |
| 205 | #if defined(HA_HAVE_CAS_DW) || defined(HA_CAS_IS_8B) |
| 206 | volatile struct fdlist_entry cur_list, next_list; |
| 207 | #endif |
| 208 | int old; |
| 209 | int new = -2; |
| 210 | int prev; |
| 211 | int next; |
| 212 | int last; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 213 | lock_self: |
| 214 | #if (defined(HA_CAS_IS_8B) || defined(HA_HAVE_CAS_DW)) |
| 215 | next_list.next = next_list.prev = -2; |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 216 | cur_list = *(volatile struct fdlist_entry *)(((char *)&fdtab[fd]) + off); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 217 | /* First, attempt to lock our own entries */ |
| 218 | do { |
| 219 | /* The FD is not in the FD cache, give up */ |
| 220 | if (unlikely(cur_list.next <= -3)) |
| 221 | return; |
| 222 | if (unlikely(cur_list.prev == -2 || cur_list.next == -2)) |
| 223 | goto lock_self; |
| 224 | } while ( |
| 225 | #ifdef HA_CAS_IS_8B |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 226 | 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] | 227 | #else |
Willy Tarreau | c3b5958 | 2019-05-27 17:37:20 +0200 | [diff] [blame] | 228 | 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] | 229 | #endif |
| 230 | ; |
| 231 | next = cur_list.next; |
| 232 | prev = cur_list.prev; |
| 233 | |
| 234 | #else |
| 235 | lock_self_next: |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 236 | next = ({ volatile int *next = &_GET_NEXT(fd, off); *next; }); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 237 | if (next == -2) |
| 238 | goto lock_self_next; |
| 239 | if (next <= -3) |
| 240 | goto done; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 241 | if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(fd, off), &next, -2))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 242 | goto lock_self_next; |
| 243 | lock_self_prev: |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 244 | prev = ({ volatile int *prev = &_GET_PREV(fd, off); *prev; }); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 245 | if (prev == -2) |
| 246 | goto lock_self_prev; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 247 | if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(fd, off), &prev, -2))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 248 | goto lock_self_prev; |
| 249 | #endif |
Olivier Houchard | d2b5d16 | 2019-03-08 13:47:21 +0100 | [diff] [blame] | 250 | __ha_barrier_atomic_store(); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 251 | |
| 252 | /* Now, lock the entries of our neighbours */ |
| 253 | if (likely(prev != -1)) { |
| 254 | redo_prev: |
| 255 | old = fd; |
| 256 | |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 257 | if (unlikely(!_HA_ATOMIC_CAS(&_GET_NEXT(prev, off), &old, new))) { |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 258 | if (unlikely(old == -2)) { |
| 259 | /* Neighbour already locked, give up and |
| 260 | * retry again once he's done |
| 261 | */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 262 | _GET_PREV(fd, off) = prev; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 263 | __ha_barrier_store(); |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 264 | _GET_NEXT(fd, off) = next; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 265 | __ha_barrier_store(); |
| 266 | goto lock_self; |
| 267 | } |
| 268 | goto redo_prev; |
| 269 | } |
| 270 | } |
| 271 | if (likely(next != -1)) { |
| 272 | redo_next: |
| 273 | old = fd; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 274 | if (unlikely(!_HA_ATOMIC_CAS(&_GET_PREV(next, off), &old, new))) { |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 275 | if (unlikely(old == -2)) { |
| 276 | /* Neighbour already locked, give up and |
| 277 | * retry again once he's done |
| 278 | */ |
| 279 | if (prev != -1) { |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 280 | _GET_NEXT(prev, off) = fd; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 281 | __ha_barrier_store(); |
| 282 | } |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 283 | _GET_PREV(fd, off) = prev; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 284 | __ha_barrier_store(); |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 285 | _GET_NEXT(fd, off) = next; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 286 | __ha_barrier_store(); |
| 287 | goto lock_self; |
| 288 | } |
| 289 | goto redo_next; |
| 290 | } |
| 291 | } |
| 292 | if (list->first == fd) |
| 293 | list->first = next; |
| 294 | __ha_barrier_store(); |
| 295 | last = list->last; |
Olivier Houchard | d360879 | 2019-03-08 18:47:42 +0100 | [diff] [blame] | 296 | while (unlikely(last == fd && (!_HA_ATOMIC_CAS(&list->last, &last, prev)))) |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 297 | __ha_compiler_barrier(); |
| 298 | /* Make sure we let other threads know we're no longer in cache, |
| 299 | * before releasing our neighbours. |
| 300 | */ |
| 301 | __ha_barrier_store(); |
| 302 | if (likely(prev != -1)) |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 303 | _GET_NEXT(prev, off) = next; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 304 | __ha_barrier_store(); |
| 305 | if (likely(next != -1)) |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 306 | _GET_PREV(next, off) = prev; |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 307 | __ha_barrier_store(); |
| 308 | /* Ok, now we're out of the fd cache */ |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 309 | _GET_NEXT(fd, off) = -(next + 4); |
Willy Tarreau | 4cc67a2 | 2018-02-05 17:14:55 +0100 | [diff] [blame] | 310 | __ha_barrier_store(); |
| 311 | done: |
| 312 | return; |
| 313 | } |
| 314 | |
Olivier Houchard | 6a2cf87 | 2018-04-25 15:10:30 +0200 | [diff] [blame] | 315 | #undef _GET_NEXT |
| 316 | #undef _GET_PREV |
| 317 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 318 | /* Deletes an FD from the fdsets. |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 319 | * The file descriptor is also closed. |
| 320 | */ |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 321 | static void fd_dodelete(int fd, int do_close) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 322 | { |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 323 | unsigned long locked = atleast2(fdtab[fd].thread_mask); |
| 324 | |
| 325 | if (locked) |
| 326 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 327 | if (fdtab[fd].linger_risk) { |
| 328 | /* this is generally set when connecting to servers */ |
| 329 | setsockopt(fd, SOL_SOCKET, SO_LINGER, |
| 330 | (struct linger *) &nolinger, sizeof(struct linger)); |
| 331 | } |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 332 | if (cur_poller.clo) |
| 333 | cur_poller.clo(fd); |
Olivier Houchard | c22580c | 2019-08-05 18:51:52 +0200 | [diff] [blame] | 334 | polled_mask[fd].poll_recv = polled_mask[fd].poll_send = 0; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 335 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 336 | fdtab[fd].state = 0; |
Willy Tarreau | 6ea20b1 | 2012-11-11 16:05:19 +0100 | [diff] [blame] | 337 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 338 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 339 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | db3b326 | 2012-07-05 23:19:22 +0200 | [diff] [blame] | 340 | fdtab[fd].owner = NULL; |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 341 | fdtab[fd].thread_mask = 0; |
Willy Tarreau | c9c8378 | 2018-01-17 18:44:46 +0100 | [diff] [blame] | 342 | if (do_close) { |
Christopher Faulet | d531f88 | 2017-06-01 16:55:03 +0200 | [diff] [blame] | 343 | close(fd); |
Olivier Houchard | 7c49d2e | 2019-04-16 18:37:05 +0200 | [diff] [blame] | 344 | _HA_ATOMIC_SUB(&ha_used_fds, 1); |
Willy Tarreau | c9c8378 | 2018-01-17 18:44:46 +0100 | [diff] [blame] | 345 | } |
Willy Tarreau | 87d54a9 | 2018-10-15 09:44:46 +0200 | [diff] [blame] | 346 | if (locked) |
| 347 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 348 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 349 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 350 | /* Deletes an FD from the fdsets. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 351 | * The file descriptor is also closed. |
| 352 | */ |
| 353 | void fd_delete(int fd) |
| 354 | { |
| 355 | fd_dodelete(fd, 1); |
| 356 | } |
| 357 | |
Willy Tarreau | 173d995 | 2018-01-26 21:48:23 +0100 | [diff] [blame] | 358 | /* Deletes an FD from the fdsets. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 359 | * The file descriptor is kept open. |
| 360 | */ |
| 361 | void fd_remove(int fd) |
| 362 | { |
| 363 | fd_dodelete(fd, 0); |
| 364 | } |
| 365 | |
Willy Tarreau | 931d8b7 | 2019-08-27 11:08:17 +0200 | [diff] [blame] | 366 | /* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg> |
| 367 | * optionally followed by a newline if <nl> is non-null, to file descriptor |
| 368 | * <fd>. The message is sent atomically using writev(). It may be truncated to |
| 369 | * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the |
| 370 | * two lists, it's just a convenience to help the caller prepend some prefixes |
| 371 | * when necessary. It takes the fd's lock to make sure no other thread will |
| 372 | * write to the same fd in parallel. Returns the number of bytes sent, or <=0 |
| 373 | * on failure. A limit to 31 total non-empty segments is enforced. The caller |
| 374 | * is responsible for taking care of making the fd non-blocking. |
| 375 | */ |
| 376 | ssize_t fd_write_frag_line(int fd, size_t maxlen, const struct ist pfx[], size_t npfx, const struct ist msg[], size_t nmsg, int nl) |
| 377 | { |
| 378 | struct iovec iovec[32]; |
| 379 | size_t totlen = 0; |
| 380 | size_t sent = 0; |
| 381 | int vec = 0; |
| 382 | |
| 383 | if (!maxlen) |
| 384 | maxlen = ~0; |
| 385 | |
| 386 | /* keep one char for a possible trailing '\n' in any case */ |
| 387 | maxlen--; |
| 388 | |
| 389 | /* make an iovec from the concatenation of all parts of the original |
| 390 | * message. Skip empty fields and truncate the whole message to maxlen, |
| 391 | * leaving one spare iovec for the '\n'. |
| 392 | */ |
| 393 | while (vec < (sizeof(iovec) / sizeof(iovec[0]) - 1)) { |
| 394 | if (!npfx) { |
| 395 | pfx = msg; |
| 396 | npfx = nmsg; |
| 397 | nmsg = 0; |
| 398 | if (!npfx) |
| 399 | break; |
| 400 | } |
| 401 | |
| 402 | iovec[vec].iov_base = pfx->ptr; |
| 403 | iovec[vec].iov_len = MIN(maxlen, pfx->len); |
| 404 | maxlen -= iovec[vec].iov_len; |
| 405 | totlen += iovec[vec].iov_len; |
| 406 | if (iovec[vec].iov_len) |
| 407 | vec++; |
| 408 | pfx++; npfx--; |
| 409 | }; |
| 410 | |
| 411 | if (nl) { |
| 412 | iovec[vec].iov_base = "\n"; |
| 413 | iovec[vec].iov_len = 1; |
| 414 | vec++; |
| 415 | } |
| 416 | |
| 417 | HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); |
| 418 | sent = writev(fd, iovec, vec); |
| 419 | HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); |
| 420 | |
| 421 | /* sent > 0 if the message was delivered */ |
| 422 | return sent; |
| 423 | } |
| 424 | |
Olivier Houchard | 2292edf | 2019-02-25 14:26:54 +0100 | [diff] [blame] | 425 | #if defined(USE_CLOSEFROM) |
| 426 | void my_closefrom(int start) |
| 427 | { |
| 428 | closefrom(start); |
| 429 | } |
| 430 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 431 | #elif defined(USE_POLL) |
Willy Tarreau | 9188ac6 | 2019-02-21 22:12:47 +0100 | [diff] [blame] | 432 | /* This is a portable implementation of closefrom(). It closes all open file |
| 433 | * descriptors starting at <start> and above. It relies on the fact that poll() |
| 434 | * will return POLLNVAL for each invalid (hence close) file descriptor passed |
| 435 | * in argument in order to skip them. It acts with batches of FDs and will |
| 436 | * typically perform one poll() call per 1024 FDs so the overhead is low in |
| 437 | * case all FDs have to be closed. |
| 438 | */ |
| 439 | void my_closefrom(int start) |
| 440 | { |
| 441 | struct pollfd poll_events[1024]; |
| 442 | struct rlimit limit; |
| 443 | int nbfds, fd, ret, idx; |
| 444 | int step, next; |
| 445 | |
| 446 | if (getrlimit(RLIMIT_NOFILE, &limit) == 0) |
| 447 | step = nbfds = limit.rlim_cur; |
| 448 | else |
| 449 | step = nbfds = 0; |
| 450 | |
| 451 | if (nbfds <= 0) { |
| 452 | /* set safe limit */ |
| 453 | nbfds = 1024; |
| 454 | step = 256; |
| 455 | } |
| 456 | |
| 457 | if (step > sizeof(poll_events) / sizeof(poll_events[0])) |
| 458 | step = sizeof(poll_events) / sizeof(poll_events[0]); |
| 459 | |
| 460 | while (start < nbfds) { |
| 461 | next = (start / step + 1) * step; |
| 462 | |
| 463 | for (fd = start; fd < next && fd < nbfds; fd++) { |
| 464 | poll_events[fd - start].fd = fd; |
| 465 | poll_events[fd - start].events = 0; |
| 466 | } |
| 467 | |
| 468 | do { |
| 469 | ret = poll(poll_events, fd - start, 0); |
| 470 | if (ret >= 0) |
| 471 | break; |
| 472 | } while (errno == EAGAIN || errno == EINTR || errno == ENOMEM); |
| 473 | |
Willy Tarreau | b8e602c | 2019-02-22 09:07:42 +0100 | [diff] [blame] | 474 | if (ret) |
| 475 | ret = fd - start; |
| 476 | |
Willy Tarreau | 9188ac6 | 2019-02-21 22:12:47 +0100 | [diff] [blame] | 477 | for (idx = 0; idx < ret; idx++) { |
| 478 | if (poll_events[idx].revents & POLLNVAL) |
| 479 | continue; /* already closed */ |
| 480 | |
| 481 | fd = poll_events[idx].fd; |
| 482 | close(fd); |
| 483 | } |
| 484 | start = next; |
| 485 | } |
| 486 | } |
| 487 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 488 | #else // defined(USE_POLL) |
Willy Tarreau | 9188ac6 | 2019-02-21 22:12:47 +0100 | [diff] [blame] | 489 | |
Willy Tarreau | 2d7f81b | 2019-02-21 22:19:17 +0100 | [diff] [blame] | 490 | /* This is a portable implementation of closefrom(). It closes all open file |
| 491 | * descriptors starting at <start> and above. This is a naive version for use |
| 492 | * when the operating system provides no alternative. |
| 493 | */ |
| 494 | void my_closefrom(int start) |
| 495 | { |
| 496 | struct rlimit limit; |
| 497 | int nbfds; |
| 498 | |
| 499 | if (getrlimit(RLIMIT_NOFILE, &limit) == 0) |
| 500 | nbfds = limit.rlim_cur; |
| 501 | else |
| 502 | nbfds = 0; |
| 503 | |
| 504 | if (nbfds <= 0) |
| 505 | nbfds = 1024; /* safe limit */ |
| 506 | |
| 507 | while (start < nbfds) |
| 508 | close(start++); |
| 509 | } |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 510 | #endif // defined(USE_POLL) |
Willy Tarreau | 2d7f81b | 2019-02-21 22:19:17 +0100 | [diff] [blame] | 511 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 512 | /* disable the specified poller */ |
| 513 | void disable_poller(const char *poller_name) |
| 514 | { |
| 515 | int 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 | for (p = 0; p < nbpollers; p++) |
| 518 | if (strcmp(pollers[p].name, poller_name) == 0) |
| 519 | pollers[p].pref = 0; |
| 520 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 521 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 522 | void poller_pipe_io_handler(int fd) |
| 523 | { |
| 524 | char buf[1024]; |
| 525 | /* Flush the pipe */ |
| 526 | while (read(fd, buf, sizeof(buf)) > 0); |
| 527 | fd_cant_recv(fd); |
| 528 | } |
| 529 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 530 | /* allocate the per-thread fd_updt thus needs to be called early after |
| 531 | * thread creation. |
| 532 | */ |
| 533 | static int alloc_pollers_per_thread() |
| 534 | { |
| 535 | fd_updt = calloc(global.maxsock, sizeof(*fd_updt)); |
| 536 | return fd_updt != NULL; |
| 537 | } |
| 538 | |
| 539 | /* Initialize the pollers per thread.*/ |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 540 | static int init_pollers_per_thread() |
| 541 | { |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 542 | int mypipe[2]; |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 543 | |
| 544 | if (pipe(mypipe) < 0) |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 545 | return 0; |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 546 | |
Olivier Houchard | 79321b9 | 2018-07-26 17:55:11 +0200 | [diff] [blame] | 547 | poller_rd_pipe = mypipe[0]; |
| 548 | poller_wr_pipe[tid] = mypipe[1]; |
| 549 | fcntl(poller_rd_pipe, F_SETFL, O_NONBLOCK); |
| 550 | fd_insert(poller_rd_pipe, poller_pipe_io_handler, poller_pipe_io_handler, |
| 551 | tid_bit); |
| 552 | fd_want_recv(poller_rd_pipe); |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 553 | return 1; |
| 554 | } |
| 555 | |
| 556 | /* Deinitialize the pollers per thread */ |
| 557 | static void deinit_pollers_per_thread() |
| 558 | { |
William Lallemand | 808e1b7 | 2018-12-15 22:34:31 +0100 | [diff] [blame] | 559 | /* rd and wr are init at the same place, but only rd is init to -1, so |
| 560 | we rely to rd to close. */ |
| 561 | if (poller_rd_pipe > -1) { |
| 562 | close(poller_rd_pipe); |
| 563 | poller_rd_pipe = -1; |
| 564 | close(poller_wr_pipe[tid]); |
| 565 | poller_wr_pipe[tid] = -1; |
| 566 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 567 | } |
| 568 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 569 | /* Release the pollers per thread, to be called late */ |
| 570 | static void free_pollers_per_thread() |
| 571 | { |
| 572 | free(fd_updt); |
| 573 | fd_updt = NULL; |
| 574 | } |
| 575 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 576 | /* |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 577 | * Initialize the pollers till the best one is found. |
| 578 | * If none works, returns 0, otherwise 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 579 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 580 | int init_pollers() |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 581 | { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 582 | int p; |
| 583 | struct poller *bp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 584 | |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 585 | if ((fdtab = calloc(global.maxsock, sizeof(struct fdtab))) == NULL) |
| 586 | goto fail_tab; |
| 587 | |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 588 | if ((polled_mask = calloc(global.maxsock, sizeof(*polled_mask))) == NULL) |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 589 | goto fail_polledmask; |
Uman Shahzad | da7eeed | 2019-01-17 08:21:39 +0000 | [diff] [blame] | 590 | |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 591 | if ((fdinfo = calloc(global.maxsock, sizeof(struct fdinfo))) == NULL) |
| 592 | goto fail_info; |
| 593 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 594 | update_list.first = update_list.last = -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 595 | |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 596 | for (p = 0; p < global.maxsock; p++) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 597 | HA_SPIN_INIT(&fdtab[p].lock); |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 598 | /* Mark the fd as out of the fd cache */ |
Olivier Houchard | 1256836 | 2018-01-31 18:07:29 +0100 | [diff] [blame] | 599 | fdtab[p].cache.next = -3; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 600 | fdtab[p].update.next = -3; |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 601 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 602 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 603 | do { |
| 604 | bp = NULL; |
| 605 | for (p = 0; p < nbpollers; p++) |
| 606 | if (!bp || (pollers[p].pref > bp->pref)) |
| 607 | bp = &pollers[p]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 608 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 609 | if (!bp || bp->pref == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 610 | break; |
| 611 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 612 | if (bp->init(bp)) { |
| 613 | memcpy(&cur_poller, bp, sizeof(*bp)); |
| 614 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 615 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 616 | } while (!bp || bp->pref == 0); |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 617 | |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 618 | free(fdinfo); |
| 619 | fail_info: |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 620 | free(polled_mask); |
| 621 | fail_polledmask: |
Uman Shahzad | da7eeed | 2019-01-17 08:21:39 +0000 | [diff] [blame] | 622 | free(fdtab); |
| 623 | fail_tab: |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 624 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 625 | } |
| 626 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 627 | /* |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 628 | * Deinitialize the pollers. |
| 629 | */ |
| 630 | void deinit_pollers() { |
| 631 | |
| 632 | struct poller *bp; |
| 633 | int p; |
| 634 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 635 | for (p = 0; p < global.maxsock; p++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 636 | HA_SPIN_DESTROY(&fdtab[p].lock); |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 637 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 638 | for (p = 0; p < nbpollers; p++) { |
| 639 | bp = &pollers[p]; |
| 640 | |
| 641 | if (bp && bp->pref) |
| 642 | bp->term(bp); |
| 643 | } |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 644 | |
Christopher Faulet | 63fe652 | 2017-08-31 17:52:09 +0200 | [diff] [blame] | 645 | free(fdinfo); fdinfo = NULL; |
| 646 | free(fdtab); fdtab = NULL; |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 647 | free(polled_mask); polled_mask = NULL; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 651 | * Lists the known pollers on <out>. |
| 652 | * Should be performed only before initialization. |
| 653 | */ |
| 654 | int list_pollers(FILE *out) |
| 655 | { |
| 656 | int p; |
| 657 | int last, next; |
| 658 | int usable; |
| 659 | struct poller *bp; |
| 660 | |
| 661 | fprintf(out, "Available polling systems :\n"); |
| 662 | |
| 663 | usable = 0; |
| 664 | bp = NULL; |
| 665 | last = next = -1; |
| 666 | while (1) { |
| 667 | for (p = 0; p < nbpollers; p++) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 668 | if ((next < 0 || pollers[p].pref > next) |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 669 | && (last < 0 || pollers[p].pref < last)) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 670 | next = pollers[p].pref; |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 671 | if (!bp || (pollers[p].pref > bp->pref)) |
| 672 | bp = &pollers[p]; |
| 673 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | if (next == -1) |
| 677 | break; |
| 678 | |
| 679 | for (p = 0; p < nbpollers; p++) { |
| 680 | if (pollers[p].pref == next) { |
| 681 | fprintf(out, " %10s : ", pollers[p].name); |
| 682 | if (pollers[p].pref == 0) |
| 683 | fprintf(out, "disabled, "); |
| 684 | else |
| 685 | fprintf(out, "pref=%3d, ", pollers[p].pref); |
| 686 | if (pollers[p].test(&pollers[p])) { |
| 687 | fprintf(out, " test result OK"); |
| 688 | if (next > 0) |
| 689 | usable++; |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 690 | } else { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 691 | fprintf(out, " test result FAILED"); |
Willy Tarreau | e79c3b2 | 2010-11-19 10:20:36 +0100 | [diff] [blame] | 692 | if (bp == &pollers[p]) |
| 693 | bp = NULL; |
| 694 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 695 | fprintf(out, "\n"); |
| 696 | } |
| 697 | } |
| 698 | last = next; |
| 699 | next = -1; |
| 700 | }; |
| 701 | fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none"); |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * Some pollers may lose their connection after a fork(). It may be necessary |
| 707 | * to create initialize part of them again. Returns 0 in case of failure, |
| 708 | * otherwise 1. The fork() function may be NULL if unused. In case of error, |
| 709 | * the the current poller is destroyed and the caller is responsible for trying |
| 710 | * another one by calling init_pollers() again. |
| 711 | */ |
| 712 | int fork_poller() |
| 713 | { |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 714 | int fd; |
Willy Tarreau | ce036bc | 2018-01-29 14:58:02 +0100 | [diff] [blame] | 715 | for (fd = 0; fd < global.maxsock; fd++) { |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 716 | if (fdtab[fd].owner) { |
| 717 | fdtab[fd].cloned = 1; |
| 718 | } |
| 719 | } |
| 720 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 721 | if (cur_poller.fork) { |
| 722 | if (cur_poller.fork(&cur_poller)) |
| 723 | return 1; |
| 724 | cur_poller.term(&cur_poller); |
| 725 | return 0; |
| 726 | } |
| 727 | return 1; |
| 728 | } |
| 729 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 730 | REGISTER_PER_THREAD_ALLOC(alloc_pollers_per_thread); |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 731 | REGISTER_PER_THREAD_INIT(init_pollers_per_thread); |
| 732 | REGISTER_PER_THREAD_DEINIT(deinit_pollers_per_thread); |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 733 | REGISTER_PER_THREAD_FREE(free_pollers_per_thread); |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 734 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 735 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 736 | * Local variables: |
| 737 | * c-indent-level: 8 |
| 738 | * c-basic-offset: 8 |
| 739 | * End: |
| 740 | */ |