Skip to content

Commit 0645909

Browse files
committed
dnsdist: Also apply UDP socket buffer sizes to backend sockets
Signed-off-by: Remi Gacogne <remi.gacogne@powerdns.com>
1 parent 5b3ebf1 commit 0645909

6 files changed

Lines changed: 144 additions & 45 deletions

File tree

pdns/dnsdistdist/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ dnsdist_SOURCES = \
258258
dnsdist-tcp-downstream.cc dnsdist-tcp-downstream.hh \
259259
dnsdist-tcp-upstream.hh \
260260
dnsdist-tcp.cc dnsdist-tcp.hh \
261+
dnsdist-udp.cc dnsdist-udp.hh \
261262
dnsdist-web.cc dnsdist-web.hh \
262263
dnsdist-xsk.cc dnsdist-xsk.hh \
263264
dnsdist.cc dnsdist.hh \
@@ -372,6 +373,7 @@ testrunner_SOURCES = \
372373
dnsdist-svc.cc dnsdist-svc.hh \
373374
dnsdist-tcp-downstream.cc \
374375
dnsdist-tcp.cc dnsdist-tcp.hh \
376+
dnsdist-udp.cc dnsdist-udp.hh \
375377
dnsdist-xsk.cc dnsdist-xsk.hh \
376378
dnsdist.hh \
377379
dnslabeltext.cc \

pdns/dnsdistdist/dnsdist-backend.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "dnsdist-rings.hh"
3737
#include "dnsdist-snmp.hh"
3838
#include "dnsdist-tcp.hh"
39+
#include "dnsdist-udp.hh"
3940
#include "dnsdist-xsk.hh"
4041
#include "dolog.hh"
4142
#include "xsk.hh"
@@ -135,6 +136,8 @@ bool DownstreamState::reconnect(bool initialAttempt)
135136
}
136137
#endif
137138

139+
dnsdist::udp::setUDPSocketBufferSizes(fd, *getLogger(), dnsdist::udp::Context::Backend, d_config.remote);
140+
138141
if (!IsAnyAddress(d_config.sourceAddr)) {
139142
#ifdef IP_BIND_ADDRESS_NO_PORT
140143
if (d_config.ipBindAddrNoPort) {

pdns/dnsdistdist/dnsdist-udp.cc

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* This file is part of PowerDNS or dnsdist.
3+
* Copyright -- PowerDNS.COM B.V. and its contributors
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of version 2 of the GNU General Public License as
7+
* published by the Free Software Foundation.
8+
*
9+
* In addition, for the avoidance of any doubt, permission is granted to
10+
* link this program with OpenSSL and to (re)distribute the binaries
11+
* produced as the result of such linking.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21+
*/
22+
23+
#include "dnsdist-udp.hh"
24+
#include "dolog.hh"
25+
#include "dnsdist-configuration.hh"
26+
27+
namespace dnsdist::udp
28+
{
29+
static std::string contextToStr(Context context)
30+
{
31+
if (context == Context::Frontend) {
32+
return "frontend";
33+
}
34+
if (context == Context::Backend) {
35+
return "backend";
36+
}
37+
38+
return "";
39+
}
40+
41+
void setUDPSocketBufferSizes(int socketDesc, const Logr::Logger& logger, Context context, const ComboAddress& addr)
42+
{
43+
const auto& immutableConfig = dnsdist::configuration::getImmutableConfiguration();
44+
if (immutableConfig.d_socketUDPSendBuffer > 0) {
45+
try {
46+
setSocketSendBuffer(socketDesc, immutableConfig.d_socketUDPSendBuffer);
47+
}
48+
catch (const std::exception& e) {
49+
if (context == Context::Frontend) {
50+
SLOG(warnlog(e.what()),
51+
logger.error(Logr::Warning, e.what(), "Failed to raise send buffer size on UDP socket", "frontend.address", Logging::Loggable(addr)));
52+
}
53+
}
54+
}
55+
else {
56+
try {
57+
auto result = raiseSocketSendBufferToMax(socketDesc);
58+
if (result > 0 && context == Context::Frontend) {
59+
SLOG(infolog("Raised send buffer to %u for %s address '%s'", result, contextToStr(context), addr.toStringWithPort()),
60+
logger.info(Logr::Info, "Raised send buffer size", "frontend.address", Logging::Loggable(addr), "network.send_buffer_size", Logging::Loggable(result)));
61+
}
62+
}
63+
catch (const std::exception& e) {
64+
if (context == Context::Frontend) {
65+
SLOG(warnlog(e.what()),
66+
logger.error(Logr::Warning, e.what(), "Failed to raise send buffer size on UDP socket", "frontend.address", Logging::Loggable(addr)));
67+
}
68+
}
69+
}
70+
71+
if (immutableConfig.d_socketUDPRecvBuffer > 0) {
72+
try {
73+
setSocketReceiveBuffer(socketDesc, immutableConfig.d_socketUDPRecvBuffer);
74+
}
75+
catch (const std::exception& e) {
76+
if (context == Context::Frontend) {
77+
SLOG(warnlog(e.what()),
78+
logger.error(Logr::Warning, e.what(), "Failed to raise receive buffer size on UDP socket", "frontend.address", Logging::Loggable(addr)));
79+
}
80+
}
81+
}
82+
else {
83+
try {
84+
auto result = raiseSocketReceiveBufferToMax(socketDesc);
85+
if (result > 0 && context == Context::Frontend) {
86+
SLOG(infolog("Raised receive buffer to %u for address '%s'", result, addr.toStringWithPort()),
87+
logger.info(Logr::Info, "Raised receive buffer size", "frontend.address", Logging::Loggable(addr), "buffer_size", Logging::Loggable(result)));
88+
}
89+
}
90+
catch (const std::exception& e) {
91+
if (context == Context::Frontend) {
92+
SLOG(warnlog(e.what()),
93+
logger.error(Logr::Warning, e.what(), "Failed to raise receive buffer size on UDP socket", "frontend.address", Logging::Loggable(addr)));
94+
}
95+
}
96+
}
97+
}
98+
99+
} // dnsdist::udp

pdns/dnsdistdist/dnsdist-udp.hh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of PowerDNS or dnsdist.
3+
* Copyright -- PowerDNS.COM B.V. and its contributors
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of version 2 of the GNU General Public License as
7+
* published by the Free Software Foundation.
8+
*
9+
* In addition, for the avoidance of any doubt, permission is granted to
10+
* link this program with OpenSSL and to (re)distribute the binaries
11+
* produced as the result of such linking.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21+
*/
22+
#pragma once
23+
24+
#include <cstdint>
25+
26+
#include "dnsdist-logging.hh"
27+
28+
namespace dnsdist::udp
29+
{
30+
enum class Context : uint8_t
31+
{
32+
Frontend,
33+
Backend
34+
};
35+
36+
void setUDPSocketBufferSizes(int socketDesc, const Logr::Logger& logger, Context context, const ComboAddress& addr);
37+
}

pdns/dnsdistdist/dnsdist.cc

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#include "dnsdist-tcp.hh"
7878
#include "dnsdist-tcp-downstream.hh"
7979
#include "dnsdist-tcp-upstream.hh"
80+
#include "dnsdist-udp.hh"
8081
#include "dnsdist-web.hh"
8182
#include "dnsdist-xsk.hh"
8283

@@ -2841,51 +2842,7 @@ static void setupLocalSocket(ClientState& clientState, const ComboAddress& addr,
28412842
}
28422843

28432844
if (!tcp) {
2844-
if (immutableConfig.d_socketUDPSendBuffer > 0) {
2845-
try {
2846-
setSocketSendBuffer(socket, immutableConfig.d_socketUDPSendBuffer);
2847-
}
2848-
catch (const std::exception& e) {
2849-
SLOG(warnlog(e.what()),
2850-
logger->error(Logr::Warning, e.what(), "Failed to raise send buffer size on UDP server socket", "frontend.address", Logging::Loggable(addr)));
2851-
}
2852-
}
2853-
else {
2854-
try {
2855-
auto result = raiseSocketSendBufferToMax(socket);
2856-
if (result > 0) {
2857-
SLOG(infolog("Raised send buffer to %u for local address '%s'", result, addr.toStringWithPort()),
2858-
logger->info(Logr::Info, "Raised send buffer size", "frontend.address", Logging::Loggable(addr), "network.send_buffer_size", Logging::Loggable(result)));
2859-
}
2860-
}
2861-
catch (const std::exception& e) {
2862-
SLOG(warnlog(e.what()),
2863-
logger->error(Logr::Warning, e.what(), "Failed to raise send buffer size on UDP server socket", "frontend.address", Logging::Loggable(addr)));
2864-
}
2865-
}
2866-
2867-
if (immutableConfig.d_socketUDPRecvBuffer > 0) {
2868-
try {
2869-
setSocketReceiveBuffer(socket, immutableConfig.d_socketUDPRecvBuffer);
2870-
}
2871-
catch (const std::exception& e) {
2872-
SLOG(warnlog(e.what()),
2873-
logger->error(Logr::Warning, e.what(), "Failed to raise receive buffer size on UDP server socket", "frontend.address", Logging::Loggable(addr)));
2874-
}
2875-
}
2876-
else {
2877-
try {
2878-
auto result = raiseSocketReceiveBufferToMax(socket);
2879-
if (result > 0) {
2880-
SLOG(infolog("Raised receive buffer to %u for local address '%s'", result, addr.toStringWithPort()),
2881-
logger->info(Logr::Info, "Raised receive buffer size", "frontend.address", Logging::Loggable(addr), "buffer_size", Logging::Loggable(result)));
2882-
}
2883-
}
2884-
catch (const std::exception& e) {
2885-
SLOG(warnlog(e.what()),
2886-
logger->error(Logr::Warning, e.what(), "Failed to raise receive buffer size on UDP server socket", "frontend.address", Logging::Loggable(addr)));
2887-
}
2888-
}
2845+
dnsdist::udp::setUDPSocketBufferSizes(socket, *logger, dnsdist::udp::Context::Frontend, addr);
28892846
}
28902847

28912848
const std::string& itf = clientState.interface;

pdns/dnsdistdist/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ common_sources += files(
188188
src_dir / 'dnsdist-systemd.cc',
189189
src_dir / 'dnsdist-tcp.cc',
190190
src_dir / 'dnsdist-tcp-downstream.cc',
191+
src_dir / 'dnsdist-udp.cc',
191192
src_dir / 'dnsdist-web.cc',
192193
src_dir / 'dnsdist-xsk.cc',
193194
src_dir / 'dnsname.cc',

0 commit comments

Comments
 (0)