Skip to content

Commit dc4f225

Browse files
dylandreimerinkaboch
authored andcommitted
Add OifIndex option for RouteGetWithOptions
The `RouteGetWithOptions` function currently has a `Oif` option which gets translated from link name to link index via a `LinkByName` call. This adds unnecessary overhead when the link index is already known. This commit adds a new `OifIndex` option to `RouteGetWithOptions` which can be specified instead of `Oif` to skip the internal link index translation. Signed-off-by: Dylan Reimerink <[email protected]>
1 parent 6f000f5 commit dc4f225

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

route_linux.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,7 @@ type RouteGetOptions struct {
15391539
Iif string
15401540
IifIndex int
15411541
Oif string
1542+
OifIndex int
15421543
VrfName string
15431544
SrcAddr net.IP
15441545
UID *uint32
@@ -1618,14 +1619,20 @@ func (h *Handle) RouteGetWithOptions(destination net.IP, options *RouteGetOption
16181619
req.AddData(nl.NewRtAttr(unix.RTA_IIF, b))
16191620
}
16201621

1622+
oifIndex := uint32(0)
16211623
if len(options.Oif) > 0 {
16221624
link, err := h.LinkByName(options.Oif)
16231625
if err != nil {
16241626
return nil, err
16251627
}
1628+
oifIndex = uint32(link.Attrs().Index)
1629+
} else if options.OifIndex > 0 {
1630+
oifIndex = uint32(options.OifIndex)
1631+
}
16261632

1633+
if oifIndex > 0 {
16271634
b := make([]byte, 4)
1628-
native.PutUint32(b, uint32(link.Attrs().Index))
1635+
native.PutUint32(b, oifIndex)
16291636

16301637
req.AddData(nl.NewRtAttr(unix.RTA_OIF, b))
16311638
}

route_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,16 @@ func TestRouteOifOption(t *testing.T) {
14361436
t.Fatal("Get route from unmatched interface")
14371437
}
14381438

1439+
// check getting route from specified Oifindex
1440+
routes, err = RouteGetWithOptions(dstIP, &RouteGetOptions{OifIndex: link1.Attrs().Index})
1441+
if err != nil {
1442+
t.Fatal(err)
1443+
}
1444+
if len(routes) != 1 || routes[0].LinkIndex != link1.Attrs().Index ||
1445+
!routes[0].Gw.Equal(gw1) {
1446+
t.Fatal("Get route from unmatched interface")
1447+
}
1448+
14391449
routes, err = RouteGetWithOptions(dstIP, &RouteGetOptions{Oif: "eth1"})
14401450
if err != nil {
14411451
t.Fatal(err)
@@ -1446,6 +1456,14 @@ func TestRouteOifOption(t *testing.T) {
14461456
t.Fatal("Get route from unmatched interface")
14471457
}
14481458

1459+
routes, err = RouteGetWithOptions(dstIP, &RouteGetOptions{OifIndex: link2.Attrs().Index})
1460+
if err != nil {
1461+
t.Fatal(err)
1462+
}
1463+
if len(routes) != 1 || routes[0].LinkIndex != link2.Attrs().Index ||
1464+
!routes[0].Gw.Equal(gw2) {
1465+
t.Fatal("Get route from unmatched interface")
1466+
}
14491467
}
14501468

14511469
func TestFilterDefaultRoute(t *testing.T) {

0 commit comments

Comments
 (0)