]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm/ModPoll.cc
Simplify appending SBuf to String (#2108)
[thirdparty/squid.git] / src / comm / ModPoll.cc
CommitLineData
1b3db6d9 1/*
1f7b830e 2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
1b3db6d9 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
1b3db6d9 7 */
bbc27441
AJ
8
9/* DEBUG: section 05 Socket Functions */
10
f7f3304a 11#include "squid.h"
d841c88d
AJ
12
13#if USE_POLL
65d448bc 14#include "anyp/PortCfg.h"
1b76e6c1 15#include "comm/Connection.h"
d841c88d 16#include "comm/Loops.h"
c4ad1349 17#include "fd.h"
d841c88d 18#include "fde.h"
582c2af2 19#include "globals.h"
1b76e6c1 20#include "ICP.h"
8822ebee 21#include "mgr/Registration.h"
4d5904f7 22#include "SquidConfig.h"
e4f1fdae 23#include "StatCounters.h"
e6ccf245 24#include "Store.h"
1b3db6d9 25
1a30fdf5 26#include <cerrno>
dc47f531
AJ
27#if HAVE_POLL_H
28#include <poll.h>
29#endif
30
31/* Needed for poll() on Linux at least */
32#if USE_POLL
33#ifndef POLLRDNORM
34#define POLLRDNORM POLLIN
35#endif
36#ifndef POLLWRNORM
37#define POLLWRNORM POLLOUT
38#endif
39#endif
40
f53969cc 41static int MAX_POLL_TIME = 1000; /* see also Comm::QuickPollRequired() */
1b3db6d9 42
43#ifndef howmany
44#define howmany(x, y) (((x)+((y)-1))/(y))
45#endif
46#ifndef NBBY
47#define NBBY 8
48#endif
49#define FD_MASK_BYTES sizeof(fd_mask)
50#define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
51
52/* STATIC */
65d448bc
AJ
53static int fdIsTcpListen(int fd);
54static int fdIsUdpListen(int fd);
1b3db6d9 55static int fdIsDns(int fd);
56static OBJH commIncomingStats;
57static int comm_check_incoming_poll_handlers(int nfds, int *fds);
58static void comm_poll_dns_incoming(void);
1b3db6d9 59
1b3db6d9 60void
d841c88d 61Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout)
1b3db6d9 62{
63 fde *F = &fd_table[fd];
64 assert(fd >= 0);
508e3438 65 assert(F->flags.open || (!handler && !client_data && !timeout));
bf95c10a 66 debugs(5, 5, "FD " << fd << ", type=" << type <<
48e7baac
AJ
67 ", handler=" << handler << ", client_data=" << client_data <<
68 ", timeout=" << timeout);
62e76326 69
1b3db6d9 70 if (type & COMM_SELECT_READ) {
62e76326 71 F->read_handler = handler;
72 F->read_data = client_data;
1b3db6d9 73 }
62e76326 74
1b3db6d9 75 if (type & COMM_SELECT_WRITE) {
62e76326 76 F->write_handler = handler;
77 F->write_data = client_data;
1b3db6d9 78 }
62e76326 79
1b3db6d9 80 if (timeout)
62e76326 81 F->timeout = squid_curtime + timeout;
1b3db6d9 82}
83
84static int
65d448bc 85fdIsUdpListen(int fd)
1b3db6d9 86{
aee3523a 87 if (icpIncomingConn != nullptr && icpIncomingConn->fd == fd)
62e76326 88 return 1;
89
aee3523a 90 if (icpOutgoingConn != nullptr && icpOutgoingConn->fd == fd)
62e76326 91 return 1;
92
1b3db6d9 93 return 0;
94}
95
96static int
97fdIsDns(int fd)
98{
4d6c8504
AJ
99 if (fd == DnsSocketA)
100 return 1;
101
102 if (fd == DnsSocketB)
62e76326 103 return 1;
104
1b3db6d9 105 return 0;
106}
107
108static int
65d448bc 109fdIsTcpListen(int fd)
1b3db6d9 110{
aee3523a
AR
111 for (AnyP::PortCfgPointer s = HttpPortList; s != nullptr; s = s->next) {
112 if (s->listenConn != nullptr && s->listenConn->fd == fd)
62e76326 113 return 1;
1b3db6d9 114 }
62e76326 115
1b3db6d9 116 return 0;
117}
118
1b3db6d9 119static int
120comm_check_incoming_poll_handlers(int nfds, int *fds)
121{
122 int i;
123 int fd;
aee3523a 124 PF *hdl = nullptr;
1b3db6d9 125 int npfds;
62e76326 126
65d448bc 127 struct pollfd pfds[3 + MAXTCPLISTENPORTS];
1b3db6d9 128 incoming_sockets_accepted = 0;
62e76326 129
098346fd 130 for (i = npfds = 0; i < nfds; ++i) {
62e76326 131 int events;
132 fd = fds[i];
133 events = 0;
134
135 if (fd_table[fd].read_handler)
136 events |= POLLRDNORM;
137
138 if (fd_table[fd].write_handler)
139 events |= POLLWRNORM;
140
141 if (events) {
142 pfds[npfds].fd = fd;
143 pfds[npfds].events = events;
144 pfds[npfds].revents = 0;
cbebe602 145 ++npfds;
62e76326 146 }
1b3db6d9 147 }
62e76326 148
316fd866 149 if (!nfds)
62e76326 150 return -1;
62e76326 151
1b3db6d9 152 getCurrentTime();
098346fd 153 ++ statCounter.syscalls.selects;
62e76326 154
316fd866 155 if (poll(pfds, npfds, 0) < 1)
62e76326 156 return incoming_sockets_accepted;
62e76326 157
cbebe602 158 for (i = 0; i < npfds; ++i) {
62e76326 159 int revents;
160
161 if (((revents = pfds[i].revents) == 0) || ((fd = pfds[i].fd) == -1))
162 continue;
163
164 if (revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR)) {
165 if ((hdl = fd_table[fd].read_handler)) {
aee3523a 166 fd_table[fd].read_handler = nullptr;
62e76326 167 hdl(fd, fd_table[fd].read_data);
168 } else if (pfds[i].events & POLLRDNORM)
e0236918 169 debugs(5, DBG_IMPORTANT, "comm_poll_incoming: FD " << fd << " NULL read handler");
62e76326 170 }
171
172 if (revents & (POLLWRNORM | POLLOUT | POLLHUP | POLLERR)) {
173 if ((hdl = fd_table[fd].write_handler)) {
aee3523a 174 fd_table[fd].write_handler = nullptr;
62e76326 175 hdl(fd, fd_table[fd].write_data);
176 } else if (pfds[i].events & POLLWRNORM)
e0236918 177 debugs(5, DBG_IMPORTANT, "comm_poll_incoming: FD " << fd << " NULL write_handler");
62e76326 178 }
1b3db6d9 179 }
62e76326 180
1b3db6d9 181 return incoming_sockets_accepted;
182}
183
184static void
65d448bc 185comm_poll_udp_incoming(void)
1b3db6d9 186{
187 int nfds = 0;
188 int fds[2];
62e76326 189
098346fd
FC
190 if (Comm::IsConnOpen(icpIncomingConn)) {
191 fds[nfds] = icpIncomingConn->fd;
192 ++nfds;
193 }
62e76326 194
098346fd
FC
195 if (icpIncomingConn != icpOutgoingConn && Comm::IsConnOpen(icpOutgoingConn)) {
196 fds[nfds] = icpOutgoingConn->fd;
197 ++nfds;
198 }
62e76326 199
17c0af35
AJ
200 if (statCounter.comm_udp.startPolling(nfds)) {
201 auto n = comm_check_incoming_poll_handlers(nfds, fds);
202 statCounter.comm_udp.finishPolling(n, Config.comm_incoming.udp);
203 }
1b3db6d9 204}
205
206static void
65d448bc 207comm_poll_tcp_incoming(void)
1b3db6d9 208{
209 int nfds = 0;
65d448bc 210 int fds[MAXTCPLISTENPORTS];
62e76326 211
65d448bc 212 // XXX: only poll sockets that won't be deferred. But how do we identify them?
a46d2c0e 213
17c0af35
AJ
214 for (AnyP::PortCfgPointer s = HttpPortList; s != nullptr; s = s->next) {
215 if (Comm::IsConnOpen(s->listenConn)) {
216 fds[nfds] = s->listenConn->fd;
217 ++nfds;
218 }
1b3db6d9 219 }
62e76326 220
17c0af35
AJ
221 if (statCounter.comm_tcp.startPolling(nfds)) {
222 auto n = comm_check_incoming_poll_handlers(nfds, fds);
223 statCounter.comm_tcp.finishPolling(n, Config.comm_incoming.tcp);
224 }
1b3db6d9 225}
226
227/* poll all sockets; call handlers for those that are ready. */
c8407295 228Comm::Flag
d841c88d 229Comm::DoSelect(int msec)
1b3db6d9 230{
231 struct pollfd pfds[SQUID_MAXFD];
62e76326 232
aee3523a 233 PF *hdl = nullptr;
1b3db6d9 234 int fd;
1b3db6d9 235 int maxfd;
236 unsigned long nfds;
237 unsigned long npending;
238 int num;
65d448bc 239 int calldns = 0, calludp = 0, calltcp = 0;
1b3db6d9 240 double timeout = current_dtime + (msec / 1000.0);
62e76326 241
1b3db6d9 242 do {
62e76326 243 double start;
244 getCurrentTime();
245 start = current_dtime;
62e76326 246
17c0af35 247 if (statCounter.comm_udp.check())
65d448bc 248 comm_poll_udp_incoming();
62e76326 249
17c0af35 250 if (statCounter.comm_dns.check())
62e76326 251 comm_poll_dns_incoming();
252
17c0af35 253 if (statCounter.comm_tcp.check())
65d448bc 254 comm_poll_tcp_incoming();
62e76326 255
65d448bc 256 calldns = calludp = calltcp = 0;
62e76326 257
258 nfds = 0;
259
260 npending = 0;
261
262 maxfd = Biggest_FD + 1;
263
098346fd 264 for (int i = 0; i < maxfd; ++i) {
62e76326 265 int events;
266 events = 0;
267 /* Check each open socket for a handler. */
268
a46d2c0e 269 if (fd_table[i].read_handler)
270 events |= POLLRDNORM;
62e76326 271
272 if (fd_table[i].write_handler)
273 events |= POLLWRNORM;
274
275 if (events) {
276 pfds[nfds].fd = i;
277 pfds[nfds].events = events;
278 pfds[nfds].revents = 0;
cbebe602 279 ++nfds;
62e76326 280
281 if ((events & POLLRDNORM) && fd_table[i].flags.read_pending)
cbebe602 282 ++npending;
62e76326 283 }
284 }
285
62e76326 286 if (npending)
287 msec = 0;
288
289 if (msec > MAX_POLL_TIME)
290 msec = MAX_POLL_TIME;
291
8ff3fa2e 292 /* nothing to do
293 *
294 * Note that this will only ever trigger when there are no log files
295 * and stdout/err/in are all closed too.
296 */
425e3a42 297 if (nfds == 0 && npending == 0) {
a553a5a3 298 if (shutting_down)
23ff0bee 299 return Comm::SHUTDOWN;
a553a5a3 300 else
23ff0bee 301 return Comm::IDLE;
a553a5a3 302 }
303
62e76326 304 for (;;) {
098346fd 305 ++ statCounter.syscalls.selects;
62e76326 306 num = poll(pfds, nfds, msec);
b69e9ffa 307 int xerrno = errno;
098346fd 308 ++ statCounter.select_loops;
62e76326 309
425e3a42 310 if (num >= 0 || npending > 0)
62e76326 311 break;
312
b69e9ffa 313 if (ignoreErrno(xerrno))
62e76326 314 continue;
315
b69e9ffa 316 debugs(5, DBG_CRITICAL, MYNAME << "poll failure: " << xstrerr(xerrno));
62e76326 317
b69e9ffa 318 assert(xerrno != EINVAL);
62e76326 319
4ee57cbe 320 return Comm::COMM_ERROR;
62e76326 321
322 /* NOTREACHED */
323 }
324
40a77eef 325 getCurrentTime();
326
bf8fe701 327 debugs(5, num ? 5 : 8, "comm_poll: " << num << "+" << npending << " FDs ready");
f30f7998 328 statCounter.select_fds_hist.count(num);
62e76326 329
330 if (num == 0 && npending == 0)
331 continue;
332
333 /* scan each socket but the accept socket. Poll this
26ac0430 334 * more frequently to minimize losses due to the 5 connect
62e76326 335 * limit in SunOS */
62e76326 336
098346fd 337 for (size_t loopIndex = 0; loopIndex < nfds; ++loopIndex) {
62e76326 338 fde *F;
339 int revents = pfds[loopIndex].revents;
340 fd = pfds[loopIndex].fd;
341
342 if (fd == -1)
343 continue;
344
345 if (fd_table[fd].flags.read_pending)
346 revents |= POLLIN;
347
348 if (revents == 0)
349 continue;
350
65d448bc
AJ
351 if (fdIsUdpListen(fd)) {
352 calludp = 1;
62e76326 353 continue;
354 }
355
356 if (fdIsDns(fd)) {
357 calldns = 1;
358 continue;
359 }
360
65d448bc
AJ
361 if (fdIsTcpListen(fd)) {
362 calltcp = 1;
62e76326 363 continue;
364 }
365
366 F = &fd_table[fd];
367
368 if (revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR)) {
bf8fe701 369 debugs(5, 6, "comm_poll: FD " << fd << " ready for reading");
62e76326 370
65d448bc 371 if ((hdl = F->read_handler)) {
aee3523a 372 F->read_handler = nullptr;
62e76326 373 hdl(fd, F->read_data);
098346fd 374 ++ statCounter.select_fds;
62e76326 375
17c0af35 376 if (statCounter.comm_udp.check())
65d448bc 377 comm_poll_udp_incoming();
62e76326 378
17c0af35 379 if (statCounter.comm_dns.check())
62e76326 380 comm_poll_dns_incoming();
381
17c0af35 382 if (statCounter.comm_tcp.check())
65d448bc 383 comm_poll_tcp_incoming();
62e76326 384 }
385 }
386
387 if (revents & (POLLWRNORM | POLLOUT | POLLHUP | POLLERR)) {
48e7baac 388 debugs(5, 6, "comm_poll: FD " << fd << " ready for writing");
62e76326 389
390 if ((hdl = F->write_handler)) {
aee3523a 391 F->write_handler = nullptr;
62e76326 392 hdl(fd, F->write_data);
098346fd 393 ++ statCounter.select_fds;
62e76326 394
17c0af35 395 if (statCounter.comm_udp.check())
65d448bc 396 comm_poll_udp_incoming();
62e76326 397
17c0af35 398 if (statCounter.comm_dns.check())
62e76326 399 comm_poll_dns_incoming();
400
17c0af35 401 if (statCounter.comm_tcp.check())
65d448bc 402 comm_poll_tcp_incoming();
62e76326 403 }
404 }
405
406 if (revents & POLLNVAL) {
6d527e0a 407 AsyncCall::Pointer ch;
fa84c01d
FC
408 debugs(5, DBG_CRITICAL, "WARNING: FD " << fd << " has handlers, but it's invalid.");
409 debugs(5, DBG_CRITICAL, "FD " << fd << " is a " << fdTypeStr[F->type]);
410 debugs(5, DBG_CRITICAL, "--> " << F->desc);
411 debugs(5, DBG_CRITICAL, "tmout:" << F->timeoutHandler << "read:" <<
bf8fe701 412 F->read_handler << " write:" << F->write_handler);
62e76326 413
aee3523a 414 for (ch = F->closeHandler; ch != nullptr; ch = ch->Next())
fa84c01d 415 debugs(5, DBG_CRITICAL, " close handler: " << ch);
62e76326 416
aee3523a 417 if (F->closeHandler != nullptr) {
62e76326 418 commCallCloseHandlers(fd);
aee3523a 419 } else if (F->timeoutHandler != nullptr) {
fa84c01d 420 debugs(5, DBG_CRITICAL, "comm_poll: Calling Timeout Handler");
26ac0430 421 ScheduleCallHere(F->timeoutHandler);
62e76326 422 }
423
aee3523a
AR
424 F->closeHandler = nullptr;
425 F->timeoutHandler = nullptr;
426 F->read_handler = nullptr;
427 F->write_handler = nullptr;
62e76326 428
429 if (F->flags.open)
430 fd_close(fd);
431 }
432 }
433
65d448bc
AJ
434 if (calludp)
435 comm_poll_udp_incoming();
62e76326 436
437 if (calldns)
438 comm_poll_dns_incoming();
439
65d448bc
AJ
440 if (calltcp)
441 comm_poll_tcp_incoming();
62e76326 442
62e76326 443 getCurrentTime();
444
445 statCounter.select_time += (current_dtime - start);
446
c8407295 447 return Comm::OK;
62e76326 448 } while (timeout > current_dtime);
449
4a7a3d56 450 debugs(5, 8, "comm_poll: time out: " << squid_curtime << ".");
62e76326 451
c8407295 452 return Comm::TIMEOUT;
1b3db6d9 453}
454
1b3db6d9 455static void
456comm_poll_dns_incoming(void)
457{
458 int nfds = 0;
459 int fds[2];
62e76326 460
098346fd
FC
461 if (DnsSocketA >= 0) {
462 fds[nfds] = DnsSocketA;
463 ++nfds;
464 }
4d6c8504 465
098346fd
FC
466 if (DnsSocketB >= 0) {
467 fds[nfds] = DnsSocketB;
468 ++nfds;
469 }
62e76326 470
17c0af35
AJ
471 if (statCounter.comm_dns.startPolling(nfds)) {
472 auto n = comm_check_incoming_poll_handlers(nfds, fds);
473 statCounter.comm_dns.finishPolling(n, Config.comm_incoming.dns);
474 }
1b3db6d9 475}
476
5acc9f37 477static void
edfab338 478commPollRegisterWithCacheManager(void)
1b3db6d9 479{
8822ebee 480 Mgr::RegisterAction("comm_poll_incoming",
d9fc6862
A
481 "comm_incoming() stats",
482 commIncomingStats, 0, 1);
1b3db6d9 483}
484
5acc9f37 485void
d841c88d 486Comm::SelectLoopInit(void)
5acc9f37
FC
487{
488 commPollRegisterWithCacheManager();
489}
1b3db6d9 490
491static void
492commIncomingStats(StoreEntry * sentry)
493{
65d448bc 494 storeAppendPrintf(sentry, "Current incoming_udp_interval: %d\n",
17c0af35 495 statCounter.comm_udp.interval >> Comm::Incoming::Factor);
1b3db6d9 496 storeAppendPrintf(sentry, "Current incoming_dns_interval: %d\n",
17c0af35 497 statCounter.comm_dns.interval >> Comm::Incoming::Factor);
65d448bc 498 storeAppendPrintf(sentry, "Current incoming_tcp_interval: %d\n",
17c0af35 499 statCounter.comm_tcp.interval >> Comm::Incoming::Factor);
1b3db6d9 500 storeAppendPrintf(sentry, "\n");
501 storeAppendPrintf(sentry, "Histogram of events per incoming socket type\n");
65d448bc 502 storeAppendPrintf(sentry, "ICP Messages handled per comm_poll_udp_incoming() call:\n");
17c0af35 503 statCounter.comm_udp.history.dump(sentry, statHistIntDumper);
1b3db6d9 504 storeAppendPrintf(sentry, "DNS Messages handled per comm_poll_dns_incoming() call:\n");
17c0af35 505 statCounter.comm_dns.history.dump(sentry, statHistIntDumper);
65d448bc 506 storeAppendPrintf(sentry, "HTTP Messages handled per comm_poll_tcp_incoming() call:\n");
17c0af35 507 statCounter.comm_tcp.history.dump(sentry, statHistIntDumper);
1b3db6d9 508}
509
1b3db6d9 510/* Called by async-io or diskd to speed up the polling */
511void
d841c88d 512Comm::QuickPollRequired(void)
1b3db6d9 513{
514 MAX_POLL_TIME = 10;
515}
516
517#endif /* USE_POLL */
f53969cc 518