Skip to content

Use nft CLI sizing for the netlink receive buffer - #370

Merged
stapelberg merged 1 commit into
google:mainfrom
North0n:with-message-buffer-size
Nov 18, 2026
Merged

stapelberg merged 1 commit into
google:mainfrom
North0n:with-message-buffer-size

Conversation

@North0n

@North0n North0n commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

This PR exposes netlink's MessageBufferSize through a connection option, leaving the default unchanged. The reason is so users can introduce their own MessageBufferSize. Motivation: 32KiB buffer is recommended for most efficient handling of dumps, such as GetSetElements()

It depends on the dependency update in #358 as MessageBufferSize was introduced in netlink v1.10.0, so I wish we can merge #358 first. Btw newer version of netlink was already shipped and maybe #358 should be updated before merge

@google-cla

google-cla Bot commented Oct 10, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@North0n

North0n commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

@stapelberg @nickgarlis, could you review this connection option? It allows tuning receive buffers for large nftables dumps. It depends on #358, which should use netlink v1.11.2 since v1.11.1 is retracted.

@nickgarlis

Copy link
Copy Markdown
Contributor

Not sure whether this is something that should be configurable by the caller. The nft CLI more or less hard-codes this to the maximum expected size in a system. Here is an example of how I use it with nftables when initializing the connection: https://github.com/nickgarlis/nftnl/blob/main/conn.go#L31

IMO, this is something that should always be on since it makes a big difference in performance especially when listing large sets.

I am happy to update #358 with the latest netlink version and perhaps Go 1.26.

@North0n

North0n commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

If you think that's reasonable default then I agree with that. Update the implementation to always use max expected size of the system

@North0n
North0n force-pushed the with-message-buffer-size branch from 75e905a to 253789e Compare November 17, 2026 07:57
@North0n North0n changed the title Add netlink's MessageBufferSize Use nft CLI sizing for the netlink receive buffer Nov 17, 2026
@North0n

North0n commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

#358 was merged and I've rebased this PR
@nickgarlis can we merge this PR now?

Comment thread conn.go Resolved
Comment thread conn.go Resolved
Comment thread conn.go Outdated
func recvBufSize(pageSize int) int {
const nfgenmsgSize = 4
nftNlmsgMaxsize := math.MaxUint16 + pageSize
nftMnlAckMaxsize := unix.SizeofNlMsghdr + nfgenmsgSize + (1 << 16) + min(pageSize, 8192)

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 we need a shared function for MNL_SOCKET_BUFFER_SIZE since it's reused in getDefaultEchoReadBuffer. Something like the following:

 // mnlSocketBufferSize mirrors libmnl's MNL_SOCKET_BUFFER_SIZE: the kernel's
 // typical netlink datagram size (NLMSG_GOODSIZE), not the netlink socket
 // buffer. Some datagrams are larger, so it is only a building block for nft's sizes.
 //
 // See https://git.netfilter.org/libmnl/tree/include/libmnl/libmnl.h?id=03da98bcd284d55212bc79e91dfb63da0ef7b937#n20
  func mnlSocketBufferSize(pageSize int) int {
        return min(pageSize, 8192)
  }

I also think that getDefaultEchoReadBuffer incorrectly uses max(pageSize, 8192) when it should have been min. It'd be good to verify.

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.

I also think that getDefaultEchoReadBuffer incorrectly uses max(pageSize, 8192) when it should have been min. It'd be good to verify.

According to usages it seems to be true. Added a mnlSocketBufferSize func and used it in both places

@stapelberg

Copy link
Copy Markdown
Collaborator

Aside from @nickgarlis’s open comment, please also run gofmt (currently, CI fails) and squash your commits (with your preferred author name, which currently changes between commits) into 1 commit. Thanks.

@North0n
North0n force-pushed the with-message-buffer-size branch from 05c2fdf to 8931a8e Compare November 18, 2026 09:36
@North0n

North0n commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Aside from @nickgarlis’s open comment, please also run gofmt (currently, CI fails) and squash your commits (with your preferred author name, which currently changes between commits) into 1 commit. Thanks.

done

@stapelberg
stapelberg merged commit 97a5c85 into google:main Nov 18, 2026
7 checks passed
@North0n
North0n deleted the with-message-buffer-size branch November 18, 2026 11:47
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