Martin Polednik has uploaded a new change for review.

Change subject: hostdev: use libvirt flags to select capability
......................................................................

hostdev: use libvirt flags to select capability

hostdevListByCaps (or list_by_caps in hostdev module) filtered devices
manually by inspecting their capability. This can be done on libvirt
level to avoid parsing unwanted devices.

Change-Id: Ia40fff8ba0bace1272666825d6c4fce18ac9a1f0
Signed-off-by: Martin Polednik <[email protected]>
---
M lib/vdsm/hostdev.py
M tests/hostdevTests.py
M tests/vmfakelib.py
3 files changed, 30 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/37/56037/1

diff --git a/lib/vdsm/hostdev.py b/lib/vdsm/hostdev.py
index 380399a..004b9b8 100644
--- a/lib/vdsm/hostdev.py
+++ b/lib/vdsm/hostdev.py
@@ -31,6 +31,21 @@
                           'scsi': 'scsi',
                           'usb_device': 'usb'}
 
+_LIBVIRT_DEVICE_FLAGS = {
+    'system': 1,
+    'pci': 2,
+    'usb_device': 4,
+    'usb': 8,
+    'net': 16,
+    'scsi_host': 32,
+    'scsi_target': 64,
+    'scsi': 128,
+    'storage': 256,
+    'fc_host': 512,
+    'vports': 1024,
+    'scsi_generic': 2048,
+}
+
 
 class NoIOMMUSupportException(Exception):
     pass
@@ -175,12 +190,12 @@
     return libvirt_device, _parse_device_params(libvirt_device.XMLDesc(0))
 
 
-def _get_devices_from_libvirt():
+def _get_devices_from_libvirt(flags=0):
     """
     Returns all available host devices from libvirt parsed to dict
     """
     return dict((device.name(), _parse_device_params(device.XMLDesc(0)))
-                for device in libvirtconnection.get().listAllDevices(0))
+                for device in libvirtconnection.get().listAllDevices(flags))
 
 
 def list_by_caps(caps=None):
@@ -195,12 +210,12 @@
             will be returned (e.g. ['pci', 'usb'] -> pci and usb devices)
     """
     devices = {}
-    libvirt_devices = _get_devices_from_libvirt()
+    flags = 0
+    if caps:
+        flags |= sum([_LIBVIRT_DEVICE_FLAGS[cap] for cap in caps])
+    libvirt_devices = _get_devices_from_libvirt(flags)
 
     for devName, params in libvirt_devices.items():
-        if caps and params['capability'] not in caps:
-            continue
-
         devices[devName] = {'params': params}
 
     devices = hooks.after_hostdev_list_by_caps(devices)
diff --git a/tests/hostdevTests.py b/tests/hostdevTests.py
index e4b6153..b7fc4d3 100644
--- a/tests/hostdevTests.py
+++ b/tests/hostdevTests.py
@@ -320,7 +320,11 @@
         ]
 
     def listAllDevices(self, flags=0):
-        return self._virNodeDevices
+        if not flags:
+            return self._virNodeDevices
+        else:
+            return [device for device in self._virNodeDevices if
+                    flags & hostdev._LIBVIRT_DEVICE_FLAGS[device.capability]]
 
 
 def _fake_totalvfs(device_name):
diff --git a/tests/vmfakelib.py b/tests/vmfakelib.py
index 8e38b49..4d409af 100644
--- a/tests/vmfakelib.py
+++ b/tests/vmfakelib.py
@@ -306,8 +306,10 @@
 
     def __init__(self, xml):
         self.xml = xml
-        self._name = etree.fromstring(self.XMLDesc(0).decode(
-            'ascii', errors='ignore')).find('name').text
+        xml_processed = etree.fromstring(self.XMLDesc(0).decode(
+            'ascii', errors='ignore'))
+        self._name = xml_processed.find('name').text
+        self.capability = xml_processed.find('capability').attrib['type']
 
     def XMLDesc(self, flags=0):
         return self.xml


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia40fff8ba0bace1272666825d6c4fce18ac9a1f0
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Martin Polednik <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to