Martin Polednik has uploaded a new change for review.

Change subject: vdsm hostdev: add support for USB devices
......................................................................

vdsm hostdev: add support for USB devices

Libvirt allows passthrough of USB devices (not busses) - this patch
exposes the functionality in vdsm

Change-Id: Iac74e7537d56bcb940ef07a4654d45cbcdbb1fb0
Signed-off-by: Martin Polednik <mpole...@redhat.com>
---
M vdsm/caps.py
M vdsm/rpc/vdsmapi-schema.json
M vdsm/virt/vm.py
3 files changed, 71 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/54/29054/1

diff --git a/vdsm/caps.py b/vdsm/caps.py
index d937d4e..8c16af6 100644
--- a/vdsm/caps.py
+++ b/vdsm/caps.py
@@ -530,7 +530,12 @@
             # back that we could use to uniquely identify and initiate a device
             continue
 
-        if capability in ('pci',):
+        if capability in ('pci', 'usb_device'):
+            # Libvirt only allows to attach USB device with capability 'usb',
+            # but the bus identifies itself as 'usb' while device as
+            # 'usb_device'
+            if dev['capability'] == 'usb_device':
+                dev['capability'] = 'usb'
             devices.append(dev)
 
     return devices
diff --git a/vdsm/rpc/vdsmapi-schema.json b/vdsm/rpc/vdsmapi-schema.json
index 921f3d4..ac10144 100644
--- a/vdsm/rpc/vdsmapi-schema.json
+++ b/vdsm/rpc/vdsmapi-schema.json
@@ -3156,33 +3156,55 @@
           'specParams': 'VmRngDeviceSpecParams'}}
 
 ##
+# @StartupPolicy:
+#
+# Possible policies for startup with device
+#
+# @mandatory:   fail if missing for any reason (the default) 
+#
+# @requisite:   fail if missing on boot up, drop if missing
+#               on migrate/restore/revert
+#
+# @optional:    drop if missing at any start attempt 
+#
+# Since: 4.16.0
+##
+{'enum': 'StartupPolicy', 'data': ['mandatory', 'requisite', 'optional']}
+
+##
 # @HostDeviceCapability:
 #
 # Properties of a host device.
 #
 # @pci:         PCI device
 #
+# @usb:         USB device
+#
 # Since: 4.16.0
 ##
-{'enum': 'HostDeviceCapability', 'data': ['pci']}
+{'enum': 'HostDeviceCapability', 'data': ['pci', 'usb']}
 
 ##
 # @HostDeviceSpecParams:
 #
 # Properties of a host device.
 #
-# @bootorder:   #optional If specified, this device is part of the boot
-#               sequence at the specified position
+# @bootorder:       #optional If specified, this device is part of the boot
+#                   sequence at the specified position (for @pci and @usb)
 #
-# @bar:         #optional ROM visibility in the guest's memory map (for @pci)
+# @bar:             #optional ROM visibility in the guest's
+#                   memory map (for @pci)
 #
-# @file:        #optional Binary file to be used as device's ROM (for @pci)
+# @file:            #optional Binary file to be used as device's ROM (for @pci)
+#
+# @startupPolicy:   #optional Possible boot handling with attached device
+#                   (for @usb)
 #
 # Since: 4.16.0
 ##
 {'type': 'HostDeviceSpecParams',
- 'data': {'*bootorder': 'int', '*bar': 'bool',
-          '*file': 'str'}}
+ 'data': {'*bootorder': 'int', '*bar': 'bool', '*file': 'str',
+          '*startupPolicy': 'StartupPolicy'}}
 
 ##
 # @HostDevice:
diff --git a/vdsm/virt/vm.py b/vdsm/virt/vm.py
index 6143cfa..6a6978e 100644
--- a/vdsm/virt/vm.py
+++ b/vdsm/virt/vm.py
@@ -1631,13 +1631,16 @@
         self.log.debug('Detaching hostdev %s', self.name)
         self._node.dettach()
 
-    def getPciAddr(self):
+    def _parsecaps(self):
         capsxml = _domParseStr(self._node.XMLDesc(0)).childNodes[0]
 
         self.log.debug('Got nodeDevice XML:\n%s',
                        capsxml.toprettyxml(encoding='utf-8'))
 
-        capsxml = capsxml.getElementsByTagName('capability')[0]
+        return capsxml.getElementsByTagName('capability')[0]
+
+    def getPciAddr(self):
+        capsxml = self._parsecaps()
         domain = capsxml.getElementsByTagName('domain')[0]. \
             firstChild.nodeValue
         bus = capsxml.getElementsByTagName('bus')[0].firstChild.nodeValue
@@ -1646,6 +1649,26 @@
         self.log.debug('PCI device %s at address {domain: %s bus: %s '
                        'slot: %s}', self.name, domain, bus, slot)
         return {'domain': domain, 'bus': bus, 'slot': slot}
+
+    def getUsbAddr(self):
+        capsxml = self._parsecaps()
+        addr = {}
+
+        addr['bus'] = capsxml.getElementsByTagName('bus')[0].firstChild. \
+            nodeValue
+        addr['device'] = capsxml.getElementsByTagName('device')[0]. \
+            firstChild.nodeValue
+        # TODO: handle nonexistant product_id and vendor_id by not adding them
+        # to addr
+        addr['product_id'] = capsxml.getElementsByTagName('product')[0].\
+            getAttribute('id')
+        addr['vendor_id'] = capsxml.getElementsByTagName('vendor')[0].\
+            getAttribute('id')
+
+        self.log.debug('USB device %s {product: %s, vendor: %s} at address '
+                       '{bus: %s device: %s}', self.name, addr['product_id'],
+                       addr['vendor_id'], addr['bus'], addr['device'])
+        return addr
 
     def getXML(self):
         """
@@ -1683,6 +1706,17 @@
 
             rom.setAttrs(**romAttrs)
 
+        elif self.capability == 'usb_device':
+            addr = self.getPciAddr()
+            try:
+                source.appendChildWithArgs('vendor', None, addr['vendor'])
+                source.appendChildWithArgs('vendor', None, addr['product'])
+            except:
+                source.appendChildWithArgs('address', None, **addr)
+
+            if 'startupPolicy' in self.specParams:
+                source.setAttrs(startupPolicy=self.specParams['startupPolicy'])
+
         return hostdev
 
 


-- 
To view, visit http://gerrit.ovirt.org/29054
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iac74e7537d56bcb940ef07a4654d45cbcdbb1fb0
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Martin Polednik <mpole...@redhat.com>
_______________________________________________
vdsm-patches mailing list
vdsm-patches@lists.fedorahosted.org
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to