blob: 3b02bed40d82973b5d8c95dcf44afa721220d8d5 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for generic select()
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>
14#include <sys/time.h>
15#include <sys/types.h>
16
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020018#include <haproxy/thread-t.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020019#include <common/ticks.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020020#include <common/time.h>
21
Willy Tarreau4f60f162007-04-08 16:39:58 +020022#include <types/global.h>
23
Willy Tarreau609aad92018-11-22 08:31:09 +010024#include <proto/activity.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020025#include <proto/fd.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020026
27
Christopher Fauletd4604ad2017-05-29 10:40:41 +020028/* private data */
Willy Tarreau173d9952018-01-26 21:48:23 +010029static int maxfd; /* # of the highest fd + 1 */
Willy Tarreaud51a5072018-01-25 16:48:46 +010030static unsigned int *fd_evts[2];
Christopher Fauletd4604ad2017-05-29 10:40:41 +020031static THREAD_LOCAL fd_set *tmp_evts[2];
Willy Tarreau4f60f162007-04-08 16:39:58 +020032
Willy Tarreau4d31fb22012-11-11 16:53:50 +010033/* Immediately remove the entry upon close() */
Willy Tarreau03e78532020-02-25 07:38:05 +010034static void __fd_clo(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +020035{
Willy Tarreaud51a5072018-01-25 16:48:46 +010036 hap_fd_clr(fd, fd_evts[DIR_RD]);
37 hap_fd_clr(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020038}
39
Olivier Houchard6b96f722018-04-25 16:58:25 +020040static void _update_fd(int fd, int *max_add_fd)
41{
42 int en;
43
44 en = fdtab[fd].state;
45
46 /* we have a single state for all threads, which is why we
47 * don't check the tid_bit. First thread to see the update
48 * takes it for every other one.
49 */
Willy Tarreau5bee3e22019-09-04 09:52:57 +020050 if (!(en & FD_EV_ACTIVE_RW)) {
Olivier Houchard53055052019-07-25 14:00:18 +000051 if (!(polled_mask[fd].poll_recv | polled_mask[fd].poll_send)) {
Olivier Houchard6b96f722018-04-25 16:58:25 +020052 /* fd was not watched, it's still not */
53 return;
54 }
55 /* fd totally removed from poll list */
56 hap_fd_clr(fd, fd_evts[DIR_RD]);
57 hap_fd_clr(fd, fd_evts[DIR_WR]);
Olivier Houchard53055052019-07-25 14:00:18 +000058 _HA_ATOMIC_AND(&polled_mask[fd].poll_recv, 0);
Olivier Houcharda3a8ea22019-08-05 23:54:37 +020059 _HA_ATOMIC_AND(&polled_mask[fd].poll_send, 0);
Olivier Houchard6b96f722018-04-25 16:58:25 +020060 }
61 else {
62 /* OK fd has to be monitored, it was either added or changed */
Willy Tarreau5bee3e22019-09-04 09:52:57 +020063 if (!(en & FD_EV_ACTIVE_R)) {
Olivier Houchard6b96f722018-04-25 16:58:25 +020064 hap_fd_clr(fd, fd_evts[DIR_RD]);
Olivier Houchard53055052019-07-25 14:00:18 +000065 if (polled_mask[fd].poll_recv & tid_bit)
66 _HA_ATOMIC_AND(&polled_mask[fd].poll_recv, ~tid_bit);
67 } else {
Olivier Houchard6b96f722018-04-25 16:58:25 +020068 hap_fd_set(fd, fd_evts[DIR_RD]);
Olivier Houchard53055052019-07-25 14:00:18 +000069 if (!(polled_mask[fd].poll_recv & tid_bit))
70 _HA_ATOMIC_OR(&polled_mask[fd].poll_recv, tid_bit);
71 }
Olivier Houchard6b96f722018-04-25 16:58:25 +020072
Willy Tarreau5bee3e22019-09-04 09:52:57 +020073 if (!(en & FD_EV_ACTIVE_W)) {
Olivier Houchard6b96f722018-04-25 16:58:25 +020074 hap_fd_clr(fd, fd_evts[DIR_WR]);
Olivier Houchard53055052019-07-25 14:00:18 +000075 if (polled_mask[fd].poll_send & tid_bit)
76 _HA_ATOMIC_AND(&polled_mask[fd].poll_send, ~tid_bit);
77 } else {
Olivier Houchard6b96f722018-04-25 16:58:25 +020078 hap_fd_set(fd, fd_evts[DIR_WR]);
Olivier Houchard53055052019-07-25 14:00:18 +000079 if (!(polled_mask[fd].poll_send & tid_bit))
80 _HA_ATOMIC_OR(&polled_mask[fd].poll_send, tid_bit);
81 }
Olivier Houchard6b96f722018-04-25 16:58:25 +020082
Olivier Houchard6b96f722018-04-25 16:58:25 +020083 if (fd > *max_add_fd)
84 *max_add_fd = fd;
85 }
86}
87
Willy Tarreau4f60f162007-04-08 16:39:58 +020088/*
89 * Select() poller
90 */
Willy Tarreau03e78532020-02-25 07:38:05 +010091static void _do_poll(struct poller *p, int exp, int wake)
Willy Tarreau4f60f162007-04-08 16:39:58 +020092{
93 int status;
94 int fd, i;
95 struct timeval delta;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020096 int delta_ms;
Willy Tarreau4f60f162007-04-08 16:39:58 +020097 int fds;
Olivier Houchard6b96f722018-04-25 16:58:25 +020098 int updt_idx;
Willy Tarreau4f60f162007-04-08 16:39:58 +020099 char count;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200100 int readnotnull, writenotnull;
Willy Tarreau173d9952018-01-26 21:48:23 +0100101 int old_maxfd, new_maxfd, max_add_fd;
Olivier Houchard6b96f722018-04-25 16:58:25 +0200102 int old_fd;
Willy Tarreau173d9952018-01-26 21:48:23 +0100103
104 max_add_fd = -1;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200105
Willy Tarreau4d31fb22012-11-11 16:53:50 +0100106 /* first, scan the update list to find changes */
107 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
108 fd = fd_updt[updt_idx];
Willy Tarreau4d31fb22012-11-11 16:53:50 +0100109
Olivier Houchardcb6c9272019-03-08 18:49:54 +0100110 _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100111 if (!fdtab[fd].owner) {
112 activity[tid].poll_drop++;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100113 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100114 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200115 _update_fd(fd, &max_add_fd);
116 }
117 /* Now scan the global update list */
118 for (old_fd = fd = update_list.first; fd != -1; fd = fdtab[fd].update.next) {
119 if (fd == -2) {
120 fd = old_fd;
121 continue;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100122 }
Olivier Houchard6b96f722018-04-25 16:58:25 +0200123 else if (fd <= -3)
124 fd = -fd -4;
125 if (fd == -1)
126 break;
127 if (fdtab[fd].update_mask & tid_bit) {
128 /* Cheat a bit, as the state is global to all pollers
129 * we don't need every thread ot take care of the
130 * update.
131 */
Olivier Houchardcb6c9272019-03-08 18:49:54 +0100132 _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~all_threads_mask);
Olivier Houchard6b96f722018-04-25 16:58:25 +0200133 done_update_polling(fd);
134 } else
135 continue;
136 if (!fdtab[fd].owner)
137 continue;
138 _update_fd(fd, &max_add_fd);
Willy Tarreau4d31fb22012-11-11 16:53:50 +0100139 }
Willy Tarreau173d9952018-01-26 21:48:23 +0100140
Olivier Houchard6b96f722018-04-25 16:58:25 +0200141
Willy Tarreau173d9952018-01-26 21:48:23 +0100142 /* maybe we added at least one fd larger than maxfd */
143 for (old_maxfd = maxfd; old_maxfd <= max_add_fd; ) {
Olivier Houchardcb6c9272019-03-08 18:49:54 +0100144 if (_HA_ATOMIC_CAS(&maxfd, &old_maxfd, max_add_fd + 1))
Willy Tarreau173d9952018-01-26 21:48:23 +0100145 break;
146 }
147
148 /* maxfd doesn't need to be precise but it needs to cover *all* active
149 * FDs. Thus we only shrink it if we have such an opportunity. The algo
150 * is simple : look for the previous used place, try to update maxfd to
151 * point to it, abort if maxfd changed in the mean time.
152 */
153 old_maxfd = maxfd;
154 do {
155 new_maxfd = old_maxfd;
156 while (new_maxfd - 1 >= 0 && !fdtab[new_maxfd - 1].owner)
157 new_maxfd--;
158 if (new_maxfd >= old_maxfd)
159 break;
Olivier Houchardcb6c9272019-03-08 18:49:54 +0100160 } while (!_HA_ATOMIC_CAS(&maxfd, &old_maxfd, new_maxfd));
Willy Tarreau173d9952018-01-26 21:48:23 +0100161
Willy Tarreau60b639c2018-08-02 10:16:17 +0200162 thread_harmless_now();
163
Willy Tarreau4d31fb22012-11-11 16:53:50 +0100164 fd_nbupdt = 0;
165
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200166 /* let's restore fdset state */
167 readnotnull = 0; writenotnull = 0;
168 for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) {
169 readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0;
170 writenotnull |= (*(((int*)tmp_evts[DIR_WR])+i) = *(((int*)fd_evts[DIR_WR])+i)) != 0;
171 }
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200172
Willy Tarreauf37ba942018-10-17 11:25:54 +0200173 /* now let's wait for events */
Willy Tarreau2ae84e42019-05-28 16:44:05 +0200174 delta_ms = wake ? 0 : compute_poll_timeout(exp);
Willy Tarreauf37ba942018-10-17 11:25:54 +0200175 delta.tv_sec = (delta_ms / 1000);
176 delta.tv_usec = (delta_ms % 1000) * 1000;
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200177 tv_entering_poll();
Willy Tarreau609aad92018-11-22 08:31:09 +0100178 activity_count_runtime();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200179 status = select(maxfd,
Willy Tarreau28d86862007-04-08 17:42:27 +0200180 readnotnull ? tmp_evts[DIR_RD] : NULL,
181 writenotnull ? tmp_evts[DIR_WR] : NULL,
Willy Tarreau4f60f162007-04-08 16:39:58 +0200182 NULL,
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200183 &delta);
Willy Tarreau48f8bc12018-11-22 18:57:37 +0100184 tv_update_date(delta_ms, status);
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200185 tv_leaving_poll(delta_ms, status);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200186
Willy Tarreau60b639c2018-08-02 10:16:17 +0200187 thread_harmless_end();
Olivier Houchard305d5ab2019-07-24 18:07:06 +0200188 if (sleeping_thread_mask & tid_bit)
189 _HA_ATOMIC_AND(&sleeping_thread_mask, ~tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200190
Willy Tarreau4f60f162007-04-08 16:39:58 +0200191 if (status <= 0)
192 return;
193
Willy Tarreau177e2b02008-07-15 00:36:31 +0200194 for (fds = 0; (fds * BITS_PER_INT) < maxfd; fds++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200195 if ((((int *)(tmp_evts[DIR_RD]))[fds] | ((int *)(tmp_evts[DIR_WR]))[fds]) == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200196 continue;
197
Willy Tarreau177e2b02008-07-15 00:36:31 +0200198 for (count = BITS_PER_INT, fd = fds * BITS_PER_INT; count && fd < maxfd; count--, fd++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200199 unsigned int n = 0;
200
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100201 if (!fdtab[fd].owner) {
202 activity[tid].poll_dead++;
203 continue;
204 }
205
206 if (!(fdtab[fd].thread_mask & tid_bit)) {
207 activity[tid].poll_skip++;
Willy Tarreau076be252012-07-06 16:02:29 +0200208 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100209 }
Willy Tarreau076be252012-07-06 16:02:29 +0200210
Willy Tarreau076be252012-07-06 16:02:29 +0200211 if (FD_ISSET(fd, tmp_evts[DIR_RD]))
Willy Tarreau6b308982019-09-06 19:05:50 +0200212 n |= FD_EV_READY_R;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200213
Willy Tarreau076be252012-07-06 16:02:29 +0200214 if (FD_ISSET(fd, tmp_evts[DIR_WR]))
Willy Tarreau6b308982019-09-06 19:05:50 +0200215 n |= FD_EV_READY_W;
Willy Tarreau5be2f352014-11-19 19:43:05 +0100216
Christopher Fauletab62f512017-08-30 10:34:36 +0200217 fd_update_events(fd, n);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200218 }
219 }
220}
221
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200222static int init_select_per_thread()
223{
224 int fd_set_bytes;
225
226 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
William Dauchy32fba0a2020-05-11 15:20:05 +0200227 tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes);
228 if (tmp_evts[DIR_RD] == NULL)
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200229 goto fail;
William Dauchy32fba0a2020-05-11 15:20:05 +0200230 tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes);
231 if (tmp_evts[DIR_WR] == NULL)
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200232 goto fail;
233 return 1;
234 fail:
235 free(tmp_evts[DIR_RD]);
236 free(tmp_evts[DIR_WR]);
237 return 0;
238}
239
240static void deinit_select_per_thread()
241{
William Dauchy32fba0a2020-05-11 15:20:05 +0200242 free(tmp_evts[DIR_WR]);
243 tmp_evts[DIR_WR] = NULL;
244 free(tmp_evts[DIR_RD]);
245 tmp_evts[DIR_RD] = NULL;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200246}
247
Willy Tarreau4f60f162007-04-08 16:39:58 +0200248/*
Willy Tarreaue54e9172007-04-09 09:23:31 +0200249 * Initialization of the select() poller.
250 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
251 * disables the poller by setting its pref to 0.
252 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100253static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200254{
Willy Tarreaue54e9172007-04-09 09:23:31 +0200255 int fd_set_bytes;
256
257 p->private = NULL;
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200258
259 if (global.maxsock > FD_SETSIZE)
William Dauchy42a50bd2020-05-11 15:20:03 +0200260 goto fail_srevt;
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200261
Willy Tarreaue54e9172007-04-09 09:23:31 +0200262 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200263
Willy Tarreaud51a5072018-01-25 16:48:46 +0100264 if ((fd_evts[DIR_RD] = calloc(1, fd_set_bytes)) == NULL)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200265 goto fail_srevt;
Willy Tarreaud51a5072018-01-25 16:48:46 +0100266 if ((fd_evts[DIR_WR] = calloc(1, fd_set_bytes)) == NULL)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200267 goto fail_swevt;
268
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200269 hap_register_per_thread_init(init_select_per_thread);
270 hap_register_per_thread_deinit(deinit_select_per_thread);
271
Willy Tarreaue54e9172007-04-09 09:23:31 +0200272 return 1;
273
274 fail_swevt:
275 free(fd_evts[DIR_RD]);
276 fail_srevt:
Willy Tarreaue54e9172007-04-09 09:23:31 +0200277 p->pref = 0;
278 return 0;
279}
280
281/*
282 * Termination of the select() poller.
283 * Memory is released and the poller is marked as unselectable.
284 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100285static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200286{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200287 free(fd_evts[DIR_WR]);
288 free(fd_evts[DIR_RD]);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200289 p->private = NULL;
290 p->pref = 0;
291}
292
293/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200294 * Check that the poller works.
295 * Returns 1 if OK, otherwise 0.
296 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100297static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200298{
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200299 if (global.maxsock > FD_SETSIZE)
300 return 0;
301
Willy Tarreau2ff76222007-04-09 19:29:56 +0200302 return 1;
303}
304
305/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200306 * It is a constructor, which means that it will automatically be called before
307 * main(). This is GCC-specific but it works at least since 2.95.
308 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200309 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200310__attribute__((constructor))
311static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200312{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200313 struct poller *p;
314
315 if (nbpollers >= MAX_POLLERS)
316 return;
317 p = &pollers[nbpollers++];
318
Willy Tarreau4f60f162007-04-08 16:39:58 +0200319 p->name = "select";
320 p->pref = 150;
Willy Tarreau5a767692017-03-13 11:38:28 +0100321 p->flags = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200322 p->private = NULL;
323
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100324 p->clo = __fd_clo;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200325 p->test = _do_test;
326 p->init = _do_init;
327 p->term = _do_term;
328 p->poll = _do_poll;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200329}
330
331
332/*
333 * Local variables:
334 * c-indent-level: 8
335 * c-basic-offset: 8
336 * End:
337 */