Skip to content

feat(socket): decode (SOL_SOCKET, SO_MARK) cmsg into SoMark(u32)#2785

Merged
SteveLauC merged 5 commits into
nix-rust:masterfrom
wngr:feat/cmsg-so-mark-decode
May 19, 2026
Merged

feat(socket): decode (SOL_SOCKET, SO_MARK) cmsg into SoMark(u32)#2785
SteveLauC merged 5 commits into
nix-rust:masterfrom
wngr:feat/cmsg-so-mark-decode

Conversation

@wngr
Copy link
Copy Markdown
Contributor

@wngr wngr commented May 6, 2026

What does this PR do

Add a ControlMessageOwned::SoMark(u32) variant and a matching arm in ControlMessageOwned::decode_from, modeled on the existing RxqOvfl(u32) arm a few lines above. Receivers that have enabled SO_RCVMARK (Linux 5.19+, kernel commit
6fd1d51cfa253b5ee7dae18d7cf1df830e9b6137) get an (SOL_SOCKET, SO_MARK) ancillary message on every datagram carrying the kernel-set skb->mark; without this arm the cmsg falls through to the catch-all Unknown variant and forces a per-recv Vec<u8> allocation to copy four bytes.

SO_MARK is in libc for linux_android and fuchsia, but SO_RCVMARK (the option that actually causes the kernel to deliver the cmsg on receive) is Linux/Android-only, so the new arm is gated #[cfg(linux_android)]. Omitting fuchsia seems like the defensive choice.

Let me know if you think this requires additional tests. I validated that behavior locally.

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

@wngr
Copy link
Copy Markdown
Contributor Author

wngr commented May 6, 2026

CI partially failing on clippy complaints seems unrelated to my changes.

Oliver Wangler added 2 commits May 19, 2026 07:46
Add a `ControlMessageOwned::SoMark(u32)` variant and a matching arm in
`ControlMessageOwned::decode_from`, modeled on the existing
`RxqOvfl(u32)` arm a few lines above. Receivers that have enabled
`SO_RCVMARK` (Linux 5.19+, kernel commit
`6fd1d51cfa253b5ee7dae18d7cf1df830e9b6137`) get an `(SOL_SOCKET,
SO_MARK)` ancillary message on every datagram carrying the kernel-set
`skb->mark`; without this arm the cmsg falls through to the catch-all
`Unknown` variant and forces a per-recv `Vec<u8>` allocation to copy
four bytes.

`SO_MARK` is in `libc` for `linux_android` and `fuchsia`, but
`SO_RCVMARK` (the option that actually causes the kernel to deliver
the cmsg on receive) is Linux/Android-only, so the new arm is gated
`#[cfg(linux_android)]`. Omitting `fuchsia` seems like the defensive
choice.
@SteveLauC SteveLauC force-pushed the feat/cmsg-so-mark-decode branch from 3d33ad8 to 46bc4db Compare May 18, 2026 23:46
@SteveLauC
Copy link
Copy Markdown
Member

CI partially failing on clippy complaints seems unrelated to my changes.

I rebased your branch to fix the CI failures.

Copy link
Copy Markdown
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.

Let me know if you think this requires additional tests. I validated that behavior locally.

Yeah, a set-get test would be needed. In order to do that, we should also implement the socket option SO_RCVMARK (pub struct RcvMark).

@SteveLauC
Copy link
Copy Markdown
Member

Use require_capability when you need priviledge.

Linux 5.19+,

Use require_kernel_version to skip the test on older kernels.

Oliver Wangler added 3 commits May 19, 2026 12:36
libc only exposes `SO_RCVMARK` under `unix/linux_like/linux/arch/*`, not under
`unix/linux_like/android/`, so the previous `linux_android` gating broke the
Android build with `cannot find value SO_RCVMARK in crate libc`. Narrow the
`RcvMark` sockopt, the `ControlMessageOwned::SoMark(u32)` variant, the
matching `decode_from` arm, and the `test_recvmsg_so_mark` integration test
to `target_os = "linux"` to match what `libc` actually offers (and what the
existing send-side `Mark` sockopt is gated on).
@SteveLauC
Copy link
Copy Markdown
Member

31
error[E0425]: cannot find value SO_RCVMARK in crate libc
--> src/sys/socket/sockopt.rs:1183:11
|
1183 | libc::SO_RCVMARK,
| ^^^^^^^^^^

Looks like this constant is not defined for android in the libc crate: https://github.com/search?q=repo%3Arust-lang%2Flibc%20SO_RCVMARK&type=code

@wngr
Copy link
Copy Markdown
Contributor Author

wngr commented May 19, 2026

You're fast :-). Yep, only scoped it now to Linux. Now it's symmetrical.

Copy link
Copy Markdown
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.

Thanks

@SteveLauC SteveLauC added this pull request to the merge queue May 19, 2026
Merged via the queue into nix-rust:master with commit fb79966 May 19, 2026
45 checks passed
@wngr wngr deleted the feat/cmsg-so-mark-decode branch May 19, 2026 11:30
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.

2 participants