Merge pull request #676 from jcvenegas/rng-device

qemu: Add rng virtio device
This commit is contained in:
Sebastien Boeuf 2018-09-11 07:03:07 -07:00 committed by GitHub
commit e02695bf7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 129 additions and 4 deletions

4
Gopkg.lock generated
View File

@ -123,11 +123,11 @@
revision = "3520598351bb3500a49ae9563f5539666ae0a27c"
[[projects]]
digest = "1:052c0d6d677c7a3f12d8f83e0a1ce20a896cc206782b035f380249d23bf1265d"
digest = "1:7434a85a1d6c2bf64f322087ec7a7f84ab9e971179bef7b95deabaf0e3f7c126"
name = "github.com/intel/govmm"
packages = ["qemu"]
pruneopts = "NUT"
revision = "1a16b5f98f133796f9c5e9b6ae3aa6d786cff9b1"
revision = "e2c716433e444017507e3587ce071868fd164380"
[[projects]]
digest = "1:01c37fcb6e2a1fe1321a97faaef74c66ac531ea292ca3f929b7189cc400b1d47"

View File

@ -52,7 +52,7 @@
[[constraint]]
name = "github.com/intel/govmm"
revision = "1a16b5f98f133796f9c5e9b6ae3aa6d786cff9b1"
revision = "e2c716433e444017507e3587ce071868fd164380"
[[constraint]]
name = "github.com/kata-containers/agent"

View File

@ -85,6 +85,9 @@ const (
// VHostVSockPCI is the vhost vsock pci driver.
VHostVSockPCI DeviceDriver = "vhost-vsock-pci"
// VirtioRng is the paravirtualized RNG device driver.
VirtioRng DeviceDriver = "virtio-rng"
)
// ObjectType is a string representing a qemu object type.
@ -1017,6 +1020,63 @@ func (vsock VSOCKDevice) QemuParams(config *Config) []string {
return qemuParams
}
// RngDevice represents a random number generator device.
type RngDevice struct {
// ID is the device ID
ID string
// Filename is entropy source on the host
Filename string
// MaxBytes is the bytes allowed to guest to get from the hosts entropy per period
MaxBytes uint
// Period is duration of a read period in seconds
Period uint
}
// Valid returns true if the RngDevice structure is valid and complete.
func (v RngDevice) Valid() bool {
if v.ID == "" {
return false
}
return true
}
// QemuParams returns the qemu parameters built out of the RngDevice.
func (v RngDevice) QemuParams(_ *Config) []string {
var qemuParams []string
//-object rng-random,filename=/dev/hwrng,id=rng0
var objectParams []string
//-device virtio-rng-pci,rng=rng0,max-bytes=1024,period=1000
var deviceParams []string
objectParams = append(objectParams, "rng-random")
objectParams = append(objectParams, "id="+v.ID)
deviceParams = append(deviceParams, string(VirtioRng))
deviceParams = append(deviceParams, "rng="+v.ID)
if v.Filename != "" {
objectParams = append(objectParams, "filename="+v.Filename)
}
if v.MaxBytes > 0 {
deviceParams = append(deviceParams, fmt.Sprintf("max-bytes=%d", v.MaxBytes))
}
if v.Period > 0 {
deviceParams = append(deviceParams, fmt.Sprintf("period=%d", v.Period))
}
qemuParams = append(qemuParams, "-object")
qemuParams = append(qemuParams, strings.Join(objectParams, ","))
qemuParams = append(qemuParams, "-device")
qemuParams = append(qemuParams, strings.Join(deviceParams, ","))
return qemuParams
}
// RTCBaseType is the qemu RTC base time type.
type RTCBaseType string

View File

@ -160,6 +160,24 @@ type HotpluggableCPU struct {
QOMPath string `json:"qom-path"`
}
// MemoryDevicesData cotains the data describes a memory device
type MemoryDevicesData struct {
Slot int `json:"slot"`
Node int `json:"node"`
Addr uint64 `json:"addr"`
Memdev string `json:"memdev"`
ID string `json:"id"`
Hotpluggable bool `json:"hotpluggable"`
Hotplugged bool `json:"hotplugged"`
Size uint64 `json:"size"`
}
// MemoryDevices represents memory devices of vm
type MemoryDevices struct {
Data MemoryDevicesData `json:"data"`
Type string `json:"type"`
}
func (q *QMP) readLoop(fromVMCh chan<- []byte) {
scanner := bufio.NewScanner(q.conn)
for scanner.Scan() {
@ -989,6 +1007,28 @@ func (q *QMP) ExecSetMigrateArguments(ctx context.Context, url string) error {
return q.executeCommand(ctx, "migrate", args, nil)
}
// ExecQueryMemoryDevices returns a slice with the list of memory devices
func (q *QMP) ExecQueryMemoryDevices(ctx context.Context) ([]MemoryDevices, error) {
response, err := q.executeCommandWithResponse(ctx, "query-memory-devices", nil, nil, nil)
if err != nil {
return nil, err
}
// convert response to json
data, err := json.Marshal(response)
if err != nil {
return nil, fmt.Errorf("Unable to extract memory devices information: %v", err)
}
var memoryDevices []MemoryDevices
// convert json to []MemoryDevices
if err = json.Unmarshal(data, &memoryDevices); err != nil {
return nil, fmt.Errorf("unable to convert json to memory devices: %v", err)
}
return memoryDevices, nil
}
// ExecHotplugMemory adds size of MiB memory to the guest
func (q *QMP) ExecHotplugMemory(ctx context.Context, qomtype, id, mempath string, size int) error {
args := map[string]interface{}{

View File

@ -64,7 +64,7 @@ assets:
description: "lightweight VMM that uses KVM"
url: "https://github.com/kata-containers/qemu"
branch: "qemu-lite-2.11.0"
commit: "a39e0b3e828ff6fb4457865ef7a021f1e7320c27"
commit: "f88622805677163b04498dcba35ceca0183b1318"
qemu:
description: "VMM that uses KVM"

View File

@ -117,6 +117,12 @@ type VFIODev struct {
BDF string
}
// RNGDev represents a random number generator device
type RNGDev struct {
// ID is used to identify the device in the hypervisor options.
ID string
}
// VhostUserDeviceAttrs represents data shared by most vhost-user devices
type VhostUserDeviceAttrs struct {
DevID string

View File

@ -79,6 +79,7 @@ const (
qmpExecCatCmd = "exec:cat"
scsiControllerID = "scsi0"
rngID = "rng0"
)
var qemuMajorVersion int
@ -482,6 +483,11 @@ func (q *qemu) createSandbox() error {
if ioThread != nil {
qemuConfig.IOThreads = []govmmQemu.IOThread{*ioThread}
}
// Add RNG device to hypervisor
rngDev := config.RNGDev{
ID: rngID,
}
qemuConfig.Devices = q.arch.appendRNGDevice(qemuConfig.Devices, rngDev)
q.qemuConfig = qemuConfig

View File

@ -82,6 +82,9 @@ type qemuArch interface {
// appendVFIODevice appends a VFIO device to devices
appendVFIODevice(devices []govmmQemu.Device, vfioDevice config.VFIODev) []govmmQemu.Device
// appendRNGDevice appends a RNG device to devices
appendRNGDevice(devices []govmmQemu.Device, rngDevice config.RNGDev) []govmmQemu.Device
// handleImagePath handles the Hypervisor Config image path
handleImagePath(config HypervisorConfig)
}
@ -505,6 +508,16 @@ func (q *qemuArchBase) appendVFIODevice(devices []govmmQemu.Device, vfioDev conf
return devices
}
func (q *qemuArchBase) appendRNGDevice(devices []govmmQemu.Device, rngDev config.RNGDev) []govmmQemu.Device {
devices = append(devices,
govmmQemu.RngDevice{
ID: rngDev.ID,
},
)
return devices
}
func (q *qemuArchBase) handleImagePath(config HypervisorConfig) {
if config.ImagePath != "" {
q.kernelParams = append(q.kernelParams, kernelRootParams...)