From 7e79fb773cba86656ab6d84e8e482df1bc822d62 Mon Sep 17 00:00:00 2001 From: Oliver Herms <oliver.herms@exaring.de> Date: Thu, 1 Nov 2018 00:23:19 +0100 Subject: [PATCH] Refactor IP versioning in API --- apps/bmp-streamer/client/main.go | 6 +++--- apps/bmp-streamer/pkg/apiserver/server.go | 7 ++++-- net/api/net.proto | 6 +++++- net/ip.go | 15 ++++++++----- net/ip_test.go | 26 +++++++++++------------ net/prefix.go | 2 +- net/prefix_test.go | 24 ++++++++++----------- route/api/route.proto | 10 ++++++--- route/bgp_path.go | 12 +++++++++-- route/bgp_path_test.go | 2 +- route/path.go | 8 ++++++- 11 files changed, 74 insertions(+), 44 deletions(-) diff --git a/apps/bmp-streamer/client/main.go b/apps/bmp-streamer/client/main.go index 05fdadc5..55a8aad7 100644 --- a/apps/bmp-streamer/client/main.go +++ b/apps/bmp-streamer/client/main.go @@ -31,9 +31,9 @@ func main() { c := pb.NewRIBServiceClient(conn) streamClient, err := c.AdjRIBInStream(context.Background(), &pb.AdjRIBInStreamRequest{ Router: &netapi.IP{ - Higher: rtr.Higher(), - Lower: rtr.Lower(), - IsLegacy: true, + Higher: rtr.Higher(), + Lower: rtr.Lower(), + Version: netapi.IP_IPv4, }, }) diff --git a/apps/bmp-streamer/pkg/apiserver/server.go b/apps/bmp-streamer/pkg/apiserver/server.go index b4dd1147..0fe16ff0 100644 --- a/apps/bmp-streamer/pkg/apiserver/server.go +++ b/apps/bmp-streamer/pkg/apiserver/server.go @@ -5,6 +5,7 @@ import ( pb "github.com/bio-routing/bio-rd/apps/bmp-streamer/pkg/bmpstreamer" net "github.com/bio-routing/bio-rd/net" + netapi "github.com/bio-routing/bio-rd/net/api" "github.com/bio-routing/bio-rd/protocols/bgp/packet" "github.com/bio-routing/bio-rd/protocols/bgp/server" "github.com/bio-routing/bio-rd/route" @@ -39,10 +40,12 @@ func (a *APIServer) AdjRIBInStream(req *pb.AdjRIBInStreamRequest, stream pb.RIBS r6 := newRIBClient() addr := net.IP{} - if req.Router.IsLegacy { + if req.Router.Version == netapi.IP_IPv4 { addr = net.IPv4(uint32(req.Router.Lower)) - } else { + } else if req.Router.Version == netapi.IP_IPv6 { addr = net.IPv6(req.Router.Higher, req.Router.Lower) + } else { + return fmt.Errorf("Unknown protocol") } ret := make(chan error) diff --git a/net/api/net.proto b/net/api/net.proto index 650789c4..1583a207 100644 --- a/net/api/net.proto +++ b/net/api/net.proto @@ -10,5 +10,9 @@ message Prefix { message IP { uint64 higher = 1; uint64 lower = 2; - bool is_legacy = 3; + enum Version { + IPv4 = 0; + IPv6 = 1; + } + Version version = 3; } diff --git a/net/ip.go b/net/ip.go index dbf17fe3..e9dc9773 100644 --- a/net/ip.go +++ b/net/ip.go @@ -4,7 +4,7 @@ import ( "fmt" "net" - "github.com/bio-routing/bio-rd/net/api" + api "github.com/bio-routing/bio-rd/net/api" ) // IP represents an IPv4 or IPv6 address @@ -19,16 +19,21 @@ func IPFromProtoIP(addr api.IP) IP { return IP{ higher: addr.Higher, lower: addr.Lower, - isLegacy: addr.IsLegacy, + isLegacy: addr.Version == api.IP_IPv4, } } // ToProto converts an IP to a proto IP func (ip IP) ToProto() *api.IP { + version := api.IP_IPv4 + if !ip.isLegacy { + version = api.IP_IPv6 + } + return &api.IP{ - Lower: ip.lower, - Higher: ip.higher, - IsLegacy: ip.isLegacy, + Lower: ip.lower, + Higher: ip.higher, + Version: version, } } diff --git a/net/ip_test.go b/net/ip_test.go index 53738b09..cc54e99f 100644 --- a/net/ip_test.go +++ b/net/ip_test.go @@ -5,7 +5,7 @@ import ( "net" "testing" - "github.com/bio-routing/bio-rd/net/api" + api "github.com/bio-routing/bio-rd/net/api" "github.com/stretchr/testify/assert" ) @@ -83,8 +83,8 @@ func TestIPToProto(t *testing.T) { isLegacy: true, }, expected: &api.IP{ - Lower: 255, - IsLegacy: true, + Lower: 255, + Version: api.IP_IPv4, }, }, { @@ -95,9 +95,9 @@ func TestIPToProto(t *testing.T) { isLegacy: false, }, expected: &api.IP{ - Higher: 1000, - Lower: 255, - IsLegacy: false, + Higher: 1000, + Lower: 255, + Version: api.IP_IPv6, }, }, } @@ -117,9 +117,9 @@ func TestIPFromProtoIP(t *testing.T) { { name: "Test IPv4", proto: api.IP{ - Lower: 100, - Higher: 0, - IsLegacy: true, + Lower: 100, + Higher: 0, + Version: api.IP_IPv4, }, expected: IP{ lower: 100, @@ -130,9 +130,9 @@ func TestIPFromProtoIP(t *testing.T) { { name: "Test IPv6", proto: api.IP{ - Lower: 100, - Higher: 200, - IsLegacy: false, + Lower: 100, + Higher: 200, + Version: api.IP_IPv6, }, expected: IP{ lower: 100, @@ -144,7 +144,7 @@ func TestIPFromProtoIP(t *testing.T) { for _, test := range tests { res := IPFromProtoIP(test.proto) - assert.Equal(t, test.expected, res, test.name) + assert.Equal(t, test.expected, res, test.name) } } diff --git a/net/prefix.go b/net/prefix.go index 22158dc0..85b4265d 100644 --- a/net/prefix.go +++ b/net/prefix.go @@ -6,7 +6,7 @@ import ( "strconv" "strings" - "github.com/bio-routing/bio-rd/net/api" + api "github.com/bio-routing/bio-rd/net/api" ) // Prefix represents an IPv4 prefix diff --git a/net/prefix_test.go b/net/prefix_test.go index 52e2aea6..41423a36 100644 --- a/net/prefix_test.go +++ b/net/prefix_test.go @@ -3,7 +3,7 @@ package net import ( "testing" - "github.com/bio-routing/bio-rd/net/api" + api "github.com/bio-routing/bio-rd/net/api" "github.com/stretchr/testify/assert" ) @@ -24,8 +24,8 @@ func TestPrefixToProto(t *testing.T) { }, expected: &api.Prefix{ Address: &api.IP{ - Lower: 200, - IsLegacy: true, + Lower: 200, + Version: api.IP_IPv4, }, Pfxlen: 24, }, @@ -42,9 +42,9 @@ func TestPrefixToProto(t *testing.T) { }, expected: &api.Prefix{ Address: &api.IP{ - Higher: 100, - Lower: 200, - IsLegacy: false, + Higher: 100, + Lower: 200, + Version: api.IP_IPv6, }, Pfxlen: 64, }, @@ -67,9 +67,9 @@ func TestNewPrefixFromProtoPrefix(t *testing.T) { name: "IPv4", proto: api.Prefix{ Address: &api.IP{ - Higher: 0, - Lower: 2000, - IsLegacy: true, + Higher: 0, + Lower: 2000, + Version: api.IP_IPv4, }, Pfxlen: 24, }, @@ -86,9 +86,9 @@ func TestNewPrefixFromProtoPrefix(t *testing.T) { name: "IPv6", proto: api.Prefix{ Address: &api.IP{ - Higher: 1000, - Lower: 2000, - IsLegacy: false, + Higher: 1000, + Lower: 2000, + Version: api.IP_IPv6, }, Pfxlen: 64, }, diff --git a/route/api/route.proto b/route/api/route.proto index 6d8ef355..c1846ad8 100644 --- a/route/api/route.proto +++ b/route/api/route.proto @@ -10,9 +10,13 @@ message Route { } message Path { - uint32 type = 1; - BGPPath BGP_path = 2; - StaticPath static_path = 3; + enum Type { + Static = 0; + BGP = 1; + } + Type type = 1; + StaticPath static_path = 2; + BGPPath BGP_path = 3; } message StaticPath { diff --git a/route/bgp_path.go b/route/bgp_path.go index c72cc615..bddc90f3 100644 --- a/route/bgp_path.go +++ b/route/bgp_path.go @@ -47,10 +47,18 @@ func (b *BGPPath) ToProto() *api.BGPPath { } ret.NextHop.Lower = b.NextHop.Lower() ret.NextHop.Higher = b.NextHop.Higher() - ret.NextHop.IsLegacy = b.NextHop.IsLegacy() + ret.NextHop.Version = netapi.IP_IPv4 + if !b.NextHop.IsLegacy() { + ret.NextHop.Version = netapi.IP_IPv6 + } + ret.Source.Lower = b.Source.Lower() ret.Source.Higher = b.Source.Higher() - ret.Source.IsLegacy = b.Source.IsLegacy() + ret.Source.Version = netapi.IP_IPv4 + if !b.Source.IsLegacy() { + ret.Source.Version = netapi.IP_IPv6 + } + ret.EBGP = b.EBGP ret.BGPIdentifier = b.BGPIdentifier ret.ClusterList = b.ClusterList diff --git a/route/bgp_path_test.go b/route/bgp_path_test.go index e4eb8823..d7b6da66 100644 --- a/route/bgp_path_test.go +++ b/route/bgp_path_test.go @@ -185,7 +185,7 @@ func TestBGPPathToProto(t *testing.T) { PathIdentifier: 10, NextHop: &netapi.IP{ Lower: 210, - IsLegacy: true, + Version: api.IP_IPv4, }, LocalPref: 20, ASPath: []*pb.ASPathSegment{ diff --git a/route/path.go b/route/path.go index 98a2c532..81bcd6a1 100644 --- a/route/path.go +++ b/route/path.go @@ -15,8 +15,14 @@ type Path struct { // ToProto converts Path to Proto Path func (p *Path) ToProto() *api.Path { + t := api.Path_Static + switch p.Type { + case BGPPathType: + t = api.Path_BGP + } + return &api.Path{ - Type: uint32(p.Type), + Type: t, BGPPath: p.BGPPath.ToProto(), StaticPath: p.StaticPath.ToProto(), } -- GitLab