From 9d0ec8c8c1a2b8d7c10d32f3b0df55090815aa2a Mon Sep 17 00:00:00 2001 From: Kaelem Chandra Date: Thu, 16 Jan 2025 21:49:33 +1300 Subject: [PATCH 1/2] Rework IOL interface mapping --- nodes/iol/iol.go | 56 +++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/nodes/iol/iol.go b/nodes/iol/iol.go index f84697ae3..9360c41e0 100644 --- a/nodes/iol/iol.go +++ b/nodes/iol/iol.go @@ -44,9 +44,15 @@ var ( //go:embed iol.cfg.tmpl cfgTemplate string - InterfaceRegexp = regexp.MustCompile(`(?:e|Ethernet)\s?(?P\d+)/(?P\d+)$`) - InterfaceOffset = 1 - InterfaceHelp = "eX/Y or EthernetX/Y (where X >= 0 and Y >= 1)" + // IntfRegexp with named capture groups for extracting slot and port + CapturingIntfRegexp = regexp.MustCompile(`(?:e|Ethernet)\s?(?P\d+)/(?P\d+)$`) + // ethX naming is the "raw" or "default" interface naming + DefaultIntfRegexp = regexp.MustCompile(`eth[1-9][0-9]*$`) + // Match on the management interface + MgmtIntfRegexp = regexp.MustCompile(`(eth0|e0/0|Ethernet0/0)$`) + // Matches on any allowed/legal interface name + AllowedIntfRegexp = regexp.MustCompile("Ethernet((0/[1-3])|([1-9]/[0-3]))$|e((0/[1-3])|([1-9]/[0-9]))$|eth[1-9][0-9]*$") + IntfHelpMsg = "Interfaces should follow Ethernet/ or e/ naming convention, where is a number from 0-9 and is a number from 0-3. You can also use ethX-based interface naming." validTypes = []string{typeIOL, typeL2} ) @@ -118,10 +124,6 @@ func (n *iol) Init(cfg *types.NodeConfig, opts ...nodes.NodeOption) error { fmt.Sprint(filepath.Join(n.Cfg.LabDir, "NETMAP"), ":/iol/NETMAP"), ) - n.InterfaceRegexp = InterfaceRegexp - n.InterfaceOffset = InterfaceOffset - n.InterfaceHelp = InterfaceHelp - return nil } @@ -280,7 +282,7 @@ type IOLInterface struct { } func (n *iol) GetMappedInterfaceName(ifName string) (string, error) { - captureGroups, err := utils.GetRegexpCaptureGroups(n.InterfaceRegexp, ifName) + captureGroups, err := utils.GetRegexpCaptureGroups(CapturingIntfRegexp, ifName) if err != nil { return "", err } @@ -312,30 +314,35 @@ func (n *iol) GetMappedInterfaceName(ifName string) (string, error) { } } -var DefaultIntfRegexp = regexp.MustCompile(`eth[1-9][0-9]*$`) - -// AddEndpoint override version maps the endpoint name to an ethX-based name before adding it to the node endpoints. Returns an error if the mapping goes wrong. +// AddEndpoint override maps the endpoint name to an ethX-based naming where neccesary, before adding it to the node endpoints. Returns an error if the mapping goes wrong or if the +// interface name is NOT allowed. func (n *iol) AddEndpoint(e links.Endpoint) error { endpointName := e.GetIfaceName() - // Slightly modified check: if it doesn't match the DefaultIntfRegexp, pass it to GetMappedInterfaceName. If it fails, then the interface name is wrong. - if n.InterfaceRegexp != nil && !(DefaultIntfRegexp.MatchString(endpointName)) { + var IFaceName, IFaceAlias string + + IFaceName = endpointName + + if !(DefaultIntfRegexp.MatchString(endpointName)) && AllowedIntfRegexp.MatchString(endpointName) { + log.Debugf("%s: %s needs mapping", n.Cfg.ShortName, endpointName) mappedName, err := n.GetMappedInterfaceName(endpointName) if err != nil { - return fmt.Errorf("%q interface name %q could not be mapped to an ethX-based interface name: %w", - n.Cfg.ShortName, e.GetIfaceName(), err) + return fmt.Errorf("%q interface name %q could not be mapped to an ethX-based interface name: %w\n%s", + n.Cfg.ShortName, e.GetIfaceName(), err, IntfHelpMsg) } log.Debugf("Interface Mapping: Mapping interface %q (ifAlias) to %q (ifName)", endpointName, mappedName) - e.SetIfaceName(mappedName) - e.SetIfaceAlias(endpointName) + IFaceName = mappedName + IFaceAlias = endpointName } + + e.SetIfaceName(IFaceName) + // should be nil if ethX naming is used. + e.SetIfaceAlias(IFaceAlias) n.Endpoints = append(n.Endpoints, e) return nil } func (n *iol) CheckInterfaceName() error { - // allow interface naming as Ethernet/ or e/ - InterfaceRegexp := regexp.MustCompile("Ethernet((0/[1-3])|([1-9]/[0-3]))$|e((0/[1-3])|([1-9]/[0-9]))$") err := n.CheckInterfaceOverlap() if err != nil { @@ -343,10 +350,15 @@ func (n *iol) CheckInterfaceName() error { } for _, e := range n.Endpoints { - IFaceName := e.GetIfaceAlias() - if !InterfaceRegexp.MatchString(IFaceName) { - return fmt.Errorf("IOL Node %q has an interface named %q which doesn't match the required pattern. Interfaces should be defined contigiously and named as Ethernet/ or e/, where is a number from 0-9 and is a number from 0-3. Management interface Ethernet0/0 cannot be used", n.Cfg.ShortName, IFaceName) + IFaceName := e.GetIfaceName() + if MgmtIntfRegexp.MatchString(IFaceName) { + return fmt.Errorf("IOL Node: %q. Management interface Ethernet0/0, e0/0 or eth0 is not allowed", n.Cfg.ShortName) + } + + if !DefaultIntfRegexp.MatchString(IFaceName) { + return fmt.Errorf("IOL Node %q has an interface named %q which doesn't match the required pattern. %s", n.Cfg.ShortName, IFaceName, IntfHelpMsg) } + } return nil From b50a7d5e6e44fcc1f519c581b5347a5eae5566ae Mon Sep 17 00:00:00 2001 From: Kaelem Chandra Date: Thu, 16 Jan 2025 21:54:44 +1300 Subject: [PATCH 2/2] `make format` --- nodes/iol/iol.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/nodes/iol/iol.go b/nodes/iol/iol.go index 9360c41e0..514b24d8c 100644 --- a/nodes/iol/iol.go +++ b/nodes/iol/iol.go @@ -44,13 +44,13 @@ var ( //go:embed iol.cfg.tmpl cfgTemplate string - // IntfRegexp with named capture groups for extracting slot and port + // IntfRegexp with named capture groups for extracting slot and port. CapturingIntfRegexp = regexp.MustCompile(`(?:e|Ethernet)\s?(?P\d+)/(?P\d+)$`) - // ethX naming is the "raw" or "default" interface naming + // ethX naming is the "raw" or "default" interface naming. DefaultIntfRegexp = regexp.MustCompile(`eth[1-9][0-9]*$`) - // Match on the management interface + // Match on the management interface. MgmtIntfRegexp = regexp.MustCompile(`(eth0|e0/0|Ethernet0/0)$`) - // Matches on any allowed/legal interface name + // Matches on any allowed/legal interface name. AllowedIntfRegexp = regexp.MustCompile("Ethernet((0/[1-3])|([1-9]/[0-3]))$|e((0/[1-3])|([1-9]/[0-9]))$|eth[1-9][0-9]*$") IntfHelpMsg = "Interfaces should follow Ethernet/ or e/ naming convention, where is a number from 0-9 and is a number from 0-3. You can also use ethX-based interface naming." @@ -322,7 +322,8 @@ func (n *iol) AddEndpoint(e links.Endpoint) error { IFaceName = endpointName - if !(DefaultIntfRegexp.MatchString(endpointName)) && AllowedIntfRegexp.MatchString(endpointName) { + if !(DefaultIntfRegexp.MatchString(endpointName)) && + AllowedIntfRegexp.MatchString(endpointName) { log.Debugf("%s: %s needs mapping", n.Cfg.ShortName, endpointName) mappedName, err := n.GetMappedInterfaceName(endpointName) if err != nil { @@ -343,7 +344,6 @@ func (n *iol) AddEndpoint(e links.Endpoint) error { } func (n *iol) CheckInterfaceName() error { - err := n.CheckInterfaceOverlap() if err != nil { return err @@ -356,7 +356,8 @@ func (n *iol) CheckInterfaceName() error { } if !DefaultIntfRegexp.MatchString(IFaceName) { - return fmt.Errorf("IOL Node %q has an interface named %q which doesn't match the required pattern. %s", n.Cfg.ShortName, IFaceName, IntfHelpMsg) + return fmt.Errorf("IOL Node %q has an interface named %q which doesn't match the required pattern. %s", + n.Cfg.ShortName, IFaceName, IntfHelpMsg) } }