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

Signed-off-by: Christopher Desiniotis <cdesiniotis@nvidia.com>
This commit is contained in:
Christopher Desiniotis 2023-06-09 16:56:08 -07:00
parent 460d246d23
commit 1b3ef9bd64
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()
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{
Path: devicePath,
Address: address,
@ -314,8 +323,8 @@ func (p *nvpci) GetGPUByPciBusID(address string) (*NvidiaPCIDevice, error) {
Config: config,
Resources: resources,
IsVF: isVF,
DeviceName: pciDB.GetDeviceName(uint16(vendorID), uint16(deviceID)),
ClassName: pciDB.GetClassName(uint32(classID)),
DeviceName: deviceName,
ClassName: className,
}
return nvdevice, nil

View File

@ -253,18 +253,32 @@ var _ Interface = (*pcidb)(nil)
// Interface returns textual description of specific attributes of PCI devices
type Interface interface {
GetDeviceName(uint16, uint16) string
GetClassName(uint32) string
GetDeviceName(uint16, uint16) (string, error)
GetClassName(uint32) (string, error)
}
// GetDeviceName return the textual description of the PCI device
func (d *pcidb) GetDeviceName(vendorID uint16, deviceID uint16) string {
return d.vendors[vendorID].devices[deviceID].name
func (d *pcidb) GetDeviceName(vendorID uint16, deviceID uint16) (string, error) {
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
func (d *pcidb) GetClassName(classID uint32) string {
return d.classes[classID].name
func (d *pcidb) GetClassName(classID uint32) (string, error) {
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