Merge branch 'update-go-nvlib' into 'main'

Update go-nvlib with new constructor API

See merge request nvidia/container-toolkit/container-toolkit!422
This commit is contained in:
Evan Lezar 2023-06-14 22:50:12 +00:00
commit c11c7695cb
7 changed files with 97 additions and 29 deletions

View File

@ -71,8 +71,8 @@ func newAllPossible(logger logger.Interface, driverRoot string) (nodeLister, err
// DeviceNodes returns a list of all possible device nodes for NVIDIA GPUs, control devices, and capability devices.
func (m allPossible) DeviceNodes() ([]deviceNode, error) {
gpus, err := nvpci.NewFrom(
filepath.Join(m.driverRoot, nvpci.PCIDevicesRoot),
gpus, err := nvpci.New(
nvpci.WithPCIDevicesRoot(filepath.Join(m.driverRoot, nvpci.PCIDevicesRoot)),
).GetGPUs()
if err != nil {
return nil, fmt.Errorf("failed to get GPU information: %v", err)

2
go.mod
View File

@ -12,7 +12,7 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1
github.com/urfave/cli/v2 v2.3.0
gitlab.com/nvidia/cloud-native/go-nvlib v0.0.0-20230522132528-649703f6b386
gitlab.com/nvidia/cloud-native/go-nvlib v0.0.0-20230613182322-7663cf900f0a
golang.org/x/mod v0.5.0
golang.org/x/sys v0.7.0
)

4
go.sum
View File

@ -77,8 +77,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
gitlab.com/nvidia/cloud-native/go-nvlib v0.0.0-20230522132528-649703f6b386 h1:byHxP+mlgNQ4GX31owfgCIq5fJCsdJMchiJHGuM2rxw=
gitlab.com/nvidia/cloud-native/go-nvlib v0.0.0-20230522132528-649703f6b386/go.mod h1:KYZksBgh18o+uzgnpDazzG4LVYtnfB96VXHMXypEtik=
gitlab.com/nvidia/cloud-native/go-nvlib v0.0.0-20230613182322-7663cf900f0a h1:lceJVurLqiWFdxK6KMDw+SIwrAsFW/af44XrNlbGw78=
gitlab.com/nvidia/cloud-native/go-nvlib v0.0.0-20230613182322-7663cf900f0a/go.mod h1:KYZksBgh18o+uzgnpDazzG4LVYtnfB96VXHMXypEtik=
golang.org/x/mod v0.5.0 h1:UG21uOlmZabA4fW5i7ZX6bjw1xELEGg/ZLgZq9auk/Q=
golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

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

2
vendor/modules.txt vendored
View File

@ -62,7 +62,7 @@ github.com/syndtr/gocapability/capability
github.com/urfave/cli/v2
# github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb
## explicit
# gitlab.com/nvidia/cloud-native/go-nvlib v0.0.0-20230522132528-649703f6b386
# gitlab.com/nvidia/cloud-native/go-nvlib v0.0.0-20230613182322-7663cf900f0a
## explicit; go 1.20
gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/device
gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/info