A mediated device will be identified by a UUID (with 'model' now being
a mandatory <hostdev> attribute to represent the mediated device API) of
the user pre-created mediated device. The data necessary to identify a
mediated device can be easily extended in the future, e.g. when
auto-creation of mediated devices should be enabled.
Additionally, this patch introduces a new virmdev module under src/util.
Signed-off-by: Erik Skultety <eskultet(a)redhat.com>
---
docs/schemas/domaincommon.rng | 22 ++
po/POTFILES.in | 1 +
src/Makefile.am | 1 +
src/conf/domain_conf.c | 82 +++++-
src/conf/domain_conf.h | 9 +
src/libvirt_private.syms | 22 ++
src/qemu/qemu_domain.c | 1 +
src/qemu/qemu_hotplug.c | 2 +
src/security/security_apparmor.c | 3 +
src/security/security_dac.c | 2 +
src/security/security_selinux.c | 2 +
src/util/virmdev.c | 487 ++++++++++++++++++++++++++++++++++++
src/util/virmdev.h | 123 +++++++++
tests/domaincapsschemadata/full.xml | 1 +
14 files changed, 757 insertions(+), 1 deletion(-)
create mode 100644 src/util/virmdev.c
create mode 100644 src/util/virmdev.h
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
index 8be5e08730..8137223ede 100644
--- a/docs/schemas/domaincommon.rng
+++ b/docs/schemas/domaincommon.rng
@@ -4033,6 +4033,7 @@
<ref name="hostdevsubsysusb"/>
<ref name="hostdevsubsysscsi"/>
<ref name="hostdevsubsyshost"/>
+ <ref name="hostdevsubsysmdev"/>
</choice>
</define>
@@ -4183,6 +4184,20 @@
</element>
</define>
+ <define name="hostdevsubsysmdev">
+ <attribute name="type">
+ <value>mdev</value>
+ </attribute>
+ <attribute name="model">
+ <choice>
+ <value>vfio-pci</value>
+ </choice>
+ </attribute>
+ <element name="source">
+ <ref name="mdevaddress"/>
+ </element>
+ </define>
+
<define name="hostdevcapsstorage">
<attribute name="type">
<value>storage</value>
@@ -4341,6 +4356,13 @@
</attribute>
</optional>
</define>
+ <define name="mdevaddress">
+ <element name="address">
+ <attribute name="uuid">
+ <ref name="UUID"/>
+ </attribute>
+ </element>
+ </define>
<define name="devices">
<element name="devices">
<interleave>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 539d60c4e6..6c35b8cbf0 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -223,6 +223,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 b129dd712d..c60f0460b0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -188,6 +188,7 @@ UTIL_SOURCES = \
util/virvhba.c util/virvhba.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/conf/domain_conf.c b/src/conf/domain_conf.c
index 4fb70f68fb..77201925ce 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -56,6 +56,7 @@
#include "virstring.h"
#include "virnetdev.h"
#include "virhostdev.h"
+#include "virmdev.h"
#define VIR_FROM_THIS VIR_FROM_DOMAIN
@@ -650,7 +651,8 @@ VIR_ENUM_IMPL(virDomainHostdevSubsys,
VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST,
"usb",
"pci",
"scsi",
- "scsi_host")
+ "scsi_host",
+ "mdev")
VIR_ENUM_IMPL(virDomainHostdevSubsysPCIBackend,
VIR_DOMAIN_HOSTDEV_PCI_BACKEND_TYPE_LAST,
@@ -2354,6 +2356,7 @@ void virDomainHostdevDefClear(virDomainHostdevDefPtr def)
break;
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
+ case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
break;
}
@@ -6374,6 +6377,41 @@ virDomainHostdevSubsysSCSIVHostDefParseXML(xmlNodePtr sourcenode,
return ret;
}
+static int
+virDomainHostdevSubsysMediatedDevDefParseXML(virDomainHostdevDefPtr def,
+ xmlXPathContextPtr ctxt)
+{
+ int ret = -1;
+ unsigned char uuid[VIR_UUID_BUFLEN] = {0};
+ char *uuidxml = NULL;
+ xmlNodePtr node = NULL;
+ virDomainHostdevSubsysMediatedDevPtr mdevsrc = &def->source.subsys.u.mdev;
+
+ if (!(node = virXPathNode("./source/address", ctxt))) {
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+ _("Missing <address> element"));
+ goto cleanup;
+ }
+
+ if (!(uuidxml = virXMLPropString(node, "uuid"))) {
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+ _("Missing 'uuid' attribute for element
<address>"));
+ goto cleanup;
+ }
+
+ if (virUUIDParse(uuidxml, uuid) < 0) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ "%s",
+ _("Cannot parse uuid attribute of element
<address>"));
+ goto cleanup;
+ }
+
+ virUUIDFormat(uuid, mdevsrc->uuidstr);
+ ret = 0;
+ cleanup:
+ VIR_FREE(uuidxml);
+ return ret;
+}
static int
virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
@@ -6387,10 +6425,12 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
char *sgio = NULL;
char *rawio = NULL;
char *backendStr = NULL;
+ char *model = NULL;
int backend;
int ret = -1;
virDomainHostdevSubsysPCIPtr pcisrc = &def->source.subsys.u.pci;
virDomainHostdevSubsysSCSIPtr scsisrc = &def->source.subsys.u.scsi;
+ virDomainHostdevSubsysMediatedDevPtr mdevsrc = &def->source.subsys.u.mdev;
/* @managed can be read from the xml document - it is always an
* attribute of the toplevel element, no matter what type of
@@ -6404,6 +6444,7 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
sgio = virXMLPropString(node, "sgio");
rawio = virXMLPropString(node, "rawio");
+ model = virXMLPropString(node, "model");
/* @type is passed in from the caller rather than read from the
* xml document, because it is specified in different places for
@@ -6470,6 +6511,28 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
}
}
+ if (def->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV) {
+ if (model) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("model is only supported with mediated
devices"));
+ goto error;
+ }
+ } else {
+ if (!model) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("Missing 'model' attribute in mediated
device's "
+ "<hostdev> element"));
+ goto error;
+ }
+
+ if ((mdevsrc->model = virMediatedDeviceModelTypeFromString(model)) < 0) {
+ virReportError(VIR_ERR_XML_ERROR,
+ _("unknown hostdev model '%s'"),
+ model);
+ goto error;
+ }
+ }
+
switch (def->source.subsys.type) {
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
if (virDomainHostdevSubsysPCIDefParseXML(sourcenode, def, flags) < 0)
@@ -6502,6 +6565,10 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
if (virDomainHostdevSubsysSCSIVHostDefParseXML(sourcenode, def) < 0)
goto error;
break;
+ case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
+ if (virDomainHostdevSubsysMediatedDevDefParseXML(def, ctxt) < 0)
+ goto error;
+ break;
default:
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -6516,6 +6583,7 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
VIR_FREE(sgio);
VIR_FREE(rawio);
VIR_FREE(backendStr);
+ VIR_FREE(model);
return ret;
}
@@ -13361,6 +13429,7 @@ virDomainHostdevDefParseXML(virDomainXMLOptionPtr xmlopt,
}
break;
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
+ case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
break;
}
@@ -14295,6 +14364,7 @@ virDomainHostdevMatchSubsys(virDomainHostdevDefPtr a,
return 1;
else
return 0;
+ case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
return 0;
}
@@ -21296,6 +21366,7 @@ virDomainHostdevDefFormatSubsys(virBufferPtr buf,
virDomainHostdevSubsysPCIPtr pcisrc = &def->source.subsys.u.pci;
virDomainHostdevSubsysSCSIPtr scsisrc = &def->source.subsys.u.scsi;
virDomainHostdevSubsysSCSIVHostPtr hostsrc = &def->source.subsys.u.scsi_host;
+ virDomainHostdevSubsysMediatedDevPtr mdevsrc = &def->source.subsys.u.mdev;
virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
virDomainHostdevSubsysSCSIiSCSIPtr iscsisrc = &scsisrc->u.iscsi;
@@ -21400,6 +21471,10 @@ virDomainHostdevDefFormatSubsys(virBufferPtr buf,
break;
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST:
break;
+ case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
+ virBufferAsprintf(buf, "<address uuid='%s'/>\n",
+ mdevsrc->uuidstr);
+ break;
default:
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unexpected hostdev type %d"),
@@ -23295,6 +23370,7 @@ virDomainHostdevDefFormat(virBufferPtr buf,
{
const char *mode = virDomainHostdevModeTypeToString(def->mode);
virDomainHostdevSubsysSCSIPtr scsisrc = &def->source.subsys.u.scsi;
+ virDomainHostdevSubsysMediatedDevPtr mdevsrc = &def->source.subsys.u.mdev;
const char *type;
if (!mode) {
@@ -23344,6 +23420,10 @@ virDomainHostdevDefFormat(virBufferPtr buf,
virBufferAsprintf(buf, " rawio='%s'",
virTristateBoolTypeToString(scsisrc->rawio));
}
+
+ if (def->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV)
+ virBufferAsprintf(buf, " model='%s'",
+ virMediatedDeviceModelTypeToString(mdevsrc->model));
}
virBufferAddLit(buf, ">\n");
virBufferAdjustIndent(buf, 2);
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 826afb42cb..536d480ba6 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -295,6 +295,7 @@ typedef enum {
VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI,
VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI,
VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST,
+ VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV,
VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST
} virDomainHostdevSubsysType;
@@ -369,6 +370,13 @@ struct _virDomainHostdevSubsysSCSI {
} u;
};
+typedef struct _virDomainHostdevSubsysMediatedDev virDomainHostdevSubsysMediatedDev;
+typedef virDomainHostdevSubsysMediatedDev *virDomainHostdevSubsysMediatedDevPtr;
+struct _virDomainHostdevSubsysMediatedDev {
+ int model; /* enum virMediatedDeviceModelType */
+ char uuidstr[VIR_UUID_STRING_BUFLEN]; /* mediated device's uuid string */
+};
+
typedef enum {
VIR_DOMAIN_HOSTDEV_SUBSYS_SCSI_HOST_PROTOCOL_TYPE_NONE,
VIR_DOMAIN_HOSTDEV_SUBSYS_SCSI_HOST_PROTOCOL_TYPE_VHOST,
@@ -394,6 +402,7 @@ struct _virDomainHostdevSubsys {
virDomainHostdevSubsysPCI pci;
virDomainHostdevSubsysSCSI scsi;
virDomainHostdevSubsysSCSIVHost scsi_host;
+ virDomainHostdevSubsysMediatedDev mdev;
} u;
};
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 8af5454b40..5f6700b42b 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1980,6 +1980,28 @@ virMacMapNew;
virMacMapRemove;
virMacMapWriteFile;
+# util/virmdev.h
+virMediatedDeviceFree;
+virMediatedDeviceGetIOMMUGroupDev;
+virMediatedDeviceGetIOMMUGroupNum;
+virMediatedDeviceGetSysfsPath;
+virMediatedDeviceGetUsedBy;
+virMediatedDeviceIsUsed;
+virMediatedDeviceListAdd;
+virMediatedDeviceListCount;
+virMediatedDeviceListDel;
+virMediatedDeviceListFind;
+virMediatedDeviceListGet;
+virMediatedDeviceListMarkDevices;
+virMediatedDeviceListNew;
+virMediatedDeviceListSteal;
+virMediatedDeviceListStealIndex;
+virMediatedDeviceModelTypeFromString;
+virMediatedDeviceModelTypeToString;
+virMediatedDeviceNew;
+virMediatedDeviceSetUsedBy;
+
+
# util/virnetdev.h
virNetDevAddMulti;
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index c1778eaf85..11157184b7 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -7060,6 +7060,7 @@ qemuDomainGetHostdevPath(virDomainDefPtr def,
break;
}
+ case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
break;
}
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index ddcbc5e259..66c469e55e 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -3907,6 +3907,8 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST:
qemuDomainRemoveSCSIVHostDevice(driver, vm, hostdev);
break;
+ case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
+ break;
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
break;
}
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c
index 0d3e891a71..f5b72e1c2d 100644
--- a/src/security/security_apparmor.c
+++ b/src/security/security_apparmor.c
@@ -901,6 +901,9 @@ AppArmorSetSecurityHostdevLabel(virSecurityManagerPtr mgr,
break;
}
+ case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
+ break;
+
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
ret = 0;
break;
diff --git a/src/security/security_dac.c b/src/security/security_dac.c
index d6a2daf747..4e968f29c0 100644
--- a/src/security/security_dac.c
+++ b/src/security/security_dac.c
@@ -964,6 +964,7 @@ virSecurityDACSetHostdevLabel(virSecurityManagerPtr mgr,
break;
}
+ case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
ret = 0;
break;
@@ -1119,6 +1120,7 @@ virSecurityDACRestoreHostdevLabel(virSecurityManagerPtr mgr,
break;
}
+ case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
ret = 0;
break;
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index a6bcf9e01f..7b3276dc34 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -1838,6 +1838,7 @@ virSecuritySELinuxSetHostdevSubsysLabel(virSecurityManagerPtr mgr,
break;
}
+ case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
ret = 0;
break;
@@ -2065,6 +2066,7 @@ virSecuritySELinuxRestoreHostdevSubsysLabel(virSecurityManagerPtr
mgr,
break;
}
+ case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
ret = 0;
break;
diff --git a/src/util/virmdev.c b/src/util/virmdev.c
new file mode 100644
index 0000000000..d1692a982a
--- /dev/null
+++ b/src/util/virmdev.c
@@ -0,0 +1,487 @@
+/*
+ * virmdev.c: helper APIs for managing host mediated devices
+ *
+ * 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 <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"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+VIR_LOG_INIT("util.mdev");
+
+struct _virMediatedDevice {
+ char *path; /* sysfs path */
+ virMediatedDeviceModelType model;
+
+ char *used_by_drvname;
+ char *used_by_domname;
+};
+
+struct _virMediatedDeviceList {
+ virObjectLockable parent;
+
+ size_t count;
+ virMediatedDevicePtr *devs;
+};
+
+VIR_ENUM_IMPL(virMediatedDeviceModel, VIR_MDEV_MODEL_TYPE_LAST,
+ "vfio-pci")
+
+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)
+
+static int
+virMediatedDeviceGetSysfsDeviceAPI(virMediatedDevicePtr dev,
+ char **device_api)
+{
+ int ret = -1;
+ char *buf = NULL;
+ char *tmp = NULL;
+ char *file = NULL;
+
+ if (virAsprintf(&file, "%s/mdev_type/device_api", dev->path) <
0)
+ goto cleanup;
+
+ /* TODO - make this a generic method to access sysfs files for various
+ * kinds of devices
+ */
+ if (!virFileExists(file)) {
+ virReportSystemError(errno, _("Failed to read '%s'"), file);
+ goto cleanup;
+ }
+
+ if (virFileReadAll(file, 1024, &buf) < 0)
+ goto cleanup;
+
+ if ((tmp = strchr(buf, '\n')))
+ *tmp = '\0';
+
+ *device_api = buf;
+ buf = NULL;
+
+ ret = 0;
+ cleanup:
+ VIR_FREE(file);
+ VIR_FREE(buf);
+ return ret;
+}
+
+
+static int
+virMediatedDeviceCheckModel(virMediatedDevicePtr dev,
+ virMediatedDeviceModelType model)
+{
+ int ret = -1;
+ char *dev_api = NULL;
+ int actual_model;
+
+ if (virMediatedDeviceGetSysfsDeviceAPI(dev, &dev_api) < 0)
+ return -1;
+
+ /* safeguard in case we've got an older libvirt which doesn't know newer
+ * device_api models yet
+ */
+ if ((actual_model = virMediatedDeviceModelTypeFromString(dev_api)) < 0) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Device API '%s' not supported yet"),
+ dev_api);
+ goto cleanup;
+ }
+
+ if (actual_model != model) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Invalid device API '%s' for device %s: "
+ "device only supports '%s'"),
+ virMediatedDeviceModelTypeToString(model),
+ dev->path, dev_api);
+ goto cleanup;
+ }
+
+ ret = 0;
+ cleanup:
+ VIR_FREE(dev_api);
+ return ret;
+}
+
+
+#ifdef __linux__
+# define MDEV_SYSFS_DEVICES "/sys/bus/mdev/devices/"
+
+virMediatedDevicePtr
+virMediatedDeviceNew(const char *uuidstr, virMediatedDeviceModelType model)
+{
+ virMediatedDevicePtr ret = NULL;
+ virMediatedDevicePtr dev = NULL;
+
+ if (VIR_ALLOC(dev) < 0)
+ return NULL;
+
+ if (!(dev->path = virMediatedDeviceGetSysfsPath(uuidstr)))
+ goto cleanup;
+
+ /* Check whether the user-provided model corresponds with the actually
+ * supported mediated device's API.
+ */
+ if (virMediatedDeviceCheckModel(dev, model))
+ goto cleanup;
+
+ dev->model = model;
+ VIR_STEAL_PTR(ret, dev);
+
+ cleanup:
+ virMediatedDeviceFree(dev);
+ return ret;
+}
+
+#else
+
+virMediatedDevicePtr
+virMediatedDeviceNew(virPCIDeviceAddressPtr pciaddr ATTRIBUTE_UNUSED,
+ const char *uuidstr ATTRIBUTE_UNUSED)
+{
+ virRerportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("not supported on non-linux platforms"));
+ return NULL;
+}
+
+#endif /* __linux__ */
+
+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"). Caller is
responsible
+ * for freeing the result.
+ */
+char *
+virMediatedDeviceGetIOMMUGroupDev(virMediatedDevicePtr dev)
+{
+ char *resultpath = NULL;
+ char *iommu_path = NULL;
+ char *vfio_path = NULL;
+
+ if (virAsprintf(&iommu_path, "%s/iommu_group", dev->path) < 0)
+ return NULL;
+
+ if (!virFileExists(iommu_path)) {
+ virReportSystemError(errno, _("Failed to access '%s'"),
iommu_path);
+ goto cleanup;
+ }
+
+ if (virFileResolveLink(iommu_path, &resultpath) < 0) {
+ virReportSystemError(errno, _("Failed to resolve '%s'"),
iommu_path);
+ goto cleanup;
+ }
+
+ if (virAsprintf(&vfio_path, "/dev/vfio/%s", last_component(resultpath))
< 0)
+ goto cleanup;
+
+ cleanup:
+ VIR_FREE(resultpath);
+ VIR_FREE(iommu_path);
+ return vfio_path;
+}
+
+
+int
+virMediatedDeviceGetIOMMUGroupNum(virMediatedDevicePtr dev)
+{
+ char *vfio_path = NULL;
+ char *group_num_str = NULL;
+ unsigned int group_num = -1;
+
+ if (!(vfio_path = virMediatedDeviceGetIOMMUGroupDev(dev)))
+ return -1;
+
+ group_num_str = last_component(vfio_path);
+ ignore_value(virStrToLong_ui(group_num_str, NULL, 10, &group_num));
+
+ VIR_FREE(vfio_path);
+ return group_num;
+}
+
+
+void
+virMediatedDeviceGetUsedBy(virMediatedDevicePtr dev,
+ const char **drvname, const 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;
+}
+
+
+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,
+ ssize_t idx)
+{
+ if (idx < 0 || idx >= list->count)
+ return NULL;
+
+ return list->devs[idx];
+}
+
+
+size_t
+virMediatedDeviceListCount(virMediatedDeviceListPtr list)
+{
+ return list->count;
+}
+
+
+virMediatedDevicePtr
+virMediatedDeviceListStealIndex(virMediatedDeviceListPtr list,
+ ssize_t 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;
+}
+
+
+bool
+virMediatedDeviceIsUsed(virMediatedDevicePtr dev,
+ virMediatedDeviceListPtr list)
+{
+ const char *drvname, *domname;
+ virMediatedDevicePtr tmp = NULL;
+
+ if ((tmp = virMediatedDeviceListFind(list, dev))) {
+ virMediatedDeviceGetUsedBy(tmp, &drvname, &domname);
+ virReportError(VIR_ERR_OPERATION_INVALID,
+ _("Mediated device %s is in use by "
+ "driver %s, domain %s"),
+ tmp->path, drvname, domname);
+ }
+
+ return !!tmp;
+}
+
+
+char *
+virMediatedDeviceGetSysfsPath(const char *uuidstr)
+{
+ char *ret = NULL;
+
+ ignore_value(virAsprintf(&ret, MDEV_SYSFS_DEVICES "%s", uuidstr));
+ return ret;
+}
+
+
+int
+virMediatedDeviceListMarkDevices(virMediatedDeviceListPtr dst,
+ virMediatedDeviceListPtr src,
+ const char *drvname,
+ const char *domname)
+{
+ int ret = -1;
+ size_t count = virMediatedDeviceListCount(src);
+ size_t i, j;
+
+ virObjectLock(dst);
+ for (i = 0; i < count; i++) {
+ virMediatedDevicePtr mdev = virMediatedDeviceListGet(src, i);
+
+ if (virMediatedDeviceIsUsed(mdev, dst) ||
+ virMediatedDeviceSetUsedBy(mdev, drvname, domname) < 0)
+ goto cleanup;
+
+ /* Copy mdev references to the driver list:
+ * - caller is responsible for NOT freeing devices in @list on success
+ * - we're responsible for performing a rollback on failure
+ */
+ if (virMediatedDeviceListAdd(dst, mdev) < 0)
+ goto rollback;
+
+ VIR_DEBUG("'%s' added to list of active mediated devices used by
'%s'",
+ mdev->path, domname);
+ }
+
+ ret = 0;
+ cleanup:
+ virObjectUnlock(dst);
+ return ret;
+
+ rollback:
+ for (j = 0; j < i; j++) {
+ virMediatedDevicePtr tmp = virMediatedDeviceListGet(src, j);
+ virMediatedDeviceListSteal(dst, tmp);
+ }
+ goto cleanup;
+}
diff --git a/src/util/virmdev.h b/src/util/virmdev.h
new file mode 100644
index 0000000000..2f3d6bb840
--- /dev/null
+++ b/src/util/virmdev.h
@@ -0,0 +1,123 @@
+/*
+ * virmdev.h: helper APIs for managing host mediated devices
+ *
+ * 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/>.
+ */
+
+#ifndef __VIR_MDEV_H__
+# define __VIR_MDEV_H__
+
+# include "internal.h"
+# include "virobject.h"
+# include "virutil.h"
+# include "virpci.h"
+
+typedef enum {
+ VIR_MDEV_MODEL_TYPE_VFIO_PCI = 0,
+
+ VIR_MDEV_MODEL_TYPE_LAST
+} virMediatedDeviceModelType;
+
+VIR_ENUM_DECL(virMediatedDeviceModel)
+
+
+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(const char *uuidstr, virMediatedDeviceModelType model);
+
+virMediatedDevicePtr
+virMediatedDeviceCopy(virMediatedDevicePtr dev);
+
+void
+virMediatedDeviceFree(virMediatedDevicePtr dev);
+
+const char *
+virMediatedDeviceGetPath(virMediatedDevicePtr dev);
+
+void
+virMediatedDeviceGetUsedBy(virMediatedDevicePtr dev,
+ const char **drvname, const char **domname);
+
+int
+virMediatedDeviceSetUsedBy(virMediatedDevicePtr dev,
+ const char *drvname,
+ const char *domname);
+
+char *
+virMediatedDeviceGetIOMMUGroupDev(virMediatedDevicePtr dev);
+
+int
+virMediatedDeviceGetIOMMUGroupNum(virMediatedDevicePtr dev);
+
+char *
+virMediatedDeviceGetSysfsPath(const char *uuidstr);
+
+bool
+virMediatedDeviceIsUsed(virMediatedDevicePtr dev,
+ virMediatedDeviceListPtr list);
+
+bool
+virMediatedDeviceIsUsed(virMediatedDevicePtr dev,
+ virMediatedDeviceListPtr list);
+
+virMediatedDeviceListPtr
+virMediatedDeviceListNew(void);
+
+int
+virMediatedDeviceListAdd(virMediatedDeviceListPtr list,
+ virMediatedDevicePtr dev);
+
+virMediatedDevicePtr
+virMediatedDeviceListGet(virMediatedDeviceListPtr list,
+ ssize_t idx);
+
+size_t
+virMediatedDeviceListCount(virMediatedDeviceListPtr list);
+
+virMediatedDevicePtr
+virMediatedDeviceListSteal(virMediatedDeviceListPtr list,
+ virMediatedDevicePtr dev);
+
+virMediatedDevicePtr
+virMediatedDeviceListStealIndex(virMediatedDeviceListPtr list,
+ ssize_t idx);
+
+void
+virMediatedDeviceListDel(virMediatedDeviceListPtr list,
+ virMediatedDevicePtr dev);
+
+virMediatedDevicePtr
+virMediatedDeviceListFind(virMediatedDeviceListPtr list,
+ virMediatedDevicePtr dev);
+
+int
+virMediatedDeviceListFindIndex(virMediatedDeviceListPtr list,
+ virMediatedDevicePtr dev);
+
+int
+virMediatedDeviceListMarkDevices(virMediatedDeviceListPtr dst,
+ virMediatedDeviceListPtr src,
+ const char *drvname,
+ const char *domname);
+#endif /* __VIR_MDEV_H__ */
diff --git a/tests/domaincapsschemadata/full.xml b/tests/domaincapsschemadata/full.xml
index 6a676253c3..82a92322e5 100644
--- a/tests/domaincapsschemadata/full.xml
+++ b/tests/domaincapsschemadata/full.xml
@@ -89,6 +89,7 @@
<value>pci</value>
<value>scsi</value>
<value>scsi_host</value>
+ <value>mdev</value>
</enum>
<enum name='capsType'>
<value>storage</value>
--
2.12.0