From: Alexander Shursha Date: Mon, 10 Mar 2025 09:05:04 +0000 (+0300) Subject: bhyve: Support passing the 'passthru' command line option X-Git-Tag: CVE-2025-12748~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e4056c458abc924a50a3f9ce8d391e27d3d78777;p=thirdparty%2Flibvirt.git bhyve: Support passing the 'passthru' command line option 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 Signed-off-by: Alexander Shursha Reviewed-by: Michal Privoznik --- diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c index 4c5b4518ea..0ceac618e8 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,33 @@ 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 +968,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 @@ + + bhyve + c7a5fdbd-edaf-9455-926a-d65c16db1809 + 219136 + 219136 + + + + 1 + + hvm + + + destroy + destroy + destroy + + + + + +
+ + + +
+ +
+ + + 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");