summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@gmail.com>2021-01-12 14:25:27 +0900
committerFUJITA Tomonori <fujita.tomonori@gmail.com>2021-01-12 14:25:27 +0900
commit635128d3e56475dfea9a06ab07feca8acf71c16a (patch)
tree119f092cab415075d90da4466610fda10bbd592c
parent496b372f7b8dcfa17f8c32285baceb6386327663 (diff)
table: fix longer-prefixes lookup
probably regression due to the introduction of critbitgo. Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
-rw-r--r--internal/pkg/table/table.go21
-rw-r--r--internal/pkg/table/table_test.go18
2 files changed, 38 insertions, 1 deletions
diff --git a/internal/pkg/table/table.go b/internal/pkg/table/table.go
index 2e0dc94a..a9f1323e 100644
--- a/internal/pkg/table/table.go
+++ b/internal/pkg/table/table.go
@@ -209,6 +209,7 @@ func (t *Table) GetLongerPrefixDestinations(key string) ([]*Destination, error)
switch t.routeFamily {
case bgp.RF_IPv4_UC, bgp.RF_IPv6_UC, bgp.RF_IPv4_MPLS, bgp.RF_IPv6_MPLS:
_, prefix, err := net.ParseCIDR(key)
+ ones, bits := prefix.Mask.Size()
if err != nil {
return nil, err
}
@@ -216,7 +217,25 @@ func (t *Table) GetLongerPrefixDestinations(key string) ([]*Destination, error)
for _, dst := range t.GetDestinations() {
r.Add(nlriToIPNet(dst.nlri), dst)
}
- r.WalkPrefix(prefix, func(_ *net.IPNet, v interface{}) bool {
+ p := &net.IPNet{
+ IP: prefix.IP,
+ Mask: net.CIDRMask((ones>>3)<<3, bits),
+ }
+ mask := 0
+ div := 0
+ if ones%8 != 0 {
+ mask = 8 - ones&0x7
+ div = ones >> 3
+ }
+ r.WalkPrefix(p, func(n *net.IPNet, v interface{}) bool {
+ if mask != 0 && n.IP[div]>>mask != p.IP[div]>>mask {
+ return true
+ }
+ l, _ := n.Mask.Size()
+
+ if ones > l {
+ return true
+ }
results = append(results, v.(*Destination))
return true
})
diff --git a/internal/pkg/table/table_test.go b/internal/pkg/table/table_test.go
index e0f1f3cf..f72e62b9 100644
--- a/internal/pkg/table/table_test.go
+++ b/internal/pkg/table/table_test.go
@@ -24,6 +24,23 @@ import (
"github.com/stretchr/testify/assert"
)
+func TestLookupLonger(t *testing.T) {
+ tbl := NewTable(bgp.RF_IPv4_UC)
+
+ tbl.setDestination(NewDestination(bgp.NewIPAddrPrefix(23, "11.0.0.0"), 0))
+ tbl.setDestination(NewDestination(bgp.NewIPAddrPrefix(24, "11.0.0.0"), 0))
+ tbl.setDestination(NewDestination(bgp.NewIPAddrPrefix(32, "11.0.0.4"), 0))
+ tbl.setDestination(NewDestination(bgp.NewIPAddrPrefix(32, "11.0.0.129"), 0))
+ tbl.setDestination(NewDestination(bgp.NewIPAddrPrefix(28, "11.0.0.144"), 0))
+ tbl.setDestination(NewDestination(bgp.NewIPAddrPrefix(29, "11.0.0.144"), 0))
+ tbl.setDestination(NewDestination(bgp.NewIPAddrPrefix(32, "11.0.0.145"), 0))
+
+ r, _ := tbl.GetLongerPrefixDestinations("11.0.0.128/25")
+ assert.Equal(t, len(r), 4)
+ r, _ = tbl.GetLongerPrefixDestinations("11.0.0.0/24")
+ assert.Equal(t, len(r), 6)
+}
+
func TestTableDeleteDest(t *testing.T) {
peerT := TableCreatePeer()
pathT := TableCreatePath(peerT)
@@ -146,6 +163,7 @@ func updateMsgT2() *bgp.BGPMessage {
nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "20.20.20.0")}
return bgp.NewBGPUpdateMessage(nil, pathAttributes, nlri)
}
+
func updateMsgT3() *bgp.BGPMessage {
origin := bgp.NewPathAttributeOrigin(0)
aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65100})}