blob: 44a2b9b64146f7244968e0fff2cd23ef7b4acc57 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for generic poll()
3 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +01004 * Copyright 2000-2014 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>
Willy Tarreau69801b82007-04-09 15:28:51 +020014#include <sys/poll.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020015#include <sys/time.h>
16#include <sys/types.h>
17
18#include <common/compat.h>
19#include <common/config.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>
22
Willy Tarreau4f60f162007-04-08 16:39:58 +020023#include <types/global.h>
24
25#include <proto/fd.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020026
27
Willy Tarreau80da05a2013-03-31 14:06:57 +020028static unsigned int *fd_evts[2];
Willy Tarreau4f60f162007-04-08 16:39:58 +020029
30/* private data */
31static struct pollfd *poll_events = NULL;
32
33
Willy Tarreau80da05a2013-03-31 14:06:57 +020034static inline unsigned int hap_fd_isset(int fd, unsigned int *evts)
Willy Tarreau4f60f162007-04-08 16:39:58 +020035{
Willy Tarreau80da05a2013-03-31 14:06:57 +020036 return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1)));
Willy Tarreau4f60f162007-04-08 16:39:58 +020037}
38
Willy Tarreau80da05a2013-03-31 14:06:57 +020039static inline void hap_fd_set(int fd, unsigned int *evts)
40{
41 evts[fd / (8*sizeof(*evts))] |= 1U << (fd & (8*sizeof(*evts) - 1));
42}
43
44static inline void hap_fd_clr(int fd, unsigned int *evts)
45{
46 evts[fd / (8*sizeof(*evts))] &= ~(1U << (fd & (8*sizeof(*evts) - 1)));
47}
48
49REGPRM1 static void __fd_clo(int fd)
50{
51 hap_fd_clr(fd, fd_evts[DIR_RD]);
52 hap_fd_clr(fd, fd_evts[DIR_WR]);
53}
54
Willy Tarreau4f60f162007-04-08 16:39:58 +020055/*
56 * Poll() poller
57 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +020058REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +020059{
60 int status;
61 int fd, nbfd;
Willy Tarreaud825eef2007-05-12 22:35:00 +020062 int wait_time;
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010063 int updt_idx, en, eo;
Willy Tarreau4f60f162007-04-08 16:39:58 +020064 int fds, count;
65 int sr, sw;
66 unsigned rn, wn; /* read new, write new */
67
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010068 /* first, scan the update list to find changes */
69 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
70 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +010071 fdtab[fd].updated = 0;
72 fdtab[fd].new = 0;
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010073
Willy Tarreauf817e9f2014-01-10 16:58:45 +010074 if (!fdtab[fd].owner)
75 continue;
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010076
Willy Tarreau25002d22014-01-25 10:32:56 +010077 eo = fdtab[fd].state;
78 en = fd_compute_new_polled_status(eo);
Willy Tarreauf817e9f2014-01-10 16:58:45 +010079
80 if ((eo ^ en) & FD_EV_POLLED_RW) {
81 /* poll status changed, update the lists */
82 fdtab[fd].state = en;
83
84 if ((eo & ~en) & FD_EV_POLLED_R)
85 hap_fd_clr(fd, fd_evts[DIR_RD]);
86 else if ((en & ~eo) & FD_EV_POLLED_R)
87 hap_fd_set(fd, fd_evts[DIR_RD]);
88
89 if ((eo & ~en) & FD_EV_POLLED_W)
90 hap_fd_clr(fd, fd_evts[DIR_WR]);
91 else if ((en & ~eo) & FD_EV_POLLED_W)
92 hap_fd_set(fd, fd_evts[DIR_WR]);
93 }
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010094 }
95 fd_nbupdt = 0;
96
Willy Tarreau4f60f162007-04-08 16:39:58 +020097 nbfd = 0;
Willy Tarreau80da05a2013-03-31 14:06:57 +020098 for (fds = 0; (fds * 8*sizeof(**fd_evts)) < maxfd; fds++) {
99 rn = fd_evts[DIR_RD][fds];
100 wn = fd_evts[DIR_WR][fds];
Willy Tarreau4f60f162007-04-08 16:39:58 +0200101
Willy Tarreau80da05a2013-03-31 14:06:57 +0200102 if (!(rn|wn))
103 continue;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200104
Willy Tarreau80da05a2013-03-31 14:06:57 +0200105 for (count = 0, fd = fds * 8*sizeof(**fd_evts); count < 8*sizeof(**fd_evts) && fd < maxfd; count++, fd++) {
106 sr = (rn >> count) & 1;
107 sw = (wn >> count) & 1;
108 if ((sr|sw)) {
109 poll_events[nbfd].fd = fd;
110 poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0);
111 nbfd++;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200112 }
113 }
114 }
115
116 /* now let's wait for events */
Willy Tarreau10146c92015-04-13 20:44:19 +0200117 if (!exp)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200118 wait_time = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200119 else if (tick_is_expired(exp, now_ms))
Willy Tarreaubdefc512007-05-14 02:02:04 +0200120 wait_time = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200121 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200122 wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200123 if (wait_time > MAX_DELAY_MS)
124 wait_time = MAX_DELAY_MS;
125 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200126
Willy Tarreau45a12512011-09-10 16:56:42 +0200127 gettimeofday(&before_poll, NULL);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200128 status = poll(poll_events, nbfd, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200129 tv_update_date(wait_time, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200130 measure_idle();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200131
132 for (count = 0; status > 0 && count < nbfd; count++) {
Willy Tarreau491c4982012-07-06 11:16:01 +0200133 int e = poll_events[count].revents;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200134 fd = poll_events[count].fd;
135
Willy Tarreau491c4982012-07-06 11:16:01 +0200136 if (!(e & ( POLLOUT | POLLIN | POLLERR | POLLHUP )))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200137 continue;
138
Willy Tarreau076be252012-07-06 16:02:29 +0200139 /* ok, we found one active fd */
140 status--;
141
142 if (!fdtab[fd].owner)
143 continue;
144
Willy Tarreau462c7202012-12-13 22:26:37 +0100145 /* it looks complicated but gcc can optimize it away when constants
146 * have same values... In fact it depends on gcc :-(
147 */
Willy Tarreau491c4982012-07-06 11:16:01 +0200148 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau462c7202012-12-13 22:26:37 +0100149 if (POLLIN == FD_POLL_IN && POLLOUT == FD_POLL_OUT &&
150 POLLERR == FD_POLL_ERR && POLLHUP == FD_POLL_HUP) {
151 fdtab[fd].ev |= e & (POLLIN|POLLOUT|POLLERR|POLLHUP);
152 }
153 else {
154 fdtab[fd].ev |=
155 ((e & POLLIN ) ? FD_POLL_IN : 0) |
156 ((e & POLLOUT) ? FD_POLL_OUT : 0) |
157 ((e & POLLERR) ? FD_POLL_ERR : 0) |
158 ((e & POLLHUP) ? FD_POLL_HUP : 0);
159 }
Willy Tarreau491c4982012-07-06 11:16:01 +0200160
Willy Tarreau5be2f352014-11-19 19:43:05 +0100161 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
162 fd_may_recv(fd);
163
164 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
165 fd_may_send(fd);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200166 }
167
Willy Tarreaue54e9172007-04-09 09:23:31 +0200168}
169
170/*
171 * Initialization of the poll() poller.
172 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
173 * disables the poller by setting its pref to 0.
174 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200175REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200176{
177 __label__ fail_swevt, fail_srevt, fail_pe;
Willy Tarreau80da05a2013-03-31 14:06:57 +0200178 int fd_evts_bytes;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200179
180 p->private = NULL;
Willy Tarreau80da05a2013-03-31 14:06:57 +0200181 fd_evts_bytes = (global.maxsock + sizeof(**fd_evts) - 1) / sizeof(**fd_evts) * sizeof(**fd_evts);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200182
Willy Tarreau80da05a2013-03-31 14:06:57 +0200183 poll_events = calloc(1, sizeof(struct pollfd) * global.maxsock);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200184
185 if (poll_events == NULL)
186 goto fail_pe;
187
Willy Tarreau80da05a2013-03-31 14:06:57 +0200188 if ((fd_evts[DIR_RD] = calloc(1, fd_evts_bytes)) == NULL)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200189 goto fail_srevt;
190
Willy Tarreau80da05a2013-03-31 14:06:57 +0200191 if ((fd_evts[DIR_WR] = calloc(1, fd_evts_bytes)) == NULL)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200192 goto fail_swevt;
193
194 return 1;
195
196 fail_swevt:
197 free(fd_evts[DIR_RD]);
198 fail_srevt:
199 free(poll_events);
200 fail_pe:
201 p->pref = 0;
202 return 0;
203}
204
205/*
206 * Termination of the poll() poller.
207 * Memory is released and the poller is marked as unselectable.
208 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200209REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200210{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200211 free(fd_evts[DIR_WR]);
212 free(fd_evts[DIR_RD]);
213 free(poll_events);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200214 p->private = NULL;
215 p->pref = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200216}
217
218/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200219 * Check that the poller works.
220 * Returns 1 if OK, otherwise 0.
221 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200222REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200223{
224 return 1;
225}
226
227/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200228 * It is a constructor, which means that it will automatically be called before
229 * main(). This is GCC-specific but it works at least since 2.95.
230 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200231 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200232__attribute__((constructor))
233static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200234{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200235 struct poller *p;
236
237 if (nbpollers >= MAX_POLLERS)
238 return;
239 p = &pollers[nbpollers++];
240
Willy Tarreau4f60f162007-04-08 16:39:58 +0200241 p->name = "poll";
242 p->pref = 200;
243 p->private = NULL;
244
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100245 p->clo = __fd_clo;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200246 p->test = _do_test;
247 p->init = _do_init;
248 p->term = _do_term;
249 p->poll = _do_poll;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200250}
251
252
253/*
254 * Local variables:
255 * c-indent-level: 8
256 * c-basic-offset: 8
257 * End:
258 */