forked from squid-cache/squid
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setting TCP keep-alive flags at accept(2) time resolves issues with client sockets timing out while waiting for the ::Server handler to run. Also resolves a bug with FTP DATA connections not having keep-alive set. These connections would truncate objects if the data transfer connection paused for too long and became timed out by the network routing system.
- Loading branch information
1 parent
a80e3b4
commit 1dde466
Showing
16 changed files
with
134 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (C) 1996-2021 The Squid Software Foundation and contributors | ||
* | ||
* Squid software is distributed under GPLv2+ license and includes | ||
* contributions from numerous individuals and organizations. | ||
* Please see the COPYING and CONTRIBUTORS files for details. | ||
*/ | ||
|
||
/* DEBUG: section 05 TCP Socket Functions */ | ||
|
||
#include "squid.h" | ||
#include "comm/Tcp.h" | ||
#include "Debug.h" | ||
|
||
#if HAVE_NETINET_TCP_H | ||
#include <netinet/tcp.h> | ||
#endif | ||
#include <type_traits> | ||
|
||
/// setsockopt(2) wrapper | ||
template <typename Option> | ||
static bool | ||
SetSocketOption(const int fd, const int level, const int optName, const Option &optValue) | ||
{ | ||
#if HAVE_STD_IS_TRIVIALLY_COPYABLE | ||
static_assert(std::is_trivially_copyable<Option>::value, "setsockopt() expects POD-like options"); | ||
#endif | ||
static_assert(!std::is_same<Option, bool>::value, "setsockopt() uses int to represent boolean options"); | ||
if (setsockopt(fd, level, optName, &optValue, sizeof(optValue)) < 0) { | ||
const auto xerrno = errno; | ||
debugs(5, DBG_IMPORTANT, "ERROR: setsockopt(2) failure: " << xstrerr(xerrno)); | ||
// TODO: Generalize to throw on errors when some callers need that. | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/// setsockopt(2) wrapper for setting typical on/off options | ||
static bool | ||
SetBooleanSocketOption(const int fd, const int level, const int optName, const bool enable) | ||
{ | ||
const int optValue = enable ? 1 : 0; | ||
return SetSocketOption(fd, level, optName, optValue); | ||
} | ||
|
||
void | ||
Comm::ApplyTcpKeepAlive(int fd, const TcpKeepAlive &cfg) | ||
{ | ||
if (!cfg.enabled) | ||
return; | ||
|
||
#if defined(TCP_KEEPCNT) | ||
if (cfg.timeout && cfg.interval) { | ||
const int count = (cfg.timeout + cfg.interval - 1) / cfg.interval; // XXX: unsigned-to-signed conversion | ||
(void)SetSocketOption(fd, IPPROTO_TCP, TCP_KEEPCNT, count); | ||
} | ||
#endif | ||
#if defined(TCP_KEEPIDLE) | ||
if (cfg.idle) { | ||
// XXX: TCP_KEEPIDLE expects an int; cfg.idle is unsigned | ||
(void)SetSocketOption(fd, IPPROTO_TCP, TCP_KEEPIDLE, cfg.idle); | ||
} | ||
#endif | ||
#if defined(TCP_KEEPINTVL) | ||
if (cfg.interval) { | ||
// XXX: TCP_KEEPINTVL expects an int; cfg.interval is unsigned | ||
(void)SetSocketOption(fd, IPPROTO_TCP, TCP_KEEPINTVL, cfg.interval); | ||
} | ||
#endif | ||
(void)SetBooleanSocketOption(fd, SOL_SOCKET, SO_KEEPALIVE, true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (C) 1996-2021 The Squid Software Foundation and contributors | ||
* | ||
* Squid software is distributed under GPLv2+ license and includes | ||
* contributions from numerous individuals and organizations. | ||
* Please see the COPYING and CONTRIBUTORS files for details. | ||
*/ | ||
|
||
#ifndef SQUID__SRC_COMM_TCP_H | ||
#define SQUID__SRC_COMM_TCP_H | ||
|
||
namespace Comm | ||
{ | ||
|
||
/// Configuration settings for the TCP keep-alive feature | ||
class TcpKeepAlive | ||
{ | ||
public: | ||
unsigned int idle = 0; | ||
unsigned int interval = 0; | ||
unsigned int timeout = 0; | ||
bool enabled = false; | ||
}; | ||
|
||
/// apply configured TCP keep-alive settings to the given FD socket | ||
void ApplyTcpKeepAlive(int fd, const TcpKeepAlive &); | ||
|
||
} // namespace Comm | ||
|
||
#endif /* SQUID__SRC_COMM_TCP_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters