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

Optimizations #597

Open
wants to merge 2 commits 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
54 changes: 26 additions & 28 deletions nl/nl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,46 +362,44 @@ func (a *RtAttr) Serialize() []byte {

type NetlinkRequest struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is an exported type. Other project may already be using it and the members you are removing are unfortunately exported as well.

unix.NlMsghdr
Data []NetlinkRequestData
RawData []byte
raw bytes.Buffer
Sockets map[int]*SocketHandle
}

// Serialize the Netlink Request into a byte array
func (req *NetlinkRequest) Serialize() []byte {
length := unix.SizeofNlMsghdr
dataBytes := make([][]byte, len(req.Data))
for i, data := range req.Data {
dataBytes[i] = data.Serialize()
length = length + len(dataBytes[i])
if req.raw.Len() == 0 {
return nil
}
length += len(req.RawData)

req.Len = uint32(length)
b := make([]byte, length)
data := req.raw.Bytes()
req.Len = uint32(len(data))

hdr := (*(*[unix.SizeofNlMsghdr]byte)(unsafe.Pointer(req)))[:]
next := unix.SizeofNlMsghdr
copy(b[0:next], hdr)
for _, data := range dataBytes {
for _, dataByte := range data {
b[next] = dataByte
next = next + 1
}
}
// Add the raw data if any
if len(req.RawData) > 0 {
copy(b[next:length], req.RawData)
}
return b
copy(data[:unix.SizeofNlMsghdr], hdr)

return data
}

func (req *NetlinkRequest) AddData(data NetlinkRequestData) {
req.Data = append(req.Data, data)
// AddRtAttr adds an RtAttr to the request
func (req *NetlinkRequest) AddRtAttr(attrType int, data []byte) *NetlinkRequest {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This one also changes existing exported methods.

return req.AddData(NewRtAttr(attrType, data))
}

// AddRawData adds raw bytes to the end of the NetlinkRequest object during serialization
func (req *NetlinkRequest) AddRawData(data []byte) {
req.RawData = append(req.RawData, data...)
// AddData serializes the given data and appends it to the request
func (req *NetlinkRequest) AddData(data NetlinkRequestData) *NetlinkRequest {
return req.AddRawData(data.Serialize())
}

// AddRawData appends raw bytes
func (req *NetlinkRequest) AddRawData(data []byte) *NetlinkRequest {
if req.raw.Len() == 0 {
// Create space for the header
req.raw.Write(make([]byte, unix.SizeofNlMsghdr))
}
req.raw.Write(data)

return req
}

// Execute the request against a the given sockType.
Expand Down
14 changes: 13 additions & 1 deletion nl/parse_attr_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nl
import (
"encoding/binary"
"fmt"
"log"
)

type Attribute struct {
Expand All @@ -18,9 +19,20 @@ func ParseAttributes(data []byte) <-chan Attribute {
i := 0
for i+4 < len(data) {
length := int(native.Uint16(data[i : i+2]))
attrType := native.Uint16(data[i+2 : i+4])

if length < 4 {
log.Printf("attribute 0x%02x has invalid length of %d bytes", attrType, length)
break
}

if len(data) < i+length {
log.Printf("attribute 0x%02x of length %d is truncated, only %d bytes remaining", attrType, length, len(data)-i)
break
}

result <- Attribute{
Type: native.Uint16(data[i+2 : i+4]),
Type: attrType,
Value: data[i+4 : i+length],
}
i += rtaAlignOf(length)
Expand Down