2 * DEBUG: section 05 Socket Functions
3 * AUTHOR: Harvest Derived
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
36 #include "ClientInfo.h"
37 #include "comm/AcceptLimiter.h"
38 #include "comm/comm_internal.h"
39 #include "comm/Connection.h"
40 #include "comm/IoCallback.h"
41 #include "comm/Loops.h"
42 #include "comm/Read.h"
43 #include "comm/TcpAcceptor.h"
44 #include "comm/Write.h"
46 #include "compat/cmsg.h"
47 #include "DescriptorSet.h"
52 #include "icmp/net_db.h"
53 #include "ip/Intercept.h"
54 #include "ip/QosConfig.h"
57 #include "profiler/Profiler.h"
59 #include "SquidConfig.h"
60 #include "StatCounters.h"
61 #include "StoreIOBuffer.h"
65 #include "ssl/support.h"
71 #include <sys/ioctl.h>
73 #ifdef HAVE_NETINET_TCP_H
74 #include <netinet/tcp.h>
81 * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything.
84 static IOCB commHalfClosedReader
;
85 static void comm_init_opened(const Comm::ConnectionPointer
&conn
, tos_t tos
, nfmark_t nfmark
, const char *note
, struct addrinfo
*AI
);
86 static int comm_apply_flags(int new_socket
, Ip::Address
&addr
, int flags
, struct addrinfo
*AI
);
89 CBDATA_CLASS_INIT(CommQuotaQueue
);
91 static void commHandleWriteHelper(void * data
);
96 static DescriptorSet
*TheHalfClosed
= NULL
; /// the set of half-closed FDs
97 static bool WillCheckHalfClosed
= false; /// true if check is scheduled
98 static EVH commHalfClosedCheck
;
99 static void commPlanHalfClosedCheck();
101 static comm_err_t
commBind(int s
, struct addrinfo
&);
102 static void commSetReuseAddr(int);
103 static void commSetNoLinger(int);
105 static void commSetTcpNoDelay(int);
107 static void commSetTcpRcvbuf(int, int);
109 fd_debug_t
*fdd_table
= NULL
;
114 return fd
>= 0 && fd_table
&& fd_table
[fd
].flags
.open
!= 0;
118 * Empty the read buffers
120 * This is a magical routine that empties the read buffers.
121 * Under some platforms (Linux) if a buffer has data in it before
122 * you call close(), the socket will hang and take quite a while
126 comm_empty_os_read_buffers(int fd
)
129 /* prevent those nasty RST packets */
130 char buf
[SQUID_TCP_SO_RCVBUF
];
132 if (fd_table
[fd
].flags
.nonblocking
) {
133 while (FD_READ_METHOD(fd
, buf
, SQUID_TCP_SO_RCVBUF
) > 0) {};
139 * synchronous wrapper around udp socket functions
142 comm_udp_recvfrom(int fd
, void *buf
, size_t len
, int flags
, Ip::Address
&from
)
144 ++ statCounter
.syscalls
.sock
.recvfroms
;
145 debugs(5,8, "comm_udp_recvfrom: FD " << fd
<< " from " << from
);
146 struct addrinfo
*AI
= NULL
;
147 Ip::Address::InitAddrInfo(AI
);
148 int x
= recvfrom(fd
, buf
, len
, flags
, AI
->ai_addr
, &AI
->ai_addrlen
);
150 Ip::Address::FreeAddrInfo(AI
);
155 comm_udp_recv(int fd
, void *buf
, size_t len
, int flags
)
158 return comm_udp_recvfrom(fd
, buf
, len
, flags
, nul
);
162 comm_udp_send(int s
, const void *buf
, size_t len
, int flags
)
164 return send(s
, buf
, len
, flags
);
168 comm_has_incomplete_write(int fd
)
170 assert(isOpen(fd
) && COMMIO_FD_WRITECB(fd
));
171 return COMMIO_FD_WRITECB(fd
)->active();
175 * Queue a write. handler/handler_data are called when the write fully
176 * completes, on error, or on file descriptor close.
179 /* Return the local port associated with fd. */
181 comm_local_port(int fd
)
184 struct addrinfo
*addr
= NULL
;
185 fde
*F
= &fd_table
[fd
];
187 /* If the fd is closed already, just return */
189 if (!F
->flags
.open
) {
190 debugs(5, 0, "comm_local_port: FD " << fd
<< " has been closed.");
194 if (F
->local_addr
.port())
195 return F
->local_addr
.port();
197 if (F
->sock_family
== AF_INET
)
200 Ip::Address::InitAddrInfo(addr
);
202 if (getsockname(fd
, addr
->ai_addr
, &(addr
->ai_addrlen
)) ) {
203 debugs(50, DBG_IMPORTANT
, "comm_local_port: Failed to retrieve TCP/UDP port number for socket: FD " << fd
<< ": " << xstrerror());
204 Ip::Address::FreeAddrInfo(addr
);
209 Ip::Address::FreeAddrInfo(addr
);
211 if (F
->local_addr
.isAnyAddr()) {
212 /* save the whole local address, not just the port. */
213 F
->local_addr
= temp
;
215 F
->local_addr
.port(temp
.port());
218 debugs(5, 6, "comm_local_port: FD " << fd
<< ": port " << F
->local_addr
.port() << "(family=" << F
->sock_family
<< ")");
219 return F
->local_addr
.port();
223 commBind(int s
, struct addrinfo
&inaddr
)
225 ++ statCounter
.syscalls
.sock
.binds
;
227 if (bind(s
, inaddr
.ai_addr
, inaddr
.ai_addrlen
) == 0) {
228 debugs(50, 6, "commBind: bind socket FD " << s
<< " to " << fd_table
[s
].local_addr
);
232 debugs(50, 0, "commBind: Cannot bind socket FD " << s
<< " to " << fd_table
[s
].local_addr
<< ": " << xstrerror());
238 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
239 * is OR of flags specified in comm.h. Defaults TOS
242 comm_open(int sock_type
,
248 return comm_openex(sock_type
, proto
, addr
, flags
, 0, 0, note
);
252 comm_open_listener(int sock_type
,
254 Comm::ConnectionPointer
&conn
,
257 /* all listener sockets require bind() */
258 conn
->flags
|= COMM_DOBIND
;
260 /* attempt native enabled port. */
261 conn
->fd
= comm_openex(sock_type
, proto
, conn
->local
, conn
->flags
, 0, 0, note
);
265 comm_open_listener(int sock_type
,
273 /* all listener sockets require bind() */
274 flags
|= COMM_DOBIND
;
276 /* attempt native enabled port. */
277 sock
= comm_openex(sock_type
, proto
, addr
, flags
, 0, 0, note
);
283 limitError(int const anErrno
)
285 return anErrno
== ENFILE
|| anErrno
== EMFILE
;
289 comm_set_v6only(int fd
, int tos
)
292 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, (char *) &tos
, sizeof(int)) < 0) {
293 debugs(50, DBG_IMPORTANT
, "comm_open: setsockopt(IPV6_V6ONLY) " << (tos
?"ON":"OFF") << " for FD " << fd
<< ": " << xstrerror());
296 debugs(50, 0, "WARNING: comm_open: setsockopt(IPV6_V6ONLY) not supported on this platform");
301 * Set the socket option required for TPROXY spoofing for:
302 * - Linux TPROXY v4 support,
303 * - OpenBSD divert-to support,
304 * - FreeBSD IPFW TPROXY v4 support.
307 comm_set_transparent(int fd
)
309 #if _SQUID_LINUX_ && defined(IP_TRANSPARENT) // Linux
310 # define soLevel SOL_IP
311 # define soFlag IP_TRANSPARENT
312 bool doneSuid
= false;
314 #elif defined(SO_BINDANY) // OpenBSD 4.7+ and NetBSD with PF
315 # define soLevel SOL_SOCKET
316 # define soFlag SO_BINDANY
318 bool doneSuid
= true;
320 #elif defined(IP_BINDANY) // FreeBSD with IPFW
321 # define soLevel IPPROTO_IP
322 # define soFlag IP_BINDANY
324 bool doneSuid
= true;
327 debugs(50, DBG_CRITICAL
, "WARNING: comm_open: setsockopt(TPROXY) not supported on this platform");
330 #if defined(soLevel) && defined(soFlag)
332 if (setsockopt(fd
, soLevel
, soFlag
, (char *) &tos
, sizeof(int)) < 0) {
333 debugs(50, DBG_IMPORTANT
, "comm_open: setsockopt(TPROXY) on FD " << fd
<< ": " << xstrerror());
335 /* mark the socket as having transparent options */
336 fd_table
[fd
].flags
.transparent
= true;
344 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
345 * is OR of flags specified in defines.h:COMM_*
348 comm_openex(int sock_type
,
357 struct addrinfo
*AI
= NULL
;
359 PROF_start(comm_open
);
360 /* Create socket for accepting new connections. */
361 ++ statCounter
.syscalls
.sock
.sockets
;
363 /* Setup the socket addrinfo details for use */
364 addr
.getAddrInfo(AI
);
365 AI
->ai_socktype
= sock_type
;
366 AI
->ai_protocol
= proto
;
368 debugs(50, 3, "comm_openex: Attempt open socket for: " << addr
);
370 new_socket
= socket(AI
->ai_family
, AI
->ai_socktype
, AI
->ai_protocol
);
372 /* under IPv6 there is the possibility IPv6 is present but disabled. */
373 /* try again as IPv4-native if possible */
374 if ( new_socket
< 0 && Ip::EnableIpv6
&& addr
.isIPv6() && addr
.setIPv4() ) {
375 /* attempt to open this IPv4-only. */
376 Ip::Address::FreeAddrInfo(AI
);
377 /* Setup the socket addrinfo details for use */
378 addr
.getAddrInfo(AI
);
379 AI
->ai_socktype
= sock_type
;
380 AI
->ai_protocol
= proto
;
381 debugs(50, 3, "comm_openex: Attempt fallback open socket for: " << addr
);
382 new_socket
= socket(AI
->ai_family
, AI
->ai_socktype
, AI
->ai_protocol
);
383 debugs(50, 2, HERE
<< "attempt open " << note
<< " socket on: " << addr
);
386 if (new_socket
< 0) {
387 /* Increase the number of reserved fd's if calls to socket()
388 * are failing because the open file table is full. This
389 * limits the number of simultaneous clients */
391 if (limitError(errno
)) {
392 debugs(50, DBG_IMPORTANT
, "comm_open: socket failure: " << xstrerror());
395 debugs(50, DBG_CRITICAL
, "comm_open: socket failure: " << xstrerror());
398 Ip::Address::FreeAddrInfo(AI
);
400 PROF_stop(comm_open
);
404 // XXX: temporary for the transition. comm_openex will eventually have a conn to play with.
405 Comm::ConnectionPointer conn
= new Comm::Connection
;
407 conn
->fd
= new_socket
;
409 debugs(50, 3, "comm_openex: Opened socket " << conn
<< " : family=" << AI
->ai_family
<< ", type=" << AI
->ai_socktype
<< ", protocol=" << AI
->ai_protocol
);
411 /* set TOS if needed */
413 Ip::Qos::setSockTos(conn
, tos
);
415 /* set netfilter mark if needed */
417 Ip::Qos::setSockNfmark(conn
, nfmark
);
419 if ( Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& addr
.isIPv6() )
420 comm_set_v6only(conn
->fd
, 1);
422 /* Windows Vista supports Dual-Sockets. BUT defaults them to V6ONLY. Turn it OFF. */
423 /* Other OS may have this administratively disabled for general use. Same deal. */
424 if ( Ip::EnableIpv6
&IPV6_SPECIAL_V4MAPPING
&& addr
.isIPv6() )
425 comm_set_v6only(conn
->fd
, 0);
427 comm_init_opened(conn
, tos
, nfmark
, note
, AI
);
428 new_socket
= comm_apply_flags(conn
->fd
, addr
, flags
, AI
);
430 Ip::Address::FreeAddrInfo(AI
);
432 PROF_stop(comm_open
);
434 // XXX transition only. prevent conn from closing the new FD on function exit.
439 /// update FD tables after a local or remote (IPC) comm_openex();
441 comm_init_opened(const Comm::ConnectionPointer
&conn
,
447 assert(Comm::IsConnOpen(conn
));
451 debugs(5, 5, HERE
<< conn
<< " is a new socket");
453 assert(!isOpen(conn
->fd
)); // NP: global isOpen checks the fde entry for openness not the Comm::Connection
454 fd_open(conn
->fd
, FD_SOCKET
, note
);
456 fdd_table
[conn
->fd
].close_file
= NULL
;
457 fdd_table
[conn
->fd
].close_line
= 0;
459 fde
*F
= &fd_table
[conn
->fd
];
460 F
->local_addr
= conn
->local
;
461 F
->tosToServer
= tos
;
463 F
->nfmarkToServer
= nfmark
;
465 F
->sock_family
= AI
->ai_family
;
468 /// apply flags after a local comm_open*() call;
469 /// returns new_socket or -1 on error
471 comm_apply_flags(int new_socket
,
476 assert(new_socket
>= 0);
478 const int sock_type
= AI
->ai_socktype
;
480 if (!(flags
& COMM_NOCLOEXEC
))
481 commSetCloseOnExec(new_socket
);
483 if ((flags
& COMM_REUSEADDR
))
484 commSetReuseAddr(new_socket
);
486 if (addr
.port() > (unsigned short) 0) {
488 if (sock_type
!= SOCK_DGRAM
)
490 commSetNoLinger(new_socket
);
493 commSetReuseAddr(new_socket
);
496 /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */
497 if ((flags
& COMM_TRANSPARENT
)) {
498 comm_set_transparent(new_socket
);
501 if ( (flags
& COMM_DOBIND
) || addr
.port() > 0 || !addr
.isAnyAddr() ) {
502 if ( !(flags
& COMM_DOBIND
) && addr
.isAnyAddr() )
503 debugs(5, DBG_IMPORTANT
,"WARNING: Squid is attempting to bind() port " << addr
<< " without being a listener.");
504 if ( addr
.isNoAddr() )
505 debugs(5,0,"CRITICAL: Squid is attempting to bind() port " << addr
<< "!!");
507 if (commBind(new_socket
, *AI
) != COMM_OK
) {
508 comm_close(new_socket
);
513 if (flags
& COMM_NONBLOCKING
)
514 if (commSetNonBlocking(new_socket
) == COMM_ERROR
) {
515 comm_close(new_socket
);
520 if (sock_type
== SOCK_STREAM
)
521 commSetTcpNoDelay(new_socket
);
525 if (Config
.tcpRcvBufsz
> 0 && sock_type
== SOCK_STREAM
)
526 commSetTcpRcvbuf(new_socket
, Config
.tcpRcvBufsz
);
532 comm_import_opened(const Comm::ConnectionPointer
&conn
,
536 debugs(5, 2, HERE
<< conn
);
537 assert(Comm::IsConnOpen(conn
));
540 comm_init_opened(conn
, 0, 0, note
, AI
);
542 if (!(conn
->flags
& COMM_NOCLOEXEC
))
543 fd_table
[conn
->fd
].flags
.close_on_exec
= true;
545 if (conn
->local
.port() > (unsigned short) 0) {
547 if (AI
->ai_socktype
!= SOCK_DGRAM
)
549 fd_table
[conn
->fd
].flags
.nolinger
= true;
552 if ((conn
->flags
& COMM_TRANSPARENT
))
553 fd_table
[conn
->fd
].flags
.transparent
= true;
555 if (conn
->flags
& COMM_NONBLOCKING
)
556 fd_table
[conn
->fd
].flags
.nonblocking
= true;
559 if (AI
->ai_socktype
== SOCK_STREAM
)
560 fd_table
[conn
->fd
].flags
.nodelay
= true;
563 /* no fd_table[fd].flags. updates needed for these conditions:
564 * if ((flags & COMM_REUSEADDR)) ...
565 * if ((flags & COMM_DOBIND) ...) ...
569 // XXX: now that raw-FD timeouts are only unset for pipes and files this SHOULD be a no-op.
570 // With handler already unset. Leaving this present until that can be verified for all code paths.
572 commUnsetFdTimeout(int fd
)
574 debugs(5, 3, HERE
<< "Remove timeout for FD " << fd
);
576 assert(fd
< Squid_MaxFD
);
577 fde
*F
= &fd_table
[fd
];
578 assert(F
->flags
.open
);
580 F
->timeoutHandler
= NULL
;
585 commSetConnTimeout(const Comm::ConnectionPointer
&conn
, int timeout
, AsyncCall::Pointer
&callback
)
587 debugs(5, 3, HERE
<< conn
<< " timeout " << timeout
);
588 assert(Comm::IsConnOpen(conn
));
589 assert(conn
->fd
< Squid_MaxFD
);
590 fde
*F
= &fd_table
[conn
->fd
];
591 assert(F
->flags
.open
);
594 F
->timeoutHandler
= NULL
;
597 if (callback
!= NULL
) {
598 typedef CommTimeoutCbParams Params
;
599 Params
¶ms
= GetCommParams
<Params
>(callback
);
601 F
->timeoutHandler
= callback
;
604 F
->timeout
= squid_curtime
+ (time_t) timeout
;
611 commUnsetConnTimeout(const Comm::ConnectionPointer
&conn
)
613 debugs(5, 3, HERE
<< "Remove timeout for " << conn
);
614 AsyncCall::Pointer nil
;
615 return commSetConnTimeout(conn
, -1, nil
);
619 comm_connect_addr(int sock
, const Ip::Address
&address
)
621 comm_err_t status
= COMM_OK
;
622 fde
*F
= &fd_table
[sock
];
626 struct addrinfo
*AI
= NULL
;
627 PROF_start(comm_connect_addr
);
629 assert(address
.port() != 0);
631 debugs(5, 9, HERE
<< "connecting socket FD " << sock
<< " to " << address
<< " (want family: " << F
->sock_family
<< ")");
633 /* Handle IPv6 over IPv4-only socket case.
634 * this case must presently be handled here since the getAddrInfo asserts on bad mappings.
635 * NP: because commResetFD is private to ConnStateData we have to return an error and
636 * trust its handled properly.
638 if (F
->sock_family
== AF_INET
&& !address
.isIPv4()) {
640 return COMM_ERR_PROTOCOL
;
643 /* Handle IPv4 over IPv6-only socket case.
644 * This case is presently handled here as it's both a known case and it's
645 * uncertain what error will be returned by the IPv6 stack in such case. It's
646 * possible this will also be handled by the errno checks below after connect()
647 * but needs carefull cross-platform verification, and verifying the address
648 * condition here is simple.
650 if (!F
->local_addr
.isIPv4() && address
.isIPv4()) {
652 return COMM_ERR_PROTOCOL
;
655 address
.getAddrInfo(AI
, F
->sock_family
);
657 /* Establish connection. */
660 if (!F
->flags
.called_connect
) {
661 F
->flags
.called_connect
= true;
662 ++ statCounter
.syscalls
.sock
.connects
;
664 x
= connect(sock
, AI
->ai_addr
, AI
->ai_addrlen
);
666 // XXX: ICAP code refuses callbacks during a pending comm_ call
667 // Async calls development will fix this.
674 debugs(5,5, "comm_connect_addr: sock=" << sock
<< ", addrinfo( " <<
675 " flags=" << AI
->ai_flags
<<
676 ", family=" << AI
->ai_family
<<
677 ", socktype=" << AI
->ai_socktype
<<
678 ", protocol=" << AI
->ai_protocol
<<
679 ", &addr=" << AI
->ai_addr
<<
680 ", addrlen=" << AI
->ai_addrlen
<<
682 debugs(5, 9, "connect FD " << sock
<< ": (" << x
<< ") " << xstrerror());
683 debugs(14,9, "connecting to: " << address
);
687 /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */
689 connect(sock
, AI
->ai_addr
, AI
->ai_addrlen
);
691 if (errno
== EINVAL
) {
692 errlen
= sizeof(err
);
693 x
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, &err
, &errlen
);
700 errlen
= sizeof(err
);
702 x
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, &err
, &errlen
);
709 * Solaris 2.4's socket emulation doesn't allow you
710 * to determine the error from a failed non-blocking
711 * connect and just returns EPIPE. Create a fake
712 * error message for connect. -- fenner@parc.xerox.com
714 if (x
< 0 && errno
== EPIPE
)
722 Ip::Address::FreeAddrInfo(AI
);
724 PROF_stop(comm_connect_addr
);
726 if (errno
== 0 || errno
== EISCONN
)
728 else if (ignoreErrno(errno
))
729 status
= COMM_INPROGRESS
;
730 else if (errno
== EAFNOSUPPORT
|| errno
== EINVAL
)
731 return COMM_ERR_PROTOCOL
;
735 address
.toStr(F
->ipaddr
, MAX_IPSTRLEN
);
737 F
->remote_port
= address
.port(); /* remote_port is HS */
739 if (status
== COMM_OK
) {
740 debugs(5, DBG_DATA
, "comm_connect_addr: FD " << sock
<< " connected to " << address
);
741 } else if (status
== COMM_INPROGRESS
) {
742 debugs(5, DBG_DATA
, "comm_connect_addr: FD " << sock
<< " connection pending");
749 commCallCloseHandlers(int fd
)
751 fde
*F
= &fd_table
[fd
];
752 debugs(5, 5, "commCallCloseHandlers: FD " << fd
);
754 while (F
->closeHandler
!= NULL
) {
755 AsyncCall::Pointer call
= F
->closeHandler
;
756 F
->closeHandler
= call
->Next();
758 // If call is not canceled schedule it for execution else ignore it
759 if (!call
->canceled()) {
760 debugs(5, 5, "commCallCloseHandlers: ch->handler=" << call
);
761 ScheduleCallHere(call
);
768 commLingerClose(int fd
, void *unused
)
770 LOCAL_ARRAY(char, buf
, 1024);
772 n
= FD_READ_METHOD(fd
, buf
, 1024);
775 debugs(5, 3, "commLingerClose: FD " << fd
<< " read: " << xstrerror());
781 commLingerTimeout(const FdeCbParams
¶ms
)
783 debugs(5, 3, "commLingerTimeout: FD " << params
.fd
);
784 comm_close(params
.fd
);
791 comm_lingering_close(int fd
)
794 if (fd_table
[fd
].ssl
)
795 ssl_shutdown_method(fd_table
[fd
].ssl
);
798 if (shutdown(fd
, 1) < 0) {
803 fd_note(fd
, "lingering close");
804 AsyncCall::Pointer call
= commCbCall(5,4, "commLingerTimeout", FdeCbPtrFun(commLingerTimeout
, NULL
));
806 debugs(5, 3, HERE
<< "FD " << fd
<< " timeout " << timeout
);
807 assert(fd_table
[fd
].flags
.open
);
808 if (callback
!= NULL
) {
809 typedef FdeCbParams Params
;
810 Params
¶ms
= GetCommParams
<Params
>(callback
);
812 fd_table
[fd
].timeoutHandler
= callback
;
813 fd_table
[fd
].timeout
= squid_curtime
+ static_cast<time_t>(10);
816 Comm::SetSelect(fd
, COMM_SELECT_READ
, commLingerClose
, NULL
, 0);
822 * enable linger with time of 0 so that when the socket is
823 * closed, TCP generates a RESET
826 comm_reset_close(const Comm::ConnectionPointer
&conn
)
832 if (setsockopt(conn
->fd
, SOL_SOCKET
, SO_LINGER
, (char *) &L
, sizeof(L
)) < 0)
833 debugs(50, DBG_CRITICAL
, "ERROR: Closing " << conn
<< " with TCP RST: " << xstrerror());
838 // Legacy close function.
840 old_comm_reset_close(int fd
)
846 if (setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, (char *) &L
, sizeof(L
)) < 0)
847 debugs(50, DBG_CRITICAL
, "ERROR: Closing FD " << fd
<< " with TCP RST: " << xstrerror());
854 commStartSslClose(const FdeCbParams
¶ms
)
856 assert(&fd_table
[params
.fd
].ssl
);
857 ssl_shutdown_method(fd_table
[params
.fd
].ssl
);
862 comm_close_complete(const FdeCbParams
¶ms
)
865 fde
*F
= &fd_table
[params
.fd
];
872 if (F
->dynamicSslContext
) {
873 SSL_CTX_free(F
->dynamicSslContext
);
874 F
->dynamicSslContext
= NULL
;
877 fd_close(params
.fd
); /* update fdstat */
880 ++ statCounter
.syscalls
.sock
.closes
;
882 /* When one connection closes, give accept() a chance, if need be */
883 Comm::AcceptLimiter::Instance().kick();
887 * Close the socket fd.
889 * + call write handlers with ERR_CLOSING
890 * + call read handlers with ERR_CLOSING
891 * + call closing handlers
893 * NOTE: COMM_ERR_CLOSING will NOT be called for CommReads' sitting in a
894 * DeferredReadManager.
897 _comm_close(int fd
, char const *file
, int line
)
899 debugs(5, 3, "comm_close: start closing FD " << fd
);
901 assert(fd
< Squid_MaxFD
);
903 fde
*F
= &fd_table
[fd
];
904 fdd_table
[fd
].close_file
= file
;
905 fdd_table
[fd
].close_line
= line
;
910 /* XXX: is this obsolete behind F->closing() ? */
911 if ( (shutting_down
|| reconfiguring
) && (!F
->flags
.open
|| F
->type
== FD_FILE
))
914 /* The following fails because ipc.c is doing calls to pipe() to create sockets! */
916 debugs(50, DBG_IMPORTANT
, HERE
<< "BUG 3556: FD " << fd
<< " is not an open socket.");
917 // XXX: do we need to run close(fd) or fd_close(fd) here?
921 assert(F
->type
!= FD_FILE
);
923 PROF_start(comm_close
);
925 F
->flags
.close_request
= true;
929 AsyncCall::Pointer startCall
=commCbCall(5,4, "commStartSslClose",
930 FdeCbPtrFun(commStartSslClose
, NULL
));
931 FdeCbParams
&startParams
= GetCommParams
<FdeCbParams
>(startCall
);
933 ScheduleCallHere(startCall
);
937 // a half-closed fd may lack a reader, so we stop monitoring explicitly
938 if (commHasHalfClosedMonitor(fd
))
939 commStopHalfClosedMonitor(fd
);
940 commUnsetFdTimeout(fd
);
942 // notify read/write handlers after canceling select reservations, if any
943 if (COMMIO_FD_WRITECB(fd
)->active()) {
944 Comm::SetSelect(fd
, COMM_SELECT_WRITE
, NULL
, NULL
, 0);
945 COMMIO_FD_WRITECB(fd
)->finish(COMM_ERR_CLOSING
, errno
);
947 if (COMMIO_FD_READCB(fd
)->active()) {
948 Comm::SetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
949 COMMIO_FD_READCB(fd
)->finish(COMM_ERR_CLOSING
, errno
);
953 if (ClientInfo
*clientInfo
= F
->clientInfo
) {
954 if (clientInfo
->selectWaiting
) {
955 clientInfo
->selectWaiting
= false;
956 // kick queue or it will get stuck as commWriteHandle is not called
957 clientInfo
->kickQuotaQueue();
962 commCallCloseHandlers(fd
);
964 comm_empty_os_read_buffers(fd
);
966 AsyncCall::Pointer completeCall
=commCbCall(5,4, "comm_close_complete",
967 FdeCbPtrFun(comm_close_complete
, NULL
));
968 FdeCbParams
&completeParams
= GetCommParams
<FdeCbParams
>(completeCall
);
969 completeParams
.fd
= fd
;
970 // must use async call to wait for all callbacks
971 // scheduled before comm_close() to finish
972 ScheduleCallHere(completeCall
);
974 PROF_stop(comm_close
);
977 /* Send a udp datagram to specified TO_ADDR. */
979 comm_udp_sendto(int fd
,
980 const Ip::Address
&to_addr
,
984 PROF_start(comm_udp_sendto
);
985 ++ statCounter
.syscalls
.sock
.sendtos
;
987 debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr
<<
988 " using FD " << fd
<< " using Port " << comm_local_port(fd
) );
990 struct addrinfo
*AI
= NULL
;
991 to_addr
.getAddrInfo(AI
, fd_table
[fd
].sock_family
);
992 int x
= sendto(fd
, buf
, len
, 0, AI
->ai_addr
, AI
->ai_addrlen
);
993 Ip::Address::FreeAddrInfo(AI
);
995 PROF_stop(comm_udp_sendto
);
1002 if (ECONNREFUSED
!= errno
)
1005 debugs(50, DBG_IMPORTANT
, "comm_udp_sendto: FD " << fd
<< ", (family=" << fd_table
[fd
].sock_family
<< ") " << to_addr
<< ": " << xstrerror());
1011 comm_add_close_handler(int fd
, CLCB
* handler
, void *data
)
1013 debugs(5, 5, "comm_add_close_handler: FD " << fd
<< ", handler=" <<
1014 handler
<< ", data=" << data
);
1016 AsyncCall::Pointer call
=commCbCall(5,4, "SomeCloseHandler",
1017 CommCloseCbPtrFun(handler
, data
));
1018 comm_add_close_handler(fd
, call
);
1022 comm_add_close_handler(int fd
, AsyncCall::Pointer
&call
)
1024 debugs(5, 5, "comm_add_close_handler: FD " << fd
<< ", AsyncCall=" << call
);
1026 /*TODO:Check for a similar scheduled AsyncCall*/
1027 // for (c = fd_table[fd].closeHandler; c; c = c->next)
1028 // assert(c->handler != handler || c->data != data);
1030 call
->setNext(fd_table
[fd
].closeHandler
);
1032 fd_table
[fd
].closeHandler
= call
;
1035 // remove function-based close handler
1037 comm_remove_close_handler(int fd
, CLCB
* handler
, void *data
)
1040 /* Find handler in list */
1041 debugs(5, 5, "comm_remove_close_handler: FD " << fd
<< ", handler=" <<
1042 handler
<< ", data=" << data
);
1044 AsyncCall::Pointer p
, prev
= NULL
;
1045 for (p
= fd_table
[fd
].closeHandler
; p
!= NULL
; prev
= p
, p
= p
->Next()) {
1046 typedef CommCbFunPtrCallT
<CommCloseCbPtrFun
> Call
;
1047 const Call
*call
= dynamic_cast<const Call
*>(p
.getRaw());
1048 if (!call
) // method callbacks have their own comm_remove_close_handler
1051 typedef CommCloseCbParams Params
;
1052 const Params
¶ms
= GetCommParams
<Params
>(p
);
1053 if (call
->dialer
.handler
== handler
&& params
.data
== data
)
1054 break; /* This is our handler */
1057 // comm_close removes all close handlers so our handler may be gone
1059 p
->dequeue(fd_table
[fd
].closeHandler
, prev
);
1060 p
->cancel("comm_remove_close_handler");
1064 // remove method-based close handler
1066 comm_remove_close_handler(int fd
, AsyncCall::Pointer
&call
)
1069 debugs(5, 5, "comm_remove_close_handler: FD " << fd
<< ", AsyncCall=" << call
);
1071 // comm_close removes all close handlers so our handler may be gone
1072 AsyncCall::Pointer p
, prev
= NULL
;
1073 for (p
= fd_table
[fd
].closeHandler
; p
!= NULL
&& p
!= call
; prev
= p
, p
= p
->Next());
1076 p
->dequeue(fd_table
[fd
].closeHandler
, prev
);
1077 call
->cancel("comm_remove_close_handler");
1081 commSetNoLinger(int fd
)
1085 L
.l_onoff
= 0; /* off */
1088 if (setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, (char *) &L
, sizeof(L
)) < 0)
1089 debugs(50, 0, "commSetNoLinger: FD " << fd
<< ": " << xstrerror());
1091 fd_table
[fd
].flags
.nolinger
= true;
1095 commSetReuseAddr(int fd
)
1099 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &on
, sizeof(on
)) < 0)
1100 debugs(50, DBG_IMPORTANT
, "commSetReuseAddr: FD " << fd
<< ": " << xstrerror());
1104 commSetTcpRcvbuf(int fd
, int size
)
1106 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, (char *) &size
, sizeof(size
)) < 0)
1107 debugs(50, DBG_IMPORTANT
, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1108 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, (char *) &size
, sizeof(size
)) < 0)
1109 debugs(50, DBG_IMPORTANT
, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1110 #ifdef TCP_WINDOW_CLAMP
1111 if (setsockopt(fd
, SOL_TCP
, TCP_WINDOW_CLAMP
, (char *) &size
, sizeof(size
)) < 0)
1112 debugs(50, DBG_IMPORTANT
, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1117 commSetNonBlocking(int fd
)
1119 #if !_SQUID_WINDOWS_
1124 int nonblocking
= TRUE
;
1127 if (fd_table
[fd
].type
!= FD_PIPE
) {
1130 if (ioctl(fd
, FIONBIO
, &nonblocking
) < 0) {
1131 debugs(50, 0, "commSetNonBlocking: FD " << fd
<< ": " << xstrerror() << " " << fd_table
[fd
].type
);
1139 #if !_SQUID_WINDOWS_
1141 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1142 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1146 if (fcntl(fd
, F_SETFL
, flags
| SQUID_NONBLOCK
) < 0) {
1147 debugs(50, 0, "commSetNonBlocking: FD " << fd
<< ": " << xstrerror());
1155 fd_table
[fd
].flags
.nonblocking
= true;
1161 commUnsetNonBlocking(int fd
)
1164 int nonblocking
= FALSE
;
1166 if (ioctlsocket(fd
, FIONBIO
, (unsigned long *) &nonblocking
) < 0) {
1171 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1172 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1176 if (fcntl(fd
, F_SETFL
, flags
& (~SQUID_NONBLOCK
)) < 0) {
1178 debugs(50, 0, "commUnsetNonBlocking: FD " << fd
<< ": " << xstrerror());
1182 fd_table
[fd
].flags
.nonblocking
= false;
1187 commSetCloseOnExec(int fd
)
1193 if ((flags
= fcntl(fd
, F_GETFD
, dummy
)) < 0) {
1194 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFD: " << xstrerror());
1198 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) < 0)
1199 debugs(50, 0, "FD " << fd
<< ": set close-on-exec failed: " << xstrerror());
1201 fd_table
[fd
].flags
.close_on_exec
= true;
1208 commSetTcpNoDelay(int fd
)
1212 if (setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *) &on
, sizeof(on
)) < 0)
1213 debugs(50, DBG_IMPORTANT
, "commSetTcpNoDelay: FD " << fd
<< ": " << xstrerror());
1215 fd_table
[fd
].flags
.nodelay
= true;
1221 commSetTcpKeepalive(int fd
, int idle
, int interval
, int timeout
)
1225 if (timeout
&& interval
) {
1226 int count
= (timeout
+ interval
- 1) / interval
;
1227 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPCNT
, &count
, sizeof(on
)) < 0)
1228 debugs(5, DBG_IMPORTANT
, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1233 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, &idle
, sizeof(on
)) < 0)
1234 debugs(5, DBG_IMPORTANT
, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1237 #ifdef TCP_KEEPINTVL
1239 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, &interval
, sizeof(on
)) < 0)
1240 debugs(5, DBG_IMPORTANT
, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1243 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &on
, sizeof(on
)) < 0)
1244 debugs(5, DBG_IMPORTANT
, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1250 fd_table
=(fde
*) xcalloc(Squid_MaxFD
, sizeof(fde
));
1251 fdd_table
= (fd_debug_t
*)xcalloc(Squid_MaxFD
, sizeof(fd_debug_t
));
1253 /* make sure the accept() socket FIFO delay queue exists */
1254 Comm::AcceptLimiter::Instance();
1256 // make sure the IO pending callback table exists
1257 Comm::CallbackTableInit();
1259 /* XXX account fd_table */
1260 /* Keep a few file descriptors free so that we don't run out of FD's
1261 * after accepting a client but before it opens a socket or a file.
1262 * Since Squid_MaxFD can be as high as several thousand, don't waste them */
1263 RESERVED_FD
= min(100, Squid_MaxFD
/ 4);
1265 TheHalfClosed
= new DescriptorSet
;
1267 /* setup the select loop module */
1268 Comm::SelectLoopInit();
1274 delete TheHalfClosed
;
1275 TheHalfClosed
= NULL
;
1277 safe_free(fd_table
);
1278 safe_free(fdd_table
);
1279 Comm::CallbackTableDestruct();
1283 // called when the queue is done waiting for the client bucket to fill
1285 commHandleWriteHelper(void * data
)
1287 CommQuotaQueue
*queue
= static_cast<CommQuotaQueue
*>(data
);
1290 ClientInfo
*clientInfo
= queue
->clientInfo
;
1291 // ClientInfo invalidates queue if freed, so if we got here through,
1292 // evenAdd cbdata protections, everything should be valid and consistent
1294 assert(clientInfo
->hasQueue());
1295 assert(clientInfo
->hasQueue(queue
));
1296 assert(!clientInfo
->selectWaiting
);
1297 assert(clientInfo
->eventWaiting
);
1298 clientInfo
->eventWaiting
= false;
1301 // check that the head descriptor is still relevant
1302 const int head
= clientInfo
->quotaPeekFd();
1303 Comm::IoCallback
*ccb
= COMMIO_FD_WRITECB(head
);
1305 if (fd_table
[head
].clientInfo
== clientInfo
&&
1306 clientInfo
->quotaPeekReserv() == ccb
->quotaQueueReserv
&&
1307 !fd_table
[head
].closing()) {
1309 // wait for the head descriptor to become ready for writing
1310 Comm::SetSelect(head
, COMM_SELECT_WRITE
, Comm::HandleWrite
, ccb
, 0);
1311 clientInfo
->selectWaiting
= true;
1315 clientInfo
->quotaDequeue(); // remove the no longer relevant descriptor
1316 // and continue looking for a relevant one
1317 } while (clientInfo
->hasQueue());
1319 debugs(77,3, HERE
<< "emptied queue");
1323 ClientInfo::hasQueue() const
1326 return !quotaQueue
->empty();
1330 ClientInfo::hasQueue(const CommQuotaQueue
*q
) const
1333 return quotaQueue
== q
;
1336 /// returns the first descriptor to be dequeued
1338 ClientInfo::quotaPeekFd() const
1341 return quotaQueue
->front();
1344 /// returns the reservation ID of the first descriptor to be dequeued
1346 ClientInfo::quotaPeekReserv() const
1349 return quotaQueue
->outs
+ 1;
1352 /// queues a given fd, creating the queue if necessary; returns reservation ID
1354 ClientInfo::quotaEnqueue(int fd
)
1357 return quotaQueue
->enqueue(fd
);
1360 /// removes queue head
1362 ClientInfo::quotaDequeue()
1365 quotaQueue
->dequeue();
1369 ClientInfo::kickQuotaQueue()
1371 if (!eventWaiting
&& !selectWaiting
&& hasQueue()) {
1372 // wait at least a second if the bucket is empty
1373 const double delay
= (bucketSize
< 1.0) ? 1.0 : 0.0;
1374 eventAdd("commHandleWriteHelper", &commHandleWriteHelper
,
1375 quotaQueue
, delay
, 0, true);
1376 eventWaiting
= true;
1380 /// calculates how much to write for a single dequeued client
1382 ClientInfo::quotaForDequed()
1384 /* If we have multiple clients and give full bucketSize to each client then
1385 * clt1 may often get a lot more because clt1->clt2 time distance in the
1386 * select(2) callback order may be a lot smaller than cltN->clt1 distance.
1387 * We divide quota evenly to be more fair. */
1389 if (!rationedCount
) {
1390 rationedCount
= quotaQueue
->size() + 1;
1392 // The delay in ration recalculation _temporary_ deprives clients from
1393 // bytes that should have trickled in while rationedCount was positive.
1396 // Rounding errors do not accumulate here, but we round down to avoid
1397 // negative bucket sizes after write with rationedCount=1.
1398 rationedQuota
= static_cast<int>(floor(bucketSize
/rationedCount
));
1399 debugs(77,5, HERE
<< "new rationedQuota: " << rationedQuota
<<
1400 '*' << rationedCount
);
1404 debugs(77,7, HERE
<< "rationedQuota: " << rationedQuota
<<
1405 " rations remaining: " << rationedCount
);
1407 // update 'last seen' time to prevent clientdb GC from dropping us
1408 last_seen
= squid_curtime
;
1409 return rationedQuota
;
1412 ///< adds bytes to the quota bucket based on the rate and passed time
1414 ClientInfo::refillBucket()
1416 // all these times are in seconds, with double precision
1417 const double currTime
= current_dtime
;
1418 const double timePassed
= currTime
- prevTime
;
1420 // Calculate allowance for the time passed. Use double to avoid
1421 // accumulating rounding errors for small intervals. For example, always
1422 // adding 1 byte instead of 1.4 results in 29% bandwidth allocation error.
1423 const double gain
= timePassed
* writeSpeedLimit
;
1425 debugs(77,5, HERE
<< currTime
<< " clt" << (const char*)hash
.key
<< ": " <<
1426 bucketSize
<< " + (" << timePassed
<< " * " << writeSpeedLimit
<<
1427 " = " << gain
<< ')');
1429 // to further combat error accumulation during micro updates,
1430 // quit before updating time if we cannot add at least one byte
1434 prevTime
= currTime
;
1436 // for "first" connections, drain initial fat before refilling but keep
1437 // updating prevTime to avoid bursts after the fat is gone
1438 if (bucketSize
> bucketSizeLimit
) {
1439 debugs(77,4, HERE
<< "not refilling while draining initial fat");
1445 // obey quota limits
1446 if (bucketSize
> bucketSizeLimit
)
1447 bucketSize
= bucketSizeLimit
;
1451 ClientInfo::setWriteLimiter(const int aWriteSpeedLimit
, const double anInitialBurst
, const double aHighWatermark
)
1453 debugs(77,5, HERE
<< "Write limits for " << (const char*)hash
.key
<<
1454 " speed=" << aWriteSpeedLimit
<< " burst=" << anInitialBurst
<<
1455 " highwatermark=" << aHighWatermark
);
1457 // set or possibly update traffic shaping parameters
1458 writeLimitingActive
= true;
1459 writeSpeedLimit
= aWriteSpeedLimit
;
1460 bucketSizeLimit
= aHighWatermark
;
1462 // but some members should only be set once for a newly activated bucket
1463 if (firstTimeConnection
) {
1464 firstTimeConnection
= false;
1466 assert(!selectWaiting
);
1467 assert(!quotaQueue
);
1468 quotaQueue
= new CommQuotaQueue(this);
1470 bucketSize
= anInitialBurst
;
1471 prevTime
= current_dtime
;
1475 CommQuotaQueue::CommQuotaQueue(ClientInfo
*info
): clientInfo(info
),
1481 CommQuotaQueue::~CommQuotaQueue()
1483 assert(!clientInfo
); // ClientInfo should clear this before destroying us
1486 /// places the given fd at the end of the queue; returns reservation ID
1488 CommQuotaQueue::enqueue(int fd
)
1490 debugs(77,5, HERE
<< "clt" << (const char*)clientInfo
->hash
.key
<<
1491 ": FD " << fd
<< " with qqid" << (ins
+1) << ' ' << fds
.size());
1496 /// removes queue head
1498 CommQuotaQueue::dequeue()
1500 assert(!fds
.empty());
1501 debugs(77,5, HERE
<< "clt" << (const char*)clientInfo
->hash
.key
<<
1502 ": FD " << fds
.front() << " with qqid" << (outs
+1) << ' ' <<
1510 * hm, this might be too general-purpose for all the places we'd
1514 ignoreErrno(int ierrno
)
1521 #if EAGAIN != EWOULDBLOCK
1544 commCloseAllSockets(void)
1549 for (fd
= 0; fd
<= Biggest_FD
; ++fd
) {
1555 if (F
->type
!= FD_SOCKET
)
1558 if (F
->flags
.ipc
) /* don't close inter-process sockets */
1561 if (F
->timeoutHandler
!= NULL
) {
1562 AsyncCall::Pointer callback
= F
->timeoutHandler
;
1563 F
->timeoutHandler
= NULL
;
1564 debugs(5, 5, "commCloseAllSockets: FD " << fd
<< ": Calling timeout handler");
1565 ScheduleCallHere(callback
);
1567 debugs(5, 5, "commCloseAllSockets: FD " << fd
<< ": calling comm_reset_close()");
1568 old_comm_reset_close(fd
);
1574 AlreadyTimedOut(fde
*F
)
1579 if (F
->timeout
== 0)
1582 if (F
->timeout
> squid_curtime
)
1589 writeTimedOut(int fd
)
1591 if (!COMMIO_FD_WRITECB(fd
)->active())
1594 if ((squid_curtime
- fd_table
[fd
].writeStart
) < Config
.Timeout
.write
)
1605 AsyncCall::Pointer callback
;
1607 for (fd
= 0; fd
<= Biggest_FD
; ++fd
) {
1610 if (writeTimedOut(fd
)) {
1611 // We have an active write callback and we are timed out
1612 debugs(5, 5, "checkTimeouts: FD " << fd
<< " auto write timeout");
1613 Comm::SetSelect(fd
, COMM_SELECT_WRITE
, NULL
, NULL
, 0);
1614 COMMIO_FD_WRITECB(fd
)->finish(COMM_ERROR
, ETIMEDOUT
);
1615 } else if (AlreadyTimedOut(F
))
1618 debugs(5, 5, "checkTimeouts: FD " << fd
<< " Expired");
1620 if (F
->timeoutHandler
!= NULL
) {
1621 debugs(5, 5, "checkTimeouts: FD " << fd
<< ": Call timeout handler");
1622 callback
= F
->timeoutHandler
;
1623 F
->timeoutHandler
= NULL
;
1624 ScheduleCallHere(callback
);
1626 debugs(5, 5, "checkTimeouts: FD " << fd
<< ": Forcing comm_close()");
1632 /// Start waiting for a possibly half-closed connection to close
1633 // by scheduling a read callback to a monitoring handler that
1634 // will close the connection on read errors.
1636 commStartHalfClosedMonitor(int fd
)
1638 debugs(5, 5, HERE
<< "adding FD " << fd
<< " to " << *TheHalfClosed
);
1639 assert(isOpen(fd
) && !commHasHalfClosedMonitor(fd
));
1640 (void)TheHalfClosed
->add(fd
); // could also assert the result
1641 commPlanHalfClosedCheck(); // may schedule check if we added the first FD
1646 commPlanHalfClosedCheck()
1648 if (!WillCheckHalfClosed
&& !TheHalfClosed
->empty()) {
1649 eventAdd("commHalfClosedCheck", &commHalfClosedCheck
, NULL
, 1.0, 1);
1650 WillCheckHalfClosed
= true;
1654 /// iterates over all descriptors that may need half-closed tests and
1655 /// calls comm_read for those that do; re-schedules the check if needed
1658 commHalfClosedCheck(void *)
1660 debugs(5, 5, HERE
<< "checking " << *TheHalfClosed
);
1662 typedef DescriptorSet::const_iterator DSCI
;
1663 const DSCI end
= TheHalfClosed
->end();
1664 for (DSCI i
= TheHalfClosed
->begin(); i
!= end
; ++i
) {
1665 Comm::ConnectionPointer c
= new Comm::Connection
; // XXX: temporary. make HalfClosed a list of these.
1667 if (!fd_table
[c
->fd
].halfClosedReader
) { // not reading already
1668 AsyncCall::Pointer call
= commCbCall(5,4, "commHalfClosedReader",
1669 CommIoCbPtrFun(&commHalfClosedReader
, NULL
));
1670 Comm::Read(c
, call
);
1671 fd_table
[c
->fd
].halfClosedReader
= call
;
1673 c
->fd
= -1; // XXX: temporary. prevent c replacement erase closing listed FD
1676 WillCheckHalfClosed
= false; // as far as we know
1677 commPlanHalfClosedCheck(); // may need to check again
1680 /// checks whether we are waiting for possibly half-closed connection to close
1681 // We are monitoring if the read handler for the fd is the monitoring handler.
1683 commHasHalfClosedMonitor(int fd
)
1685 return TheHalfClosed
->has(fd
);
1688 /// stop waiting for possibly half-closed connection to close
1690 commStopHalfClosedMonitor(int const fd
)
1692 debugs(5, 5, HERE
<< "removing FD " << fd
<< " from " << *TheHalfClosed
);
1694 // cancel the read if one was scheduled
1695 AsyncCall::Pointer reader
= fd_table
[fd
].halfClosedReader
;
1697 Comm::ReadCancel(fd
, reader
);
1698 fd_table
[fd
].halfClosedReader
= NULL
;
1700 TheHalfClosed
->del(fd
);
1703 /// I/O handler for the possibly half-closed connection monitoring code
1705 commHalfClosedReader(const Comm::ConnectionPointer
&conn
, char *, size_t size
, comm_err_t flag
, int, void *)
1707 // there cannot be more data coming in on half-closed connections
1709 assert(conn
!= NULL
);
1710 assert(commHasHalfClosedMonitor(conn
->fd
)); // or we would have canceled the read
1712 fd_table
[conn
->fd
].halfClosedReader
= NULL
; // done reading, for now
1714 // nothing to do if fd is being closed
1715 if (flag
== COMM_ERR_CLOSING
)
1718 // if read failed, close the connection
1719 if (flag
!= COMM_OK
) {
1720 debugs(5, 3, HERE
<< "closing " << conn
);
1725 // continue waiting for close or error
1726 commPlanHalfClosedCheck(); // make sure this fd will be checked again
1729 CommRead::CommRead() : conn(NULL
), buf(NULL
), len(0), callback(NULL
) {}
1731 CommRead::CommRead(const Comm::ConnectionPointer
&c
, char *buf_
, int len_
, AsyncCall::Pointer
&callback_
)
1732 : conn(c
), buf(buf_
), len(len_
), callback(callback_
) {}
1734 DeferredRead::DeferredRead () : theReader(NULL
), theContext(NULL
), theRead(), cancelled(false) {}
1736 DeferredRead::DeferredRead (DeferrableRead
*aReader
, void *data
, CommRead
const &aRead
) : theReader(aReader
), theContext (data
), theRead(aRead
), cancelled(false) {}
1738 DeferredReadManager::~DeferredReadManager()
1741 assert (deferredReads
.empty());
1744 /* explicit instantiation required for some systems */
1746 /// \cond AUTODOCS_IGNORE
1747 template cbdata_type CbDataList
<DeferredRead
>::CBDATA_CbDataList
;
1751 DeferredReadManager::delayRead(DeferredRead
const &aRead
)
1753 debugs(5, 3, "Adding deferred read on " << aRead
.theRead
.conn
);
1754 CbDataList
<DeferredRead
> *temp
= deferredReads
.push_back(aRead
);
1756 // We have to use a global function as a closer and point to temp
1757 // instead of "this" because DeferredReadManager is not a job and
1758 // is not even cbdata protected
1759 // XXX: and yet we use cbdata protection functions on it??
1760 AsyncCall::Pointer closer
= commCbCall(5,4,
1761 "DeferredReadManager::CloseHandler",
1762 CommCloseCbPtrFun(&CloseHandler
, temp
));
1763 comm_add_close_handler(aRead
.theRead
.conn
->fd
, closer
);
1764 temp
->element
.closer
= closer
; // remeber so that we can cancel
1768 DeferredReadManager::CloseHandler(const CommCloseCbParams
¶ms
)
1770 if (!cbdataReferenceValid(params
.data
))
1773 CbDataList
<DeferredRead
> *temp
= (CbDataList
<DeferredRead
> *)params
.data
;
1775 temp
->element
.closer
= NULL
;
1776 temp
->element
.markCancelled();
1780 DeferredReadManager::popHead(CbDataListContainer
<DeferredRead
> &deferredReads
)
1782 assert (!deferredReads
.empty());
1784 DeferredRead
&read
= deferredReads
.head
->element
;
1786 // NOTE: at this point the connection has been paused/stalled for an unknown
1787 // amount of time. We must re-validate that it is active and usable.
1789 // If the connection has been closed already. Cancel this read.
1790 if (!Comm::IsConnOpen(read
.theRead
.conn
)) {
1791 if (read
.closer
!= NULL
) {
1792 read
.closer
->cancel("Connection closed before.");
1795 read
.markCancelled();
1798 if (!read
.cancelled
) {
1799 comm_remove_close_handler(read
.theRead
.conn
->fd
, read
.closer
);
1803 DeferredRead result
= deferredReads
.pop_front();
1809 DeferredReadManager::kickReads(int const count
)
1811 /* if we had CbDataList::size() we could consolidate this and flushReads */
1818 size_t remaining
= count
;
1820 while (!deferredReads
.empty() && remaining
) {
1821 DeferredRead aRead
= popHead(deferredReads
);
1824 if (!aRead
.cancelled
)
1830 DeferredReadManager::flushReads()
1832 CbDataListContainer
<DeferredRead
> reads
;
1833 reads
= deferredReads
;
1834 deferredReads
= CbDataListContainer
<DeferredRead
>();
1836 // XXX: For fairness this SHOULD randomize the order
1837 while (!reads
.empty()) {
1838 DeferredRead aRead
= popHead(reads
);
1844 DeferredReadManager::kickARead(DeferredRead
const &aRead
)
1846 if (aRead
.cancelled
)
1849 if (Comm::IsConnOpen(aRead
.theRead
.conn
) && fd_table
[aRead
.theRead
.conn
->fd
].closing())
1852 debugs(5, 3, "Kicking deferred read on " << aRead
.theRead
.conn
);
1854 aRead
.theReader(aRead
.theContext
, aRead
.theRead
);
1858 DeferredRead::markCancelled()
1864 CommSelectEngine::checkEvents(int timeout
)
1866 static time_t last_timeout
= 0;
1868 /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */
1869 if (squid_curtime
> last_timeout
) {
1870 last_timeout
= squid_curtime
;
1874 switch (Comm::DoSelect(timeout
)) {
1890 fatal_dump("comm.cc: Internal error -- this should never happen.");
1895 /// Create a unix-domain socket (UDS) that only supports FD_MSGHDR I/O.
1897 comm_open_uds(int sock_type
,
1899 struct sockaddr_un
* addr
,
1902 // TODO: merge with comm_openex() when Ip::Address becomes NetAddress
1906 PROF_start(comm_open
);
1907 /* Create socket for accepting new connections. */
1908 ++ statCounter
.syscalls
.sock
.sockets
;
1910 /* Setup the socket addrinfo details for use */
1913 AI
.ai_family
= PF_UNIX
;
1914 AI
.ai_socktype
= sock_type
;
1915 AI
.ai_protocol
= proto
;
1916 AI
.ai_addrlen
= SUN_LEN(addr
);
1917 AI
.ai_addr
= (sockaddr
*)addr
;
1918 AI
.ai_canonname
= NULL
;
1921 debugs(50, 3, HERE
<< "Attempt open socket for: " << addr
->sun_path
);
1923 if ((new_socket
= socket(AI
.ai_family
, AI
.ai_socktype
, AI
.ai_protocol
)) < 0) {
1924 /* Increase the number of reserved fd's if calls to socket()
1925 * are failing because the open file table is full. This
1926 * limits the number of simultaneous clients */
1928 if (limitError(errno
)) {
1929 debugs(50, DBG_IMPORTANT
, HERE
<< "socket failure: " << xstrerror());
1932 debugs(50, DBG_CRITICAL
, HERE
<< "socket failure: " << xstrerror());
1935 PROF_stop(comm_open
);
1939 debugs(50, 3, "Opened UDS FD " << new_socket
<< " : family=" << AI
.ai_family
<< ", type=" << AI
.ai_socktype
<< ", protocol=" << AI
.ai_protocol
);
1942 debugs(50, 5, HERE
<< "FD " << new_socket
<< " is a new socket");
1944 assert(!isOpen(new_socket
));
1945 fd_open(new_socket
, FD_MSGHDR
, NULL
);
1947 fdd_table
[new_socket
].close_file
= NULL
;
1949 fdd_table
[new_socket
].close_line
= 0;
1951 fd_table
[new_socket
].sock_family
= AI
.ai_family
;
1953 if (!(flags
& COMM_NOCLOEXEC
))
1954 commSetCloseOnExec(new_socket
);
1956 if (flags
& COMM_REUSEADDR
)
1957 commSetReuseAddr(new_socket
);
1959 if (flags
& COMM_NONBLOCKING
) {
1960 if (commSetNonBlocking(new_socket
) != COMM_OK
) {
1961 comm_close(new_socket
);
1962 PROF_stop(comm_open
);
1967 if (flags
& COMM_DOBIND
) {
1968 if (commBind(new_socket
, AI
) != COMM_OK
) {
1969 comm_close(new_socket
);
1970 PROF_stop(comm_open
);
1976 if (sock_type
== SOCK_STREAM
)
1977 commSetTcpNoDelay(new_socket
);
1981 if (Config
.tcpRcvBufsz
> 0 && sock_type
== SOCK_STREAM
)
1982 commSetTcpRcvbuf(new_socket
, Config
.tcpRcvBufsz
);
1984 PROF_stop(comm_open
);