Skip to contentSkip to conversation

virtio: Add vhost-user based RNG

PATCH Superseded Mathieu Poirier 3 participants 15 messages Jul 10, 2021 – Jul 23, 2021 Arm/Amlogic Meson SoC support

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover virtio: Add vhost-user based RNG

This sets adds a vhost-user based random number generator (RNG), similar to what has been done for i2c and virtiofsd, with the implementation following the patterns already set forth in those.

Applies cleanly to git://git.qemu.org/qemu.git master(05de778b5b8a).

Thanks,
Mathieu

Mathieu Poirier (4):
vhost-user-rng: Add vhost-user-rng implementation
vhost-user-rng-pci: Add vhost-user-rng-pci implementation
vhost-user-rng: backend: Add RNG vhost-user daemon implementation
docs: Add documentation for vhost based RNG implementation

----
New for V3:
- Rebased to latest master branch.
- Fixed documentation warning.
- Updated call to vhost_dev_init() to match new signature.
- Dropped MAINTAINERS patch since it was already applied.

 docs/tools/index.rst                     |   1 +
 docs/tools/vhost-user-rng.rst            |  74 +++++
 hw/virtio/Kconfig                        |   5 +
 hw/virtio/meson.build                    |   2 +
 hw/virtio/vhost-user-rng-pci.c           |  79 +++++
 hw/virtio/vhost-user-rng.c               | 294 +++++++++++++++++
 include/hw/virtio/vhost-user-rng.h       |  33 ++
 tools/meson.build                        |   8 +
 tools/vhost-user-rng/50-qemu-rng.json.in |   5 +
 tools/vhost-user-rng/main.c              | 403 +++++++++++++++++++++++
 tools/vhost-user-rng/meson.build         |  10 +
 11 files changed, 914 insertions(+)
 create mode 100644 docs/tools/vhost-user-rng.rst
 create mode 100644 hw/virtio/vhost-user-rng-pci.c
 create mode 100644 hw/virtio/vhost-user-rng.c
 create mode 100644 include/hw/virtio/vhost-user-rng.h
 create mode 100644 tools/vhost-user-rng/50-qemu-rng.json.in
 create mode 100644 tools/vhost-user-rng/main.c
 create mode 100644 tools/vhost-user-rng/meson.build
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/4 vhost-user-rng: Add vhost-user-rng implementation

Following in the footsteps of what whas done for vhost-user-i2c and virtiofsd, introduce a random number generator (RNG) backend that communicates with a vhost-user server to retrieve entropy. That way another VMM could be using the same vhost-user daemon and avoid having to write yet another RNG driver.

---
 hw/virtio/Kconfig                  |   5 +
 hw/virtio/meson.build              |   1 +
 hw/virtio/vhost-user-rng.c         | 294 +++++++++++++++++++++++++++++
 include/hw/virtio/vhost-user-rng.h |  33 ++++
 4 files changed, 333 insertions(+)
 create mode 100644 hw/virtio/vhost-user-rng.c
 create mode 100644 include/hw/virtio/vhost-user-rng.h
hw/virtio/Kconfig +5 -0
index 0eda25c4e1bf..69066ab14e6d 100644
@@ -58,3 +58,8 @@ config VIRTIO_MEM
     depends on LINUX
     depends on VIRTIO_MEM_SUPPORTED
     select MEM_DEVICE
+
+config VHOST_USER_RNG
+    bool
+    default y
+    depends on VIRTIO && VHOST_USER
hw/virtio/meson.build +1 -0
index fbff9bc9d4de..e386791f2a05 100644
@@ -25,6 +25,7 @@ virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock.
 virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c'))
 virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c'))
 virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c'))
+virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: files('vhost-user-rng.c'))
 
 virtio_pci_ss = ss.source_set()
 virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-pci.c'))
hw/virtio/vhost-user-rng.c new file +294 -0

294 lines not on this page · raw message

include/hw/virtio/vhost-user-rng.h new file +33 -0
new file mode 100644
index 000000000000..071539996d1d
@@ -0,0 +1,33 @@
+/*
+ * Vhost-user RNG virtio device
+ *
+ * Copyright (c) 2021 Mathieu Poirier <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef _QEMU_VHOST_USER_RNG_H
+#define _QEMU_VHOST_USER_RNG_H
+
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/vhost.h"
+#include "hw/virtio/vhost-user.h"
+#include "chardev/char-fe.h"
+
+#define TYPE_VHOST_USER_RNG "vhost-user-rng"
+OBJECT_DECLARE_SIMPLE_TYPE(VHostUserRNG, VHOST_USER_RNG)
+
+struct VHostUserRNG {
+    /*< private >*/
+    VirtIODevice parent;
+    CharBackend chardev;
+    struct vhost_virtqueue *vhost_vq;
+    struct vhost_dev vhost_dev;
+    VhostUserState vhost_user;
+    VirtQueue *req_vq;
+    bool connected;
+
+    /*< public >*/
+};
+
+#endif /* _QEMU_VHOST_USER_RNG_H */
Signature · 1 line
2.25.1
Reviewed-by on v3 1/4
#2 lore raw

Re: [PATCH v3 1/4] vhost-user-rng: Add vhost-user-rng implementation

Quoted 10 lines · Mathieu Poirier

Following in the footsteps of what whas done for vhost-user-i2c and virtiofsd, introduce a random number generator (RNG) backend that communicates with a vhost-user server to retrieve entropy. That way another VMM could be using the same vhost-user daemon and avoid having to write yet another RNG driver.

---
 hw/virtio/Kconfig                  |   5 +
 hw/virtio/meson.build              |   1 +

FWIW there are simple merge failures for the meson and Kconfig parts due to I2C being merged.

<snip>

hw/virtio/vhost-user-rng.c line 219

Quoted 7 lines
hw/virtio/vhost-user-rng.c new file +7 -0
+
+    rng->vhost_dev.nvqs = 1;
+    rng->vhost_dev.vqs = g_new0(struct vhost_virtqueue, rng->vhost_dev.nvqs);
+    if (!rng->vhost_dev.vqs) {
+        error_setg_errno(errp, -1, "memory allocation failed");
+        goto vhost_dev_init_failed;
+    }

g_new0 will abort on memory allocation failure (which is fine for userspace ;-) so you don't need the check and erro handling exit here.

Quoted 104 lines
hw/virtio/vhost-user-rng.c new file +75 -0
+
+    ret = vhost_dev_init(&rng->vhost_dev, &rng->vhost_user,
+                         VHOST_BACKEND_TYPE_USER, 0, errp);
+    if (ret < 0) {
+        error_setg_errno(errp, -ret, "vhost_dev_init() failed");
+        goto vhost_dev_init_failed;
+    }
+
+    qemu_chr_fe_set_handlers(&rng->chardev, NULL, NULL, vu_rng_event, NULL,
+                             dev, NULL, true);
+
+    return;
+
+vhost_dev_init_failed:
+    virtio_delete_queue(rng->req_vq);
+virtio_add_queue_failed:
+    virtio_cleanup(vdev);
+    vhost_user_cleanup(&rng->vhost_user);
+}
+
+static void vu_rng_device_unrealize(DeviceState *dev)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+    VHostUserRNG *rng = VHOST_USER_RNG(dev);
+
+    vu_rng_set_status(vdev, 0);
+
+    vhost_dev_cleanup(&rng->vhost_dev);
+    g_free(rng->vhost_dev.vqs);
+    rng->vhost_dev.vqs = NULL;
+    virtio_delete_queue(rng->req_vq);
+    virtio_cleanup(vdev);
+    vhost_user_cleanup(&rng->vhost_user);
+}
+
+static const VMStateDescription vu_rng_vmstate = {
+    .name = "vhost-user-rng",
+    .unmigratable = 1,
+};
+
+static Property vu_rng_properties[] = {
+    DEFINE_PROP_CHR("chardev", VHostUserRNG, chardev),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void vu_rng_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
+
+    device_class_set_props(dc, vu_rng_properties);
+    dc->vmsd = &vu_rng_vmstate;
+    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+
+    vdc->realize = vu_rng_device_realize;
+    vdc->unrealize = vu_rng_device_unrealize;
+    vdc->get_features = vu_rng_get_features;
+    vdc->set_status = vu_rng_set_status;
+    vdc->guest_notifier_mask = vu_rng_guest_notifier_mask;
+    vdc->guest_notifier_pending = vu_rng_guest_notifier_pending;
+}
+
+static const TypeInfo vu_rng_info = {
+    .name = TYPE_VHOST_USER_RNG,
+    .parent = TYPE_VIRTIO_DEVICE,
+    .instance_size = sizeof(VHostUserRNG),
+    .class_init = vu_rng_class_init,
+};
+
+static void vu_rng_register_types(void)
+{
+    type_register_static(&vu_rng_info);
+}
+
+type_init(vu_rng_register_types)
include/hw/virtio/vhost-user-rng.h new file +23 -0
new file mode 100644
index 000000000000..071539996d1d
@@ -0,0 +1,33 @@
+/*
+ * Vhost-user RNG virtio device
+ *
+ * Copyright (c) 2021 Mathieu Poirier <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef _QEMU_VHOST_USER_RNG_H
+#define _QEMU_VHOST_USER_RNG_H
+
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/vhost.h"
+#include "hw/virtio/vhost-user.h"
+#include "chardev/char-fe.h"
+
+#define TYPE_VHOST_USER_RNG "vhost-user-rng"
+OBJECT_DECLARE_SIMPLE_TYPE(VHostUserRNG, VHOST_USER_RNG)
+
+struct VHostUserRNG {
+    /*< private >*/
+    VirtIODevice parent;
+    CharBackend chardev;

include/hw/virtio/vhost-user-rng.h line 33

Quoted 10 lines
include/hw/virtio/vhost-user-rng.h new file +10 -0
+    struct vhost_virtqueue *vhost_vq;
+    struct vhost_dev vhost_dev;
+    VhostUserState vhost_user;
+    VirtQueue *req_vq;
+    bool connected;
+
+    /*< public >*/
+};
+
+#endif /* _QEMU_VHOST_USER_RNG_H */

Otherwise:

Signature · 1 line
Alex Bennée
#3 lore raw

Re: [PATCH v3 1/4] vhost-user-rng: Add vhost-user-rng implementation

Hi Alex,

Quoted 9 lines · Alex Bennée
Quoted 6 lines · Mathieu Poirier

Following in the footsteps of what whas done for vhost-user-i2c and virtiofsd, introduce a random number generator (RNG) backend that communicates with a vhost-user server to retrieve entropy. That way another VMM could be using the same vhost-user daemon and avoid having to write yet another RNG driver.

Quoted 8 lines
Quoted 4 lines
---
 hw/virtio/Kconfig                  |   5 +
 hw/virtio/meson.build              |   1 +

FWIW there are simple merge failures for the meson and Kconfig parts due to I2C being merged.

Right, merging I2C before this set will definitely break things.

Quoted 9 lines

<snip>

hw/virtio/vhost-user-rng.c line 219

Quoted 7 lines
hw/virtio/vhost-user-rng.c new file +7 -0
+
+    rng->vhost_dev.nvqs = 1;
+    rng->vhost_dev.vqs = g_new0(struct vhost_virtqueue, rng->vhost_dev.nvqs);
+    if (!rng->vhost_dev.vqs) {
+        error_setg_errno(errp, -1, "memory allocation failed");
+        goto vhost_dev_init_failed;
+    }
Quoted 3 lines

g_new0 will abort on memory allocation failure (which is fine for userspace ;-) so you don't need the check and erro handling exit here.

Ok, I'll fix this.

Quoted 117 lines
Quoted 104 lines
hw/virtio/vhost-user-rng.c new file +75 -0
+
+    ret = vhost_dev_init(&rng->vhost_dev, &rng->vhost_user,
+                         VHOST_BACKEND_TYPE_USER, 0, errp);
+    if (ret < 0) {
+        error_setg_errno(errp, -ret, "vhost_dev_init() failed");
+        goto vhost_dev_init_failed;
+    }
+
+    qemu_chr_fe_set_handlers(&rng->chardev, NULL, NULL, vu_rng_event, NULL,
+                             dev, NULL, true);
+
+    return;
+
+vhost_dev_init_failed:
+    virtio_delete_queue(rng->req_vq);
+virtio_add_queue_failed:
+    virtio_cleanup(vdev);
+    vhost_user_cleanup(&rng->vhost_user);
+}
+
+static void vu_rng_device_unrealize(DeviceState *dev)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+    VHostUserRNG *rng = VHOST_USER_RNG(dev);
+
+    vu_rng_set_status(vdev, 0);
+
+    vhost_dev_cleanup(&rng->vhost_dev);
+    g_free(rng->vhost_dev.vqs);
+    rng->vhost_dev.vqs = NULL;
+    virtio_delete_queue(rng->req_vq);
+    virtio_cleanup(vdev);
+    vhost_user_cleanup(&rng->vhost_user);
+}
+
+static const VMStateDescription vu_rng_vmstate = {
+    .name = "vhost-user-rng",
+    .unmigratable = 1,
+};
+
+static Property vu_rng_properties[] = {
+    DEFINE_PROP_CHR("chardev", VHostUserRNG, chardev),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void vu_rng_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
+
+    device_class_set_props(dc, vu_rng_properties);
+    dc->vmsd = &vu_rng_vmstate;
+    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+
+    vdc->realize = vu_rng_device_realize;
+    vdc->unrealize = vu_rng_device_unrealize;
+    vdc->get_features = vu_rng_get_features;
+    vdc->set_status = vu_rng_set_status;
+    vdc->guest_notifier_mask = vu_rng_guest_notifier_mask;
+    vdc->guest_notifier_pending = vu_rng_guest_notifier_pending;
+}
+
+static const TypeInfo vu_rng_info = {
+    .name = TYPE_VHOST_USER_RNG,
+    .parent = TYPE_VIRTIO_DEVICE,
+    .instance_size = sizeof(VHostUserRNG),
+    .class_init = vu_rng_class_init,
+};
+
+static void vu_rng_register_types(void)
+{
+    type_register_static(&vu_rng_info);
+}
+
+type_init(vu_rng_register_types)
include/hw/virtio/vhost-user-rng.h new file +23 -0
new file mode 100644
index 000000000000..071539996d1d
@@ -0,0 +1,33 @@
+/*
+ * Vhost-user RNG virtio device
+ *
+ * Copyright (c) 2021 Mathieu Poirier <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef _QEMU_VHOST_USER_RNG_H
+#define _QEMU_VHOST_USER_RNG_H
+
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/vhost.h"
+#include "hw/virtio/vhost-user.h"
+#include "chardev/char-fe.h"
+
+#define TYPE_VHOST_USER_RNG "vhost-user-rng"
+OBJECT_DECLARE_SIMPLE_TYPE(VHostUserRNG, VHOST_USER_RNG)
+
+struct VHostUserRNG {
+    /*< private >*/
+    VirtIODevice parent;
+    CharBackend chardev;

include/hw/virtio/vhost-user-rng.h line 33

Quoted 10 lines
include/hw/virtio/vhost-user-rng.h new file +10 -0
+    struct vhost_virtqueue *vhost_vq;
+    struct vhost_dev vhost_dev;
+    VhostUserState vhost_user;
+    VirtQueue *req_vq;
+    bool connected;
+
+    /*< public >*/
+};
+
+#endif /* _QEMU_VHOST_USER_RNG_H */

Otherwise:

Quoted 1 line

Thanks for taking the time to review this set.

Quoted 3 lines
Signature · 2 lines
-- 
Alex Bennée
#4 lore raw

PATCH v3 2/4 vhost-user-rng-pci: Add vhost-user-rng-pci implementation

In reply to Mathieu Poirier

This patch provides a PCI bus interface to the vhost-user-rng backed. The implentation is similar to what was done for vhost-user-i2c-pci and vhost-user-fs-pci.

---
 hw/virtio/meson.build          |  1 +
 hw/virtio/vhost-user-rng-pci.c | 79 ++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
 create mode 100644 hw/virtio/vhost-user-rng-pci.c
hw/virtio/meson.build +1 -0
index e386791f2a05..1430b370e64d 100644
@@ -26,6 +26,7 @@ virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c'))
 virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c'))
 virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c'))
 virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: files('vhost-user-rng.c'))
+virtio_ss.add(when: ['CONFIG_VHOST_USER_RNG', 'CONFIG_VIRTIO_PCI'], if_true: files('vhost-user-rng-pci.c'))
 
 virtio_pci_ss = ss.source_set()
 virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-pci.c'))
hw/virtio/vhost-user-rng-pci.c new file +79 -0

79 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on v3 2/4
#5 lore raw

Re: [PATCH v3 2/4] vhost-user-rng-pci: Add vhost-user-rng-pci implementation

Quoted 5 lines · Mathieu Poirier

This patch provides a PCI bus interface to the vhost-user-rng backed. The implentation is similar to what was done for vhost-user-i2c-pci and vhost-user-fs-pci.

<snip>

hw/virtio/meson.build virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c')) · line 30

Quoted 10 lines
hw/virtio/meson.build +1 -0
index e386791f2a05..1430b370e64d 100644
@@ -26,6 +26,7 @@ virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c'))
 virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c'))
 virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c'))
 virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: files('vhost-user-rng.c'))
+virtio_ss.add(when: ['CONFIG_VHOST_USER_RNG', 'CONFIG_VIRTIO_PCI'], if_true: files('vhost-user-rng-pci.c'))
 

Another minor merge conflict here to be fixed on rebase. Otherwise:

Signature · 1 line
Alex Bennée
#6 lore raw

Re: [PATCH v3 2/4] vhost-user-rng-pci: Add vhost-user-rng-pci implementation

In reply to Mathieu Poirier
Quoted 6 lines · Mathieu Poirier

This patch provides a PCI bus interface to the vhost-user-rng backed. The implentation is similar to what was done for vhost-user-i2c-pci and vhost-user-fs-pci.

---

<snip>

hw/virtio/vhost-user-rng-pci.c line 55

Quoted 12 lines
hw/virtio/vhost-user-rng-pci.c new file +12 -0
+static void vhost_user_rng_pci_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
+    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
+    k->realize = vhost_user_rng_pci_realize;
+    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+    device_class_set_props(dc, vhost_user_rng_pci_properties);
+    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
+    pcidev_k->device_id = 0; /* Set by virtio-pci based on virtio id */
+    pcidev_k->revision = 0x00;
+    pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;

I noticed the other RNGs use:

pcidev_k->class_id = PCI_CLASS_OTHERS;

Signature · 1 line
Alex Bennée
#7 lore raw

PATCH v3 3/4 vhost-user-rng: backend: Add RNG vhost-user daemon implementation

In reply to Mathieu Poirier

This patch provides the vhost-user backend implementation to work in tandem with the vhost-user-rng implementation of the QEMU VMM.

It uses the vhost-user API so that other VMM can re-use the interface without having to write the driver again.

---
 tools/meson.build                        |   8 +
 tools/vhost-user-rng/50-qemu-rng.json.in |   5 +
 tools/vhost-user-rng/main.c              | 403 +++++++++++++++++++++++
 tools/vhost-user-rng/meson.build         |  10 +
 4 files changed, 426 insertions(+)
 create mode 100644 tools/vhost-user-rng/50-qemu-rng.json.in
 create mode 100644 tools/vhost-user-rng/main.c
 create mode 100644 tools/vhost-user-rng/meson.build
tools/meson.build +8 -0
index 3e5a0abfa29f..66b0a11fbb45 100644
@@ -24,3 +24,11 @@ endif
 if have_virtiofsd
   subdir('virtiofsd')
 endif
+
+have_virtiorng = (have_system and
+    have_tools and
+    'CONFIG_LINUX' in config_host)
+
+if have_virtiorng
+  subdir('vhost-user-rng')
+endif
tools/vhost-user-rng/50-qemu-rng.json.in new file +5 -0
new file mode 100644
index 000000000000..9186c3c6fe1d
@@ -0,0 +1,5 @@
+{
+  "description": "QEMU vhost-user-rng",
+  "type": "bridge",
+  "binary": "@libexecdir@/vhost-user-rng"
+}
tools/vhost-user-rng/main.c new file +403 -0

403 lines not on this page · raw message

tools/vhost-user-rng/meson.build new file +10 -0
new file mode 100644
index 000000000000..4dc386daf335
@@ -0,0 +1,10 @@
+executable('vhost-user-rng', files(
+  'main.c'),
+  dependencies: [qemuutil, glib, gio, rt],
+  install: true,
+  install_dir: get_option('libexecdir'))
+
+configure_file(input: '50-qemu-rng.json.in',
+               output: '50-qemu-rng.json',
+               configuration: config_host,
+               install_dir: qemu_datadir / 'vhost-user')
Signature · 1 line
2.25.1
#8 lore raw

Re: [PATCH v3 3/4] vhost-user-rng: backend: Add RNG vhost-user daemon implementation

Quoted 88 lines · Mathieu Poirier

This patch provides the vhost-user backend implementation to work in tandem with the vhost-user-rng implementation of the QEMU VMM.

It uses the vhost-user API so that other VMM can re-use the interface without having to write the driver again.

---
 tools/meson.build                        |   8 +
 tools/vhost-user-rng/50-qemu-rng.json.in |   5 +
 tools/vhost-user-rng/main.c              | 403 +++++++++++++++++++++++
 tools/vhost-user-rng/meson.build         |  10 +
 4 files changed, 426 insertions(+)
 create mode 100644 tools/vhost-user-rng/50-qemu-rng.json.in
 create mode 100644 tools/vhost-user-rng/main.c
 create mode 100644 tools/vhost-user-rng/meson.build
tools/meson.build +8 -0
index 3e5a0abfa29f..66b0a11fbb45 100644
@@ -24,3 +24,11 @@ endif
 if have_virtiofsd
   subdir('virtiofsd')
 endif
+
+have_virtiorng = (have_system and
+    have_tools and
+    'CONFIG_LINUX' in config_host)
+
+if have_virtiorng
+  subdir('vhost-user-rng')
+endif
tools/vhost-user-rng/50-qemu-rng.json.in new file +5 -0
new file mode 100644
index 000000000000..9186c3c6fe1d
@@ -0,0 +1,5 @@
+{
+  "description": "QEMU vhost-user-rng",
+  "type": "bridge",
+  "binary": "@libexecdir@/vhost-user-rng"
+}
tools/vhost-user-rng/main.c new file +38 -0
new file mode 100644
index 000000000000..c3b8f6922757
@@ -0,0 +1,403 @@
+/*
+ * VIRTIO RNG Emulation via vhost-user
+ *
+ * Copyright (c) 2021 Mathieu Poirier <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define G_LOG_DOMAIN "vhost-user-rng"
+#define G_LOG_USE_STRUCTURED 1
+
+#include <glib.h>
+#include <gio/gio.h>
+#include <gio/gunixsocketaddress.h>
+#include <glib-unix.h>
+#include <glib/gstdio.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include <inttypes.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <time.h>
+#include <unistd.h>
+#include <endian.h>
+#include <assert.h>
+
+#include "qemu/cutils.h"
+#include "subprojects/libvhost-user/libvhost-user-glib.h"
+#include "subprojects/libvhost-user/libvhost-user.h"
+
+#ifndef container_of
+#define container_of(ptr, type, member) ({                      \

tools/vhost-user-rng/main.c line 48

Quoted 10 lines
tools/vhost-user-rng/main.c new file +10 -0
+        const typeof(((type *) 0)->member) * __mptr = (ptr);     \
+        (type *) ((char *) __mptr - offsetof(type, member)); })
+#endif
+
+typedef struct {
+    VugDev dev;
+    struct itimerspec ts;
+    timer_t rate_limit_timer;
+    pthread_mutex_t rng_mutex;
+    pthread_cond_t rng_cond;

I'm confused by the need for a mutex in a single-threaded application.

Quoted 53 lines
tools/vhost-user-rng/main.c new file +53 -0
+    int64_t quota_remaining;
+    bool activate_timer;
+    GMainLoop *loop;
+} VuRNG;
+
+static gboolean print_cap, verbose;
+static gchar *source_path, *socket_path;
+static gint source_fd, socket_fd = -1;
+
+/* Defaults tailored on virtio-rng.c */
+static uint32_t period_ms = 1 << 16;
+static uint64_t max_bytes = INT64_MAX;
+
+static void check_rate_limit(union sigval sv)
+{
+    VuRNG *rng = sv.sival_ptr;
+    bool wakeup = false;
+
+    pthread_mutex_lock(&rng->rng_mutex);
+    /*
+     * The timer has expired and the guest has used all available
+     * entropy, which means function vu_rng_handle_request() is waiting
+     * on us.  As such wake it up once we're done here.
+     */
+    if (rng->quota_remaining == 0) {
+        wakeup = true;
+    }
+
+    /*
+     * Reset the entropy available to the guest and tell function
+     * vu_rng_handle_requests() to start the timer before using it.
+     */
+    rng->quota_remaining = max_bytes;
+    rng->activate_timer = true;
+    pthread_mutex_unlock(&rng->rng_mutex);
+
+    if (wakeup) {
+        pthread_cond_signal(&rng->rng_cond);
+    }
+}
+
+static void setup_timer(VuRNG *rng)
+{
+    struct sigevent sev;
+    int ret;
+
+    memset(&rng->ts, 0, sizeof(struct itimerspec));
+    rng->ts.it_value.tv_sec = period_ms / 1000;
+    rng->ts.it_value.tv_nsec = (period_ms % 1000) * 1000000;
+
+    /*
+     * Call function check_rate_limit() as if it was the start of
+     * a new thread when the timer expires.

tools/vhost-user-rng/main.c line 111

Quoted 10 lines
tools/vhost-user-rng/main.c new file +10 -0
+     */
+    sev.sigev_notify = SIGEV_THREAD;
+    sev.sigev_notify_function = check_rate_limit;
+    sev.sigev_value.sival_ptr = rng;
+    /* Needs to be NULL if defaults attributes are to be used. */
+    sev.sigev_notify_attributes = NULL;
+    ret = timer_create(CLOCK_MONOTONIC, &sev, &rng->rate_limit_timer);
+    if (ret < 0) {
+        fprintf(stderr, "timer_create() failed\n");
+    }

Ahh I see why now. I think you could avoid this by using glib's own internal g_timeout_add() function. This would then create a timer which would call it's callback periodically (if it returns true to persist the GSource). As the whole execution is effectively event driven you would avoid the need for locking.

Quoted 40 lines
tools/vhost-user-rng/main.c new file +40 -0
+
+}
+
+
+/* Virtio helpers */
+static uint64_t rng_get_features(VuDev *dev)
+{
+    if (verbose) {
+        g_info("%s: replying", __func__);
+    }
+    return 0;
+}
+
+static void rng_set_features(VuDev *dev, uint64_t features)
+{
+    if (verbose && features) {
+        g_autoptr(GString) s = g_string_new("Requested un-handled feature");
+        g_string_append_printf(s, " 0x%" PRIx64 "", features);
+        g_info("%s: %s", __func__, s->str);
+    }
+}
+
+static void vu_rng_handle_requests(VuDev *dev, int qidx)
+{
+    VuRNG *rng = container_of(dev, VuRNG, dev.parent);
+    VuVirtq *vq = vu_get_queue(dev, qidx);
+    VuVirtqElement *elem;
+    size_t to_read;
+    int len, ret;
+
+    for (;;) {
+        /* Get element in the vhost virtqueue */
+        elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement));
+        if (!elem) {
+            break;
+        }
+
+        /* Get the amount of entropy to read from the vhost server */
+        to_read = elem->in_sg[0].iov_len;
+

tools/vhost-user-rng/main.c line 161

Quoted 10 lines
tools/vhost-user-rng/main.c new file +10 -0
+        pthread_mutex_lock(&rng->rng_mutex);
+
+        /*
+         * We have consumed all entropy available for this time slice.
+         * Wait for the timer (check_rate_limit()) to tell us about the
+         * start of a new time slice.
+         */
+        if (rng->quota_remaining == 0) {
+            pthread_cond_wait(&rng->rng_cond, &rng->rng_mutex);
+        }

Hmm this complicates things. Ideally you wouldn't want to block here on processing the virtqueue. This will end up block the guest. I'll need to think about this.

Signature · 1 line
Alex Bennée
#9 lore raw

Re: [PATCH v3 3/4] vhost-user-rng: backend: Add RNG vhost-user daemon implementation

In reply to Mathieu Poirier
Quoted 7 lines · Mathieu Poirier

This patch provides the vhost-user backend implementation to work in tandem with the vhost-user-rng implementation of the QEMU VMM.

It uses the vhost-user API so that other VMM can re-use the interface without having to write the driver again.

Try the following patch which creates a nested main loop and runs it until the g_timeout_add fires again.

--8<---------------cut here---------------start------------->8--- tools/virtio/vhost-user-rng: avoid mutex by using nested main loop

As we are blocking anyway all we really need to do is run a main loop until the timer fires and the data is consumed.

1 file changed, 30 insertions(+), 76 deletions(-)

tools/vhost-user-rng/main.c | 106 +++++++++++++-------------------------------

modified tools/vhost-user-rng/main.c
@@ -42,13 +42,10 @@

 typedef struct {
     VugDev dev;
-    struct itimerspec ts;
-    timer_t rate_limit_timer;
-    pthread_mutex_t rng_mutex;
-    pthread_cond_t rng_cond;
     int64_t quota_remaining;
-    bool activate_timer;
+    guint timer;
     GMainLoop *loop;

+ GMainLoop *blocked;
} VuRNG;

static gboolean print_cap, verbose;
@@ -59,66 +56,26 @@ static gint source_fd, socket_fd = -1;
static uint32_t period_ms = 1 << 16;
static uint64_t max_bytes = INT64_MAX;

-static void check_rate_limit(union sigval sv) +static gboolean check_rate_limit(gpointer user_data)
{

-    VuRNG *rng = sv.sival_ptr;
-    bool wakeup = false;
+    VuRNG *rng = (VuRNG *) user_data;

- pthread_mutex_lock(&rng->rng_mutex);
- /*

-     * The timer has expired and the guest has used all available
-     * entropy, which means function vu_rng_handle_request() is waiting
-     * on us.  As such wake it up once we're done here.
-     */

- if (rng->quota_remaining == 0) {
- wakeup = true;
+ if (rng->blocked) {
+ g_info("%s: called while blocked", __func__);

+        g_main_loop_quit(rng->blocked);
     }
-
     /*
      * Reset the entropy available to the guest and tell function
      * vu_rng_handle_requests() to start the timer before using it.
      */
     rng->quota_remaining = max_bytes;

- rng->activate_timer = true;
- pthread_mutex_unlock(&rng->rng_mutex);
-
- if (wakeup) {
- pthread_cond_signal(&rng->rng_cond);
- }
-}
-
-static void setup_timer(VuRNG *rng)
-{
- struct sigevent sev;
- int ret;
-

-    memset(&rng->ts, 0, sizeof(struct itimerspec));
-    rng->ts.it_value.tv_sec = period_ms / 1000;
-    rng->ts.it_value.tv_nsec = (period_ms % 1000) * 1000000;
-
-    /*
-     * Call function check_rate_limit() as if it was the start of
-     * a new thread when the timer expires.
-     */
-    sev.sigev_notify = SIGEV_THREAD;
-    sev.sigev_notify_function = check_rate_limit;
-    sev.sigev_value.sival_ptr = rng;
-    /* Needs to be NULL if defaults attributes are to be used. */
-    sev.sigev_notify_attributes = NULL;
-    ret = timer_create(CLOCK_MONOTONIC, &sev, &rng->rate_limit_timer);
-    if (ret < 0) {

- fprintf(stderr, "timer_create() failed\n");
- }
-
+ return true;
}

-
/* Virtio helpers */
static uint64_t rng_get_features(VuDev *dev)
{
- if (verbose) {
- g_info("%s: replying", __func__);
- }

+    g_info("%s: replying", __func__);
     return 0;

}

@@ -137,7 +94,7 @@ static void vu_rng_handle_requests(VuDev *dev, int qidx)

     VuVirtq *vq = vu_get_queue(dev, qidx);
     VuVirtqElement *elem;
     size_t to_read;

- int len, ret;
+ int len;

     for (;;) {
         /* Get element in the vhost virtqueue */
@@ -149,24 +106,21 @@ static void vu_rng_handle_requests(VuDev *dev, int qidx)
         /* Get the amount of entropy to read from the vhost server */
         to_read = elem->in_sg[0].iov_len;

- pthread_mutex_lock(&rng->rng_mutex);
-
/*
* We have consumed all entropy available for this time slice.
* Wait for the timer (check_rate_limit()) to tell us about the
* start of a new time slice.
*/
if (rng->quota_remaining == 0) {
- pthread_cond_wait(&rng->rng_cond, &rng->rng_mutex);
- }
-
- /* Start the timer if the last time slice has expired */
- if (rng->activate_timer == true) {

-            rng->activate_timer = false;
-            ret = timer_settime(rng->rate_limit_timer, 0, &rng->ts, NULL);
-            if (ret < 0) {
-                fprintf(stderr, "timer_settime() failed\n");
-            }
+            g_assert(!rng->blocked);
+            rng->blocked = g_main_loop_new(g_main_loop_get_context(rng->loop), false);
+            g_info("attempting to consume %ld bytes but no quota left (%s)",
+                   to_read,
+                   g_main_loop_is_running(rng->loop) ? "running" : "not running");
+            g_main_loop_run(rng->blocked);
+            g_info("return from blocked loop: %ld", rng->quota_remaining);
+            g_main_loop_unref(rng->blocked);
+            rng->blocked = false;
         }
 
         /* Make sure we don't read more than it's available */

@@ -183,8 +137,6 @@ static void vu_rng_handle_requests(VuDev *dev, int qidx)

         rng->quota_remaining -= len;

- pthread_mutex_unlock(&rng->rng_mutex);
-

         vu_queue_push(dev, vq, elem, len);
         free(elem);
     }
@@ -373,6 +325,7 @@ int main(int argc, char *argv[])
      * can add it's GSource watches.
      */
     rng.loop = g_main_loop_new(NULL, FALSE);

+ rng.blocked = NULL;

     if (!vug_init(&rng.dev, 1, g_socket_get_fd(socket),
                   panic, &vuiface)) {
@@ -380,24 +333,25 @@ int main(int argc, char *argv[])
         exit(EXIT_FAILURE);
     }
-    rng.quota_remaining = max_bytes;
-    rng.activate_timer = true;
-    pthread_mutex_init(&rng.rng_mutex, NULL);
-    pthread_cond_init(&rng.rng_cond, NULL);
-    setup_timer(&rng);
-
     if (verbose) {

- g_info("period_ms: %d tv_sec: %ld tv_nsec: %lu\n",
- period_ms, rng.ts.it_value.tv_sec, rng.ts.it_value.tv_nsec);
+ g_log_set_handler(NULL, G_LOG_LEVEL_MASK, g_log_default_handler, NULL);
+ g_setenv("G_MESSAGES_DEBUG", "all", true);
+ } else {
+ g_log_set_handler(NULL,
+ G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR,

+                          g_log_default_handler, NULL);
     }
+    rng.quota_remaining = max_bytes;
+    rng.timer = g_timeout_add(period_ms, check_rate_limit, &rng);
+    g_info("period_ms: %"PRId32", timer %d\n", period_ms, rng.timer);
+
     g_message("entering main loop, awaiting messages");
     g_main_loop_run(rng.loop);
     g_message("finished main loop, cleaning up");
 
     g_main_loop_unref(rng.loop);
     vug_deinit(&rng.dev);
-    timer_delete(rng.rate_limit_timer);
     close(source_fd);
     unlink(socket_path);

}
--8<---------------cut here---------------end--------------->8---

Signature · 1 line
Alex Bennée
#10 lore raw

Re: [PATCH v3 3/4] vhost-user-rng: backend: Add RNG vhost-user daemon implementation

Quoted 11 lines · Alex Bennée
Quoted 7 lines · Mathieu Poirier

This patch provides the vhost-user backend implementation to work in tandem with the vhost-user-rng implementation of the QEMU VMM.

It uses the vhost-user API so that other VMM can re-use the interface without having to write the driver again.

Quoted 9 lines

Try the following patch which creates a nested main loop and runs it until the g_timeout_add fires again.

--8<---------------cut here---------------start------------->8--- tools/virtio/vhost-user-rng: avoid mutex by using nested main loop

As we are blocking anyway all we really need to do is run a main loop until the timer fires and the data is consumed.

Right, I made the implemenation blocking to be as close as possible to what virtio-rng does.

I took a look at your patch below and it should do the trick. Testing yielded the same results as my solution so this is good. To me the nested loop is a little unorthodox to solve this kind of problem but it has less lines of code and avoids spinning a new thread to deal with the timer.

I'll send another revision.

Thanks for the review,
Mathieu

Quoted 195 lines
1 file changed, 30 insertions(+), 76 deletions(-)

tools/vhost-user-rng/main.c | 106 +++++++++++++-------------------------------

modified tools/vhost-user-rng/main.c

(quoted diff) +30 -77
@@ -42,13 +42,10 @@
 
 typedef struct {
     VugDev dev;
-    struct itimerspec ts;
-    timer_t rate_limit_timer;
-    pthread_mutex_t rng_mutex;
-    pthread_cond_t rng_cond;
     int64_t quota_remaining;
-    bool activate_timer;
+    guint timer;
     GMainLoop *loop;
+    GMainLoop *blocked;
 } VuRNG;
 
 static gboolean print_cap, verbose;
@@ -59,66 +56,26 @@ static gint source_fd, socket_fd = -1;
 static uint32_t period_ms = 1 << 16;
 static uint64_t max_bytes = INT64_MAX;
 
-static void check_rate_limit(union sigval sv)
+static gboolean check_rate_limit(gpointer user_data)
 {
-    VuRNG *rng = sv.sival_ptr;
-    bool wakeup = false;
+    VuRNG *rng = (VuRNG *) user_data;
 
-    pthread_mutex_lock(&rng->rng_mutex);
-    /*
-     * The timer has expired and the guest has used all available
-     * entropy, which means function vu_rng_handle_request() is waiting
-     * on us.  As such wake it up once we're done here.
-     */
-    if (rng->quota_remaining == 0) {
-        wakeup = true;
+    if (rng->blocked) {
+        g_info("%s: called while blocked", __func__);
+        g_main_loop_quit(rng->blocked);
     }
-
     /*
      * Reset the entropy available to the guest and tell function
      * vu_rng_handle_requests() to start the timer before using it.
      */
     rng->quota_remaining = max_bytes;
-    rng->activate_timer = true;
-    pthread_mutex_unlock(&rng->rng_mutex);
-
-    if (wakeup) {
-        pthread_cond_signal(&rng->rng_cond);
-    }
-}
-
-static void setup_timer(VuRNG *rng)
-{
-    struct sigevent sev;
-    int ret;
-
-    memset(&rng->ts, 0, sizeof(struct itimerspec));
-    rng->ts.it_value.tv_sec = period_ms / 1000;
-    rng->ts.it_value.tv_nsec = (period_ms % 1000) * 1000000;
-
-    /*
-     * Call function check_rate_limit() as if it was the start of
-     * a new thread when the timer expires.
-     */
-    sev.sigev_notify = SIGEV_THREAD;
-    sev.sigev_notify_function = check_rate_limit;
-    sev.sigev_value.sival_ptr = rng;
-    /* Needs to be NULL if defaults attributes are to be used. */
-    sev.sigev_notify_attributes = NULL;
-    ret = timer_create(CLOCK_MONOTONIC, &sev, &rng->rate_limit_timer);
-    if (ret < 0) {
-        fprintf(stderr, "timer_create() failed\n");
-    }
-
+    return true;
 }
 
-
 /* Virtio helpers */
 static uint64_t rng_get_features(VuDev *dev)
 {
-    if (verbose) {
-        g_info("%s: replying", __func__);
-    }
+    g_info("%s: replying", __func__);
     return 0;
 }
 
@@ -137,7 +94,7 @@ static void vu_rng_handle_requests(VuDev *dev, int qidx)
     VuVirtq *vq = vu_get_queue(dev, qidx);
     VuVirtqElement *elem;
     size_t to_read;
-    int len, ret;
+    int len;
 
     for (;;) {
         /* Get element in the vhost virtqueue */
@@ -149,24 +106,21 @@ static void vu_rng_handle_requests(VuDev *dev, int qidx)
         /* Get the amount of entropy to read from the vhost server */
         to_read = elem->in_sg[0].iov_len;
 
-        pthread_mutex_lock(&rng->rng_mutex);
-
         /*
          * We have consumed all entropy available for this time slice.
          * Wait for the timer (check_rate_limit()) to tell us about the
          * start of a new time slice.
          */
         if (rng->quota_remaining == 0) {
-            pthread_cond_wait(&rng->rng_cond, &rng->rng_mutex);
-        }
-
-        /* Start the timer if the last time slice has expired */
-        if (rng->activate_timer == true) {
-            rng->activate_timer = false;
-            ret = timer_settime(rng->rate_limit_timer, 0, &rng->ts, NULL);
-            if (ret < 0) {
-                fprintf(stderr, "timer_settime() failed\n");
-            }
+            g_assert(!rng->blocked);
+            rng->blocked = g_main_loop_new(g_main_loop_get_context(rng->loop), false);
+            g_info("attempting to consume %ld bytes but no quota left (%s)",
+                   to_read,
+                   g_main_loop_is_running(rng->loop) ? "running" : "not running");
+            g_main_loop_run(rng->blocked);
+            g_info("return from blocked loop: %ld", rng->quota_remaining);
+            g_main_loop_unref(rng->blocked);
+            rng->blocked = false;
         }
 
         /* Make sure we don't read more than it's available */
@@ -183,8 +137,6 @@ static void vu_rng_handle_requests(VuDev *dev, int qidx)
 
         rng->quota_remaining -= len;
 
-        pthread_mutex_unlock(&rng->rng_mutex);
-
         vu_queue_push(dev, vq, elem, len);
         free(elem);
     }
@@ -373,6 +325,7 @@ int main(int argc, char *argv[])
      * can add it's GSource watches.
      */
     rng.loop = g_main_loop_new(NULL, FALSE);
+    rng.blocked = NULL;
 
     if (!vug_init(&rng.dev, 1, g_socket_get_fd(socket),
                   panic, &vuiface)) {
@@ -380,24 +333,25 @@ int main(int argc, char *argv[])
         exit(EXIT_FAILURE);
     }
 
-    rng.quota_remaining = max_bytes;
-    rng.activate_timer = true;
-    pthread_mutex_init(&rng.rng_mutex, NULL);
-    pthread_cond_init(&rng.rng_cond, NULL);
-    setup_timer(&rng);
-
     if (verbose) {
-        g_info("period_ms: %d tv_sec: %ld tv_nsec: %lu\n",
-               period_ms, rng.ts.it_value.tv_sec, rng.ts.it_value.tv_nsec);
+        g_log_set_handler(NULL, G_LOG_LEVEL_MASK, g_log_default_handler, NULL);
+        g_setenv("G_MESSAGES_DEBUG", "all", true);
+    } else {
+        g_log_set_handler(NULL,
+                          G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR,
+                          g_log_default_handler, NULL);
     }
 
+    rng.quota_remaining = max_bytes;
+    rng.timer = g_timeout_add(period_ms, check_rate_limit, &rng);
+    g_info("period_ms: %"PRId32", timer %d\n", period_ms, rng.timer);
+
     g_message("entering main loop, awaiting messages");
     g_main_loop_run(rng.loop);
     g_message("finished main loop, cleaning up");
 
     g_main_loop_unref(rng.loop);
     vug_deinit(&rng.dev);
-    timer_delete(rng.rate_limit_timer);
     close(source_fd);
     unlink(socket_path);
 }
--8<---------------cut here---------------end--------------->8---
Signature · 2 lines
-- 
Alex Bennée
#11 lore raw

Re: [PATCH v3 3/4] vhost-user-rng: backend: Add RNG vhost-user daemon implementation

Quoted 22 lines · Mathieu Poirier
Quoted 11 lines · Alex Bennée
Quoted 7 lines · Mathieu Poirier

This patch provides the vhost-user backend implementation to work in tandem with the vhost-user-rng implementation of the QEMU VMM.

It uses the vhost-user API so that other VMM can re-use the interface without having to write the driver again.

Quoted 9 lines

Try the following patch which creates a nested main loop and runs it until the g_timeout_add fires again.

--8<---------------cut here---------------start------------->8--- tools/virtio/vhost-user-rng: avoid mutex by using nested main loop

As we are blocking anyway all we really need to do is run a main loop until the timer fires and the data is consumed.

Quoted 7 lines

Right, I made the implemenation blocking to be as close as possible to what virtio-rng does.

I took a look at your patch below and it should do the trick. Testing yielded the same results as my solution so this is good. To me the nested loop is a little unorthodox to solve this kind of problem but it has less lines of code and avoids spinning a new thread to deal with the timer.

It might be worth considering just running a g_main_context_iteration() of the main loop:

https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-main-context-iteration

and then you could avoid the hassle of having a special blocking loop (and having to special case the quit). However you can't be sure the quota has filled up so you need to test on the loop. My hack up (on top of this patch):

--8<---------------cut here---------------start------------->8---
modified tools/vhost-user-rng/main.c

@@ -45,7 +45,6 @@ typedef struct {
     int64_t quota_remaining;
     guint timer;
     GMainLoop *loop;

- GMainLoop *blocked;
} VuRNG;

static gboolean print_cap, verbose;
@@ -59,10 +58,8 @@ static uint64_t max_bytes = INT64_MAX;
static gboolean check_rate_limit(gpointer user_data)

 {
     VuRNG *rng = (VuRNG *) user_data;

-
- if (rng->blocked) {
- g_info("%s: called while blocked", __func__);
- g_main_loop_quit(rng->blocked);
+ if (!rng->quota_remaining) {

+        g_info("%s: replenishing empty quota", __func__);
     }
     /*
      * Reset the entropy available to the guest and tell function
@@ -112,15 +109,12 @@ static void vu_rng_handle_requests(VuDev *dev, int qidx)
          * start of a new time slice.
          */
         if (rng->quota_remaining == 0) {
-            g_assert(!rng->blocked);
-            rng->blocked = g_main_loop_new(g_main_loop_get_context(rng->loop), false);
-            g_info("attempting to consume %ld bytes but no quota left (%s)",
-                   to_read,
-                   g_main_loop_is_running(rng->loop) ? "running" : "not running");
-            g_main_loop_run(rng->blocked);
-            g_info("return from blocked loop: %ld", rng->quota_remaining);
-            g_main_loop_unref(rng->blocked);
-            rng->blocked = false;
+            g_info("blocking on consuming %ld bytes as no quota", to_read);
+            do {
+                g_main_context_iteration(g_main_loop_get_context(rng->loop),
+                                         true);
+                g_info("return from blocked loop: %ld", rng->quota_remaining);
+            } while (!rng->quota_remaining);
         }
 
         /* Make sure we don't read more than it's available */
@@ -325,7 +319,6 @@ int main(int argc, char *argv[])
      * can add it's GSource watches.
      */
     rng.loop = g_main_loop_new(NULL, FALSE);

- rng.blocked = NULL;

     if (!vug_init(&rng.dev, 1, g_socket_get_fd(socket),
                   panic, &vuiface)) {

--8<---------------cut here---------------end--------------->8---

And then with:

10:24 root@buster/aarch64 [~] >dd if=/dev/hwrng of=/dev/null status=progress 77312 bytes (77 kB, 76 KiB) copied, 150 s, 0.5 kB/s

and:

  ./tools/vhost-user-rng/vhost-user-rng --socket-path vrng.sock -v -m 512 -p 1000

I was seeing:

vhost-user-rng-INFO: 10:27:14.453: blocking on consuming 64 bytes as no quota
vhost-user-rng-INFO: 10:27:14.453: return from blocked loop: 0
vhost-user-rng-INFO: 10:27:15.451: check_rate_limit: replenishing empty quota
vhost-user-rng-INFO: 10:27:15.451: return from blocked loop: 512
vhost-user-rng-INFO: 10:27:15.457: blocking on consuming 64 bytes as no quota
vhost-user-rng-INFO: 10:27:15.457: return from blocked loop: 0
vhost-user-rng-INFO: 10:27:16.453: check_rate_limit: replenishing empty quota
vhost-user-rng-INFO: 10:27:16.453: return from blocked loop: 512
vhost-user-rng-INFO: 10:27:16.456: blocking on consuming 64 bytes as no quota
vhost-user-rng-INFO: 10:27:16.456: return from blocked loop: 0
vhost-user-rng-INFO: 10:27:17.454: check_rate_limit: replenishing empty quota
vhost-user-rng-INFO: 10:27:17.454: return from blocked loop: 512
vhost-user-rng-INFO: 10:27:17.456: blocking on consuming 64 bytes as no quota
vhost-user-rng-INFO: 10:27:17.456: return from blocked loop: 0

which seemed reasonable enough...

Quoted 201 lines

I'll send another revision.

Thanks for the review,
Mathieu

Quoted 195 lines
1 file changed, 30 insertions(+), 76 deletions(-)

tools/vhost-user-rng/main.c | 106 +++++++++++++-------------------------------

modified tools/vhost-user-rng/main.c

(quoted diff) +30 -77
@@ -42,13 +42,10 @@
 
 typedef struct {
     VugDev dev;
-    struct itimerspec ts;
-    timer_t rate_limit_timer;
-    pthread_mutex_t rng_mutex;
-    pthread_cond_t rng_cond;
     int64_t quota_remaining;
-    bool activate_timer;
+    guint timer;
     GMainLoop *loop;
+    GMainLoop *blocked;
 } VuRNG;
 
 static gboolean print_cap, verbose;
@@ -59,66 +56,26 @@ static gint source_fd, socket_fd = -1;
 static uint32_t period_ms = 1 << 16;
 static uint64_t max_bytes = INT64_MAX;
 
-static void check_rate_limit(union sigval sv)
+static gboolean check_rate_limit(gpointer user_data)
 {
-    VuRNG *rng = sv.sival_ptr;
-    bool wakeup = false;
+    VuRNG *rng = (VuRNG *) user_data;
 
-    pthread_mutex_lock(&rng->rng_mutex);
-    /*
-     * The timer has expired and the guest has used all available
-     * entropy, which means function vu_rng_handle_request() is waiting
-     * on us.  As such wake it up once we're done here.
-     */
-    if (rng->quota_remaining == 0) {
-        wakeup = true;
+    if (rng->blocked) {
+        g_info("%s: called while blocked", __func__);
+        g_main_loop_quit(rng->blocked);
     }
-
     /*
      * Reset the entropy available to the guest and tell function
      * vu_rng_handle_requests() to start the timer before using it.
      */
     rng->quota_remaining = max_bytes;
-    rng->activate_timer = true;
-    pthread_mutex_unlock(&rng->rng_mutex);
-
-    if (wakeup) {
-        pthread_cond_signal(&rng->rng_cond);
-    }
-}
-
-static void setup_timer(VuRNG *rng)
-{
-    struct sigevent sev;
-    int ret;
-
-    memset(&rng->ts, 0, sizeof(struct itimerspec));
-    rng->ts.it_value.tv_sec = period_ms / 1000;
-    rng->ts.it_value.tv_nsec = (period_ms % 1000) * 1000000;
-
-    /*
-     * Call function check_rate_limit() as if it was the start of
-     * a new thread when the timer expires.
-     */
-    sev.sigev_notify = SIGEV_THREAD;
-    sev.sigev_notify_function = check_rate_limit;
-    sev.sigev_value.sival_ptr = rng;
-    /* Needs to be NULL if defaults attributes are to be used. */
-    sev.sigev_notify_attributes = NULL;
-    ret = timer_create(CLOCK_MONOTONIC, &sev, &rng->rate_limit_timer);
-    if (ret < 0) {
-        fprintf(stderr, "timer_create() failed\n");
-    }
-
+    return true;
 }
 
-
 /* Virtio helpers */
 static uint64_t rng_get_features(VuDev *dev)
 {
-    if (verbose) {
-        g_info("%s: replying", __func__);
-    }
+    g_info("%s: replying", __func__);
     return 0;
 }
 
@@ -137,7 +94,7 @@ static void vu_rng_handle_requests(VuDev *dev, int qidx)
     VuVirtq *vq = vu_get_queue(dev, qidx);
     VuVirtqElement *elem;
     size_t to_read;
-    int len, ret;
+    int len;
 
     for (;;) {
         /* Get element in the vhost virtqueue */
@@ -149,24 +106,21 @@ static void vu_rng_handle_requests(VuDev *dev, int qidx)
         /* Get the amount of entropy to read from the vhost server */
         to_read = elem->in_sg[0].iov_len;
 
-        pthread_mutex_lock(&rng->rng_mutex);
-
         /*
          * We have consumed all entropy available for this time slice.
          * Wait for the timer (check_rate_limit()) to tell us about the
          * start of a new time slice.
          */
         if (rng->quota_remaining == 0) {
-            pthread_cond_wait(&rng->rng_cond, &rng->rng_mutex);
-        }
-
-        /* Start the timer if the last time slice has expired */
-        if (rng->activate_timer == true) {
-            rng->activate_timer = false;
-            ret = timer_settime(rng->rate_limit_timer, 0, &rng->ts, NULL);
-            if (ret < 0) {
-                fprintf(stderr, "timer_settime() failed\n");
-            }
+            g_assert(!rng->blocked);
+            rng->blocked = g_main_loop_new(g_main_loop_get_context(rng->loop), false);
+            g_info("attempting to consume %ld bytes but no quota left (%s)",
+                   to_read,
+                   g_main_loop_is_running(rng->loop) ? "running" : "not running");
+            g_main_loop_run(rng->blocked);
+            g_info("return from blocked loop: %ld", rng->quota_remaining);
+            g_main_loop_unref(rng->blocked);
+            rng->blocked = false;
         }
 
         /* Make sure we don't read more than it's available */
@@ -183,8 +137,6 @@ static void vu_rng_handle_requests(VuDev *dev, int qidx)
 
         rng->quota_remaining -= len;
 
-        pthread_mutex_unlock(&rng->rng_mutex);
-
         vu_queue_push(dev, vq, elem, len);
         free(elem);
     }
@@ -373,6 +325,7 @@ int main(int argc, char *argv[])
      * can add it's GSource watches.
      */
     rng.loop = g_main_loop_new(NULL, FALSE);
+    rng.blocked = NULL;
 
     if (!vug_init(&rng.dev, 1, g_socket_get_fd(socket),
                   panic, &vuiface)) {
@@ -380,24 +333,25 @@ int main(int argc, char *argv[])
         exit(EXIT_FAILURE);
     }
 
-    rng.quota_remaining = max_bytes;
-    rng.activate_timer = true;
-    pthread_mutex_init(&rng.rng_mutex, NULL);
-    pthread_cond_init(&rng.rng_cond, NULL);
-    setup_timer(&rng);
-
     if (verbose) {
-        g_info("period_ms: %d tv_sec: %ld tv_nsec: %lu\n",
-               period_ms, rng.ts.it_value.tv_sec, rng.ts.it_value.tv_nsec);
+        g_log_set_handler(NULL, G_LOG_LEVEL_MASK, g_log_default_handler, NULL);
+        g_setenv("G_MESSAGES_DEBUG", "all", true);
+    } else {
+        g_log_set_handler(NULL,
+                          G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR,
+                          g_log_default_handler, NULL);
     }
 
+    rng.quota_remaining = max_bytes;
+    rng.timer = g_timeout_add(period_ms, check_rate_limit, &rng);
+    g_info("period_ms: %"PRId32", timer %d\n", period_ms, rng.timer);
+
     g_message("entering main loop, awaiting messages");
     g_main_loop_run(rng.loop);
     g_message("finished main loop, cleaning up");
 
     g_main_loop_unref(rng.loop);
     vug_deinit(&rng.dev);
-    timer_delete(rng.rate_limit_timer);
     close(source_fd);
     unlink(socket_path);
 }
--8<---------------cut here---------------end--------------->8---
Signature · 2 lines
-- 
Alex Bennée
Signature · 1 line
Alex Bennée
#12 lore raw

PATCH v3 4/4 docs: Add documentation for vhost based RNG implementation

In reply to Mathieu Poirier

Add description and example for the vhost-user based RNG implementation. Tailored on Viresh Kumar's vhost-user-i2c documentation.

---
 docs/tools/index.rst          |  1 +
 docs/tools/vhost-user-rng.rst | 74 +++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)
 create mode 100644 docs/tools/vhost-user-rng.rst
docs/tools/index.rst +1 -0
index d923834a7398..9d80fa89eceb 100644
@@ -15,5 +15,6 @@ Contents:
    qemu-nbd
    qemu-pr-helper
    qemu-trace-stap
+   vhost-user-rng
    virtfs-proxy-helper
    virtiofsd
docs/tools/vhost-user-rng.rst new file +74 -0

74 lines not on this page · raw message

Signature · 1 line
2.25.1
#13 lore raw

Re: [PATCH v3 4/4] docs: Add documentation for vhost based RNG implementation

Quoted 91 lines · Mathieu Poirier

Add description and example for the vhost-user based RNG implementation. Tailored on Viresh Kumar's vhost-user-i2c documentation.

---
 docs/tools/index.rst          |  1 +
 docs/tools/vhost-user-rng.rst | 74 +++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)
 create mode 100644 docs/tools/vhost-user-rng.rst
docs/tools/index.rst +1 -0
index d923834a7398..9d80fa89eceb 100644
@@ -15,5 +15,6 @@ Contents:
    qemu-nbd
    qemu-pr-helper
    qemu-trace-stap
+   vhost-user-rng
    virtfs-proxy-helper
    virtiofsd
docs/tools/vhost-user-rng.rst new file +64 -0
new file mode 100644
index 000000000000..7f69d7bb3c58
@@ -0,0 +1,74 @@
+QEMU vhost-user-rng - RNG emulation backend
+===========================================
+
+Synopsis
+--------
+
+**vhost-user-rng** [*OPTIONS*]
+
+Description
+-----------
+
+This program is a vhost-user backend that emulates a VirtIO random number
+generator (RNG).  It uses the host's random number generator pool,
+/dev/urandom by default but configurable at will, to satisfy requests from
+guests.
+
+This program is designed to work with QEMU's ``-device
+vhost-user-rng-pci`` but should work with any virtual machine monitor
+(VMM) that supports vhost-user. See the Examples section below.
+
+Options
+-------
+
+.. program:: vhost-user-rng
+
+.. option:: -h, --help
+
+  Print help.
+
+.. option:: -v, --verbose
+
+   Increase verbosity of output
+
+.. option:: -s, --socket-path=PATH
+
+  Listen on vhost-user UNIX domain socket at PATH. Incompatible with --fd.
+
+.. option:: -f, --fd=FDNUM
+
+  Accept connections from vhost-user UNIX domain socket file descriptor FDNUM.
+  The file descriptor must already be listening for connections.
+  Incompatible with --socket-path.
+
+.. option:: -p, --period
+
+  Rate, in milliseconds, at which the RNG hardware can generate random data.
+  Used in conjunction with the --max-bytes option.
+
+.. option:: -m, --max-bytes
+
+  In conjuction with the --period parameter, provides the maximum number of byte
+  per milliseconds a RNG device can generate.
+
+Examples
+--------
+
+The daemon should be started first:
+
+::
+
+  host# vhost-user-rng --socket-path=rng.sock --period=1000 --max-bytes=4096
+
+The QEMU invocation needs to create a chardev socket the device can
+use to communicate as well as share the guests memory over a memfd.

docs/tools/vhost-user-rng.rst line 74

Quoted 10 lines
docs/tools/vhost-user-rng.rst new file +10 -0
+
+::
+
+  host# qemu-system								\
+      -chardev socket,path=$(PATH)/rng.sock,id=rng0				\
+      -device vhost-user-rng-pci,chardev=rng0					\
+      -m 4096 									\
+      -object memory-backend-file,id=mem,size=4G,mem-path=/dev/shm,share=on	\
+      -numa node,memdev=mem							\
+      ...

Would it be worth pointing out how a guest may consume the randomness? I appreciate this will be guest specific but currently I'm struggling how to consume the entropy in a Linux guest.

cat /dev/urandom > /dev/null

didn't seem to cause any to be consumed above what was during boot up.

Signature · 1 line
Alex Bennée
#14 lore raw
In reply to Mathieu Poirier
Quoted 5 lines · Mathieu Poirier

This sets adds a vhost-user based random number generator (RNG), similar to what has been done for i2c and virtiofsd, with the implementation following the patterns already set forth in those.

Applies cleanly to git://git.qemu.org/qemu.git master(05de778b5b8a).

There were several meson-related issues related to this patchset, so I dropped it from the pull request for now.
Please work to resolve, and re-submit, preferably after the release.

Quoted 38 lines

Thanks,
Mathieu

Mathieu Poirier (4):
vhost-user-rng: Add vhost-user-rng implementation
vhost-user-rng-pci: Add vhost-user-rng-pci implementation
vhost-user-rng: backend: Add RNG vhost-user daemon implementation
docs: Add documentation for vhost based RNG implementation

----
New for V3:
- Rebased to latest master branch.
- Fixed documentation warning.
- Updated call to vhost_dev_init() to match new signature.
- Dropped MAINTAINERS patch since it was already applied.

 docs/tools/index.rst                     |   1 +
 docs/tools/vhost-user-rng.rst            |  74 +++++
 hw/virtio/Kconfig                        |   5 +
 hw/virtio/meson.build                    |   2 +
 hw/virtio/vhost-user-rng-pci.c           |  79 +++++
 hw/virtio/vhost-user-rng.c               | 294 +++++++++++++++++
 include/hw/virtio/vhost-user-rng.h       |  33 ++
 tools/meson.build                        |   8 +
 tools/vhost-user-rng/50-qemu-rng.json.in |   5 +
 tools/vhost-user-rng/main.c              | 403 +++++++++++++++++++++++
 tools/vhost-user-rng/meson.build         |  10 +
 11 files changed, 914 insertions(+)
 create mode 100644 docs/tools/vhost-user-rng.rst
 create mode 100644 hw/virtio/vhost-user-rng-pci.c
 create mode 100644 hw/virtio/vhost-user-rng.c
 create mode 100644 include/hw/virtio/vhost-user-rng.h
 create mode 100644 tools/vhost-user-rng/50-qemu-rng.json.in
 create mode 100644 tools/vhost-user-rng/main.c
 create mode 100644 tools/vhost-user-rng/meson.build
Signature · 1 line
2.25.1