Skip to content

Commit a7f4239

Browse files
committed
pkg/packet/bgp: move ContainsCIDR to table package
Move ContainsCIDR to table package and make it private. Signed-off-by: FUJITA Tomonori <[email protected]>
1 parent 4b73d20 commit a7f4239

File tree

4 files changed

+77
-77
lines changed

4 files changed

+77
-77
lines changed

internal/pkg/table/table.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,13 @@ func (t *Table) GetKnownPathListWithMac(id string, as uint32, rt bgp.ExtendedCom
620620
return paths
621621
}
622622

623+
// ContainsCIDR checks if one IPNet is a subnet of another.
624+
func containsCIDR(n1, n2 *net.IPNet) bool {
625+
ones1, _ := n1.Mask.Size()
626+
ones2, _ := n2.Mask.Size()
627+
return ones1 <= ones2 && n1.Contains(n2.IP)
628+
}
629+
623630
func (t *Table) Select(option ...TableSelectOption) (*Table, error) {
624631
id := GLOBAL_RIB_NAME
625632
var vrf *Vrf
@@ -754,7 +761,7 @@ func (t *Table) Select(option ...TableSelectOption) (*Table, error) {
754761
for _, dst := range t.GetDestinations() {
755762
tablePrefix := nlriToIPNet(dst.nlri)
756763

757-
if bgp.ContainsCIDR(prefix, tablePrefix) {
764+
if containsCIDR(prefix, tablePrefix) {
758765
r.setDestination(dst)
759766
}
760767
}
@@ -782,7 +789,7 @@ func (t *Table) Select(option ...TableSelectOption) (*Table, error) {
782789
for _, dst := range t.GetDestinations() {
783790
tablePrefix := nlriToIPNet(dst.nlri)
784791

785-
if bgp.ContainsCIDR(tablePrefix, prefix) {
792+
if containsCIDR(tablePrefix, prefix) {
786793
r.setDestination(dst)
787794
}
788795
}

internal/pkg/table/table_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,3 +773,71 @@ func Test_RouteTargetKey(t *testing.T) {
773773
_, err = extCommRouteTargetKey(r.RouteTarget)
774774
assert.NotNil(err)
775775
}
776+
777+
func TestContainsCIDR(t *testing.T) {
778+
tests := []struct {
779+
name string
780+
prefix1 string
781+
prefix2 string
782+
result bool
783+
}{
784+
{
785+
name: "v4 prefix2 is a subnet of prefix1",
786+
prefix1: "172.17.0.0/16",
787+
prefix2: "172.17.192.0/18",
788+
result: true,
789+
},
790+
{
791+
name: "v4 prefix2 is a supernet of prefix1",
792+
prefix1: "172.17.191.0/18",
793+
prefix2: "172.17.0.0/16",
794+
result: false,
795+
},
796+
{
797+
name: "v4 prefix2 is not a subnet of prefix1",
798+
prefix1: "10.10.20.0/30",
799+
prefix2: "10.10.30.3/32",
800+
result: false,
801+
},
802+
{
803+
name: "v4 prefix2 is equal to prefix1",
804+
prefix1: "10.10.20.0/30",
805+
prefix2: "10.10.20.0/30",
806+
result: true,
807+
},
808+
{
809+
name: "v6 prefix2 is not a subnet of prefix1",
810+
prefix1: "1::/64",
811+
prefix2: "2::/72",
812+
result: false,
813+
},
814+
{
815+
name: "v6 prefix2 is a supernet of prefix1",
816+
prefix1: "1::/64",
817+
prefix2: "1::/32",
818+
result: false,
819+
},
820+
{
821+
name: "v6 prefix2 is a subnet of prefix1",
822+
prefix1: "1::/64",
823+
prefix2: "1::/112",
824+
result: true,
825+
},
826+
{
827+
name: "v6 prefix2 is equal to prefix1",
828+
prefix1: "100:100::/64",
829+
prefix2: "100:100::/64",
830+
result: true,
831+
},
832+
}
833+
834+
for _, tt := range tests {
835+
t.Run(tt.name, func(t *testing.T) {
836+
_, prefixNet1, _ := net.ParseCIDR(tt.prefix1)
837+
_, prefixNet2, _ := net.ParseCIDR(tt.prefix2)
838+
839+
result := containsCIDR(prefixNet1, prefixNet2)
840+
assert.Equal(t, tt.result, result)
841+
})
842+
}
843+
}

pkg/packet/bgp/bgp.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,13 +1886,6 @@ func ParseVPNPrefix(prefix string) (RouteDistinguisherInterface, netip.Prefix, e
18861886
return rd, p, err
18871887
}
18881888

1889-
// ContainsCIDR checks if one IPNet is a subnet of another.
1890-
func ContainsCIDR(n1, n2 *net.IPNet) bool {
1891-
ones1, _ := n1.Mask.Size()
1892-
ones2, _ := n2.Mask.Size()
1893-
return ones1 <= ones2 && n1.Contains(n2.IP)
1894-
}
1895-
18961889
//
18971890
// RFC3107 Carrying Label Information in BGP-4
18981891
//

pkg/packet/bgp/bgp_test.go

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,74 +1326,6 @@ func TestParseVPNPrefix(t *testing.T) {
13261326
}
13271327
}
13281328

1329-
func TestContainsCIDR(t *testing.T) {
1330-
tests := []struct {
1331-
name string
1332-
prefix1 string
1333-
prefix2 string
1334-
result bool
1335-
}{
1336-
{
1337-
name: "v4 prefix2 is a subnet of prefix1",
1338-
prefix1: "172.17.0.0/16",
1339-
prefix2: "172.17.192.0/18",
1340-
result: true,
1341-
},
1342-
{
1343-
name: "v4 prefix2 is a supernet of prefix1",
1344-
prefix1: "172.17.191.0/18",
1345-
prefix2: "172.17.0.0/16",
1346-
result: false,
1347-
},
1348-
{
1349-
name: "v4 prefix2 is not a subnet of prefix1",
1350-
prefix1: "10.10.20.0/30",
1351-
prefix2: "10.10.30.3/32",
1352-
result: false,
1353-
},
1354-
{
1355-
name: "v4 prefix2 is equal to prefix1",
1356-
prefix1: "10.10.20.0/30",
1357-
prefix2: "10.10.20.0/30",
1358-
result: true,
1359-
},
1360-
{
1361-
name: "v6 prefix2 is not a subnet of prefix1",
1362-
prefix1: "1::/64",
1363-
prefix2: "2::/72",
1364-
result: false,
1365-
},
1366-
{
1367-
name: "v6 prefix2 is a supernet of prefix1",
1368-
prefix1: "1::/64",
1369-
prefix2: "1::/32",
1370-
result: false,
1371-
},
1372-
{
1373-
name: "v6 prefix2 is a subnet of prefix1",
1374-
prefix1: "1::/64",
1375-
prefix2: "1::/112",
1376-
result: true,
1377-
},
1378-
{
1379-
name: "v6 prefix2 is equal to prefix1",
1380-
prefix1: "100:100::/64",
1381-
prefix2: "100:100::/64",
1382-
result: true,
1383-
},
1384-
}
1385-
1386-
for _, tt := range tests {
1387-
t.Run(tt.name, func(t *testing.T) {
1388-
_, prefixNet1, _ := net.ParseCIDR(tt.prefix1)
1389-
_, prefixNet2, _ := net.ParseCIDR(tt.prefix2)
1390-
1391-
result := ContainsCIDR(prefixNet1, prefixNet2)
1392-
assert.Equal(t, tt.result, result)
1393-
})
1394-
}
1395-
}
1396-
13971329
func Test_ParseEthernetSegmentIdentifier(t *testing.T) {
13981330
assert := assert.New(t)
13991331

0 commit comments

Comments
 (0)