Files
kata-containers/src/runtime/virtcontainers/qemu_amd64.go
David Gibson 97a02131c6 qemu: Detect and fail a bad machine type earlier
Currently, newQemuArch() doesn't return an error.  So, if passed an invalid
machine type, it will return a technically valid, but unusable qemuArch
object, which will probably fail with other errors shortly down the track.

Change this, to more cleanly fail the newQemuArch itself, letting us
detect a bad machine type earlier.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2020-06-24 21:07:33 +10:00

192 lines
4.2 KiB
Go

// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"fmt"
"time"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types"
govmmQemu "github.com/intel/govmm/qemu"
)
type qemuAmd64 struct {
// inherit from qemuArchBase, overwrite methods if needed
qemuArchBase
vmFactory bool
}
const (
defaultQemuPath = "/usr/bin/qemu-system-x86_64"
defaultQemuMachineType = QemuPC
defaultQemuMachineOptions = "accel=kvm,kernel_irqchip"
qmpMigrationWaitTimeout = 5 * time.Second
)
var qemuPaths = map[string]string{
QemuPCLite: "/usr/bin/qemu-lite-system-x86_64",
QemuPC: defaultQemuPath,
QemuQ35: defaultQemuPath,
}
var kernelParams = []Param{
{"tsc", "reliable"},
{"no_timer_check", ""},
{"rcupdate.rcu_expedited", "1"},
{"i8042.direct", "1"},
{"i8042.dumbkbd", "1"},
{"i8042.nopnp", "1"},
{"i8042.noaux", "1"},
{"noreplace-smp", ""},
{"reboot", "k"},
{"console", "hvc0"},
{"console", "hvc1"},
{"cryptomgr.notests", ""},
{"net.ifnames", "0"},
{"pci", "lastbus=0"},
}
var supportedQemuMachines = []govmmQemu.Machine{
{
Type: QemuPCLite,
Options: defaultQemuMachineOptions,
},
{
Type: QemuPC,
Options: defaultQemuMachineOptions,
},
{
Type: QemuQ35,
Options: defaultQemuMachineOptions,
},
{
Type: QemuVirt,
Options: defaultQemuMachineOptions,
},
}
// MaxQemuVCPUs returns the maximum number of vCPUs supported
func MaxQemuVCPUs() uint32 {
return uint32(240)
}
func newQemuArch(config HypervisorConfig) (qemuArch, error) {
machineType := config.HypervisorMachineType
if machineType == "" {
machineType = defaultQemuMachineType
}
var mp *govmmQemu.Machine
for _, m := range supportedQemuMachines {
if m.Type == machineType {
mp = &m
break
}
}
if mp == nil {
return nil, fmt.Errorf("unrecognised machinetype: %v", machineType)
}
factory := false
if config.BootToBeTemplate || config.BootFromTemplate {
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 i, m := range qemuMachines {
if m.Type == QemuQ35 {
qemuMachines[i].Options = q35QemuIOMMUOptions
}
}
} else {
kernelParams = append(kernelParams,
Param{"iommu", "off"})
}
q := &qemuAmd64{
qemuArchBase: qemuArchBase{
machineType: machineType,
memoryOffset: config.MemOffset,
qemuPaths: qemuPaths,
supportedQemuMachines: qemuMachines,
kernelParamsNonDebug: kernelParamsNonDebug,
kernelParamsDebug: kernelParamsDebug,
kernelParams: kernelParams,
disableNvdimm: config.DisableImageNvdimm,
dax: true,
},
vmFactory: factory,
}
q.handleImagePath(config)
return q, nil
}
func (q *qemuAmd64) capabilities() types.Capabilities {
var caps types.Capabilities
if q.machineType == QemuPC ||
q.machineType == QemuQ35 ||
q.machineType == QemuVirt {
caps.SetBlockDeviceHotplugSupport()
}
caps.SetMultiQueueSupport()
caps.SetFsSharingSupport()
return caps
}
func (q *qemuAmd64) bridges(number uint32) {
q.Bridges = genericBridges(number, q.machineType)
}
func (q *qemuAmd64) cpuModel() string {
cpuModel := defaultCPUModel
if q.nestedRun {
cpuModel += ",pmu=off"
}
// VMX is not migratable yet.
// issue: https://github.com/kata-containers/kata-containers/src/runtime/issues/1750
if q.vmFactory {
virtLog.WithField("subsystem", "qemuAmd64").Warn("VMX is not migratable yet: turning it off")
cpuModel += ",vmx=off"
}
return cpuModel
}
func (q *qemuAmd64) memoryTopology(memoryMb, hostMemoryMb uint64, slots uint8) govmmQemu.Memory {
return genericMemoryTopology(memoryMb, hostMemoryMb, slots, q.memoryOffset)
}
func (q *qemuAmd64) appendImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) {
if !q.disableNvdimm {
return q.appendNvdimmImage(devices, path)
}
return q.appendBlockImage(devices, path)
}
// appendBridges appends to devices the given bridges
func (q *qemuAmd64) appendBridges(devices []govmmQemu.Device) []govmmQemu.Device {
return genericAppendBridges(devices, q.Bridges, q.machineType)
}