2
* FD polling functions for linux epoll()
4
* Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
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.
15
#include <sys/types.h>
17
#include <common/compat.h>
18
#include <common/config.h>
19
#include <common/standard.h>
20
#include <common/time.h>
21
#include <common/tools.h>
24
#include <types/global.h>
27
#include <proto/task.h>
29
#if defined(USE_MY_EPOLL)
30
#include <common/epoll.h>
32
#include <sys/syscall.h>
33
static _syscall1 (int, epoll_create, int, size);
34
static _syscall4 (int, epoll_ctl, int, epfd, int, op, int, fd, struct epoll_event *, event);
35
static _syscall4 (int, epoll_wait, int, epfd, struct epoll_event *, events, int, maxevents, int, timeout);
37
#include <sys/epoll.h>
40
/* This is what we store in a list. It consists in old values and fds to detect changes. */
42
unsigned int prev:2; // previous state mask. New one is in fd_evts.
43
unsigned int fd:30; // file descriptor
46
static int nbchanges = 0; // number of changes pending
47
static struct fd_chg *chg_list = NULL; // list of changes
48
static struct fd_chg **chg_ptr = NULL; // per-fd changes
50
/* Each 32-bit word contains 2-bit descriptors of the latest state for 16 FDs :
51
* desc = (u32 >> (2*fd)) & 3
52
* desc = 0 : FD not set
53
* 1 : WRITE not set, READ set
54
* 2 : WRITE set, READ not set
55
* 3 : WRITE set, READ set
58
static uint32_t *fd_evts;
61
static struct epoll_event *epoll_events;
64
/* This structure may be used for any purpose. Warning! do not use it in
65
* recursive functions !
67
static struct epoll_event ev;
69
/* converts a direction to a single bitmask.
73
#define DIR2MSK(dir) ((dir) + 1)
75
/* converts an FD to an fd_evts offset and to a bit shift */
76
#define FD2OFS(fd) ((uint32_t)(fd) >> 4)
77
#define FD2BIT(fd) (((uint32_t)(fd) & 0xF) << 1)
78
#define FD2MSK(fd) (3 << FD2BIT(fd))
81
* Returns non-zero if direction <dir> is already set for <fd>.
83
REGPRM2 static int __fd_is_set(const int fd, int dir)
85
return (fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(dir);
89
* Adds, mods or deletes <fd> according to current status and to new desired
95
* 3 = EPOLLIN | EPOLLOUT
98
static int dmsk2event[4] = { 0, EPOLLIN, EPOLLOUT, EPOLLIN | EPOLLOUT };
101
REGPRM2 static void fd_flush_changes()
108
for (chg = 0; chg < nbchanges; chg++) {
109
prev = chg_list[chg].prev;
110
fd = chg_list[chg].fd;
114
next = (fd_evts[ofs] >> FD2BIT(fd)) & 3;
117
/* if the value was unchanged, do nothing */
120
ev.events = dmsk2event[next];
125
/* we want to delete it now */
126
opcode = EPOLL_CTL_DEL;
128
/* we want to switch it */
129
opcode = EPOLL_CTL_MOD;
132
/* the FD did not exist, let's add it */
133
opcode = EPOLL_CTL_ADD;
136
epoll_ctl(epoll_fd, opcode, fd, &ev);
141
REGPRM2 static void alloc_chg_list(const int fd, int old_evt)
145
if (unlikely(chg_ptr[fd]))
148
#if LIMIT_NUMBER_OF_CHANGES
153
ptr = &chg_list[nbchanges++];
159
REGPRM2 static int __fd_set(const int fd, int dir)
161
uint32_t ofs = FD2OFS(fd);
162
uint32_t dmsk = DIR2MSK(dir);
165
old_evt = fd_evts[ofs] >> FD2BIT(fd);
167
if (unlikely(old_evt & dmsk))
170
alloc_chg_list(fd, old_evt);
172
fd_evts[ofs] |= dmsk;
176
REGPRM2 static int __fd_clr(const int fd, int dir)
178
uint32_t ofs = FD2OFS(fd);
179
uint32_t dmsk = DIR2MSK(dir);
182
old_evt = fd_evts[ofs] >> FD2BIT(fd);
184
if (unlikely(!(old_evt & dmsk)))
187
alloc_chg_list(fd, old_evt);
189
fd_evts[ofs] &= ~dmsk;
193
REGPRM1 static void __fd_rem(int fd)
195
uint32_t ofs = FD2OFS(fd);
197
if (unlikely(!((fd_evts[ofs] >> FD2BIT(fd)) & 3)))
200
alloc_chg_list(fd, 0);
201
fd_evts[ofs] &= ~FD2MSK(fd);
206
* On valid epoll() implementations, a call to close() automatically removes
207
* the fds. This means that the FD will appear as previously unset.
209
REGPRM1 static void __fd_clo(int fd)
212
fd_evts[FD2OFS(fd)] &= ~FD2MSK(fd);
224
REGPRM2 static void _do_poll(struct poller *p, struct timeval *exp)
231
if (likely(nbchanges))
234
/* now let's wait for events */
235
if (tv_iseternity(exp))
237
else if (tv_isge(&now, exp))
240
wait_time = __tv_ms_elapsed(&now, exp) + 1;
242
fd = MIN(maxfd, global.tune.maxpollevents);
243
status = epoll_wait(epoll_fd, epoll_events, fd, wait_time);
246
for (count = 0; count < status; count++) {
247
fd = epoll_events[count].data.fd;
249
if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_RD)) {
250
if (fdtab[fd].state == FD_STCLOSE)
252
if (epoll_events[count].events & ( EPOLLIN | EPOLLERR | EPOLLHUP ))
253
fdtab[fd].cb[DIR_RD].f(fd);
256
if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_WR)) {
257
if (fdtab[fd].state == FD_STCLOSE)
259
if (epoll_events[count].events & ( EPOLLOUT | EPOLLERR | EPOLLHUP ))
260
fdtab[fd].cb[DIR_WR].f(fd);
266
* Initialization of the epoll() poller.
267
* Returns 0 in case of failure, non-zero in case of success. If it fails, it
268
* disables the poller by setting its pref to 0.
270
REGPRM1 static int _do_init(struct poller *p)
272
__label__ fail_chg_ptr, fail_chg_list, fail_fdevt, fail_ee, fail_fd;
276
fd_set_bytes = 4 * (global.maxsock + 15) / 16;
278
epoll_fd = epoll_create(global.maxsock + 1);
282
epoll_events = (struct epoll_event*)
283
calloc(1, sizeof(struct epoll_event) * global.tune.maxpollevents);
285
if (epoll_events == NULL)
288
if ((fd_evts = (uint32_t *)calloc(1, fd_set_bytes)) == NULL)
291
chg_list = (struct fd_chg *)calloc(1, sizeof(struct fd_chg) * global.maxsock);
292
if (chg_list == NULL)
295
chg_ptr = (struct fd_chg **)calloc(1, sizeof(struct fd_chg*) * global.maxsock);
316
* Termination of the epoll() poller.
317
* Memory is released and the poller is marked as unselectable.
319
REGPRM1 static void _do_term(struct poller *p)
345
* Check that the poller works.
346
* Returns 1 if OK, otherwise 0.
348
REGPRM1 static int _do_test(struct poller *p)
352
fd = epoll_create(global.maxsock + 1);
360
* Recreate the epoll file descriptor after a fork(). Returns 1 if OK,
361
* otherwise 0. It will ensure that all processes will not share their
362
* epoll_fd. Some side effects were encountered because of this, such
363
* as epoll_wait() returning an FD which was previously deleted.
365
REGPRM1 static int _do_fork(struct poller *p)
368
epoll_fd = epoll_create(global.maxsock + 1);
375
* It is a constructor, which means that it will automatically be called before
376
* main(). This is GCC-specific but it works at least since 2.95.
377
* Special care must be taken so that it does not need any uninitialized data.
379
__attribute__((constructor))
380
static void _do_register(void)
384
if (nbpollers >= MAX_POLLERS)
386
p = &pollers[nbpollers++];
398
p->is_set = __fd_is_set;
399
p->cond_s = p->set = __fd_set;
400
p->cond_c = p->clr = __fd_clr;