Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Loading