Skip to content

Commit

Permalink
Add NICNameSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-howell committed May 2, 2022
1 parent 231bc69 commit 9526731
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
2 changes: 2 additions & 0 deletions pkg/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
19 changes: 19 additions & 0 deletions pkg/resources/deviceSelectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
16 changes: 11 additions & 5 deletions pkg/resources/pciDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
type pciDevice struct {
basePciDevice *ghw.PCIDevice
pfAddr string
nicName string
macAddr string
driver string
vfID int
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ type PciDevice interface {
GetDeviceCode() string
GetPciAddr() string
GetPfPciAddr() string
GetNICName() string
GetMacAddr() string
IsSriovPF() bool
GetSubClass() string
Expand Down
16 changes: 15 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
Expand All @@ -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
Expand Down

0 comments on commit 9526731

Please sign in to comment.