From 2489b946bca4e2bbfd524de260331972329a00d4 Mon Sep 17 00:00:00 2001 From: Charles-Henri Bruyand Date: Fri, 22 Dec 2023 16:20:23 +0100 Subject: [PATCH] dnsdist: try to increase receive and send buffers to max --- pdns/dnsdist.cc | 18 ++++++++++++++++++ pdns/iputils.cc | 33 +++++++++++++++++++++++++++++++++ pdns/iputils.hh | 2 ++ 3 files changed, 53 insertions(+) diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index 3df62e18e2..e4283a84cd 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -2380,6 +2380,15 @@ static void setupLocalSocket(ClientState& clientState, const ComboAddress& addr, catch (const std::exception& e) { warnlog(e.what()); } + } else { + try { + auto result = raiseSocketSendBufferToMax(socket); + if (result > 0) { + infolog("Raised send buffer to %u for local address '%s'", result, addr.toStringWithPort()); + } + } catch (const std::exception& e) { + warnlog(e.what()); + } } if (g_socketUDPRecvBuffer > 0) { @@ -2389,6 +2398,15 @@ static void setupLocalSocket(ClientState& clientState, const ComboAddress& addr, catch (const std::exception& e) { warnlog(e.what()); } + } else { + try { + auto result = raiseSocketReceiveBufferToMax(socket); + if (result > 0) { + infolog("Raised receive buffer to %u for local address '%s'", result, addr.toStringWithPort()); + } + } catch (const std::exception& e) { + warnlog(e.what()); + } } } diff --git a/pdns/iputils.cc b/pdns/iputils.cc index 2eda93507c..e52755670b 100644 --- a/pdns/iputils.cc +++ b/pdns/iputils.cc @@ -25,6 +25,7 @@ #include "iputils.hh" +#include #include #include @@ -537,6 +538,38 @@ void setSocketSendBuffer(int fd, uint32_t size) setSocketBuffer(fd, SO_SNDBUF, size); } +static uint32_t raiseSocketBufferToMax(int fd, int optname, const std::string& readMaxFromFile) +{ + std::ifstream ifs(readMaxFromFile); + if (ifs) { + std::string line; + if (getline(ifs, line)) { + auto max = pdns::checked_stoi(line); + setSocketBuffer(fd, optname, max); + return max; + } + } + return 0; +} + +uint32_t raiseSocketReceiveBufferToMax(int fd) +{ +#ifdef __linux__ + return raiseSocketBufferToMax(fd, SO_RCVBUF, "/proc/sys/net/core/rmem_max"); +#else + return 0; +#endif +} + +uint32_t raiseSocketSendBufferToMax(int fd) +{ +#ifdef __linux__ + return raiseSocketBufferToMax(fd, SO_SNDBUF, "/proc/sys/net/core/wmem_max"); +#else + return 0; +#endif +} + std::set getListOfNetworkInterfaces() { std::set result; diff --git a/pdns/iputils.hh b/pdns/iputils.hh index 18c15bcc53..7f0159e552 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -1729,3 +1729,5 @@ std::vector getListOfRangesOfNetworkInterface(const std::string& itf); void setSocketBuffer(int fd, int optname, uint32_t size); void setSocketReceiveBuffer(int fd, uint32_t size); void setSocketSendBuffer(int fd, uint32_t size); +uint32_t raiseSocketReceiveBufferToMax(int fd); +uint32_t raiseSocketSendBufferToMax(int fd); -- 2.47.2