]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
ch: prepare domain definition for pci passthrough
authorWei Liu <liuwe@microsoft.com>
Fri, 11 Oct 2024 18:13:06 +0000 (13:13 -0500)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 15 Nov 2024 12:15:27 +0000 (13:15 +0100)
Check if the domain definition is valid for PCI passthrough and update
it if necessary.

Signed-off-by: Wei Liu <liuwe@microsoft.com>
Signed-off-by: Praveen K Paladugu <prapal@linux.microsoft.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
po/POTFILES
src/ch/ch_hostdev.c [new file with mode: 0644]
src/ch/ch_hostdev.h [new file with mode: 0644]
src/ch/ch_process.c
src/ch/meson.build

index c20781e1a8f30b9f64b47b5f100ff829681b4064..3514aa3dcad741e398aa0f91c0c9ff9634ebfed4 100644 (file)
@@ -21,6 +21,7 @@ src/bhyve/bhyve_process.c
 src/ch/ch_conf.c
 src/ch/ch_domain.c
 src/ch/ch_driver.c
+src/ch/ch_hostdev.c
 src/ch/ch_interface.c
 src/ch/ch_monitor.c
 src/ch/ch_process.c
diff --git a/src/ch/ch_hostdev.c b/src/ch/ch_hostdev.c
new file mode 100644 (file)
index 0000000..c18137b
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * ch_hostdev.c: Cloud Hypervisor hostdev management
+ *
+ * Copyright (C) 2021 Wei Liu <liuwe@microsoft.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+
+#include "ch_hostdev.h"
+#include "virlog.h"
+
+#define VIR_FROM_THIS VIR_FROM_CH
+
+VIR_LOG_INIT("ch.ch_hostdev");
+
+static int
+virCHDomainPrepareHostdevPCI(virDomainHostdevDef *hostdev)
+{
+    bool supportsPassthroughVFIO = virHostdevHostSupportsPassthroughVFIO();
+    virDeviceHostdevPCIDriverName *driverName = &hostdev->source.subsys.u.pci.driver.name;
+
+    /* assign defaults for hostdev passthrough */
+    switch (*driverName) {
+    case VIR_DEVICE_HOSTDEV_PCI_DRIVER_NAME_DEFAULT:
+        if (supportsPassthroughVFIO) {
+            *driverName = VIR_DEVICE_HOSTDEV_PCI_DRIVER_NAME_VFIO;
+        } else {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                           _("host doesn't support passthrough of host PCI devices"));
+            return -1;
+        }
+        break;
+
+    case VIR_DEVICE_HOSTDEV_PCI_DRIVER_NAME_VFIO:
+        if (!supportsPassthroughVFIO) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                           _("host doesn't support VFIO PCI passthrough"));
+            return false;
+        }
+        break;
+
+    case VIR_DEVICE_HOSTDEV_PCI_DRIVER_NAME_KVM:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("host doesn't support legacy PCI passthrough"));
+        return false;
+
+    case VIR_DEVICE_HOSTDEV_PCI_DRIVER_NAME_XEN:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("CH does not support device assignment mode '%1$s'"),
+                       virDeviceHostdevPCIDriverNameTypeToString(*driverName));
+        return false;
+
+    default:
+    case VIR_DEVICE_HOSTDEV_PCI_DRIVER_NAME_LAST:
+        virReportEnumRangeError(virDeviceHostdevPCIDriverName, *driverName);
+        break;
+    }
+
+    return true;
+}
+
+int
+virCHDomainPrepareHostdev(virDomainHostdevDef *hostdev)
+{
+    if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
+        return 0;
+
+    switch (hostdev->source.subsys.type) {
+    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
+        return virCHDomainPrepareHostdevPCI(hostdev);
+    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI:
+    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
+    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST:
+    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
+    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
+        break;
+    }
+
+    return 0;
+}
diff --git a/src/ch/ch_hostdev.h b/src/ch/ch_hostdev.h
new file mode 100644 (file)
index 0000000..f9ba40a
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * ch_hostdev.h: Cloud Hypervisor hostdev management
+ *
+ * Copyright (C) 2021 Wei Liu <liuwe@microsoft.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include "ch_conf.h"
+#include "domain_conf.h"
+
+int
+virCHDomainPrepareHostdev(virDomainHostdevDef *hostdev);
index 9816509e494fdac641de802816709879083ba7aa..c5b5b6ebb21ece577b386d1adbed7a604f8cb365 100644 (file)
@@ -37,6 +37,7 @@
 #include "virnuma.h"
 #include "virstring.h"
 #include "ch_interface.h"
+#include "ch_hostdev.h"
 
 #define VIR_FROM_THIS VIR_FROM_CH
 
@@ -808,6 +809,40 @@ virCHProcessStartValidate(virCHDriver *driver,
     return 0;
 }
 
+static int
+virCHProcessPrepareDomainHostdevs(virDomainObj *vm)
+{
+    size_t i;
+
+    for (i = 0; i < vm->def->nhostdevs; i++) {
+        virDomainHostdevDef *hostdev = vm->def->hostdevs[i];
+
+        if (virCHDomainPrepareHostdev(hostdev) < 0)
+            return -1;
+    }
+
+    return 0;
+}
+
+/**
+ * virCHProcessPrepareDomain:
+ * @vm: domain object
+ *
+ * This function groups all code that modifies only live XML of a domain which
+ * is about to start and it's the only place to do those modifications.
+ *
+ * This function MUST be called before virCHProcessPrepareHost().
+ *
+ */
+static int
+virCHProcessPrepareDomain(virDomainObj *vm)
+{
+    if (virCHProcessPrepareDomainHostdevs(vm) < 0)
+        return -1;
+
+    return 0;
+}
+
 /**
  * virCHProcessStart:
  * @driver: pointer to driver structure
@@ -839,6 +874,10 @@ virCHProcessStart(virCHDriver *driver,
         return -1;
     }
 
+    if (virCHProcessPrepareDomain(vm) < 0) {
+        return -1;
+    }
+
     if (!priv->monitor) {
         /* And we can get the first monitor connection now too */
         if (!(priv->monitor = virCHProcessConnectMonitor(driver, vm))) {
index 633966aac7bc27a243552f12f162e02f215aab55..ca1291c15877a4d8f14ed29a27b48e838f5c1ae4 100644 (file)
@@ -13,6 +13,8 @@ ch_driver_sources = [
   'ch_monitor.h',
   'ch_process.c',
   'ch_process.h',
+  'ch_hostdev.c',
+  'ch_hostdev.h',
 ]
 
 driver_source_files += files(ch_driver_sources)