From 9bfa7927950b04cce84c14d1d982de702189ef97 Mon Sep 17 00:00:00 2001 From: Archana Shinde Date: Fri, 22 Sep 2017 16:02:27 -0700 Subject: [PATCH] vfio: Add ability to pass VFIO devices to qemu VFIO is meant for exposing exposing direct device access to the virtual machine. Add ability to append VFIO devices to qemu command line. Signed-off-by: Archana Shinde --- qemu.go | 26 ++++++++++++++++++++++++++ qemu_test.go | 10 ++++++++++ 2 files changed, 36 insertions(+) diff --git a/qemu.go b/qemu.go index 3bd79284b..66bf21ebc 100644 --- a/qemu.go +++ b/qemu.go @@ -591,6 +591,32 @@ func (blkdev BlockDevice) QemuParams(config *Config) []string { return qemuParams } +// VFIODevice represents a qemu vfio device meant for direct access by guest OS. +type VFIODevice struct { + // Bus-Device-Function of device + BDF string +} + +// Valid returns true if the VFIODevice structure is valid and complete. +func (vfioDev VFIODevice) Valid() bool { + if vfioDev.BDF == "" { + return false + } + + return true +} + +// QemuParams returns the qemu parameters built out of this vfio device. +func (vfioDev VFIODevice) QemuParams(config *Config) []string { + var qemuParams []string + + deviceParam := fmt.Sprintf("vfio-pci,host=%s", vfioDev.BDF) + qemuParams = append(qemuParams, "-device") + qemuParams = append(qemuParams, deviceParam) + + return qemuParams +} + // RTCBaseType is the qemu RTC base time type. type RTCBaseType string diff --git a/qemu_test.go b/qemu_test.go index 98bf151fd..f3d28e3b5 100644 --- a/qemu_test.go +++ b/qemu_test.go @@ -217,6 +217,16 @@ func TestAppendDeviceBlock(t *testing.T) { testAppend(blkdev, deviceBlockString, t) } +var deviceVFIOString = "-device vfio-pci,host=02:10.0" + +func TestAppendDeviceVFIO(t *testing.T) { + vfioDevice := VFIODevice{ + BDF: "02:10.0", + } + + testAppend(vfioDevice, deviceVFIOString, t) +} + func TestAppendEmptyDevice(t *testing.T) { device := SerialDevice{}