Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evpn: scope mac-mobility handling to MAC-VRF of the route #2805

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/pkg/table/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,16 @@ func (path *Path) SetExtCommunities(exts []bgp.ExtendedCommunityInterface, doRep
}
}

func (path *Path) GetRouteTargets() []bgp.ExtendedCommunityInterface {
rts := make([]bgp.ExtendedCommunityInterface, 0)
for _, ec := range path.GetExtCommunities() {
if t, st := ec.GetTypes(); t <= bgp.EC_TYPE_TRANSITIVE_FOUR_OCTET_AS_SPECIFIC && st == bgp.EC_SUBTYPE_ROUTE_TARGET {
rts = append(rts, ec)
}
}
return rts
}

func (path *Path) GetLargeCommunities() []*bgp.LargeCommunity {
if a := path.getPathAttr(bgp.BGP_ATTR_TYPE_LARGE_COMMUNITY); a != nil {
v := a.(*bgp.PathAttributeLargeCommunities).Values
Expand Down
72 changes: 55 additions & 17 deletions internal/pkg/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ type Table struct {
routeFamily bgp.RouteFamily
destinations map[string]*Destination
logger log.Logger
// index of evpn prefixes with paths to a specific MAC
// this is a map[MAC address]map[prefix]struct{}
// index of evpn prefixes with paths to a specific MAC in a MAC-VRF
// this is a map[rt, MAC address]map[prefix]struct{}
// this holds a map for a set of prefixes.
macIndex map[string]map[string]struct{}
}
Expand Down Expand Up @@ -146,12 +146,15 @@ func (t *Table) deleteDest(dest *Destination) {

if nlri, ok := dest.nlri.(*bgp.EVPNNLRI); ok {
if macadv, ok := nlri.RouteTypeData.(*bgp.EVPNMacIPAdvertisementRoute); ok {
mac := *(*string)(unsafe.Pointer(&macadv.MacAddress))
key := t.tableKey(nlri)
if keys, ok := t.macIndex[mac]; ok {
delete(keys, key)
if len(keys) == 0 {
delete(t.macIndex, mac)
for _, path := range dest.knownPathList {
for _, ec := range path.GetRouteTargets() {
macKey := t.macKey(ec, macadv.MacAddress)
if keys, ok := t.macIndex[macKey]; ok {
delete(keys, t.tableKey(nlri))
if len(keys) == 0 {
delete(t.macIndex, macKey)
}
}
}
}
}
Expand Down Expand Up @@ -214,6 +217,32 @@ func (t *Table) getOrCreateDest(nlri bgp.AddrPrefixInterface, size int) *Destina
return dest
}

func (t *Table) update(newPath *Path) *Update {
t.validatePath(newPath)
dst := t.getOrCreateDest(newPath.GetNlri(), 64)
u := dst.Calculate(t.logger, newPath)

if len(dst.knownPathList) == 0 {
t.deleteDest(dst)
return u
}

if nlri, ok := newPath.GetNlri().(*bgp.EVPNNLRI); ok {
if macadv, ok := nlri.RouteTypeData.(*bgp.EVPNMacIPAdvertisementRoute); ok {
tableKey := t.tableKey(nlri)
for _, ec := range newPath.GetRouteTargets() {
macKey := t.macKey(ec, macadv.MacAddress)
if _, ok := t.macIndex[macKey]; !ok {
t.macIndex[macKey] = make(map[string]struct{})
}
t.macIndex[macKey][tableKey] = struct{}{}
}
}
}

return u
}

func (t *Table) GetDestinations() map[string]*Destination {
return t.destinations
}
Expand Down Expand Up @@ -392,16 +421,19 @@ func (t *Table) GetMUPDestinationsWithRouteType(p string) ([]*Destination, error
}

func (t *Table) setDestination(dst *Destination) {
t.destinations[t.tableKey(dst.nlri)] = dst
tableKey := t.tableKey(dst.nlri)
t.destinations[tableKey] = dst

if nlri, ok := dst.nlri.(*bgp.EVPNNLRI); ok {
if macadv, ok := nlri.RouteTypeData.(*bgp.EVPNMacIPAdvertisementRoute); ok {
mac := *(*string)(unsafe.Pointer(&macadv.MacAddress))
key := t.tableKey(nlri)
if keys, ok := t.macIndex[mac]; ok {
keys[key] = struct{}{}
} else {
t.macIndex[mac] = map[string]struct{}{key: {}}
for _, path := range dst.knownPathList {
for _, ec := range path.GetRouteTargets() {
macKey := t.macKey(ec, macadv.MacAddress)
if _, ok := t.macIndex[macKey]; !ok {
t.macIndex[macKey] = make(map[string]struct{})
}
t.macIndex[macKey][tableKey] = struct{}{}
}
}
}
}
Expand Down Expand Up @@ -437,6 +469,12 @@ func (t *Table) tableKey(nlri bgp.AddrPrefixInterface) string {
return nlri.String()
}

func (t *Table) macKey(rt bgp.ExtendedCommunityInterface, mac net.HardwareAddr) string {
b, _ := rt.Serialize()
b = append(b, mac...)
return *(*string)(unsafe.Pointer(&b))
}

func (t *Table) Bests(id string, as uint32) []*Path {
paths := make([]*Path, 0, len(t.destinations))
for _, dst := range t.destinations {
Expand Down Expand Up @@ -467,9 +505,9 @@ func (t *Table) GetKnownPathList(id string, as uint32) []*Path {
return paths
}

func (t *Table) GetKnownPathListWithMac(id string, as uint32, mac net.HardwareAddr, onlyBest bool) []*Path {
func (t *Table) GetKnownPathListWithMac(id string, as uint32, rt bgp.ExtendedCommunityInterface, mac net.HardwareAddr, onlyBest bool) []*Path {
var paths []*Path
if prefixes, ok := t.macIndex[*(*string)(unsafe.Pointer(&mac))]; ok {
if prefixes, ok := t.macIndex[t.macKey(rt, mac)]; ok {
for prefix := range prefixes {
if dst, ok := t.destinations[prefix]; ok {
if onlyBest {
Expand Down
33 changes: 16 additions & 17 deletions internal/pkg/table/table_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,6 @@ func (manager *TableManager) DeleteVrf(name string) ([]*Path, error) {
return msgs, nil
}

func (manager *TableManager) update(newPath *Path) *Update {
t := manager.Tables[newPath.GetRouteFamily()]
t.validatePath(newPath)
dst := t.getOrCreateDest(newPath.GetNlri(), 64)
u := dst.Calculate(manager.logger, newPath)
if len(dst.knownPathList) == 0 {
t.deleteDest(dst)
}
return u
}

func (manager *TableManager) Update(newPath *Path) []*Update {
if newPath == nil || newPath.IsEOR() {
return nil
Expand All @@ -207,12 +196,12 @@ func (manager *TableManager) Update(newPath *Path) []*Update {
// Except for a special case with EVPN, we'll have one destination.
updates := make([]*Update, 0, 1)
family := newPath.GetRouteFamily()
if _, ok := manager.Tables[family]; ok {
updates = append(updates, manager.update(newPath))
if table, ok := manager.Tables[family]; ok {
updates = append(updates, table.update(newPath))

if family == bgp.RF_EVPN {
for _, p := range manager.handleMacMobility(newPath) {
updates = append(updates, manager.update(p))
updates = append(updates, table.update(p))
}
}
}
Expand Down Expand Up @@ -255,7 +244,17 @@ func (manager *TableManager) handleMacMobility(path *Path) []*Path {
}
e1, et1, m1, s1, i1 := f(path)

for _, path2 := range manager.GetPathListWithMac(GLOBAL_RIB_NAME, 0, []bgp.RouteFamily{bgp.RF_EVPN}, m1) {
// Extract the route targets to scope the lookup to the MAC-VRF with the MAC address.
// This will help large EVPN instances where a single MAC is present in a lot of MAC-VRFs (e.g.
// an anycast router).
// A route may have multiple route targets, to target multiple MAC-VRFs (e.g. in both an L2VNI
// and L3VNI in the VXLAN case).
var paths []*Path
for _, ec := range path.GetRouteTargets() {
paths = append(paths, manager.GetPathListWithMac(GLOBAL_RIB_NAME, 0, []bgp.RouteFamily{bgp.RF_EVPN}, ec, m1)...)
}

for _, path2 := range paths {
if !path2.IsLocal() || path2.GetNlri().(*bgp.EVPNNLRI).RouteType != bgp.EVPN_ROUTE_TYPE_MAC_IP_ADVERTISEMENT {
continue
}
Expand Down Expand Up @@ -326,10 +325,10 @@ func (manager *TableManager) GetPathList(id string, as uint32, rfList []bgp.Rout
return paths
}

func (manager *TableManager) GetPathListWithMac(id string, as uint32, rfList []bgp.RouteFamily, mac net.HardwareAddr) []*Path {
func (manager *TableManager) GetPathListWithMac(id string, as uint32, rfList []bgp.RouteFamily, rt bgp.ExtendedCommunityInterface, mac net.HardwareAddr) []*Path {
var paths []*Path
for _, t := range manager.tables(rfList...) {
paths = append(paths, t.GetKnownPathListWithMac(id, as, mac, false)...)
paths = append(paths, t.GetKnownPathListWithMac(id, as, rt, mac, false)...)
}
return paths
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2191,8 +2191,10 @@ func (s *BgpServer) fixupApiPath(vrfId string, pathList []*table.Path) error {
switch r := nlri.RouteTypeData.(type) {
case *bgp.EVPNMacIPAdvertisementRoute:
// MAC Mobility Extended Community
mac := path.GetNlri().(*bgp.EVPNNLRI).RouteTypeData.(*bgp.EVPNMacIPAdvertisementRoute).MacAddress
paths := s.globalRib.GetPathListWithMac(table.GLOBAL_RIB_NAME, 0, []bgp.RouteFamily{bgp.RF_EVPN}, mac)
var paths []*table.Path
for _, ec := range path.GetRouteTargets() {
paths = append(paths, s.globalRib.GetPathListWithMac(table.GLOBAL_RIB_NAME, 0, []bgp.RouteFamily{bgp.RF_EVPN}, ec, r.MacAddress)...)
}
if m := getMacMobilityExtendedCommunity(r.ETag, r.MacAddress, paths); m != nil {
pm := getMacMobilityExtendedCommunity(r.ETag, r.MacAddress, []*table.Path{path})
if pm == nil {
Expand Down
38 changes: 35 additions & 3 deletions test/scenario_test/evpn_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ def setUpClass(cls):

time.sleep(initial_wait_time)
g1.local("gobgp vrf add vrf1 rd 10:10 rt both 10:10")
g2.local("gobgp vrf add vrf1 rd 10:10 rt both 10:10")
g1.local("gobgp vrf add vrf2 rd 10:20 rt both 10:20")
g2.local("gobgp vrf add vrf1 rd 20:10 rt both 10:10")
g2.local("gobgp vrf add vrf2 rd 20:20 rt both 10:20")

for a, b in combinations(ctns, 2):
a.add_peer(b, vpn=True, passwd='evpn')
Expand Down Expand Up @@ -114,7 +116,7 @@ def test_02_add_evpn_route(self):
def test_03_check_mac_mobility(self):
self.g2.local('gobgp global rib add '
'-a evpn macadv 11:22:33:44:55:66 10.0.0.1 esi AS 2 1 1 etag 1000 label 1000 '
'rd 10:20 rt 10:10')
'rd 20:10 rt 10:10')

time.sleep(3)

Expand All @@ -130,7 +132,7 @@ def test_03_check_mac_mobility(self):
def test_04_check_mac_mobility_again(self):
self.g1.local('gobgp global rib add '
'-a evpn macadv 11:22:33:44:55:66 10.0.0.1 esi AS 3 1 1 etag 1000 label 1000 '
'rd 10:20 rt 10:10')
'rd 10:10 rt 10:10')

time.sleep(3)

Expand All @@ -143,6 +145,36 @@ def test_04_check_mac_mobility_again(self):
self.assertTrue(path['nexthop'] in n_addrs)
self.assertEqual(get_mac_mobility_sequence(path['attrs']), 1)

def test_05_check_mac_mobility_per_mac_vrf(self):
self.g2.local('gobgp global rib add '
'-a evpn macadv 11:22:33:44:55:66 10.0.0.1 esi AS 4 1 1 etag 2000 label 2000 '
'rd 20:20 rt 10:20')

time.sleep(3)

grib = self.g2.get_global_rib(rf='evpn')
self.assertEqual(len(grib), 2)
# first route is from previous tests
dst = grib[0]
self.assertEqual(len(dst['paths']), 1)
path = dst['paths'][0]
n_addrs = [i[1].split('/')[0] for i in self.g1.ip_addrs]
self.assertTrue(path['nexthop'] in n_addrs)
self.assertEqual(get_mac_mobility_sequence(path['attrs']), 1)

# dump global rib again on other gobgp instance to have our second route have the nexthop
# filled out. otherwise it'd be 0.0.0.0.
grib = self.g1.get_global_rib(rf='evpn')
self.assertEqual(len(grib), 2)
# second route from this test, in another mac-vrf
dst = grib[1]
self.assertEqual(len(dst['paths']), 1)
path = dst['paths'][0]
n_addrs = [i[1].split('/')[0] for i in self.g2.ip_addrs]
self.assertTrue(path['nexthop'] in n_addrs)
# no mac mobility for this route
self.assertEqual(get_mac_mobility_sequence(path['attrs']), -1)


if __name__ == '__main__':
output = local("which docker 2>&1 > /dev/null ; echo $?", capture=True)
Expand Down
Loading