Skip to contentSkip to conversation

rng-builtin: add an RNG backend that uses qemu_guest_getrandom()

PATCH Applied Laurent Vivier 2 participants 5 messages Aug 20, 2019 – Sep 4, 2019

lore
Share & tools
lore
#0 lore raw

v8 cover rng-builtin: add an RNG backend that uses qemu_guest_getrandom()

Add a new RNG backend using QEMU builtin getrandom function.

v8: rebase on v4.1

v7: rebase on master
Make rng-builtin asynchronous with QEMUBH (removed existing R-b)

v6: remove "sysemu/rng-random.h" from virtio-rng.c
rebase on qemu_getrandom v8

v5: PATCH 1 s/linux/Linux/
remove superfluous includes from rng-builtin.c
don't update rng-random documentation
add a patch from Markus to keep the default backend out of VirtIORNGConf move TYPE_RNG_BUILTIN to sysemu/rng.h and remove sysemu/rng-builtin.h

v4: update PATCH 1 commit message

v3: Include Kashyap's patch in the series
Add a patch to change virtio-rng default backend to rng-builtin

v2: Update qemu-options.hx
describe the new backend and specify virtio-rng uses the rng-random by default

Laurent Vivier (2):
rng-builtin: add an RNG backend that uses qemu_guest_getrandom()
virtio-rng: change default backend to rng-builtin

Markus Armbruster (1):
virtio-rng: Keep the default backend out of VirtIORNGConf

 backends/Makefile.objs         |  2 +-
 backends/rng-builtin.c         | 77 ++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-rng.c         | 19 ++++-----
 include/hw/virtio/virtio-rng.h |  2 -
 include/sysemu/rng.h           |  2 +
 qemu-options.hx                |  7 ++++
 6 files changed, 95 insertions(+), 14 deletions(-)
 create mode 100644 backends/rng-builtin.c
Signature · 1 line
2.21.0
#1 lore raw

PATCH v8 1/3 rng-builtin: add an RNG backend that uses qemu_guest_getrandom()

Add a new RNG backend using QEMU builtin getrandom function.

It can be created and used with something like:

    ... -object rng-builtin,id=rng0 -device virtio-rng,rng=rng0 ...
---
 backends/Makefile.objs |  2 +-
 backends/rng-builtin.c | 78 ++++++++++++++++++++++++++++++++++++++++++
 qemu-options.hx        |  7 ++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 backends/rng-builtin.c
backends/Makefile.objs +1 -1
index 981e8e122f2c..f0691116e86e 100644
@@ -1,4 +1,4 @@
-common-obj-y += rng.o rng-egd.o
+common-obj-y += rng.o rng-egd.o rng-builtin.o
 common-obj-$(CONFIG_POSIX) += rng-random.o
 
 common-obj-$(CONFIG_TPM) += tpm.o
backends/rng-builtin.c new file +78 -0

78 lines not on this page · raw message

qemu-options.hx +7 -0
index 9621e934c0bf..312586cc18f8 100644
@@ -4323,6 +4323,13 @@ other options.
 
 The @option{share} boolean option is @var{on} by default with memfd.
 
+@item -object rng-builtin,id=@var{id}
+
+Creates a random number generator backend which obtains entropy from
+QEMU builtin functions. The @option{id} parameter is a unique ID that
+will be used to reference this entropy backend from the @option{virtio-rng}
+device.
+
 @item -object rng-random,id=@var{id},filename=@var{/dev/random}
 
 Creates a random number generator backend which obtains entropy from
Signature · 1 line
2.21.0
#2 lore raw

PATCH v8 2/3 virtio-rng: Keep the default backend out of VirtIORNGConf

In reply to Laurent Vivier

From: Markus Armbruster <[email protected]>

The default backend is only used within virtio_rng_device_realize(). Replace VirtIORNGConf member default_backend by a local variable. Adjust its type to reduce conversions.

While there, pass &error_abort instead of NULL when failure would be a programming error.

---
 hw/virtio/virtio-rng.c         | 20 +++++++++-----------
 include/hw/virtio/virtio-rng.h |  2 --
 2 files changed, 9 insertions(+), 13 deletions(-)
hw/virtio/virtio-rng.c +9 -11
index d1650e1dee19..fcf3b2e32912 100644
@@ -19,6 +19,7 @@
 #include "hw/virtio/virtio-rng.h"
 #include "sysemu/rng.h"
 #include "sysemu/runstate.h"
+#include "sysemu/rng-random.h"
 #include "qom/object_interfaces.h"
 #include "trace.h"
 
@@ -192,27 +193,24 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp)
     }
 
     if (vrng->conf.rng == NULL) {
-        vrng->conf.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
+        Object *default_backend = object_new(TYPE_RNG_RANDOM);
 
-        user_creatable_complete(USER_CREATABLE(vrng->conf.default_backend),
+        user_creatable_complete(USER_CREATABLE(default_backend),
                                 &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
-            object_unref(OBJECT(vrng->conf.default_backend));
+            object_unref(default_backend);
             return;
         }
 
-        object_property_add_child(OBJECT(dev),
-                                  "default-backend",
-                                  OBJECT(vrng->conf.default_backend),
-                                  NULL);
+        object_property_add_child(OBJECT(dev), "default-backend",
+                                  default_backend, &error_abort);
 
         /* The child property took a reference, we can safely drop ours now */
-        object_unref(OBJECT(vrng->conf.default_backend));
+        object_unref(default_backend);
 
-        object_property_set_link(OBJECT(dev),
-                                 OBJECT(vrng->conf.default_backend),
-                                 "rng", NULL);
+        object_property_set_link(OBJECT(dev), default_backend,
+                                 "rng", &error_abort);
     }
 
     vrng->rng = vrng->conf.rng;
include/hw/virtio/virtio-rng.h +0 -2
index ff699335e3b9..bd05d734b87d 100644
@@ -14,7 +14,6 @@
 
 #include "hw/virtio/virtio.h"
 #include "sysemu/rng.h"
-#include "sysemu/rng-random.h"
 #include "standard-headers/linux/virtio_rng.h"
 
 #define TYPE_VIRTIO_RNG "virtio-rng-device"
@@ -27,7 +26,6 @@ struct VirtIORNGConf {
     RngBackend *rng;
     uint64_t max_bytes;
     uint32_t period_ms;
-    RngRandom *default_backend;
 };
 
 typedef struct VirtIORNG {
Signature · 1 line
2.21.0
#3 lore raw

PATCH v8 3/3 virtio-rng: change default backend to rng-builtin

In reply to Laurent Vivier
---
 backends/rng-builtin.c | 1 -
 hw/virtio/virtio-rng.c | 3 +--
 include/sysemu/rng.h   | 2 ++
 qemu-options.hx        | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
backends/rng-builtin.c +0 -1
index 3381d47174df..ba1b8d66b83e 100644
@@ -10,7 +10,6 @@
 #include "qemu/main-loop.h"
 #include "qemu/guest-random.h"
 
-#define TYPE_RNG_BUILTIN "rng-builtin"
 #define RNG_BUILTIN(obj) OBJECT_CHECK(RngBuiltin, (obj), TYPE_RNG_BUILTIN)
 
 typedef struct RngBuiltin {
hw/virtio/virtio-rng.c +1 -2
index fcf3b2e32912..e93bed020f64 100644
@@ -19,7 +19,6 @@
 #include "hw/virtio/virtio-rng.h"
 #include "sysemu/rng.h"
 #include "sysemu/runstate.h"
-#include "sysemu/rng-random.h"
 #include "qom/object_interfaces.h"
 #include "trace.h"
 
@@ -193,7 +192,7 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp)
     }
 
     if (vrng->conf.rng == NULL) {
-        Object *default_backend = object_new(TYPE_RNG_RANDOM);
+        Object *default_backend = object_new(TYPE_RNG_BUILTIN);
 
         user_creatable_complete(USER_CREATABLE(default_backend),
                                 &local_err);
include/sysemu/rng.h +2 -0
index 9b22c156f8e6..fa6eada78cd7 100644
@@ -24,6 +24,8 @@
 #define RNG_BACKEND_CLASS(klass) \
     OBJECT_CLASS_CHECK(RngBackendClass, (klass), TYPE_RNG_BACKEND)
 
+#define TYPE_RNG_BUILTIN "rng-builtin"
+
 typedef struct RngRequest RngRequest;
 typedef struct RngBackendClass RngBackendClass;
 typedef struct RngBackend RngBackend;
qemu-options.hx +1 -1
index 312586cc18f8..14b0bbeffc96 100644
@@ -4328,7 +4328,7 @@ The @option{share} boolean option is @var{on} by default with memfd.
 Creates a random number generator backend which obtains entropy from
 QEMU builtin functions. The @option{id} parameter is a unique ID that
 will be used to reference this entropy backend from the @option{virtio-rng}
-device.
+device. By default, the @option{virtio-rng} device uses this RNG backend.
 
 @item -object rng-random,id=@var{id},filename=@var{/dev/random}
 
Signature · 1 line
2.21.0
#4 lore raw
In reply to Laurent Vivier
Quoted 7 lines · Laurent Vivier

Add a new RNG backend using QEMU builtin getrandom function.

v8: rebase on v4.1

v7: rebase on master
Make rng-builtin asynchronous with QEMUBH (removed existing R-b)

Quoted 8 lines

v6: remove "sysemu/rng-random.h" from virtio-rng.c
rebase on qemu_getrandom v8

v5: PATCH 1 s/linux/Linux/
remove superfluous includes from rng-builtin.c
don't update rng-random documentation
add a patch from Markus to keep the default backend out of VirtIORNGConf move TYPE_RNG_BUILTIN to sysemu/rng.h and remove sysemu/rng-builtin.h

Applied, thanks!

Quoted 27 lines

v4: update PATCH 1 commit message

v3: Include Kashyap's patch in the series
Add a patch to change virtio-rng default backend to rng-builtin

v2: Update qemu-options.hx
describe the new backend and specify virtio-rng uses the rng-random by default

Laurent Vivier (2):
rng-builtin: add an RNG backend that uses qemu_guest_getrandom()
virtio-rng: change default backend to rng-builtin

Markus Armbruster (1):
virtio-rng: Keep the default backend out of VirtIORNGConf

 backends/Makefile.objs         |  2 +-
 backends/rng-builtin.c         | 77 ++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-rng.c         | 19 ++++-----
 include/hw/virtio/virtio-rng.h |  2 -
 include/sysemu/rng.h           |  2 +
 qemu-options.hx                |  7 ++++
 6 files changed, 95 insertions(+), 14 deletions(-)
 create mode 100644 backends/rng-builtin.c
Signature · 1 line
2.21.0