Skip to content

Commit

Permalink
dnsdist: Enable PMTU discovery and disable fragmentation on QUIC binds
Browse files Browse the repository at this point in the history
  • Loading branch information
rgacogne committed Jan 8, 2024
1 parent 7d86946 commit 3198b2c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pdns/dnsdist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2360,10 +2360,20 @@ static void setupLocalSocket(ClientState& clientState, const ComboAddress& addr,
}
}

/* Only set this on IPv4 UDP sockets.
Don't set it for DNSCrypt binds. DNSCrypt pads queries for privacy
purposes, so we do receive large, sometimes fragmented datagrams. */
if (!tcp && !clientState.dnscryptCtx) {
const bool isQUIC = clientState.doqFrontend != nullptr || clientState.doh3Frontend != nullptr;
if (isQUIC) {
/* disable fragmentation and force PMTU discovery for QUIC-enabled sockets */
try {
setSocketForcePMTU(socket, addr.sin4.sin_family);
}
catch (const std::exception& e) {
warnlog("Failed to set IP_MTU_DISCOVER on QUIC server socket for local address '%s': %s", addr.toStringWithPort(), e.what());
}
}
else if (!tcp && !clientState.dnscryptCtx) {
/* Only set this on IPv4 UDP sockets.
Don't set it for DNSCrypt binds. DNSCrypt pads queries for privacy
purposes, so we do receive large, sometimes fragmented datagrams. */
try {
setSocketIgnorePMTU(socket, addr.sin4.sin_family);
}
Expand Down
21 changes: 21 additions & 0 deletions pdns/iputils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,27 @@ void setSocketIgnorePMTU([[maybe_unused]] int sockfd, [[maybe_unused]] int famil
}
}

void setSocketForcePMTU([[maybe_unused]] int sockfd, [[maybe_unused]] int family)
{
if (family == AF_INET) {
#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
/* IP_PMTUDISC_DO enables Path MTU discovery and prevents fragmentation */
SSetsockopt(sockfd, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DO);
#elif defined(IP_DONTFRAG)
/* at least this prevents fragmentation */
SSetsockopt(sockfd, IPPROTO_IP, IP_DONTFRAG, 1);
#endif /* defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) */
}
else {
#if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
/* IPV6_PMTUDISC_DO enables Path MTU discovery and prevents fragmentation */
SSetsockopt(sockfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, IPV6_PMTUDISC_DO);
#elif defined(IPV6_DONTFRAG)
/* at least this prevents fragmentation */
SSetsockopt(sockfd, IPPROTO_IPV6, IPV6_DONTFRAG, 1);
#endif /* defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) */
}
}

bool setReusePort(int sockfd)
{
Expand Down
1 change: 1 addition & 0 deletions pdns/iputils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,7 @@ int SAccept(int sockfd, ComboAddress& remote);
int SListen(int sockfd, int limit);
int SSetsockopt(int sockfd, int level, int opname, int value);
void setSocketIgnorePMTU(int sockfd, int family);
void setSocketForcePMTU(int sockfd, int family);
bool setReusePort(int sockfd);

#if defined(IP_PKTINFO)
Expand Down

0 comments on commit 3198b2c

Please sign in to comment.