Skip to content

Commit

Permalink
fix(evpn): netlink reviews
Browse files Browse the repository at this point in the history
Signed-off-by: atulpatel261194 <[email protected]>
  • Loading branch information
atulpatel261194 committed Aug 7, 2024
1 parent 58512e1 commit bb3e67f
Showing 1 changed file with 64 additions and 48 deletions.
112 changes: 64 additions & 48 deletions pkg/netlink/netlink_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,54 +35,69 @@ func notifyDBChanges() {
notifyDBCompChanges[L2NexthopKey, *L2NexthopStruct](latestL2Nexthop, l2Nexthops, L2NEXTHOP, l2NexthopOperations)
}

// nolint
// Filterable method for each type
type Filterable interface {
ShouldFilter() bool
}

// applyInstallFilters install the filters
func applyInstallFilters() {
for key, route := range latestRoutes {
if !route.installFilterRoute() {
// Remove route from its nexthop(s)
delete(latestRoutes, key)
}
}
filterMap(latestRoutes, func(r *RouteStruct) bool { return r.installFilterRoute() })
filterMap(latestNexthop, func(n *NexthopStruct) bool { return n.installFilterNH() })
filterMap(latestFDB, func(f *FdbEntryStruct) bool { return f.installFilterFDB() })
filterMap(latestL2Nexthop, func(l *L2NexthopStruct) bool { return l.installFilterL2N() })
}

for key, nexthop := range latestNexthop {
if !nexthop.installFilterNH() {
delete(latestNexthop, key)
// filterMap install the filters
func filterMap[K comparable, V Filterable](m map[K]V, shouldFilter func(V) bool) {
for key, value := range m {
if !shouldFilter(value) {
delete(m, key)
}
}
}

for key, fdb := range latestFDB {
if !fdb.installFilterFDB() {
delete(latestFDB, key)
}
}
for key, l2 := range latestL2Nexthop {
if !l2.installFilterL2N() {
delete(latestL2Nexthop, key)
}
}
// ShouldFilter method for each type
func (route *RouteStruct) ShouldFilter() bool {
return route.installFilterRoute()
}

// annotateDBEntries annonates the database entries
func annotateDBEntries() {
for _, nexthop := range latestNexthop {
nexthop.annotate()
latestNexthop[nexthop.Key] = nexthop
}
for _, route := range latestRoutes {
route.annotate()
latestRoutes[route.Key] = route
}
// ShouldFilter method for each type
func (nexthop *NexthopStruct) ShouldFilter() bool {
return nexthop.installFilterNH()
}

for _, fdb := range latestFDB {
fdb.annotate()
latestFDB[fdb.Key] = fdb
}
for _, l2n := range latestL2Nexthop {
l2n.annotate()
latestL2Nexthop[l2n.Key] = l2n
// ShouldFilter method for each type
func (fdb *FdbEntryStruct) ShouldFilter() bool {
return fdb.installFilterFDB()
}

// ShouldFilter method for each type
func (l *L2NexthopStruct) ShouldFilter() bool {
return l.installFilterL2N()
}

// Annotatable interface
type Annotatable interface {
annotate()
}

// annotateMap annonates the latest db map updates
func annotateMap[K comparable, V Annotatable](m map[K]V) {
for key, value := range m {
value.annotate()
m[key] = value
}
}

// annotateDBEntries annonates the latest db updates
func annotateDBEntries() {
annotateMap(latestNexthop)
annotateMap(latestRoutes)
annotateMap(latestFDB)
annotateMap(latestL2Nexthop)
}

// readLatestNetlinkState reads the latest netlink state
func readLatestNetlinkState() {
vrfs, _ := infradb.GetAllVrfs()
Expand Down Expand Up @@ -114,6 +129,15 @@ func resyncWithKernel() {
deleteLatestDB()
}

// notifyUpdates notifies the db updates
func notifyUpdates[K comparable, V any](items map[K]V, deletionType string) {
for _, item := range items {
notifyAddDel(item, deletionType)
}
}

// Usage

// monitorNetlink moniters the netlink
func monitorNetlink() {
for !stopMonitoring.Load() {
Expand All @@ -125,17 +149,9 @@ func monitorNetlink() {
log.Printf("netlink: One final netlink poll to identify what's still left.")
// Inform subscribers to delete configuration for any still remaining Netlink DB objects.
log.Printf("netlink: Delete any residual objects in DB")
for _, route := range routes {
notifyAddDel(route, RouteDeleted)
}

for _, nexthop := range nexthops {
notifyAddDel(nexthop, NexthopDeleted)
}

for _, fdb := range fDB {
notifyAddDel(fdb, FdbEntryDeleted)
}
notifyUpdates(routes, RouteDeleted)
notifyUpdates(nexthops, NexthopDeleted)
notifyUpdates(fDB, FdbEntryDeleted)
log.Printf("netlink: DB cleanup completed.")
}

Expand Down

0 comments on commit bb3e67f

Please sign in to comment.