From: Roman Bogorodskiy Date: Sun, 6 Apr 2025 09:59:18 +0000 (+0200) Subject: bhyve: support random number generator device X-Git-Tag: v11.3.0-rc1~41 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=03fac384c5aee04793a7cb34a60639306ff0ce03;p=thirdparty%2Flibvirt.git 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: So extend the bhyve driver to support that and add a set of tests for this feature. Signed-off-by: Roman Bogorodskiy Reviewed-by: Michal Privoznik --- 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 @@ + + bhyve + df3be7e7-a104-11e3-aeb0-50e5492bd3dc + 219136 + 1 + + hvm + + + + + + +
+ + + + + +
+ + + + + + 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 @@ + + bhyve + df3be7e7-a104-11e3-aeb0-50e5492bd3dc + 219136 + 219136 + 1 + + hvm + + + + destroy + restart + destroy + + + + + +
+ + + +
+ + + + + +
+ + + /dev/random +
+ + + 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");