bhyve: support random number generator device

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>
This commit is contained in:
Roman Bogorodskiy
2025-04-10 15:05:22 +02:00
parent 97ed7f22b0
commit 03fac384c5
10 changed files with 133 additions and 0 deletions
+15
View 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;
+2
View 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);
+23
View 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;
+11
View 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;
}
@@ -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
@@ -0,0 +1,4 @@
bhyveload \
-m 214 \
-d /tmp/freebsd.img \
bhyve
@@ -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>
+3
View 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");
@@ -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>
+1
View 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");