From 14ce4b01bab5396b2d902bf9ac1bf364452d5197 Mon Sep 17 00:00:00 2001 From: Zvonko Kaiser Date: Fri, 6 May 2022 15:11:54 -0700 Subject: [PATCH] runtime: Adding the correct detection of mediated PCIe devices Fixes #4212 Backport-of: https://github.com/kata-containers/kata-containers/pull/4213 Signed-off-by: Zvonko Kaiser --- src/runtime/virtcontainers/device/drivers/utils.go | 5 ++--- src/runtime/virtcontainers/device/drivers/vfio.go | 14 ++++++++++++++ .../virtcontainers/device/drivers/vfio_test.go | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/runtime/virtcontainers/device/drivers/utils.go b/src/runtime/virtcontainers/device/drivers/utils.go index ecabed83d3..aa97747219 100644 --- a/src/runtime/virtcontainers/device/drivers/utils.go +++ b/src/runtime/virtcontainers/device/drivers/utils.go @@ -45,9 +45,8 @@ func deviceLogger() *logrus.Entry { return api.DeviceLogger() } -// Identify PCIe device by /sys/bus/pci/slots/xx/max_bus_speed, sample content "8.0 GT/s PCIe" -// The /sys/bus/pci/slots/xx/address contains bdf, sample content "0000:04:00" -// bdf format: bus:slot.function +// Identify PCIe device by reading the size of the PCI config space +// Plain PCI device have 256 bytes of config space where PCIe devices have 4K func isPCIeDevice(bdf string) bool { if len(strings.Split(bdf, ":")) == 2 { bdf = PCIDomain + ":" + bdf diff --git a/src/runtime/virtcontainers/device/drivers/vfio.go b/src/runtime/virtcontainers/device/drivers/vfio.go index 8507564f60..8e36ff46ba 100644 --- a/src/runtime/virtcontainers/device/drivers/vfio.go +++ b/src/runtime/virtcontainers/device/drivers/vfio.go @@ -222,6 +222,7 @@ func getVFIODetails(deviceFileName, iommuDevicesPath string) (deviceBDF, deviceS // Get sysfsdev of device eg. /sys/devices/pci0000:00/0000:00:02.0/f79944e4-5a3d-11e8-99ce-479cbab002e4 sysfsDevStr := filepath.Join(iommuDevicesPath, deviceFileName) deviceSysfsDev, err = getSysfsDev(sysfsDevStr) + deviceBDF = getBDF(getMediatedBDF(deviceSysfsDev)) default: err = fmt.Errorf("Incorrect tokens found while parsing vfio details: %s", deviceFileName) } @@ -229,10 +230,23 @@ func getVFIODetails(deviceFileName, iommuDevicesPath string) (deviceBDF, deviceS return deviceBDF, deviceSysfsDev, vfioDeviceType, err } +// getMediatedBDF returns the BDF of a VF +// Expected input string format is /sys/devices/pci0000:d7/BDF0/BDF1/.../MDEVBDF/UUID +func getMediatedBDF(deviceSysfsDev string) string { + tokens := strings.SplitN(deviceSysfsDev, "/", -1) + if len(tokens) < 4 { + return "" + } + return tokens[len(tokens)-2] +} + // getBDF returns the BDF of pci device // Expected input string format is []:[][].[] eg. 0000:02:10.0 func getBDF(deviceSysStr string) string { tokens := strings.SplitN(deviceSysStr, ":", 2) + if len(tokens) == 1 { + return "" + } return tokens[1] } diff --git a/src/runtime/virtcontainers/device/drivers/vfio_test.go b/src/runtime/virtcontainers/device/drivers/vfio_test.go index e98e59e47a..05118871f5 100644 --- a/src/runtime/virtcontainers/device/drivers/vfio_test.go +++ b/src/runtime/virtcontainers/device/drivers/vfio_test.go @@ -46,4 +46,5 @@ func TestGetVFIODetails(t *testing.T) { assert.Nil(t, err) } } + }