Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for DSCP and TTL / Hop Limit #2425

Merged
merged 27 commits into from
Jul 22, 2024

Conversation

crisidev
Copy link
Contributor

@crisidev crisidev commented Jun 3, 2024

What does this PR do

This PR improves the support for setting and retrieving of IP headers values for

  • Incoming packets IPv4 TTL (IP_RECVTTL)
  • Incoming packets IPv6 Hop Limit (IPV6_RECVHOPLIMIT)
  • Outgoing and incoming packets IPv4 DSCP (IP_TOS)
  • Outgoing and incoming packets IPv6 DSCP (IPV6_RECVTCLASS)

It includes the socket options and the appropriate control messages for sendmsg and recvmsg.

Note: I am not 100% sure I understand correctly the cfg values and feature flags of nix, so they could be set wrongly in the PR.

Checklist:

  • I have read CONTRIBUTING.md
  • I have written necessary tests and rustdoc comments
  • A change log has been added if this PR modifies nix's API

* Support IP_RECVTTL and IPV6_RECVHOPLIMIT socket options
and related control messages for recvmsg.
* Support setting DSCP in control messages for both sendmsg
and recvmsg.

Signed-off-by: Bigo <1781140+crisidev@users.noreply.github.com>
@crisidev crisidev marked this pull request as ready for review June 3, 2024 17:45
@crisidev crisidev marked this pull request as draft June 3, 2024 18:07
@crisidev crisidev marked this pull request as ready for review June 4, 2024 14:55
src/sys/socket/sockopt.rs Outdated Show resolved Hide resolved
src/sys/socket/sockopt.rs Outdated Show resolved Hide resolved
src/sys/socket/sockopt.rs Outdated Show resolved Hide resolved
src/sys/socket/sockopt.rs Outdated Show resolved Hide resolved
src/sys/socket/mod.rs Show resolved Hide resolved
src/sys/socket/mod.rs Outdated Show resolved Hide resolved
src/sys/socket/mod.rs Outdated Show resolved Hide resolved
src/sys/socket/mod.rs Show resolved Hide resolved
test/sys/test_socket.rs Show resolved Hide resolved
test/sys/test_socket.rs Outdated Show resolved Hide resolved
@SteveLauC
Copy link
Member

SteveLauC commented Jun 10, 2024

Hi, thanks for the PR! Sorry for the late response!

Note: I am not 100% sure I understand correctly the cfg values and feature flags of nix, so they could be set wrongly in the PR.

For the cfg for targets (OSes), see

nix/build.rs

Lines 20 to 27 in 5fde28e

// cfg aliases we would like to use
apple_targets: { any(ios, macos, watchos, tvos, visionos) },
bsd: { any(freebsd, dragonfly, netbsd, openbsd, apple_targets) },
bsd_without_apple: { any(freebsd, dragonfly, netbsd, openbsd) },
linux_android: { any(android, linux) },
freebsdlike: { any(dragonfly, freebsd) },
netbsdlike: { any(netbsd, openbsd) },
solarish: { any(illumos, solaris) },

@crisidev
Copy link
Contributor Author

Hi, thanks for the PR! Sorry for the late response!

Thanks a lot for the review and don't worry about the timing :)

I'll address the comments and publish a new version!

Note: I am not 100% sure I understand correctly the cfg values and feature flags of nix, so they could be set wrongly in the PR.

For the cfg for targets (OSes), see

nix/build.rs

Lines 20 to 27 in 5fde28e

// cfg aliases we would like to use
apple_targets: { any(ios, macos, watchos, tvos, visionos) },
bsd: { any(freebsd, dragonfly, netbsd, openbsd, apple_targets) },
bsd_without_apple: { any(freebsd, dragonfly, netbsd, openbsd) },
linux_android: { any(android, linux) },
freebsdlike: { any(dragonfly, freebsd) },
netbsdlike: { any(netbsd, openbsd) },
solarish: { any(illumos, solaris) },

crisidev and others added 3 commits June 14, 2024 12:54
@crisidev
Copy link
Contributor Author

The code we have right now does not build on freebsd 14 because

error[E0531]: cannot find unit struct, unit variant or constant `IP_RECVTTL` in crate `libc`
    --> src/sys/socket/mod.rs:1018:38
     |
1018 |             (libc::IPPROTO_IP, libc::IP_RECVTTL) => {
     |                                      ^^^^^^^^^^ help: a constant with a similar name exists: `IP_RECVTOS`
     |
    ::: /.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/libc-0.2.155/src/unix/bsd/freebsdlike/freebsd/mod.rs:3685:1
     |
3685 | pub const IP_RECVTOS: ::c_int = 68;
     | ----------------------------- similarly named constant `IP_RECVTOS` defined here

I think we need to enable RECVTTL only on linux.

@SteveLauC
Copy link
Member

SteveLauC commented Jun 15, 2024

The code we have right now does not build on freebsd 14 because

error[E0531]: cannot find unit struct, unit variant or constant `IP_RECVTTL` in crate `libc`
    --> src/sys/socket/mod.rs:1018:38
     |
1018 |             (libc::IPPROTO_IP, libc::IP_RECVTTL) => {
     |                                      ^^^^^^^^^^ help: a constant with a similar name exists: `IP_RECVTOS`
     |
    ::: /.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/libc-0.2.155/src/unix/bsd/freebsdlike/freebsd/mod.rs:3685:1
     |
3685 | pub const IP_RECVTOS: ::c_int = 68;
     | ----------------------------- similarly named constant `IP_RECVTOS` defined here

I think we need to enable RECVTTL only on linux.

On FreeBSD, IP_RECVTTL should be used, it is available on FreeBSD, our CI says it does not exist because the Rust libc crate does not include it, we need to add this constant to the libc crate.

Update: PR filed rust-lang/libc#3750

Update: PR merged, we can use the libc from git now:

libc = { git = "https://github.com/rust-lang/libc", branch = "libc-0.2", features = ["extra_traits"] }

src/sys/socket/sockopt.rs Outdated Show resolved Hide resolved
src/sys/socket/mod.rs Show resolved Hide resolved
src/sys/socket/mod.rs Show resolved Hide resolved
src/sys/socket/mod.rs Outdated Show resolved Hide resolved
@crisidev
Copy link
Contributor Author

I am moving this back to draft, while I find some time to finish to implement the changes and fix all the tests.

@crisidev crisidev marked this pull request as draft June 17, 2024 11:50
@SteveLauC
Copy link
Member

HI @crisidev, would you like to finish this? That type thing should be easy to deal with as we know the type used by the OS kernel:)

@crisidev
Copy link
Contributor Author

Hey, sorry, life got in the middle.. I'd love to finish this, but I am finally on holidays for a couple of weeks. I will be able to restart working on this in the second week of August if that's ok..

@SteveLauC
Copy link
Member

SteveLauC commented Jul 20, 2024

but I am finally on holidays for a couple of weeks. I will be able to restart working on this in the second week of August if that's ok..

Yeah, that is totally fine, enjoy you holiday, we all have a life😁

@crisidev
Copy link
Contributor Author

but I am finally on holidays for a couple of weeks. I will be able to restart working on this in the second week of August if that's ok..

Yeah, that is totally fine, enjoy you holiday, we all have a life😁

I found some time during a lazy Sunday and I am trying to finish this 😄

CI is making my life very hard right now, I'll try to fix it and move this to non draft.

@crisidev
Copy link
Contributor Author

I think I am done fixing CI issues. I had to disable the tests for IPTOS / IPV6TCLASS on mips since they return bogus values and I don't have a good way to troubleshoot it.

@crisidev crisidev marked this pull request as ready for review July 21, 2024 15:59
@crisidev
Copy link
Contributor Author

@SteveLauC first of all, thanks for all the patient comments, I really appreciate the help. I think this is ready for review now!

Copy link
Member

@SteveLauC SteveLauC left a comment

Choose a reason for hiding this comment

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

Well, sorry for so many comments, I hope they won't affect your holiday:)

Most of them are trivial cfg issues, so should be easy to fix

src/sys/socket/mod.rs Outdated Show resolved Hide resolved
src/sys/socket/mod.rs Outdated Show resolved Hide resolved
src/sys/socket/mod.rs Outdated Show resolved Hide resolved
src/sys/socket/mod.rs Outdated Show resolved Hide resolved
src/sys/socket/mod.rs Show resolved Hide resolved
test/sys/test_sockopt.rs Outdated Show resolved Hide resolved
test/sys/test_sockopt.rs Outdated Show resolved Hide resolved
test/sys/test_sockopt.rs Outdated Show resolved Hide resolved
test/sys/test_sockopt.rs Outdated Show resolved Hide resolved
test/sys/test_socket.rs Outdated Show resolved Hide resolved
@crisidev
Copy link
Contributor Author

Well, sorry for so many comments, I hope they won't affect your holiday:)

Most of them are trivial cfg issues, so should be easy to fix

Please don't you worry about this. I am quite new to this crate and without your help, I would be able to make a meaningful contribution. At the end, you did most of the work, I am really glad this is moving forward thanks to your help.

I'll try to fix everything today, I think it start to look good, apologies for the mess I did with cfg flags.

@crisidev
Copy link
Contributor Author

I think I could use another review now :)

Copy link
Member

@SteveLauC SteveLauC left a comment

Choose a reason for hiding this comment

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

Just one little thing, let me commit this change:)

test/sys/test_socket.rs Outdated Show resolved Hide resolved
@SteveLauC SteveLauC added this pull request to the merge queue Jul 22, 2024
Merged via the queue into nix-rust:master with commit 25b437c Jul 22, 2024
36 checks passed
@SteveLauC
Copy link
Member

Thanks!

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.

None yet

2 participants