Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into drop1.0
Browse files Browse the repository at this point in the history
Signed-off-by: atulpatel261194 <Atul.Patel@intel.com>
  • Loading branch information
atulpatel261194 committed Sep 1, 2024
2 parents 2d9d9cd + aeedab7 commit 2c2aa05
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 80 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ var logger *log.Logger
func setupLogger(filename string) {
var err error
filename = filepath.Clean(filename)
out, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
out, err := os.OpenFile(filename, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
log.Panic(err)
}
Expand Down
59 changes: 27 additions & 32 deletions pkg/infradb/infradb.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,25 @@ func UpdateLB(lb *LogicalBridge) error {

return nil
}
func removeVniFromVpns(vni uint32) error {
vpns := make(map[uint32]bool)
if vni != 0 {
found, err := infradb.client.Get("vpns", &vpns)
if err != nil {
return err
}
if !found {
return ErrKeyNotFound
}
delete(vpns, vni)

err = infradb.client.Set("vpns", &vpns)
if err != nil {
return err
}
}
return nil
}

// UpdateLBStatus updates the status of logical bridge object based on the component report
// nolint: funlen
Expand Down Expand Up @@ -321,23 +340,11 @@ func UpdateLBStatus(name string, resourceVersion string, notificationID string,
}

// Delete VNI from the VPN map
vpns := make(map[uint32]bool)
found, err = infradb.client.Get("vpns", &vpns)
if err != nil {
log.Println(err)
return err
}
if !found {
log.Println("UpdateLBStatus(): No VPNs have been found")
return ErrKeyNotFound
}
if lb.Spec.Vni != nil {
delete(vpns, *lb.Spec.Vni)
}
err = infradb.client.Set("vpns", &vpns)
if err != nil {
log.Println(err)
return err
err = removeVniFromVpns(*lb.Spec.Vni)
if err != nil {
return err
}
}

lbs := make(map[string]bool)
Expand Down Expand Up @@ -975,23 +982,11 @@ func UpdateVrfStatus(name string, resourceVersion string, notificationID string,
}

// Delete VNI from the VPN map
vpns := make(map[uint32]bool)
found, err = infradb.client.Get("vpns", &vpns)
if err != nil {
log.Println(err)
return err
}
if !found {
log.Println("UpdateVrfStatus(): No VPNs have been found")
return ErrKeyNotFound
}
if vrf.Spec.Vni != nil {
delete(vpns, *vrf.Spec.Vni)
}
err = infradb.client.Set("vpns", &vpns)
if err != nil {
log.Println(err)
return err
err = removeVniFromVpns(*vrf.Spec.Vni)
if err != nil {
return err
}
}

// Delete VRF from VRFs Map
Expand Down
2 changes: 1 addition & 1 deletion pkg/netlink/fdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func checkFdbType(fdbtype int) bool {
}

// installFilterFDB install fdb filer
func (fdb *FdbEntryStruct) filter() bool {
func (fdb *FdbEntryStruct) installFilterFDB() bool {
// Drop entries w/o VLAN ID or associated LogicalBridge ...
// ... other than with L2 nexthops of type VXLAN and BridgePort ...
// ... and VXLAN entries with unresolved underlay nextop.
Expand Down
2 changes: 1 addition & 1 deletion pkg/netlink/l2nexthop.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (l2n *L2NexthopStruct) annotate() {
}

// installFilterL2N install the l2 filter
func (l2n *L2NexthopStruct) filter() bool {
func (l2n *L2NexthopStruct) installFilterL2N() bool {
keep := !(l2n.Type == 0 && l2n.Resolved && len(l2n.FdbRefs) == 0)
return keep
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/netlink/neighbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,8 @@ func cmdProcessNb(nbs []NeighIPStruct, v string) NeighList {
// addNeigh adds the neigh
func addNeigh(dump NeighList) {
for _, n := range dump.NS {
n.neighborAnnotate()
if len(latestNeighbors) == 0 {
latestNeighbors[n.Key] = n
} else if !CheckNdup(n.Key) {
n = n.neighborAnnotate()
if len(latestNeighbors) == 0 || !CheckNdup(n.Key) {
latestNeighbors[n.Key] = n
}
}
Expand Down
90 changes: 51 additions & 39 deletions pkg/netlink/netlink_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,57 +35,67 @@ func notifyDBChanges() {
notifyDBCompChanges[L2NexthopKey, *L2NexthopStruct](latestL2Nexthop, l2Nexthops, L2NEXTHOP, l2NexthopOperations)
}

// dboperations interface
type dboperations interface {
annotate()
filter() bool
// Filterable method for each type
type Filterable interface {
ShouldFilter() bool
}

type genericDB[K comparable, V dboperations] struct {
compDB map[K]V
// applyInstallFilters install the filters
func applyInstallFilters() {
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() })
}

func newGenericDB[K comparable, V dboperations](data map[K]V) *genericDB[K, V] {
return &genericDB[K, V]{compDB: data}
// 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)
}
}
}

func (db *genericDB[K, V]) annotate() {
for key, value := range db.compDB {
value.annotate()
db.compDB[key] = value
}
// ShouldFilter method for each type
func (route *RouteStruct) ShouldFilter() bool {
return route.installFilterRoute()
}

func (db *genericDB[K, V]) filter() {
for key, value := range db.compDB {
if !value.filter() {
delete(db.compDB, key)
}
}
// ShouldFilter method for each type
func (nexthop *NexthopStruct) ShouldFilter() bool {
return nexthop.installFilterNH()
}

// annotateAndFilterDB annonates and filters the latest db updates
func annotateAndFilterDB() {
latestNexthopDB := newGenericDB(latestNexthop)
latestNexthopDB.annotate()
latestNexthopDB.filter()
// ShouldFilter method for each type
func (fdb *FdbEntryStruct) ShouldFilter() bool {
return fdb.installFilterFDB()
}

latestRoutesDB := newGenericDB(latestRoutes)
latestRoutesDB.annotate()
latestRoutesDB.filter()
// ShouldFilter method for each type
func (l *L2NexthopStruct) ShouldFilter() bool {
return l.installFilterL2N()
}

latestFdbDB := newGenericDB(latestFDB)
latestFdbDB.annotate()
latestFdbDB.filter()
// Annotatable interface
type Annotatable interface {
annotate()
}

latestL2NexthopDB := newGenericDB(latestL2Nexthop)
latestL2NexthopDB.annotate()
latestL2NexthopDB.filter()
// 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
}
}

latestNexthop = latestNexthopDB.compDB
latestRoutes = latestRoutesDB.compDB
latestFDB = latestFdbDB.compDB
latestL2Nexthop = latestL2NexthopDB.compDB
// annotateDBEntries annonates the latest db updates
func annotateDBEntries() {
annotateMap(latestNexthop)
annotateMap(latestRoutes)
annotateMap(latestFDB)
annotateMap(latestL2Nexthop)
}

// readLatestNetlinkState reads the latest netlink state
Expand All @@ -106,8 +116,10 @@ func readLatestNetlinkState() {
func resyncWithKernel() {
// Build a new DB snapshot from netlink and other sources
readLatestNetlinkState()
// Annotate and filter the latest DB entries
annotateAndFilterDB()
// Annotate the latest DB entries
annotateDBEntries()
// Filter the latest DB to retain only entries to be installed
applyInstallFilters()
// Compute changes between current and latest DB versions and inform subscribers about the changes
notifyDBChanges()
routes = latestRoutes
Expand Down
2 changes: 1 addition & 1 deletion pkg/netlink/nexthop.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func checkNhType(nType int) bool {
}

// installFilterNH install the neighbor filter
func (nexthop *NexthopStruct) filter() bool {
func (nexthop *NexthopStruct) installFilterNH() bool {
check := checkNhType(nexthop.NhType)
keep := check && nexthop.Resolved && len(nexthop.RouteRefs) != 0
return keep
Expand Down
2 changes: 1 addition & 1 deletion pkg/netlink/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func checkRtype(rType string) bool {
}

// installFilterRoute install the route filter
func (route *RouteStruct) filter() bool {
func (route *RouteStruct) installFilterRoute() bool {
var nh []*NexthopStruct
for _, n := range route.Nexthops {
if n.Resolved {
Expand Down

0 comments on commit 2c2aa05

Please sign in to comment.