]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
bhyve: Tie the 'passthru' option to the 'hostdev' XML config
authorAlexander Shursha <kekek2@ya.ru>
Mon, 10 Mar 2025 09:05:05 +0000 (12:05 +0300)
committerRoman Bogorodskiy <bogorodskiy@gmail.com>
Sat, 8 Nov 2025 09:27:56 +0000 (10:27 +0100)
Signed-off-by: Alexander Shursha <kekek2@ya.ru>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/bhyve/bhyve_parse_command.c
tests/bhyveargv2xmldata/bhyveargv2xml-passthru.args [new file with mode: 0644]
tests/bhyveargv2xmldata/bhyveargv2xml-passthru.xml [new file with mode: 0644]
tests/bhyveargv2xmltest.c

index 7a4e48fbf85c0380d01944d7f3fded3b01307a1f..d62ea64beb544cfea78f7f881fcd8d4c0a957526 100644 (file)
@@ -6,6 +6,7 @@
  * Copyright (c) 2011 NetApp, Inc.
  * Copyright (C) 2020 Fabian Freyer
  * 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
@@ -666,6 +667,69 @@ bhyveParsePCIRND(virDomainDef *def,
     return 0;
 }
 
+static int
+bhyveParsePassthru(virDomainDef *def G_GNUC_UNUSED,
+                   unsigned pcibus,
+                   unsigned pcislot,
+                   unsigned pcifunction,
+                   char *addr)
+{
+    /* -s slot,bus/slot/function */
+    /* -s slot,pcibus:slot:function */
+    virDomainHostdevDef *hostdev = NULL;
+    g_auto(GStrv) params = NULL;
+    GStrv param;
+    char *p = NULL;
+
+    if (!addr) {
+        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+                       _("No PCI address provided"));
+        return -1;
+    }
+
+    hostdev = virDomainHostdevDefNew();
+    hostdev->mode = VIR_DOMAIN_HOSTDEV_MODE_SUBSYS;
+    hostdev->source.subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI;
+
+    hostdev->info->type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
+    hostdev->info->addr.pci.bus = pcibus;
+    hostdev->info->addr.pci.slot = pcislot;
+    hostdev->info->addr.pci.function = pcifunction;
+
+    if (!(params = g_strsplit(addr, ":", -1))) {
+        virReportError(VIR_ERR_OPERATION_FAILED, _("Failed to parse PCI address %1$s"), addr);
+        goto error;
+    }
+    if (g_str_equal(addr, *params)) {
+        g_free(params);
+        if (!(params = g_strsplit(addr, "/", -1))) {
+            virReportError(VIR_ERR_OPERATION_FAILED, _("Failed to parse PCI address %1$s"), addr);
+            goto error;
+        }
+    }
+    if (g_strv_length(params) != 3) {
+        virReportError(VIR_ERR_OPERATION_FAILED, _("Failed to parse PCI address %1$s"), addr);
+        goto error;
+    }
+    param = params;
+    if (virStrToLong_uip(*param++, &p, 10, &hostdev->source.subsys.u.pci.addr.bus) < 0 ||
+        virStrToLong_uip(*param++, &p, 10, &hostdev->source.subsys.u.pci.addr.slot) < 0 ||
+        virStrToLong_uip(*param++, &p, 10, &hostdev->source.subsys.u.pci.addr.function) < 0) {
+        virReportError(VIR_ERR_OPERATION_FAILED, _("Failed to parse PCI address %1$s"), addr);
+        goto error;
+    }
+
+    hostdev->source.subsys.u.pci.addr.domain = 0;
+    hostdev->managed = false;
+
+    VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev);
+    return 0;
+
+ error:
+    virDomainHostdevDefFree(hostdev);
+    return -1;
+}
+
 static int
 bhyveParseBhyvePCIArg(virDomainDef *def,
                       virDomainXMLOption *xmlopt,
@@ -732,6 +796,8 @@ bhyveParseBhyvePCIArg(virDomainDef *def,
         bhyveParsePCIFbuf(def, xmlopt, caps, bus, slot, function, conf);
     else if (STREQ(emulation, "virtio-rnd"))
         bhyveParsePCIRND(def, xmlopt, caps, bus, slot, function, conf);
+    else if (STREQ(emulation, "passthru"))
+        bhyveParsePassthru(def, bus, slot, function, conf);
 
     VIR_FREE(emulation);
     VIR_FREE(slotdef);
diff --git a/tests/bhyveargv2xmldata/bhyveargv2xml-passthru.args b/tests/bhyveargv2xmldata/bhyveargv2xml-passthru.args
new file mode 100644 (file)
index 0000000..4eb1ff1
--- /dev/null
@@ -0,0 +1,7 @@
+/usr/sbin/bhyve \
+-c 1 \
+-m 214 \
+-H \
+-P \
+-s 0:0,hostbridge \
+-s 7,passthru,3/0/0 bhyve
diff --git a/tests/bhyveargv2xmldata/bhyveargv2xml-passthru.xml b/tests/bhyveargv2xmldata/bhyveargv2xml-passthru.xml
new file mode 100644 (file)
index 0000000..64118ff
--- /dev/null
@@ -0,0 +1,22 @@
+<domain type='bhyve'>
+  <name>bhyve</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>219136</memory>
+  <currentMemory unit='KiB'>219136</currentMemory>
+  <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>
+    <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 82abce5b8f2278b8e637c213bb80313abd08e030..acfaade24c224e75793f3d91b2ea650ae24ecc12 100644 (file)
@@ -161,6 +161,7 @@ mymain(void)
     DO_TEST("virtio-blk");
     DO_TEST("virtio-net");
     DO_TEST("e1000");
+    DO_TEST("passthru");
     DO_TEST_WARN("virtio-net2");
     DO_TEST_WARN("virtio-net3");
     DO_TEST_WARN("virtio-net4");