Skip to content

Commit

Permalink
support kernel 5.19 and up
Browse files Browse the repository at this point in the history
Kernel 5.19 added an attribute in the LinkStats64.
torvalds/linux@794c24e

This will fix running on kernels 5.19 and above.

Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
  • Loading branch information
jsimonetti committed Aug 23, 2022
1 parent 0377538 commit 377642b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 13 deletions.
14 changes: 10 additions & 4 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func (a *LinkStats) unmarshalBinary(b []byte) error {
a.RXCompressed = nativeEndian.Uint32(b[84:88])
a.TXCompressed = nativeEndian.Uint32(b[88:92])

if l == 96 {
if l == 96 { // kernel 4.6+
a.RXNoHandler = nativeEndian.Uint32(b[92:96])
}

Expand Down Expand Up @@ -453,13 +453,15 @@ type LinkStats64 struct {
TXCompressed uint64

RXNoHandler uint64 // dropped, no handler found

RXOtherhostDropped uint64 // Number of packets dropped due to mismatch in destination MAC address.
}

// unmarshalBinary unmarshals the contents of a byte slice into a LinkMessage.
func (a *LinkStats64) unmarshalBinary(b []byte) error {
l := len(b)
if l != 184 && l != 192 {
return fmt.Errorf("incorrect size, want: 184 or 192")
if l != 184 && l != 192 && l != 200 {
return fmt.Errorf("incorrect size, want: 184 or 192 or 200")
}

a.RXPackets = nativeEndian.Uint64(b[0:8])
Expand Down Expand Up @@ -489,10 +491,14 @@ func (a *LinkStats64) unmarshalBinary(b []byte) error {
a.RXCompressed = nativeEndian.Uint64(b[168:176])
a.TXCompressed = nativeEndian.Uint64(b[176:184])

if l == 192 {
if l > 191 { // kernel 4.6+
a.RXNoHandler = nativeEndian.Uint64(b[184:192])
}

if l > 199 { // kernel 5.19+
a.RXOtherhostDropped = nativeEndian.Uint64(b[192:200])
}

return nil
}

Expand Down
80 changes: 71 additions & 9 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,22 +520,27 @@ func TestLinkStats64UnmarshalBinary(t *testing.T) {
}{
{
name: "empty",
err: fmt.Errorf("incorrect size, want: 184 or 192"),
err: fmt.Errorf("incorrect size, want: 184 or 192 or 200"),
},
{
name: "invalid < 184",
b: make([]byte, 183),
err: fmt.Errorf("incorrect size, want: 184 or 192"),
err: fmt.Errorf("incorrect size, want: 184 or 192 or 200"),
},
{
name: "invalid > 192",
name: "invalid > 184 < 192",
b: make([]byte, 188),
err: fmt.Errorf("incorrect size, want: 184 or 192 or 200"),
},
{
name: "invalid > 192 < 200",
b: make([]byte, 193),
err: fmt.Errorf("incorrect size, want: 184 or 192"),
err: fmt.Errorf("incorrect size, want: 184 or 192 or 200"),
},
{
name: "invalid > 184 < 192",
b: make([]byte, 188),
err: fmt.Errorf("incorrect size, want: 184 or 192"),
name: "invalid > 200",
b: make([]byte, 201),
err: fmt.Errorf("incorrect size, want: 184 or 192 or 200"),
},
{
name: "kernel <4.6",
Expand Down Expand Up @@ -616,7 +621,7 @@ func TestLinkStats64UnmarshalBinary(t *testing.T) {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
m: &LinkStats64{
RXPackets: 0x1b650,
Expand All @@ -642,7 +647,64 @@ func TestLinkStats64UnmarshalBinary(t *testing.T) {
TXWindowErrors: 0x0,
RXCompressed: 0x0,
TXCompressed: 0x0,
RXNoHandler: 0x0,
RXNoHandler: 0x1,
},
},
{
name: "kernel 5.19+",
b: []byte{
0x50, 0xb6, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x06, 0xc9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xa9, 0x41, 0xcd, 0x09, 0x00, 0x00, 0x00, 0x00,
0x96, 0x96, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
m: &LinkStats64{
RXPackets: 0x1b650,
TXPackets: 0xc906,
RXBytes: 0x9cd41a9,
TXBytes: 0x2a9696,
RXErrors: 0x0,
TXErrors: 0x0,
RXDropped: 0x0,
TXDropped: 0x0,
Multicast: 0x0,
Collisions: 0x0,
RXLengthErrors: 0x0,
RXOverErrors: 0x0,
RXCRCErrors: 0x0,
RXFrameErrors: 0x0,
RXFIFOErrors: 0x0,
RXMissedErrors: 0x0,
TXAbortedErrors: 0x0,
TXCarrierErrors: 0x0,
TXFIFOErrors: 0x0,
TXHeartbeatErrors: 0x0,
TXWindowErrors: 0x0,
RXCompressed: 0x0,
TXCompressed: 0x0,
RXNoHandler: 0x1,
RXOtherhostDropped: 0x2,
},
},
}
Expand Down

0 comments on commit 377642b

Please sign in to comment.