From 9526731d62dbf8bd89409ea7b3a8eefcaad3acb9 Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Mon, 2 May 2022 12:37:27 -0500 Subject: [PATCH] Add NICNameSelector --- pkg/factory/factory.go | 2 ++ pkg/resources/deviceSelectors.go | 19 +++++++++++++++++++ pkg/resources/pciDevice.go | 16 +++++++++++----- pkg/types/types.go | 1 + pkg/utils/utils.go | 16 +++++++++++++++- 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/pkg/factory/factory.go b/pkg/factory/factory.go index 7e06b4be7..704797780 100644 --- a/pkg/factory/factory.go +++ b/pkg/factory/factory.go @@ -80,6 +80,8 @@ func (rf *resourceFactory) GetSelector(attr string, values []string) (types.Devi return resources.NewDeviceSelector(values), nil case "drivers": return resources.NewDriverSelector(values), nil + case "nicNames": + return resources.NewNICNameSelector(values), nil case "macAddresses": return resources.NewMacAddressSelector(values), nil case "pciAddresses": diff --git a/pkg/resources/deviceSelectors.go b/pkg/resources/deviceSelectors.go index be33990fd..363746791 100644 --- a/pkg/resources/deviceSelectors.go +++ b/pkg/resources/deviceSelectors.go @@ -92,6 +92,25 @@ func (s *macAddressSelector) Filter(inDevices []types.PciDevice) []types.PciDevi return filteredList } +// NewNICNameSelector returns a NetDevSelector interface for netDev list +func NewNICNameSelector(macAddresses []string) types.DeviceSelector { + return &macAddressSelector{macAddresses: macAddresses} +} + +type nicNameSelector struct { + nicNames []string +} + +func (s *nicNameSelector) Filter(inDevices []types.PciDevice) []types.PciDevice { + filteredList := make([]types.PciDevice, 0) + for _, dev := range inDevices { + if contains(s.nicNames, dev.GetNICName()) { + filteredList = append(filteredList, dev) + } + } + return filteredList +} + // NewPciAddressSelector returns a NetDevSelector interface for netDev list func NewPciAddressSelector(pciAddresses []string) types.DeviceSelector { return &pciAddressSelector{pciAddresses: pciAddresses} diff --git a/pkg/resources/pciDevice.go b/pkg/resources/pciDevice.go index 68d44979c..a39022558 100644 --- a/pkg/resources/pciDevice.go +++ b/pkg/resources/pciDevice.go @@ -27,6 +27,7 @@ import ( type pciDevice struct { basePciDevice *ghw.PCIDevice pfAddr string + nicName string macAddr string driver string vfID int @@ -56,11 +57,11 @@ func NewPciDevice(dev *ghw.PCIDevice, rFactory types.ResourceFactory, infoProvid return nil, err } - // Get MAC Address - macAddr, err := utils.GetMacAddr(pciAddr) - if err != nil { - return nil, err - } + // Get NIC name. Ignoring the error because not all PCI Devices will be NICs + nicName, _ := utils.GetNICName(pciAddr) + + // Get MAC Address. Ignoring the error because not all PCI Devices will have MAC addresses + macAddr, _ := utils.GetMacAddr(pciAddr) // Get driver info driverName, err := utils.GetDriverName(pciAddr) @@ -96,6 +97,7 @@ func NewPciDevice(dev *ghw.PCIDevice, rFactory types.ResourceFactory, infoProvid return &pciDevice{ basePciDevice: dev, pfAddr: pfAddr, + nicName: nicName, macAddr: macAddr, driver: driverName, vfID: vfID, @@ -109,6 +111,10 @@ func (pd *pciDevice) GetPfPciAddr() string { return pd.pfAddr } +func (pd *pciDevice) GetNICName() string { + return pd.nicName +} + func (pd *pciDevice) GetMacAddr() string { return pd.macAddr } diff --git a/pkg/types/types.go b/pkg/types/types.go index 1754053f0..35d9150ab 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -183,6 +183,7 @@ type PciDevice interface { GetDeviceCode() string GetPciAddr() string GetPfPciAddr() string + GetNICName() string GetMacAddr() string IsSriovPF() bool GetSubClass() string diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 1002fc5b9..9f9f0e8fa 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -62,6 +62,21 @@ func GetPfAddr(pciAddr string) (string, error) { return filepath.Base(pciinfo), nil } +// GetNICName returns NIC name of the PCI device whose address is pciAddr +func GetNICName(pciAddr string) (string, error) { + networkInfo, err := ghw.Network(ghw.WithPathOverrides(ghw.PathOverrides{"/sys": sysDir})) + if err != nil { + return "", fmt.Errorf("GetNICName(): error getting network info: %v", err) + } + + for _, nic := range networkInfo.NICs { + if nic.PCIAddress != nil && *nic.PCIAddress == pciAddr { + return nic.Name, nil + } + } + return "", fmt.Errorf("error getting NIC name for PCI device %s %v", pciAddr, err) +} + // GetMacAddr returns the MAC address of the PCI device whose address is pciAddr func GetMacAddr(pciAddr string) (string, error) { networkInfo, err := ghw.Network(ghw.WithPathOverrides(ghw.PathOverrides{"/sys": sysDir})) @@ -75,7 +90,6 @@ func GetMacAddr(pciAddr string) (string, error) { } } return "", fmt.Errorf("error getting MAC address for PCI device %s %v", pciAddr, err) - } // GetPfName returns SRIOV PF name for the given VF