From 8931a8e4eaae9911660ebe6cf414cf7f08022671 Mon Sep 17 00:00:00 2001 From: Ivan Mautin Date: Sat, 10 Oct 2026 19:00:26 +0300 Subject: [PATCH] Use nft CLI sizing for the netlink receive buffer --- conn.go | 29 ++++++++++++++++++++++++-- conn_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 conn_test.go diff --git a/conn.go b/conn.go index c986c13..2e7d4b9 100644 --- a/conn.go +++ b/conn.go @@ -437,6 +437,28 @@ func (cc *Conn) FlushRuleset() { }) } +// 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) +} + +// recvBufSize returns the userspace receive buffer size for the netlink connection. +// There are different restrictions for different messages in nft CLI: +// NFT_NLMSG_MAXSIZE for dump: https://git.netfilter.org/nftables/tree/src/mnl.c?h=v1.1.6#n87 +// NFT_MNL_ACK_MAXSIZE for acknowledgements and errors: https://git.netfilter.org/nftables/tree/src/mnl.c?h=v1.1.6#n394 +// We use the larger size to accommodate both dumps and batch replies, +// since dialNetlink does not distinguish between these operations. +func recvBufSize(pageSize int) int { + const nfgenmsgSize = 4 + nftNlmsgMaxsize := math.MaxUint16 + pageSize + nftMnlAckMaxsize := unix.SizeofNlMsghdr + nfgenmsgSize + (1 << 16) + mnlSocketBufferSize(pageSize) + return max(nftNlmsgMaxsize, nftMnlAckMaxsize) +} + func (cc *Conn) dialNetlink() (*netlink.Conn, error) { var ( conn *netlink.Conn @@ -446,7 +468,10 @@ func (cc *Conn) dialNetlink() (*netlink.Conn, error) { if cc.TestDial != nil { conn = nltest.Dial(cc.TestDial) } else { - conn, err = netlink.Dial(unix.NETLINK_NETFILTER, &netlink.Config{NetNS: cc.NetNS}) + conn, err = netlink.Dial(unix.NETLINK_NETFILTER, &netlink.Config{ + NetNS: cc.NetNS, + MessageBufferSize: recvBufSize(os.Getpagesize()), + }) } if err != nil { @@ -609,7 +634,7 @@ func (cc *Conn) enlargeWriteBuffer(conn *netlink.Conn) error { // and https://git.netfilter.org/nftables/tree/src/mnl.c?id=713592c6008a8c589a00d3d3d2e49709ff2de62c#n391 func (cc *Conn) getDefaultEchoReadBuffer() int { pageSize := os.Getpagesize() - return max(pageSize, 8192) * 1024 + return mnlSocketBufferSize(pageSize) * 1024 } // enlargeReadBuffer automatically sets the read buffer of the given connection diff --git a/conn_test.go b/conn_test.go new file mode 100644 index 0000000..824dafe --- /dev/null +++ b/conn_test.go @@ -0,0 +1,57 @@ +// Copyright 2026 Google LLC. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package nftables + +import "testing" + +func TestMnlSocketBufferSize(t *testing.T) { + for _, tt := range []struct { + name string + pageSize int + want int + }{ + {name: "4KiB", pageSize: 4096, want: 4096}, + {name: "8KiB", pageSize: 8192, want: 8192}, + {name: "16KiB", pageSize: 16384, want: 8192}, + {name: "64KiB", pageSize: 65536, want: 8192}, + } { + t.Run(tt.name, func(t *testing.T) { + if got := mnlSocketBufferSize(tt.pageSize); got != tt.want { + t.Errorf("mnlSocketBufferSize(%d) = %d, want %d", tt.pageSize, got, tt.want) + } + }) + } +} + +func TestRecvBufSize(t *testing.T) { + for _, tt := range []struct { + name string + pageSize int + want int + }{ + // The ACK buffer is larger for small pages. + {name: "4KiB", pageSize: 4096, want: 69652}, + {name: "8KiB", pageSize: 8192, want: 73748}, + // The dump buffer is larger for large pages. + {name: "16KiB", pageSize: 16384, want: 81919}, + {name: "64KiB", pageSize: 65536, want: 131071}, + } { + t.Run(tt.name, func(t *testing.T) { + if got := recvBufSize(tt.pageSize); got != tt.want { + t.Errorf("recvBufSize(%d) = %d, want %d", tt.pageSize, got, tt.want) + } + }) + } +}