2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 05 Socket Functions */
14 #include "anyp/PortCfg.h"
15 #include "comm/Connection.h"
16 #include "comm/Loops.h"
21 #include "mgr/Registration.h"
22 #include "SquidConfig.h"
23 #include "StatCounters.h"
31 /* Needed for poll() on Linux at least */
34 #define POLLRDNORM POLLIN
37 #define POLLWRNORM POLLOUT
41 static int MAX_POLL_TIME
= 1000; /* see also Comm::QuickPollRequired() */
44 #define howmany(x, y) (((x)+((y)-1))/(y))
49 #define FD_MASK_BYTES sizeof(fd_mask)
50 #define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
53 static int fdIsTcpListen(int fd
);
54 static int fdIsUdpListen(int fd
);
55 static int fdIsDns(int fd
);
56 static OBJH commIncomingStats
;
57 static int comm_check_incoming_poll_handlers(int nfds
, int *fds
);
58 static void comm_poll_dns_incoming(void);
61 * Automatic tuning for incoming requests:
63 * INCOMING sockets are the ICP and HTTP ports. We need to check these
64 * fairly regularly, but how often? When the load increases, we
65 * want to check the incoming sockets more often. If we have a lot
66 * of incoming ICP, then we need to check these sockets more than
67 * if we just have HTTP.
69 * The variables 'incoming_icp_interval' and 'incoming_http_interval'
70 * determine how many normal I/O events to process before checking
71 * incoming sockets again. Note we store the incoming_interval
72 * multiplied by a factor of (2^INCOMING_FACTOR) to have some
73 * pseudo-floating point precision.
75 * The variable 'udp_io_events' and 'tcp_io_events' counts how many normal
76 * I/O events have been processed since the last check on the incoming
77 * sockets. When io_events > incoming_interval, its time to check incoming
80 * Every time we check incoming sockets, we count how many new messages
81 * or connections were processed. This is used to adjust the
82 * incoming_interval for the next iteration. The new incoming_interval
83 * is calculated as the current incoming_interval plus what we would
84 * like to see as an average number of events minus the number of
85 * events just processed.
87 * incoming_interval = incoming_interval + target_average - number_of_events_processed
89 * There are separate incoming_interval counters for TCP-based, UDP-based, and DNS events
91 * You can see the current values of the incoming_interval's, as well as
92 * a histogram of 'incoming_events' by asking the cache manager
93 * for 'comm_incoming', e.g.:
95 * % ./client mgr:comm_poll_incoming
99 * - We have MAX_INCOMING_INTEGER as a magic upper limit on
100 * incoming_interval for both types of sockets. At the
101 * largest value the cache will effectively be idling.
103 * - The higher the INCOMING_FACTOR, the slower the algorithm will
104 * respond to load spikes/increases/decreases in demand. A value
105 * between 3 and 8 is recommended.
108 #define MAX_INCOMING_INTEGER 256
109 #define INCOMING_FACTOR 5
110 #define MAX_INCOMING_INTERVAL (MAX_INCOMING_INTEGER << INCOMING_FACTOR)
111 static int udp_io_events
= 0; ///< I/O events passed since last UDP receiver socket poll
112 static int dns_io_events
= 0; ///< I/O events passed since last DNS socket poll
113 static int tcp_io_events
= 0; ///< I/O events passed since last TCP listening socket poll
114 static int incoming_udp_interval
= 16 << INCOMING_FACTOR
;
115 static int incoming_dns_interval
= 16 << INCOMING_FACTOR
;
116 static int incoming_tcp_interval
= 16 << INCOMING_FACTOR
;
117 #define commCheckUdpIncoming (++udp_io_events > (incoming_udp_interval>> INCOMING_FACTOR))
118 #define commCheckDnsIncoming (++dns_io_events > (incoming_dns_interval>> INCOMING_FACTOR))
119 #define commCheckTcpIncoming (++tcp_io_events > (incoming_tcp_interval>> INCOMING_FACTOR))
122 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
124 fde
*F
= &fd_table
[fd
];
126 assert(F
->flags
.open
|| (!handler
&& !client_data
&& !timeout
));
127 debugs(5, 5, "FD " << fd
<< ", type=" << type
<<
128 ", handler=" << handler
<< ", client_data=" << client_data
<<
129 ", timeout=" << timeout
);
131 if (type
& COMM_SELECT_READ
) {
132 F
->read_handler
= handler
;
133 F
->read_data
= client_data
;
136 if (type
& COMM_SELECT_WRITE
) {
137 F
->write_handler
= handler
;
138 F
->write_data
= client_data
;
142 F
->timeout
= squid_curtime
+ timeout
;
146 fdIsUdpListen(int fd
)
148 if (icpIncomingConn
!= nullptr && icpIncomingConn
->fd
== fd
)
151 if (icpOutgoingConn
!= nullptr && icpOutgoingConn
->fd
== fd
)
160 if (fd
== DnsSocketA
)
163 if (fd
== DnsSocketB
)
170 fdIsTcpListen(int fd
)
172 for (AnyP::PortCfgPointer s
= HttpPortList
; s
!= nullptr; s
= s
->next
) {
173 if (s
->listenConn
!= nullptr && s
->listenConn
->fd
== fd
)
181 comm_check_incoming_poll_handlers(int nfds
, int *fds
)
188 struct pollfd pfds
[3 + MAXTCPLISTENPORTS
];
189 incoming_sockets_accepted
= 0;
191 for (i
= npfds
= 0; i
< nfds
; ++i
) {
196 if (fd_table
[fd
].read_handler
)
197 events
|= POLLRDNORM
;
199 if (fd_table
[fd
].write_handler
)
200 events
|= POLLWRNORM
;
204 pfds
[npfds
].events
= events
;
205 pfds
[npfds
].revents
= 0;
214 ++ statCounter
.syscalls
.selects
;
216 if (poll(pfds
, npfds
, 0) < 1)
217 return incoming_sockets_accepted
;
219 for (i
= 0; i
< npfds
; ++i
) {
222 if (((revents
= pfds
[i
].revents
) == 0) || ((fd
= pfds
[i
].fd
) == -1))
225 if (revents
& (POLLRDNORM
| POLLIN
| POLLHUP
| POLLERR
)) {
226 if ((hdl
= fd_table
[fd
].read_handler
)) {
227 fd_table
[fd
].read_handler
= nullptr;
228 hdl(fd
, fd_table
[fd
].read_data
);
229 } else if (pfds
[i
].events
& POLLRDNORM
)
230 debugs(5, DBG_IMPORTANT
, "comm_poll_incoming: FD " << fd
<< " NULL read handler");
233 if (revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
| POLLERR
)) {
234 if ((hdl
= fd_table
[fd
].write_handler
)) {
235 fd_table
[fd
].write_handler
= nullptr;
236 hdl(fd
, fd_table
[fd
].write_data
);
237 } else if (pfds
[i
].events
& POLLWRNORM
)
238 debugs(5, DBG_IMPORTANT
, "comm_poll_incoming: FD " << fd
<< " NULL write_handler");
242 return incoming_sockets_accepted
;
246 comm_poll_udp_incoming(void)
253 if (Comm::IsConnOpen(icpIncomingConn
)) {
254 fds
[nfds
] = icpIncomingConn
->fd
;
258 if (icpIncomingConn
!= icpOutgoingConn
&& Comm::IsConnOpen(icpOutgoingConn
)) {
259 fds
[nfds
] = icpOutgoingConn
->fd
;
266 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
268 incoming_udp_interval
+= Config
.comm_incoming
.udp
.average
- nevents
;
270 if (incoming_udp_interval
< Config
.comm_incoming
.udp
.min_poll
)
271 incoming_udp_interval
= Config
.comm_incoming
.udp
.min_poll
;
273 if (incoming_udp_interval
> MAX_INCOMING_INTERVAL
)
274 incoming_udp_interval
= MAX_INCOMING_INTERVAL
;
276 if (nevents
> INCOMING_UDP_MAX
)
277 nevents
= INCOMING_UDP_MAX
;
279 statCounter
.comm_udp_incoming
.count(nevents
);
283 comm_poll_tcp_incoming(void)
286 int fds
[MAXTCPLISTENPORTS
];
291 // XXX: only poll sockets that won't be deferred. But how do we identify them?
293 for (j
= 0; j
< NHttpSockets
; ++j
) {
294 if (HttpSockets
[j
] < 0)
297 fds
[nfds
] = HttpSockets
[j
];
301 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
302 incoming_tcp_interval
= incoming_tcp_interval
303 + Config
.comm_incoming
.tcp
.average
- nevents
;
305 if (incoming_tcp_interval
< Config
.comm_incoming
.tcp
.min_poll
)
306 incoming_tcp_interval
= Config
.comm_incoming
.tcp
.min_poll
;
308 if (incoming_tcp_interval
> MAX_INCOMING_INTERVAL
)
309 incoming_tcp_interval
= MAX_INCOMING_INTERVAL
;
311 if (nevents
> INCOMING_TCP_MAX
)
312 nevents
= INCOMING_TCP_MAX
;
314 statCounter
.comm_tcp_incoming
.count(nevents
);
317 /* poll all sockets; call handlers for those that are ready. */
319 Comm::DoSelect(int msec
)
321 struct pollfd pfds
[SQUID_MAXFD
];
327 unsigned long npending
;
329 int calldns
= 0, calludp
= 0, calltcp
= 0;
330 double timeout
= current_dtime
+ (msec
/ 1000.0);
335 start
= current_dtime
;
337 if (commCheckUdpIncoming
)
338 comm_poll_udp_incoming();
340 if (commCheckDnsIncoming
)
341 comm_poll_dns_incoming();
343 if (commCheckTcpIncoming
)
344 comm_poll_tcp_incoming();
346 calldns
= calludp
= calltcp
= 0;
352 maxfd
= Biggest_FD
+ 1;
354 for (int i
= 0; i
< maxfd
; ++i
) {
357 /* Check each open socket for a handler. */
359 if (fd_table
[i
].read_handler
)
360 events
|= POLLRDNORM
;
362 if (fd_table
[i
].write_handler
)
363 events
|= POLLWRNORM
;
367 pfds
[nfds
].events
= events
;
368 pfds
[nfds
].revents
= 0;
371 if ((events
& POLLRDNORM
) && fd_table
[i
].flags
.read_pending
)
379 if (msec
> MAX_POLL_TIME
)
380 msec
= MAX_POLL_TIME
;
384 * Note that this will only ever trigger when there are no log files
385 * and stdout/err/in are all closed too.
387 if (nfds
== 0 && npending
== 0) {
389 return Comm::SHUTDOWN
;
395 ++ statCounter
.syscalls
.selects
;
396 num
= poll(pfds
, nfds
, msec
);
398 ++ statCounter
.select_loops
;
400 if (num
>= 0 || npending
> 0)
403 if (ignoreErrno(xerrno
))
406 debugs(5, DBG_CRITICAL
, MYNAME
<< "poll failure: " << xstrerr(xerrno
));
408 assert(xerrno
!= EINVAL
);
410 return Comm::COMM_ERROR
;
417 debugs(5, num
? 5 : 8, "comm_poll: " << num
<< "+" << npending
<< " FDs ready");
418 statCounter
.select_fds_hist
.count(num
);
420 if (num
== 0 && npending
== 0)
423 /* scan each socket but the accept socket. Poll this
424 * more frequently to minimize losses due to the 5 connect
427 for (size_t loopIndex
= 0; loopIndex
< nfds
; ++loopIndex
) {
429 int revents
= pfds
[loopIndex
].revents
;
430 fd
= pfds
[loopIndex
].fd
;
435 if (fd_table
[fd
].flags
.read_pending
)
441 if (fdIsUdpListen(fd
)) {
451 if (fdIsTcpListen(fd
)) {
458 if (revents
& (POLLRDNORM
| POLLIN
| POLLHUP
| POLLERR
)) {
459 debugs(5, 6, "comm_poll: FD " << fd
<< " ready for reading");
461 if ((hdl
= F
->read_handler
)) {
462 F
->read_handler
= nullptr;
463 hdl(fd
, F
->read_data
);
464 ++ statCounter
.select_fds
;
466 if (commCheckUdpIncoming
)
467 comm_poll_udp_incoming();
469 if (commCheckDnsIncoming
)
470 comm_poll_dns_incoming();
472 if (commCheckTcpIncoming
)
473 comm_poll_tcp_incoming();
477 if (revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
| POLLERR
)) {
478 debugs(5, 6, "comm_poll: FD " << fd
<< " ready for writing");
480 if ((hdl
= F
->write_handler
)) {
481 F
->write_handler
= nullptr;
482 hdl(fd
, F
->write_data
);
483 ++ statCounter
.select_fds
;
485 if (commCheckUdpIncoming
)
486 comm_poll_udp_incoming();
488 if (commCheckDnsIncoming
)
489 comm_poll_dns_incoming();
491 if (commCheckTcpIncoming
)
492 comm_poll_tcp_incoming();
496 if (revents
& POLLNVAL
) {
497 AsyncCall::Pointer ch
;
498 debugs(5, DBG_CRITICAL
, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
499 debugs(5, DBG_CRITICAL
, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
]);
500 debugs(5, DBG_CRITICAL
, "--> " << F
->desc
);
501 debugs(5, DBG_CRITICAL
, "tmout:" << F
->timeoutHandler
<< "read:" <<
502 F
->read_handler
<< " write:" << F
->write_handler
);
504 for (ch
= F
->closeHandler
; ch
!= nullptr; ch
= ch
->Next())
505 debugs(5, DBG_CRITICAL
, " close handler: " << ch
);
507 if (F
->closeHandler
!= nullptr) {
508 commCallCloseHandlers(fd
);
509 } else if (F
->timeoutHandler
!= nullptr) {
510 debugs(5, DBG_CRITICAL
, "comm_poll: Calling Timeout Handler");
511 ScheduleCallHere(F
->timeoutHandler
);
514 F
->closeHandler
= nullptr;
515 F
->timeoutHandler
= nullptr;
516 F
->read_handler
= nullptr;
517 F
->write_handler
= nullptr;
525 comm_poll_udp_incoming();
528 comm_poll_dns_incoming();
531 comm_poll_tcp_incoming();
535 statCounter
.select_time
+= (current_dtime
- start
);
538 } while (timeout
> current_dtime
);
540 debugs(5, 8, "comm_poll: time out: " << squid_curtime
<< ".");
542 return Comm::TIMEOUT
;
546 comm_poll_dns_incoming(void)
553 if (DnsSocketA
< 0 && DnsSocketB
< 0)
556 if (DnsSocketA
>= 0) {
557 fds
[nfds
] = DnsSocketA
;
561 if (DnsSocketB
>= 0) {
562 fds
[nfds
] = DnsSocketB
;
566 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
571 incoming_dns_interval
+= Config
.comm_incoming
.dns
.average
- nevents
;
573 if (incoming_dns_interval
< Config
.comm_incoming
.dns
.min_poll
)
574 incoming_dns_interval
= Config
.comm_incoming
.dns
.min_poll
;
576 if (incoming_dns_interval
> MAX_INCOMING_INTERVAL
)
577 incoming_dns_interval
= MAX_INCOMING_INTERVAL
;
579 if (nevents
> INCOMING_DNS_MAX
)
580 nevents
= INCOMING_DNS_MAX
;
582 statCounter
.comm_dns_incoming
.count(nevents
);
586 commPollRegisterWithCacheManager(void)
588 Mgr::RegisterAction("comm_poll_incoming",
589 "comm_incoming() stats",
590 commIncomingStats
, 0, 1);
594 Comm::SelectLoopInit(void)
596 commPollRegisterWithCacheManager();
600 commIncomingStats(StoreEntry
* sentry
)
602 storeAppendPrintf(sentry
, "Current incoming_udp_interval: %d\n",
603 incoming_udp_interval
>> INCOMING_FACTOR
);
604 storeAppendPrintf(sentry
, "Current incoming_dns_interval: %d\n",
605 incoming_dns_interval
>> INCOMING_FACTOR
);
606 storeAppendPrintf(sentry
, "Current incoming_tcp_interval: %d\n",
607 incoming_tcp_interval
>> INCOMING_FACTOR
);
608 storeAppendPrintf(sentry
, "\n");
609 storeAppendPrintf(sentry
, "Histogram of events per incoming socket type\n");
610 storeAppendPrintf(sentry
, "ICP Messages handled per comm_poll_udp_incoming() call:\n");
611 statCounter
.comm_udp_incoming
.dump(sentry
, statHistIntDumper
);
612 storeAppendPrintf(sentry
, "DNS Messages handled per comm_poll_dns_incoming() call:\n");
613 statCounter
.comm_dns_incoming
.dump(sentry
, statHistIntDumper
);
614 storeAppendPrintf(sentry
, "HTTP Messages handled per comm_poll_tcp_incoming() call:\n");
615 statCounter
.comm_tcp_incoming
.dump(sentry
, statHistIntDumper
);
618 /* Called by async-io or diskd to speed up the polling */
620 Comm::QuickPollRequired(void)
625 #endif /* USE_POLL */