Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReceiveToBuf #212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
33 changes: 27 additions & 6 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Socket interface {
Send(m Message) error
SendMessages(m []Message) error
Receive() ([]Message, error)
ReceiveToBuf(buf []byte) ([]Message, error)
}

// Dial dials a connection to netlink, using the specified netlink family.
Expand Down Expand Up @@ -135,7 +136,7 @@ func (c *Conn) Execute(m Message) ([]Message, error) {
return nil, err
}

res, err := c.lockedReceive()
res, err := c.lockedReceive(nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -228,14 +229,27 @@ func (c *Conn) Receive() ([]Message, error) {
c.mu.RLock()
defer c.mu.RUnlock()

return c.lockedReceive()
return c.lockedReceive(nil)
}

// ReceiveToBuf receives one or more messages from netlink to buffer. Multi-part messages are
// handled transparently and returned as a single slice of Messages, with the
// final empty "multi-part done" message removed.
//
// If any of the messages indicate a netlink error, that error will be returned.
func (c *Conn) ReceiveToBuf(buf []byte) ([]Message, error) {
// Wait for any concurrent calls to Execute to finish before proceeding.
c.mu.RLock()
defer c.mu.RUnlock()

return c.lockedReceive(buf)
}

// lockedReceive implements Receive, but must be called with c.mu acquired for reading.
// We rely on the kernel to deal with concurrent reads and writes to the netlink
// socket itself.
func (c *Conn) lockedReceive() ([]Message, error) {
msgs, err := c.receive()
func (c *Conn) lockedReceive(buf []byte) ([]Message, error) {
msgs, err := c.receive(buf)
if err != nil {
c.debug(func(d *debugger) {
d.debugf(1, "recv: err: %v", err)
Expand Down Expand Up @@ -266,7 +280,7 @@ func (c *Conn) lockedReceive() ([]Message, error) {

// receive is the internal implementation of Conn.Receive, which can be called
// recursively to handle multi-part messages.
func (c *Conn) receive() ([]Message, error) {
func (c *Conn) receive(buf []byte) ([]Message, error) {
// NB: All non-nil errors returned from this function *must* be of type
// OpError in order to maintain the appropriate contract with callers of
// this package.
Expand All @@ -276,7 +290,14 @@ func (c *Conn) receive() ([]Message, error) {

var res []Message
for {
msgs, err := c.sock.Receive()
var msgs []Message
var err error

if len(buf) == 0 {
msgs, err = c.sock.Receive()
} else {
msgs, err = c.sock.ReceiveToBuf(buf)
}
if err != nil {
return nil, newOpError("receive", err)
}
Expand Down
32 changes: 32 additions & 0 deletions conn_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,38 @@ func (c *conn) Receive() ([]Message, error) {
return msgs, nil
}

// ReceiveToBuf receives one or more Messages from netlink to buffer.
// Buffer should be aligned on a 4-byte boundary.
func (c *conn) ReceiveToBuf(buf []byte) ([]Message, error) {
// Read out all available messages
n, _, _, _, err := c.s.Recvmsg(context.Background(), buf, nil, 0)
if err != nil {
return nil, err
}

alignedLen := nlmsgAlign(n)
if alignedLen > len(buf) {
alignedLen -= nlmsgAlignTo
}

raw, err := syscall.ParseNetlinkMessage(buf[:alignedLen])
if err != nil {
return nil, err
}

msgs := make([]Message, 0, len(raw))
for _, r := range raw {
m := Message{
Header: sysToHeader(r.Header),
Data: r.Data,
}

msgs = append(msgs, m)
}

return msgs, nil
}

// Close closes the connection.
func (c *conn) Close() error { return c.s.Close() }

Expand Down
9 changes: 5 additions & 4 deletions conn_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type conn struct{}
func dial(_ int, _ *Config) (*conn, uint32, error) { return nil, 0, errUnimplemented }
func newError(_ int) error { return errUnimplemented }

func (c *conn) Send(_ Message) error { return errUnimplemented }
func (c *conn) SendMessages(_ []Message) error { return errUnimplemented }
func (c *conn) Receive() ([]Message, error) { return nil, errUnimplemented }
func (c *conn) Close() error { return errUnimplemented }
func (c *conn) Send(_ Message) error { return errUnimplemented }
func (c *conn) SendMessages(_ []Message) error { return errUnimplemented }
func (c *conn) Receive() ([]Message, error) { return nil, errUnimplemented }
func (c *conn) ReceiveToBuf(buf []byte) ([]Message, error) { return nil, errUnimplemented }
func (c *conn) Close() error { return errUnimplemented }
4 changes: 4 additions & 0 deletions nltest/nltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ func (c *socket) Receive() ([]netlink.Message, error) {
return msgs, err
}

func (c *socket) ReceiveToBuf(buf []byte) ([]netlink.Message, error) {
return nil, nil
}

func panicf(format string, a ...interface{}) {
panic(fmt.Sprintf(format, a...))
}