blob: b976868ea7a2737004c468dd7ce83bb8e1224af7 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for linux epoll()
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 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 Tarreau58094f22007-04-10 01:33:20 +020019#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020020#include <common/ticks.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020021#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020022#include <common/tools.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020023
24#include <types/fd.h>
25#include <types/global.h>
26
Willy Tarreau332740d2009-05-10 09:57:21 +020027#include <proto/signal.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020028#include <proto/task.h>
29
30#if defined(USE_MY_EPOLL)
Willy Tarreau69801b82007-04-09 15:28:51 +020031#include <common/epoll.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020032#include <errno.h>
33#include <sys/syscall.h>
Willy Tarreaub40d4202007-04-15 23:57:41 +020034static _syscall1 (int, epoll_create, int, size);
35static _syscall4 (int, epoll_ctl, int, epfd, int, op, int, fd, struct epoll_event *, event);
36static _syscall4 (int, epoll_wait, int, epfd, struct epoll_event *, events, int, maxevents, int, timeout);
Willy Tarreau69801b82007-04-09 15:28:51 +020037#else
38#include <sys/epoll.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020039#endif
40
Willy Tarreau58094f22007-04-10 01:33:20 +020041/* This is what we store in a list. It consists in old values and fds to detect changes. */
42struct fd_chg {
43 unsigned int prev:2; // previous state mask. New one is in fd_evts.
44 unsigned int fd:30; // file descriptor
45};
Willy Tarreau4f60f162007-04-08 16:39:58 +020046
Willy Tarreau58094f22007-04-10 01:33:20 +020047static int nbchanges = 0; // number of changes pending
48static struct fd_chg *chg_list = NULL; // list of changes
49static struct fd_chg **chg_ptr = NULL; // per-fd changes
50
51/* Each 32-bit word contains 2-bit descriptors of the latest state for 16 FDs :
52 * desc = (u32 >> (2*fd)) & 3
53 * desc = 0 : FD not set
54 * 1 : WRITE not set, READ set
55 * 2 : WRITE set, READ not set
56 * 3 : WRITE set, READ set
57 */
58
59static uint32_t *fd_evts;
Willy Tarreau4f60f162007-04-08 16:39:58 +020060
61/* private data */
62static struct epoll_event *epoll_events;
63static int epoll_fd;
64
Willy Tarreau58094f22007-04-10 01:33:20 +020065/* This structure may be used for any purpose. Warning! do not use it in
66 * recursive functions !
67 */
68static struct epoll_event ev;
69
70/* converts a direction to a single bitmask.
71 * 0 => 1
72 * 1 => 2
73 */
74#define DIR2MSK(dir) ((dir) + 1)
75
76/* converts an FD to an fd_evts offset and to a bit shift */
77#define FD2OFS(fd) ((uint32_t)(fd) >> 4)
78#define FD2BIT(fd) (((uint32_t)(fd) & 0xF) << 1)
79#define FD2MSK(fd) (3 << FD2BIT(fd))
Willy Tarreau4f60f162007-04-08 16:39:58 +020080
81/*
Willy Tarreau58094f22007-04-10 01:33:20 +020082 * Returns non-zero if direction <dir> is already set for <fd>.
Willy Tarreau4f60f162007-04-08 16:39:58 +020083 */
Willy Tarreau63455a92007-04-09 15:34:49 +020084REGPRM2 static int __fd_is_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020085{
Willy Tarreau58094f22007-04-10 01:33:20 +020086 return (fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(dir);
Willy Tarreau4f60f162007-04-08 16:39:58 +020087}
88
Willy Tarreau58094f22007-04-10 01:33:20 +020089/*
90 * Adds, mods or deletes <fd> according to current status and to new desired
91 * mask <dmask> :
92 *
93 * 0 = nothing
94 * 1 = EPOLLIN
95 * 2 = EPOLLOUT
96 * 3 = EPOLLIN | EPOLLOUT
97 *
98 */
99static int dmsk2event[4] = { 0, EPOLLIN, EPOLLOUT, EPOLLIN | EPOLLOUT };
100
101
102REGPRM2 static void fd_flush_changes()
Willy Tarreau4f60f162007-04-08 16:39:58 +0200103{
Willy Tarreau58094f22007-04-10 01:33:20 +0200104 uint32_t ofs;
105 int opcode;
106 int prev, next;
107 int chg, fd;
108
109 for (chg = 0; chg < nbchanges; chg++) {
110 prev = chg_list[chg].prev;
111 fd = chg_list[chg].fd;
112 chg_ptr[fd] = NULL;
113
114 ofs = FD2OFS(fd);
115 next = (fd_evts[ofs] >> FD2BIT(fd)) & 3;
116
117 if (prev == next)
118 /* if the value was unchanged, do nothing */
119 continue;
120
121 ev.events = dmsk2event[next];
122 ev.data.fd = fd;
123
124 if (prev) {
125 if (!next) {
126 /* we want to delete it now */
127 opcode = EPOLL_CTL_DEL;
128 } else {
129 /* we want to switch it */
130 opcode = EPOLL_CTL_MOD;
131 }
132 } else {
133 /* the FD did not exist, let's add it */
134 opcode = EPOLL_CTL_ADD;
135 }
136
137 epoll_ctl(epoll_fd, opcode, fd, &ev);
138 }
139 nbchanges = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200140}
141
Willy Tarreau58094f22007-04-10 01:33:20 +0200142REGPRM2 static void alloc_chg_list(const int fd, int old_evt)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200143{
Willy Tarreau58094f22007-04-10 01:33:20 +0200144 struct fd_chg *ptr;
145
Willy Tarreau70bcfb72008-01-27 02:21:53 +0100146 if (unlikely(chg_ptr[fd] != NULL))
Willy Tarreau58094f22007-04-10 01:33:20 +0200147 return;
148
149#if LIMIT_NUMBER_OF_CHANGES
150 if (nbchanges > 2)
151 fd_flush_changes();
152#endif
153
154 ptr = &chg_list[nbchanges++];
155 chg_ptr[fd] = ptr;
156 ptr->fd = fd;
157 ptr->prev = old_evt;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200158}
159
Willy Tarreau58094f22007-04-10 01:33:20 +0200160REGPRM2 static int __fd_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200161{
Willy Tarreau58094f22007-04-10 01:33:20 +0200162 uint32_t ofs = FD2OFS(fd);
163 uint32_t dmsk = DIR2MSK(dir);
164 uint32_t old_evt;
165
166 old_evt = fd_evts[ofs] >> FD2BIT(fd);
167 old_evt &= 3;
168 if (unlikely(old_evt & dmsk))
169 return 0;
170
171 alloc_chg_list(fd, old_evt);
172 dmsk <<= FD2BIT(fd);
173 fd_evts[ofs] |= dmsk;
174 return 1;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200175}
176
Willy Tarreau58094f22007-04-10 01:33:20 +0200177REGPRM2 static int __fd_clr(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200178{
Willy Tarreau58094f22007-04-10 01:33:20 +0200179 uint32_t ofs = FD2OFS(fd);
180 uint32_t dmsk = DIR2MSK(dir);
181 uint32_t old_evt;
182
183 old_evt = fd_evts[ofs] >> FD2BIT(fd);
184 old_evt &= 3;
185 if (unlikely(!(old_evt & dmsk)))
186 return 0;
187
188 alloc_chg_list(fd, old_evt);
189 dmsk <<= FD2BIT(fd);
190 fd_evts[ofs] &= ~dmsk;
191 return 1;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200192}
193
Willy Tarreau58094f22007-04-10 01:33:20 +0200194REGPRM1 static void __fd_rem(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200195{
Willy Tarreau58094f22007-04-10 01:33:20 +0200196 uint32_t ofs = FD2OFS(fd);
197
198 if (unlikely(!((fd_evts[ofs] >> FD2BIT(fd)) & 3)))
199 return;
200
201 alloc_chg_list(fd, 0);
202 fd_evts[ofs] &= ~FD2MSK(fd);
203 return;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200204}
205
Willy Tarreau58094f22007-04-10 01:33:20 +0200206/*
207 * On valid epoll() implementations, a call to close() automatically removes
208 * the fds. This means that the FD will appear as previously unset.
209 */
210REGPRM1 static void __fd_clo(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200211{
Willy Tarreau58094f22007-04-10 01:33:20 +0200212 struct fd_chg *ptr;
213 fd_evts[FD2OFS(fd)] &= ~FD2MSK(fd);
214 ptr = chg_ptr[fd];
215 if (ptr) {
216 ptr->prev = 0;
217 chg_ptr[fd] = NULL;
218 }
219 return;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200220}
221
Willy Tarreau4f60f162007-04-08 16:39:58 +0200222/*
223 * epoll() poller
224 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200225REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200226{
227 int status;
228 int fd;
Willy Tarreau58094f22007-04-10 01:33:20 +0200229 int count;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200230 int wait_time;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200231
Willy Tarreau58094f22007-04-10 01:33:20 +0200232 if (likely(nbchanges))
233 fd_flush_changes();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200234
Willy Tarreau4f60f162007-04-08 16:39:58 +0200235 /* now let's wait for events */
Willy Tarreau332740d2009-05-10 09:57:21 +0200236 if (run_queue || signal_queue_len)
Willy Tarreau3a628112008-06-13 21:06:56 +0200237 wait_time = 0;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200238 else if (!exp)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200239 wait_time = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200240 else if (tick_is_expired(exp, now_ms))
Willy Tarreaubdefc512007-05-14 02:02:04 +0200241 wait_time = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200242 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200243 wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200244 if (wait_time > MAX_DELAY_MS)
245 wait_time = MAX_DELAY_MS;
246 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200247
Willy Tarreau1db37712007-06-03 17:16:49 +0200248 fd = MIN(maxfd, global.tune.maxpollevents);
249 status = epoll_wait(epoll_fd, epoll_events, fd, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200250 tv_update_date(wait_time, status);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200251
252 for (count = 0; count < status; count++) {
253 fd = epoll_events[count].data.fd;
254
Willy Tarreau58094f22007-04-10 01:33:20 +0200255 if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_RD)) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200256 if (fdtab[fd].state == FD_STCLOSE)
257 continue;
258 if (epoll_events[count].events & ( EPOLLIN | EPOLLERR | EPOLLHUP ))
259 fdtab[fd].cb[DIR_RD].f(fd);
260 }
261
Willy Tarreau58094f22007-04-10 01:33:20 +0200262 if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_WR)) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200263 if (fdtab[fd].state == FD_STCLOSE)
264 continue;
265 if (epoll_events[count].events & ( EPOLLOUT | EPOLLERR | EPOLLHUP ))
266 fdtab[fd].cb[DIR_WR].f(fd);
267 }
268 }
269}
270
271/*
Willy Tarreaue54e9172007-04-09 09:23:31 +0200272 * Initialization of the epoll() poller.
273 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
274 * disables the poller by setting its pref to 0.
275 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200276REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200277{
Willy Tarreau58094f22007-04-10 01:33:20 +0200278 __label__ fail_chg_ptr, fail_chg_list, fail_fdevt, fail_ee, fail_fd;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200279 int fd_set_bytes;
280
281 p->private = NULL;
Willy Tarreau58094f22007-04-10 01:33:20 +0200282 fd_set_bytes = 4 * (global.maxsock + 15) / 16;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200283
284 epoll_fd = epoll_create(global.maxsock + 1);
285 if (epoll_fd < 0)
286 goto fail_fd;
287
288 epoll_events = (struct epoll_event*)
Willy Tarreau1db37712007-06-03 17:16:49 +0200289 calloc(1, sizeof(struct epoll_event) * global.tune.maxpollevents);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200290
291 if (epoll_events == NULL)
292 goto fail_ee;
293
Willy Tarreau58094f22007-04-10 01:33:20 +0200294 if ((fd_evts = (uint32_t *)calloc(1, fd_set_bytes)) == NULL)
295 goto fail_fdevt;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200296
Willy Tarreau58094f22007-04-10 01:33:20 +0200297 chg_list = (struct fd_chg *)calloc(1, sizeof(struct fd_chg) * global.maxsock);
298 if (chg_list == NULL)
299 goto fail_chg_list;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200300
Willy Tarreau58094f22007-04-10 01:33:20 +0200301 chg_ptr = (struct fd_chg **)calloc(1, sizeof(struct fd_chg*) * global.maxsock);
302 if (chg_ptr == NULL)
303 goto fail_chg_ptr;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200304
305 return 1;
306
Willy Tarreau58094f22007-04-10 01:33:20 +0200307 fail_chg_ptr:
308 free(chg_list);
309 fail_chg_list:
310 free(fd_evts);
311 fail_fdevt:
Willy Tarreaue54e9172007-04-09 09:23:31 +0200312 free(epoll_events);
313 fail_ee:
314 close(epoll_fd);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200315 epoll_fd = -1;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200316 fail_fd:
317 p->pref = 0;
318 return 0;
319}
320
321/*
322 * Termination of the epoll() poller.
323 * Memory is released and the poller is marked as unselectable.
324 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200325REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200326{
Willy Tarreau58094f22007-04-10 01:33:20 +0200327 fd_flush_changes();
Willy Tarreaue54e9172007-04-09 09:23:31 +0200328
Willy Tarreaua534fea2008-08-03 12:19:50 +0200329 free(chg_ptr);
330 free(chg_list);
331 free(fd_evts);
332 free(epoll_events);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200333
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200334 if (epoll_fd >= 0) {
335 close(epoll_fd);
336 epoll_fd = -1;
337 }
Willy Tarreaue54e9172007-04-09 09:23:31 +0200338
Willy Tarreau58094f22007-04-10 01:33:20 +0200339 chg_ptr = NULL;
340 chg_list = NULL;
341 fd_evts = NULL;
342 epoll_events = NULL;
343
Willy Tarreaue54e9172007-04-09 09:23:31 +0200344 p->private = NULL;
345 p->pref = 0;
346}
347
348/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200349 * Check that the poller works.
350 * Returns 1 if OK, otherwise 0.
351 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200352REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200353{
354 int fd;
355
356 fd = epoll_create(global.maxsock + 1);
357 if (fd < 0)
358 return 0;
359 close(fd);
360 return 1;
361}
362
363/*
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200364 * Recreate the epoll file descriptor after a fork(). Returns 1 if OK,
365 * otherwise 0. It will ensure that all processes will not share their
366 * epoll_fd. Some side effects were encountered because of this, such
367 * as epoll_wait() returning an FD which was previously deleted.
368 */
369REGPRM1 static int _do_fork(struct poller *p)
370{
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200371 if (epoll_fd >= 0)
372 close(epoll_fd);
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200373 epoll_fd = epoll_create(global.maxsock + 1);
374 if (epoll_fd < 0)
375 return 0;
376 return 1;
377}
378
379/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200380 * It is a constructor, which means that it will automatically be called before
381 * main(). This is GCC-specific but it works at least since 2.95.
382 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200383 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200384__attribute__((constructor))
385static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200386{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200387 struct poller *p;
388
389 if (nbpollers >= MAX_POLLERS)
390 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200391
392 epoll_fd = -1;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200393 p = &pollers[nbpollers++];
394
Willy Tarreau4f60f162007-04-08 16:39:58 +0200395 p->name = "epoll";
396 p->pref = 300;
397 p->private = NULL;
398
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200399 p->test = _do_test;
400 p->init = _do_init;
401 p->term = _do_term;
402 p->poll = _do_poll;
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200403 p->fork = _do_fork;
Willy Tarreau58094f22007-04-10 01:33:20 +0200404
405 p->is_set = __fd_is_set;
406 p->cond_s = p->set = __fd_set;
407 p->cond_c = p->clr = __fd_clr;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200408 p->rem = __fd_rem;
409 p->clo = __fd_clo;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200410}
411
412
413/*
414 * Local variables:
415 * c-indent-level: 8
416 * c-basic-offset: 8
417 * End:
418 */