Skip to content

Commit

Permalink
Use NetworkMonitor in zedrouter
Browse files Browse the repository at this point in the history
NetworkMonitor provides all features needed by zedrouter to watch for
changes in the network stack (used mostly for policy based routing
purposes). This way we can remove some code from the zedrouter and
avoid duplication. Moreover, NetworkMonitor is an interface and there
will be multiple implementations for different network stacks
(currently there is only one for Linux).

This is just a small step in the process of zedrouter refactoring.

Signed-off-by: Milan Lenco <milan@zededa.com>
  • Loading branch information
milan-zededa committed Feb 13, 2023
1 parent a116453 commit e2d39fd
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 730 deletions.
8 changes: 4 additions & 4 deletions pkg/pillar/cmd/zedrouter/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1693,10 +1693,10 @@ func executeIPTablesRule(operation string, rule types.IPTablesRule) error {
return err
}

func createFlowMonDummyInterface() {
func createFlowMonDummyInterface(ctx *zedrouterContext) {
// Check if our dummy interface already exits.
link, err := netlink.LinkByName(dummyIntfName)
if link != nil {
_, exists, _ := ctx.networkMonitor.GetInterfaceIndex(dummyIntfName)
if exists {
log.Functionf("createFlowMonDummyInterface: %s already present", dummyIntfName)
return
}
Expand Down Expand Up @@ -1731,7 +1731,7 @@ func createFlowMonDummyInterface() {
}

iifIndex := slink.Attrs().Index
err = AddFwMarkRuleToDummy(iifIndex)
err := AddFwMarkRuleToDummy(ctx, iifIndex)
if err != nil {
log.Errorf("createFlowMonDummyInterface: FwMark rule for %s failed: %s",
dummyIntfName, err)
Expand Down
147 changes: 0 additions & 147 deletions pkg/pillar/cmd/zedrouter/ifindex.go

This file was deleted.

47 changes: 27 additions & 20 deletions pkg/pillar/cmd/zedrouter/networkinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ func doNetworkInstanceActivate(ctx *zedrouterContext,
if status.Type == types.NetworkInstanceTypeSwitch {
// Start meta-data server if the bridge corresponding
// to switch network instance has a valid IPv4 address
bridgeAddr, found := getSwitchIPv4Addr(status.BridgeIfindex)
bridgeAddr, found := getSwitchIPv4Addr(ctx, status.BridgeIfindex)
if found {
status.MetaDataServerIP = bridgeAddr
err = createServer4(ctx, bridgeAddr, status.BridgeName)
Expand All @@ -1325,8 +1325,8 @@ func doNetworkInstanceActivate(ctx *zedrouterContext,
return err
}

func getSwitchIPv4Addr(bridgeIndex int) (string, bool) {
addrs, _, _, err := devicenetwork.GetIPAddrs(log, bridgeIndex)
func getSwitchIPv4Addr(ctx *zedrouterContext, bridgeIndex int) (string, bool) {
addrs, _, err := ctx.networkMonitor.GetInterfaceAddrs(bridgeIndex)
if err == nil {
for _, addr := range addrs {
if addr.IP.IsLinkLocalUnicast() {
Expand All @@ -1343,9 +1343,9 @@ func getSwitchIPv4Addr(bridgeIndex int) (string, bool) {
// In the case where the port maps to multiple underlying ports
// (For Ex: uplink), only include ports that have an ifindex.
//
// If there is no such port with ifindex, then retain the whole list.
// NetworkInstance creation will fail when programming default routes
// and iptable rules in that case - and that should be fine.
// If there is no such port with ifindex, then retain the whole list.
// NetworkInstance creation will fail when programming default routes
// and iptable rules in that case - and that should be fine.
func getIfNameListForLLOrIfname(
ctx *zedrouterContext,
llOrIfname string) []string {
Expand All @@ -1355,21 +1355,12 @@ func getIfNameListForLLOrIfname(

filteredList := make([]string, 0)
for _, ifName := range ifNameList {
// It is perfectly normal for DNS to list ports which do not actually
// exist (and have error reported).
dnsPort := ctx.deviceNetworkStatus.GetPortByIfName(ifName)
if dnsPort != nil {
// XXX - We have a bug in MakeDeviceNetworkStatus where we are allowing
// a device without the corresponding linux interface. We can
// remove this check for ifindex here when the MakeDeviceStatus
// is fixed.
// XXX That bug has been fixed. Retest without this code?
ifIndex, err := IfnameToIndex(log, ifName)
if err == nil {
log.Functionf("ifName %s, ifindex: %d added to filteredList",
ifName, ifIndex)
if _, exists, _ := ctx.networkMonitor.GetInterfaceIndex(ifName); exists {
filteredList = append(filteredList, ifName)
} else {
log.Functionf("ifIndex not found for ifName(%s) - err: %s",
ifName, err.Error())
}
} else {
log.Functionf("DeviceNetworkStatus not found for ifName(%s)",
Expand Down Expand Up @@ -1690,7 +1681,7 @@ func natActivate(ctx *zedrouterContext,
log.Errorf("IptableCmd failed: %s", err)
return err
}
err = PbrRouteAddAll(status.BridgeName, a)
err = PbrRouteAddAll(ctx, status.BridgeName, a)
if err != nil {
log.Errorf("PbrRouteAddAll for Bridge(%s) and interface %s failed. "+
"Err: %s", status.BridgeName, a, err)
Expand Down Expand Up @@ -1725,7 +1716,7 @@ func natInactivate(ctx *zedrouterContext,
net.ParseIP(status.BridgeIPAddr), devicenetwork.PbrNatOutGatewayPrio)
devicenetwork.DelSourceRule(log, status.BridgeIfindex, status.Subnet, true, devicenetwork.PbrNatOutPrio)
devicenetwork.DelInwardSourceRule(log, status.BridgeIfindex, status.Subnet, true, devicenetwork.PbrNatInPrio)
err = PbrRouteDeleteAll(status.BridgeName, oldUplinkIntf)
err = PbrRouteDeleteAll(ctx, status.BridgeName, oldUplinkIntf)
if err != nil {
log.Errorf("natInactivate: PbrRouteDeleteAll failed %s\n", err)
}
Expand Down Expand Up @@ -1924,6 +1915,22 @@ func vifNameToBridgeName(ctx *zedrouterContext, vifName string) string {
return ""
}

// Returns true if ifName references network instance bridge.
func isNIBridge(ctx *zedrouterContext, ifName string) bool {
pub := ctx.pubNetworkInstanceStatus
if pub == nil {
return false
}
instanceItems := pub.GetAll()
for _, st := range instanceItems {
status := st.(types.NetworkInstanceStatus)
if status.BridgeName == ifName {
return true
}
}
return false
}

// Get All ifindices for the Network Instances which are using ifname
func getAllNIindices(ctx *zedrouterContext, ifname string) []int {

Expand Down
Loading

0 comments on commit e2d39fd

Please sign in to comment.