Skip to contentSkip to conversation

virtio: Add vhost-user based RNG

PATCH Superseded Mathieu Poirier 2 participants 6 messages Sep 27, 2021 – Oct 12, 2021 Arm/Amlogic Meson SoC support

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover virtio: Add vhost-user based RNG

This set implements a random number generator (RNG) device that follows the vhost-user protocol.

The main difference between V3 is the absence of the vhost-user daemon implemenation. It was dropped to favour the rust implementation currently being considered under the vhost-device crate[1] of the rust-vmm repository[1].

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

Thanks,
Mathieu

[1]. https://github.com/rust-vmm/vhost-device/pull/29

Changes for V4:
1. Addressed merge conflicts
2. Dropped error path after call to g_new0()
3. Changed #define in vhost-user-rng-pci.c
4. Dropped vhost-user-daemon implementation
5. Modified documentation to reflect the above (4)
6. Added Alex's RB tags.

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

 docs/system/devices/vhost-user-rng.rst |  39 ++++
 hw/virtio/Kconfig                      |   5 +
 hw/virtio/meson.build                  |   2 +
 hw/virtio/vhost-user-rng-pci.c         |  79 +++++++
 hw/virtio/vhost-user-rng.c             | 289 +++++++++++++++++++++++++
 include/hw/virtio/vhost-user-rng.h     |  33 +++
 6 files changed, 447 insertions(+)
 create mode 100644 docs/system/devices/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
Signature · 1 line
2.25.1
#1 lore raw

PATCH v4 1/3 vhost-user-rng: Add vhost-user-rng implementation

Introduce a random number generator (RNG) backend that communicates with a vhost-user server to retrieve entropy. That way other VMM that comply with the vhost user protocl can use the same vhost-user daemon without having to write yet another RNG driver.

---
 hw/virtio/Kconfig                  |   5 +
 hw/virtio/meson.build              |   1 +
 hw/virtio/vhost-user-rng.c         | 289 +++++++++++++++++++++++++++++
 include/hw/virtio/vhost-user-rng.h |  33 ++++
 4 files changed, 328 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 35ab45e2095c..c144d42f9bd0 100644
@@ -63,3 +63,8 @@ config VHOST_USER_I2C
     bool
     default y
     depends on VIRTIO && VHOST_USER
+
+config VHOST_USER_RNG
+    bool
+    default y
+    depends on VIRTIO && VHOST_USER
hw/virtio/meson.build +1 -0
index bc352a600911..ae6b2cde1068 100644
@@ -27,6 +27,7 @@ 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_I2C', if_true: files('vhost-user-i2c.c'))
 virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_I2C'], if_true: files('vhost-user-i2c-pci.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 +289 -0

289 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
#2 lore raw

PATCH v4 2/3 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 backend.

---
 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 ae6b2cde1068..521f7d64a86a 100644
@@ -28,6 +28,7 @@ virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c'))
 virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c'))
 virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_I2C'], if_true: files('vhost-user-i2c-pci.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
#3 lore raw

PATCH v4 3/3 docs: Add documentation for vhost based RNG implementation

In reply to Mathieu Poirier

Add description and example for the vhost-user based RNG implementation.

---
 docs/system/devices/vhost-user-rng.rst | 39 ++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 docs/system/devices/vhost-user-rng.rst
docs/system/devices/vhost-user-rng.rst new file +39 -0
new file mode 100644
index 000000000000..a145d4105c1a
@@ -0,0 +1,39 @@
+QEMU vhost-user-rng - RNG emulation
+===================================
+
+Background
+----------
+
+What follows builds on the material presented in vhost-user.rst - it should
+be reviewed before moving forward with the content in this file.
+
+Description
+-----------
+
+The vhost-user-rng device implementation was designed to work with a random
+number generator daemon such as the one found in the vhost-device crate of
+the rust-vmm project available on github [1].
+
+[1]. https://github.com/rust-vmm/vhost-device
+
+Examples
+--------
+
+The daemon should be started first:
+
+::
+
+  host# vhost-device-rng --socket-path=rng.sock -c 1 -m 512 -p 1000
+
+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.
+
+::
+
+  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							\
+      ...
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH v4 3/3] docs: Add documentation for vhost based RNG implementation

Quoted 3 lines · Mathieu Poirier

Add description and example for the vhost-user based RNG implementation.

Causes a build failure:

Warning, treated as error:
/scm/qemu/docs/system/devices/vhost-user-rng.rst:document isn't included in any toctree

Quoted 52 lines
---
 docs/system/devices/vhost-user-rng.rst | 39 ++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 docs/system/devices/vhost-user-rng.rst
docs/system/devices/vhost-user-rng.rst new file +39 -0
new file mode 100644
index 000000000000..a145d4105c1a
@@ -0,0 +1,39 @@
+QEMU vhost-user-rng - RNG emulation
+===================================
+
+Background
+----------
+
+What follows builds on the material presented in vhost-user.rst - it should
+be reviewed before moving forward with the content in this file.
+
+Description
+-----------
+
+The vhost-user-rng device implementation was designed to work with a random
+number generator daemon such as the one found in the vhost-device crate of
+the rust-vmm project available on github [1].
+
+[1]. https://github.com/rust-vmm/vhost-device
+
+Examples
+--------
+
+The daemon should be started first:
+
+::
+
+  host# vhost-device-rng --socket-path=rng.sock -c 1 -m 512 -p 1000
+
+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.
+
+::
+
+  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							\
+      ...
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH v4 3/3] docs: Add documentation for vhost based RNG implementation

Quoted 11 lines · Michael S. Tsirkin
Quoted 3 lines · Mathieu Poirier

Add description and example for the vhost-user based RNG implementation.

Causes a build failure:

Warning, treated as error:
/scm/qemu/docs/system/devices/vhost-user-rng.rst:document isn't included in any toctree

(Sigh...)

I simply don't see those errors on my side... I wonder if it's a tooling version problem. Anyways I sent a new revision, hopefully it will be enough to make the build happy.

Thanks for the patience,
Mathieu

Quoted 55 lines
Quoted 54 lines

---
 docs/system/devices/vhost-user-rng.rst | 39 ++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 docs/system/devices/vhost-user-rng.rst
docs/system/devices/vhost-user-rng.rst new file +39 -1
new file mode 100644
index 000000000000..a145d4105c1a
@@ -0,0 +1,39 @@
+QEMU vhost-user-rng - RNG emulation
+===================================
+
+Background
+----------
+
+What follows builds on the material presented in vhost-user.rst - it should
+be reviewed before moving forward with the content in this file.
+
+Description
+-----------
+
+The vhost-user-rng device implementation was designed to work with a random
+number generator daemon such as the one found in the vhost-device crate of
+the rust-vmm project available on github [1].
+
+[1]. https://github.com/rust-vmm/vhost-device
+
+Examples
+--------
+
+The daemon should be started first:
+
+::
+
+  host# vhost-device-rng --socket-path=rng.sock -c 1 -m 512 -p 1000
+
+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.
+
+::
+
+  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                                                  \
+      ...
--

2.25.1