Skip to content

Commit

Permalink
Add support for running inside a VM with vfio-noiommu
Browse files Browse the repository at this point in the history
Fixed link path

Check for missing name

fix name missing

fix err name

Add dual return
  • Loading branch information
atyronesmith committed Oct 23, 2020
1 parent b7f6d3e commit 395931e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
8 changes: 4 additions & 4 deletions pkg/resources/vfioResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func (rp *vfioResource) GetDeviceSpecs(pciAddr string) []*pluginapi.DeviceSpec {
Permissions: "mrw",
})

vfioDev, err := utils.GetVFIODeviceFile(pciAddr)
vfioDevHost, vfioDevContainer, err := utils.GetVFIODeviceFile(pciAddr)
if err != nil {
glog.Errorf("GetDeviceSpecs(): error getting vfio device file for device: %s", pciAddr)
glog.Errorf("GetDeviceSpecs(): error getting vfio device file for device: %s, %s", pciAddr, err.Error())
} else {
devSpecs = append(devSpecs, &pluginapi.DeviceSpec{
HostPath: vfioDev,
ContainerPath: vfioDev,
HostPath: vfioDevHost,
ContainerPath: vfioDevContainer,
Permissions: "mrw",
})
}
Expand Down
21 changes: 18 additions & 3 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func ValidResourceName(name string) bool {
}

// GetVFIODeviceFile returns a vfio device files for vfio-pci bound PCI device's PCI address
func GetVFIODeviceFile(dev string) (devFile string, err error) {
func GetVFIODeviceFile(dev string) (devFileHost string, devFileContainer string, err error) {
// Get iommu group for this device
devPath := filepath.Join(sysBusPci, dev)
_, err = os.Lstat(devPath)
Expand Down Expand Up @@ -290,8 +290,23 @@ func GetVFIODeviceFile(dev string) (devFile string, err error) {
err = fmt.Errorf("GetVFIODeviceFile(): error reading symlink to iommu_group %v", err)
return
}

devFile = filepath.Join("/dev/vfio", filepath.Base(linkName))
devFileContainer = filepath.Join("/dev/vfio", filepath.Base(linkName))
devFileHost = devFileContainer

// Get a file path to the iommu group name
namePath := filepath.Join(linkName, "name")
// Read the iommu group name
// The name file will not exist on baremetal
vfioName, errName := ioutil.ReadFile(namePath)
if errName == nil {
vName := strings.TrimSpace(string(vfioName))

// if the iommu group name == vfio-noiommu then we are in a VM, adjust path to vfio device
if vName == "vfio-noiommu" {
linkName = filepath.Join(filepath.Dir(linkName), "noiommu-"+filepath.Base(linkName))
devFileHost = filepath.Join("/dev/vfio", filepath.Base(linkName))
}
}

return
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ var _ = Describe("In the utils package", func() {
DescribeTable("getting VFIO device file",
func(fs *FakeFilesystem, device, expected string, shouldFail bool) {
defer fs.Use()()
actual, err := GetVFIODeviceFile(device)
Expect(actual).To(Equal(expected))
//TODO: adapt test to running in a virtual environment
actualHost, _, err := GetVFIODeviceFile(device)
Expect(actualHost).To(Equal(expected))
assertShouldFail(err, shouldFail)
},
Entry("could not get directory information for device",
Expand Down

0 comments on commit 395931e

Please sign in to comment.