2 * Copyright (C) 1996-2025 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 */
15 #include "anyp/PortCfg.h"
16 #include "comm/Connection.h"
17 #include "comm/Loops.h"
21 #include "mgr/Registration.h"
22 #include "SquidConfig.h"
23 #include "StatCounters.h"
32 static int MAX_POLL_TIME
= 1000; /* see also Comm::QuickPollRequired() */
35 #define howmany(x, y) (((x)+((y)-1))/(y))
40 #define FD_MASK_BYTES sizeof(fd_mask)
41 #define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
44 static int examine_select(fd_set
*, fd_set
*);
45 static int fdIsTcpListener(int fd
);
46 static int fdIsUdpListener(int fd
);
47 static int fdIsDns(int fd
);
48 static OBJH commIncomingStats
;
49 static int comm_check_incoming_select_handlers(int nfds
, int *fds
);
50 static void comm_select_dns_incoming(void);
51 static void commUpdateReadBits(int fd
, PF
* handler
);
52 static void commUpdateWriteBits(int fd
, PF
* handler
);
54 static struct timeval zero_tv
;
55 static fd_set global_readfds
;
56 static fd_set global_writefds
;
61 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
63 fde
*F
= &fd_table
[fd
];
65 assert(F
->flags
.open
|| (!handler
&& !client_data
&& !timeout
));
66 debugs(5, 5, "FD " << fd
<< ", type=" << type
<<
67 ", handler=" << handler
<< ", client_data=" << client_data
<<
68 ", timeout=" << timeout
);
70 if (type
& COMM_SELECT_READ
) {
71 F
->read_handler
= handler
;
72 F
->read_data
= client_data
;
73 commUpdateReadBits(fd
, handler
);
76 if (type
& COMM_SELECT_WRITE
) {
77 F
->write_handler
= handler
;
78 F
->write_data
= client_data
;
79 commUpdateWriteBits(fd
, handler
);
83 F
->timeout
= squid_curtime
+ timeout
;
87 fdIsUdpListener(int fd
)
89 if (icpIncomingConn
!= nullptr && fd
== icpIncomingConn
->fd
)
92 if (icpOutgoingConn
!= nullptr && fd
== icpOutgoingConn
->fd
)
101 if (fd
== DnsSocketA
)
104 if (fd
== DnsSocketB
)
111 fdIsTcpListener(int fd
)
113 for (AnyP::PortCfgPointer s
= HttpPortList
; s
!= nullptr; s
= s
->next
) {
114 if (s
->listenConn
!= nullptr && s
->listenConn
->fd
== fd
)
122 comm_check_incoming_select_handlers(int nfds
, int *fds
)
131 FD_ZERO(&write_mask
);
132 incoming_sockets_accepted
= 0;
134 for (i
= 0; i
< nfds
; ++i
) {
137 if (fd_table
[fd
].read_handler
) {
138 FD_SET(fd
, &read_mask
);
144 if (fd_table
[fd
].write_handler
) {
145 FD_SET(fd
, &write_mask
);
157 ++ statCounter
.syscalls
.selects
;
159 if (select(maxfd
, &read_mask
, &write_mask
, nullptr, &zero_tv
) < 1)
160 return incoming_sockets_accepted
;
162 for (i
= 0; i
< nfds
; ++i
) {
165 if (FD_ISSET(fd
, &read_mask
)) {
166 if ((hdl
= fd_table
[fd
].read_handler
) != nullptr) {
167 fd_table
[fd
].read_handler
= nullptr;
168 commUpdateReadBits(fd
, nullptr);
169 hdl(fd
, fd_table
[fd
].read_data
);
171 debugs(5, DBG_IMPORTANT
, "comm_select_incoming: FD " << fd
<< " NULL read handler");
175 if (FD_ISSET(fd
, &write_mask
)) {
176 if ((hdl
= fd_table
[fd
].write_handler
) != nullptr) {
177 fd_table
[fd
].write_handler
= nullptr;
178 commUpdateWriteBits(fd
, nullptr);
179 hdl(fd
, fd_table
[fd
].write_data
);
181 debugs(5, DBG_IMPORTANT
, "comm_select_incoming: FD " << fd
<< " NULL write handler");
186 return incoming_sockets_accepted
;
190 comm_select_udp_incoming(void)
195 if (Comm::IsConnOpen(icpIncomingConn
)) {
196 fds
[nfds
] = icpIncomingConn
->fd
;
200 if (Comm::IsConnOpen(icpOutgoingConn
) && icpIncomingConn
!= icpOutgoingConn
) {
201 fds
[nfds
] = icpOutgoingConn
->fd
;
205 if (statCounter
.comm_udp
.startPolling(nfds
)) {
206 auto n
= comm_check_incoming_select_handlers(nfds
, fds
);
207 statCounter
.comm_udp
.finishPolling(n
, Config
.comm_incoming
.udp
);
212 comm_select_tcp_incoming(void)
215 int fds
[MAXTCPLISTENPORTS
];
217 // XXX: only poll sockets that won't be deferred. But how do we identify them?
219 for (AnyP::PortCfgPointer s
= HttpPortList
; s
!= nullptr; s
= s
->next
) {
220 if (Comm::IsConnOpen(s
->listenConn
)) {
221 fds
[nfds
] = s
->listenConn
->fd
;
226 if (statCounter
.comm_tcp
.startPolling(nfds
)) {
227 auto n
= comm_check_incoming_select_handlers(nfds
, fds
);
228 statCounter
.comm_tcp
.finishPolling(n
, Config
.comm_incoming
.tcp
);
232 /* Select on all sockets; call handlers for those that are ready. */
234 Comm::DoSelect(int msec
)
245 int calldns
= 0, calludp
= 0, calltcp
= 0;
253 struct timeval poll_time
;
254 double timeout
= current_dtime
+ (msec
/ 1000.0);
260 start
= current_dtime
;
262 if (statCounter
.comm_udp
.check())
263 comm_select_udp_incoming();
265 if (statCounter
.comm_dns
.check())
266 comm_select_dns_incoming();
268 if (statCounter
.comm_tcp
.check())
269 comm_select_tcp_incoming();
271 calldns
= calludp
= calltcp
= 0;
273 maxfd
= Biggest_FD
+ 1;
275 memcpy(&readfds
, &global_readfds
,
276 howmany(maxfd
, FD_MASK_BITS
) * FD_MASK_BYTES
);
278 memcpy(&writefds
, &global_writefds
,
279 howmany(maxfd
, FD_MASK_BITS
) * FD_MASK_BYTES
);
281 /* remove stalled FDs, and deal with pending descriptors */
284 FD_ZERO(&pendingfds
);
286 maxindex
= howmany(maxfd
, FD_MASK_BITS
);
288 fdsp
= (fd_mask
*) & readfds
;
290 for (j
= 0; j
< maxindex
; ++j
) {
291 if ((tmask
= fdsp
[j
]) == 0)
292 continue; /* no bits here */
294 for (k
= 0; k
< FD_MASK_BITS
; ++k
) {
295 if (!EBIT_TEST(tmask
, k
))
298 /* Found a set bit */
299 fd
= (j
* FD_MASK_BITS
) + k
;
301 if (FD_ISSET(fd
, &readfds
) && fd_table
[fd
].flags
.read_pending
) {
302 FD_SET(fd
, &pendingfds
);
308 if (nreadfds
+ nwritefds
== 0) {
309 assert(shutting_down
);
310 return Comm::SHUTDOWN
;
313 if (msec
> MAX_POLL_TIME
)
314 msec
= MAX_POLL_TIME
;
320 poll_time
.tv_sec
= msec
/ 1000;
321 poll_time
.tv_usec
= (msec
% 1000) * 1000;
322 ++ statCounter
.syscalls
.selects
;
323 num
= select(maxfd
, &readfds
, &writefds
, nullptr, &poll_time
);
325 ++ statCounter
.select_loops
;
327 if (num
>= 0 || pending
> 0)
330 if (ignoreErrno(xerrno
))
333 debugs(5, DBG_CRITICAL
, MYNAME
<< "select failure: " << xstrerr(xerrno
));
335 examine_select(&readfds
, &writefds
);
337 return Comm::COMM_ERROR
;
342 if (num
< 0 && !pending
)
347 debugs(5, num
? 5 : 8, "comm_select: " << num
<< "+" << pending
<< " FDs ready");
349 statCounter
.select_fds_hist
.count(num
);
351 if (num
== 0 && pending
== 0)
354 /* Scan return fd masks for ready descriptors */
355 fdsp
= (fd_mask
*) & readfds
;
357 pfdsp
= (fd_mask
*) & pendingfds
;
359 maxindex
= howmany(maxfd
, FD_MASK_BITS
);
361 for (j
= 0; j
< maxindex
; ++j
) {
362 if ((tmask
= (fdsp
[j
] | pfdsp
[j
])) == 0)
363 continue; /* no bits here */
365 for (k
= 0; k
< FD_MASK_BITS
; ++k
) {
367 break; /* no more bits left */
369 if (!EBIT_TEST(tmask
, k
))
372 /* Found a set bit */
373 fd
= (j
* FD_MASK_BITS
) + k
;
375 EBIT_CLR(tmask
, k
); /* this will be done */
377 if (fdIsUdpListener(fd
)) {
387 if (fdIsTcpListener(fd
)) {
393 debugs(5, 6, "comm_select: FD " << fd
<< " ready for reading");
395 if (nullptr == (hdl
= F
->read_handler
))
398 F
->read_handler
= nullptr;
399 commUpdateReadBits(fd
, nullptr);
400 hdl(fd
, F
->read_data
);
401 ++ statCounter
.select_fds
;
403 if (statCounter
.comm_udp
.check())
404 comm_select_udp_incoming();
406 if (statCounter
.comm_dns
.check())
407 comm_select_dns_incoming();
409 if (statCounter
.comm_tcp
.check())
410 comm_select_tcp_incoming();
415 fdsp
= (fd_mask
*) & writefds
;
417 for (j
= 0; j
< maxindex
; ++j
) {
418 if ((tmask
= fdsp
[j
]) == 0)
419 continue; /* no bits here */
421 for (k
= 0; k
< FD_MASK_BITS
; ++k
) {
423 break; /* no more bits left */
425 if (!EBIT_TEST(tmask
, k
))
428 /* Found a set bit */
429 fd
= (j
* FD_MASK_BITS
) + k
;
431 EBIT_CLR(tmask
, k
); /* this will be done */
433 if (fdIsUdpListener(fd
)) {
443 if (fdIsTcpListener(fd
)) {
449 debugs(5, 6, "comm_select: FD " << fd
<< " ready for writing");
451 if ((hdl
= F
->write_handler
)) {
452 F
->write_handler
= nullptr;
453 commUpdateWriteBits(fd
, nullptr);
454 hdl(fd
, F
->write_data
);
455 ++ statCounter
.select_fds
;
457 if (statCounter
.comm_udp
.check())
458 comm_select_udp_incoming();
460 if (statCounter
.comm_dns
.check())
461 comm_select_dns_incoming();
463 if (statCounter
.comm_tcp
.check())
464 comm_select_tcp_incoming();
470 comm_select_udp_incoming();
473 comm_select_dns_incoming();
476 comm_select_tcp_incoming();
480 statCounter
.select_time
+= (current_dtime
- start
);
483 } while (timeout
> current_dtime
);
484 debugs(5, 8, "comm_select: time out: " << squid_curtime
);
486 return Comm::TIMEOUT
;
490 comm_select_dns_incoming(void)
495 if (DnsSocketA
>= 0) {
496 fds
[nfds
] = DnsSocketA
;
500 if (DnsSocketB
>= 0) {
501 fds
[nfds
] = DnsSocketB
;
505 if (statCounter
.comm_dns
.startPolling(nfds
)) {
506 auto n
= comm_check_incoming_select_handlers(nfds
, fds
);
507 statCounter
.comm_dns
.finishPolling(n
, Config
.comm_incoming
.dns
);
512 Comm::SelectLoopInit(void)
516 FD_ZERO(&global_readfds
);
517 FD_ZERO(&global_writefds
);
518 nreadfds
= nwritefds
= 0;
520 Mgr::RegisterAction("comm_select_incoming",
521 "comm_incoming() stats",
522 commIncomingStats
, 0, 1);
526 * examine_select - debug routine.
528 * I spend the day chasing this core dump that occurs when both the client
529 * and the server side of a cache fetch simultaneoulsy abort the
530 * connection. While I haven't really studied the code to figure out how
531 * it happens, the snippet below may prevent the cache from exitting:
533 * Call this from where the select loop fails.
536 examine_select(fd_set
* readfds
, fd_set
* writefds
)
543 AsyncCall::Pointer ch
= nullptr;
547 debugs(5, DBG_CRITICAL
, "examine_select: Examining open file descriptors...");
549 for (fd
= 0; fd
< Squid_MaxFD
; ++fd
) {
552 tv
.tv_sec
= tv
.tv_usec
= 0;
554 if (FD_ISSET(fd
, readfds
))
556 else if (FD_ISSET(fd
, writefds
))
557 FD_SET(fd
, &write_x
);
561 ++ statCounter
.syscalls
.selects
;
564 if (!fstat(fd
, &sb
)) {
565 debugs(5, 5, "FD " << fd
<< " is valid.");
571 debugs(5, DBG_CRITICAL
, "fstat(FD " << fd
<< "): " << xstrerr(xerrno
));
572 debugs(5, DBG_CRITICAL
, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
573 debugs(5, DBG_CRITICAL
, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
] << " called '" << F
->desc
<< "'");
574 debugs(5, DBG_CRITICAL
, "tmout:" << F
->timeoutHandler
<< " read:" << F
->read_handler
<< " write:" << F
->write_handler
);
576 for (ch
= F
->closeHandler
; ch
!= nullptr; ch
= ch
->Next())
577 debugs(5, DBG_CRITICAL
, " close handler: " << ch
);
579 if (F
->closeHandler
!= nullptr) {
580 commCallCloseHandlers(fd
);
581 } else if (F
->timeoutHandler
!= nullptr) {
582 debugs(5, DBG_CRITICAL
, "examine_select: Calling Timeout Handler");
583 ScheduleCallHere(F
->timeoutHandler
);
586 F
->closeHandler
= nullptr;
587 F
->timeoutHandler
= nullptr;
588 F
->read_handler
= nullptr;
589 F
->write_handler
= nullptr;
591 FD_CLR(fd
, writefds
);
598 commIncomingStats(StoreEntry
* sentry
)
600 storeAppendPrintf(sentry
, "Current incoming_udp_interval: %d\n",
601 statCounter
.comm_udp
.interval
>> Comm::Incoming::Factor
);
602 storeAppendPrintf(sentry
, "Current incoming_dns_interval: %d\n",
603 statCounter
.comm_dns
.interval
>> Comm::Incoming::Factor
);
604 storeAppendPrintf(sentry
, "Current incoming_tcp_interval: %d\n",
605 statCounter
.comm_tcp
.interval
>> Comm::Incoming::Factor
);
606 storeAppendPrintf(sentry
, "\n");
607 storeAppendPrintf(sentry
, "Histogram of events per incoming socket type\n");
608 storeAppendPrintf(sentry
, "ICP Messages handled per comm_select_udp_incoming() call:\n");
609 statCounter
.comm_udp
.history
.dump(sentry
, statHistIntDumper
);
610 storeAppendPrintf(sentry
, "DNS Messages handled per comm_select_dns_incoming() call:\n");
611 statCounter
.comm_dns
.history
.dump(sentry
, statHistIntDumper
);
612 storeAppendPrintf(sentry
, "HTTP Messages handled per comm_select_tcp_incoming() call:\n");
613 statCounter
.comm_tcp
.history
.dump(sentry
, statHistIntDumper
);
617 commUpdateReadBits(int fd
, PF
* handler
)
619 if (handler
&& !FD_ISSET(fd
, &global_readfds
)) {
620 FD_SET(fd
, &global_readfds
);
622 } else if (!handler
&& FD_ISSET(fd
, &global_readfds
)) {
623 FD_CLR(fd
, &global_readfds
);
629 commUpdateWriteBits(int fd
, PF
* handler
)
631 if (handler
&& !FD_ISSET(fd
, &global_writefds
)) {
632 FD_SET(fd
, &global_writefds
);
634 } else if (!handler
&& FD_ISSET(fd
, &global_writefds
)) {
635 FD_CLR(fd
, &global_writefds
);
640 /* Called by async-io or diskd to speed up the polling */
642 Comm::QuickPollRequired(void)
647 #endif /* USE_SELECT */