[virt-tools-list] [PATCH V4 1/2] Add comparison function for NodeDevice

Lin Ma lma at suse.com
Fri Sep 12 13:57:40 UTC 2014


PCIDevice and USBDevice include respective comparison logic.

Signed-off-by: Lin Ma <lma at suse.com>
---
 virtManager/details.py | 47 ++------------------------------
 virtinst/nodedev.py    | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+), 45 deletions(-)

diff --git a/virtManager/details.py b/virtManager/details.py
index 9fae87a..d0ee732 100644
--- a/virtManager/details.py
+++ b/virtManager/details.py
@@ -42,6 +42,7 @@ from virtManager.graphwidgets import Sparkline
 import virtinst
 from virtinst import util
 from virtinst import VirtualRNGDevice
+from virtinst import NodeDevice
 
 
 # Parameters that can be edited in the details window
@@ -255,61 +256,17 @@ def _build_hostdev_label(hostdev):
 
 
 def lookup_nodedev(vmmconn, hostdev):
-    def intify(val, do_hex=False):
-        try:
-            if do_hex:
-                return int(val or '0x00', 16)
-            else:
-                return int(val)
-        except:
-            return -1
-
-    def attrVal(node, attr):
-        if not hasattr(node, attr):
-            return None
-        return getattr(node, attr)
-
     devtype = hostdev.type
     found_dev = None
 
-    vendor_id = product_id = bus = device = domain = slot = func = None
-
     # For USB we want a device, not a bus
     if devtype == 'usb':
         devtype    = 'usb_device'
-        vendor_id  = hostdev.vendor or -1
-        product_id = hostdev.product or -1
-        bus        = intify(hostdev.bus)
-        device     = intify(hostdev.device)
-
-    elif devtype == 'pci':
-        domain     = intify(hostdev.domain, True)
-        bus        = intify(hostdev.bus, True)
-        slot       = intify(hostdev.slot, True)
-        func       = intify(hostdev.function, True)
 
     devs = vmmconn.get_nodedevs(devtype, None)
     for dev in devs:
-        # Try to match with product_id|vendor_id|bus|device
-        if ((attrVal(dev, "product_id") == product_id or product_id == -1) and
-            (attrVal(dev, "vendor_id") == vendor_id or vendor_id == -1) and
-            (attrVal(dev, "bus") == bus or bus == -1) and
-            (attrVal(dev, "device") == device or device == -1)):
+        if NodeDevice.compare_to_nodedev(hostdev, dev):
             found_dev = dev
-        else:
-            # Try to get info from bus/addr
-            dev_id = intify(attrVal(dev, "device"))
-            bus_id = intify(attrVal(dev, "bus"))
-            dom_id = intify(attrVal(dev, "domain"))
-            func_id = intify(attrVal(dev, "function"))
-            slot_id = intify(attrVal(dev, "slot"))
-
-            if ((dev_id == device and bus_id == bus) or
-                (dom_id == domain and func_id == func and
-                 bus_id == bus and slot_id == slot)):
-                found_dev = dev
-
-        if found_dev:
             break
 
     return found_dev
diff --git a/virtinst/nodedev.py b/virtinst/nodedev.py
index 17524dc..3e5a8b2 100644
--- a/virtinst/nodedev.py
+++ b/virtinst/nodedev.py
@@ -121,6 +121,16 @@ class NodeDevice(XMLBuilder):
         """
         return self.name
 
+    @staticmethod
+    def compare_to_nodedev(hostdev, nodedev):
+        cls = _typeToDeviceClass(nodedev.device_type)
+        devtype = hostdev.type
+        if devtype == "usb":
+            devtype = "usb_device"
+        if devtype == nodedev.device_type:
+            if cls.compare_to_nodedev(hostdev, nodedev):
+                return True
+        return False
 
 class SystemDevice(NodeDevice):
     hw_vendor = XMLProperty("./capability/hardware/vendor")
@@ -176,6 +186,39 @@ class PCIDevice(NodeDevice):
 
         return "%s %s %s" % (devstr, self.vendor_name, self.product_name)
 
+    @staticmethod
+    def compare_to_nodedev(hostdev, nodedev):
+        def intify(val, do_hex=False):
+            try:
+                if do_hex:
+                    return int(val or '0x00', 16)
+                else:
+                    return int(val)
+            except:
+                return -1
+
+        def attrVal(node, attr):
+            if not hasattr(node, attr):
+                return None
+            return getattr(node, attr)
+
+        h_bus = h_dev = h_dom = h_slot = h_func = None
+
+        h_dom = intify(hostdev.domain, True)
+        h_bus = intify(hostdev.bus, True)
+        h_slot = intify(hostdev.slot, True)
+        h_func = intify(hostdev.function, True)
+        n_dev = intify(attrVal(nodedev, "device"))
+        n_bus = intify(attrVal(nodedev, "bus"))
+        n_dom = intify(attrVal(nodedev, "domain"))
+        n_func = intify(attrVal(nodedev, "function"))
+        n_slot = intify(attrVal(nodedev, "slot"))
+
+        if ((n_dev == h_dev and n_bus == h_bus) or
+            (n_dom == h_dom and n_func == h_func and
+             n_bus == h_bus and n_slot == h_slot)):
+            return True
+
 
 class USBDevice(NodeDevice):
     bus = XMLProperty("./capability/bus")
@@ -192,6 +235,35 @@ class USBDevice(NodeDevice):
                              str(self.product_name))
         return desc
 
+    @staticmethod
+    def compare_to_nodedev(hostdev, nodedev):
+        def intify(val, do_hex=False):
+            try:
+                if do_hex:
+                    return int(val or '0x00', 16)
+                else:
+                    return int(val)
+            except:
+                return -1
+
+        def attrVal(node, attr):
+            if not hasattr(node, attr):
+                return None
+            return getattr(node, attr)
+
+        h_vid = h_pid = h_bus = h_dev = None
+
+        h_vid = hostdev.vendor or -1
+        h_pid = hostdev.product or -1
+        h_bus = intify(hostdev.bus)
+        h_dev = intify(hostdev.device)
+
+        if ((attrVal(nodedev, "product_id") == h_pid or h_pid == -1) and
+            (attrVal(nodedev, "vendor_id") == h_vid or h_vid == -1) and
+            (intify(attrVal(nodedev, "bus")) == h_bus or h_bus == -1) and
+            (intify(attrVal(nodedev, "device")) == h_dev or h_dev == -1)):
+            return True
+
 
 class StorageDevice(NodeDevice):
     block = XMLProperty("./capability/block")
-- 
1.8.4




More information about the virt-tools-list mailing list