qemu: enable iommu on q35

Add a configuration option and a Pod Annotation

If activated:
- Add kernel parameters to load iommu
- Add irqchip=split in the kvm options
- Add a vIOMMU to the VM

Fixes #2694
Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com>
This commit is contained in:
Adrian Moreno 2020-03-12 13:00:29 +01:00 committed by Fabiano Fidêncio
parent 7faaa06a52
commit b97287090b
11 changed files with 66 additions and 2 deletions

View File

@ -129,6 +129,12 @@ block_device_driver = "@DEFBLOCKSTORAGEDRIVER_FC@"
# result in memory pre allocation # result in memory pre allocation
#enable_hugepages = true #enable_hugepages = true
# Enable vIOMMU, default false
# Enabling this will result in the VM having a vIOMMU device
# This will also add the following options to the kernel's
# command line: intel_iommu=on,iommu=pt
#enable_iommu = true
# Enable swap of vm memory. Default false. # Enable swap of vm memory. Default false.
# The behaviour is undefined if mem_prealloc is also set to true # The behaviour is undefined if mem_prealloc is also set to true
#enable_swap = true #enable_swap = true

View File

@ -183,6 +183,12 @@ enable_vhost_user_store = @DEFENABLEVHOSTUSERSTORE@
# simulated block device nodes for vhost-user devices to live. # simulated block device nodes for vhost-user devices to live.
vhost_user_store_path = "@DEFVHOSTUSERSTOREPATH@" vhost_user_store_path = "@DEFVHOSTUSERSTOREPATH@"
# Enable vIOMMU, default false
# Enabling this will result in the VM having a vIOMMU device
# This will also add the following options to the kernel's
# command line: intel_iommu=on,iommu=pt
#enable_iommu = true
# Enable file based guest memory support. The default is an empty string which # Enable file based guest memory support. The default is an empty string which
# will disable this feature. In the case of virtio-fs, this is enabled # will disable this feature. In the case of virtio-fs, this is enabled
# automatically and '/dev/shm' is used as the backing folder. # automatically and '/dev/shm' is used as the backing folder.

View File

@ -190,6 +190,12 @@ enable_vhost_user_store = @DEFENABLEVHOSTUSERSTORE@
# simulated block device nodes for vhost-user devices to live. # simulated block device nodes for vhost-user devices to live.
vhost_user_store_path = "@DEFVHOSTUSERSTOREPATH@" vhost_user_store_path = "@DEFVHOSTUSERSTOREPATH@"
# Enable vIOMMU, default false
# Enabling this will result in the VM having a vIOMMU device
# This will also add the following options to the kernel's
# command line: intel_iommu=on,iommu=pt
#enable_iommu = true
# Enable file based guest memory support. The default is an empty string which # Enable file based guest memory support. The default is an empty string which
# will disable this feature. In the case of virtio-fs, this is enabled # will disable this feature. In the case of virtio-fs, this is enabled
# automatically and '/dev/shm' is used as the backing folder. # automatically and '/dev/shm' is used as the backing folder.

View File

@ -38,6 +38,7 @@ const defaultBlockDeviceCacheNoflush bool = false
const defaultEnableIOThreads bool = false const defaultEnableIOThreads bool = false
const defaultEnableMemPrealloc bool = false const defaultEnableMemPrealloc bool = false
const defaultEnableHugePages bool = false const defaultEnableHugePages bool = false
const defaultEnableIOMMU bool = false
const defaultFileBackedMemRootDir string = "" const defaultFileBackedMemRootDir string = ""
const defaultEnableSwap bool = false const defaultEnableSwap bool = false
const defaultEnableDebug bool = false const defaultEnableDebug bool = false

View File

@ -118,6 +118,7 @@ type hypervisor struct {
MemPrealloc bool `toml:"enable_mem_prealloc"` MemPrealloc bool `toml:"enable_mem_prealloc"`
HugePages bool `toml:"enable_hugepages"` HugePages bool `toml:"enable_hugepages"`
VirtioMem bool `toml:"enable_virtio_mem"` VirtioMem bool `toml:"enable_virtio_mem"`
IOMMU bool `toml:"enable_iommu"`
FileBackedMemRootDir string `toml:"file_mem_backend"` FileBackedMemRootDir string `toml:"file_mem_backend"`
Swap bool `toml:"enable_swap"` Swap bool `toml:"enable_swap"`
Debug bool `toml:"enable_debug"` Debug bool `toml:"enable_debug"`
@ -645,6 +646,7 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) {
VirtioFSExtraArgs: h.VirtioFSExtraArgs, VirtioFSExtraArgs: h.VirtioFSExtraArgs,
MemPrealloc: h.MemPrealloc, MemPrealloc: h.MemPrealloc,
HugePages: h.HugePages, HugePages: h.HugePages,
IOMMU: h.IOMMU,
FileBackedMemRootDir: h.FileBackedMemRootDir, FileBackedMemRootDir: h.FileBackedMemRootDir,
Mlock: !h.Swap, Mlock: !h.Swap,
Debug: h.Debug, Debug: h.Debug,
@ -1086,6 +1088,7 @@ func GetDefaultHypervisorConfig() vc.HypervisorConfig {
DefaultBridges: defaultBridgesCount, DefaultBridges: defaultBridgesCount,
MemPrealloc: defaultEnableMemPrealloc, MemPrealloc: defaultEnableMemPrealloc,
HugePages: defaultEnableHugePages, HugePages: defaultEnableHugePages,
IOMMU: defaultEnableIOMMU,
FileBackedMemRootDir: defaultFileBackedMemRootDir, FileBackedMemRootDir: defaultFileBackedMemRootDir,
Mlock: !defaultEnableSwap, Mlock: !defaultEnableSwap,
Debug: defaultEnableDebug, Debug: defaultEnableDebug,

View File

@ -358,6 +358,9 @@ type HypervisorConfig struct {
// VirtioMem is used to enable/disable virtio-mem // VirtioMem is used to enable/disable virtio-mem
VirtioMem bool VirtioMem bool
// IOMMU specifies if the VM should have a vIOMMU
IOMMU bool
// Realtime Used to enable/disable realtime // Realtime Used to enable/disable realtime
Realtime bool Realtime bool

View File

@ -148,6 +148,9 @@ const (
// HugePages is a sandbox annotation to specify if the memory should be pre-allocated from huge pages // HugePages is a sandbox annotation to specify if the memory should be pre-allocated from huge pages
HugePages = kataAnnotHypervisorPrefix + "enable_hugepages" HugePages = kataAnnotHypervisorPrefix + "enable_hugepages"
// Iommu is a sandbox annotation to specify if the VM should have a vIOMMU device
IOMMU = kataAnnotHypervisorPrefix + "enable_iommu"
// FileBackedMemRootDir is a sandbox annotation to soecify file based memory backend root directory // FileBackedMemRootDir is a sandbox annotation to soecify file based memory backend root directory
FileBackedMemRootDir = kataAnnotHypervisorPrefix + "file_mem_backend" FileBackedMemRootDir = kataAnnotHypervisorPrefix + "file_mem_backend"

View File

@ -539,6 +539,15 @@ func addHypervisorMemoryOverrides(ocispec specs.Spec, sbConfig *vc.SandboxConfig
sbConfig.HypervisorConfig.HugePages = hugePages sbConfig.HypervisorConfig.HugePages = hugePages
} }
if value, ok := ocispec.Annotations[vcAnnotations.IOMMU]; ok {
iommu, err := strconv.ParseBool(value)
if err != nil {
return fmt.Errorf("Error parsing annotation for iommu: Please specify boolean value 'true|false'")
}
sbConfig.HypervisorConfig.IOMMU = iommu
}
return nil return nil
} }

View File

@ -771,6 +771,7 @@ func TestAddHypervisorAnnotations(t *testing.T) {
ocispec.Annotations[vcAnnotations.EnableSwap] = "true" ocispec.Annotations[vcAnnotations.EnableSwap] = "true"
ocispec.Annotations[vcAnnotations.FileBackedMemRootDir] = "/dev/shm" ocispec.Annotations[vcAnnotations.FileBackedMemRootDir] = "/dev/shm"
ocispec.Annotations[vcAnnotations.HugePages] = "true" ocispec.Annotations[vcAnnotations.HugePages] = "true"
ocispec.Annotations[vcAnnotations.IOMMU] = "true"
ocispec.Annotations[vcAnnotations.BlockDeviceDriver] = "virtio-scsi" ocispec.Annotations[vcAnnotations.BlockDeviceDriver] = "virtio-scsi"
ocispec.Annotations[vcAnnotations.DisableBlockDeviceUse] = "true" ocispec.Annotations[vcAnnotations.DisableBlockDeviceUse] = "true"
ocispec.Annotations[vcAnnotations.EnableIOThreads] = "true" ocispec.Annotations[vcAnnotations.EnableIOThreads] = "true"
@ -802,6 +803,7 @@ func TestAddHypervisorAnnotations(t *testing.T) {
assert.Equal(config.HypervisorConfig.Mlock, false) assert.Equal(config.HypervisorConfig.Mlock, false)
assert.Equal(config.HypervisorConfig.FileBackedMemRootDir, "/dev/shm") assert.Equal(config.HypervisorConfig.FileBackedMemRootDir, "/dev/shm")
assert.Equal(config.HypervisorConfig.HugePages, true) assert.Equal(config.HypervisorConfig.HugePages, true)
assert.Equal(config.HypervisorConfig.IOMMU, true)
assert.Equal(config.HypervisorConfig.BlockDeviceDriver, "virtio-scsi") assert.Equal(config.HypervisorConfig.BlockDeviceDriver, "virtio-scsi")
assert.Equal(config.HypervisorConfig.DisableBlockDeviceUse, true) assert.Equal(config.HypervisorConfig.DisableBlockDeviceUse, true)
assert.Equal(config.HypervisorConfig.EnableIOThreads, true) assert.Equal(config.HypervisorConfig.EnableIOThreads, true)

View File

@ -409,6 +409,13 @@ func (q *qemu) buildDevices(initrdPath string) ([]govmmQemu.Device, *govmmQemu.I
} }
} }
if q.config.IOMMU {
devices, err = q.arch.appendIOMMU(devices)
if err != nil {
return nil, nil, err
}
}
var ioThread *govmmQemu.IOThread var ioThread *govmmQemu.IOThread
if q.config.BlockDeviceDriver == config.VirtioSCSI { if q.config.BlockDeviceDriver == config.VirtioSCSI {
return q.arch.appendSCSIController(devices, q.config.EnableIOThreads) return q.arch.appendSCSIController(devices, q.config.EnableIOThreads)

View File

@ -48,7 +48,6 @@ var kernelParams = []Param{
{"reboot", "k"}, {"reboot", "k"},
{"console", "hvc0"}, {"console", "hvc0"},
{"console", "hvc1"}, {"console", "hvc1"},
{"iommu", "off"},
{"cryptomgr.notests", ""}, {"cryptomgr.notests", ""},
{"net.ifnames", "0"}, {"net.ifnames", "0"},
{"pci", "lastbus=0"}, {"pci", "lastbus=0"},
@ -89,12 +88,31 @@ func newQemuArch(config HypervisorConfig) qemuArch {
factory = true factory = true
} }
var qemuMachines = supportedQemuMachines
if config.IOMMU {
var q35QemuIOMMUOptions = "accel=kvm,kernel_irqchip=split"
kernelParams = append(kernelParams,
Param{"intel_iommu", "on"})
kernelParams = append(kernelParams,
Param{"iommu", "pt"})
for _, m := range qemuMachines {
if m.Type == QemuQ35 {
m.Options = q35QemuIOMMUOptions
}
}
} else {
kernelParams = append(kernelParams,
Param{"iommu", "off"})
}
q := &qemuAmd64{ q := &qemuAmd64{
qemuArchBase: qemuArchBase{ qemuArchBase: qemuArchBase{
machineType: machineType, machineType: machineType,
memoryOffset: config.MemOffset, memoryOffset: config.MemOffset,
qemuPaths: qemuPaths, qemuPaths: qemuPaths,
supportedQemuMachines: supportedQemuMachines, supportedQemuMachines: qemuMachines,
kernelParamsNonDebug: kernelParamsNonDebug, kernelParamsNonDebug: kernelParamsNonDebug,
kernelParamsDebug: kernelParamsDebug, kernelParamsDebug: kernelParamsDebug,
kernelParams: kernelParams, kernelParams: kernelParams,