]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
bhyve: Support passing the 'passthru' command line option
authorAlexander Shursha <kekek2@ya.ru>
Mon, 10 Mar 2025 09:05:04 +0000 (12:05 +0300)
committerRoman Bogorodskiy <bogorodskiy@gmail.com>
Sat, 8 Nov 2025 09:27:27 +0000 (10:27 +0100)
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>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/bhyve/bhyve_command.c
src/bhyve/bhyve_domain.c
tests/bhyvexml2argvdata/bhyvexml2argv-passthru.args [new file with mode: 0644]
tests/bhyvexml2argvdata/bhyvexml2argv-passthru.ldargs [new file with mode: 0644]
tests/bhyvexml2argvdata/bhyvexml2argv-passthru.xml [new file with mode: 0644]
tests/bhyvexml2argvtest.c

index 4c5b4518eab7615d2f202e11e1500d3cc941d6ff..0ceac618e874dd57d20c3f4c40bacc9c1e3b0633 100644 (file)
@@ -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);
index 3c7997ad8617a793e4946be183241874a92c2eb9..63d61b9f855d0dcf6bae06cbbde363eba3c57c02 100644 (file)
@@ -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 (file)
index 0000000..2ff43ad
--- /dev/null
@@ -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 (file)
index 0000000..5905f4b
--- /dev/null
@@ -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 (file)
index 0000000..02c932f
--- /dev/null
@@ -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>
index 6837db2c7ae93f8f22717281bf028a5365c46650..5d234b39fa2aae3cdf7e28ef84019dd998dabaf3 100644 (file)
@@ -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");