blob: a566917bac8855b07f6773667e21285e6091a111 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for linux epoll()
3 *
Willy Tarreau43d8fb22011-08-22 17:12:02 +02004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreau4f60f162007-04-08 16:39:58 +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 *
11 */
12
13#include <unistd.h>
14#include <sys/time.h>
15#include <sys/types.h>
16
17#include <common/compat.h>
18#include <common/config.h>
Willy Tarreau43d8fb22011-08-22 17:12:02 +020019#include <common/epoll.h>
Willy Tarreau58094f22007-04-10 01:33:20 +020020#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020021#include <common/ticks.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020022#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020023#include <common/tools.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020024
25#include <types/fd.h>
26#include <types/global.h>
27
Willy Tarreau332740d2009-05-10 09:57:21 +020028#include <proto/signal.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020029#include <proto/task.h>
30
Willy Tarreau58094f22007-04-10 01:33:20 +020031/* This is what we store in a list. It consists in old values and fds to detect changes. */
32struct fd_chg {
33 unsigned int prev:2; // previous state mask. New one is in fd_evts.
34 unsigned int fd:30; // file descriptor
35};
Willy Tarreau4f60f162007-04-08 16:39:58 +020036
Willy Tarreau58094f22007-04-10 01:33:20 +020037static int nbchanges = 0; // number of changes pending
38static struct fd_chg *chg_list = NULL; // list of changes
39static struct fd_chg **chg_ptr = NULL; // per-fd changes
40
41/* Each 32-bit word contains 2-bit descriptors of the latest state for 16 FDs :
42 * desc = (u32 >> (2*fd)) & 3
43 * desc = 0 : FD not set
44 * 1 : WRITE not set, READ set
45 * 2 : WRITE set, READ not set
46 * 3 : WRITE set, READ set
47 */
48
49static uint32_t *fd_evts;
Willy Tarreau4f60f162007-04-08 16:39:58 +020050
51/* private data */
52static struct epoll_event *epoll_events;
53static int epoll_fd;
54
Willy Tarreau58094f22007-04-10 01:33:20 +020055/* This structure may be used for any purpose. Warning! do not use it in
56 * recursive functions !
57 */
58static struct epoll_event ev;
59
60/* converts a direction to a single bitmask.
61 * 0 => 1
62 * 1 => 2
63 */
64#define DIR2MSK(dir) ((dir) + 1)
65
66/* converts an FD to an fd_evts offset and to a bit shift */
67#define FD2OFS(fd) ((uint32_t)(fd) >> 4)
68#define FD2BIT(fd) (((uint32_t)(fd) & 0xF) << 1)
69#define FD2MSK(fd) (3 << FD2BIT(fd))
Willy Tarreau4f60f162007-04-08 16:39:58 +020070
71/*
Willy Tarreau58094f22007-04-10 01:33:20 +020072 * Returns non-zero if direction <dir> is already set for <fd>.
Willy Tarreau4f60f162007-04-08 16:39:58 +020073 */
Willy Tarreau63455a92007-04-09 15:34:49 +020074REGPRM2 static int __fd_is_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020075{
Willy Tarreau58094f22007-04-10 01:33:20 +020076 return (fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(dir);
Willy Tarreau4f60f162007-04-08 16:39:58 +020077}
78
Willy Tarreau58094f22007-04-10 01:33:20 +020079/*
80 * Adds, mods or deletes <fd> according to current status and to new desired
81 * mask <dmask> :
82 *
83 * 0 = nothing
84 * 1 = EPOLLIN
85 * 2 = EPOLLOUT
86 * 3 = EPOLLIN | EPOLLOUT
87 *
88 */
89static int dmsk2event[4] = { 0, EPOLLIN, EPOLLOUT, EPOLLIN | EPOLLOUT };
90
91
92REGPRM2 static void fd_flush_changes()
Willy Tarreau4f60f162007-04-08 16:39:58 +020093{
Willy Tarreau58094f22007-04-10 01:33:20 +020094 uint32_t ofs;
95 int opcode;
96 int prev, next;
97 int chg, fd;
98
99 for (chg = 0; chg < nbchanges; chg++) {
100 prev = chg_list[chg].prev;
101 fd = chg_list[chg].fd;
102 chg_ptr[fd] = NULL;
103
104 ofs = FD2OFS(fd);
105 next = (fd_evts[ofs] >> FD2BIT(fd)) & 3;
106
107 if (prev == next)
108 /* if the value was unchanged, do nothing */
109 continue;
110
111 ev.events = dmsk2event[next];
112 ev.data.fd = fd;
113
114 if (prev) {
115 if (!next) {
116 /* we want to delete it now */
117 opcode = EPOLL_CTL_DEL;
118 } else {
119 /* we want to switch it */
120 opcode = EPOLL_CTL_MOD;
121 }
122 } else {
123 /* the FD did not exist, let's add it */
124 opcode = EPOLL_CTL_ADD;
125 }
126
127 epoll_ctl(epoll_fd, opcode, fd, &ev);
128 }
129 nbchanges = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200130}
131
Willy Tarreau58094f22007-04-10 01:33:20 +0200132REGPRM2 static void alloc_chg_list(const int fd, int old_evt)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200133{
Willy Tarreau58094f22007-04-10 01:33:20 +0200134 struct fd_chg *ptr;
135
Willy Tarreau70bcfb72008-01-27 02:21:53 +0100136 if (unlikely(chg_ptr[fd] != NULL))
Willy Tarreau58094f22007-04-10 01:33:20 +0200137 return;
138
139#if LIMIT_NUMBER_OF_CHANGES
140 if (nbchanges > 2)
141 fd_flush_changes();
142#endif
143
144 ptr = &chg_list[nbchanges++];
145 chg_ptr[fd] = ptr;
146 ptr->fd = fd;
147 ptr->prev = old_evt;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200148}
149
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200150REGPRM2 static void __fd_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200151{
Willy Tarreau58094f22007-04-10 01:33:20 +0200152 uint32_t ofs = FD2OFS(fd);
153 uint32_t dmsk = DIR2MSK(dir);
154 uint32_t old_evt;
155
156 old_evt = fd_evts[ofs] >> FD2BIT(fd);
157 old_evt &= 3;
158 if (unlikely(old_evt & dmsk))
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200159 return;
Willy Tarreau58094f22007-04-10 01:33:20 +0200160
161 alloc_chg_list(fd, old_evt);
162 dmsk <<= FD2BIT(fd);
163 fd_evts[ofs] |= dmsk;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200164}
165
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200166REGPRM2 static void __fd_clr(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200167{
Willy Tarreau58094f22007-04-10 01:33:20 +0200168 uint32_t ofs = FD2OFS(fd);
169 uint32_t dmsk = DIR2MSK(dir);
170 uint32_t old_evt;
171
172 old_evt = fd_evts[ofs] >> FD2BIT(fd);
173 old_evt &= 3;
174 if (unlikely(!(old_evt & dmsk)))
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200175 return;
Willy Tarreau58094f22007-04-10 01:33:20 +0200176
177 alloc_chg_list(fd, old_evt);
178 dmsk <<= FD2BIT(fd);
179 fd_evts[ofs] &= ~dmsk;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200180}
181
Willy Tarreau58094f22007-04-10 01:33:20 +0200182REGPRM1 static void __fd_rem(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200183{
Willy Tarreau58094f22007-04-10 01:33:20 +0200184 uint32_t ofs = FD2OFS(fd);
185
186 if (unlikely(!((fd_evts[ofs] >> FD2BIT(fd)) & 3)))
187 return;
188
189 alloc_chg_list(fd, 0);
190 fd_evts[ofs] &= ~FD2MSK(fd);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200191}
192
Willy Tarreau58094f22007-04-10 01:33:20 +0200193/*
194 * On valid epoll() implementations, a call to close() automatically removes
195 * the fds. This means that the FD will appear as previously unset.
196 */
197REGPRM1 static void __fd_clo(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200198{
Willy Tarreau58094f22007-04-10 01:33:20 +0200199 struct fd_chg *ptr;
200 fd_evts[FD2OFS(fd)] &= ~FD2MSK(fd);
201 ptr = chg_ptr[fd];
202 if (ptr) {
203 ptr->prev = 0;
204 chg_ptr[fd] = NULL;
205 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200206}
207
Willy Tarreau4f60f162007-04-08 16:39:58 +0200208/*
209 * epoll() poller
210 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200211REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200212{
213 int status;
214 int fd;
Willy Tarreau58094f22007-04-10 01:33:20 +0200215 int count;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200216 int wait_time;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200217
Willy Tarreau58094f22007-04-10 01:33:20 +0200218 if (likely(nbchanges))
219 fd_flush_changes();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200220
Willy Tarreau4f60f162007-04-08 16:39:58 +0200221 /* now let's wait for events */
Willy Tarreau332740d2009-05-10 09:57:21 +0200222 if (run_queue || signal_queue_len)
Willy Tarreau3a628112008-06-13 21:06:56 +0200223 wait_time = 0;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200224 else if (!exp)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200225 wait_time = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200226 else if (tick_is_expired(exp, now_ms))
Willy Tarreaubdefc512007-05-14 02:02:04 +0200227 wait_time = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200228 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200229 wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200230 if (wait_time > MAX_DELAY_MS)
231 wait_time = MAX_DELAY_MS;
232 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200233
Willy Tarreau1db37712007-06-03 17:16:49 +0200234 fd = MIN(maxfd, global.tune.maxpollevents);
Willy Tarreau45a12512011-09-10 16:56:42 +0200235 gettimeofday(&before_poll, NULL);
Willy Tarreau1db37712007-06-03 17:16:49 +0200236 status = epoll_wait(epoll_fd, epoll_events, fd, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200237 tv_update_date(wait_time, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200238 measure_idle();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200239
240 for (count = 0; count < status; count++) {
Willy Tarreau491c4982012-07-06 11:16:01 +0200241 int e = epoll_events[count].events;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200242 fd = epoll_events[count].data.fd;
243
Willy Tarreau076be252012-07-06 16:02:29 +0200244 if (!fdtab[fd].owner)
245 continue;
246
Willy Tarreau491c4982012-07-06 11:16:01 +0200247 /* it looks complicated but gcc can optimize it away when constants
248 * have same values.
249 */
250 fdtab[fd].ev &= FD_POLL_STICKY;
251 fdtab[fd].ev |=
252 ((e & EPOLLIN ) ? FD_POLL_IN : 0) |
253 ((e & EPOLLPRI) ? FD_POLL_PRI : 0) |
254 ((e & EPOLLOUT) ? FD_POLL_OUT : 0) |
255 ((e & EPOLLERR) ? FD_POLL_ERR : 0) |
256 ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
257
Willy Tarreau9845e752012-07-06 11:44:28 +0200258 if (fdtab[fd].iocb && fdtab[fd].owner && fdtab[fd].ev)
259 fdtab[fd].iocb(fd);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200260 }
261}
262
263/*
Willy Tarreaue54e9172007-04-09 09:23:31 +0200264 * Initialization of the epoll() poller.
265 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
266 * disables the poller by setting its pref to 0.
267 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200268REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200269{
Willy Tarreau58094f22007-04-10 01:33:20 +0200270 __label__ fail_chg_ptr, fail_chg_list, fail_fdevt, fail_ee, fail_fd;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200271 int fd_set_bytes;
272
273 p->private = NULL;
Willy Tarreau58094f22007-04-10 01:33:20 +0200274 fd_set_bytes = 4 * (global.maxsock + 15) / 16;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200275
276 epoll_fd = epoll_create(global.maxsock + 1);
277 if (epoll_fd < 0)
278 goto fail_fd;
279
280 epoll_events = (struct epoll_event*)
Willy Tarreau1db37712007-06-03 17:16:49 +0200281 calloc(1, sizeof(struct epoll_event) * global.tune.maxpollevents);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200282
283 if (epoll_events == NULL)
284 goto fail_ee;
285
Willy Tarreau58094f22007-04-10 01:33:20 +0200286 if ((fd_evts = (uint32_t *)calloc(1, fd_set_bytes)) == NULL)
287 goto fail_fdevt;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200288
Willy Tarreau58094f22007-04-10 01:33:20 +0200289 chg_list = (struct fd_chg *)calloc(1, sizeof(struct fd_chg) * global.maxsock);
290 if (chg_list == NULL)
291 goto fail_chg_list;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200292
Willy Tarreau58094f22007-04-10 01:33:20 +0200293 chg_ptr = (struct fd_chg **)calloc(1, sizeof(struct fd_chg*) * global.maxsock);
294 if (chg_ptr == NULL)
295 goto fail_chg_ptr;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200296
297 return 1;
298
Willy Tarreau58094f22007-04-10 01:33:20 +0200299 fail_chg_ptr:
300 free(chg_list);
301 fail_chg_list:
302 free(fd_evts);
303 fail_fdevt:
Willy Tarreaue54e9172007-04-09 09:23:31 +0200304 free(epoll_events);
305 fail_ee:
306 close(epoll_fd);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200307 epoll_fd = -1;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200308 fail_fd:
309 p->pref = 0;
310 return 0;
311}
312
313/*
314 * Termination of the epoll() poller.
315 * Memory is released and the poller is marked as unselectable.
316 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200317REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200318{
Willy Tarreau58094f22007-04-10 01:33:20 +0200319 fd_flush_changes();
Willy Tarreaue54e9172007-04-09 09:23:31 +0200320
Willy Tarreaua534fea2008-08-03 12:19:50 +0200321 free(chg_ptr);
322 free(chg_list);
323 free(fd_evts);
324 free(epoll_events);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200325
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200326 if (epoll_fd >= 0) {
327 close(epoll_fd);
328 epoll_fd = -1;
329 }
Willy Tarreaue54e9172007-04-09 09:23:31 +0200330
Willy Tarreau58094f22007-04-10 01:33:20 +0200331 chg_ptr = NULL;
332 chg_list = NULL;
333 fd_evts = NULL;
334 epoll_events = NULL;
335
Willy Tarreaue54e9172007-04-09 09:23:31 +0200336 p->private = NULL;
337 p->pref = 0;
338}
339
340/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200341 * Check that the poller works.
342 * Returns 1 if OK, otherwise 0.
343 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200344REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200345{
346 int fd;
347
348 fd = epoll_create(global.maxsock + 1);
349 if (fd < 0)
350 return 0;
351 close(fd);
352 return 1;
353}
354
355/*
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200356 * Recreate the epoll file descriptor after a fork(). Returns 1 if OK,
357 * otherwise 0. It will ensure that all processes will not share their
358 * epoll_fd. Some side effects were encountered because of this, such
359 * as epoll_wait() returning an FD which was previously deleted.
360 */
361REGPRM1 static int _do_fork(struct poller *p)
362{
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200363 if (epoll_fd >= 0)
364 close(epoll_fd);
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200365 epoll_fd = epoll_create(global.maxsock + 1);
366 if (epoll_fd < 0)
367 return 0;
368 return 1;
369}
370
371/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200372 * It is a constructor, which means that it will automatically be called before
373 * main(). This is GCC-specific but it works at least since 2.95.
374 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200375 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200376__attribute__((constructor))
377static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200378{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200379 struct poller *p;
380
381 if (nbpollers >= MAX_POLLERS)
382 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200383
384 epoll_fd = -1;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200385 p = &pollers[nbpollers++];
386
Willy Tarreau4f60f162007-04-08 16:39:58 +0200387 p->name = "epoll";
388 p->pref = 300;
389 p->private = NULL;
390
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200391 p->test = _do_test;
392 p->init = _do_init;
393 p->term = _do_term;
394 p->poll = _do_poll;
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200395 p->fork = _do_fork;
Willy Tarreau58094f22007-04-10 01:33:20 +0200396
397 p->is_set = __fd_is_set;
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200398 p->set = __fd_set;
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200399 p->wai = __fd_set;
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200400 p->clr = __fd_clr;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200401 p->rem = __fd_rem;
402 p->clo = __fd_clo;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200403}
404
405
406/*
407 * Local variables:
408 * c-indent-level: 8
409 * c-basic-offset: 8
410 * End:
411 */