Skip to contentSkip to conversation

add paravirtualization hwrng support (v2)

PATCH In review Anthony Liguori 4 participants 13 messages Oct 30, 2012 – Oct 31, 2012 Kbuild

lore
Share & tools
lore
#0 lore raw

cover add paravirtualization hwrng support (v2)

Hi,

This series implements the backend and frontend infrastructure for virtio-rng. This is similar to previous series sent out by both Amit and myself although it has been trimmed down considerably.

In terms of backends, a file and EGD backend are supported. The file defaults to /dev/random based on the feedback from Peter. It's still possible to support /dev/urandom though as an entropy source by overriding the file name.

v1 -> v2:
- Include default backend patch from separate series
- Add a rate limiting patch

My take away from all of the various discussions on what the Right Way to use virtio-rng is:

1) /dev/random should always be used as the entropy source (I've left it configurable though)

2) I think the Right Way to configure virtio-rng is to figure out what the available entropy is on the host, and then decide how to allocate that to each guest. As such, I've implemented rate limiting.

I think QEMU is the right place to do this because this is a property of specific virtual machines. I can imagine a cloud provider wanting to guarantee a certain level of entropy for different classes of VMs. Even if rngd could be used to do this, configuring it differently for different guests would be cumbersome.

3) qemu -device virtio-rng-pci will Just Work but risks exhausting host entropy. This means we can't make it the default for machines. But for most command line users, I think this is the behavior they want.

#1 lore raw

PATCH 1/8 vl: add -object option to create QOM objects from the command line

This will create a new QOM object in the '/objects' path. Note that properties are set in order which allows for simple objects to be initialized entirely with this option and then realized.

This option is roughly equivalent to -device but for things that are not devices.

---
 qemu-config.c   | 10 ++++++++++
 qemu-options.hx |  8 ++++++++
 vl.c            | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+)
qemu-config.c +10 -0
index e854fff..5dee47c 100644
@@ -678,6 +678,15 @@ static QemuOptsList qemu_add_fd_opts = {
     },
 };
 
+static QemuOptsList qemu_object_opts = {
+    .name = "object",
+    .implied_opt_name = "qom-type",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
+    .desc = {
+        { }
+    },
+};
+
 static QemuOptsList *vm_config_groups[32] = {
     &qemu_drive_opts,
     &qemu_chardev_opts,
@@ -695,6 +704,7 @@ static QemuOptsList *vm_config_groups[32] = {
     &qemu_iscsi_opts,
     &qemu_sandbox_opts,
     &qemu_add_fd_opts,
+    &qemu_object_opts,
     NULL,
 };
 
qemu-options.hx +8 -0
index a67a255..d201a82 100644
@@ -2888,6 +2888,14 @@ STEXI
 Enable FIPS 140-2 compliance mode.
 ETEXI
 
+DEF("object", HAS_ARG, QEMU_OPTION_object,
+    "-object TYPENAME[,PROP1=VALUE1,...]\n"
+    "                create an new object of type TYPENAME setting properties\n"
+    "                in the order they are specified.  Note that the 'id'\n"
+    "                property must be set.  These objects are placed in the\n"
+    "                '/objects' path.\n",
+    QEMU_ARCH_ALL)
+
 HXCOMM This is the last statement. Insert new options before this line!
 STEXI
 @end table
vl.c +56 -0

80 lines not on this page · raw message

Signature · 1 line
1.8.0
#2 lore raw

PATCH 2/8 object: add object_property_add_bool (v2)

In reply to Anthony Liguori

---
v1 -> v2
- Fix whitespace (Andreas Faerber)

---
 include/qemu/object.h | 16 +++++++++++++++
 qom/object.c          | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
include/qemu/object.h +16 -0
index cc75fee..be707f1 100644
@@ -947,6 +947,22 @@ void object_property_add_str(Object *obj, const char *name,
                              struct Error **errp);
 
 /**
+ * object_property_add_bool:
+ * @obj: the object to add a property to
+ * @name: the name of the property
+ * @get: the getter or NULL if the property is write-only.
+ * @set: the setter or NULL if the property is read-only
+ * @errp: if an error occurs, a pointer to an area to store the error
+ *
+ * Add a bool property using getters/setters.  This function will add a
+ * property of type 'bool'.
+ */
+void object_property_add_bool(Object *obj, const char *name,
+                              bool (*get)(Object *, struct Error **),
+                              void (*set)(Object *, bool, struct Error **),
+                              struct Error **errp);
+
+/**
  * object_child_foreach:
  * @obj: the object whose children will be navigated
  * @fn: the iterator function to be called
qom/object.c +56 -0

62 lines not on this page · raw message

Signature · 1 line
1.8.0
#3 lore raw

PATCH 3/8 rng: add RndBackend abstract object class

In reply to Anthony Liguori

This is the backend used by devices that need to request entropy.

---
 Makefile.objs          |  2 ++
 backends/Makefile.objs |  1 +
 backends/rng.c         | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/qemu/rng.h     | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 189 insertions(+)
 create mode 100644 backends/Makefile.objs
 create mode 100644 backends/rng.c
 create mode 100644 include/qemu/rng.h
Makefile.objs +2 -0
index 9eca179..1a14de6 100644
@@ -101,6 +101,8 @@ common-obj-y += vl.o
 
 common-obj-$(CONFIG_SLIRP) += slirp/
 
+common-obj-y += backends/
+
 ######################################################################
 # libseccomp
 ifeq ($(CONFIG_SECCOMP),y)
backends/Makefile.objs new file +1 -0
new file mode 100644
index 0000000..06e08c7
@@ -0,0 +1 @@
+common-obj-y += rng.o
backends/rng.c new file +93 -0

93 lines not on this page · raw message

include/qemu/rng.h new file +93 -0

93 lines not on this page · raw message

Signature · 1 line
1.8.0
#4 lore raw

PATCH 4/8 rng-random: add an RNG backend that uses /dev/random (v2)

In reply to Anthony Liguori

The filename can be overridden but it expects a non-blocking source of entropy. A typical invocation would be:

qemu -object rng-random,id=rng0 -device virtio-rng-pci,rng=rng0

This can also be used with /dev/urandom by using the command line:

qemu -object rng-random,filename=/dev/urandom,id=rng0 \
-device virtio-rng-pci,rng=rng0

---
v1 -> v2
- merged header split patch into this one

---
 backends/Makefile.objs    |   2 +-
 backends/rng-random.c     | 161 ++++++++++++++++++++++++++++++++++++++++++++++
 include/qemu/rng-random.h |  22 +++++++
 3 files changed, 184 insertions(+), 1 deletion(-)
 create mode 100644 backends/rng-random.c
 create mode 100644 include/qemu/rng-random.h
backends/Makefile.objs +1 -1
index 06e08c7..23ca19b 100644
@@ -1 +1 @@
-common-obj-y += rng.o
+common-obj-y += rng.o rng-random.o
backends/rng-random.c new file +161 -0

161 lines not on this page · raw message

include/qemu/rng-random.h new file +22 -0
new file mode 100644
index 0000000..6249290
@@ -0,0 +1,22 @@
+/*
+ * QEMU Random Number Generator Backend
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef QEMU_RNG_RANDOM_H
+#define QEMU_RNG_RANDOM_H
+
+#include "qemu/object.h"
+
+#define TYPE_RNG_RANDOM "rng-random"
+#define RNG_RANDOM(obj) OBJECT_CHECK(RndRandom, (obj), TYPE_RNG_RANDOM)
+
+typedef struct RndRandom RndRandom;
+
+#endif
Signature · 1 line
1.8.0
#5 lore raw

Re: [Qemu-devel] [PATCH 4/8] rng-random: add an RNG backend that uses /dev/random (v2)

Il 31/10/2012 00:02, Anthony Liguori ha scritto:

Quoted 71 lines

The filename can be overridden but it expects a non-blocking source of entropy. A typical invocation would be:

qemu -object rng-random,id=rng0 -device virtio-rng-pci,rng=rng0

This can also be used with /dev/urandom by using the command line:

qemu -object rng-random,filename=/dev/urandom,id=rng0 \
-device virtio-rng-pci,rng=rng0

---
v1 -> v2
- merged header split patch into this one

---
 backends/Makefile.objs    |   2 +-
 backends/rng-random.c     | 161 ++++++++++++++++++++++++++++++++++++++++++++++
 include/qemu/rng-random.h |  22 +++++++
 3 files changed, 184 insertions(+), 1 deletion(-)
 create mode 100644 backends/rng-random.c
 create mode 100644 include/qemu/rng-random.h
backends/Makefile.objs +1 -1
index 06e08c7..23ca19b 100644
@@ -1 +1 @@
-common-obj-y += rng.o
+common-obj-y += rng.o rng-random.o
backends/rng-random.c new file +36 -0
new file mode 100644
index 0000000..8325686
@@ -0,0 +1,161 @@
+/*
+ * QEMU Random Number Generator Backend
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/rng-random.h"
+#include "qemu/rng.h"
+#include "qerror.h"
+#include "main-loop.h"
+
+struct RndRandom
+{
+    RngBackend parent;
+
+    int fd;
+    char *filename;
+
+    EntropyReceiveFunc *receive_func;
+    void *opaque;
+    size_t size;
+};
+
+/**
+ * A simple and incomplete backend to request entropy from /dev/random.
+ *
+ * This backend exposes an additional "filename" property that can be used to
+ * set the filename to use to open the backend.
+ */
+

backends/rng-random.c line 46

Quoted 10 lines
backends/rng-random.c new file +10 -0
+static void entropy_available(void *opaque)
+{
+    RndRandom *s = RNG_RANDOM(opaque);
+    uint8_t buffer[s->size];
+    ssize_t len;
+
+    len = read(s->fd, buffer, s->size);
+    g_assert(len != -1);
+
+    s->receive_func(s->opaque, buffer, s->size);

Should be len here, not s->size.

Otherwise looks good.

Paolo

Quoted 145 lines
backends/rng-random.c new file +115 -0
+    s->receive_func = NULL;
+
+    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+}
+
+static void rng_random_request_entropy(RngBackend *b, size_t size,
+                                        EntropyReceiveFunc *receive_entropy,
+                                        void *opaque)
+{
+    RndRandom *s = RNG_RANDOM(b);
+
+    if (s->receive_func) {
+        s->receive_func(s->opaque, NULL, 0);
+    }
+
+    s->receive_func = receive_entropy;
+    s->opaque = opaque;
+    s->size = size;
+
+    qemu_set_fd_handler(s->fd, entropy_available, NULL, s);
+}
+
+static void rng_random_opened(RngBackend *b, Error **errp)
+{
+    RndRandom *s = RNG_RANDOM(b);
+
+    if (s->filename == NULL) {
+        error_set(errp, QERR_INVALID_PARAMETER_VALUE,
+                  "filename", "a valid filename");
+    } else {
+        s->fd = open(s->filename, O_RDONLY | O_NONBLOCK);
+
+        if (s->fd == -1) {
+            error_set(errp, QERR_OPEN_FILE_FAILED, s->filename);
+        }
+    }
+}
+
+static char *rng_random_get_filename(Object *obj, Error **errp)
+{
+    RndRandom *s = RNG_RANDOM(obj);
+
+    if (s->filename) {
+        return g_strdup(s->filename);
+    }
+
+    return NULL;
+}
+
+static void rng_random_set_filename(Object *obj, const char *filename,
+                                 Error **errp)
+{
+    RngBackend *b = RNG_BACKEND(obj);
+    RndRandom *s = RNG_RANDOM(obj);
+
+    if (b->opened) {
+        error_set(errp, QERR_PERMISSION_DENIED);
+        return;
+    }
+
+    if (s->filename) {
+        g_free(s->filename);
+    }
+
+    s->filename = g_strdup(filename);
+}
+
+static void rng_random_init(Object *obj)
+{
+    RndRandom *s = RNG_RANDOM(obj);
+
+    object_property_add_str(obj, "filename",
+                            rng_random_get_filename,
+                            rng_random_set_filename,
+                            NULL);
+
+    s->filename = g_strdup("/dev/random");
+}
+
+static void rng_random_finalize(Object *obj)
+{
+    RndRandom *s = RNG_RANDOM(obj);
+
+    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+
+    if (s->fd != -1) {
+        close(s->fd);
+    }
+
+    g_free(s->filename);
+}
+
+static void rng_random_class_init(ObjectClass *klass, void *data)
+{
+    RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
+
+    rbc->request_entropy = rng_random_request_entropy;
+    rbc->opened = rng_random_opened;
+}
+
+static TypeInfo rng_random_info = {
+    .name = TYPE_RNG_RANDOM,
+    .parent = TYPE_RNG_BACKEND,
+    .instance_size = sizeof(RndRandom),
+    .class_init = rng_random_class_init,
+    .instance_init = rng_random_init,
+    .instance_finalize = rng_random_finalize,
+};
+
+static void register_types(void)
+{
+    type_register_static(&rng_random_info);
+}
+
+type_init(register_types);
include/qemu/rng-random.h new file +22 -1
new file mode 100644
index 0000000..6249290
@@ -0,0 +1,22 @@
+/*
+ * QEMU Random Number Generator Backend
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef QEMU_RNG_RANDOM_H
+#define QEMU_RNG_RANDOM_H
+
+#include "qemu/object.h"
+
+#define TYPE_RNG_RANDOM "rng-random"
+#define RNG_RANDOM(obj) OBJECT_CHECK(RndRandom, (obj), TYPE_RNG_RANDOM)
+
+typedef struct RndRandom RndRandom;
+
+#endif
-- 1.8.0
#6 lore raw

PATCH 5/8 rng-egd: introduce EGD compliant RNG backend

In reply to Anthony Liguori

This backend talks EGD to a CharDriverState. A typical way to invoke this would be:

qemu -chardev socket,host=localhost,port=1024,id=chr0 \
     -object rng-egd,chardev=chr0,id=egd0 \
     -device virtio-rng-pci,rng=egd0
---
 backends/Makefile.objs |   2 +-
 backends/rng-egd.c     | 215 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 216 insertions(+), 1 deletion(-)
 create mode 100644 backends/rng-egd.c
backends/Makefile.objs +1 -1
index 23ca19b..875eebc 100644
@@ -1 +1 @@
-common-obj-y += rng.o rng-random.o
+common-obj-y += rng.o rng-random.o rng-egd.o
backends/rng-egd.c new file +215 -0

215 lines not on this page · raw message

Signature · 1 line
1.8.0
#7 lore raw

PATCH 6/8 virtio-rng: hardware random number generator device

In reply to Anthony Liguori

From: Amit Shah <[email protected]>

The Linux kernel already has a virtio-rng driver, this is the device implementation.

When the guest asks for entropy from the virtio hwrng, it puts a buffer in the vq. We then put entropy into that buffer, and push it back to the guest.

---
aliguori: converted to new RngBackend interface
aliguori: remove entropy needed event
aliguori: fix migration

---
 hw/Makefile.objs     |   1 +
 hw/pci.h             |   1 +
 hw/s390-virtio-bus.c |  37 +++++++++
 hw/s390-virtio-bus.h |   2 +
 hw/virtio-pci.c      |  60 +++++++++++++++
 hw/virtio-pci.h      |   2 +
 hw/virtio-rng.c      | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/virtio-rng.h      |  24 ++++++
 hw/virtio.h          |   3 +
 9 files changed, 341 insertions(+)
 create mode 100644 hw/virtio-rng.c
 create mode 100644 hw/virtio-rng.h
hw/Makefile.objs +1 -0
index af4ab0c..ea46f81 100644
@@ -1,6 +1,7 @@
 common-obj-y = usb/ ide/
 common-obj-y += loader.o
 common-obj-$(CONFIG_VIRTIO) += virtio-console.o
+common-obj-$(CONFIG_VIRTIO) += virtio-rng.o
 common-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o
 common-obj-y += fw_cfg.o
 common-obj-$(CONFIG_PCI) += pci.o pci_bridge.o pci_bridge_dev.o
hw/pci.h +1 -0
index 241c1d8..4da0c2a 100644
@@ -76,6 +76,7 @@
 #define PCI_DEVICE_ID_VIRTIO_BALLOON     0x1002
 #define PCI_DEVICE_ID_VIRTIO_CONSOLE     0x1003
 #define PCI_DEVICE_ID_VIRTIO_SCSI        0x1004
+#define PCI_DEVICE_ID_VIRTIO_RNG         0x1005
 
 #define FMT_PCIBUS                      PRIx64
 
hw/s390-virtio-bus.c +37 -0
index 5849a96..e0ac2d1 100644
@@ -26,6 +26,7 @@
 #include "loader.h"
 #include "elf.h"
 #include "hw/virtio.h"
+#include "hw/virtio-rng.h"
 #include "hw/virtio-serial.h"
 #include "hw/virtio-net.h"
 #include "hw/sysbus.h"
@@ -206,6 +207,18 @@ static int s390_virtio_scsi_init(VirtIOS390Device *dev)
     return s390_virtio_device_init(dev, vdev);
 }
 
+static int s390_virtio_rng_init(VirtIOS390Device *dev)
+{
+    VirtIODevice *vdev;
+
+    vdev = virtio_rng_init((DeviceState *)dev, &dev->rng);
+    if (!vdev) {
+        return -1;
+    }
+
+    return s390_virtio_device_init(dev, vdev);
+}
+
 static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
 {
     ram_addr_t token_off;
@@ -448,6 +461,29 @@ static TypeInfo s390_virtio_serial = {
     .class_init    = s390_virtio_serial_class_init,
 };
 
+static void s390_virtio_rng_initfn(Object *obj)
+{
+    VirtIOS390Device *dev = VIRTIO_S390_DEVICE(obj);
+
+    object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
+                             (Object **)&dev->rng.rng, NULL);
+}
+
+static void s390_virtio_rng_class_init(ObjectClass *klass, void *data)
+{
+    VirtIOS390DeviceClass *k = VIRTIO_S390_DEVICE_CLASS(klass);
+
+    k->init = s390_virtio_rng_init;
+}
+
+static TypeInfo s390_virtio_rng = {
+    .name          = "virtio-rng-s390",
+    .parent        = TYPE_VIRTIO_S390_DEVICE,
+    .instance_size = sizeof(VirtIOS390Device),
+    .instance_init = s390_virtio_rng_initfn,
+    .class_init    = s390_virtio_rng_class_init,
+};
+
 static int s390_virtio_busdev_init(DeviceState *dev)
 {
     VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
@@ -528,6 +564,7 @@ static void s390_virtio_register_types(void)
     type_register_static(&s390_virtio_blk);
     type_register_static(&s390_virtio_net);
     type_register_static(&s390_virtio_scsi);
+    type_register_static(&s390_virtio_rng);
     type_register_static(&s390_virtio_bridge_info);
 }
 
hw/s390-virtio-bus.h +2 -0
index 4873134..a83afe7 100644
@@ -19,6 +19,7 @@
 
 #include "virtio-blk.h"
 #include "virtio-net.h"
+#include "virtio-rng.h"
 #include "virtio-serial.h"
 #include "virtio-scsi.h"
 
@@ -75,6 +76,7 @@ struct VirtIOS390Device {
     virtio_serial_conf serial;
     virtio_net_conf net;
     VirtIOSCSIConf scsi;
+    VirtIORNGConf rng;
 };
 
 typedef struct VirtIOS390Bus {
hw/virtio-pci.c +60 -0

78 lines not on this page · raw message

hw/virtio-pci.h +2 -0
index ac9d522..b58d9a2 100644
@@ -17,6 +17,7 @@
 
 #include "virtio-blk.h"
 #include "virtio-net.h"
+#include "virtio-rng.h"
 #include "virtio-serial.h"
 #include "virtio-scsi.h"
 
@@ -46,6 +47,7 @@ typedef struct {
     virtio_serial_conf serial;
     virtio_net_conf net;
     VirtIOSCSIConf scsi;
+    VirtIORNGConf rng;
     bool ioeventfd_disabled;
     bool ioeventfd_started;
     VirtIOIRQFD *vector_irqfd;
hw/virtio-rng.c new file +211 -0

211 lines not on this page · raw message

hw/virtio-rng.h new file +24 -0
new file mode 100644
index 0000000..fbb0104
@@ -0,0 +1,24 @@
+/*
+ * Virtio RNG Support
+ *
+ * Copyright Red Hat, Inc. 2012
+ * Copyright Amit Shah <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * (at your option) any later version.  See the COPYING file in the
+ * top-level directory.
+ */
+
+#ifndef _QEMU_VIRTIO_RNG_H
+#define _QEMU_VIRTIO_RNG_H
+
+#include "qemu/rng.h"
+
+/* The Virtio ID for the virtio rng device */
+#define VIRTIO_ID_RNG    4
+
+struct VirtIORNGConf {
+    RngBackend *rng;
+};
+
+#endif
hw/virtio.h +3 -0
index ac482be..df8d0f7 100644
@@ -203,6 +203,8 @@ VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *serial);
 VirtIODevice *virtio_balloon_init(DeviceState *dev);
 typedef struct VirtIOSCSIConf VirtIOSCSIConf;
 VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *conf);
+typedef struct VirtIORNGConf VirtIORNGConf;
+VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf);
 #ifdef CONFIG_LINUX
 VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf);
 #endif
@@ -213,6 +215,7 @@ void virtio_blk_exit(VirtIODevice *vdev);
 void virtio_serial_exit(VirtIODevice *vdev);
 void virtio_balloon_exit(VirtIODevice *vdev);
 void virtio_scsi_exit(VirtIODevice *vdev);
+void virtio_rng_exit(VirtIODevice *vdev);
 
 #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
 	DEFINE_PROP_BIT("indirect_desc", _state, _field, \
Signature · 1 line
1.8.0
#8 lore raw

PATCH 7/8 virtio-rng: add rate limiting support

In reply to Anthony Liguori

This adds parameters to virtio-rng-pci to allow rate limiting the entropy a guest receives. An example command line:

$ qemu -device virtio-rng-pci,max-bytes=1024,period=1000

Would limit entropy collection to 1Kb/s.

---
 hw/virtio-pci.c |  7 +++++++
 hw/virtio-rng.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++--------
 hw/virtio-rng.h |  2 ++
 3 files changed, 64 insertions(+), 8 deletions(-)
hw/virtio-pci.c +7 -0
index a3d4777..f90296d 100644
@@ -1015,6 +1015,13 @@ static void virtio_rng_initfn(Object *obj)
 
 static Property virtio_rng_properties[] = {
     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
+    /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s.  If
+       you have an entropy source capable of generating more entropy than this
+       and you can pass it through via virtio-rng, then hats off to you.  Until
+       then, this is unlimited for all practical purposes.
+    */
+    DEFINE_PROP_UINT64("max-bytes", VirtIOPCIProxy, rng.max_bytes, INT64_MAX),
+    DEFINE_PROP_UINT32("period", VirtIOPCIProxy, rng.period_ms, 1 << 16),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/virtio-rng.c +55 -8

111 lines not on this page · raw message

hw/virtio-rng.h +2 -0
index fbb0104..7324d0a 100644
@@ -19,6 +19,8 @@
 
 struct VirtIORNGConf {
     RngBackend *rng;
+    uint64_t max_bytes;
+    uint32_t period_ms;
 };
 
 #endif
Signature · 1 line
1.8.0
#9 lore raw

PATCH 8/8 virtio-rng-pci: create a default backend if none exists

In reply to Anthony Liguori

This allows you to specify:

  $ qemu -device virtio-rng-pci

And things will Just Work with a reasonable default.

---
 hw/virtio-pci.c | 13 +++++++++++++
 hw/virtio-rng.h |  2 ++
 2 files changed, 15 insertions(+)
hw/virtio-pci.c +13 -0
index f90296d..71f4fb5 100644
@@ -857,6 +857,19 @@ static int virtio_rng_init_pci(PCIDevice *pci_dev)
     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
     VirtIODevice *vdev;
 
+    if (proxy->rng.rng == NULL) {
+        proxy->rng.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
+
+        object_property_add_child(OBJECT(pci_dev),
+                                  "default-backend",
+                                  OBJECT(proxy->rng.default_backend),
+                                  NULL);
+
+        object_property_set_link(OBJECT(pci_dev),
+                                 OBJECT(proxy->rng.default_backend),
+                                 "rng", NULL);
+    }
+
     vdev = virtio_rng_init(&pci_dev->qdev, &proxy->rng);
     if (!vdev) {
         return -1;
hw/virtio-rng.h +2 -0
index 7324d0a..f42d748 100644
@@ -13,6 +13,7 @@
 #define _QEMU_VIRTIO_RNG_H
 
 #include "qemu/rng.h"
+#include "qemu/rng-random.h"
 
 /* The Virtio ID for the virtio rng device */
 #define VIRTIO_ID_RNG    4
@@ -21,6 +22,7 @@ struct VirtIORNGConf {
     RngBackend *rng;
     uint64_t max_bytes;
     uint32_t period_ms;
+    RndRandom *default_backend;
 };
 
 #endif
Signature · 1 line
1.8.0
#10 lore raw
In reply to Anthony Liguori
Quoted 11 lines · Anthony Liguori

My take away from all of the various discussions on what the Right Way to use virtio-rng is:

1) /dev/random should always be used as the entropy source (I've left it configurable though)

2) I think the Right Way to configure virtio-rng is to figure out what the available entropy is on the host, and then decide how to allocate that to each guest. As such, I've implemented rate limiting.

Quoted 6 lines

I think QEMU is the right place to do this because this is a property of specific virtual machines. I can imagine a cloud provider wanting to guarantee a certain level of entropy for different classes of VMs. Even if rngd could be used to do this, configuring it differently for different guests would be cumbersome.

rngd is not where this should happen, it should be in the /dev/random implementation in the (host) kernel. That way it is applicable to all types of clients, not just Qemu.

Quoted 3 lines

3) qemu -device virtio-rng-pci will Just Work but risks exhausting host entropy. This means we can't make it the default for machines. But for most command line users, I think this is the behavior they want.

It's a bit unfortunate, but I'm not going to push on that point.

Given the migration issue I'll write up an implementation of a DRNG (RDRAND/RDSEED) backend once this is upstream. If RDRAND is disabled in the guest, but available in the host, this would be the one to use. If RDRAND is available in the guest it should be used directly if rngd is new enough, but since virtio-rng has been in the kernel since 2008 there still might be some guests which could use such an implementation without having been RDRAND-enabled.

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
#11 lore raw
Quoted 19 lines · H. Peter Anvin
Quoted 11 lines · Anthony Liguori

My take away from all of the various discussions on what the Right Way to use virtio-rng is:

1) /dev/random should always be used as the entropy source (I've left it configurable though)

2) I think the Right Way to configure virtio-rng is to figure out what the available entropy is on the host, and then decide how to allocate that to each guest. As such, I've implemented rate limiting.

Quoted 6 lines

I think QEMU is the right place to do this because this is a property of specific virtual machines. I can imagine a cloud provider wanting to guarantee a certain level of entropy for different classes of VMs. Even if rngd could be used to do this, configuring it differently for different guests would be cumbersome.

Quoted 3 lines

rngd is not where this should happen, it should be in the /dev/random implementation in the (host) kernel. That way it is applicable to all types of clients, not just Qemu.

Even so, we still should do rate limiting in QEMU.

If you had a way to do this in the kernel (say, through cgroups), then this limits both QEMU and the guest. QEMU does do some encryption for things like TLS with VNC. This shouldn't affect the amount of entropy available for the guest though.

So I can certainly understanding setting a limit on the QEMU process beyond having QEMU limit how much entropy the guest receives. The two mechanisms are complimentary.

Quoted 7 lines
Quoted 4 lines

3) qemu -device virtio-rng-pci will Just Work but risks exhausting host entropy. This means we can't make it the default for machines. But for most command line users, I think this is the behavior they want.

It's a bit unfortunate, but I'm not going to push on that point.

Quoted 7 lines

Given the migration issue I'll write up an implementation of a DRNG (RDRAND/RDSEED) backend once this is upstream. If RDRAND is disabled in the guest, but available in the host, this would be the one to use. If RDRAND is available in the guest it should be used directly if rngd is new enough, but since virtio-rng has been in the kernel since 2008 there still might be some guests which could use such an implementation without having been RDRAND-enabled.

Sounds like a good idea.

Regards,

Anthony Liguori

Quoted 7 lines

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
#12 lore raw
In reply to H. Peter Anvin
Quoted 29 lines · H. Peter Anvin
Quoted 11 lines · Anthony Liguori

My take away from all of the various discussions on what the Right Way to use virtio-rng is:

1) /dev/random should always be used as the entropy source (I've left it configurable though)

2) I think the Right Way to configure virtio-rng is to figure out what the available entropy is on the host, and then decide how to allocate that to each guest. As such, I've implemented rate limiting.

Quoted 6 lines

I think QEMU is the right place to do this because this is a property of specific virtual machines. I can imagine a cloud provider wanting to guarantee a certain level of entropy for different classes of VMs. Even if rngd could be used to do this, configuring it differently for different guests would be cumbersome.

rngd is not where this should happen, it should be in the /dev/random implementation in the (host) kernel. That way it is applicable to all types of clients, not just Qemu.

Quoted 3 lines

3) qemu -device virtio-rng-pci will Just Work but risks exhausting host entropy. This means we can't make it the default for machines. But for most command line users, I think this is the behavior they want.

It's a bit unfortunate, but I'm not going to push on that point.

Quoted 8 lines

Given the migration issue I'll write up an implementation of a DRNG (RDRAND/RDSEED) backend once this is upstream. If RDRAND is disabled in the guest, but available in the host, this would be the one to use. If RDRAND is available in the guest it should be used directly if rngd is new enough, but since virtio-rng has been in the kernel since 2008 there still might be some guests which could use such an implementation without having been RDRAND-enabled.

That is also a good idea for emulation, and especially to provide non-x86 guests with entropy.

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net