Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * FD polling functions for generic poll() |
| 3 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 4 | * Copyright 2000-2014 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 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> |
Willy Tarreau | 69801b8 | 2007-04-09 15:28:51 +0200 | [diff] [blame] | 14 | #include <sys/poll.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 15 | #include <sys/time.h> |
| 16 | #include <sys/types.h> |
| 17 | |
| 18 | #include <common/compat.h> |
| 19 | #include <common/config.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 20 | #include <common/ticks.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 21 | #include <common/time.h> |
| 22 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 23 | #include <types/global.h> |
| 24 | |
| 25 | #include <proto/fd.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 26 | |
| 27 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 28 | static unsigned int *fd_evts[2]; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 29 | |
| 30 | /* private data */ |
| 31 | static struct pollfd *poll_events = NULL; |
| 32 | |
| 33 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 34 | static inline unsigned int hap_fd_isset(int fd, unsigned int *evts) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 35 | { |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 36 | return evts[fd / (8*sizeof(*evts))] & (1U << (fd & (8*sizeof(*evts) - 1))); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 39 | static 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 | |
| 44 | static 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 | |
| 49 | REGPRM1 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 Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 55 | /* |
| 56 | * Poll() poller |
| 57 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 58 | REGPRM2 static void _do_poll(struct poller *p, int exp) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 59 | { |
| 60 | int status; |
| 61 | int fd, nbfd; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 62 | int wait_time; |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 63 | int updt_idx, en, eo; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 64 | int fds, count; |
| 65 | int sr, sw; |
| 66 | unsigned rn, wn; /* read new, write new */ |
| 67 | |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 68 | /* 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 Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 71 | fdtab[fd].updated = 0; |
| 72 | fdtab[fd].new = 0; |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 73 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 74 | if (!fdtab[fd].owner) |
| 75 | continue; |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 76 | |
Willy Tarreau | 25002d2 | 2014-01-25 10:32:56 +0100 | [diff] [blame] | 77 | eo = fdtab[fd].state; |
| 78 | en = fd_compute_new_polled_status(eo); |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 79 | |
| 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 Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 94 | } |
| 95 | fd_nbupdt = 0; |
| 96 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 97 | nbfd = 0; |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 98 | 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 Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 101 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 102 | if (!(rn|wn)) |
| 103 | continue; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 104 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 105 | 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 Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /* now let's wait for events */ |
Willy Tarreau | 10146c9 | 2015-04-13 20:44:19 +0200 | [diff] [blame] | 117 | if (!exp) |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 118 | wait_time = MAX_DELAY_MS; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 119 | else if (tick_is_expired(exp, now_ms)) |
Willy Tarreau | bdefc51 | 2007-05-14 02:02:04 +0200 | [diff] [blame] | 120 | wait_time = 0; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 121 | else { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 122 | wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 123 | if (wait_time > MAX_DELAY_MS) |
| 124 | wait_time = MAX_DELAY_MS; |
| 125 | } |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 126 | |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 127 | gettimeofday(&before_poll, NULL); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 128 | status = poll(poll_events, nbfd, wait_time); |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 129 | tv_update_date(wait_time, status); |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 130 | measure_idle(); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 131 | |
| 132 | for (count = 0; status > 0 && count < nbfd; count++) { |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 133 | int e = poll_events[count].revents; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 134 | fd = poll_events[count].fd; |
| 135 | |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 136 | if (!(e & ( POLLOUT | POLLIN | POLLERR | POLLHUP ))) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 137 | continue; |
| 138 | |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 139 | /* ok, we found one active fd */ |
| 140 | status--; |
| 141 | |
| 142 | if (!fdtab[fd].owner) |
| 143 | continue; |
| 144 | |
Willy Tarreau | 462c720 | 2012-12-13 22:26:37 +0100 | [diff] [blame] | 145 | /* it looks complicated but gcc can optimize it away when constants |
| 146 | * have same values... In fact it depends on gcc :-( |
| 147 | */ |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 148 | fdtab[fd].ev &= FD_POLL_STICKY; |
Willy Tarreau | 462c720 | 2012-12-13 22:26:37 +0100 | [diff] [blame] | 149 | 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 Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 160 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 161 | 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 Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 168 | } |
| 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 Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 175 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 176 | { |
| 177 | __label__ fail_swevt, fail_srevt, fail_pe; |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 178 | int fd_evts_bytes; |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 179 | |
| 180 | p->private = NULL; |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 181 | fd_evts_bytes = (global.maxsock + sizeof(**fd_evts) - 1) / sizeof(**fd_evts) * sizeof(**fd_evts); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 182 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 183 | poll_events = calloc(1, sizeof(struct pollfd) * global.maxsock); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 184 | |
| 185 | if (poll_events == NULL) |
| 186 | goto fail_pe; |
| 187 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 188 | if ((fd_evts[DIR_RD] = calloc(1, fd_evts_bytes)) == NULL) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 189 | goto fail_srevt; |
| 190 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 191 | if ((fd_evts[DIR_WR] = calloc(1, fd_evts_bytes)) == NULL) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 192 | 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 Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 209 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 210 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 211 | free(fd_evts[DIR_WR]); |
| 212 | free(fd_evts[DIR_RD]); |
| 213 | free(poll_events); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 214 | p->private = NULL; |
| 215 | p->pref = 0; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 219 | * Check that the poller works. |
| 220 | * Returns 1 if OK, otherwise 0. |
| 221 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 222 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 223 | { |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 228 | * 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 Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 231 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 232 | __attribute__((constructor)) |
| 233 | static void _do_register(void) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 234 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 235 | struct poller *p; |
| 236 | |
| 237 | if (nbpollers >= MAX_POLLERS) |
| 238 | return; |
| 239 | p = &pollers[nbpollers++]; |
| 240 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 241 | p->name = "poll"; |
| 242 | p->pref = 200; |
| 243 | p->private = NULL; |
| 244 | |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 245 | p->clo = __fd_clo; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 246 | p->test = _do_test; |
| 247 | p->init = _do_init; |
| 248 | p->term = _do_term; |
| 249 | p->poll = _do_poll; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
| 253 | /* |
| 254 | * Local variables: |
| 255 | * c-indent-level: 8 |
| 256 | * c-basic-offset: 8 |
| 257 | * End: |
| 258 | */ |