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

[libvirt] [PATCH 01/16] util: Introduce new module virmdev



Beside creation, disposal, getter, and setter methods the module exports
methods to work with lists of mediated devices.

Signed-off-by: Erik Skultety <eskultet redhat com>
---
 po/POTFILES.in           |   1 +
 src/Makefile.am          |   1 +
 src/libvirt_private.syms |  17 +++
 src/util/virmdev.c       | 375 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virmdev.h       |  85 +++++++++++
 5 files changed, 479 insertions(+)
 create mode 100644 src/util/virmdev.c
 create mode 100644 src/util/virmdev.h

diff --git a/po/POTFILES.in b/po/POTFILES.in
index 365ea66..53c674e 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -218,6 +218,7 @@ src/util/virlease.c
 src/util/virlockspace.c
 src/util/virlog.c
 src/util/virmacmap.c
+src/util/virmdev.c
 src/util/virnetdev.c
 src/util/virnetdevbandwidth.c
 src/util/virnetdevbridge.c
diff --git a/src/Makefile.am b/src/Makefile.am
index 2f32d41..6510e1d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -186,6 +186,7 @@ UTIL_SOURCES =							\
 		util/viruuid.c util/viruuid.h			\
 		util/virxdrdefs.h                               \
 		util/virxml.c util/virxml.h			\
+		util/virmdev.c util/virmdev.h			\
 		$(NULL)
 
 EXTRA_DIST += $(srcdir)/util/keymaps.csv $(srcdir)/util/virkeycode-mapgen.py
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 8e994c7..4047945 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1960,6 +1960,23 @@ virMacMapNew;
 virMacMapRemove;
 virMacMapWriteFile;
 
+# util/virmdev.h
+virMediatedDeviceFree;
+virMediatedDeviceGetIOMMUGroupDev;
+virMediatedDeviceGetPath;
+virMediatedDeviceGetUsedBy;
+virMediatedDeviceListAdd;
+virMediatedDeviceListCount;
+virMediatedDeviceListDel;
+virMediatedDeviceListFind;
+virMediatedDeviceListGet;
+virMediatedDeviceListNew;
+virMediatedDeviceListSteal;
+virMediatedDeviceListStealIndex;
+virMediatedDeviceNew;
+virMediatedDeviceSetUsedBy;
+
+
 
 # util/virnetdev.h
 virNetDevAddMulti;
diff --git a/src/util/virmdev.c b/src/util/virmdev.c
new file mode 100644
index 0000000..0b70798
--- /dev/null
+++ b/src/util/virmdev.c
@@ -0,0 +1,375 @@
+/*
+ * virmdev.c: helper APIs for managing host MDEV devices
+ *
+ * Copyright (C) 2017-2018 Red Hat, Inc.
+ *
+ * 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/>.
+ *
+ * Authors:
+ *     Erik Skultety <eskultet redhat com>
+ */
+
+#include <config.h>
+
+#include <fcntl.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include "virmdev.h"
+#include "dirname.h"
+#include "virlog.h"
+#include "viralloc.h"
+#include "vircommand.h"
+#include "virerror.h"
+#include "virfile.h"
+#include "virkmod.h"
+#include "virstring.h"
+#include "virutil.h"
+#include "viruuid.h"
+#include "virhostdev.h"
+
+VIR_LOG_INIT("util.mdev");
+
+struct _virMediatedDevice {
+    char *path;             /* sysfs path */
+    bool managed;
+
+    char *used_by_drvname;
+    char *used_by_domname;
+};
+
+struct _virMediatedDeviceList {
+    virObjectLockable parent;
+
+    size_t count;
+    virMediatedDevicePtr *devs;
+};
+
+
+/* For virReportOOMError()  and virReportSystemError() */
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+static virClassPtr virMediatedDeviceListClass;
+
+static void virMediatedDeviceListDispose(void *obj);
+
+static int virMediatedOnceInit(void)
+{
+    if (!(virMediatedDeviceListClass = virClassNew(virClassForObjectLockable(),
+                                                   "virMediatedDeviceList",
+                                                   sizeof(virMediatedDeviceList),
+                                                   virMediatedDeviceListDispose)))
+        return -1;
+
+    return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virMediated)
+
+#ifdef __linux__
+# define MDEV_SYSFS "/sys/class/mdev_bus/"
+
+virMediatedDevicePtr
+virMediatedDeviceNew(virPCIDeviceAddressPtr pciaddr, const char *uuidstr)
+{
+    virMediatedDevicePtr dev = NULL, ret = NULL;
+    char *pcistr = NULL;
+
+    if (virAsprintf(&pcistr, "%.4x:%.2x:%.2x.%.1x", pciaddr->domain,
+                    pciaddr->bus, pciaddr->slot, pciaddr->function) < 0)
+        return NULL;
+
+    if (VIR_ALLOC(dev) < 0)
+        goto cleanup;
+
+    if (virAsprintf(&dev->path, MDEV_SYSFS "%s/%s", pcistr, uuidstr) < 0)
+       goto cleanup;
+
+    ret = dev;
+    dev = NULL;
+
+ cleanup:
+    VIR_FREE(pcistr);
+    virMediatedDeviceFree(dev);
+    return ret;
+}
+
+
+void
+virMediatedDeviceFree(virMediatedDevicePtr dev)
+{
+    if (!dev)
+        return;
+    VIR_FREE(dev->path);
+    VIR_FREE(dev->used_by_drvname);
+    VIR_FREE(dev->used_by_domname);
+    VIR_FREE(dev);
+}
+
+
+const char *
+virMediatedDeviceGetPath(virMediatedDevicePtr dev)
+{
+    return dev->path;
+}
+
+
+/* Returns an absolute canonicalized path to the device used to control the
+ * mediated device's IOMMU group (e.g. "/dev/vfio/15")
+ */
+char *
+virMediatedDeviceGetIOMMUGroupDev(virMediatedDevicePtr dev)
+{
+    char *resultpath = NULL;
+    char *iommu_linkpath = NULL;
+    char *vfio_path = NULL;
+
+    if (virAsprintf(&iommu_linkpath, "%s/iommu_group", dev->path) < 0)
+        return NULL;
+
+    if (virFileIsLink(iommu_linkpath) != 1) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("IOMMU group file %s is not a symlink"),
+                       iommu_linkpath);
+        goto cleanup;
+    }
+
+    if (virFileResolveLink(iommu_linkpath, &resultpath) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unable to resolve IOMMU group symlink %s"),
+                       iommu_linkpath);
+        goto cleanup;
+    }
+
+    if (virAsprintf(&vfio_path, "/dev/vfio/%s", last_component(resultpath)) < 0)
+        goto cleanup;
+
+ cleanup:
+    VIR_FREE(resultpath);
+    VIR_FREE(iommu_linkpath);
+    return vfio_path;
+}
+
+
+void
+virMediatedDeviceGetUsedBy(virMediatedDevicePtr dev,
+                           char **drvname, char **domname)
+{
+    *drvname = dev->used_by_drvname;
+    *domname = dev->used_by_domname;
+}
+
+
+int
+virMediatedDeviceSetUsedBy(virMediatedDevicePtr dev,
+                           const char *drvname,
+                           const char *domname)
+{
+    VIR_FREE(dev->used_by_drvname);
+    VIR_FREE(dev->used_by_domname);
+    if (VIR_STRDUP(dev->used_by_drvname, drvname) < 0)
+        return -1;
+    if (VIR_STRDUP(dev->used_by_domname, domname) < 0)
+        return -1;
+
+    return 0;
+}
+
+
+#else
+static const char *unsupported = N_("not supported on non-linux platforms");
+
+virMediatedDevicePtr
+virMediatedDeviceNew(virPCIDeviceAddressPtr pciaddr ATTRIBUTE_UNUSED,
+                     const char *uuidstr ATTRIBUTE_UNUSED)
+{
+    virRerportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
+    return NULL;
+}
+
+
+char *
+virMediatedDeviceGetIOMMUGroupDev(virMediatedDevicePtr dev ATTRIBUTE_UNUSED)
+{
+    virRerportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
+    return NULL;
+}
+
+
+void
+virMediatedDeviceFree(virMediatedDevicePtr dev ATTRIBUTE_UNUSED)
+{
+    virRerportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
+}
+
+
+const char *
+virMediatedDeviceGetPath(virMediatedDevicePtr dev ATTRIBUTE_UNUSED)
+{
+    virRerportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
+    return NULL;
+}
+
+
+void
+virMediatedDeviceGetUsedBy(virMediatedDevicePtr dev,
+                           char **drvname, char **domname)
+{
+    virRerportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
+    *drvname = NULL;
+    *domname = NULL;
+}
+
+
+int
+virMediatedDeviceSetUsedBy(virMediatedDevicePtr dev ATTRIBUTE_UNUSED,
+                           const char *drvname ATTRIBUTE_UNUSED,
+                           const char *domname ATTRIBUTE_UNUSED)
+{
+    virRerportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
+    return -1;
+}
+#endif /* __linux__ */
+
+virMediatedDeviceListPtr
+virMediatedDeviceListNew(void)
+{
+    virMediatedDeviceListPtr list;
+
+    if (virMediatedInitialize() < 0)
+        return NULL;
+
+    if (!(list = virObjectLockableNew(virMediatedDeviceListClass)))
+        return NULL;
+
+    return list;
+}
+
+
+static void
+virMediatedDeviceListDispose(void *obj)
+{
+    virMediatedDeviceListPtr list = obj;
+    size_t i;
+
+    for (i = 0; i < list->count; i++) {
+        virMediatedDeviceFree(list->devs[i]);
+        list->devs[i] = NULL;
+    }
+
+    list->count = 0;
+    VIR_FREE(list->devs);
+}
+
+
+int
+virMediatedDeviceListAdd(virMediatedDeviceListPtr list,
+                         virMediatedDevicePtr dev)
+{
+    if (virMediatedDeviceListFind(list, dev)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Device %s is already in use"), dev->path);
+        return -1;
+    }
+    return VIR_APPEND_ELEMENT(list->devs, list->count, dev);
+}
+
+
+virMediatedDevicePtr
+virMediatedDeviceListGet(virMediatedDeviceListPtr list,
+                         int idx)
+{
+    if (idx >= list->count)
+        return NULL;
+    if (idx < 0)
+        return NULL;
+
+    return list->devs[idx];
+}
+
+
+size_t
+virMediatedDeviceListCount(virMediatedDeviceListPtr list)
+{
+    return list->count;
+}
+
+
+virMediatedDevicePtr
+virMediatedDeviceListStealIndex(virMediatedDeviceListPtr list,
+                                int idx)
+{
+    virMediatedDevicePtr ret;
+
+    if (idx < 0 || idx >= list->count)
+        return NULL;
+
+    ret = list->devs[idx];
+    VIR_DELETE_ELEMENT(list->devs, idx, list->count);
+    return ret;
+}
+
+
+virMediatedDevicePtr
+virMediatedDeviceListSteal(virMediatedDeviceListPtr list,
+                           virMediatedDevicePtr dev)
+{
+    int idx = virMediatedDeviceListFindIndex(list, dev);
+
+    return virMediatedDeviceListStealIndex(list, idx);
+}
+
+
+void
+virMediatedDeviceListDel(virMediatedDeviceListPtr list,
+                         virMediatedDevicePtr dev)
+{
+    virMediatedDevicePtr ret = virMediatedDeviceListSteal(list, dev);
+    virMediatedDeviceFree(ret);
+}
+
+
+int
+virMediatedDeviceListFindIndex(virMediatedDeviceListPtr list,
+                               virMediatedDevicePtr dev)
+{
+    size_t i;
+
+    for (i = 0; i < list->count; i++) {
+        virMediatedDevicePtr other = list->devs[i];
+        if (STREQ(other->path, dev->path))
+            return i;
+    }
+    return -1;
+}
+
+
+virMediatedDevicePtr
+virMediatedDeviceListFind(virMediatedDeviceListPtr list,
+                          virMediatedDevicePtr dev)
+{
+    int idx;
+
+    if ((idx = virMediatedDeviceListFindIndex(list, dev)) >= 0)
+        return list->devs[idx];
+    else
+        return NULL;
+}
diff --git a/src/util/virmdev.h b/src/util/virmdev.h
new file mode 100644
index 0000000..c4d97f3
--- /dev/null
+++ b/src/util/virmdev.h
@@ -0,0 +1,85 @@
+/*
+ * virmdev.h: helper APIs for managing host mediated devices
+ *
+ * Copyright (C) 2017-2018 Red Hat, Inc.
+ *
+ * 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/>.
+ *
+ * Authors:
+ *     Erik Skultety <eskultet redhat com>
+ */
+
+#ifndef __VIR_MDEV_H__
+# define __VIR_MDEV_H__
+
+# include "internal.h"
+# include "virobject.h"
+# include "virutil.h"
+# include "virpci.h"
+
+typedef struct _virMediatedDevice virMediatedDevice;
+typedef virMediatedDevice *virMediatedDevicePtr;
+typedef struct _virMediatedDeviceAddress virMediatedDeviceAddress;
+typedef virMediatedDeviceAddress *virMediatedDeviceAddressPtr;
+typedef struct _virMediatedDeviceList virMediatedDeviceList;
+typedef virMediatedDeviceList *virMediatedDeviceListPtr;
+
+typedef int (*virMediatedDeviceCallback)(virMediatedDevicePtr dev,
+                                         const char *path, void *opaque);
+
+virMediatedDevicePtr virMediatedDeviceNew(virPCIDeviceAddressPtr addr,
+                                          const char *uuidstr);
+
+virMediatedDevicePtr virMediatedDeviceCopy(virMediatedDevicePtr dev);
+
+void virMediatedDeviceFree(virMediatedDevicePtr dev);
+
+const char *virMediatedDeviceGetPath(virMediatedDevicePtr dev);
+
+void virMediatedDeviceGetUsedBy(virMediatedDevicePtr dev,
+                                char **drvname, char **domname);
+
+int virMediatedDeviceSetUsedBy(virMediatedDevicePtr dev,
+                               const char *drvname,
+                               const char *domname);
+
+char *virMediatedDeviceGetIOMMUGroupDev(virMediatedDevicePtr dev);
+
+virMediatedDeviceListPtr virMediatedDeviceListNew(void);
+
+int  virMediatedDeviceListAdd(virMediatedDeviceListPtr list,
+                              virMediatedDevicePtr dev);
+
+virMediatedDevicePtr virMediatedDeviceListGet(virMediatedDeviceListPtr list,
+                                              int idx);
+
+size_t virMediatedDeviceListCount(virMediatedDeviceListPtr list);
+
+virMediatedDevicePtr virMediatedDeviceListSteal(virMediatedDeviceListPtr list,
+                                                virMediatedDevicePtr dev);
+
+virMediatedDevicePtr virMediatedDeviceListStealIndex(virMediatedDeviceListPtr list,
+                                                     int idx);
+
+void virMediatedDeviceListDel(virMediatedDeviceListPtr list,
+                              virMediatedDevicePtr dev);
+
+virMediatedDevicePtr virMediatedDeviceListFind(virMediatedDeviceListPtr list,
+                                               virMediatedDevicePtr dev);
+
+int virMediatedDeviceListFindIndex(virMediatedDeviceListPtr list,
+                                   virMediatedDevicePtr dev);
+
+#endif /* __VIR_MDEV_H__ */
-- 
2.10.2


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