Refactor how mdev's are represented internally in nvmdev.

The 'mdev' string now represents the absolute path to an
mdev device (/sys/bus/pci/devices/<addr>/<uuid>) instead
of the  mdev_type directory for the mdev device
(/sys/bus/pci/devices/<addr>/mdev_supported_types/<mdev-type>).
This is more intuitive and will make it easier to get
more information about a particular mdev device -
like the driver or iommu_group it belongs to - which can
be found at /sys/bus/pci/devices/<addr>/<uuid>.

Signed-off-by: Christopher Desiniotis <cdesiniotis@nvidia.com>
This commit is contained in:
Christopher Desiniotis 2022-07-08 11:53:03 -07:00
parent d65cf69086
commit 805db5afa8
3 changed files with 23 additions and 13 deletions

View File

@ -183,14 +183,20 @@ func (m *MockNvmdev) AddMockA100Parent(address string, numaNode int) error {
// AddMockA100Mdev creates an A100 like MDEV (vGPU) mock device. // AddMockA100Mdev creates an A100 like MDEV (vGPU) mock device.
// The corresponding mocked parent A100 device must be created beforehand. // The corresponding mocked parent A100 device must be created beforehand.
func (m *MockNvmdev) AddMockA100Mdev(uuid string, mdevType string, parentMdevTypeDir string) error { func (m *MockNvmdev) AddMockA100Mdev(uuid string, mdevType string, mdevTypeDir string, parentDeviceDir string) error {
deviceDir := filepath.Join(m.mdevDevicesRoot, uuid) mdevDeviceDir := filepath.Join(parentDeviceDir, uuid)
err := os.MkdirAll(deviceDir, 0755) err := os.Mkdir(mdevDeviceDir, 0755)
if err != nil { if err != nil {
return err return err
} }
err = os.Symlink(parentMdevTypeDir, filepath.Join(deviceDir, "mdev_type")) parentMdevTypeDir := filepath.Join(parentDeviceDir, "mdev_supported_types", mdevTypeDir)
err = os.Symlink(parentMdevTypeDir, filepath.Join(mdevDeviceDir, "mdev_type"))
if err != nil {
return err
}
err = os.Symlink(mdevDeviceDir, filepath.Join(m.mdevDevicesRoot, uuid))
if err != nil { if err != nil {
return err return err
} }

View File

@ -153,27 +153,33 @@ func NewDevice(root string, uuid string) (*Device, error) {
return &device, nil return &device, nil
} }
// mdev represents the path to an NVIDIA mdev (vGPU) device.
type mdev string type mdev string
func newMdev(devicePath string) (mdev, error) { func newMdev(devicePath string) (mdev, error) {
mdevTypeDir, err := filepath.EvalSymlinks(path.Join(devicePath, "mdev_type")) mdevDir, err := filepath.EvalSymlinks(devicePath)
if err != nil { if err != nil {
return "", fmt.Errorf("error resolving mdev_type link: %v", err) return "", fmt.Errorf("error resolving symlink for %s: %v", devicePath, err)
} }
return mdev(mdevTypeDir), nil return mdev(mdevDir), nil
} }
func (m mdev) String() string { func (m mdev) String() string {
return string(m) return string(m)
} }
func (m mdev) parentDevicePath() string { func (m mdev) parentDevicePath() string {
// /sys/bus/pci/devices/<addr>/mdev_supported_types/<mdev_type> // /sys/bus/pci/devices/<addr>/<uuid>
return path.Dir(path.Dir(string(m))) return path.Dir(string(m))
} }
func (m mdev) Type() (string, error) { func (m mdev) Type() (string, error) {
mdevType, err := os.ReadFile(path.Join(string(m), "name")) mdevTypeDir, err := filepath.EvalSymlinks(path.Join(string(m), "mdev_type"))
if err != nil {
return "", fmt.Errorf("error resolving mdev_type link for mdev %s: %v", m, err)
}
mdevType, err := os.ReadFile(path.Join(mdevTypeDir, "name"))
if err != nil { if err != nil {
return "", fmt.Errorf("unable to read mdev_type name for mdev %s: %v", m, err) return "", fmt.Errorf("unable to read mdev_type name for mdev %s: %v", m, err)
} }

View File

@ -18,7 +18,6 @@ package nvmdev
import ( import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"path/filepath"
"testing" "testing"
) )
@ -41,8 +40,7 @@ func TestNvmdev(t *testing.T) {
require.Nil(t, err, "Error checking if A100-4Q vGPU type is available for creation") require.Nil(t, err, "Error checking if A100-4Q vGPU type is available for creation")
require.True(t, available, "A100-4C should be available to create") require.True(t, available, "A100-4C should be available to create")
err = nvmdev.AddMockA100Mdev("b1914f0a-15cf-416e-8967-55fc7cb68e20", "A100-4C", err = nvmdev.AddMockA100Mdev("b1914f0a-15cf-416e-8967-55fc7cb68e20", "A100-4C", "nvidia-500", parentDevs[0].Path)
filepath.Join(parentDevs[0].Path, "mdev_supported_types/nvidia-500"))
require.Nil(t, err, "Error adding Mock A100 mediated device") require.Nil(t, err, "Error adding Mock A100 mediated device")
mdevs, err := nvmdev.GetAllDevices() mdevs, err := nvmdev.GetAllDevices()