Bhyve supports the Virtio RNG interface. It's always using the
/dev/random device and doesn't have any configuration options.

Thus, in XML it's represented as:

  <rng model='virtio'>
    <backend model='random'/>
  </rng>

So extend the bhyve driver to support that and add a set of tests for
this feature.

Signed-off-by: Roman Bogorodskiy <bogorods...@gmail.com>
---
 src/bhyve/bhyve_capabilities.c                | 15 ++++++++
 src/bhyve/bhyve_capabilities.h                |  2 +
 src/bhyve/bhyve_command.c                     | 23 ++++++++++++
 src/bhyve/bhyve_device.c                      | 11 ++++++
 .../bhyvexml2argv-virtio-rnd.args             | 11 ++++++
 .../bhyvexml2argv-virtio-rnd.ldargs           |  4 ++
 .../bhyvexml2argv-virtio-rnd.xml              | 26 +++++++++++++
 tests/bhyvexml2argvtest.c                     |  3 ++
 .../bhyvexml2xmlout-virtio-rnd.xml            | 37 +++++++++++++++++++
 tests/bhyvexml2xmltest.c                      |  1 +
 10 files changed, 133 insertions(+)
 create mode 100644 tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.args
 create mode 100644 tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.ldargs
 create mode 100644 tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.xml
 create mode 100644 tests/bhyvexml2xmloutdata/bhyvexml2xmlout-virtio-rnd.xml

diff --git a/src/bhyve/bhyve_capabilities.c b/src/bhyve/bhyve_capabilities.c
index b065256cf0..37ae5d2872 100644
--- a/src/bhyve/bhyve_capabilities.c
+++ b/src/bhyve/bhyve_capabilities.c
@@ -4,6 +4,7 @@
  * Copyright (C) 2014 Roman Bogorodskiy
  * Copyright (C) 2014 Semihalf
  * Copyright (C) 2020 Fabian Freyer
+ * Copyright (C) 2025 The FreeBSD Foundation
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -327,6 +328,17 @@ bhyveProbeCapsVirtio9p(unsigned int *caps, char *binary)
 }
 
 
+static int
+bhyveProbeCapsVirtioRnd(unsigned int *caps, char *binary)
+{
+    return bhyveProbeCapsDeviceHelper(caps, binary,
+                                      "-s",
+                                      "0,virtio-rnd",
+                                      "pci slot 0:0: unknown device 
\"virtio-rnd\"",
+                                      BHYVE_CAP_VIRTIO_RND);
+}
+
+
 int
 virBhyveProbeCaps(unsigned int *caps)
 {
@@ -364,6 +376,9 @@ virBhyveProbeCaps(unsigned int *caps)
     if ((ret = bhyveProbeCapsVirtio9p(caps, binary)))
         goto out;
 
+    if ((ret = bhyveProbeCapsVirtioRnd(caps, binary)))
+        goto out;
+
  out:
     VIR_FREE(binary);
     return ret;
diff --git a/src/bhyve/bhyve_capabilities.h b/src/bhyve/bhyve_capabilities.h
index 582eae083d..9b24241dc1 100644
--- a/src/bhyve/bhyve_capabilities.h
+++ b/src/bhyve/bhyve_capabilities.h
@@ -2,6 +2,7 @@
  * bhyve_capabilities.h: bhyve capabilities module
  *
  * Copyright (C) 2014 Semihalf
+ * Copyright (C) 2025 The FreeBSD Foundation
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -52,6 +53,7 @@ typedef enum {
     BHYVE_CAP_SOUND_HDA = 1 << 7,
     BHYVE_CAP_VNC_PASSWORD = 1 << 8,
     BHYVE_CAP_VIRTIO_9P = 1 << 9,
+    BHYVE_CAP_VIRTIO_RND = 1 << 10,
 } virBhyveCapsFlags;
 
 int virBhyveProbeGrubCaps(virBhyveGrubCapsFlags *caps);
diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c
index bc287307c8..50de3e1660 100644
--- a/src/bhyve/bhyve_command.c
+++ b/src/bhyve/bhyve_command.c
@@ -2,6 +2,7 @@
  * bhyve_command.c: bhyve command generation
  *
  * Copyright (C) 2014 Roman Bogorodskiy
+ * Copyright (C) 2025 The FreeBSD Foundation
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -152,6 +153,24 @@ bhyveBuildConsoleArgStr(const virDomainDef *def, 
virCommand *cmd)
     return 0;
 }
 
+static int
+bhyveBuildRNGArgStr(const virDomainDef *def G_GNUC_UNUSED,
+                    virDomainRNGDef *rng,
+                    virCommand *cmd)
+{
+    if (rng->backend != VIR_DOMAIN_RNG_BACKEND_RANDOM) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("RNG backend is not supported"));
+        return -1;
+    }
+
+    virCommandAddArg(cmd, "-s");
+    virCommandAddArgFormat(cmd, "%d:%d,virtio-rnd",
+                           rng->info.addr.pci.slot,
+                           rng->info.addr.pci.function);
+    return 0;
+}
+
 static int
 bhyveBuildAHCIControllerArgStr(const virDomainDef *def,
                                virDomainControllerDef *controller,
@@ -807,6 +826,10 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, 
virDomainDef *def,
     if (bhyveBuildConsoleArgStr(def, cmd) < 0)
         return NULL;
 
+    for (i = 0; i < def->nrngs; i++)
+        if (bhyveBuildRNGArgStr(def, def->rngs[i], cmd) < 0)
+            return NULL;
+
     if (def->namespaceData) {
         bhyveDomainCmdlineDef *bhyvecmd;
 
diff --git a/src/bhyve/bhyve_device.c b/src/bhyve/bhyve_device.c
index e4d14c4102..68983d5bc4 100644
--- a/src/bhyve/bhyve_device.c
+++ b/src/bhyve/bhyve_device.c
@@ -2,6 +2,7 @@
  * bhyve_device.c: bhyve device management
  *
  * Copyright (C) 2014 Roman Bogorodskiy
+ * Copyright (C) 2025 The FreeBSD Foundation
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -184,6 +185,16 @@ bhyveAssignDevicePCISlots(virDomainDef *def,
             return -1;
     }
 
+    for (i = 0; i < def->nrngs; i++) {
+        if (!virDeviceInfoPCIAddressIsWanted(&def->rngs[i]->info))
+            continue;
+        if (virDomainPCIAddressReserveNextAddr(addrs,
+                                               &def->rngs[i]->info,
+                                               VIR_PCI_CONNECT_TYPE_PCI_DEVICE,
+                                               -1) < 0)
+            return -1;
+    }
+
     return 0;
 }
 
diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.args 
b/tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.args
new file mode 100644
index 0000000000..849e4599f8
--- /dev/null
+++ b/tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.args
@@ -0,0 +1,11 @@
+bhyve \
+-c 1 \
+-m 214 \
+-u \
+-H \
+-P \
+-s 0:0,hostbridge \
+-s 2:0,ahci,hd:/tmp/freebsd.img \
+-s 3:0,virtio-net,faketapdev,mac=52:54:00:b9:94:02 \
+-s 4:0,virtio-rnd \
+bhyve
diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.ldargs 
b/tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.ldargs
new file mode 100644
index 0000000000..5905f4b3e6
--- /dev/null
+++ b/tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.ldargs
@@ -0,0 +1,4 @@
+bhyveload \
+-m 214 \
+-d /tmp/freebsd.img \
+bhyve
diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.xml 
b/tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.xml
new file mode 100644
index 0000000000..78a4e3f400
--- /dev/null
+++ b/tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.xml
@@ -0,0 +1,26 @@
+<domain type='bhyve'>
+  <name>bhyve</name>
+  <uuid>df3be7e7-a104-11e3-aeb0-50e5492bd3dc</uuid>
+  <memory>219136</memory>
+  <vcpu>1</vcpu>
+  <os>
+    <type>hvm</type>
+  </os>
+  <devices>
+    <disk type='file'>
+      <driver name='file' type='raw'/>
+      <source file='/tmp/freebsd.img'/>
+      <target dev='hda' bus='sata'/>
+      <address type='drive' controller='0' bus='0' target='2' unit='0'/>
+    </disk>
+    <interface type='bridge'>
+      <mac address='52:54:00:b9:94:02'/>
+      <model type='virtio'/>
+      <source bridge="virbr0"/>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' 
function='0x0'/>
+    </interface>
+    <rng model='virtio'>
+      <backend model='random'/>
+    </rng>
+  </devices>
+</domain>
diff --git a/tests/bhyvexml2argvtest.c b/tests/bhyvexml2argvtest.c
index cdaa32f65c..74d9ba4f70 100644
--- a/tests/bhyvexml2argvtest.c
+++ b/tests/bhyvexml2argvtest.c
@@ -243,6 +243,9 @@ mymain(void)
     DO_TEST_FAILURE("fs-9p-unsupported-accessmode");
     driver.bhyvecaps &= ~BHYVE_CAP_VIRTIO_9P;
     DO_TEST_FAILURE("fs-9p");
+    DO_TEST("virtio-rnd");
+    driver.bhyvecaps &= ~BHYVE_CAP_VIRTIO_RND;
+    DO_TEST_FAILURE("virtio-rnd");
 
     /* Address allocation tests */
     DO_TEST("addr-single-sata-disk");
diff --git a/tests/bhyvexml2xmloutdata/bhyvexml2xmlout-virtio-rnd.xml 
b/tests/bhyvexml2xmloutdata/bhyvexml2xmlout-virtio-rnd.xml
new file mode 100644
index 0000000000..61645c719d
--- /dev/null
+++ b/tests/bhyvexml2xmloutdata/bhyvexml2xmlout-virtio-rnd.xml
@@ -0,0 +1,37 @@
+<domain type='bhyve'>
+  <name>bhyve</name>
+  <uuid>df3be7e7-a104-11e3-aeb0-50e5492bd3dc</uuid>
+  <memory unit='KiB'>219136</memory>
+  <currentMemory unit='KiB'>219136</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='x86_64'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <disk type='file' device='disk'>
+      <driver name='file' type='raw'/>
+      <source file='/tmp/freebsd.img'/>
+      <target dev='hda' bus='sata'/>
+      <address type='drive' controller='0' bus='0' target='2' unit='0'/>
+    </disk>
+    <controller type='pci' index='0' model='pci-root'/>
+    <controller type='sata' index='0'>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' 
function='0x0'/>
+    </controller>
+    <interface type='bridge'>
+      <mac address='52:54:00:b9:94:02'/>
+      <source bridge='virbr0'/>
+      <model type='virtio'/>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' 
function='0x0'/>
+    </interface>
+    <rng model='virtio'>
+      <backend model='random'>/dev/random</backend>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' 
function='0x0'/>
+    </rng>
+  </devices>
+</domain>
diff --git a/tests/bhyvexml2xmltest.c b/tests/bhyvexml2xmltest.c
index 428f1b47bb..98006dac04 100644
--- a/tests/bhyvexml2xmltest.c
+++ b/tests/bhyvexml2xmltest.c
@@ -114,6 +114,7 @@ mymain(void)
     DO_TEST_DIFFERENT("sound");
     DO_TEST_DIFFERENT("isa-controller");
     DO_TEST_DIFFERENT("fs-9p");
+    DO_TEST_DIFFERENT("virtio-rnd");
 
     /* Address allocation tests */
     DO_TEST_DIFFERENT("addr-single-sata-disk");
-- 
2.49.0

Reply via email to