]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
bhyve: support random number generator device
authorRoman Bogorodskiy <bogorodskiy@gmail.com>
Sun, 6 Apr 2025 09:59:18 +0000 (11:59 +0200)
committerRoman Bogorodskiy <bogorodskiy@gmail.com>
Thu, 10 Apr 2025 13:05:22 +0000 (15:05 +0200)
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 <bogorodskiy@gmail.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/bhyve/bhyve_capabilities.c
src/bhyve/bhyve_capabilities.h
src/bhyve/bhyve_command.c
src/bhyve/bhyve_device.c
tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.args [new file with mode: 0644]
tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.ldargs [new file with mode: 0644]
tests/bhyvexml2argvdata/bhyvexml2argv-virtio-rnd.xml [new file with mode: 0644]
tests/bhyvexml2argvtest.c
tests/bhyvexml2xmloutdata/bhyvexml2xmlout-virtio-rnd.xml [new file with mode: 0644]
tests/bhyvexml2xmltest.c

index b065256cf0f036874afd824a899ff7ed7d07d638..37ae5d2872921287748112d5ed19187721c4c2e7 100644 (file)
@@ -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;
index 582eae083d227e12a54bf61b330a9793dd193236..9b24241dc1009d4d682ede99efbc92601eeae34b 100644 (file)
@@ -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);
index bc287307c89a69f29388cbb93242c80673e173f1..50de3e166072188cd41d7bcb16fe2607cbfb1285 100644 (file)
@@ -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;
 
index e4d14c4102efd4c687539c2020444173858e4739..68983d5bc49fad27612177facb3fd3a0ff7d1e58 100644 (file)
@@ -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 (file)
index 0000000..849e459
--- /dev/null
@@ -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 (file)
index 0000000..5905f4b
--- /dev/null
@@ -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 (file)
index 0000000..78a4e3f
--- /dev/null
@@ -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>
index cdaa32f65c3fabe7b26263879fbd845f80982c8b..74d9ba4f70af3e4600109d57d2b7ea80367b6bda 100644 (file)
@@ -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 (file)
index 0000000..61645c7
--- /dev/null
@@ -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>
index 428f1b47bbb97d243f91dd80a07dea4b8f58081f..98006dac048177abe76411968c94af61c1ec7d94 100644 (file)
@@ -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");