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 void hap_fd_set(int fd, unsigned int *evts) |
| 35 | { |
| 36 | evts[fd / (8*sizeof(*evts))] |= 1U << (fd & (8*sizeof(*evts) - 1)); |
| 37 | } |
| 38 | |
| 39 | static 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 | |
| 44 | REGPRM1 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 Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 50 | /* |
| 51 | * Poll() poller |
| 52 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 53 | REGPRM2 static void _do_poll(struct poller *p, int exp) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 54 | { |
| 55 | int status; |
| 56 | int fd, nbfd; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 57 | int wait_time; |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 58 | int updt_idx, en, eo; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 59 | int fds, count; |
| 60 | int sr, sw; |
| 61 | unsigned rn, wn; /* read new, write new */ |
| 62 | |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 63 | /* 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 Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 66 | fdtab[fd].updated = 0; |
| 67 | fdtab[fd].new = 0; |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 68 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 69 | if (!fdtab[fd].owner) |
| 70 | continue; |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 71 | |
Willy Tarreau | 25002d2 | 2014-01-25 10:32:56 +0100 | [diff] [blame] | 72 | eo = fdtab[fd].state; |
| 73 | en = fd_compute_new_polled_status(eo); |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 74 | |
| 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 Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 89 | } |
| 90 | fd_nbupdt = 0; |
| 91 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 92 | nbfd = 0; |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 93 | 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 Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 96 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 97 | if (!(rn|wn)) |
| 98 | continue; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 99 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 100 | 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 Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /* now let's wait for events */ |
Willy Tarreau | 10146c9 | 2015-04-13 20:44:19 +0200 | [diff] [blame] | 112 | if (!exp) |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 113 | wait_time = MAX_DELAY_MS; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 114 | else if (tick_is_expired(exp, now_ms)) |
Willy Tarreau | bdefc51 | 2007-05-14 02:02:04 +0200 | [diff] [blame] | 115 | wait_time = 0; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 116 | else { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 117 | wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 118 | if (wait_time > MAX_DELAY_MS) |
| 119 | wait_time = MAX_DELAY_MS; |
| 120 | } |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 121 | |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 122 | gettimeofday(&before_poll, NULL); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 123 | status = poll(poll_events, nbfd, wait_time); |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 124 | tv_update_date(wait_time, status); |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 125 | measure_idle(); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 126 | |
| 127 | for (count = 0; status > 0 && count < nbfd; count++) { |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 128 | int e = poll_events[count].revents; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 129 | fd = poll_events[count].fd; |
| 130 | |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 131 | if (!(e & ( POLLOUT | POLLIN | POLLERR | POLLHUP ))) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 132 | continue; |
| 133 | |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 134 | /* ok, we found one active fd */ |
| 135 | status--; |
| 136 | |
| 137 | if (!fdtab[fd].owner) |
| 138 | continue; |
| 139 | |
Willy Tarreau | 462c720 | 2012-12-13 22:26:37 +0100 | [diff] [blame] | 140 | /* it looks complicated but gcc can optimize it away when constants |
| 141 | * have same values... In fact it depends on gcc :-( |
| 142 | */ |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 143 | fdtab[fd].ev &= FD_POLL_STICKY; |
Willy Tarreau | 462c720 | 2012-12-13 22:26:37 +0100 | [diff] [blame] | 144 | 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 Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 155 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 156 | 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 Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 163 | } |
| 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 Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 170 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 171 | { |
| 172 | __label__ fail_swevt, fail_srevt, fail_pe; |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 173 | int fd_evts_bytes; |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 174 | |
| 175 | p->private = NULL; |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 176 | 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] | 177 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 178 | poll_events = calloc(1, sizeof(struct pollfd) * global.maxsock); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 179 | |
| 180 | if (poll_events == NULL) |
| 181 | goto fail_pe; |
| 182 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 183 | if ((fd_evts[DIR_RD] = calloc(1, fd_evts_bytes)) == NULL) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 184 | goto fail_srevt; |
| 185 | |
Willy Tarreau | 80da05a | 2013-03-31 14:06:57 +0200 | [diff] [blame] | 186 | if ((fd_evts[DIR_WR] = calloc(1, fd_evts_bytes)) == NULL) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 187 | 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 Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 204 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 205 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 206 | free(fd_evts[DIR_WR]); |
| 207 | free(fd_evts[DIR_RD]); |
| 208 | free(poll_events); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 209 | p->private = NULL; |
| 210 | p->pref = 0; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 214 | * Check that the poller works. |
| 215 | * Returns 1 if OK, otherwise 0. |
| 216 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 217 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 218 | { |
| 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 223 | * 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 Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 226 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 227 | __attribute__((constructor)) |
| 228 | static void _do_register(void) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 229 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 230 | struct poller *p; |
| 231 | |
| 232 | if (nbpollers >= MAX_POLLERS) |
| 233 | return; |
| 234 | p = &pollers[nbpollers++]; |
| 235 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 236 | p->name = "poll"; |
| 237 | p->pref = 200; |
| 238 | p->private = NULL; |
| 239 | |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 240 | p->clo = __fd_clo; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 241 | p->test = _do_test; |
| 242 | p->init = _do_init; |
| 243 | p->term = _do_term; |
| 244 | p->poll = _do_poll; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
| 248 | /* |
| 249 | * Local variables: |
| 250 | * c-indent-level: 8 |
| 251 | * c-basic-offset: 8 |
| 252 | * End: |
| 253 | */ |