Update vendoring

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar
2023-06-14 17:55:18 +02:00
parent 743d290577
commit bcf3a70174
6 changed files with 95 additions and 27 deletions

View File

@@ -44,7 +44,7 @@ func NewMockNvpci() (mock *MockNvpci, rerr error) {
}()
mock = &MockNvpci{
NewFrom(rootDir).(*nvpci),
New(WithPCIDevicesRoot(rootDir)).(*nvpci),
}
return mock, nil

View File

@@ -65,6 +65,7 @@ type ResourceInterface interface {
type nvpci struct {
pciDevicesRoot string
pcidbPath string
}
var _ Interface = (*nvpci)(nil)
@@ -124,14 +125,33 @@ func (d *NvidiaPCIDevice) Reset() error {
}
// New interface that allows us to get a list of all NVIDIA PCI devices
func New() Interface {
return NewFrom(PCIDevicesRoot)
func New(opts ...Option) Interface {
n := &nvpci{}
for _, opt := range opts {
opt(n)
}
if n.pciDevicesRoot == "" {
n.pciDevicesRoot = PCIDevicesRoot
}
return n
}
// NewFrom interface allows us to get a list of all NVIDIA PCI devices at a specific root directory
func NewFrom(root string) Interface {
return &nvpci{
pciDevicesRoot: root,
// Option defines a function for passing options to the New() call
type Option func(*nvpci)
// WithPCIDevicesRoot provides an Option to set the root path
// for PCI devices on the system.
func WithPCIDevicesRoot(root string) Option {
return func(n *nvpci) {
n.pciDevicesRoot = root
}
}
// WithPCIDatabasePath provides an Option to set the path
// to the pciids database file.
func WithPCIDatabasePath(path string) Option {
return func(n *nvpci) {
n.pcidbPath = path
}
}
@@ -282,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,
@@ -294,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

@@ -172,24 +172,48 @@ type parser struct {
}
}
// Various locations of pci.ids for different distributions. These may be more
// up to date then the embedded pci.ids db
var defaultPCIdbPaths = []string{
"/usr/share/misc/pci.ids", // Ubuntu
"/usr/local/share/pci.ids", // RHEL like with manual update
"/usr/share/hwdata/pci.ids", // RHEL like
"/usr/share/pci.ids", // SUSE
}
// This is a fallback if all of the locations fail
//go:embed default_pci.ids
var defaultPCIdb []byte
// NewDB Parse the PCI DB in its default locations or use the default
// builtin pci.ids db.
func NewDB() Interface {
// Various locations of pci.ids for differente distributions these may be more
// up to date then the embedded pci.ids db
pcidbs := []string{
"/usr/share/misc/pci.ids", // Ubuntu
"/usr/local/share/pci.ids", // RHEL like with manual update
"/usr/share/hwdata/pci.ids", // RHEL like
"/usr/share/pci.ids", // SUSE
func NewDB(opts ...Option) Interface {
db := &pcidb{}
for _, opt := range opts {
opt(db)
}
pcidbs := defaultPCIdbPaths
if db.path != "" {
pcidbs = append([]string{db.path}, defaultPCIdbPaths...)
}
return newParser(pcidbs).parse()
}
// Option defines a function for passing options to the NewDB() call
type Option func(*pcidb)
// WithFilePath provides an Option to set the file path
// for the pciids database used by pciids interface.
// The file path provided takes precedence over all other
// paths.
func WithFilePath(path string) Option {
return func(db *pcidb) {
db.path = path
}
}
// newParser will attempt to read the db pci.ids from well known places or fall
// back to an internal db
func newParser(pcidbs []string) *parser {
@@ -229,24 +253,39 @@ 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
type pcidb struct {
vendors map[uint16]vendor
classes map[uint32]class
path string
}
// vendor PCI vendors/devices/subVendors/SubDevices