Skip to content

net: add support for TCLASS option on IPv6#7781

Merged
ADD-SP merged 4 commits intotokio-rs:masterfrom
jtojnar:ipv6-tos
Dec 29, 2025
Merged

net: add support for TCLASS option on IPv6#7781
ADD-SP merged 4 commits intotokio-rs:masterfrom
jtojnar:ipv6-tos

Conversation

@jtojnar
Copy link
Contributor

@jtojnar jtojnar commented Dec 19, 2025

  • Rename {Udp,Tcp}Socket::tos to tos_v4 as it is IPv4-specific, following the rename from socket2 0.6.0.
  • Add {Tcp,Udp}Socket::tclass_v6 as its IPv6 equivalent.
  • Fix affect/effect typos.

Motivation

I want to be able to set IPv6 Traffic Class field as conveniently as the corresponding IPv4 Type of Service field, which allows setting the Differentiated Services subfield necessary for Quality of Service requirements.

Solution

Just addding an extra getter/setter exposing the underlying API.

@ADD-SP ADD-SP added A-tokio Area: The main tokio crate M-net Module: tokio/net labels Dec 19, 2025
@Darksonn
Copy link
Member

We can't rename functions.

@jtojnar
Copy link
Contributor Author

jtojnar commented Dec 19, 2025

Not even when keeping the original function as deprecated? I followed the example of 28c9a14.

I can drop the rename and keep the comment change, if the policy changed.

Copy link
Member

@martin-g martin-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed changes look good to me!
The semver CI check also does not find any issues.

I'd suggest to add some tests though!

@jtojnar
Copy link
Contributor Author

jtojnar commented Dec 19, 2025

I'd suggest to add some tests though!

I do not see any setsockopt tests in https://github.com/tokio-rs/tokio/blob/master/tokio/tests/udp.rs

I guess I could check if getter returns the value passed to a setter like the underlying library does but not sure if it is worth it duplicating it here.

@martin-g
Copy link
Member

not sure if it is worth it duplicating it here.

I think it is's worth it. If Tokio's setter gets broken somehow and stops delegating to the underlying library then the getter will return the wrong value.
Same for the getters and the deprecated functions.

I understand that it is an effort! You could wait for others' opinion on the current changes first! And I could help with the tests if you like !

Copy link
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a simple getter/setter test should be enough.

@ADD-SP ADD-SP added the S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. label Dec 21, 2025
@ADD-SP
Copy link
Member

ADD-SP commented Dec 25, 2025

Hi @jtojnar , could you add a simple getter/setter test?

@jtojnar jtojnar changed the title net: Add support for DiffServ on IPv6 net: Add support for TCLASS option on IPv6 Dec 28, 2025
@jtojnar jtojnar force-pushed the ipv6-tos branch 3 times, most recently from 76aec40 to c03efa5 Compare December 28, 2025 07:20
@jtojnar
Copy link
Contributor Author

jtojnar commented Dec 28, 2025

Added option test for all existing options, mirroring the tests in socket2 library.

For some reason multicast_ttl_v4 fails on CI for Windows (in setsockopt) and on MacOS in getsockopt) so made those conditional.


#[cfg(not(target_os = "windows"))]
// failed to set option: Os { code: 10022, kind: InvalidInput, message: "An invalid argument was supplied." }
#[cfg(not(target_os = "macos"))] // failed to get initial value: Os { code: 22, kind: InvalidInput, message: "Invalid argument" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@jtojnar jtojnar Dec 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weirdly, it fails even on Linux on CI for socket2 library so not sure if I should even include it here. Edit: Never mind, that was because I accidentally used SOCK_STREAM instead of SOCK_DGRAM in tests. Now socket2 is all green, even without using c_uchar, which is unexpected the other way around.

@jtojnar jtojnar force-pushed the ipv6-tos branch 2 times, most recently from 8779a0b to 1656a28 Compare December 28, 2025 08:58
@ADD-SP ADD-SP removed the S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. label Dec 28, 2025
jtojnar and others added 3 commits December 28, 2025 16:18
It is IPv4-specific. Follow the rename from socket2 0.6.0.
The macro is based on the one from socket2 library:
https://github.com/rust-lang/socket2/blob/a18be6a302b7f9c127c3593edec5d8d2690839a7/tests/socket.rs#L1345

Co-authored-by: Thomas de Zeeuw <thomasdezeeuw@gmail.com>
This allows setting `Traffic Class` field, which is the equivalent of IPv4 `Type of Service` field: https://man.openbsd.org/ip6#IPV6_TCLASS

Not supported on Windows: https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-ipv6-socket-options
Copy link
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me, just two minor questions.

@ADD-SP ADD-SP added S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. and removed S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. labels Dec 29, 2025
@ADD-SP ADD-SP changed the title net: Add support for TCLASS option on IPv6 net: add support for TCLASS option on IPv6 Dec 29, 2025
Copy link
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ADD-SP ADD-SP merged commit 7388f2d into tokio-rs:master Dec 29, 2025
93 checks passed
@jtojnar jtojnar deleted the ipv6-tos branch December 29, 2025 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio Area: The main tokio crate M-net Module: tokio/net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants