diff options
author | Chris Stockton <cstockton@godaddy.com> | 2017-05-31 08:37:09 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2017-05-31 08:37:09 +0900 |
commit | b529f81ed7d8428200a3f5dcf99a57db9f9f769e (patch) | |
tree | 03f9916f6e26307f8226a158e72d1c8b71ec35d4 | |
parent | b4831ce8e91e6f27e3b241efd0c3fdfd8feca43a (diff) |
table: reduce allocs in NewDestination
In NewDestination we add additional path to AddrToRadixkey to check
the concrete type of nlri.
-rw-r--r-- | table/destination.go | 20 | ||||
-rw-r--r-- | table/destination_test.go | 28 |
2 files changed, 45 insertions, 3 deletions
diff --git a/table/destination.go b/table/destination.go index 3535bbfe..9664fdbc 100644 --- a/table/destination.go +++ b/table/destination.go @@ -64,6 +64,24 @@ func CidrToRadixkey(cidr string) string { return IpToRadixkey(n.IP, uint8(ones)) } +func AddrToRadixkey(addr bgp.AddrPrefixInterface) string { + var ( + ip net.IP + size uint8 + ) + switch T := addr.(type) { + case *bgp.IPAddrPrefix: + mask := net.CIDRMask(int(T.Length), net.IPv4len*8) + ip, size = T.Prefix.Mask(mask).To4(), uint8(T.Length) + case *bgp.IPv6AddrPrefix: + mask := net.CIDRMask(int(T.Length), net.IPv6len*8) + ip, size = T.Prefix.Mask(mask).To16(), uint8(T.Length) + default: + return CidrToRadixkey(addr.String()) + } + return IpToRadixkey(ip, size) +} + type PeerInfo struct { AS uint32 ID net.IP @@ -140,7 +158,7 @@ func NewDestination(nlri bgp.AddrPrefixInterface, known ...*Path) *Destination { } switch d.routeFamily { case bgp.RF_IPv4_UC, bgp.RF_IPv6_UC, bgp.RF_IPv4_MPLS, bgp.RF_IPv6_MPLS: - d.RadixKey = CidrToRadixkey(nlri.String()) + d.RadixKey = AddrToRadixkey(nlri) } return d } diff --git a/table/destination_test.go b/table/destination_test.go index 49c63e32..e1f13410 100644 --- a/table/destination_test.go +++ b/table/destination_test.go @@ -17,11 +17,13 @@ package table import ( //"fmt" - "github.com/osrg/gobgp/packet/bgp" - "github.com/stretchr/testify/assert" + "fmt" "net" "testing" "time" + + "github.com/osrg/gobgp/packet/bgp" + "github.com/stretchr/testify/assert" ) func TestDestinationNewIPv4(t *testing.T) { @@ -402,6 +404,28 @@ func TestRadixkey(t *testing.T) { assert.Equal(t, CidrToRadixkey("::ffff:0.0.0.0/96")+"000010100000001100100000", CidrToRadixkey("::ffff:10.3.32.0/120")) } +func TestIpToRadixkey(t *testing.T) { + for i := byte(0); i < 255; i += 3 { + for y := byte(1); y < 128; y *= 2 { + ip := net.IPv4(i, i+2, i+3, i-y) + for n := uint8(16); n <= 32; n += 2 { + exp := CidrToRadixkey(fmt.Sprintf("%v/%d", ip.To4(), n)) + got := IpToRadixkey(ip.To4(), n) + if exp != got { + t.Fatalf(`exp %v; got %v`, exp, got) + } + } + for n := uint8(116); n <= 128; n += 2 { + exp := CidrToRadixkey(fmt.Sprintf("::ffff:%v/%d", ip.To16(), n)) + got := IpToRadixkey(ip.To16(), n) + if exp != got { + t.Fatalf(`exp %v; got %v`, exp, got) + } + } + } + } +} + func TestMultipath(t *testing.T) { UseMultiplePaths.Enabled = true origin := bgp.NewPathAttributeOrigin(0) |