--------------------- PatchSet 11384 Date: 2008/02/07 18:56:32 Author: Henrik Nordstrom Branch: HEAD Tag: (none) Log: TCP keepalive support Members: src/cache_cf.cc:1.538->1.539 src/cf.data.pre:1.497->1.498 src/client_side.cc:1.773->1.774 src/comm.cc:1.442->1.443 src/comm.h:1.33->1.34 src/structs.h:1.573->1.574 Index: squid3/src/cache_cf.cc =================================================================== RCS file: /cvsroot/squid/squid3/src/cache_cf.cc,v retrieving revision 1.538 retrieving revision 1.539 diff -u -r1.538 -r1.539 --- squid3/src/cache_cf.cc 22 Jan 2008 15:34:28 -0000 1.538 +++ squid3/src/cache_cf.cc 8 Feb 2008 01:56:32 -0000 1.539 @@ -1,6 +1,6 @@ /* - * $Id: cache_cf.cc,v 1.538 2008/01/22 15:34:28 hno Exp $ + * $Id: cache_cf.cc,v 1.539 2008/02/08 01:56:32 hno Exp $ * * DEBUG: section 3 Configuration File Parsing * AUTHOR: Harvest Derived @@ -2914,6 +2914,23 @@ self_destruct(); } #endif + } else if (strcmp(token, "tcpkeepalive") == 0) { + s->tcp_keepalive.enabled = 1; + } else if (strncmp(token, "tcpkeepalive=", 13) == 0) { + char *t = token + 13; + s->tcp_keepalive.enabled = 1; + s->tcp_keepalive.idle = atoi(t); + t = strchr(t, ','); + if (t) { + t++; + s->tcp_keepalive.interval = atoi(t); + t = strchr(t, ','); + } + if (t) { + t++; + s->tcp_keepalive.timeout = atoi(t); + t = strchr(t, ','); + } } else { self_destruct(); } @@ -3007,6 +3024,14 @@ storeAppendPrintf(e, " disable-pmtu-discovery=%s", pmtu); } + + if (s->tcp_keepalive.enabled) { + if (s->tcp_keepalive.idle || s->tcp_keepalive.interval || s->tcp_keepalive.timeout) { + storeAppendPrintf(e, " tcp_keepalive=%d,%d,%d", s->tcp_keepalive.idle, s->tcp_keepalive.interval, s->tcp_keepalive.timeout); + } else { + storeAppendPrintf(e, " tcp_keepalive"); + } + } } static void Index: squid3/src/cf.data.pre =================================================================== RCS file: /cvsroot/squid/squid3/src/cf.data.pre,v retrieving revision 1.497 retrieving revision 1.498 diff -u -r1.497 -r1.498 --- squid3/src/cf.data.pre 6 Feb 2008 06:54:14 -0000 1.497 +++ squid3/src/cf.data.pre 8 Feb 2008 01:56:32 -0000 1.498 @@ -1,6 +1,6 @@ # -# $Id: cf.data.pre,v 1.497 2008/02/06 06:54:14 amosjeffries Exp $ +# $Id: cf.data.pre,v 1.498 2008/02/08 01:56:32 hno Exp $ # # SQUID Web Proxy Cache http://www.squid-cache.org/ # ---------------------------------------------------------- @@ -929,6 +929,12 @@ name= Specifies a internal name for the port. Defaults to the port specification (port or addr:port) + keepalive[=idle,interval,timeout] + Enable TCP keepalive probes of idle connections + idle is the initial time before TCP starts probing + the connection, interval how often to probe, and + timeout the time before giving up. + If you run Squid on a dual-homed machine with an internal and an external interface we recommend you to specify the internal address:port in http_port. This way Squid will only be Index: squid3/src/client_side.cc =================================================================== RCS file: /cvsroot/squid/squid3/src/client_side.cc,v retrieving revision 1.773 retrieving revision 1.774 diff -u -r1.773 -r1.774 --- squid3/src/client_side.cc 3 Feb 2008 10:00:29 -0000 1.773 +++ squid3/src/client_side.cc 8 Feb 2008 01:56:33 -0000 1.774 @@ -1,6 +1,6 @@ /* - * $Id: client_side.cc,v 1.773 2008/02/03 10:00:29 amosjeffries Exp $ + * $Id: client_side.cc,v 1.774 2008/02/08 01:56:33 hno Exp $ * * DEBUG: section 33 Client-side Routines * AUTHOR: Duane Wessels @@ -2782,6 +2782,10 @@ #endif + if (s->tcp_keepalive.enabled) { + commSetTcpKeepalive(newfd, s->tcp_keepalive.idle, s->tcp_keepalive.interval, s->tcp_keepalive.timeout); + } + connState->readSomeData(); clientdbEstablished(details->peer, 1); @@ -2976,6 +2980,10 @@ #endif + if (s->http.tcp_keepalive.enabled) { + commSetTcpKeepalive(newfd, s->http.tcp_keepalive.idle, s->http.tcp_keepalive.interval, s->http.tcp_keepalive.timeout); + } + commSetSelect(newfd, COMM_SELECT_READ, clientNegotiateSSL, connState, 0); clientdbEstablished(details->peer, 1); Index: squid3/src/comm.cc =================================================================== RCS file: /cvsroot/squid/squid3/src/comm.cc,v retrieving revision 1.442 retrieving revision 1.443 diff -u -r1.442 -r1.443 --- squid3/src/comm.cc 19 Jan 2008 07:11:35 -0000 1.442 +++ squid3/src/comm.cc 8 Feb 2008 01:56:33 -0000 1.443 @@ -1,6 +1,6 @@ /* - * $Id: comm.cc,v 1.442 2008/01/19 07:11:35 amosjeffries Exp $ + * $Id: comm.cc,v 1.443 2008/02/08 01:56:33 hno Exp $ * * DEBUG: section 5 Socket Functions * AUTHOR: Harvest Derived @@ -1956,6 +1956,32 @@ #endif +void +commSetTcpKeepalive(int fd, int idle, int interval, int timeout) +{ + int on = 1; +#ifdef TCP_KEEPCNT + if (timeout && interval) { + int count = (timeout + interval - 1) / interval; + if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(on)) < 0) + debug(5, 1) ("commSetKeepalive: FD %d: %s\n", fd, xstrerror()); + } +#endif +#ifdef TCP_KEEPIDLE + if (idle) { + if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(on)) < 0) + debug(5, 1) ("commSetKeepalive: FD %d: %s\n", fd, xstrerror()); + } +#endif +#ifdef TCP_KEEPINTVL + if (interval) { + if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(on)) < 0) + debug(5, 1) ("commSetKeepalive: FD %d: %s\n", fd, xstrerror()); + } +#endif + if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on)) < 0) + debug(5, 1) ("commSetKeepalive: FD %d: %s\n", fd, xstrerror()); +} void comm_init(void) { Index: squid3/src/comm.h =================================================================== RCS file: /cvsroot/squid/squid3/src/comm.h,v retrieving revision 1.33 retrieving revision 1.34 diff -u -r1.33 -r1.34 --- squid3/src/comm.h 7 Jan 2008 16:22:06 -0000 1.33 +++ squid3/src/comm.h 8 Feb 2008 01:56:33 -0000 1.34 @@ -39,6 +39,7 @@ SQUIDCEXTERN int commSetNonBlocking(int fd); SQUIDCEXTERN int commUnsetNonBlocking(int fd); SQUIDCEXTERN void commSetCloseOnExec(int fd); +SQUIDCEXTERN void commSetTcpKeepalive(int fd, int idle, int interval, int timeout); extern void _comm_close(int fd, char const *file, int line); #define comm_close(fd) (_comm_close((fd), __FILE__, __LINE__)) SQUIDCEXTERN void comm_reset_close(int fd); Index: squid3/src/structs.h =================================================================== RCS file: /cvsroot/squid/squid3/src/structs.h,v retrieving revision 1.573 retrieving revision 1.574 diff -u -r1.573 -r1.574 --- squid3/src/structs.h 19 Jan 2008 07:15:30 -0000 1.573 +++ squid3/src/structs.h 8 Feb 2008 01:56:33 -0000 1.574 @@ -1,6 +1,6 @@ /* - * $Id: structs.h,v 1.573 2008/01/19 07:15:30 amosjeffries Exp $ + * $Id: structs.h,v 1.574 2008/02/08 01:56:33 hno Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -147,6 +147,12 @@ unsigned int tproxy: 1; /* spoof client ip using tproxy */ #endif + struct { + unsigned int enabled; + unsigned int idle; + unsigned int interval; + unsigned int timeout; + } tcp_keepalive; };