Skip to content

Add socket receive buffer size configuration#53

Closed
DineshI-MS wants to merge 3 commits intoflorianl:mainfrom
DineshI-MS:main
Closed

Add socket receive buffer size configuration#53
DineshI-MS wants to merge 3 commits intoflorianl:mainfrom
DineshI-MS:main

Conversation

@DineshI-MS
Copy link
Copy Markdown

Problem

The library currently doesn't provide a way to set the netlink socket receive buffer size. This causes ENOBUFS errors under moderate to high packet rates (>5k pps) with the default 208KB buffer.

Solution

This PR adds a new SockBufSize field to the Config struct that allows users to set the socket receive buffer size using SO_RCVBUFFORCE (with fallback to SO_RCVBUF).

Changes

  • Added SockBufSize uint32 field to Config struct
  • Modified Open() to apply the socket buffer size if specified
  • Added tests to verify the functionality
  • Updated documentation and examples

Testing

  • Tested with various buffer sizes (0, 512KB, 2MB)
  • Verified no regression when SockBufSize = 0 (default behavior)
  • Confirmed ENOBUFS errors are eliminated with 2MB buffer at 10k+ pps

Backwards Compatibility

This change is fully backwards compatible:

  • The zero value (0) maintains existing behavior
  • No changes required for existing users
  • Only users who need larger buffers need to set this field

Comment thread nflog.go
Comment thread nflog.go
Copy link
Copy Markdown
Owner

@florianl florianl Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DineshI-MS Looking more at this approach. It might be easier to just provide an API to set the receiver buffer:

// SetReadBuffer SetReadBuffer sets the size of the receive buffer associated with this socket.
func (nflog *Nflog) SetReadBuffer(bytes int) error {
	return nflog.Con.SetReadBuffer(bytes)
}

This would redude the complexity of configuration and would also users allow to change it any time. Would you be open to update your PR with such an approach?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for providing this suggestion. I was able to set the buffer size this way without making any additional changes. I wasn’t aware of the API functions that are already part of netlink.Conn

  nf, err := nflog.Open(&config)
   if err != nil {
       return fmt.Errorf("failed to open nflog: %w", err)
   }
   defer nf.Close()

   reqSocketBufferSize := (512 * 1024) // set default as 512 KB

   if err := nf.Con.SetReadBuffer(reqSocketBufferSize); err != nil {
       return fmt.Errorf("failed to set read buffer: %w", err)
   }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DineshI-MS Would you be open to update your PR with this new approach?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As Nflog already exposes *netlink.Conn, it might be best to just improve documentation. I might open a new PR in a few days improving documentation, if this PR becomes stale.

@florianl
Copy link
Copy Markdown
Owner

Closing as resolved with #54.

@florianl florianl closed this Sep 27, 2025
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.

2 participants