Antoni Segura Puimedon has uploaded a new change for review.

Change subject: netinfo/sampling: split speed into nic and bond specific methods
......................................................................

netinfo/sampling: split speed into nic and bond specific methods

Speed is calculated very differently for nics and bonds. This patch
splits the logic into two different methods and makes netinfo and
sampling use them.

Change-Id: If8d02ce7dec2cafec99478f3b5f089f1fce76d7e
Signed-off-by: Antoni S. Puimedon <asegu...@redhat.com>
---
M lib/vdsm/ipwrapper.py
M lib/vdsm/netinfo.py
M vdsm/sampling.py
3 files changed, 42 insertions(+), 30 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/42/21542/1

diff --git a/lib/vdsm/ipwrapper.py b/lib/vdsm/ipwrapper.py
index ebfda41..5d3e0ae 100644
--- a/lib/vdsm/ipwrapper.py
+++ b/lib/vdsm/ipwrapper.py
@@ -178,6 +178,9 @@
             detectedType = LinkType.NIC
         return detectedType
 
+    def isBOND(self):
+        return self.type == LinkType.BOND
+
     def isDUMMY(self):
         return self.type == LinkType.DUMMY
 
diff --git a/lib/vdsm/netinfo.py b/lib/vdsm/netinfo.py
index dd54a96..2fcf431 100644
--- a/lib/vdsm/netinfo.py
+++ b/lib/vdsm/netinfo.py
@@ -264,34 +264,34 @@
         return operstateFile.read().strip()
 
 
-def speed(dev):
-    # return the speed of devices that are capable of replying
+def nicSpeed(nicName):
+    """Returns the nic speed if it is a legal value and nicName refers to a
+    nic, 0 otherwise."""
     try:
-        # operstat() filters out down/disabled nics
-        if operstate(dev) != OPERSTATE_UP:
-            return 0
-
-        # nics() filters out OS devices (bonds, vlans, bridges)
-        # virtio is a valid device, but doesn't support speed
-        if dev in nics() and not isvirtio(dev):
-            # the device may have been disabled/downed after checking
-            # so we validate the return value as sysfs may return
-            # special values to indicate the device is down/disabled
-            with open('/sys/class/net/%s/speed' % dev) as speedFile:
-                s = int(speedFile.read())
-            if s not in (2 ** 16 - 1, 2 ** 32 - 1) or s > 0:
-                return s
-        elif dev in bondings():
-            bondopts = bondOpts(dev, keys=['slaves', 'active_slave', 'mode'])
-            if bondopts['slaves']:
-                if bondopts['mode'][1] in _BONDING_FAILOVER_MODES:
-                    s = speed(bondopts['active_slave'][0])
-                elif bondopts['mode'][1] in _BONDING_LOADBALANCE_MODES:
-                    s = sum(speed(slave) for slave in bondopts['slaves'])
-                return s
-
+        with open('/sys/class/net/%s/speed' % nicName) as speedFile:
+            s = int(speedFile.read())
+        if s not in (2 ** 16 - 1, 2 ** 32 - 1) or s > 0:
+            return s
+    except OSError as ose:
+        if ose.errno != errno.ENOENT:
+            logging.exception('cannot read %s nic speed', nicName)
     except Exception:
-        logging.exception('cannot read %s speed', dev)
+        logging.exception('cannot read %s speed', nicName)
+    return 0
+
+
+def bondSpeed(bondName):
+    """Returns the bond speed if bondName refers to a bond, 0 otherwise."""
+    opts = bondOpts(bondName, keys=['slaves', 'active_slave', 'mode'])
+    try:
+        if opts['slaves']:
+            if opts['mode'][1] in _BONDING_FAILOVER_MODES:
+                s = nicSpeed(opts['active_slave'][0])
+            elif opts['mode'][1] in _BONDING_LOADBALANCE_MODES:
+                s = sum(nicSpeed(slave) for slave in opts['slaves'])
+            return s
+    except Exception:
+        logging.exception('cannot read %s speed', bondName)
     return 0
 
 
@@ -522,7 +522,7 @@
 
 def _nicinfo(nic, paddr):
     info = _devinfo(nic)
-    info.update({'hwaddr': gethwaddr(nic), 'speed': speed(nic)})
+    info.update({'hwaddr': gethwaddr(nic), 'speed': nicSpeed(nic)})
     if paddr.get(nic):
         info['permhwaddr'] = paddr[nic]
     return (nic, info)
diff --git a/vdsm/sampling.py b/vdsm/sampling.py
index d87f5bf..bc7acb5 100644
--- a/vdsm/sampling.py
+++ b/vdsm/sampling.py
@@ -35,6 +35,7 @@
 
 from vdsm import utils
 from vdsm import netinfo
+from vdsm.ipwrapper import getLinks
 from vdsm.constants import P_VDSM_RUN
 
 _THP_STATE_PATH = '/sys/kernel/mm/transparent_hugepage/enabled'
@@ -384,9 +385,17 @@
         self._stopEvent.set()
 
     def _updateIfidsIfrates(self):
-        self._ifids = netinfo.nics() + netinfo.bondings() + netinfo.vlans() + \
-            netinfo.bridges()
-        self._ifrates = map(netinfo.speed, self._ifids)
+        devices = getLinks()
+        self._ifids = [dev.name for dev in devices]
+        self._ifrates = []
+        for dev in devices:
+            if dev.isNIC():
+                speed = netinfo.nicSpeed(dev.name)
+            elif dev.isBOND():
+                speed = netinfo.bondSpeed(dev.name)
+            else:
+                speed = 0
+            self._ifrates.append(speed)
 
     def sample(self):
         self._updateIfidsIfrates()


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If8d02ce7dec2cafec99478f3b5f089f1fce76d7e
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Antoni Segura Puimedon <asegu...@redhat.com>
_______________________________________________
vdsm-patches mailing list
vdsm-patches@lists.fedorahosted.org
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to