blob: 5d63cc3713fbc8af6f6f6a3a6d3835ba94bf37f9 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * File descriptors management functions.
3 *
Willy Tarreau7be79a42012-11-11 15:02:54 +01004 * Copyright 2000-2012 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 Tarreau7be79a42012-11-11 15:02:54 +010011 * This code implements "speculative I/O". The principle is to try to perform
12 * expected I/O before registering the events in the poller. Each time this
13 * succeeds, it saves a possibly expensive system call to set the event. It
14 * generally succeeds for all reads after an accept(), and for writes after a
15 * connect(). It also improves performance for streaming connections because
16 * even if only one side is polled, the other one may react accordingly
17 * depending on the fill level of the buffer. This behaviour is also the only
18 * one compatible with event-based pollers (eg: EPOLL_ET).
19 *
20 * More importantly, it enables I/O operations that are backed by invisible
21 * buffers. For example, SSL is able to read a whole socket buffer and not
22 * deliver it to the application buffer because it's full. Unfortunately, it
23 * won't be reported by a poller anymore until some new activity happens. The
24 * only way to call it again thus is to perform speculative I/O as soon as
25 * reading on the FD is enabled again.
26 *
27 * The speculative I/O uses a list of expected events and a list of updates.
28 * Expected events are events that are expected to come and that we must report
29 * to the application until it asks to stop or to poll. Updates are new requests
30 * for changing an FD state. Updates are the only way to create new events. This
31 * is important because it means that the number of speculative events cannot
32 * increase between updates and will only grow one at a time while processing
33 * updates. All updates must always be processed, though events might be
34 * processed by small batches if required.
35 *
36 * There is no direct link between the FD and the updates list. There is only a
37 * bit in the fdtab[] to indicate than a file descriptor is already present in
38 * the updates list. Once an fd is present in the updates list, it will have to
39 * be considered even if its changes are reverted in the middle or if the fd is
40 * replaced.
41 *
42 * It is important to understand that as long as all expected events are
43 * processed, they might starve the polled events, especially because polled
44 * I/O starvation quickly induces more speculative I/O. One solution to this
45 * consists in only processing a part of the events at once, but one drawback
46 * is that unhandled events will still wake the poller up. Using an event-driven
47 * poller such as EPOLL_ET will solve this issue though.
48 *
49 * A file descriptor has a distinct state for each direction. This state is a
50 * combination of two bits :
51 * bit 0 = active Y/N : is set if the FD is active, which means that its
52 * handler will be called without prior polling ;
53 * bit 1 = polled Y/N : is set if the FD was subscribed to polling
54 *
55 * It is perfectly valid to have both bits set at a time, which generally means
56 * that the FD was reported by polling, was marked active and not yet unpolled.
57 * Such a state must not last long to avoid unneeded wakeups.
58 *
59 * The state of the FD as of last change is preserved in two other bits. These
60 * ones are useful to save a significant amount of system calls during state
61 * changes, because there is no need to update the FD status in the system until
62 * we're about to call the poller.
63 *
64 * Since we do not want to scan all the FD list to find speculative I/O events,
65 * we store them in a list consisting in a linear array holding only the FD
66 * indexes right now. Note that a closed FD cannot exist in the spec list,
67 * because it is closed by fd_delete() which in turn calls __fd_clo() which
68 * always removes it from the list.
69 *
70 * For efficiency reasons, we will store the Read and Write bits interlaced to
71 * form a 4-bit field, so that we can simply shift the value right by 0/1 and
72 * get what we want :
73 * 3 2 1 0
74 * Wp Rp Wa Ra
75 *
76 * The FD array has to hold a back reference to the speculative list. This
77 * reference is always valid unless the FD if currently being polled and not
78 * updated (in which case the reference points to index 0).
79 *
80 * We store the FD state in the 4 lower bits of fdtab[fd].spec_e, and save the
81 * previous state upon changes in the 4 higher bits, so that changes are easy
82 * to spot.
Willy Tarreaubaaee002006-06-26 02:48:02 +020083 */
84
Willy Tarreau2ff76222007-04-09 19:29:56 +020085#include <stdio.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020086#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020087#include <unistd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020088#include <sys/types.h>
89
Willy Tarreau2dd0d472006-06-29 17:53:05 +020090#include <common/compat.h>
91#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020092
Willy Tarreau7be79a42012-11-11 15:02:54 +010093#include <types/global.h>
94
Willy Tarreau2a429502006-10-15 14:52:29 +020095#include <proto/fd.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020096#include <proto/port_range.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020097
98struct fdtab *fdtab = NULL; /* array of all the file descriptors */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +020099struct fdinfo *fdinfo = NULL; /* less-often used infos for file descriptors */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100int maxfd; /* # of the highest fd + 1 */
101int totalconn; /* total # of terminated sessions */
102int actconn; /* # of active sessions */
103
Willy Tarreau4f60f162007-04-08 16:39:58 +0200104struct poller pollers[MAX_POLLERS];
105struct poller cur_poller;
106int nbpollers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107
Willy Tarreau7be79a42012-11-11 15:02:54 +0100108/* FD status is defined by the poller's status and by the speculative I/O list */
109int fd_nbspec = 0; // number of speculative events in the list
110int fd_nbupdt = 0; // number of updates in the list
111unsigned int *fd_spec = NULL; // speculative I/O list
112unsigned int *fd_updt = NULL; // FD updates list
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113
Willy Tarreau4f60f162007-04-08 16:39:58 +0200114/* Deletes an FD from the fdsets, and recomputes the maxfd limit.
115 * The file descriptor is also closed.
116 */
117void fd_delete(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118{
Willy Tarreau49b046d2012-08-09 12:11:58 +0200119 cur_poller.clo(fd);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200120 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
121 fdinfo[fd].port_range = NULL;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200122 close(fd);
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200123 fdtab[fd].owner = NULL;
Willy Tarreau1720abd2012-11-11 17:08:32 +0100124 fdtab[fd].new = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200126 while ((maxfd-1 >= 0) && !fdtab[maxfd-1].owner)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200127 maxfd--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129
Willy Tarreaubaaee002006-06-26 02:48:02 +0200130
Willy Tarreau4f60f162007-04-08 16:39:58 +0200131/* disable the specified poller */
132void disable_poller(const char *poller_name)
133{
134 int p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200135
Willy Tarreau4f60f162007-04-08 16:39:58 +0200136 for (p = 0; p < nbpollers; p++)
137 if (strcmp(pollers[p].name, poller_name) == 0)
138 pollers[p].pref = 0;
139}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200140
141/*
Willy Tarreau4f60f162007-04-08 16:39:58 +0200142 * Initialize the pollers till the best one is found.
143 * If none works, returns 0, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200144 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200145int init_pollers()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200146{
Willy Tarreau4f60f162007-04-08 16:39:58 +0200147 int p;
148 struct poller *bp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149
Willy Tarreau7be79a42012-11-11 15:02:54 +0100150 if ((fd_spec = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL)
151 goto fail_spec;
152
153 if ((fd_updt = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL)
154 goto fail_updt;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200155
Willy Tarreau4f60f162007-04-08 16:39:58 +0200156 do {
157 bp = NULL;
158 for (p = 0; p < nbpollers; p++)
159 if (!bp || (pollers[p].pref > bp->pref))
160 bp = &pollers[p];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200161
Willy Tarreau4f60f162007-04-08 16:39:58 +0200162 if (!bp || bp->pref == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163 break;
164
Willy Tarreau4f60f162007-04-08 16:39:58 +0200165 if (bp->init(bp)) {
166 memcpy(&cur_poller, bp, sizeof(*bp));
167 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200168 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200169 } while (!bp || bp->pref == 0);
170 return 0;
Willy Tarreau7be79a42012-11-11 15:02:54 +0100171
172 fail_updt:
173 free(fd_spec);
174 fail_spec:
175 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200176}
177
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178/*
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200179 * Deinitialize the pollers.
180 */
181void deinit_pollers() {
182
183 struct poller *bp;
184 int p;
185
186 for (p = 0; p < nbpollers; p++) {
187 bp = &pollers[p];
188
189 if (bp && bp->pref)
190 bp->term(bp);
191 }
Willy Tarreau7be79a42012-11-11 15:02:54 +0100192
193 free(fd_updt);
194 free(fd_spec);
195 fd_updt = NULL;
196 fd_spec = NULL;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200197}
198
199/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200200 * Lists the known pollers on <out>.
201 * Should be performed only before initialization.
202 */
203int list_pollers(FILE *out)
204{
205 int p;
206 int last, next;
207 int usable;
208 struct poller *bp;
209
210 fprintf(out, "Available polling systems :\n");
211
212 usable = 0;
213 bp = NULL;
214 last = next = -1;
215 while (1) {
216 for (p = 0; p < nbpollers; p++) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200217 if ((next < 0 || pollers[p].pref > next)
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100218 && (last < 0 || pollers[p].pref < last)) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200219 next = pollers[p].pref;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100220 if (!bp || (pollers[p].pref > bp->pref))
221 bp = &pollers[p];
222 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200223 }
224
225 if (next == -1)
226 break;
227
228 for (p = 0; p < nbpollers; p++) {
229 if (pollers[p].pref == next) {
230 fprintf(out, " %10s : ", pollers[p].name);
231 if (pollers[p].pref == 0)
232 fprintf(out, "disabled, ");
233 else
234 fprintf(out, "pref=%3d, ", pollers[p].pref);
235 if (pollers[p].test(&pollers[p])) {
236 fprintf(out, " test result OK");
237 if (next > 0)
238 usable++;
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100239 } else {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200240 fprintf(out, " test result FAILED");
Willy Tarreaue79c3b22010-11-19 10:20:36 +0100241 if (bp == &pollers[p])
242 bp = NULL;
243 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200244 fprintf(out, "\n");
245 }
246 }
247 last = next;
248 next = -1;
249 };
250 fprintf(out, "Total: %d (%d usable), will use %s.\n", nbpollers, usable, bp ? bp->name : "none");
251 return 0;
252}
253
254/*
255 * Some pollers may lose their connection after a fork(). It may be necessary
256 * to create initialize part of them again. Returns 0 in case of failure,
257 * otherwise 1. The fork() function may be NULL if unused. In case of error,
258 * the the current poller is destroyed and the caller is responsible for trying
259 * another one by calling init_pollers() again.
260 */
261int fork_poller()
262{
263 if (cur_poller.fork) {
264 if (cur_poller.fork(&cur_poller))
265 return 1;
266 cur_poller.term(&cur_poller);
267 return 0;
268 }
269 return 1;
270}
271
272/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273 * Local variables:
274 * c-indent-level: 8
275 * c-basic-offset: 8
276 * End:
277 */