Skip to content

freebsd(13.2): add netlink/netlink.h support - #5326

Open
dybucc wants to merge 3 commits into
rust-lang:mainfrom
dybucc:freebsd-netlink
Open

freebsd(13.2): add netlink/netlink.h support#5326
dybucc wants to merge 3 commits into
rust-lang:mainfrom
dybucc:freebsd-netlink

Conversation

@dybucc

@dybucc dybucc commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Description

This PR updates #3201 with merge conflicts resolved and follows the new plan at
1.

The patch adds support for netlink.h interfaces in OpenBSD, where there's an
item resolution conflict if we expose the Rust bindings alongside those of
if_mib.h. This set of APIs is "scoped" in C because they live on separate
headers. In rust-lang/libc, we reexport all items at the root crate level, which
makes item resolution fail.

Note this depends on #5325. It won't pass tests but it will build. This is
because the test templates will gather all items in a single file, so item
resolution fails. We can't really skip these items altogether from the tests, so
it may just be necessary to extend ctest to allow skipping module-specific
Rust items.

Checklist

  • Relevant tests in libc-test/semver have been updated
  • Commit messages permalink to headers for added or changed API
  • Placeholder or unstable values like *LAST or *MAX have the standard
    doc comment
  • Tested locally (cargo test -p libc-test --target mytarget); especially
    relevant for platforms that may not be checked in CI

@rustbot label +stable-nominated

Footnotes

  1. https://github.com/rust-lang/libc/pull/3201#issuecomment-4736374182

@rustbot rustbot added O-bsd O-freebsd S-waiting-on-review stable-nominated This PR should be considered for cherry-pick to libc's stable release branch labels Jul 24, 2026
@dybucc dybucc mentioned this pull request Jul 24, 2026
2 tasks
@dybucc
dybucc force-pushed the freebsd-netlink branch from f53fbf1 to c7886ff Compare July 27, 2026 06:28
@rustbot rustbot added the A-CI Area: CI-related items label Jul 27, 2026
@rustbot

This comment has been minimized.

@dybucc
dybucc force-pushed the freebsd-netlink branch 3 times, most recently from 653f197 to 0b6bb29 Compare July 29, 2026 07:34
@rustbot

This comment has been minimized.

@tgross35

Copy link
Copy Markdown
Contributor

The API looks fine from a quick skim, but since there is no hurry, I think it may be worth trying to add support to ctest first so the tricky test setup isn't needed. (It's useful otherwise too.)

Sketched some of that up at #5344

@dybucc

dybucc commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Noted. Since you already pinged some contributor on that issue, I'll wait and
see whether they want to implement it. Otherwise, I'll do it.

@tgross35

tgross35 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Could you try adding a separate TestGenerator instance like @mbyx recommended in that issue? Since it's split off, that should avoid the need to reexport libc::netlink at test root too.

@tgross35

tgross35 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Either author or blocked, depending on whether that works.

@rustbot author

@rustbot

rustbot commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

Comment thread libc-test/build/main.rs Outdated
Comment on lines +2530 to +2539
!matches!(
c.ident(),
"CTRL_CMD_UNSPEC"
| "CTRL_CMD_NEWFAMILY"
| "CTRL_CMD_DELFAMILY"
| "CTRL_CMD_GETFAMILY"
| "CTRL_CMD_NEWOPS"
| "CTRL_CMD_DELOPS"
| "CTRL_CMD_GETOPS"
| "CTRL_CMD_NEWMCAST_GRP"

@tgross35 tgross35 Aug 10, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These could be put into functions like is_netlink_const(c: &Const) -> bool so the list can be shared the two places it's needed.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread libc-test/build/main.rs Outdated
let freebsd14 = matches!(freebsd_ver, Some(n) if n >= 14);
let freebsd15 = matches!(freebsd_ver, Some(n) if n >= 15);

if let Some(net_header) = net_header {

@tgross35 tgross35 Aug 10, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you can drop the net_header arg, one invocation of test_freebsd can create all 2-3 ctest objects. I assume you're doing this to share the setup above, so I applied 023c0a3 to make that cfg a bit easier to share.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. I didn't actually mean that. That was just cruft from prior attempts with
a different approach.

Comment thread src/new/freebsd/netlink/mod.rs Outdated
@@ -0,0 +1,6 @@
//! Directory: `netlink/`
//!
//! <https://github.com/freebsd/freebsd-src/tree/df9d6403caa6426e92f5e100602f4d2be474bbae/sys/netlink>

@tgross35 tgross35 Aug 10, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fyi, no need to permalink these kind of files since we do want to see updates, and the file locations are usually stable enough. As long as there's a permalink in the commit message to show us what it actually looked like at that point in time.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You mean the files for the upstream directories only, or also the files
referring to the upstream header files?

@dybucc

dybucc commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

It seems having separate invocations gets the job done just fine. Still, that
was the same thing I tried in the initial patch. The added complexity comes when
we need to get the SemVer tests to pass.

Those seem to me like they're going to need extending (though it's notably less
work than with ctest as it's all in libc-test's build script.)

@rustbot

This comment has been minimized.

@tgross35

Copy link
Copy Markdown
Contributor

I haven't tried this but do things just work™️ if the semver tests have lines like netlink::NETLINK_ADD_MEMBERSHIP?

@rustbot

This comment has been minimized.

@dybucc dybucc changed the title freebsd(13.2): add net/netlink.h support freebsd(13.2): add netlink/netlink.h support Aug 11, 2026
@rustbot

This comment has been minimized.

@dybucc

dybucc commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

I haven't tried this but do things just work™️ if the semver tests have lines
like netlink::NETLINK_ADD_MEMBERSHIP?

@tgross35 They do. Just cleaned up commit history as all tests seem to pass now.

@dybucc
dybucc force-pushed the freebsd-netlink branch 2 times, most recently from 2b52cf7 to 30133e0 Compare August 11, 2026 07:19
Comment thread src/new/mod.rs Outdated
Comment on lines +68 to +75
pub(crate) use freebsd::*;
// FIXME(1.0,remove): glob reexport should be the default.
pub use freebsd::netlink;
pub(crate) use freebsd::{
net,
netinet6,
sys,
unistd,
};

@tgross35 tgross35 Aug 11, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this work?

pub(crate) use freebsd::*;
pub use netlink;

View changes since the review

Comment on lines +7 to +33
pub const CTRL_CMD_UNSPEC: c_int = 0;
pub const CTRL_CMD_NEWFAMILY: c_int = 1;
pub const CTRL_CMD_DELFAMILY: c_int = 2;
pub const CTRL_CMD_GETFAMILY: c_int = 3;
pub const CTRL_CMD_NEWOPS: c_int = 4;
pub const CTRL_CMD_DELOPS: c_int = 5;
pub const CTRL_CMD_GETOPS: c_int = 6;
pub const CTRL_CMD_NEWMCAST_GRP: c_int = 7;
pub const CTRL_CMD_DELMCAST_GRP: c_int = 8;
pub const CTRL_CMD_GETMCAST_GRP: c_int = 9;
pub const CTRL_CMD_GETPOLICY: c_int = 10;

pub const CTRL_ATTR_UNSPEC: c_int = 0;
pub const CTRL_ATTR_FAMILY_ID: c_int = 1;
pub const CTRL_ATTR_FAMILY_NAME: c_int = 2;
pub const CTRL_ATTR_VERSION: c_int = 3;
pub const CTRL_ATTR_HDRSIZE: c_int = 4;
pub const CTRL_ATTR_MAXATTR: c_int = 5;
pub const CTRL_ATTR_OPS: c_int = 6;
pub const CTRL_ATTR_MCAST_GROUPS: c_int = 7;
pub const CTRL_ATTR_POLICY: c_int = 8;
pub const CTRL_ATTR_OP_POLICY: c_int = 9;
pub const CTRL_ATTR_OP: c_int = 10;

pub const CTRL_ATTR_MCAST_GRP_UNSPEC: c_int = 0;
pub const CTRL_ATTR_MCAST_GRP_NAME: c_int = 1;
pub const CTRL_ATTR_MCAST_GRP_ID: c_int = 2;

@tgross35 tgross35 Aug 11, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think these aren't in the semver file

View changes since the review

Comment thread libc-test/semver/freebsd.txt Outdated
Comment on lines +2175 to +2227
netlink::netlink::NETLINK_ADD_MEMBERSHIP
netlink::netlink::NETLINK_AUDIT
netlink::netlink::NETLINK_BROADCAST_ERROR
netlink::netlink::NETLINK_CAP_ACK
netlink::netlink::NETLINK_CONNECTOR
netlink::netlink::NETLINK_DNRTMSG
netlink::netlink::NETLINK_DROP_MEMBERSHIP
netlink::netlink::NETLINK_EXT_ACK
netlink::netlink::NETLINK_FIB_LOOKUP
netlink::netlink::NETLINK_FIREWALL
netlink::netlink::NETLINK_GENERIC
netlink::netlink::NETLINK_GET_STRICT_CHK
netlink::netlink::NETLINK_IP6_FW
netlink::netlink::NETLINK_ISCSI
netlink::netlink::NETLINK_KOBJECT_UEVENT
netlink::netlink::NETLINK_LISTEN_ALL_NSID
netlink::netlink::NETLINK_LIST_MEMBERSHIPS
netlink::netlink::NETLINK_NETFILTER
netlink::netlink::NETLINK_NFLOG
netlink::netlink::NETLINK_NO_ENOBUFS
netlink::netlink::NETLINK_PKTINFO
netlink::netlink::NETLINK_ROUTE
netlink::netlink::NETLINK_RX_RING
netlink::netlink::NETLINK_SELINUX
netlink::netlink::NETLINK_SOCK_DIAG
netlink::netlink::NETLINK_TX_RING
netlink::netlink::NETLINK_UNUSED
netlink::netlink::NETLINK_USERSOCK
netlink::netlink::NETLINK_XFRM
netlink::netlink::NLMSG_ALIGNTO
netlink::netlink::NLMSG_DONE
netlink::netlink::NLMSG_ERROR
netlink::netlink::NLMSG_NOOP
netlink::netlink::NLMSG_OVERRUN
netlink::netlink::NLM_F_ACK
netlink::netlink::NLM_F_ACK_TLVS
netlink::netlink::NLM_F_APPEND
netlink::netlink::NLM_F_ATOMIC
netlink::netlink::NLM_F_CAPPED
netlink::netlink::NLM_F_CREATE
netlink::netlink::NLM_F_DUMP
netlink::netlink::NLM_F_DUMP_FILTERED
netlink::netlink::NLM_F_DUMP_INTR
netlink::netlink::NLM_F_ECHO
netlink::netlink::NLM_F_EXCL
netlink::netlink::NLM_F_MATCH
netlink::netlink::NLM_F_MULTI
netlink::netlink::NLM_F_NONREC
netlink::netlink::NLM_F_REPLACE
netlink::netlink::NLM_F_REQUEST
netlink::netlink::NLM_F_ROOT
netlink::netlink::NL_ITEM_ALIGN_SIZE
netlink::netlink::SOL_NETLINK

@tgross35 tgross35 Aug 11, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be better if we can keep all this directly within a netlink module. In the exports, perhaps an in-file mod netlink { pub use ... }

View changes since the review

Comment thread libc-test/build/main.rs
Comment on lines +2609 to +2629
let mut netlink_cfg = cfg.clone();
headers!(netlink_cfg, "netlink/netlink.h",);
netlink_cfg
.skip_struct(|ty| !matches!(ty.ident(), "sockaddr_nl"))
.skip_const(|c| !is_netlink_const(c))
.skip_union(|_| true)
.skip_alias(|_| true)
.skip_static(|_| true)
.skip_fn(|_| true)
.skip_c_enum(|_| true);
ctest::generate_test(&mut netlink_cfg, "../src/lib.rs", "netlink_ctest_output.rs").unwrap();

// We must restore the above sure-skips because `TestGenerator` shares skips
// between cloned instances (here `cfg` and `netlink_cfg`.)
cfg.skip_struct(|_| false)
.skip_const(|_| false)
.skip_union(|_| false)
.skip_alias(|_| false)
.skip_static(|_| false)
.skip_fn(|_| false)
.skip_c_enum(|_| false);

@tgross35 tgross35 Aug 11, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sharing skips isn't intentional, why does this happen?

View changes since the review

Comment thread libc-test/build/main.rs
Comment on lines +3103 to +3105
// This is tested in a separate ctest invocation because some symbols
// `netlink/netlink.h` conflict with some other symbols from `net/if_mib.h`.
"sockaddr_nl" => true,

@tgross35 tgross35 Aug 11, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this one needed still? I think we should be checking a sockaddr_nl in each ctest invocation

View changes since the review

@rustbot

rustbot commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

dybucc and others added 3 commits August 11, 2026 19:29
This is an early subset of the Netlink interface, but it proves
sufficient for monitoring changes in IP addresses. Coverage can be
extended later as needed.  See [^1] and [^2].

[^1]: <https://github.com/freebsd/freebsd-src/blob/df9d6403caa6426e92f5e100602f4d2be474bbae/sys/netlink/netlink.h>
[^2]: <https://github.com/freebsd/freebsd-src/blob/df9d6403caa6426e92f5e100602f4d2be474bbae/sys/netlink/netlink_generic.h>

A small workaround has been necessary in the SemVer tests to ensure we
get the right paths to the public submodules for the `netlink/netlink.h`
interfaces. Those symbols now are also prepended a `netlink::netlink`.

Signed-off-by: Yann Dirson <yann.dirson@vates.fr>

Co-authored-by: Yann Dirson <yann.dirson@vates.fr>
Add specific test for `netlink/netlink.h` bindings. This is necessary to
avoid conflicts with the bindings for `net/if_mib.h`. libc-test now
builds two different `TestGenerator` instances.

One of the instances builds tests for the same set of bindings as before
this patchset, while the other builds tests only for the
`netlink/netlink.h` bindings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-CI Area: CI-related items O-bsd O-freebsd S-waiting-on-author stable-nominated This PR should be considered for cherry-pick to libc's stable release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants