Skip to content

Add Socket::(set_)dontfrag_v4 and Socket::(set_)dontfrag_v6 - #679

Open
bhaskargurram-ai wants to merge 2 commits into
rust-lang:masterfrom
bhaskargurram-ai:feat/dontfrag
Open

bhaskargurram-ai wants to merge 2 commits into
rust-lang:masterfrom
bhaskargurram-ai:feat/dontfrag

Conversation

@bhaskargurram-ai

Copy link
Copy Markdown
Contributor

Closes #318.

Adds getters and setters for IP_DONTFRAG (IP_DONTFRAGMENT on Windows) and IPV6_DONTFRAG. These stop outgoing datagrams from being fragmented, which UDP senders need for path MTU discovery (the use case in the issue and in #487).

  • dontfrag_v6/set_dontfrag_v6: Linux, Android, Apple platforms, FreeBSD, DragonFly, NetBSD, OpenBSD, illumos, Solaris and Windows. That covers all tier 1 platforms, so it isn't behind all.
  • dontfrag_v4/set_dontfrag_v4: Apple platforms, FreeBSD, illumos, Solaris and Windows, behind all. Linux has no IP_DONTFRAG. Its equivalent is IP_MTU_DISCOVER with IP_PMTUDISC_DO, which takes a mode rather than a bool, so I left it out.

I took the platform lists from where libc and windows-sys define the constants. The tests set and get both options on UDP sockets, since Windows only accepts IP_DONTFRAGMENT on datagram sockets. cargo check --all-targets, with and without --all-features, passes for x86_64 Linux, macOS, Windows (msvc) and FreeBSD, and clippy, fmt and doc are clean. I couldn't run the new tests here: this machine has no IPv6, and Linux doesn't have the IPv4 option, so CI will be their first run

Sets IP_DONTFRAG (IP_DONTFRAGMENT on Windows) and IPV6_DONTFRAG, so that
UDP senders can do path MTU discovery without the packets being fragmented.

Linux has no IP_DONTFRAG (it uses IP_MTU_DISCOVER instead), so the IPv4
version is behind the all feature.

Closes rust-lang#318
@pederbe

pederbe commented Nov 15, 2026

Copy link
Copy Markdown

I tested these DF APIs for udpstp's Windows transport using a safe Rust probe against 8c4c12921ae12be7c3b316dc0dffee66cddb48b0. I ran the checks on Windows build 26300 x64 with Rust 1.100.0.

I tested IPv4 and IPv6 with connected and unconnected UDP sockets.

Case Observed result
Set and clear DF repeatedly Readback matched every requested value
1700-byte UDP payload, interface MTU 1400, DF on Every send returned WSAEMSGSIZE 10040
Same interface case, DF off Every complete payload arrived
1420-byte UDP payload, interface MTU 1500, routed MTU 1400, DF on The first packet elicited ICMP fragmentation-needed or packet-too-big. Subsequent sends returned 10040
Same routed case, DF off IPv4 router fragmentation worked. IPv6 source fragmentation worked after packet-too-big feedback

I checked packet captures to verify the DF and fragmentation behavior. I ran the tests in a native Windows VM with an isolated Linux router, so these results cover that virtual path and Windows build.

I suggest using the existing Bool type in both getters, as only_v6 does following #655. The Windows helper handles both one-byte and four-byte boolean results. On Unix, Bool is a c_int alias, and this file already imports it.

Windows can return a one-byte boolean for these options; Bool handles
both sizes, as only_v6 already does.
@bhaskargurram-ai

Copy link
Copy Markdown
Contributor Author

I tested these DF APIs for udpstp's Windows transport using a safe Rust probe against 8c4c12921ae12be7c3b316dc0dffee66cddb48b0. I ran the checks on Windows build 26300 x64 with Rust 1.100.0.

I tested IPv4 and IPv6 with connected and unconnected UDP sockets.

Case Observed result
Set and clear DF repeatedly Readback matched every requested value
1700-byte UDP payload, interface MTU 1400, DF on Every send returned WSAEMSGSIZE 10040
Same interface case, DF off Every complete payload arrived
1420-byte UDP payload, interface MTU 1500, routed MTU 1400, DF on The first packet elicited ICMP fragmentation-needed or packet-too-big. Subsequent sends returned 10040
Same routed case, DF off IPv4 router fragmentation worked. IPv6 source fragmentation worked after packet-too-big feedback
I checked packet captures to verify the DF and fragmentation behavior. I ran the tests in a native Windows VM with an isolated Linux router, so these results cover that virtual path and Windows build.

I suggest using the existing Bool type in both getters, as only_v6 does following #655. The Windows helper handles both one-byte and four-byte boolean results. On Unix, Bool is a c_int alias, and this file already imports it.

Thanks a lot for testing this on Windows with packet captures, that's really useful. Good point on Bool: both getters now use it (50c48e1), same as only_v6.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support set IP_DONTFRAG

2 participants