Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IOMMUFD support #56

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add IOMMUFD support #56

wants to merge 2 commits into from

Conversation

zvonkok
Copy link
Contributor

@zvonkok zvonkok commented Mar 12, 2025

Add IOMMUFD parsing to NVPCI device.
The new kernel interface exposes VFIO devices via IOMMUFD backend besides the old VFIO container backend aka IOMMU Groups.
Add the proper parsing function to gather the correct information.

@zvonkok zvonkok changed the title Add iommufd Add IOMMUFD support Mar 12, 2025
zvonkok added 2 commits March 12, 2025 20:57
The new kernel interface exposes VFIO devices via IOMMUFD
backend besides the old VFIO container backend aka IOMMU Groups.

Add the proper parsing function to gather the correct information.

Signed-off-by: Zvonko Kaiser <[email protected]>
Simple test for proper IOMMUFD parsing

Signed-off-by: Zvonko Kaiser <[email protected]>
Comment on lines +87 to +92
vfioDev := filepath.Join(deviceDir, "vfio-dev")
vfioFD := filepath.Join(vfioDev, "vfio8")
err = os.MkdirAll(vfioFD, 0755)
if err != nil {
return err
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Is there a benefit in having two variables?

Suggested change
vfioDev := filepath.Join(deviceDir, "vfio-dev")
vfioFD := filepath.Join(vfioDev, "vfio8")
err = os.MkdirAll(vfioFD, 0755)
if err != nil {
return err
}
vfioFD := filepath.Join(deviceDir, "vfio-dev", "vfio8)
err = os.MkdirAll(vfioFD, 0755)
if err != nil {
return err
}

@@ -74,6 +74,33 @@ func TestNvpci(t *testing.T) {
_, err = nvpci.GetGPUByIndex(1)
require.Error(t, err, "No error returned when getting GPU at invalid index")
}
func TestNvpciIOMMUFD(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we need a newline before this?

Suggested change
func TestNvpciIOMMUFD(t *testing.T) {
func TestNvpciIOMMUFD(t *testing.T) {

@@ -521,6 +528,31 @@ func getDriver(devicePath string) (string, error) {
return "", err
}

// /sys/bus/pci/devices/0000:df:00.0/vfio-dev/vfio0.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a more descriptive docstring.

// /sys/bus/pci/devices/0000:df:00.0/vfio-dev/vfio0.
func getIOMMUFD(devicePath string) (int, error) {
vfioDevDir := filepath.Join(devicePath, "vfio-dev")
// Read the directory; expect exactly one entry
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't check for exactly one entry below. Should we?

case os.IsNotExist(err):
return -1, nil
case err == nil:
if len(entries) == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check for the vfio prefix in the entries? Does using filepath.Glob make sense here if we're expecting a particular pattern?

name := entries[0].Name()
// Strip the "vfio" prefix to get the numeric part
idxStr := strings.TrimPrefix(name, "vfio")
idx, err := strconv.Atoi(idxStr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Should we use ParseInt here and specify a 64 bit width as we do for the IOMMU Group?

devices, err := nvpci.GetGPUs()
require.Nil(t, err, "Error getting GPUs")
require.Equal(t, 1, len(devices), "Wrong number of GPU devices")
require.Equal(t, 8, devices[0].IommuFD, "Wrong IOMMUFD found for device")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require.Equal(t, 8, devices[0].IommuFD, "Wrong IOMMUFD found for device")
require.Equal(t, tc.IOMMUFD, devices[0].IommuFD, "Wrong IOMMUFD found for device")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants