blob: 80d88eb91e488baec75ae501303c404020c80a7b [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 void hap_fd_set(int fd, unsigned int *evts)
35{
36 evts[fd / (8*sizeof(*evts))] |= 1U << (fd & (8*sizeof(*evts) - 1));
37}
38
39static inline void hap_fd_clr(int fd, unsigned int *evts)
40{
41 evts[fd / (8*sizeof(*evts))] &= ~(1U << (fd & (8*sizeof(*evts) - 1)));
42}
43
44REGPRM1 static void __fd_clo(int fd)
45{
46 hap_fd_clr(fd, fd_evts[DIR_RD]);
47 hap_fd_clr(fd, fd_evts[DIR_WR]);
48}
49
Willy Tarreau4f60f162007-04-08 16:39:58 +020050/*
51 * Poll() poller
52 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +020053REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +020054{
55 int status;
56 int fd, nbfd;
Willy Tarreaud825eef2007-05-12 22:35:00 +020057 int wait_time;
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010058 int updt_idx, en, eo;
Willy Tarreau4f60f162007-04-08 16:39:58 +020059 int fds, count;
60 int sr, sw;
61 unsigned rn, wn; /* read new, write new */
62
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010063 /* first, scan the update list to find changes */
64 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
65 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +010066 fdtab[fd].updated = 0;
67 fdtab[fd].new = 0;
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010068
Willy Tarreauf817e9f2014-01-10 16:58:45 +010069 if (!fdtab[fd].owner)
70 continue;
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010071
Willy Tarreau25002d22014-01-25 10:32:56 +010072 eo = fdtab[fd].state;
73 en = fd_compute_new_polled_status(eo);
Willy Tarreauf817e9f2014-01-10 16:58:45 +010074
75 if ((eo ^ en) & FD_EV_POLLED_RW) {
76 /* poll status changed, update the lists */
77 fdtab[fd].state = en;
78
79 if ((eo & ~en) & FD_EV_POLLED_R)
80 hap_fd_clr(fd, fd_evts[DIR_RD]);
81 else if ((en & ~eo) & FD_EV_POLLED_R)
82 hap_fd_set(fd, fd_evts[DIR_RD]);
83
84 if ((eo & ~en) & FD_EV_POLLED_W)
85 hap_fd_clr(fd, fd_evts[DIR_WR]);
86 else if ((en & ~eo) & FD_EV_POLLED_W)
87 hap_fd_set(fd, fd_evts[DIR_WR]);
88 }
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010089 }
90 fd_nbupdt = 0;
91
Willy Tarreau4f60f162007-04-08 16:39:58 +020092 nbfd = 0;
Willy Tarreau80da05a2013-03-31 14:06:57 +020093 for (fds = 0; (fds * 8*sizeof(**fd_evts)) < maxfd; fds++) {
94 rn = fd_evts[DIR_RD][fds];
95 wn = fd_evts[DIR_WR][fds];
Willy Tarreau4f60f162007-04-08 16:39:58 +020096
Willy Tarreau80da05a2013-03-31 14:06:57 +020097 if (!(rn|wn))
98 continue;
Willy Tarreau4f60f162007-04-08 16:39:58 +020099
Willy Tarreau80da05a2013-03-31 14:06:57 +0200100 for (count = 0, fd = fds * 8*sizeof(**fd_evts); count < 8*sizeof(**fd_evts) && fd < maxfd; count++, fd++) {
101 sr = (rn >> count) & 1;
102 sw = (wn >> count) & 1;
103 if ((sr|sw)) {
104 poll_events[nbfd].fd = fd;
105 poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0);
106 nbfd++;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200107 }
108 }
109 }
110
111 /* now let's wait for events */
Willy Tarreau10146c92015-04-13 20:44:19 +0200112 if (!exp)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200113 wait_time = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200114 else if (tick_is_expired(exp, now_ms))
Willy Tarreaubdefc512007-05-14 02:02:04 +0200115 wait_time = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200116 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200117 wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200118 if (wait_time > MAX_DELAY_MS)
119 wait_time = MAX_DELAY_MS;
120 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200121
Willy Tarreau45a12512011-09-10 16:56:42 +0200122 gettimeofday(&before_poll, NULL);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200123 status = poll(poll_events, nbfd, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200124 tv_update_date(wait_time, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200125 measure_idle();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200126
127 for (count = 0; status > 0 && count < nbfd; count++) {
Willy Tarreau491c4982012-07-06 11:16:01 +0200128 int e = poll_events[count].revents;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200129 fd = poll_events[count].fd;
130
Willy Tarreau491c4982012-07-06 11:16:01 +0200131 if (!(e & ( POLLOUT | POLLIN | POLLERR | POLLHUP )))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200132 continue;
133
Willy Tarreau076be252012-07-06 16:02:29 +0200134 /* ok, we found one active fd */
135 status--;
136
137 if (!fdtab[fd].owner)
138 continue;
139
Willy Tarreau462c7202012-12-13 22:26:37 +0100140 /* it looks complicated but gcc can optimize it away when constants
141 * have same values... In fact it depends on gcc :-(
142 */
Willy Tarreau491c4982012-07-06 11:16:01 +0200143 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau462c7202012-12-13 22:26:37 +0100144 if (POLLIN == FD_POLL_IN && POLLOUT == FD_POLL_OUT &&
145 POLLERR == FD_POLL_ERR && POLLHUP == FD_POLL_HUP) {
146 fdtab[fd].ev |= e & (POLLIN|POLLOUT|POLLERR|POLLHUP);
147 }
148 else {
149 fdtab[fd].ev |=
150 ((e & POLLIN ) ? FD_POLL_IN : 0) |
151 ((e & POLLOUT) ? FD_POLL_OUT : 0) |
152 ((e & POLLERR) ? FD_POLL_ERR : 0) |
153 ((e & POLLHUP) ? FD_POLL_HUP : 0);
154 }
Willy Tarreau491c4982012-07-06 11:16:01 +0200155
Willy Tarreau5be2f352014-11-19 19:43:05 +0100156 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
157 fd_may_recv(fd);
158
159 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
160 fd_may_send(fd);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200161 }
162
Willy Tarreaue54e9172007-04-09 09:23:31 +0200163}
164
165/*
166 * Initialization of the poll() poller.
167 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
168 * disables the poller by setting its pref to 0.
169 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200170REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200171{
172 __label__ fail_swevt, fail_srevt, fail_pe;
Willy Tarreau80da05a2013-03-31 14:06:57 +0200173 int fd_evts_bytes;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200174
175 p->private = NULL;
Willy Tarreau80da05a2013-03-31 14:06:57 +0200176 fd_evts_bytes = (global.maxsock + sizeof(**fd_evts) - 1) / sizeof(**fd_evts) * sizeof(**fd_evts);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200177
Willy Tarreau80da05a2013-03-31 14:06:57 +0200178 poll_events = calloc(1, sizeof(struct pollfd) * global.maxsock);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200179
180 if (poll_events == NULL)
181 goto fail_pe;
182
Willy Tarreau80da05a2013-03-31 14:06:57 +0200183 if ((fd_evts[DIR_RD] = calloc(1, fd_evts_bytes)) == NULL)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200184 goto fail_srevt;
185
Willy Tarreau80da05a2013-03-31 14:06:57 +0200186 if ((fd_evts[DIR_WR] = calloc(1, fd_evts_bytes)) == NULL)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200187 goto fail_swevt;
188
189 return 1;
190
191 fail_swevt:
192 free(fd_evts[DIR_RD]);
193 fail_srevt:
194 free(poll_events);
195 fail_pe:
196 p->pref = 0;
197 return 0;
198}
199
200/*
201 * Termination of the poll() poller.
202 * Memory is released and the poller is marked as unselectable.
203 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200204REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200205{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200206 free(fd_evts[DIR_WR]);
207 free(fd_evts[DIR_RD]);
208 free(poll_events);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200209 p->private = NULL;
210 p->pref = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200211}
212
213/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200214 * Check that the poller works.
215 * Returns 1 if OK, otherwise 0.
216 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200217REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200218{
219 return 1;
220}
221
222/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200223 * It is a constructor, which means that it will automatically be called before
224 * main(). This is GCC-specific but it works at least since 2.95.
225 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200226 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200227__attribute__((constructor))
228static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200229{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200230 struct poller *p;
231
232 if (nbpollers >= MAX_POLLERS)
233 return;
234 p = &pollers[nbpollers++];
235
Willy Tarreau4f60f162007-04-08 16:39:58 +0200236 p->name = "poll";
237 p->pref = 200;
238 p->private = NULL;
239
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100240 p->clo = __fd_clo;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200241 p->test = _do_test;
242 p->init = _do_init;
243 p->term = _do_term;
244 p->poll = _do_poll;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200245}
246
247
248/*
249 * Local variables:
250 * c-indent-level: 8
251 * c-basic-offset: 8
252 * End:
253 */