mirror of
https://github.com/clearml/go-nvlib
synced 2025-06-26 18:28:08 +00:00
Merge branch 'pciids-build-options' into 'main'
Allow clients of the pciids API to set the pci.ids filepath See merge request nvidia/cloud-native/go-nvlib!43
This commit is contained in:
commit
460d246d23
@ -380,5 +380,7 @@ func (p *ParentDevice) GetAvailableMDEVInstances(mdevType string) (int, error) {
|
|||||||
func newNvidiaPCIDeviceFromPath(devicePath string) (*nvpci.NvidiaPCIDevice, error) {
|
func newNvidiaPCIDeviceFromPath(devicePath string) (*nvpci.NvidiaPCIDevice, error) {
|
||||||
root := filepath.Dir(devicePath)
|
root := filepath.Dir(devicePath)
|
||||||
address := filepath.Base(devicePath)
|
address := filepath.Base(devicePath)
|
||||||
return nvpci.NewFrom(root).GetGPUByPciBusID(address)
|
return nvpci.New(
|
||||||
|
nvpci.WithPCIDevicesRoot(root),
|
||||||
|
).GetGPUByPciBusID(address)
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ func NewMockNvpci() (mock *MockNvpci, rerr error) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
mock = &MockNvpci{
|
mock = &MockNvpci{
|
||||||
NewFrom(rootDir).(*nvpci),
|
New(WithPCIDevicesRoot(rootDir)).(*nvpci),
|
||||||
}
|
}
|
||||||
|
|
||||||
return mock, nil
|
return mock, nil
|
||||||
|
@ -65,6 +65,7 @@ type ResourceInterface interface {
|
|||||||
|
|
||||||
type nvpci struct {
|
type nvpci struct {
|
||||||
pciDevicesRoot string
|
pciDevicesRoot string
|
||||||
|
pcidbPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Interface = (*nvpci)(nil)
|
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
|
// New interface that allows us to get a list of all NVIDIA PCI devices
|
||||||
func New() Interface {
|
func New(opts ...Option) Interface {
|
||||||
return NewFrom(PCIDevicesRoot)
|
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
|
// Option defines a function for passing options to the New() call
|
||||||
func NewFrom(root string) Interface {
|
type Option func(*nvpci)
|
||||||
return &nvpci{
|
|
||||||
pciDevicesRoot: root,
|
// 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
// This is a fallback if all of the locations fail
|
||||||
//go:embed default_pci.ids
|
//go:embed default_pci.ids
|
||||||
var defaultPCIdb []byte
|
var defaultPCIdb []byte
|
||||||
|
|
||||||
// NewDB Parse the PCI DB in its default locations or use the default
|
// NewDB Parse the PCI DB in its default locations or use the default
|
||||||
// builtin pci.ids db.
|
// builtin pci.ids db.
|
||||||
func NewDB() Interface {
|
func NewDB(opts ...Option) Interface {
|
||||||
// Various locations of pci.ids for differente distributions these may be more
|
db := &pcidb{}
|
||||||
// up to date then the embedded pci.ids db
|
for _, opt := range opts {
|
||||||
pcidbs := []string{
|
opt(db)
|
||||||
"/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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pcidbs := defaultPCIdbPaths
|
||||||
|
if db.path != "" {
|
||||||
|
pcidbs = append([]string{db.path}, defaultPCIdbPaths...)
|
||||||
|
}
|
||||||
|
|
||||||
return newParser(pcidbs).parse()
|
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
|
// newParser will attempt to read the db pci.ids from well known places or fall
|
||||||
// back to an internal db
|
// back to an internal db
|
||||||
func newParser(pcidbs []string) *parser {
|
func newParser(pcidbs []string) *parser {
|
||||||
@ -247,6 +271,7 @@ func (d *pcidb) GetClassName(classID uint32) string {
|
|||||||
type pcidb struct {
|
type pcidb struct {
|
||||||
vendors map[uint16]vendor
|
vendors map[uint16]vendor
|
||||||
classes map[uint32]class
|
classes map[uint32]class
|
||||||
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
// vendor PCI vendors/devices/subVendors/SubDevices
|
// vendor PCI vendors/devices/subVendors/SubDevices
|
||||||
|
Loading…
Reference in New Issue
Block a user