From 7597fb3adf34e12400441be7b67538d00c3a7443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sven=20G=C3=B6thel?= Date: Tue, 29 Oct 2024 09:17:28 +0100 Subject: [PATCH] net::Defaults.maxTCPConnections: Use system's maximum concurrent TCP connections, disable if undefined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Used system value on a Linux kernel are - /proc/sys/net/ipv4/tcp_max_orphans See https://www.kernel.org/doc/html/latest/networking/ip-sysctl.html - /proc/sys/net/nf_conntrack_max See https://www.kernel.org/doc/html/latest/networking/nf_conntrack-sysctl.html Signed-off-by: Sven Göthel Change-Id: Iad74f253bdac5636757b130b299b5deacda658db --- common/Util-desktop.cpp | 16 ++++++++++++++++ common/Util-mobile.cpp | 1 + common/Util.hpp | 3 +++ net/Socket.cpp | 2 +- wsd/COOLWSD.cpp | 3 +++ 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/common/Util-desktop.cpp b/common/Util-desktop.cpp index bec440cf85f57..c37965ca01840 100644 --- a/common/Util-desktop.cpp +++ b/common/Util-desktop.cpp @@ -11,6 +11,7 @@ #include #include "Util.hpp" +#include "FileUtil.hpp" #ifdef __linux__ #include @@ -160,6 +161,21 @@ std::size_t getTotalSystemMemoryKb() return totalMemKb; } +std::size_t getMaxConcurrentTCPConnections() +{ +#ifdef __linux__ + char line[1024+1]; // includes EOS + const ssize_t tcp_max_orphans = FileUtil::readDecimal("/proc/sys/net/ipv4/tcp_max_orphans", line, sizeof(line)-1, 0); + const ssize_t nf_conntrack_max = FileUtil::readDecimal("/proc/sys/net/nf_conntrack_max", line, sizeof(line)-1, 0); + LOG_DBG("MaxConcurrentTCPConnections: min(orphans " << tcp_max_orphans + << ", conntrack " << nf_conntrack_max << ") = " + << std::min(tcp_max_orphans, nf_conntrack_max)); + return std::min(tcp_max_orphans, nf_conntrack_max); +#else + return 0; +#endif +} + std::size_t getFromCGroup(const std::string& group, const std::string& key) { std::size_t num = 0; diff --git a/common/Util-mobile.cpp b/common/Util-mobile.cpp index 5271422398e2c..a8f9ac4cfcdea 100644 --- a/common/Util-mobile.cpp +++ b/common/Util-mobile.cpp @@ -21,6 +21,7 @@ int spawnProcess(const std::string& cmd, const StringVector& args) { return 0; } std::string getHumanizedBytes(unsigned long nBytes) { return std::string(); } size_t getTotalSystemMemoryKb() { return 0; } +std::size_t getMaxConcurrentTCPConnections() { return 0; } std::size_t getFromFile(const char* path) { return 0; } std::size_t getCGroupMemLimit() { return 0; } std::size_t getCGroupMemSoftLimit() { return 0; } diff --git a/common/Util.hpp b/common/Util.hpp index eadbea1e8137e..a7197de67cf0c 100644 --- a/common/Util.hpp +++ b/common/Util.hpp @@ -353,6 +353,9 @@ namespace Util /// Returns the total physical memory (in kB) available in the system size_t getTotalSystemMemoryKb(); + /// Returns the maximum number of concurrent TCP connections, zero if undefined. + std::size_t getMaxConcurrentTCPConnections(); + /// Returns the numerical content of a file at @path std::size_t getFromFile(const char *path); diff --git a/net/Socket.cpp b/net/Socket.cpp index eaf2aaeedf388..02e21bbce863b 100644 --- a/net/Socket.cpp +++ b/net/Socket.cpp @@ -68,7 +68,7 @@ std::unique_ptr SocketPoll::PollWatchdog; std::atomic StreamSocket::ExternalConnectionCount = 0; net::DefaultValues net::Defaults = { .inactivityTimeout = std::chrono::seconds(3600), - .maxExtConnections = 200000 /* arbitrary value to be resolved */ }; + .maxExtConnections = 0 /* unlimited default */}; #define SOCKET_ABSTRACT_UNIX_NAME "0coolwsd-" diff --git a/wsd/COOLWSD.cpp b/wsd/COOLWSD.cpp index ae00f311893f2..0c996c2debddc 100644 --- a/wsd/COOLWSD.cpp +++ b/wsd/COOLWSD.cpp @@ -2334,6 +2334,9 @@ void COOLWSD::innerInitialize(Poco::Util::Application& self) } UnitWSD::get().setWSD(this); + // net::Defaults: Determine maxExtConnections field + net::Defaults.maxExtConnections = std::max(Util::getMaxConcurrentTCPConnections(), std::max(3, MAX_CONNECTIONS)); + // Allow UT to manipulate before using configuration values. UnitWSD::get().configure(conf);