From: Alexander Shursha <kekek2@ya.ru> Bhyve supports PCI device passthrough using the following syntax: bhyve ... -s 4:0,passthru,5/2/0 ... Where 5/2/0 is PCI address of the device in the host, and "4:0" is the address in the guest. Currently, user is responsible for reserving the device for passthrough, i.e. by configuring pptdevs in loader.conf(5), or using devctl(8) to detach the device. Co-authored-by: Roman Bogorodskiy <bogorodskiy@gmail.com> Signed-off-by: Alexander Shursha <kekek2@ya.ru> --- src/bhyve/bhyve_command.c | 27 ++++++++++++++++ src/bhyve/bhyve_domain.c | 6 ++++ .../bhyvexml2argv-passthru.args | 10 ++++++ .../bhyvexml2argv-passthru.ldargs | 4 +++ .../bhyvexml2argv-passthru.xml | 31 +++++++++++++++++++ tests/bhyvexml2argvtest.c | 1 + 6 files changed, 79 insertions(+) create mode 100644 tests/bhyvexml2argvdata/bhyvexml2argv-passthru.args create mode 100644 tests/bhyvexml2argvdata/bhyvexml2argv-passthru.ldargs create mode 100644 tests/bhyvexml2argvdata/bhyvexml2argv-passthru.xml diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c index 4c5b4518ea..4224cdf0fc 100644 --- a/src/bhyve/bhyve_command.c +++ b/src/bhyve/bhyve_command.c @@ -3,6 +3,7 @@ * * Copyright (C) 2014 Roman Bogorodskiy * Copyright (C) 2025 The FreeBSD Foundation + * Copyright (C) 2024-2025 Future Crew, LLC * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -216,6 +217,29 @@ bhyveBuildRNGArgStr(const virDomainDef *def G_GNUC_UNUSED, return 0; } +static int +bhyveBuildHostdevArgStr(const virDomainDef *def, virCommand *cmd) +{ + size_t i; + + for (i = 0; i < def->nhostdevs; i++) { + virDomainHostdevDef *hostdev = def->hostdevs[i]; + virDomainHostdevSubsys *subsys = &hostdev->source.subsys; + + if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS || + subsys->type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) + continue; + virCommandAddArg(cmd, "-s"); + virCommandAddArgFormat(cmd, "%d:%d,passthru,%d/%d/%d", + hostdev->info->addr.pci.slot, + hostdev->info->addr.pci.function, + subsys->u.pci.addr.bus, + subsys->u.pci.addr.slot, + subsys->u.pci.addr.function); + } + return 0; +} + static int bhyveBuildAHCIControllerArgStr(const virDomainDef *def, virDomainControllerDef *controller, @@ -940,6 +964,9 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, virDomainDef *def, virCommandAddArg(cmd, bhyvecmd->args[i]); } + if (bhyveBuildHostdevArgStr(def, cmd) < 0) + return NULL; + virCommandAddArg(cmd, def->name); return g_steal_pointer(&cmd); diff --git a/src/bhyve/bhyve_domain.c b/src/bhyve/bhyve_domain.c index 3c7997ad86..63d61b9f85 100644 --- a/src/bhyve/bhyve_domain.c +++ b/src/bhyve/bhyve_domain.c @@ -345,6 +345,12 @@ bhyveDomainDefValidate(const virDomainDef *def, } } + if (def->nhostdevs && !def->mem.locked) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("using passthrough devices requires locking guest memory")); + return -1; + } + if (!def->os.loader) return 0; diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.args b/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.args new file mode 100644 index 0000000000..2ff43ad3b1 --- /dev/null +++ b/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.args @@ -0,0 +1,10 @@ +bhyve \ +-c 1 \ +-m 214 \ +-S \ +-H \ +-P \ +-s 0:0,hostbridge \ +-s 2:0,ahci,hd:/tmp/freebsd.img \ +-s 7:0,passthru,3/0/0 \ +bhyve diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.ldargs b/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.ldargs new file mode 100644 index 0000000000..5905f4b3e6 --- /dev/null +++ b/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.ldargs @@ -0,0 +1,4 @@ +bhyveload \ +-m 214 \ +-d /tmp/freebsd.img \ +bhyve diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.xml b/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.xml new file mode 100644 index 0000000000..02c932f62b --- /dev/null +++ b/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.xml @@ -0,0 +1,31 @@ +<domain type='bhyve'> + <name>bhyve</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>219136</memory> + <currentMemory unit='KiB'>219136</currentMemory> + <memoryBacking> + <locked/> + </memoryBacking> + <vcpu placement='static'>1</vcpu> + <os> + <type>hvm</type> + </os> + <clock offset='localtime'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>destroy</on_reboot> + <on_crash>destroy</on_crash> + <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> + <hostdev mode='subsystem' type='pci' managed='no'> + <source> + <address domain='0x0000' bus='0x03' slot='0x00' function='0x0'/> + </source> + <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> + </hostdev> + </devices> +</domain> diff --git a/tests/bhyvexml2argvtest.c b/tests/bhyvexml2argvtest.c index 6837db2c7a..5d234b39fa 100644 --- a/tests/bhyvexml2argvtest.c +++ b/tests/bhyvexml2argvtest.c @@ -222,6 +222,7 @@ mymain(void) DO_TEST("serial-grub"); DO_TEST("localtime"); DO_TEST("net-e1000"); + DO_TEST("passthru"); DO_TEST("uefi"); DO_TEST("uefi-nvram"); DO_TEST("uefi-nvram-template-set"); -- 2.51.0