From aae0fc2a922b3e31dae7648c547fca2ac2587625 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Fri, 23 Nov 2012 13:50:29 +0000 Subject: [PATCH] Add support for The device type has long had a redundant "mode" attribute, which has always been "subsys". This finally introduces a new mode "capabilities", which will be used by the LXC driver for device assignment. Since container based virtualization uses a single kernel, the idea of assigning physical PCI devices doesn't make sense. It is still reasonable to assign USB devices, but for assigning arbitrary nodes in /dev, the new 'capabilities' mode is to be used. The first capability support is 'storage', which is for assignment of block devices. Functionally this is really pretty similar to the support. The only difference is the device node name is identical in both host and container namespaces. /dev/sdf1 The second capability support is 'misc', which is for assignment of character devices. There is no existing parallel to this. Again the device node is the same inside & outside the container. /dev/input/event3 The reason for keeping the char & storage devices separate in the domain XML, is to mirror the split in the node device XML. NB the node device XML does not yet report character devices, but that's another new patch to come Signed-off-by: Daniel P. Berrange --- docs/formatdomain.html.in | 55 ++++++++- docs/schemas/domaincommon.rng | 150 ++++++++++++++++------- src/conf/domain_audit.c | 92 ++++++++++---- src/conf/domain_conf.c | 173 ++++++++++++++++++++++++++- src/conf/domain_conf.h | 31 ++++- src/libvirt_private.syms | 1 + tests/lxcxml2xmldata/lxc-hostdev.xml | 34 ++++++ tests/lxcxml2xmltest.c | 1 + 8 files changed, 455 insertions(+), 82 deletions(-) create mode 100644 tests/lxcxml2xmldata/lxc-hostdev.xml diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in index 292e032a66..8d9ab9e424 100644 --- a/docs/formatdomain.html.in +++ b/docs/formatdomain.html.in @@ -2123,13 +2123,15 @@ -

USB and PCI devices

+

Host device assignment

+ +
USB / PCI devices

USB and PCI devices attached to the host can be passed through - to the guest using - the hostdev element. since after - 0.4.4 for USB and 0.6.0 for PCI (KVM only): + to the guest using the hostdev element. + since after 0.4.4 for USB and 0.6.0 for PCI + (KVM only):

@@ -2247,6 +2249,51 @@
       more details on the address element.
     
 
+
+    
Block / character devices
+ +

+ Block / character devices from the host can be passed through + to the guest using the hostdev element. This is + only possible with container based virtualization. + since after 1.0.1 for LXC: +

+ +
+...
+<hostdev mode='capabilities' type='storage'>
+  <source>
+    <block>/dev/sdf1</block>
+  </source>
+</hostdev>
+...
+    
+ +
+...
+<hostdev mode='capabilities' type='misc'>
+  <source>
+    <char>/dev/input/event3</char>
+  </source>
+</hostdev>
+...
+    
+ +
+
hostdev
+
The hostdev element is the main container for describing + host devices. For block/character device passthrough mode is + always "capabilities" and type is "block" for a block + device and "char" for a character device. +
+
source
+
The source element describes the device as seen from the host. + For block devices, the path to the block device in the host + OS is provided in the nested "block" element, while for character + devices the "char" element is used +
+
+

Redirected devices

diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng index cdc511568e..0529d627e1 100644 --- a/docs/schemas/domaincommon.rng +++ b/docs/schemas/domaincommon.rng @@ -2834,63 +2834,123 @@ + - - - - subsystem - capabilities - - - - - - usb - pci - - - - - - yes - no - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + - + + + + + + + + + subsystem + + + + + + yes + no + + + + + + + + + + + + capabilities + + + + + + + + + + + + + + + pci + + + + + + + + + + + + + + usb + + + + + + + + + + + + + + + + + + + + storage + + + + + + + + + + + misc + + + + + + + + diff --git a/src/conf/domain_audit.c b/src/conf/domain_audit.c index 939d2139d6..97b3aa41f4 100644 --- a/src/conf/domain_audit.c +++ b/src/conf/domain_audit.c @@ -260,42 +260,84 @@ virDomainAuditHostdev(virDomainObjPtr vm, virDomainHostdevDefPtr hostdev, virt = "?"; } - switch (hostdev->source.subsys.type) { - case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: - if (virAsprintf(&address, "%.4x:%.2x:%.2x.%.1x", - hostdev->source.subsys.u.pci.domain, - hostdev->source.subsys.u.pci.bus, - hostdev->source.subsys.u.pci.slot, - hostdev->source.subsys.u.pci.function) < 0) { + switch (hostdev->mode) { + case VIR_DOMAIN_HOSTDEV_MODE_SUBSYS: + switch (hostdev->source.subsys.type) { + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: + if (virAsprintf(&address, "%.4x:%.2x:%.2x.%.1x", + hostdev->source.subsys.u.pci.domain, + hostdev->source.subsys.u.pci.bus, + hostdev->source.subsys.u.pci.slot, + hostdev->source.subsys.u.pci.function) < 0) { + VIR_WARN("OOM while encoding audit message"); + goto cleanup; + } + break; + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: + if (virAsprintf(&address, "%.3d.%.3d", + hostdev->source.subsys.u.usb.bus, + hostdev->source.subsys.u.usb.device) < 0) { + VIR_WARN("OOM while encoding audit message"); + goto cleanup; + } + break; + default: + VIR_WARN("Unexpected hostdev type while encoding audit message: %d", + hostdev->source.subsys.type); + goto cleanup; + } + + if (!(device = virAuditEncode("device", VIR_AUDIT_STR(address)))) { VIR_WARN("OOM while encoding audit message"); goto cleanup; } + + VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success, + "virt=%s resrc=dev reason=%s %s uuid=%s bus=%s %s", + virt, reason, vmname, uuidstr, + virDomainHostdevSubsysTypeToString(hostdev->source.subsys.type), + device); break; - case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: - if (virAsprintf(&address, "%.3d.%.3d", - hostdev->source.subsys.u.usb.bus, - hostdev->source.subsys.u.usb.device) < 0) { - VIR_WARN("OOM while encoding audit message"); + + case VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES: + switch (hostdev->source.caps.type) { + case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE: + if (!(device = virAuditEncode("disk", + VIR_AUDIT_STR(hostdev->source.caps.u.storage.block)))) { + VIR_WARN("OOM while encoding audit message"); + goto cleanup; + } + + VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success, + "virt=%s resrc=hostdev reason=%s %s uuid=%s %s", + virt, reason, vmname, uuidstr, device); + break; + + case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC: + if (!(device = virAuditEncode("chardev", + VIR_AUDIT_STR(hostdev->source.caps.u.misc.chardev)))) { + VIR_WARN("OOM while encoding audit message"); + goto cleanup; + } + + VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success, + "virt=%s resrc=hostdev reason=%s %s uuid=%s %s", + virt, reason, vmname, uuidstr, device); + break; + + default: + VIR_WARN("Unexpected hostdev type while encoding audit message: %d", + hostdev->source.caps.type); goto cleanup; } break; + default: - VIR_WARN("Unexpected hostdev type while encoding audit message: %d", - hostdev->source.subsys.type); + VIR_WARN("Unexpected hostdev mode while encoding audit message: %d", + hostdev->mode); goto cleanup; } - if (!(device = virAuditEncode("device", VIR_AUDIT_STR(address)))) { - VIR_WARN("OOM while encoding audit message"); - goto cleanup; - } - - VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success, - "virt=%s resrc=dev reason=%s %s uuid=%s bus=%s %s", - virt, reason, vmname, uuidstr, - virDomainHostdevSubsysTypeToString(hostdev->source.subsys.type), - device); - cleanup: VIR_FREE(vmname); VIR_FREE(device); diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 19af0584f4..6a7646ed24 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -536,6 +536,10 @@ VIR_ENUM_IMPL(virDomainHostdevSubsys, VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST, "usb", "pci") +VIR_ENUM_IMPL(virDomainHostdevCaps, VIR_DOMAIN_HOSTDEV_CAPS_TYPE_LAST, + "storage", + "misc") + VIR_ENUM_IMPL(virDomainPciRombarMode, VIR_DOMAIN_PCI_ROMBAR_LAST, "default", @@ -1442,6 +1446,17 @@ void virDomainHostdevDefClear(virDomainHostdevDefPtr def) */ if (def->parent.type == VIR_DOMAIN_DEVICE_NONE) virDomainDeviceInfoFree(def->info); + + if (def->mode == VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES) { + switch (def->source.caps.type) { + case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE: + VIR_FREE(def->source.caps.u.storage.block); + break; + case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC: + VIR_FREE(def->source.caps.u.misc.chardev); + break; + } + } } void virDomainHostdevDefFree(virDomainHostdevDefPtr def) @@ -3028,6 +3043,71 @@ error: return ret; } +static int +virDomainHostdevDefParseXMLCaps(xmlNodePtr node ATTRIBUTE_UNUSED, + xmlXPathContextPtr ctxt, + const char *type, + virDomainHostdevDefPtr def) +{ + xmlNodePtr sourcenode; + int ret = -1; + + /* @type is passed in from the caller rather than read from the + * xml document, because it is specified in different places for + * different kinds of defs - it is an attribute of + * /

for an intelligent hostdev (), + * but an attribute of the toplevel element for a standard + * . (the functions we're going to call expect address + * type to already be known). + */ + if (type) { + if ((def->source.caps.type + = virDomainHostdevCapsTypeFromString(type)) < 0) { + virReportError(VIR_ERR_XML_ERROR, + _("unknown host device source address type '%s'"), + type); + goto error; + } + } else { + virReportError(VIR_ERR_XML_ERROR, + "%s", _("missing source address type")); + goto error; + } + + if (!(sourcenode = virXPathNode("./source", ctxt))) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Missing element in hostdev device")); + goto error; + } + + switch (def->source.caps.type) { + case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE: + if (!(def->source.caps.u.storage.block = + virXPathString("string(./source/block[1])", ctxt))) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Missing element in hostdev storage device")); + goto error; + } + break; + case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC: + if (!(def->source.caps.u.misc.chardev = + virXPathString("string(./source/char[1])", ctxt))) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Missing element in hostdev character device")); + goto error; + } + break; + default: + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("address type='%s' not supported in hostdev interfaces"), + virDomainHostdevCapsTypeToString(def->source.caps.type)); + goto error; + } + ret = 0; +error: + return ret; +} + int virDomainDiskFindControllerModel(virDomainDefPtr def, virDomainDiskDefPtr disk, @@ -7384,6 +7464,11 @@ virDomainHostdevDefParseXML(const xmlNodePtr node, if (virDomainHostdevDefParseXMLSubsys(node, ctxt, type, def, flags) < 0) goto error; break; + case VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES: + /* parse managed/mode/type, and the element */ + if (virDomainHostdevDefParseXMLCaps(node, ctxt, type, def) < 0) + goto error; + break; default: virReportError(VIR_ERR_INTERNAL_ERROR, _("Unexpected hostdev mode %d"), def->mode); @@ -7940,6 +8025,41 @@ virDomainHostdevMatchSubsys(virDomainHostdevDefPtr a, } +static int +virDomainHostdevMatchCapsStorage(virDomainHostdevDefPtr a, + virDomainHostdevDefPtr b) +{ + return STREQ_NULLABLE(a->source.caps.u.storage.block, + b->source.caps.u.storage.block); +} + + +static int +virDomainHostdevMatchCapsMisc(virDomainHostdevDefPtr a, + virDomainHostdevDefPtr b) +{ + return STREQ_NULLABLE(a->source.caps.u.misc.chardev, + b->source.caps.u.misc.chardev); +} + + +static int +virDomainHostdevMatchCaps(virDomainHostdevDefPtr a, + virDomainHostdevDefPtr b) +{ + if (a->source.caps.type != b->source.caps.type) + return 0; + + switch (a->source.caps.type) { + case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE: + return virDomainHostdevMatchCapsStorage(a, b); + case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC: + return virDomainHostdevMatchCapsMisc(a, b); + } + return 0; +} + + static int virDomainHostdevMatch(virDomainHostdevDefPtr a, virDomainHostdevDefPtr b) @@ -7950,6 +8070,8 @@ virDomainHostdevMatch(virDomainHostdevDefPtr a, switch (a->mode) { case VIR_DOMAIN_HOSTDEV_MODE_SUBSYS: return virDomainHostdevMatchSubsys(a, b); + case VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES: + return virDomainHostdevMatchCaps(a, b); } return 0; } @@ -12516,6 +12638,35 @@ virDomainHostdevDefFormatSubsys(virBufferPtr buf, return 0; } +static int +virDomainHostdevDefFormatCaps(virBufferPtr buf, + virDomainHostdevDefPtr def) +{ + virBufferAddLit(buf, "\n"); + + virBufferAdjustIndent(buf, 2); + switch (def->source.caps.type) + { + case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE: + virBufferEscapeString(buf, "%s\n", + def->source.caps.u.storage.block); + break; + case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC: + virBufferEscapeString(buf, "%s\n", + def->source.caps.u.misc.chardev); + break; + default: + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unexpected hostdev type %d"), + def->source.caps.type); + return -1; + } + + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "\n"); + return 0; +} + static int virDomainActualNetDefFormat(virBufferPtr buf, virDomainActualNetDefPtr def, @@ -13588,14 +13739,28 @@ virDomainHostdevDefFormat(virBufferPtr buf, return -1; } break; + case VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES: + type = virDomainHostdevCapsTypeToString(def->source.caps.type); + if (!type) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unexpected hostdev type %d"), + def->source.caps.type); + return -1; + } + break; default: virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected hostdev mode %d"), def->mode); return -1; } - virBufferAsprintf(buf, " \n", - mode, type, def->managed ? "yes" : "no"); + virBufferAsprintf(buf, " mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) + virBufferAsprintf(buf, " managed='%s'>\n", + def->managed ? "yes" : "no"); + else + virBufferAddLit(buf, ">\n"); virBufferAdjustIndent(buf, 6); switch (def->mode) { @@ -13603,6 +13768,10 @@ virDomainHostdevDefFormat(virBufferPtr buf, if (virDomainHostdevDefFormatSubsys(buf, def, flags, false) < 0) return -1; break; + case VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES: + if (virDomainHostdevDefFormatCaps(buf, def) < 0) + return -1; + break; } virBufferAdjustIndent(buf, -6); diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index bc9ef88864..5062e07f05 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -383,6 +383,29 @@ struct _virDomainHostdevSubsys { } u; }; + +enum virDomainHostdevCapsType { + VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE, + VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC, + + VIR_DOMAIN_HOSTDEV_CAPS_TYPE_LAST +}; + +typedef struct _virDomainHostdevCaps virDomainHostdevCaps; +typedef virDomainHostdevCaps *virDomainHostdevCapsPtr; +struct _virDomainHostdevCaps { + int type; /* enum virDOmainHostdevCapsType */ + union { + struct { + char *block; + } storage; + struct { + char *chardev; + } misc; + } u; +}; + + /* basic device for direct passthrough */ struct _virDomainHostdevDef { virDomainDeviceDef parent; /* higher level Def containing this */ @@ -392,12 +415,7 @@ struct _virDomainHostdevDef { unsigned int missing : 1; union { virDomainHostdevSubsys subsys; - struct { - /* TBD: struct capabilities see: - * https://www.redhat.com/archives/libvir-list/2008-July/msg00429.html - */ - int dummy; - } caps; + virDomainHostdevCaps caps; } source; virDomainHostdevOrigStates origstates; virDomainDeviceInfoPtr info; /* Guest address */ @@ -2246,6 +2264,7 @@ VIR_ENUM_DECL(virDomainWatchdogAction) VIR_ENUM_DECL(virDomainVideo) VIR_ENUM_DECL(virDomainHostdevMode) VIR_ENUM_DECL(virDomainHostdevSubsys) +VIR_ENUM_DECL(virDomainHostdevCaps) VIR_ENUM_DECL(virDomainPciRombarMode) VIR_ENUM_DECL(virDomainHub) VIR_ENUM_DECL(virDomainRedirdevBus) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 5907d1d54e..4ec19a0e8a 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -412,6 +412,7 @@ virDomainGraphicsSpiceZlibCompressionTypeToString; virDomainGraphicsTypeFromString; virDomainGraphicsTypeToString; virDomainHasDiskMirror; +virDomainHostdevCapsTypeToString; virDomainHostdevDefAlloc; virDomainHostdevDefClear; virDomainHostdevDefFree; diff --git a/tests/lxcxml2xmldata/lxc-hostdev.xml b/tests/lxcxml2xmldata/lxc-hostdev.xml new file mode 100644 index 0000000000..02a87a7b13 --- /dev/null +++ b/tests/lxcxml2xmldata/lxc-hostdev.xml @@ -0,0 +1,34 @@ + + demo + 8369f1ac-7e46-e869-4ca5-759d51478066 + 500000 + 500000 + 1 + + exe + /bin/sh + + + destroy + restart + destroy + + + + + + + + + + + /dev/sdf1 + + + + + /dev/tty0 + + + + diff --git a/tests/lxcxml2xmltest.c b/tests/lxcxml2xmltest.c index 6a879393dc..8694ca20f3 100644 --- a/tests/lxcxml2xmltest.c +++ b/tests/lxcxml2xmltest.c @@ -121,6 +121,7 @@ mymain(void) setenv("PATH", "/bin", 1); DO_TEST("systemd"); + DO_TEST("hostdev"); virCapabilitiesFree(caps);