Skip to content

Commit

Permalink
Add support for RTA_FLOW
Browse files Browse the repository at this point in the history
This adds support for the RTA_FLOW routing msg
attribute and adds the Realm field to the Route
object.
  • Loading branch information
tobias-urdin committed Sep 20, 2021
1 parent fc21756 commit 555f7fb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Route struct {
NewDst Destination
Encap Encap
Via Destination
Realm int
MTU int
Window int
Rtt int
Expand Down Expand Up @@ -95,6 +96,7 @@ func (r Route) String() string {
}
elems = append(elems, fmt.Sprintf("Flags: %s", r.ListFlags()))
elems = append(elems, fmt.Sprintf("Table: %d", r.Table))
elems = append(elems, fmt.Sprintf("Realm: %d", r.Realm))
return fmt.Sprintf("{%s}", strings.Join(elems, " "))
}

Expand All @@ -108,6 +110,7 @@ func (r Route) Equal(x Route) bool {
nexthopInfoSlice(r.MultiPath).Equal(x.MultiPath) &&
r.Protocol == x.Protocol &&
r.Priority == x.Priority &&
r.Realm == x.Realm &&
r.Table == x.Table &&
r.Type == x.Type &&
r.Tos == x.Tos &&
Expand Down
10 changes: 10 additions & 0 deletions route_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (
RT_FILTER_PRIORITY
RT_FILTER_MARK
RT_FILTER_MASK
RT_FILTER_REALM
)

const (
Expand Down Expand Up @@ -893,6 +894,11 @@ func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg
native.PutUint32(b, uint32(route.Priority))
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_PRIORITY, b))
}
if route.Realm > 0 {
b := make([]byte, 4)
native.PutUint32(b, uint32(route.Realm))
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_FLOW, b))
}
if route.Tos > 0 {
msg.Tos = uint8(route.Tos)
}
Expand Down Expand Up @@ -1061,6 +1067,8 @@ func (h *Handle) RouteListFiltered(family int, filter *Route, filterMask uint64)
continue
case filterMask&RT_FILTER_TOS != 0 && route.Tos != filter.Tos:
continue
case filterMask&RT_FILTER_REALM != 0 && route.Realm != filter.Realm:
continue
case filterMask&RT_FILTER_OIF != 0 && route.LinkIndex != filter.LinkIndex:
continue
case filterMask&RT_FILTER_IIF != 0 && route.ILinkIndex != filter.ILinkIndex:
Expand Down Expand Up @@ -1127,6 +1135,8 @@ func deserializeRoute(m []byte) (Route, error) {
route.ILinkIndex = int(native.Uint32(attr.Value[0:4]))
case unix.RTA_PRIORITY:
route.Priority = int(native.Uint32(attr.Value[0:4]))
case unix.RTA_FLOW:
route.Realm = int(native.Uint32(attr.Value[0:4]))
case unix.RTA_TABLE:
route.Table = int(native.Uint32(attr.Value[0:4]))
case unix.RTA_MULTIPATH:
Expand Down
33 changes: 31 additions & 2 deletions route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ func TestRouteFilterAllTables(t *testing.T) {
Type: unix.RTN_UNICAST,
Tos: 14,
Hoplimit: 100,
Realm: 328,
}
if err := RouteAdd(&route); err != nil {
t.Fatal(err)
Expand All @@ -588,7 +589,8 @@ func TestRouteFilterAllTables(t *testing.T) {
Type: unix.RTN_UNICAST,
Tos: 14,
Hoplimit: 100,
}, RT_FILTER_DST|RT_FILTER_SRC|RT_FILTER_SCOPE|RT_FILTER_TABLE|RT_FILTER_TYPE|RT_FILTER_TOS|RT_FILTER_HOPLIMIT)
Realm: 328,
}, RT_FILTER_DST|RT_FILTER_SRC|RT_FILTER_SCOPE|RT_FILTER_TABLE|RT_FILTER_TYPE|RT_FILTER_TOS|RT_FILTER_HOPLIMIT|RT_FILTER_REALM)
if err != nil {
t.Fatal(err)
}
Expand All @@ -615,6 +617,9 @@ func TestRouteFilterAllTables(t *testing.T) {
if route.Hoplimit != 100 {
t.Fatal("Invalid Hoplimit. Route not added properly")
}
if route.Realm != 328 {
t.Fatal("Invalid Realm. Route not added properly")
}
}
}

Expand Down Expand Up @@ -658,6 +663,7 @@ func TestRouteExtraFields(t *testing.T) {
Type: unix.RTN_UNICAST,
Tos: 14,
Hoplimit: 100,
Realm: 239,
}
if err := RouteAdd(&route); err != nil {
t.Fatal(err)
Expand All @@ -670,7 +676,8 @@ func TestRouteExtraFields(t *testing.T) {
Type: unix.RTN_UNICAST,
Tos: 14,
Hoplimit: 100,
}, RT_FILTER_DST|RT_FILTER_SRC|RT_FILTER_SCOPE|RT_FILTER_TABLE|RT_FILTER_TYPE|RT_FILTER_TOS|RT_FILTER_HOPLIMIT)
Realm: 239,
}, RT_FILTER_DST|RT_FILTER_SRC|RT_FILTER_SCOPE|RT_FILTER_TABLE|RT_FILTER_TYPE|RT_FILTER_TOS|RT_FILTER_HOPLIMIT|RT_FILTER_REALM)
if err != nil {
t.Fatal(err)
}
Expand All @@ -696,6 +703,9 @@ func TestRouteExtraFields(t *testing.T) {
if routes[0].Hoplimit != 100 {
t.Fatal("Invalid Hoplimit. Route not added properly")
}
if routes[0].Realm != 239 {
t.Fatal("Invalid Realm. Route not added properly")
}
}

func TestRouteMultiPath(t *testing.T) {
Expand Down Expand Up @@ -920,6 +930,12 @@ func TestRouteEqual(t *testing.T) {
Hoplimit: 1,
Gw: net.IPv4(1, 1, 1, 1),
},
{
LinkIndex: 20,
Dst: nil,
Realm: 29,
Gw: net.IPv4(1, 1, 1, 1),
},
{
LinkIndex: 20,
Dst: nil,
Expand Down Expand Up @@ -969,6 +985,19 @@ func TestRouteEqual(t *testing.T) {
Type: unix.RTN_UNICAST,
Hoplimit: 100,
},
{
LinkIndex: 3,
Dst: &net.IPNet{
IP: net.IPv4(1, 1, 1, 1),
Mask: net.CIDRMask(32, 32),
},
Src: net.IPv4(127, 3, 3, 3),
Scope: unix.RT_SCOPE_LINK,
Priority: 13,
Table: unix.RT_TABLE_MAIN,
Type: unix.RTN_UNICAST,
Realm: 129,
},
{
LinkIndex: 10,
MPLSDst: &mplsDst,
Expand Down

0 comments on commit 555f7fb

Please sign in to comment.