From bd51f89c3038396efbf25f2846c4f9cd627f0ce7 Mon Sep 17 00:00:00 2001 From: Han Han Date: Thu, 9 Jan 2020 16:00:08 +0800 Subject: [PATCH] qemu: Implement builtin rng backend Since v4.2-rc0, QEMU introduced a builtin rng backend that uses getrandom() syscall to generate random. Add it to libvirt with the backend model 'builtin'. https://bugzilla.redhat.com/show_bug.cgi?id=1785091 Signed-off-by: Han Han Reviewed-by: Michal Privoznik --- src/qemu/qemu_capabilities.c | 2 ++ src/qemu/qemu_command.c | 20 +++++++---- .../domaincapsdata/qemu_4.2.0-q35.x86_64.xml | 1 + .../domaincapsdata/qemu_4.2.0-tcg.x86_64.xml | 1 + .../qemu_4.2.0-virt.aarch64.xml | 1 + tests/domaincapsdata/qemu_4.2.0.aarch64.xml | 1 + tests/domaincapsdata/qemu_4.2.0.ppc64.xml | 1 + tests/domaincapsdata/qemu_4.2.0.s390x.xml | 1 + tests/domaincapsdata/qemu_4.2.0.x86_64.xml | 1 + .../virtio-rng-builtin.x86_64-latest.args | 35 +++++++++++++++++++ tests/qemuxml2argvdata/virtio-rng-builtin.xml | 26 ++++++++++++++ tests/qemuxml2argvtest.c | 1 + .../virtio-rng-builtin.x86_64-latest.xml | 31 ++++++++++++++++ tests/qemuxml2xmltest.c | 1 + 14 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 tests/qemuxml2argvdata/virtio-rng-builtin.x86_64-latest.args create mode 100644 tests/qemuxml2argvdata/virtio-rng-builtin.xml create mode 100644 tests/qemuxml2xmloutdata/virtio-rng-builtin.x86_64-latest.xml diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 5ba3519386..ca4d4c1a40 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -5844,6 +5844,8 @@ virQEMUCapsFillDomainDeviceRNGCaps(virQEMUCapsPtr qemuCaps, VIR_DOMAIN_CAPS_ENUM_SET(rng->backendModel, VIR_DOMAIN_RNG_BACKEND_EGD); if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_RNG_RANDOM)) VIR_DOMAIN_CAPS_ENUM_SET(rng->backendModel, VIR_DOMAIN_RNG_BACKEND_RANDOM); + if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_RNG_BUILTIN)) + VIR_DOMAIN_CAPS_ENUM_SET(rng->backendModel, VIR_DOMAIN_RNG_BACKEND_BUILTIN); } diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index c8322c2112..46680fd829 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -5583,6 +5583,7 @@ qemuBuildRNGBackendChrdevStr(virLogManagerPtr logManager, switch ((virDomainRNGBackend) rng->backend) { case VIR_DOMAIN_RNG_BACKEND_RANDOM: + case VIR_DOMAIN_RNG_BACKEND_BUILTIN: case VIR_DOMAIN_RNG_BACKEND_LAST: /* no chardev backend is needed */ return 0; @@ -5595,11 +5596,6 @@ qemuBuildRNGBackendChrdevStr(virLogManagerPtr logManager, cdevflags))) return -1; break; - - case VIR_DOMAIN_RNG_BACKEND_BUILTIN: - virReportUnsupportedError(); - return -1; - break; } return 0; @@ -5651,8 +5647,18 @@ qemuBuildRNGBackendProps(virDomainRNGDefPtr rng, break; case VIR_DOMAIN_RNG_BACKEND_BUILTIN: - virReportUnsupportedError(); - return -1; + if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_RNG_BUILTIN)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("this qemu doesn't support the rng-builtin " + "backend")); + return -1; + } + + if (qemuMonitorCreateObjectProps(props, "rng-builtin", objAlias, + NULL) < 0) + return -1; + + break; case VIR_DOMAIN_RNG_BACKEND_LAST: virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", diff --git a/tests/domaincapsdata/qemu_4.2.0-q35.x86_64.xml b/tests/domaincapsdata/qemu_4.2.0-q35.x86_64.xml index c4c6bfb564..eaa3b75695 100644 --- a/tests/domaincapsdata/qemu_4.2.0-q35.x86_64.xml +++ b/tests/domaincapsdata/qemu_4.2.0-q35.x86_64.xml @@ -167,6 +167,7 @@ random egd + builtin diff --git a/tests/domaincapsdata/qemu_4.2.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_4.2.0-tcg.x86_64.xml index a7f8d9c5ee..af482e3821 100644 --- a/tests/domaincapsdata/qemu_4.2.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_4.2.0-tcg.x86_64.xml @@ -178,6 +178,7 @@ random egd + builtin diff --git a/tests/domaincapsdata/qemu_4.2.0-virt.aarch64.xml b/tests/domaincapsdata/qemu_4.2.0-virt.aarch64.xml index e5954717cc..908406bf11 100644 --- a/tests/domaincapsdata/qemu_4.2.0-virt.aarch64.xml +++ b/tests/domaincapsdata/qemu_4.2.0-virt.aarch64.xml @@ -136,6 +136,7 @@ random egd + builtin diff --git a/tests/domaincapsdata/qemu_4.2.0.aarch64.xml b/tests/domaincapsdata/qemu_4.2.0.aarch64.xml index bb02b1d850..c66550cec5 100644 --- a/tests/domaincapsdata/qemu_4.2.0.aarch64.xml +++ b/tests/domaincapsdata/qemu_4.2.0.aarch64.xml @@ -134,6 +134,7 @@ random egd + builtin diff --git a/tests/domaincapsdata/qemu_4.2.0.ppc64.xml b/tests/domaincapsdata/qemu_4.2.0.ppc64.xml index 6d3ada3735..7906482eee 100644 --- a/tests/domaincapsdata/qemu_4.2.0.ppc64.xml +++ b/tests/domaincapsdata/qemu_4.2.0.ppc64.xml @@ -100,6 +100,7 @@ random egd + builtin diff --git a/tests/domaincapsdata/qemu_4.2.0.s390x.xml b/tests/domaincapsdata/qemu_4.2.0.s390x.xml index 101d9d028b..fbb3905f92 100644 --- a/tests/domaincapsdata/qemu_4.2.0.s390x.xml +++ b/tests/domaincapsdata/qemu_4.2.0.s390x.xml @@ -190,6 +190,7 @@ random egd + builtin diff --git a/tests/domaincapsdata/qemu_4.2.0.x86_64.xml b/tests/domaincapsdata/qemu_4.2.0.x86_64.xml index f0e0f182a4..c82d12d3ce 100644 --- a/tests/domaincapsdata/qemu_4.2.0.x86_64.xml +++ b/tests/domaincapsdata/qemu_4.2.0.x86_64.xml @@ -167,6 +167,7 @@ random egd + builtin diff --git a/tests/qemuxml2argvdata/virtio-rng-builtin.x86_64-latest.args b/tests/qemuxml2argvdata/virtio-rng-builtin.x86_64-latest.args new file mode 100644 index 0000000000..e55117728c --- /dev/null +++ b/tests/qemuxml2argvdata/virtio-rng-builtin.x86_64-latest.args @@ -0,0 +1,35 @@ +LC_ALL=C \ +PATH=/bin \ +HOME=/tmp/lib/domain--1-QEMUGuest1 \ +USER=test \ +LOGNAME=test \ +XDG_DATA_HOME=/tmp/lib/domain--1-QEMUGuest1/.local/share \ +XDG_CACHE_HOME=/tmp/lib/domain--1-QEMUGuest1/.cache \ +XDG_CONFIG_HOME=/tmp/lib/domain--1-QEMUGuest1/.config \ +QEMU_AUDIO_DRV=none \ +/usr/bin/qemu-system-x86_64 \ +-name guest=QEMUGuest1,debug-threads=on \ +-S \ +-object secret,id=masterKey0,format=raw,\ +file=/tmp/lib/domain--1-QEMUGuest1/master-key.aes \ +-machine pc,accel=kvm,usb=off,dump-guest-core=off \ +-m 214 \ +-overcommit mem-lock=off \ +-smp 1,sockets=1,cores=1,threads=1 \ +-uuid c7a5fdbd-edaf-9455-926a-d65c16db1809 \ +-display none \ +-no-user-config \ +-nodefaults \ +-chardev socket,id=charmonitor,fd=1729,server,nowait \ +-mon chardev=charmonitor,id=monitor,mode=control \ +-rtc base=utc \ +-no-shutdown \ +-no-acpi \ +-boot strict=on \ +-device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 \ +-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x2 \ +-object rng-builtin,id=objrng0 \ +-device virtio-rng-pci,rng=objrng0,id=rng0,bus=pci.0,addr=0x3 \ +-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,\ +resourcecontrol=deny \ +-msg timestamp=on diff --git a/tests/qemuxml2argvdata/virtio-rng-builtin.xml b/tests/qemuxml2argvdata/virtio-rng-builtin.xml new file mode 100644 index 0000000000..0a03eafc4b --- /dev/null +++ b/tests/qemuxml2argvdata/virtio-rng-builtin.xml @@ -0,0 +1,26 @@ + + QEMUGuest1 + c7a5fdbd-edaf-9455-926a-d65c16db1809 + 219100 + 219100 + 1 + + hvm + + + + destroy + restart + destroy + + /usr/bin/qemu-system-x86_64 + + + + + + + + + + diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index b923590930..10b8f1806e 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -2121,6 +2121,7 @@ mymain(void) DO_TEST("virtio-rng-egd", QEMU_CAPS_DEVICE_VIRTIO_RNG, QEMU_CAPS_OBJECT_RNG_EGD); + DO_TEST_CAPS_LATEST("virtio-rng-builtin"); DO_TEST_CAPS_VER("virtio-rng-egd-unix", "2.5.0"); DO_TEST_CAPS_LATEST("virtio-rng-egd-unix"); DO_TEST("virtio-rng-multiple", diff --git a/tests/qemuxml2xmloutdata/virtio-rng-builtin.x86_64-latest.xml b/tests/qemuxml2xmloutdata/virtio-rng-builtin.x86_64-latest.xml new file mode 100644 index 0000000000..9ff57c4ad6 --- /dev/null +++ b/tests/qemuxml2xmloutdata/virtio-rng-builtin.x86_64-latest.xml @@ -0,0 +1,31 @@ + + QEMUGuest1 + c7a5fdbd-edaf-9455-926a-d65c16db1809 + 219100 + 219100 + 1 + + hvm + + + + destroy + restart + destroy + + /usr/bin/qemu-system-x86_64 + +
+ + + + + +
+ + + +
+ + + diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c index 3cefc64833..65ffaf9cfc 100644 --- a/tests/qemuxml2xmltest.c +++ b/tests/qemuxml2xmltest.c @@ -630,6 +630,7 @@ mymain(void) QEMU_CAPS_DEVICE_VIRTIO_RNG); DO_TEST("virtio-rng-egd", QEMU_CAPS_DEVICE_VIRTIO_RNG); + DO_TEST_CAPS_LATEST("virtio-rng-builtin"); DO_TEST("pseries-nvram", QEMU_CAPS_DEVICE_SPAPR_PCI_HOST_BRIDGE,