Merge branch 'pciids-error-handling' into 'main'

Update pciids interface to return errors for invalid vendor / device ids

See merge request nvidia/cloud-native/go-nvlib!44
This commit is contained in:
Christopher Desiniotis 2023-06-13 18:23:22 +00:00
commit 7663cf900f
2 changed files with 31 additions and 8 deletions

View File

@ -302,6 +302,15 @@ func (p *nvpci) GetGPUByPciBusID(address string) (*NvidiaPCIDevice, error) {
pciDB := pciids.NewDB() pciDB := pciids.NewDB()
deviceName, err := pciDB.GetDeviceName(uint16(vendorID), uint16(deviceID))
if err != nil {
return nil, fmt.Errorf("unable to get device name: %v", err)
}
className, err := pciDB.GetClassName(uint32(classID))
if err != nil {
return nil, fmt.Errorf("unable to get class name for device: %v", err)
}
nvdevice := &NvidiaPCIDevice{ nvdevice := &NvidiaPCIDevice{
Path: devicePath, Path: devicePath,
Address: address, Address: address,
@ -314,8 +323,8 @@ func (p *nvpci) GetGPUByPciBusID(address string) (*NvidiaPCIDevice, error) {
Config: config, Config: config,
Resources: resources, Resources: resources,
IsVF: isVF, IsVF: isVF,
DeviceName: pciDB.GetDeviceName(uint16(vendorID), uint16(deviceID)), DeviceName: deviceName,
ClassName: pciDB.GetClassName(uint32(classID)), ClassName: className,
} }
return nvdevice, nil return nvdevice, nil

View File

@ -253,18 +253,32 @@ var _ Interface = (*pcidb)(nil)
// Interface returns textual description of specific attributes of PCI devices // Interface returns textual description of specific attributes of PCI devices
type Interface interface { type Interface interface {
GetDeviceName(uint16, uint16) string GetDeviceName(uint16, uint16) (string, error)
GetClassName(uint32) string GetClassName(uint32) (string, error)
} }
// GetDeviceName return the textual description of the PCI device // GetDeviceName return the textual description of the PCI device
func (d *pcidb) GetDeviceName(vendorID uint16, deviceID uint16) string { func (d *pcidb) GetDeviceName(vendorID uint16, deviceID uint16) (string, error) {
return d.vendors[vendorID].devices[deviceID].name vendor, ok := d.vendors[vendorID]
if !ok {
return "", fmt.Errorf("failed to find vendor with id '%x'", vendorID)
}
device, ok := vendor.devices[deviceID]
if !ok {
return "", fmt.Errorf("failed to find device with id '%x'", deviceID)
}
return device.name, nil
} }
// GetClassName resturn the textual description of the PCI device class // GetClassName resturn the textual description of the PCI device class
func (d *pcidb) GetClassName(classID uint32) string { func (d *pcidb) GetClassName(classID uint32) (string, error) {
return d.classes[classID].name class, ok := d.classes[classID]
if !ok {
return "", fmt.Errorf("failed to find class with id '%x'", classID)
}
return class.name, nil
} }
// pcidb The complete set of PCI vendors and PCI classes // pcidb The complete set of PCI vendors and PCI classes