Skip to content

Cleanup and refactorings#84

Merged
hulthe merged 7 commits intomainfrom
improvements
Nov 19, 2025
Merged

Cleanup and refactorings#84
hulthe merged 7 commits intomainfrom
improvements

Conversation

@hulthe
Copy link
Contributor

@hulthe hulthe commented Nov 13, 2025

Fixing and refactorings of things I've encountered to hopefully make nftnl better


This change is Reviewable

@hulthe hulthe force-pushed the improvements branch 4 times, most recently from 5554d42 to 562a0a5 Compare November 18, 2025 10:19
@hulthe hulthe marked this pull request as ready for review November 18, 2025 10:19
Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

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

@MarkusPettersson98 reviewed 14 of 24 files at r1, 2 of 3 files at r2, all commit messages.
Reviewable status: 16 of 24 files reviewed, 2 unresolved discussions (waiting on @hulthe)


nftnl/src/table.rs line 27 at r2 (raw file):

    /// Creates a new table instance with the given name and protocol family.
    pub fn new<T: AsRef<CStr>>(name: T, family: ProtoFamily) -> Table {
        let table = unsafe { try_alloc!(sys::nftnl_table_alloc()) };

Very minor nit: The unsafe scope could be kept down by moving it inside of the try_alloc! invocation. This is done in some places already, and not in other places.

Code quote:

let table = unsafe { try_alloc!(sys::nftnl_table_alloc()) };

nftnl/src/expr/counter.rs line 12 at r2 (raw file):

impl Expression for Counter {
    fn to_expr(&self, _rule: &Rule) -> ptr::NonNull<sys::nftnl_expr> {
        try_alloc!(unsafe { sys::nftnl_expr_alloc(c"counter".as_ptr()) })

Like here!

Code quote:

try_alloc!(unsafe { sys::nftnl_expr_alloc(c"counter".as_ptr()) })

Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

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

🙌

@MarkusPettersson98 reviewed 7 of 24 files at r1, 1 of 3 files at r2.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @hulthe)


nftnl/examples/add-rules.rs line 195 at r2 (raw file):

    socket.send_all(batch)?;

    // TODO: this buffer must be aligned to nlmsghdr

TODO

Code quote:

// TODO: this buffer must be aligned to nlmsghdr

Copy link
Contributor Author

@hulthe hulthe left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @MarkusPettersson98 and @Serock3)


nftnl/src/table.rs line 27 at r2 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

Very minor nit: The unsafe scope could be kept down by moving it inside of the try_alloc! invocation. This is done in some places already, and not in other places.

Done

Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

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

@MarkusPettersson98 reviewed 2 of 2 files at r3, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @Serock3)

Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

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

:lgtm:

@MarkusPettersson98 reviewed 5 of 5 files at r4, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @Serock3)

> `NLMSG_ERROR` and `NLMSG_DONE` are of practical importance. They carry
> return codes for operations. Note that unless the NLM_F_ACK flag is
> set on the request Netlink will not respond with NLMSG_ERROR if there
> is no error. To avoid having to special-case this quirk it is
> recommended to always set NLM_F_ACK.

source:
https://www.kernel.org/doc/html/next/userspace-api/netlink/intro.html
This removes the burden about having to worry about null-pointers when
looking at struct/function definitions.
The impl forgot add the trailing null-pointer required for C strings,
and since libnftnl accepts C-strings, is better to just use the CStr
rust types directly.
@hulthe hulthe merged commit bb510d7 into main Nov 19, 2025
8 of 9 checks passed
@hulthe hulthe deleted the improvements branch November 19, 2025 15:05
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.

3 participants