Change in vdsm[master]: hostdev: add support for usb devices

mpolednik at redhat.com mpolednik at redhat.com
Thu Apr 9 10:30:44 UTC 2015


Martin Polednik has uploaded a new change for review.

Change subject: hostdev: add support for usb devices
......................................................................

hostdev: add support for usb devices

In order to support USB passthrough, we only have to pass usb hostdev
compilant XML. Detaching and reattaching is not needed, and was
implemented before due to documentation bug in libvirt.

Change-Id: Ibf9b4f302353f3006e1f945dd342d351039ba387
Signed-off-by: Martin Polednik <mpolednik at redhat.com>
---
M vdsm/hostdev.py
M vdsm/virt/vm.py
M vdsm/virt/vmdevices/hostdevice.py
3 files changed, 41 insertions(+), 22 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/15/39715/1

diff --git a/vdsm/hostdev.py b/vdsm/hostdev.py
index 7713346..437df13 100644
--- a/vdsm/hostdev.py
+++ b/vdsm/hostdev.py
@@ -24,13 +24,24 @@
 from vdsm import libvirtconnection
 import supervdsm
 
-_DETACH_REQUIRING_CAPS = ('usb_device', 'pci')
-_UDEV_REQUIRING_CAPS = ('pci')
+
+CAPABILITY_TO_XML_ATTR = {'pci': 'pci',
+                          'scsi': 'scsi',
+                          'usb_device': 'usb'}
+
+
+class Capability:
+    USB = 'usb_device'
+    PCI = 'pci'
 
 
 def _name_to_pci_path(device_name):
     return device_name[4:].replace('_', '.').replace('.', ':', 2)
 
+
+def address_to_name(device_type, address):
+    return {'pci': pci_address_to_name,
+            'usb': usb_address_to_name}[device_type](**address)
 
 def pci_address_to_name(domain, bus, slot, function):
     """
@@ -42,6 +53,15 @@
                                         bus[2:],
                                         slot[2:],
                                         function[2:])
+
+
+def usb_address_to_name(bus, device):
+    """
+    Convert 4 attributes that identify the pci device on the bus to
+    libvirt's pci name: pci_${domain}_${bus}_${slot}_${function}.
+    The first 2 characters are hex notation that is unwanted in the name.
+    """
+    return 'usb_usb{0}'.format(bus)
 
 
 def _sriov_totalvfs(device_name):
@@ -169,12 +189,9 @@
 def detach_detachable(device_name):
     libvirt_device, device_params = _get_device_ref_and_params(device_name)
 
-    iommu_group = device_params['iommu_group']
-
-    if device_params['capability'] in _UDEV_REQUIRING_CAPS:
+    if device_params['capability'] in Capability.PCI:
+        iommu_group = device_params['iommu_group']
         supervdsm.getProxy().appropriateIommuGroup(iommu_group)
-
-    if device_params['capability'] in _DETACH_REQUIRING_CAPS:
         libvirt_device.detachFlags(None)
 
     return device_params
@@ -183,12 +200,9 @@
 def reattach_detachable(device_name):
     libvirt_device, device_params = _get_device_ref_and_params(device_name)
 
-    iommu_group = device_params['iommu_group']
-
-    if device_params['capability'] in _UDEV_REQUIRING_CAPS:
+    if device_params['capability'] in Capability.PCI:
+        iommu_group = device_params['iommu_group']
         supervdsm.getProxy().rmAppropriateIommuGroup(iommu_group)
-
-    if device_params['capability'] in _DETACH_REQUIRING_CAPS:
         libvirt_device.reAttach()
 
 
diff --git a/vdsm/virt/vm.py b/vdsm/virt/vm.py
index f9bf4a1..183012f 100644
--- a/vdsm/virt/vm.py
+++ b/vdsm/virt/vm.py
@@ -4190,17 +4190,21 @@
         Obtain host device info from libvirt
         """
         for x in self._domain.get_device_elements('hostdev'):
+            device_type = x.getAttribute('type')
             alias = x.getElementsByTagName('alias')[0].getAttribute('name')
             address = self._getUnderlyingDeviceAddress(x)
             source = x.getElementsByTagName('source')[0]
-            device = hostdev.pci_address_to_name(
-                **self._getUnderlyingDeviceAddress(source))
+            device = hostdev.address_to_name(
+                device_type, self._getUnderlyingDeviceAddress(source))
 
             # We can assume the device name to be correct since we're
             # inspecting source element. For the address, we may look at
             # both addresses and determine the correct one.
-            if (hostdev.pci_address_to_name(**address) == device):
-                address = self._getUnderlyingDeviceAddress(x, 1)
+            if hostdev.address_to_name(device_type, address) == device:
+                try:
+                    address = self._getUnderlyingDeviceAddress(x, 1)
+                except IndexError:
+                    pass
 
             known_device = False
             for dev in self.conf['devices']:
diff --git a/vdsm/virt/vmdevices/hostdevice.py b/vdsm/virt/vmdevices/hostdevice.py
index 24dc102..eb43d6e 100644
--- a/vdsm/virt/vmdevices/hostdevice.py
+++ b/vdsm/virt/vmdevices/hostdevice.py
@@ -18,7 +18,8 @@
 # Refer to the README and COPYING files for full details of the license
 #
 
-from hostdev import get_device_params, detach_detachable
+from hostdev import get_device_params, detach_detachable, \
+    CAPABILITY_TO_XML_ATTR
 from . import core
 from . import hwclass
 
@@ -54,17 +55,17 @@
         </devices>
         """
         hostdev = self.createXmlElem(hwclass.HOSTDEV, None)
-        hostdev.setAttrs(managed='no', mode='subsystem',
-                         type=self._deviceParams['capability'])
+        hostdev.setAttrs(
+            managed='no', mode='subsystem',
+            type=CAPABILITY_TO_XML_ATTR[self._deviceParams['capability']])
         source = hostdev.appendChildWithArgs('source')
 
         if hasattr(self, 'bootOrder'):
             hostdev.appendChildWithArgs(
                 'boot', order=self.bootOrder)
 
-        if self._deviceParams['capability'] == 'pci':
-            source.appendChildWithArgs('address', None,
-                                       **self._deviceParams['address'])
+        source.appendChildWithArgs('address', None,
+                                    **self._deviceParams['address'])
 
         if hasattr(self, 'address'):
             hostdev.appendChildWithArgs('address', None, **self.address)


-- 
To view, visit https://gerrit.ovirt.org/39715
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibf9b4f302353f3006e1f945dd342d351039ba387
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Martin Polednik <mpolednik at redhat.com>


More information about the vdsm-patches mailing list