Expose accumulated message size

AFAIU, netlink has a message size limit of ~32KB.

This means that a single transaction would be limited to a few
operations. Going over the limit would result in the following error
after flushing:

netlink receive: recvmsg: no buffer space available

If I am not mistaken, the only alternative is to then divide big
transactions into smaller chunks. The only issue is that it's hard
to tell when the limit has been exceeded.

That's why I suggest exposing a function that would produce the
accumulated message size since the messages slice is private.

- https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netlink/af_netlink.c?id=1e26c5e28ca5821a824e90dd359556f5e9e7b89f#n1930
- https://web.git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git/commit/?id=d35c99ff77ecb2eb239731b799386f3b3637a31e
This commit is contained in:
nickgarlis 2025-03-26 17:46:24 +01:00
parent 8095c51678
commit 4c1b4e9a74
1 changed files with 16 additions and 0 deletions

16
conn.go
View File

@ -329,6 +329,22 @@ func (cc *Conn) FlushRuleset() {
}) })
} }
// GetMessageSize returns the total size of all messages in the buffer.
// This is useful for making sure that the messages will not exceed the limits
// of the netlink buffer which is 32768 + ~320 bytes. See also
// https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netlink/af_netlink.c?id=1e26c5e28ca5821a824e90dd359556f5e9e7b89f#n1930
// and
// https://web.git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git/commit/?id=d35c99ff77ecb2eb239731b799386f3b3637a31e
func (cc *Conn) GetMessageSize() uint32 {
cc.mu.Lock()
defer cc.mu.Unlock()
var total uint32
for _, msg := range cc.messages {
total += uint32(len(msg.Data)) + 16 // 16 bytes for the header
}
return total
}
func (cc *Conn) dialNetlink() (*netlink.Conn, error) { func (cc *Conn) dialNetlink() (*netlink.Conn, error) {
var ( var (
conn *netlink.Conn conn *netlink.Conn