From: Remi Gacogne Date: Thu, 18 Nov 2021 16:38:56 +0000 (+0100) Subject: dnsdist: Add a function to set the UDP recv/snd buffer sizes X-Git-Tag: dnsdist-1.7.0-beta2~14^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2ad23378b19ff170e5abc0380a95fe2d9123e79d;p=thirdparty%2Fpdns.git dnsdist: Add a function to set the UDP recv/snd buffer sizes And raise them to 16777216 by default. --- diff --git a/pdns/dnsdist-console.cc b/pdns/dnsdist-console.cc index 44e33cc027..bcf605844a 100644 --- a/pdns/dnsdist-console.cc +++ b/pdns/dnsdist-console.cc @@ -633,6 +633,7 @@ const std::vector g_consoleKeywords{ { "setTCPRecvTimeout", true, "n", "set the read timeout on TCP connections from the client, in seconds" }, { "setTCPSendTimeout", true, "n", "set the write timeout on TCP connections from the client, in seconds" }, { "setUDPMultipleMessagesVectorSize", true, "n", "set the size of the vector passed to recvmmsg() to receive UDP messages. Default to 1 which means that the feature is disabled and recvmsg() is used instead" }, + { "setUDPSocketBufferSizes", true, "recv, send", "Set the size of the receive (SO_RCVBUF) and send (SO_SNDBUF) buffers for incoming UDP sockets" }, { "setUDPTimeout", true, "n", "set the maximum time dnsdist will wait for a response from a backend over UDP, in seconds" }, { "setVerboseHealthChecks", true, "bool", "set whether health check errors will be logged" }, { "setWebserverConfig", true, "[{password=string, apiKey=string, customHeaders, statsRequireAuthentication}]", "Updates webserver configuration" }, diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index fde21a9fcc..a05454ef5f 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -2780,6 +2780,23 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } } }); + + luaCtx.writeFunction("setUDPSocketBufferSizes", [client](uint64_t recv, uint64_t snd) { + if (client) { + return; + } + checkParameterBound("setUDPSocketBufferSizes", recv, std::numeric_limits::max()); + checkParameterBound("setUDPSocketBufferSizes", snd, std::numeric_limits::max()); + setLuaSideEffect(); + + if (g_configurationDone) { + g_outputBuffer = "setUDPSocketBufferSizes cannot be used at runtime!\n"; + return; + } + + g_socketUDPSendBuffer = snd; + g_socketUDPRecvBuffer = recv; + }); } vector> setupLua(LuaContext& luaCtx, bool client, bool configCheck, const std::string& config) diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index 4d43eadec2..e679d64b48 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -141,6 +141,8 @@ bool g_servFailOnNoPolicy{false}; bool g_truncateTC{false}; bool g_fixupCase{false}; bool g_dropEmptyQueries{false}; +uint32_t g_socketUDPSendBuffer{16777216}; +uint32_t g_socketUDPRecvBuffer{16777216}; std::set g_capabilitiesToRetain; @@ -2073,6 +2075,26 @@ static void setUpLocalBind(std::unique_ptr& cs) } } + if (!cs->tcp) { + if (g_socketUDPSendBuffer > 0) { + try { + setSocketSendBuffer(cs->udpFD, g_socketUDPSendBuffer); + } + catch (const std::exception& e) { + warnlog(e.what()); + } + } + + if (g_socketUDPRecvBuffer > 0) { + try { + setSocketReceiveBuffer(cs->udpFD, g_socketUDPRecvBuffer); + } + catch (const std::exception& e) { + warnlog(e.what()); + } + } + } + const std::string& itf = cs->interface; if (!itf.empty()) { #ifdef SO_BINDTODEVICE diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index 4576f84359..6ee0b33828 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -1016,6 +1016,8 @@ extern std::string g_apiConfigDirectory; extern bool g_servFailOnNoPolicy; extern size_t g_udpVectorSize; extern bool g_allowEmptyResponse; +extern uint32_t g_socketUDPSendBuffer; +extern uint32_t g_socketUDPRecvBuffer; extern shared_ptr g_defaultBPFFilter; extern std::vector > g_dynBPFFilters; diff --git a/pdns/dnsdistdist/docs/reference/tuning.rst b/pdns/dnsdistdist/docs/reference/tuning.rst index 90442f5b80..da70e97049 100644 --- a/pdns/dnsdistdist/docs/reference/tuning.rst +++ b/pdns/dnsdistdist/docs/reference/tuning.rst @@ -160,6 +160,15 @@ Tuning related functions :param int num: maximum number of UDP queries to accept +.. function:: setUDPSocketBufferSize(recv, send) + + .. versionadded:: 1.7.0 + + Set the size of the receive (SO_RCVBUF) and send (SO_SNDBUF) buffers for incoming UDP sockets. + + :param int recv: SO_RCVBUF value. Default is 16777216. 0 means the system value will be kept. + :param int send: SO_SNDBUF value. Default is 16777216. 0 means the system value will be kept. + .. function:: setUDPTimeout(num) Set the maximum time dnsdist will wait for a response from a backend over UDP, in seconds. Defaults to 2