Skip to content

Commit

Permalink
rtnetlink: support for RTA_PREF
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Layher <mdlayher@gmail.com>
  • Loading branch information
mdlayher committed Mar 13, 2022
1 parent e899fd5 commit 670220a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/unix/types_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const (
RTA_EXPIRES = linux.RTA_EXPIRES
RTA_METRICS = linux.RTA_METRICS
RTA_MULTIPATH = linux.RTA_MULTIPATH
RTA_PREF = linux.RTA_PREF
RTAX_ADVMSS = linux.RTAX_ADVMSS
RTAX_FEATURES = linux.RTAX_FEATURES
RTAX_INITCWND = linux.RTAX_INITCWND
Expand Down
1 change: 1 addition & 0 deletions internal/unix/types_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const (
RTA_EXPIRES = 0x17
RTA_METRICS = 0x8
RTA_MULTIPATH = 0x9
RTA_PREF = 0x14
RTAX_ADVMSS = 0x8
RTAX_FEATURES = 0xc
RTAX_INITCWND = 0xb
Expand Down
8 changes: 8 additions & 0 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ type RouteAttributes struct {
Priority uint32
Table uint32
Mark uint32
Pref *uint8
Expires *uint32
Metrics *RouteMetrics
Multipath []NextHop
Expand Down Expand Up @@ -194,6 +195,9 @@ func (a *RouteAttributes) decode(ad *netlink.AttributeDecoder) error {
ad.Nested(a.Metrics.decode)
case unix.RTA_MULTIPATH:
ad.Do(a.parseMultipath)
case unix.RTA_PREF:
pref := ad.Uint8()
a.Pref = &pref
}
}

Expand Down Expand Up @@ -229,6 +233,10 @@ func (a *RouteAttributes) encode(ae *netlink.AttributeEncoder) error {
ae.Uint32(unix.RTA_MARK, a.Mark)
}

if a.Pref != nil {
ae.Uint8(unix.RTA_PREF, *a.Pref)
}

if a.Expires != nil {
ae.Uint32(unix.RTA_EXPIRES, *a.Expires)
}
Expand Down
10 changes: 9 additions & 1 deletion route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import (
func TestRouteMessageMarshalUnmarshalBinary(t *testing.T) {
skipBigEndian(t)

timeout := uint32(255)
var (
timeout = uint32(255)
pref = uint8(1)
)

tests := []struct {
name string
m *RouteMessage
Expand Down Expand Up @@ -56,6 +60,7 @@ func TestRouteMessageMarshalUnmarshalBinary(t *testing.T) {
Priority: 1,
Table: 2,
Mark: 3,
Pref: &pref,
Expires: &timeout,
Metrics: &RouteMetrics{
AdvMSS: 1,
Expand Down Expand Up @@ -127,6 +132,9 @@ func TestRouteMessageMarshalUnmarshalBinary(t *testing.T) {
// Mark
0x08, 0x00, 0x10, 0x00,
0x03, 0x00, 0x00, 0x00,
// Pref
0x05, 0x00, 0x14, 0x00,
0x01, 0x00, 0x00, 0x00,
// Expires
0x08, 0x00, 0x17, 0x00,
0xff, 0x00, 0x00, 0x00,
Expand Down

0 comments on commit 670220a

Please sign in to comment.