blob: b64130ed0a1e5c4c1e44670c2cab28ea3cde9a35 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * File descriptors management functions.
3 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +01004 * Copyright 2000-2014 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Tarreauf817e9f2014-01-10 16:58:45 +010011 * 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 Tarreau7be79a42012-11-11 15:02:54 +010015 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010016 * 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 Tarreau7be79a42012-11-11 15:02:54 +010027 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010028 * 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 Tarreau7be79a42012-11-11 15:02:54 +010044 *
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 Tarreauf817e9f2014-01-10 16:58:45 +010053 * I/O starvation quickly induces more cached I/O. One solution to this
Willy Tarreau7be79a42012-11-11 15:02:54 +010054 * consists in only processing a part of the events at once, but one drawback
Willy Tarreauf817e9f2014-01-10 16:58:45 +010055 * 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 Tarreau7be79a42012-11-11 15:02:54 +010057 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010058 * 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 Tarreau7be79a42012-11-11 15:02:54 +010063 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010064 * 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 Tarreau7be79a42012-11-11 15:02:54 +010067 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010068 * 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 Tarreau7be79a42012-11-11 15:02:54 +010071 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010072 * 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 Tarreau7be79a42012-11-11 15:02:54 +010083 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010084 * We have 8 possible states for each direction based on these 3 flags :
Willy Tarreau7be79a42012-11-11 15:02:54 +010085 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010086 * +---+---+---+----------+---------------------------------------------+
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 Tarreau7be79a42012-11-11 15:02:54 +010098 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +010099 * 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 Tarreaubaaee002006-06-26 02:48:02 +0200145 */
146
Willy Tarreau2ff76222007-04-09 19:29:56 +0200147#include <stdio.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +0200148#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149#include <unistd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200150#include <sys/types.h>
151
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200152#include <common/compat.h>
153#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154
Willy Tarreau7be79a42012-11-11 15:02:54 +0100155#include <types/global.h>
156
Willy Tarreau2a429502006-10-15 14:52:29 +0200157#include <proto/fd.h>
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200158#include <proto/log.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200159#include <proto/port_range.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200160
161struct fdtab *fdtab = NULL; /* array of all the file descriptors */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200162struct fdinfo *fdinfo = NULL; /* less-often used infos for file descriptors */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163int maxfd; /* # of the highest fd + 1 */
164int totalconn; /* total # of terminated sessions */
165int actconn; /* # of active sessions */
166
Willy Tarreau4f60f162007-04-08 16:39:58 +0200167struct poller pollers[MAX_POLLERS];
168struct poller cur_poller;
169int nbpollers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170
Willy Tarreau16f649c2014-01-25 19:10:48 +0100171unsigned int *fd_cache = NULL; // FD events cache
Willy Tarreau16f649c2014-01-25 19:10:48 +0100172int fd_cache_num = 0; // number of events in the cache
Christopher Faulet69553fe2018-01-15 11:57:03 +0100173unsigned long fd_cache_mask = 0; // Mask of threads with events in the cache
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200174
175THREAD_LOCAL int *fd_updt = NULL; // FD updates list
176THREAD_LOCAL int fd_nbupdt = 0; // number of updates in the list
177
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100178__decl_hathreads(HA_SPINLOCK_T fdtab_lock); /* global lock to protect fdtab array */
179__decl_hathreads(HA_RWLOCK_T fdcache_lock); /* global lock to protect fd_cache array */
180__decl_hathreads(HA_SPINLOCK_T poll_lock); /* global lock to protect poll info */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200181
Willy Tarreau4f60f162007-04-08 16:39:58 +0200182/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
183 * The file descriptor is also closed.
184 */
Olivier Houchard1fc05162017-04-06 01:05:05 +0200185static void fd_dodelete(int fd, int do_close)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100187 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreauad38ace2013-12-15 14:19:38 +0100188 if (fdtab[fd].linger_risk) {
189 /* this is generally set when connecting to servers */
190 setsockopt(fd, SOL_SOCKET, SO_LINGER,
191 (struct linger *) &nolinger, sizeof(struct linger));
192 }
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100193 if (cur_poller.clo)
194 cur_poller.clo(fd);
195
Willy Tarreau899d9572014-01-25 19:20:35 +0100196 fd_release_cache_entry(fd);
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100197 fdtab[fd].state = 0;
Willy Tarreau6ea20b12012-11-11 16:05:19 +0100198
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200199 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
200 fdinfo[fd].port_range = NULL;
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200201 fdtab[fd].owner = NULL;
Willy Tarreauebc78d72018-01-20 23:53:50 +0100202 fdtab[fd].update_mask &= ~tid_bit;
Willy Tarreau1720abd2012-11-11 17:08:32 +0100203 fdtab[fd].new = 0;
Willy Tarreauf65610a2017-10-31 16:06:06 +0100204 fdtab[fd].thread_mask = 0;
Willy Tarreauc9c83782018-01-17 18:44:46 +0100205 if (do_close) {
206 fdtab[fd].polled_mask = 0;
Christopher Fauletd531f882017-06-01 16:55:03 +0200207 close(fd);
Willy Tarreauc9c83782018-01-17 18:44:46 +0100208 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100209 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200210
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100211 HA_SPIN_LOCK(FDTAB_LOCK, &fdtab_lock);
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200212 while ((maxfd-1 >= 0) && !fdtab[maxfd-1].owner)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200213 maxfd--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100214 HA_SPIN_UNLOCK(FDTAB_LOCK, &fdtab_lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200216
Olivier Houchard1fc05162017-04-06 01:05:05 +0200217/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
218 * The file descriptor is also closed.
219 */
220void fd_delete(int fd)
221{
222 fd_dodelete(fd, 1);
223}
224
225/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
226 * The file descriptor is kept open.
227 */
228void fd_remove(int fd)
229{
230 fd_dodelete(fd, 0);
231}
232
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100233/* Scan and process the cached events. This should be called right after
Willy Tarreau5be2f352014-11-19 19:43:05 +0100234 * the poller. The loop may cause new entries to be created, for example
235 * if a listener causes an accept() to initiate a new incoming connection
236 * wanting to attempt an recv().
Willy Tarreau09f24562012-11-11 16:43:45 +0100237 */
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100238void fd_process_cached_events()
Willy Tarreau09f24562012-11-11 16:43:45 +0100239{
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100240 int fd, entry, e;
Willy Tarreau09f24562012-11-11 16:43:45 +0100241
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100242 HA_RWLOCK_RDLOCK(FDCACHE_LOCK, &fdcache_lock);
Christopher Faulet69553fe2018-01-15 11:57:03 +0100243 fd_cache_mask &= ~tid_bit;
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100244 for (entry = 0; entry < fd_cache_num; ) {
245 fd = fd_cache[entry];
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200246
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100247 if (!(fdtab[fd].thread_mask & tid_bit)) {
248 activity[tid].fd_skip++;
Christopher Faulet8aae8b12017-08-30 10:56:25 +0200249 goto next;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100250 }
Christopher Faulet69553fe2018-01-15 11:57:03 +0100251
252 fd_cache_mask |= tid_bit;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100253 if (HA_SPIN_TRYLOCK(FD_LOCK, &fdtab[fd].lock)) {
254 activity[tid].fd_lock++;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200255 goto next;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100256 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200257
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100258 HA_RWLOCK_RDUNLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau09f24562012-11-11 16:43:45 +0100259
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200260 e = fdtab[fd].state;
Willy Tarreau09f24562012-11-11 16:43:45 +0100261 fdtab[fd].ev &= FD_POLL_STICKY;
262
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100263 if ((e & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R))
Willy Tarreau09f24562012-11-11 16:43:45 +0100264 fdtab[fd].ev |= FD_POLL_IN;
265
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100266 if ((e & (FD_EV_READY_W | FD_EV_ACTIVE_W)) == (FD_EV_READY_W | FD_EV_ACTIVE_W))
Willy Tarreau09f24562012-11-11 16:43:45 +0100267 fdtab[fd].ev |= FD_POLL_OUT;
268
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200269 if (fdtab[fd].iocb && fdtab[fd].owner && fdtab[fd].ev) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100270 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreau09f24562012-11-11 16:43:45 +0100271 fdtab[fd].iocb(fd);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200272 }
273 else {
Willy Tarreau5be2f352014-11-19 19:43:05 +0100274 fd_release_cache_entry(fd);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100275 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200276 }
Willy Tarreau09f24562012-11-11 16:43:45 +0100277
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100278 HA_RWLOCK_RDLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100279 /* If the fd was removed from the cache, it has been
Willy Tarreau09f24562012-11-11 16:43:45 +0100280 * replaced by the next one that we don't want to skip !
281 */
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100282 if (entry < fd_cache_num && fd_cache[entry] != fd) {
283 activity[tid].fd_del++;
Willy Tarreau09f24562012-11-11 16:43:45 +0100284 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100285 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200286 next:
Willy Tarreau033cd9d2014-01-25 19:24:15 +0100287 entry++;
Willy Tarreau09f24562012-11-11 16:43:45 +0100288 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100289 HA_RWLOCK_RDUNLOCK(FDCACHE_LOCK, &fdcache_lock);
Willy Tarreau09f24562012-11-11 16:43:45 +0100290}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291
Willy Tarreau4f60f162007-04-08 16:39:58 +0200292/* disable the specified poller */
293void disable_poller(const char *poller_name)
294{
295 int p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200296
Willy Tarreau4f60f162007-04-08 16:39:58 +0200297 for (p = 0; p < nbpollers; p++)
298 if (strcmp(pollers[p].name, poller_name) == 0)
299 pollers[p].pref = 0;
300}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200302/* Initialize the pollers per thread */
303static int init_pollers_per_thread()
304{
305 if ((fd_updt = calloc(global.maxsock, sizeof(*fd_updt))) == NULL)
306 return 0;
307 return 1;
308}
309
310/* Deinitialize the pollers per thread */
311static void deinit_pollers_per_thread()
312{
313 free(fd_updt);
314 fd_updt = NULL;
315}
316
Willy Tarreaubaaee002006-06-26 02:48:02 +0200317/*
Willy Tarreau4f60f162007-04-08 16:39:58 +0200318 * Initialize the pollers till the best one is found.
319 * If none works, returns 0, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200321int init_pollers()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200322{
Willy Tarreau4f60f162007-04-08 16:39:58 +0200323 int p;
324 struct poller *bp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325
Christopher Faulet63fe6522017-08-31 17:52:09 +0200326 if ((fdtab = calloc(global.maxsock, sizeof(struct fdtab))) == NULL)
327 goto fail_tab;
328
329 if ((fdinfo = calloc(global.maxsock, sizeof(struct fdinfo))) == NULL)
330 goto fail_info;
331
332 if ((fd_cache = calloc(global.maxsock, sizeof(*fd_cache))) == NULL)
Willy Tarreau16f649c2014-01-25 19:10:48 +0100333 goto fail_cache;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100334
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200335 hap_register_per_thread_init(init_pollers_per_thread);
336 hap_register_per_thread_deinit(deinit_pollers_per_thread);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200337
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200338 for (p = 0; p < global.maxsock; p++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100339 HA_SPIN_INIT(&fdtab[p].lock);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200340
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100341 HA_SPIN_INIT(&fdtab_lock);
342 HA_RWLOCK_INIT(&fdcache_lock);
343 HA_SPIN_INIT(&poll_lock);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200344 do {
345 bp = NULL;
346 for (p = 0; p < nbpollers; p++)
347 if (!bp || (pollers[p].pref > bp->pref))
348 bp = &pollers[p];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349
Willy Tarreau4f60f162007-04-08 16:39:58 +0200350 if (!bp || bp->pref == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351 break;
352
Willy Tarreau4f60f162007-04-08 16:39:58 +0200353 if (bp->init(bp)) {
354 memcpy(&cur_poller, bp, sizeof(*bp));
355 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200357 } while (!bp || bp->pref == 0);
358 return 0;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100359
Willy Tarreau16f649c2014-01-25 19:10:48 +0100360 fail_cache:
Christopher Faulet63fe6522017-08-31 17:52:09 +0200361 free(fdinfo);
362 fail_info:
363 free(fdtab);
364 fail_tab:
Willy Tarreau7be79a42012-11-11 15:02:54 +0100365 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366}
367
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200369 * Deinitialize the pollers.
370 */
371void deinit_pollers() {
372
373 struct poller *bp;
374 int p;
375
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200376 for (p = 0; p < global.maxsock; p++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100377 HA_SPIN_DESTROY(&fdtab[p].lock);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200378
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200379 for (p = 0; p < nbpollers; p++) {
380 bp = &pollers[p];
381
382 if (bp && bp->pref)
383 bp->term(bp);
384 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200385
Christopher Faulet63fe6522017-08-31 17:52:09 +0200386 free(fd_cache); fd_cache = NULL;
387 free(fdinfo); fdinfo = NULL;
388 free(fdtab); fdtab = NULL;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200389
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100390 HA_SPIN_DESTROY(&fdtab_lock);
391 HA_RWLOCK_DESTROY(&fdcache_lock);
392 HA_SPIN_DESTROY(&poll_lock);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200393}
394
395/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200396 * Lists the known pollers on <out>.
397 * Should be performed only before initialization.
398 */
399int list_pollers(FILE *out)
400{
401 int p;
402 int last, next;
403 int usable;
404 struct poller *bp;
405
406 fprintf(out, "Available polling systems :\n");
407
408 usable = 0;
409 bp = NULL;
410 last = next = -1;
411 while (1) {
412 for (p = 0; p < nbpollers; p++) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200413 if ((next < 0 || pollers[p].pref > next)
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100414 && (last < 0 || pollers[p].pref < last)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200415 next = pollers[p].pref;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100416 if (!bp || (pollers[p].pref > bp->pref))
417 bp = &pollers[p];
418 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200419 }
420
421 if (next == -1)
422 break;
423
424 for (p = 0; p < nbpollers; p++) {
425 if (pollers[p].pref == next) {
426 fprintf(out, " %10s : ", pollers[p].name);
427 if (pollers[p].pref == 0)
428 fprintf(out, "disabled, ");
429 else
430 fprintf(out, "pref=%3d, ", pollers[p].pref);
431 if (pollers[p].test(&pollers[p])) {
432 fprintf(out, " test result OK");
433 if (next > 0)
434 usable++;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100435 } else {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200436 fprintf(out, " test result FAILED");
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100437 if (bp == &pollers[p])
438 bp = NULL;
439 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200440 fprintf(out, "\n");
441 }
442 }
443 last = next;
444 next = -1;
445 };
446 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
447 return 0;
448}
449
450/*
451 * Some pollers may lose their connection after a fork(). It may be necessary
452 * to create initialize part of them again. Returns 0 in case of failure,
453 * otherwise 1. The fork() function may be NULL if unused. In case of error,
454 * the the current poller is destroyed and the caller is responsible for trying
455 * another one by calling init_pollers() again.
456 */
457int fork_poller()
458{
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200459 int fd;
460 for (fd = 0; fd <= maxfd; fd++) {
461 if (fdtab[fd].owner) {
462 fdtab[fd].cloned = 1;
463 }
464 }
465
Willy Tarreau2ff76222007-04-09 19:29:56 +0200466 if (cur_poller.fork) {
467 if (cur_poller.fork(&cur_poller))
468 return 1;
469 cur_poller.term(&cur_poller);
470 return 0;
471 }
472 return 1;
473}
474
475/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476 * Local variables:
477 * c-indent-level: 8
478 * c-basic-offset: 8
479 * End:
480 */