Compare commits

..

1 Commits

Author SHA1 Message Date
Nick Garlis 273b8dc454
Merge dc9df31dfa into 8a8ad2be81 2025-06-09 20:06:33 +02:00
2 changed files with 21 additions and 14 deletions

30
conn.go
View File

@ -250,10 +250,12 @@ func (cc *Conn) Flush() error {
} }
defer func() { _ = closer() }() defer func() { _ = closer() }()
if err = cc.enlargeWriteBuffer(conn); err != nil { err = cc.setWriteBuffer(conn)
if err != nil {
return err return err
} }
if err = cc.enlargeReadBuffer(conn); err != nil { err = cc.setReadBuffer(conn)
if err != nil {
return err return err
} }
@ -435,14 +437,14 @@ func (cc *Conn) allocateTransactionID() uint32 {
func (cc *Conn) getMessageSize() int { func (cc *Conn) getMessageSize() int {
var total int var total int
for _, msg := range cc.messages { for _, msg := range cc.messages {
total += len(msg.Data) + unix.NLMSG_HDRLEN total += len(msg.Data) + 16 // 16 bytes for the header
} }
return total return total
} }
// enlargeWriteBuffer automatically sets the write buffer of the given // setWriteBuffer automatically sets the write buffer of the given connection to
// connection to the accumulated message size. This is only done if the current // the accumulated message size. This is only done if the current write buffer
// write buffer is smaller than the message size. // is smaller than the message size.
// //
// nftables actually handles this differently, it multiplies the number of // nftables actually handles this differently, it multiplies the number of
// iovec entries by 2MB. This is not possible to do here as our underlying // iovec entries by 2MB. This is not possible to do here as our underlying
@ -450,9 +452,9 @@ func (cc *Conn) getMessageSize() int {
// won't expose the number of entries. // won't expose the number of entries.
// https://git.netfilter.org/nftables/tree/src/mnl.c?id=713592c6008a8c589a00d3d3d2e49709ff2de62c#n262 // https://git.netfilter.org/nftables/tree/src/mnl.c?id=713592c6008a8c589a00d3d3d2e49709ff2de62c#n262
// //
// TODO: Update this function to mimic the behavior of nftables once our // TODO: Update this function to mimic the behavior of nftables once those
// socket library supports multiple iovec entries. // limitations are no longer present.
func (cc *Conn) enlargeWriteBuffer(conn *netlink.Conn) error { func (cc *Conn) setWriteBuffer(conn *netlink.Conn) error {
messageSize := cc.getMessageSize() messageSize := cc.getMessageSize()
writeBuffer, err := conn.WriteBuffer() writeBuffer, err := conn.WriteBuffer()
if err != nil { if err != nil {
@ -472,15 +474,19 @@ func (cc *Conn) enlargeWriteBuffer(conn *netlink.Conn) error {
// and https://git.netfilter.org/nftables/tree/src/mnl.c?id=713592c6008a8c589a00d3d3d2e49709ff2de62c#n391 // and https://git.netfilter.org/nftables/tree/src/mnl.c?id=713592c6008a8c589a00d3d3d2e49709ff2de62c#n391
func (cc *Conn) getDefaultEchoReadBuffer() int { func (cc *Conn) getDefaultEchoReadBuffer() int {
pageSize := os.Getpagesize() pageSize := os.Getpagesize()
return max(pageSize, 8192) * 1024 if pageSize < 8192 {
return pageSize * 1024
}
return 8192 * 1024
} }
// enlargeReadBuffer automatically sets the read buffer of the given connection // setReadBuffer automatically sets the read buffer of the given connection
// to the required size. This is only done if the current read buffer is smaller // to the required size. This is only done if the current read buffer is smaller
// than the required size. // than the required size.
// //
// See https://git.netfilter.org/nftables/tree/src/mnl.c?id=713592c6008a8c589a00d3d3d2e49709ff2de62c#n426 // See https://git.netfilter.org/nftables/tree/src/mnl.c?id=713592c6008a8c589a00d3d3d2e49709ff2de62c#n426
func (cc *Conn) enlargeReadBuffer(conn *netlink.Conn) error { func (cc *Conn) setReadBuffer(conn *netlink.Conn) error {
var bufferSize int var bufferSize int
// If there are any messages with the Echo flag, we initialize the buffer size // If there are any messages with the Echo flag, we initialize the buffer size

View File

@ -7413,7 +7413,7 @@ func TestAutoBufferSize(t *testing.T) {
Table: table, Table: table,
}) })
for range 4096 { for i := 0; i < 4096; i++ {
conn.AddRule(&nftables.Rule{ conn.AddRule(&nftables.Rule{
Table: table, Table: table,
Chain: chain, Chain: chain,
@ -7425,7 +7425,8 @@ func TestAutoBufferSize(t *testing.T) {
}) })
} }
if err := conn.Flush(); err != nil { err := conn.Flush()
if err != nil {
t.Fatalf("failed to flush: %v", err) t.Fatalf("failed to flush: %v", err)
} }
} }