Skip to content

Commit

Permalink
Add UT for VXLANFDB.
Browse files Browse the repository at this point in the history
  • Loading branch information
fasaxc committed Jan 26, 2024
1 parent 8ae03ae commit b6944fb
Show file tree
Hide file tree
Showing 4 changed files with 374 additions and 21 deletions.
42 changes: 39 additions & 3 deletions felix/netlinkshim/mocknetlink/netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ type MockNetlinkDataplane struct {
}

type NeighKey struct {
MAC string
LinkIndex int
MAC string
IP ip.Addr
}

Expand Down Expand Up @@ -485,6 +486,7 @@ func (d *MockNetlinkDataplane) LinkByName(name string) (netlink.Link, error) {
if d.DeleteInterfaceAfterLinkByName {
defer delete(d.NameToLink, name)
}
log.Debugf("Looking for interface: %s", name)
if link, ok := d.NameToLink[name]; ok {
return link.copy(), nil
}
Expand Down Expand Up @@ -880,6 +882,27 @@ func (d *MockNetlinkDataplane) RouteDel(route *netlink.Route) error {
}
}

// AddNeighs allows test code to add neighbours to the mock dataplane
// without going through the netlink API.
func (d *MockNetlinkDataplane) AddNeighs(family int, neighs ...netlink.Neigh) {
err := d.checkNeighFamily(family)
if err != nil {
panic(err)
}
if d.NeighsByFamily[family] == nil {
d.NeighsByFamily[family] = map[NeighKey]*netlink.Neigh{}
}
for _, neigh := range neighs {
neigh := neigh
nk := NeighKey{
LinkIndex: neigh.LinkIndex,
MAC: neigh.HardwareAddr.String(),
IP: ip.FromNetIP(neigh.IP),
}
d.NeighsByFamily[family][nk] = &neigh
}
}

func (d *MockNetlinkDataplane) NeighAdd(neigh *netlink.Neigh) error {
family := neigh.Family
err := d.checkNeighFamily(family)
Expand All @@ -896,7 +919,11 @@ func (d *MockNetlinkDataplane) NeighAdd(neigh *netlink.Neigh) error {
if neigh.HardwareAddr == nil {
return unix.EINVAL
}
if neigh.LinkIndex == 0 {
return unix.EINVAL
}
nk := NeighKey{
LinkIndex: neigh.LinkIndex,
MAC: neigh.HardwareAddr.String(),
IP: ip.FromNetIP(neigh.IP),
}
Expand Down Expand Up @@ -951,7 +978,11 @@ func (d *MockNetlinkDataplane) NeighSet(neigh *netlink.Neigh) error {
if neigh.HardwareAddr == nil {
return unix.EINVAL
}
if neigh.LinkIndex == 0 {
return unix.EINVAL
}
nk := NeighKey{
LinkIndex: neigh.LinkIndex,
MAC: neigh.HardwareAddr.String(),
IP: ip.FromNetIP(neigh.IP),
}
Expand All @@ -976,7 +1007,11 @@ func (d *MockNetlinkDataplane) NeighDel(neigh *netlink.Neigh) error {
if neigh.HardwareAddr == nil {
return unix.EINVAL
}
if neigh.LinkIndex == 0 {
return unix.EINVAL
}
nk := NeighKey{
LinkIndex: neigh.LinkIndex,
MAC: neigh.HardwareAddr.String(),
IP: ip.FromNetIP(neigh.IP),
}
Expand Down Expand Up @@ -1011,8 +1046,9 @@ func (d *MockNetlinkDataplane) AddStaticArpEntry(cidr ip.CIDR, destMAC net.Hardw

linkIndex := d.NameToLink[ifaceName].LinkAttrs.Index
d.NeighsByFamily[unix.AF_INET][NeighKey{
MAC: destMAC.String(),
IP: cidr.Addr(),
LinkIndex: linkIndex,
MAC: destMAC.String(),
IP: cidr.Addr(),
}] = &netlink.Neigh{
Family: unix.AF_INET,
LinkIndex: linkIndex,
Expand Down
59 changes: 41 additions & 18 deletions felix/vxlanfdb/vxlan_fdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,39 @@ type VTEP struct {
}

type VXLANFDB struct {
ifaceName string
ifIndex int
arpEntries *deltatracker.DeltaTracker[string, ipMACMapping]
fdbEntries *deltatracker.DeltaTracker[string, ipMACMapping]
logCxt *log.Entry
resyncPending bool
logNextSuccess bool
nl *handlemgr.HandleManager
family int
ifaceName string
ifIndex int
arpEntries *deltatracker.DeltaTracker[string, ipMACMapping]
fdbEntries *deltatracker.DeltaTracker[string, ipMACMapping]
logCxt *log.Entry
resyncPending bool
logNextSuccess bool
nl *handlemgr.HandleManager
family int

newNetlinkHandle func() (netlinkshim.Interface, error)
}

type ipMACMapping struct {
IP ip.Addr
MAC net.HardwareAddr
}

func New(family int, ifaceName string, featureDetector environment.FeatureDetectorIface, netlinkTimeout time.Duration) *VXLANFDB {
type VXLANFDBOption func(*VXLANFDB)

func WithNetlinkHandleShim(newNetlinkHandle func() (netlinkshim.Interface, error)) VXLANFDBOption {
return func(fdb *VXLANFDB) {
fdb.newNetlinkHandle = newNetlinkHandle
}
}

func New(
family int,
ifaceName string,
featureDetector environment.FeatureDetectorIface,
netlinkTimeout time.Duration,
opts ...VXLANFDBOption,
) *VXLANFDB {
switch family {
case unix.AF_INET, unix.AF_INET6:
default:
Expand All @@ -77,14 +93,21 @@ func New(family int, ifaceName string, featureDetector environment.FeatureDetect
}),
resyncPending: true,
logNextSuccess: true,
nl: handlemgr.NewHandleManager(
featureDetector,
handlemgr.WithSocketTimeout(netlinkTimeout),
// The Netlink library doesn't seem to be able to list
// both types of neighbors in strict mode.
handlemgr.WithStrictModeOverride(false),
),
newNetlinkHandle: netlinkshim.NewRealNetlink,
}

for _, o := range opts {
o(&f)
}

f.nl = handlemgr.NewHandleManager(
featureDetector,
handlemgr.WithSocketTimeout(netlinkTimeout),
// The Netlink library doesn't seem to be able to list
// both types of neighbors in strict mode.
handlemgr.WithStrictModeOverride(false),
handlemgr.WithNewHandleOverride(f.newNetlinkHandle),
)
return &f
}

Expand Down Expand Up @@ -287,7 +310,7 @@ func (r *VXLANFDB) resync(nl netlinkshim.Interface) error {
}
r.ifIndex = link.Attrs().Index

// Refresh the neighbours.
// Refresh the neighbors.
existingNeigh, err := nl.NeighList(r.ifIndex, unix.AF_INET)
if err != nil {
r.logCxt.WithError(err).Error("Failed to list neighbors")
Expand Down
Loading

0 comments on commit b6944fb

Please sign in to comment.