Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
drivers: change BindDevicetoVFIO signature
Browse files Browse the repository at this point in the history
BindDevicetoVFIO now returns the vfio path to the brand new vfio device bound

Signed-off-by: Julio Montes <julio.montes@intel.com>
  • Loading branch information
Julio Montes committed Jun 16, 2020
1 parent 970ef45 commit 581ff97
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions virtcontainers/device/drivers/vfio.go
Expand Up @@ -9,6 +9,7 @@ package drivers
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand All @@ -27,6 +28,8 @@ const (
pciDriverBindPath = "/sys/bus/pci/drivers/%s/bind"
vfioNewIDPath = "/sys/bus/pci/drivers/vfio-pci/new_id"
vfioRemoveIDPath = "/sys/bus/pci/drivers/vfio-pci/remove_id"
iommuGroupPath = "/sys/bus/pci/devices/%s/iommu_group"
vfioDevPath = "/dev/vfio/%s"
pcieRootPortPrefix = "rp"
)

Expand Down Expand Up @@ -242,7 +245,7 @@ func getSysfsDev(sysfsDevStr string) (string, error) {

// BindDevicetoVFIO binds the device to vfio driver after unbinding from host.
// Will be called by a network interface or a generic pcie device.
func BindDevicetoVFIO(bdf, hostDriver, vendorDeviceID string) error {
func BindDevicetoVFIO(bdf, hostDriver, vendorDeviceID string) (string, error) {

// Unbind from the host driver
unbindDriverPath := fmt.Sprintf(pciDriverUnbindPath, bdf)
Expand All @@ -252,7 +255,7 @@ func BindDevicetoVFIO(bdf, hostDriver, vendorDeviceID string) error {
}).Info("Unbinding device from driver")

if err := utils.WriteToFile(unbindDriverPath, []byte(bdf)); err != nil {
return err
return "", err
}

// Add device id to vfio driver.
Expand All @@ -262,7 +265,7 @@ func BindDevicetoVFIO(bdf, hostDriver, vendorDeviceID string) error {
}).Info("Writing vendor-device-id to vfio new-id path")

if err := utils.WriteToFile(vfioNewIDPath, []byte(vendorDeviceID)); err != nil {
return err
return "", err
}

// Bind to vfio-pci driver.
Expand All @@ -276,7 +279,12 @@ func BindDevicetoVFIO(bdf, hostDriver, vendorDeviceID string) error {
// Device may be already bound at this time because of earlier write to new_id, ignore error
utils.WriteToFile(bindDriverPath, []byte(bdf))

return nil
groupPath, err := os.Readlink(fmt.Sprintf(iommuGroupPath, bdf))
if err != nil {
return "", err
}

return fmt.Sprintf(vfioDevPath, filepath.Base(groupPath)), nil
}

// BindDevicetoHost binds the device to the host driver driver after unbinding from vfio-pci.
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/physical_endpoint.go
Expand Up @@ -202,7 +202,7 @@ func createPhysicalEndpoint(netInfo NetworkInfo) (*PhysicalEndpoint, error) {
return physicalEndpoint, nil
}

func bindNICToVFIO(endpoint *PhysicalEndpoint) error {
func bindNICToVFIO(endpoint *PhysicalEndpoint) (string, error) {
return drivers.BindDevicetoVFIO(endpoint.BDF, endpoint.Driver, endpoint.VendorDeviceID)
}

Expand Down

0 comments on commit 581ff97

Please sign in to comment.