blob: b4844998ad495bd549821b631bab1cbb0db091e8 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for linux epoll()
3 *
4 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
5 *
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 Tarreau4f60f162007-04-08 16:39:58 +020020#include <common/time.h>
21
22#include <types/fd.h>
23#include <types/global.h>
24
25#include <proto/fd.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020026#include <proto/task.h>
27
28#if defined(USE_MY_EPOLL)
Willy Tarreau69801b82007-04-09 15:28:51 +020029#include <common/epoll.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020030#include <errno.h>
31#include <sys/syscall.h>
Willy Tarreaub40d4202007-04-15 23:57:41 +020032static _syscall1 (int, epoll_create, int, size);
33static _syscall4 (int, epoll_ctl, int, epfd, int, op, int, fd, struct epoll_event *, event);
34static _syscall4 (int, epoll_wait, int, epfd, struct epoll_event *, events, int, maxevents, int, timeout);
Willy Tarreau69801b82007-04-09 15:28:51 +020035#else
36#include <sys/epoll.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020037#endif
38
Willy Tarreau58094f22007-04-10 01:33:20 +020039/* This is what we store in a list. It consists in old values and fds to detect changes. */
40struct fd_chg {
41 unsigned int prev:2; // previous state mask. New one is in fd_evts.
42 unsigned int fd:30; // file descriptor
43};
Willy Tarreau4f60f162007-04-08 16:39:58 +020044
Willy Tarreau58094f22007-04-10 01:33:20 +020045static int nbchanges = 0; // number of changes pending
46static struct fd_chg *chg_list = NULL; // list of changes
47static struct fd_chg **chg_ptr = NULL; // per-fd changes
48
49/* Each 32-bit word contains 2-bit descriptors of the latest state for 16 FDs :
50 * desc = (u32 >> (2*fd)) & 3
51 * desc = 0 : FD not set
52 * 1 : WRITE not set, READ set
53 * 2 : WRITE set, READ not set
54 * 3 : WRITE set, READ set
55 */
56
57static uint32_t *fd_evts;
Willy Tarreau4f60f162007-04-08 16:39:58 +020058
59/* private data */
60static struct epoll_event *epoll_events;
61static int epoll_fd;
62
Willy Tarreau58094f22007-04-10 01:33:20 +020063/* This structure may be used for any purpose. Warning! do not use it in
64 * recursive functions !
65 */
66static struct epoll_event ev;
67
68/* converts a direction to a single bitmask.
69 * 0 => 1
70 * 1 => 2
71 */
72#define DIR2MSK(dir) ((dir) + 1)
73
74/* converts an FD to an fd_evts offset and to a bit shift */
75#define FD2OFS(fd) ((uint32_t)(fd) >> 4)
76#define FD2BIT(fd) (((uint32_t)(fd) & 0xF) << 1)
77#define FD2MSK(fd) (3 << FD2BIT(fd))
Willy Tarreau4f60f162007-04-08 16:39:58 +020078
79/*
Willy Tarreau58094f22007-04-10 01:33:20 +020080 * Returns non-zero if direction <dir> is already set for <fd>.
Willy Tarreau4f60f162007-04-08 16:39:58 +020081 */
Willy Tarreau63455a92007-04-09 15:34:49 +020082REGPRM2 static int __fd_is_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020083{
Willy Tarreau58094f22007-04-10 01:33:20 +020084 return (fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(dir);
Willy Tarreau4f60f162007-04-08 16:39:58 +020085}
86
Willy Tarreau58094f22007-04-10 01:33:20 +020087/*
88 * Adds, mods or deletes <fd> according to current status and to new desired
89 * mask <dmask> :
90 *
91 * 0 = nothing
92 * 1 = EPOLLIN
93 * 2 = EPOLLOUT
94 * 3 = EPOLLIN | EPOLLOUT
95 *
96 */
97static int dmsk2event[4] = { 0, EPOLLIN, EPOLLOUT, EPOLLIN | EPOLLOUT };
98
99
100REGPRM2 static void fd_flush_changes()
Willy Tarreau4f60f162007-04-08 16:39:58 +0200101{
Willy Tarreau58094f22007-04-10 01:33:20 +0200102 uint32_t ofs;
103 int opcode;
104 int prev, next;
105 int chg, fd;
106
107 for (chg = 0; chg < nbchanges; chg++) {
108 prev = chg_list[chg].prev;
109 fd = chg_list[chg].fd;
110 chg_ptr[fd] = NULL;
111
112 ofs = FD2OFS(fd);
113 next = (fd_evts[ofs] >> FD2BIT(fd)) & 3;
114
115 if (prev == next)
116 /* if the value was unchanged, do nothing */
117 continue;
118
119 ev.events = dmsk2event[next];
120 ev.data.fd = fd;
121
122 if (prev) {
123 if (!next) {
124 /* we want to delete it now */
125 opcode = EPOLL_CTL_DEL;
126 } else {
127 /* we want to switch it */
128 opcode = EPOLL_CTL_MOD;
129 }
130 } else {
131 /* the FD did not exist, let's add it */
132 opcode = EPOLL_CTL_ADD;
133 }
134
135 epoll_ctl(epoll_fd, opcode, fd, &ev);
136 }
137 nbchanges = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200138}
139
Willy Tarreau58094f22007-04-10 01:33:20 +0200140REGPRM2 static void alloc_chg_list(const int fd, int old_evt)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200141{
Willy Tarreau58094f22007-04-10 01:33:20 +0200142 struct fd_chg *ptr;
143
144 if (unlikely(chg_ptr[fd]))
145 return;
146
147#if LIMIT_NUMBER_OF_CHANGES
148 if (nbchanges > 2)
149 fd_flush_changes();
150#endif
151
152 ptr = &chg_list[nbchanges++];
153 chg_ptr[fd] = ptr;
154 ptr->fd = fd;
155 ptr->prev = old_evt;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200156}
157
Willy Tarreau58094f22007-04-10 01:33:20 +0200158REGPRM2 static int __fd_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200159{
Willy Tarreau58094f22007-04-10 01:33:20 +0200160 uint32_t ofs = FD2OFS(fd);
161 uint32_t dmsk = DIR2MSK(dir);
162 uint32_t old_evt;
163
164 old_evt = fd_evts[ofs] >> FD2BIT(fd);
165 old_evt &= 3;
166 if (unlikely(old_evt & dmsk))
167 return 0;
168
169 alloc_chg_list(fd, old_evt);
170 dmsk <<= FD2BIT(fd);
171 fd_evts[ofs] |= dmsk;
172 return 1;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200173}
174
Willy Tarreau58094f22007-04-10 01:33:20 +0200175REGPRM2 static int __fd_clr(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200176{
Willy Tarreau58094f22007-04-10 01:33:20 +0200177 uint32_t ofs = FD2OFS(fd);
178 uint32_t dmsk = DIR2MSK(dir);
179 uint32_t old_evt;
180
181 old_evt = fd_evts[ofs] >> FD2BIT(fd);
182 old_evt &= 3;
183 if (unlikely(!(old_evt & dmsk)))
184 return 0;
185
186 alloc_chg_list(fd, old_evt);
187 dmsk <<= FD2BIT(fd);
188 fd_evts[ofs] &= ~dmsk;
189 return 1;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200190}
191
Willy Tarreau58094f22007-04-10 01:33:20 +0200192REGPRM1 static void __fd_rem(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200193{
Willy Tarreau58094f22007-04-10 01:33:20 +0200194 uint32_t ofs = FD2OFS(fd);
195
196 if (unlikely(!((fd_evts[ofs] >> FD2BIT(fd)) & 3)))
197 return;
198
199 alloc_chg_list(fd, 0);
200 fd_evts[ofs] &= ~FD2MSK(fd);
201 return;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200202}
203
Willy Tarreau58094f22007-04-10 01:33:20 +0200204/*
205 * On valid epoll() implementations, a call to close() automatically removes
206 * the fds. This means that the FD will appear as previously unset.
207 */
208REGPRM1 static void __fd_clo(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200209{
Willy Tarreau58094f22007-04-10 01:33:20 +0200210 struct fd_chg *ptr;
211 fd_evts[FD2OFS(fd)] &= ~FD2MSK(fd);
212 ptr = chg_ptr[fd];
213 if (ptr) {
214 ptr->prev = 0;
215 chg_ptr[fd] = NULL;
216 }
217 return;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200218}
219
Willy Tarreau4f60f162007-04-08 16:39:58 +0200220/*
221 * epoll() poller
222 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200223REGPRM2 static void _do_poll(struct poller *p, int wait_time)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200224{
225 int status;
226 int fd;
Willy Tarreau58094f22007-04-10 01:33:20 +0200227 int count;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200228
Willy Tarreau58094f22007-04-10 01:33:20 +0200229 if (likely(nbchanges))
230 fd_flush_changes();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200231
Willy Tarreau4f60f162007-04-08 16:39:58 +0200232 /* now let's wait for events */
233 status = epoll_wait(epoll_fd, epoll_events, maxfd, wait_time);
234 tv_now(&now);
235
236 for (count = 0; count < status; count++) {
237 fd = epoll_events[count].data.fd;
238
Willy Tarreau58094f22007-04-10 01:33:20 +0200239 if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_RD)) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200240 if (fdtab[fd].state == FD_STCLOSE)
241 continue;
242 if (epoll_events[count].events & ( EPOLLIN | EPOLLERR | EPOLLHUP ))
243 fdtab[fd].cb[DIR_RD].f(fd);
244 }
245
Willy Tarreau58094f22007-04-10 01:33:20 +0200246 if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_WR)) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200247 if (fdtab[fd].state == FD_STCLOSE)
248 continue;
249 if (epoll_events[count].events & ( EPOLLOUT | EPOLLERR | EPOLLHUP ))
250 fdtab[fd].cb[DIR_WR].f(fd);
251 }
252 }
253}
254
255/*
Willy Tarreaue54e9172007-04-09 09:23:31 +0200256 * Initialization of the epoll() poller.
257 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
258 * disables the poller by setting its pref to 0.
259 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200260REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200261{
Willy Tarreau58094f22007-04-10 01:33:20 +0200262 __label__ fail_chg_ptr, fail_chg_list, fail_fdevt, fail_ee, fail_fd;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200263 int fd_set_bytes;
264
265 p->private = NULL;
Willy Tarreau58094f22007-04-10 01:33:20 +0200266 fd_set_bytes = 4 * (global.maxsock + 15) / 16;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200267
268 epoll_fd = epoll_create(global.maxsock + 1);
269 if (epoll_fd < 0)
270 goto fail_fd;
271
272 epoll_events = (struct epoll_event*)
273 calloc(1, sizeof(struct epoll_event) * global.maxsock);
274
275 if (epoll_events == NULL)
276 goto fail_ee;
277
Willy Tarreau58094f22007-04-10 01:33:20 +0200278 if ((fd_evts = (uint32_t *)calloc(1, fd_set_bytes)) == NULL)
279 goto fail_fdevt;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200280
Willy Tarreau58094f22007-04-10 01:33:20 +0200281 chg_list = (struct fd_chg *)calloc(1, sizeof(struct fd_chg) * global.maxsock);
282 if (chg_list == NULL)
283 goto fail_chg_list;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200284
Willy Tarreau58094f22007-04-10 01:33:20 +0200285 chg_ptr = (struct fd_chg **)calloc(1, sizeof(struct fd_chg*) * global.maxsock);
286 if (chg_ptr == NULL)
287 goto fail_chg_ptr;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200288
289 return 1;
290
Willy Tarreau58094f22007-04-10 01:33:20 +0200291 fail_chg_ptr:
292 free(chg_list);
293 fail_chg_list:
294 free(fd_evts);
295 fail_fdevt:
Willy Tarreaue54e9172007-04-09 09:23:31 +0200296 free(epoll_events);
297 fail_ee:
298 close(epoll_fd);
299 epoll_fd = 0;
300 fail_fd:
301 p->pref = 0;
302 return 0;
303}
304
305/*
306 * Termination of the epoll() poller.
307 * Memory is released and the poller is marked as unselectable.
308 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200309REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200310{
Willy Tarreau58094f22007-04-10 01:33:20 +0200311 fd_flush_changes();
Willy Tarreaue54e9172007-04-09 09:23:31 +0200312
Willy Tarreau58094f22007-04-10 01:33:20 +0200313 if (chg_ptr)
314 free(chg_ptr);
315 if (chg_list)
316 free(chg_list);
317 if (fd_evts)
318 free(fd_evts);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200319 if (epoll_events)
320 free(epoll_events);
321
322 close(epoll_fd);
323 epoll_fd = 0;
324
Willy Tarreau58094f22007-04-10 01:33:20 +0200325 chg_ptr = NULL;
326 chg_list = NULL;
327 fd_evts = NULL;
328 epoll_events = NULL;
329
Willy Tarreaue54e9172007-04-09 09:23:31 +0200330 p->private = NULL;
331 p->pref = 0;
332}
333
334/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200335 * Check that the poller works.
336 * Returns 1 if OK, otherwise 0.
337 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200338REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200339{
340 int fd;
341
342 fd = epoll_create(global.maxsock + 1);
343 if (fd < 0)
344 return 0;
345 close(fd);
346 return 1;
347}
348
349/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200350 * It is a constructor, which means that it will automatically be called before
351 * main(). This is GCC-specific but it works at least since 2.95.
352 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200353 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200354__attribute__((constructor))
355static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200356{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200357 struct poller *p;
358
359 if (nbpollers >= MAX_POLLERS)
360 return;
361 p = &pollers[nbpollers++];
362
Willy Tarreau4f60f162007-04-08 16:39:58 +0200363 p->name = "epoll";
364 p->pref = 300;
365 p->private = NULL;
366
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200367 p->test = _do_test;
368 p->init = _do_init;
369 p->term = _do_term;
370 p->poll = _do_poll;
Willy Tarreau58094f22007-04-10 01:33:20 +0200371
372 p->is_set = __fd_is_set;
373 p->cond_s = p->set = __fd_set;
374 p->cond_c = p->clr = __fd_clr;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200375 p->rem = __fd_rem;
376 p->clo = __fd_clo;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200377}
378
379
380/*
381 * Local variables:
382 * c-indent-level: 8
383 * c-basic-offset: 8
384 * End:
385 */