]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: try to increase receive and send buffers to max
authorCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Fri, 22 Dec 2023 15:20:23 +0000 (16:20 +0100)
committerCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Tue, 26 Dec 2023 12:10:22 +0000 (13:10 +0100)
pdns/dnsdist.cc
pdns/iputils.cc
pdns/iputils.hh

index 3df62e18e2d6e784fd55f9d3a914fa44d48cde63..e4283a84cdfdaf50b65d5e73a5b256d81c912cea 100644 (file)
@@ -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());
+      }
     }
   }
 
index 2eda93507caaa6588a2f9668f2255f675349ac68..e52755670b15637dd392dc4adea14e12bf37895f 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "iputils.hh"
 
+#include <fstream>
 #include <sys/socket.h>
 #include <boost/format.hpp>
 
@@ -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<uint32_t>(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<std::string> getListOfNetworkInterfaces()
 {
   std::set<std::string> result;
index 18c15bcc5343eb2d8a61d3e6e3cee975d561e948..7f0159e55206280ec297c898e189b3904afc6f89 100644 (file)
@@ -1729,3 +1729,5 @@ std::vector<Netmask> 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);