diff --git a/route/netlink_path.go b/route/netlink_path.go index 1f01495abc7ed69fff57aa808d7cce6e753641dc..ebd721cd54e2ed9cc1d4ebeb12b5b5ae897ceae0 100644 --- a/route/netlink_path.go +++ b/route/netlink_path.go @@ -4,12 +4,20 @@ import ( "fmt" bnet "github.com/bio-routing/bio-rd/net" - log "github.com/sirupsen/logrus" "github.com/vishvananda/netlink" ) -// ProtoBio is the protocol number constant for bio from /etc/iproute2/rt_protos -const ProtoBio = 45 +const ( + ProtoUnspec = 0 // unspec (from /etc/iproute2/rt_protos) + ProtoRedirect = 1 // redirect (from /etc/iproute2/rt_protos) + ProtoKernel = 2 // kernel (from /etc/iproute2/rt_protos) + ProtoBoot = 3 // boot (from /etc/iproute2/rt_protos) + ProtoStatic = 4 // static (from /etc/iproute2/rt_protos) + ProtoZebra = 11 // zebra (from /etc/iproute2/rt_protos) + ProtoBird = 12 // bird (from /etc/iproute2/rt_protos) + ProtoDHCP = 16 // dhcp (from /etc/iproute2/rt_protos) + ProtoBio = 45 // bio +) // NetlinkPath represents a path learned via Netlink of a route type NetlinkPath struct { @@ -65,9 +73,6 @@ func NewNlPathFromRoute(r *netlink.Route, kernel bool) (*NetlinkPath, error) { dst = bnet.NewPfxFromIPNet(r.Dst) } - log.Warnf("IPFromBytes: %v goes to %v", r.Src, src) - log.Warnf("IPFromBytes: %v goes to %v", r.Dst, dst) - nextHop, _ := bnet.IPFromBytes(r.Gw) return &NetlinkPath{ diff --git a/route/path_test.go b/route/path_test.go index b22cf1f559603c6c679c5c962ce9455cc4286b94..d409cbd9b6a162e6a7ce42308df11cc94fde0569 100644 --- a/route/path_test.go +++ b/route/path_test.go @@ -1,10 +1,12 @@ package route import ( + "fmt" "testing" bnet "github.com/bio-routing/bio-rd/net" "github.com/stretchr/testify/assert" + "github.com/vishvananda/netlink" ) func TestPathNextHop(t *testing.T) { @@ -321,3 +323,182 @@ func TestPathsContains(t *testing.T) { } } } + +func TestNewNlPath(t *testing.T) { + tests := []struct { + name string + source *Path + expected *NetlinkPath + }{ + { + name: "BGPPath", + source: &Path{ + Type: BGPPathType, + BGPPath: &BGPPath{ + NextHop: bnet.IPv4(123), + }, + }, + expected: &NetlinkPath{ + NextHop: bnet.IPv4(123), + Protocol: ProtoBio, + }, + }, + } + + for _, test := range tests { + var converted *NetlinkPath + + switch test.source.Type { + case BGPPathType: + converted = NewNlPathFromBgpPath(test.source.BGPPath) + + default: + assert.Fail(t, fmt.Sprintf("Source-type %d is not supported", test.source.Type)) + } + + assert.Equalf(t, test.expected, converted, test.name) + } +} + +func TestNewNlPathFromNetlinkRoute(t *testing.T) { + tests := []struct { + name string + source *netlink.Route + expected *NetlinkPath + expectError bool + }{ + { + name: "Simple", + source: &netlink.Route{ + Dst: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8).GetIPNet(), + Src: bnet.IPv4(456).Bytes(), + Gw: bnet.IPv4(789).Bytes(), + Protocol: ProtoKernel, + Priority: 1, + Table: 254, + Type: 1, + }, + expected: &NetlinkPath{ + Dst: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), + Src: bnet.IPv4(456), + NextHop: bnet.IPv4(789), + Protocol: ProtoKernel, + Priority: 1, + Table: 254, + Type: 1, + Kernel: true, + }, + expectError: false, + }, + { + name: "No source, no destination", + source: &netlink.Route{ + Gw: bnet.IPv4(789).Bytes(), + Protocol: ProtoKernel, + Priority: 1, + Table: 254, + Type: 1, + }, + expected: &NetlinkPath{}, + expectError: true, + }, + { + name: "No source but destination", + source: &netlink.Route{ + Dst: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8).GetIPNet(), + Gw: bnet.IPv4(789).Bytes(), + Protocol: ProtoKernel, + Priority: 1, + Table: 254, + Type: 1, + }, + expected: &NetlinkPath{ + Dst: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), + Src: bnet.IPv4FromOctets(0, 0, 0, 0), + NextHop: bnet.IPv4(789), + Protocol: ProtoKernel, + Priority: 1, + Table: 254, + Type: 1, + Kernel: true, + }, + expectError: false, + }, + { + name: "Source but no destination", + source: &netlink.Route{ + Src: bnet.IPv4(456).Bytes(), + Gw: bnet.IPv4(789).Bytes(), + Protocol: ProtoKernel, + Priority: 1, + Table: 254, + Type: 1, + }, + expected: &NetlinkPath{ + Dst: bnet.NewPfx(bnet.IPv4FromOctets(0, 0, 0, 0), 0), + Src: bnet.IPv4(456), + NextHop: bnet.IPv4(789), + Protocol: ProtoKernel, + Priority: 1, + Table: 254, + Type: 1, + Kernel: true, + }, + expectError: false, + }, + { + name: "No source but destination IPv6", + source: &netlink.Route{ + Dst: bnet.NewPfx(bnet.IPv6(2001, 0), 48).GetIPNet(), + Gw: bnet.IPv6(2001, 2).Bytes(), + Protocol: ProtoKernel, + Priority: 1, + Table: 254, + Type: 1, + }, + expected: &NetlinkPath{ + Dst: bnet.NewPfx(bnet.IPv6(2001, 0), 48), + Src: bnet.IPv6FromBlocks(0, 0, 0, 0, 0, 0, 0, 0), + NextHop: bnet.IPv6(2001, 2), + Protocol: ProtoKernel, + Priority: 1, + Table: 254, + Type: 1, + Kernel: true, + }, + expectError: false, + }, + { + name: "Source but no destination IPv6", + source: &netlink.Route{ + Src: bnet.IPv6(2001, 0).Bytes(), + Gw: bnet.IPv6(2001, 2).Bytes(), + Protocol: ProtoKernel, + Priority: 1, + Table: 254, + Type: 1, + }, + expected: &NetlinkPath{ + Dst: bnet.NewPfx(bnet.IPv6FromBlocks(0, 0, 0, 0, 0, 0, 0, 0), 0), + Src: bnet.IPv6(2001, 0), + NextHop: bnet.IPv6(2001, 2), + Protocol: ProtoKernel, + Priority: 1, + Table: 254, + Type: 1, + Kernel: true, + }, + expectError: false, + }, + } + + for _, test := range tests { + converted, err := NewNlPathFromRoute(test.source, true) + if test.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equalf(t, test.expected, converted, test.name) + } + } +}