[virt-tools-list] [PATCH V6 1/2] Add comparison function for NodeDevice
Lin Ma
lma at suse.com
Tue Sep 16 15:44:21 UTC 2014
PCIDevice and USBDevice include respective comparison logic.
Signed-off-by: Lin Ma <lma at suse.com>
---
virtManager/details.py | 46 +------------------------------------
virtinst/nodedev.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 62 insertions(+), 45 deletions(-)
diff --git a/virtManager/details.py b/virtManager/details.py
index 4cb1baf..bab87ee 100644
--- a/virtManager/details.py
+++ b/virtManager/details.py
@@ -255,61 +255,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 dev.compare_to_hostdev(hostdev):
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 9c990b3..5db21b9 100644
--- a/virtinst/nodedev.py
+++ b/virtinst/nodedev.py
@@ -47,6 +47,22 @@ def _lookupNodeName(conn, name):
return NodeDevice.parse(conn, xml)
+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)
+
+
class NodeDevice(XMLBuilder):
CAPABILITY_TYPE_SYSTEM = "system"
CAPABILITY_TYPE_NET = "net"
@@ -121,6 +137,10 @@ class NodeDevice(XMLBuilder):
"""
return self.name
+ def compare_to_hostdev(self, hostdev):
+ ignore = hostdev
+ return False
+
class SystemDevice(NodeDevice):
hw_vendor = XMLProperty("./capability/hardware/vendor")
@@ -176,6 +196,27 @@ class PCIDevice(NodeDevice):
return "%s %s %s" % (devstr, self.vendor_name, self.product_name)
+ def compare_to_hostdev(self, hostdev):
+ if hostdev.type != self.device_type:
+ return False
+
+ 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(self, "device"))
+ n_bus = _intify(_attrVal(self, "bus"))
+ n_dom = _intify(_attrVal(self, "domain"))
+ n_func = _intify(_attrVal(self, "function"))
+ n_slot = _intify(_attrVal(self, "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")
@@ -219,6 +260,26 @@ class USBDevice(NodeDevice):
desc = "%s %s" % (busstr, devstr)
return desc
+ def compare_to_hostdev(self, hostdev):
+ devtype = hostdev.type
+ if devtype == "usb":
+ devtype = "usb_device"
+ if devtype != self.device_type:
+ return False
+
+ 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(self, "product_id") == h_pid or h_pid == -1) and
+ (_attrVal(self, "vendor_id") == h_vid or h_vid == -1) and
+ (_intify(_attrVal(self, "bus")) == h_bus or h_bus == -1) and
+ (_intify(_attrVal(self, "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