Add socket options related to fragmentation (#3757)

### Motivation

SwiftNIO provides convenience functions for a range of socket options.
This PR adds convenience options for fragmentation-related socket
options on Darwin and Linux. One particular use case is disabling
fragmentation when using QUIC, which disallows IP fragmentation of its
datagrams.

### Modifications

* Add `NIOBSDSocket.Option.ip_dontfrag` (Darwin).
* Add `NIOBSDSocket.Option.ipv6_dontfrag` (Darwin and Linux).
* Add `NIOBSDSocket.Option.ip_mtu_discover` and `.ipv6_mtu_discover`
(Linux). They set the path MTU discovery mode, which influences
fragmentation.

### Result

Adopters can easily configure IP fragmentation.
This commit is contained in:
Raphael Hiesgen authored and GitHub committed 2026-10-05 09:05:01 +01:00
1 parent d09ffc8ad6
commit 6db9d9abd9
4 files changed
+91

No files matched your search

+1
View File
@@ -48,6 +48,7 @@ extern const int CNIODarwin_IPTOS_ECN_ECT1;
extern const int CNIODarwin_IPTOS_ECN_CE;
extern const int CNIODarwin_IPV6_RECVPKTINFO;
extern const int CNIODarwin_IPV6_PKTINFO;
extern const int CNIODarwin_IPV6_DONTFRAG;
int CNIODarwin_sendmmsg(int sockfd, CNIODarwin_mmsghdr *msgvec, unsigned int vlen, int flags);
int CNIODarwin_recvmmsg(int sockfd, CNIODarwin_mmsghdr *msgvec, unsigned int vlen, int flags, struct timespec *timeout);
+1
View File
@@ -90,6 +90,7 @@ const int CNIODarwin_IPTOS_ECN_ECT1 = IPTOS_ECN_ECT1;
const int CNIODarwin_IPTOS_ECN_CE = IPTOS_ECN_CE;
const int CNIODarwin_IPV6_RECVPKTINFO = IPV6_RECVPKTINFO;
const int CNIODarwin_IPV6_PKTINFO = IPV6_PKTINFO;
const int CNIODarwin_IPV6_DONTFRAG = IPV6_DONTFRAG;
const unsigned long CNIODarwin_IOCTL_VM_SOCKETS_GET_LOCAL_CID = IOCTL_VM_SOCKETS_GET_LOCAL_CID;
+51
View File
@@ -102,6 +102,7 @@ private let sysInet_ntop:
inet_ntop
private let sysInet_pton: @convention(c) (CInt, UnsafePointer<CChar>?, UnsafeMutableRawPointer?) -> CInt = inet_pton
#elseif canImport(Darwin)
import CNIODarwin
import Darwin
private let sysInet_ntop:
@@ -407,6 +408,28 @@ extension NIOBSDSocket.Option {
}
}
#if canImport(Darwin)
extension NIOBSDSocket.Option {
/// Set the don't fragment bit on outgoing IPv4 packets.
@inlinable
public static var ip_dontfrag: NIOBSDSocket.Option {
NIOBSDSocket.Option(rawValue: IP_DONTFRAG)
}
}
#endif
#if os(Linux)
extension NIOBSDSocket.Option {
/// Set the path MTU discovery mode for IPv4, one of the `IP_PMTUDISC_*` values.
///
/// - Seealso: `man 7 ip` (Linux)
@inlinable
public static var ip_mtu_discover: NIOBSDSocket.Option {
NIOBSDSocket.Option(rawValue: IP_MTU_DISCOVER)
}
}
#endif
// IPv6 Options
extension NIOBSDSocket.Option {
/// Add an IPv6 group membership.
@@ -447,6 +470,34 @@ extension NIOBSDSocket.Option {
}
}
#if canImport(Darwin) || os(Linux)
extension NIOBSDSocket.Option {
/// Don't fragment outgoing IPv6 packets.
///
/// - Seealso: https://datatracker.ietf.org/doc/html/rfc3542#section-11.2
@inlinable
public static var ipv6_dontfrag: NIOBSDSocket.Option {
#if canImport(Darwin)
NIOBSDSocket.Option(rawValue: CNIODarwin_IPV6_DONTFRAG)
#else
NIOBSDSocket.Option(rawValue: IPV6_DONTFRAG)
#endif
}
}
#endif
#if os(Linux)
extension NIOBSDSocket.Option {
/// Set the path MTU discovery mode for IPv6, one of the `IPV6_PMTUDISC_*` values.
///
/// - Seealso: `man 7 ipv6` (Linux)
@inlinable
public static var ipv6_mtu_discover: NIOBSDSocket.Option {
NIOBSDSocket.Option(rawValue: IPV6_MTU_DISCOVER)
}
}
#endif
// TCP Options
extension NIOBSDSocket.Option {
/// Disables the Nagle algorithm for send coalescing.
@@ -1013,6 +1013,44 @@ class DatagramChannelTests: XCTestCase {
)
}
#if canImport(Darwin)
func testSetGetIPDontFragOption() throws {
let option = ChannelOptions.ipOption(.ip_dontfrag)
try self.firstChannel.setOption(option, value: 1).wait()
XCTAssertEqual(try self.firstChannel.getOption(option).wait(), 1)
}
#endif
#if canImport(Darwin) || os(Linux)
func testSetGetIPv6DontFragOption() throws {
guard System.supportsIPv6 else {
return // need to skip IPv6 tests if we don't support it.
}
let channel = try self.buildChannel(group: self.group, host: "::1")
let option = ChannelOptions.Types.SocketOption(level: .ipv6, name: .ipv6_dontfrag)
try channel.setOption(option, value: 1).wait()
XCTAssertEqual(try channel.getOption(option).wait(), 1)
}
#endif
#if os(Linux)
func testSetGetIPMTUDiscoverOption() throws {
let option = ChannelOptions.ipOption(.ip_mtu_discover)
try self.firstChannel.setOption(option, value: SocketOptionValue(IP_PMTUDISC_PROBE)).wait()
XCTAssertEqual(try self.firstChannel.getOption(option).wait(), SocketOptionValue(IP_PMTUDISC_PROBE))
}
func testSetGetIPv6MTUDiscoverOption() throws {
guard System.supportsIPv6 else {
return // need to skip IPv6 tests if we don't support it.
}
let channel = try self.buildChannel(group: self.group, host: "::1")
let option = ChannelOptions.Types.SocketOption(level: .ipv6, name: .ipv6_mtu_discover)
try channel.setOption(option, value: SocketOptionValue(IPV6_PMTUDISC_PROBE)).wait()
XCTAssertEqual(try channel.getOption(option).wait(), SocketOptionValue(IPV6_PMTUDISC_PROBE))
}
#endif
private func testSimpleReceivePacketInfo(address: String) throws {
// Fake sending packet to self on the loopback interface
let expectedPacketInfo = try constructNIOPacketInfo(address: address)