[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 11/22] pci: new iommu_group functions



Any device which belongs to an "IOMMU group" (used by vfio) will
have links to all devices of its group listed in
/sys/bus/pci/$device/iommu_group/devices;
/sys/bus/pci/$device/iommu_group is actually a link to
/sys/kernel/iommu_groups/$n, where $n is the group number (there
will be a corresponding device node at /dev/vfio/$n once the
devices are bound to the vfio-pci driver)

The following functions are added:

virPCIDeviceGetIOMMUGroupList

  Gets a virPCIDeviceList with one virPCIDeviceList for each device
  in the same IOMMU group as the provided virPCIDevice (a copy of the
  original device object is included in the list.

virPCIIOMMUGroupIterate

  Calls the function @actor once for each device in the group that
  contains the given virPCIDeviceAddress.

virPCIGetIOMMUGroupAddresses

  Fills in a virPCIDeviceAddressPtr * with an array of
  virPCIDeviceAddress, one for each device in the iommu group of the
  provided virPCIDeviceAddress (including a copy of the original).

virPCIGetIOMMUGroupNum

  Returns the group number as an int (a valid group number will always
  be 0 or greater).  If there is no iommu_group link in the device's
  directory (usually indicating that vfio isn't loaded), -2 will be
  returned. On any real error, -1 will be returned.
---
 src/libvirt_private.syms |   4 +
 src/util/virpci.c        | 217 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virpci.h        |  15 +++-
 3 files changed, 233 insertions(+), 3 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 2f8f202..dd3a138 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1653,6 +1653,7 @@ virPCIDeviceDetach;
 virPCIDeviceFileIterate;
 virPCIDeviceFree;
 virPCIDeviceGetIOMMUGroupDev;
+virPCIDeviceGetIOMMUGroupList;
 virPCIDeviceGetManaged;
 virPCIDeviceGetName;
 virPCIDeviceGetRemoveSlot;
@@ -1682,11 +1683,14 @@ virPCIDeviceSetStubDriver;
 virPCIDeviceSetUnbindFromStub;
 virPCIDeviceSetUsedBy;
 virPCIDeviceWaitForCleanup;
+virPCIGetIOMMUGroupAddresses;
+virPCIGetIOMMUGroupNum;
 virPCIGetNetName;
 virPCIGetPhysicalFunction;
 virPCIGetVirtualFunctionIndex;
 virPCIGetVirtualFunctionInfo;
 virPCIGetVirtualFunctions;
+virPCIIOMMUGroupIterate;
 virPCIIsVirtualFunction;
 virPCIParseDeviceAddress;
 
diff --git a/src/util/virpci.c b/src/util/virpci.c
index c34f872..cbade1b 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -1861,6 +1861,223 @@ cleanup:
     return ret;
 }
 
+
+/* virPCIIOMMUGroupIterate:
+ *   Call @actor for all devices in the same iommu_group as orig
+ *   (including orig itself) Even if there is no iommu_group for the
+ *   device, call @actor once for orig.
+ */
+int
+virPCIIOMMUGroupIterate(virPCIDeviceAddressPtr orig,
+                        virPCIDeviceAddressActor actor,
+                        void *opaque)
+{
+    char *groupPath = NULL;
+    DIR *groupDir = NULL;
+    int ret = -1;
+    struct dirent *ent;
+
+    if (virAsprintf(&groupPath,
+                    PCI_SYSFS "devices/%04x:%02x:%02x.%x/iommu_group/devices",
+                    orig->domain, orig->bus, orig->slot, orig->function) < 0) {
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    if (!(groupDir = opendir(groupPath))) {
+        /* just process the original device, nothing more */
+        ret = (actor)(orig, opaque);
+        goto cleanup;
+    }
+
+    while ((ent = readdir(groupDir)) != NULL) {
+        virPCIDeviceAddress newDev;
+
+        if (ent->d_name[0] == '.')
+            continue;
+
+        if (virPCIParseDeviceAddress(ent->d_name, &newDev) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Found invalid device link '%s' in '%s'"),
+                           ent->d_name, groupPath);
+            goto cleanup;
+        }
+
+        if ((actor)(&newDev, opaque) < 0)
+            goto cleanup;
+    }
+
+    ret = 0;
+
+cleanup:
+    VIR_FREE(groupPath);
+    if (groupDir)
+        closedir(groupDir);
+    return ret;
+}
+
+
+static int
+virPCIDeviceGetIOMMUGroupAddOne(virPCIDeviceAddressPtr newDevAddr, void *opaque)
+{
+    int ret = -1;
+    virPCIDeviceListPtr groupList = opaque;
+    virPCIDevicePtr newDev;
+
+    if (!(newDev = virPCIDeviceNew(newDevAddr->domain, newDevAddr->bus,
+                                   newDevAddr->slot, newDevAddr->function))) {
+        goto cleanup;
+    }
+
+    if (virPCIDeviceListAdd(groupList, newDev) < 0) {
+        goto cleanup;
+    }
+    newDev = NULL; /* it's now on the list */
+    ret = 0;
+cleanup:
+    virPCIDeviceFree(newDev);
+    return ret;
+}
+
+
+/*
+ * virPCIDeviceGetIOMMUGroupList - return a virPCIDeviceList containing
+ * all of the devices in the same iommu_group as @dev.
+ *
+ * Return the new list, or NULL on failure
+ */
+virPCIDeviceListPtr
+virPCIDeviceGetIOMMUGroupList(virPCIDevicePtr dev)
+{
+    virPCIDeviceListPtr groupList = virPCIDeviceListNew();
+    virPCIDeviceAddress devAddr = { dev->domain, dev->bus,
+                                    dev->slot, dev->function };
+
+    if (!groupList)
+        goto error;
+
+    if (virPCIIOMMUGroupIterate(&devAddr, virPCIDeviceGetIOMMUGroupAddOne,
+                                groupList) < 0) {
+        goto error;
+    }
+    return groupList;
+
+error:
+    virObjectUnref(groupList);
+    return NULL;
+}
+
+
+typedef struct {
+    virPCIDeviceAddressPtr **iommuGroupDevices;
+    size_t *nIommuGroupDevices;
+} virPCIDeviceAddressList;
+typedef virPCIDeviceAddressList *virPCIDeviceAddressListPtr;
+
+static int
+virPCIGetIOMMUGroupAddressesAddOne(virPCIDeviceAddressPtr newDevAddr, void *opaque)
+{
+    int ret = -1;
+    virPCIDeviceAddressListPtr addrList = opaque;
+    virPCIDeviceAddressPtr copyAddr;
+
+    /* make a copy to insert onto the list */
+    if (VIR_ALLOC(copyAddr) < 0)
+        goto cleanup;
+
+    *copyAddr = *newDevAddr;
+
+    if (VIR_APPEND_ELEMENT(*addrList->iommuGroupDevices,
+                           *addrList->nIommuGroupDevices, copyAddr) < 0) {
+        goto cleanup;
+    }
+
+    ret = 0;
+cleanup:
+    VIR_FREE(copyAddr);
+    return ret;
+}
+
+
+/*
+ * virPCIGetIOMMUGroupAddresses - return a virPCIDeviceList containing
+ * all of the devices in the same iommu_group as @dev.
+ *
+ * Return the new list, or NULL on failure
+ */
+int
+virPCIGetIOMMUGroupAddresses(virPCIDeviceAddressPtr devAddr,
+                             virPCIDeviceAddressPtr **iommuGroupDevices,
+                             size_t *nIommuGroupDevices)
+{
+    int ret = -1;
+    virPCIDeviceAddressList addrList = { iommuGroupDevices,
+                                         nIommuGroupDevices };
+
+    if (virPCIIOMMUGroupIterate(devAddr,
+                                virPCIGetIOMMUGroupAddressesAddOne,
+                                &addrList) < 0) {
+        goto cleanup;
+    }
+
+    ret = 0;
+cleanup:
+    return ret;
+}
+
+
+/* virPCIGetIOMMUGroupNum - return the group number of this PCI
+ * device's iommu_group, or -2 if there is no iommu_group for the
+ * device (or -1 if there was any other error)
+ */
+int
+virPCIGetIOMMUGroupNum(virPCIDeviceAddressPtr addr)
+{
+    char *devName = NULL;
+    char *devPath = NULL;
+    char *groupPath = NULL;
+    const char *groupNumStr;
+    unsigned int groupNum;
+    int ret = -1;
+
+    if (virAsprintf(&devName, "%.4x:%.2x:%.2x.%.1x", addr->domain,
+                    addr->bus, addr->slot, addr->function) < 0) {
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    if (virPCIFile(&devPath, devName, "iommu_group") < 0)
+        goto cleanup;
+    if (virFileIsLink(devPath) != 1) {
+        ret = -2;
+        goto cleanup;
+    }
+    if (virFileResolveLink(devPath, &groupPath) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unable to resolve device %s iommu_group symlink %s"),
+                       devName, devPath);
+        goto cleanup;
+    }
+
+    groupNumStr = last_component(groupPath);
+    if (virStrToLong_ui(groupNumStr, NULL, 10, &groupNum) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("device %s iommu_group symlink %s has "
+                         "invalid group number %s"),
+                       devName, groupPath, groupNumStr);
+        ret = -1;
+        goto cleanup;
+    }
+
+    ret = groupNum;
+cleanup:
+    VIR_FREE(devName);
+    VIR_FREE(devPath);
+    VIR_FREE(groupPath);
+    return ret;
+}
+
+
 /* virPCIDeviceGetIOMMUGroupDev - return the name of the device used
  * to control this PCI device's group (e.g. "/dev/vfio/15")
  */
diff --git a/src/util/virpci.h b/src/util/virpci.h
index bcf1b81..972f86b 100644
--- a/src/util/virpci.h
+++ b/src/util/virpci.h
@@ -112,12 +112,21 @@ int virPCIDeviceListFindIndex(virPCIDeviceListPtr list,
  */
 typedef int (*virPCIDeviceFileActor)(virPCIDevicePtr dev,
                                      const char *path, void *opaque);
-
 int virPCIDeviceFileIterate(virPCIDevicePtr dev,
                             virPCIDeviceFileActor actor,
                             void *opaque);
-char *
-virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev);
+
+typedef int (*virPCIDeviceAddressActor)(virPCIDeviceAddress *addr,
+                                        void *opaque);
+int virPCIIOMMUGroupIterate(virPCIDeviceAddressPtr orig,
+                            virPCIDeviceAddressActor actor,
+                            void *opaque);
+virPCIDeviceListPtr virPCIDeviceGetIOMMUGroupList(virPCIDevicePtr dev);
+int virPCIGetIOMMUGroupAddresses(virPCIDeviceAddressPtr devAddr,
+                                 virPCIDeviceAddressPtr **iommuGroupDevices,
+                                 size_t *nIommuGroupDevices);
+int virPCIGetIOMMUGroupNum(virPCIDeviceAddressPtr dev);
+char *virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev);
 
 int virPCIDeviceIsAssignable(virPCIDevicePtr dev,
                              int strict_acs_check);
-- 
1.7.11.7


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]