Change in vdsm[master]: setupNetworks: optimization

asegurap at redhat.com asegurap at redhat.com
Wed Jan 22 14:38:37 UTC 2014


Antoni Segura Puimedon has uploaded a new change for review.

Change subject: setupNetworks: optimization
......................................................................

setupNetworks: optimization

Change-Id: Ia32b0333052c05c92475bddcc5e9ce805fb82c94
Signed-off-by: Antoni S. Puimedon <asegurap at redhat.com>
---
M lib/vdsm/netinfo.py
M vdsm/configNetwork.py
2 files changed, 54 insertions(+), 19 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/75/23575/1

diff --git a/lib/vdsm/netinfo.py b/lib/vdsm/netinfo.py
index e089483..094c089 100644
--- a/lib/vdsm/netinfo.py
+++ b/lib/vdsm/netinfo.py
@@ -22,6 +22,7 @@
 import errno
 from glob import iglob
 from itertools import chain
+import libvirt
 import logging
 import os
 import shlex
@@ -107,6 +108,15 @@
 def bridges():
     return [b.split('/')[-2] for b in iglob('/sys/class/net/*/bridge')
             if b.split('/')[-2] != DUMMY_BRIDGE]
+
+
+def networkExists(netName):
+    conn = libvirtconnection.get()
+    try:
+        conn.networkLookupByName(netName)
+    except libvirt.libvirtError:
+        return False
+    return True
 
 
 def networks():
@@ -552,23 +562,37 @@
     return addrs
 
 
-def get():
+def _libvirtNets2vdsm(nets, gateways=None, ipv6routes=None, ipAddrs=None):
+    if gateways is None:
+        gateways = getRoutes()
+    if ipv6routes is None:
+        ipv6routes = getIPv6Routes()
+    if ipAddrs is None:
+        ipAddrs = _getIpAddrs()
+    d = {}
+    for net, netAttr in nets.iteritems():
+        try:
+            d[net] = _getNetInfo(netAttr.get('iface', net), netAttr['bridged'],
+                                 gateways, ipv6routes, ipAddrs,
+                                 netAttr.get('qosInbound'),
+                                 netAttr.get('qosOutbound'))
+        except KeyError:
+            continue  # Do not report missing libvirt networks.
+    return d
+
+
+def get(nets=None):
     d = {'bondings': {}, 'bridges': {}, 'networks': {}, 'nics': {},
          'vlans': {}}
     gateways = getRoutes()
     ipv6routes = getIPv6Routes()
     paddr = permAddr()
     ipAddrs = _getIpAddrs()
-
-    for net, netAttr in networks().iteritems():
-        try:
-            d['networks'][net] = _getNetInfo(netAttr.get('iface', net),
-                                             netAttr['bridged'], gateways,
-                                             ipv6routes, ipAddrs,
-                                             netAttr.get('qosInbound'),
-                                             netAttr.get('qosOutbound'))
-        except KeyError:
-            continue  # Do not report missing libvirt networks.
+    if nets is None:
+        nets = networks()
+        d['networks'] = _libvirtNets2vdsm(nets, gateways, ipv6routes, ipAddrs)
+    else:
+        d['networks'] = nets
 
     for dev in (link for link in getLinks() if not link.isHidden()):
         if dev.isBRIDGE():
@@ -664,6 +688,13 @@
         self.nics = _netinfo['nics']
         self.bondings = _netinfo['bondings']
 
+    def updateDevices(self):
+        _netinfo = get(nets=self.networks)
+        self.networks = _netinfo['networks']
+        self.vlans = _netinfo['vlans']
+        self.nics = _netinfo['nics']
+        self.bondings = _netinfo['bondings']
+
     def getNetworksAndVlansForIface(self, iface):
         """ Returns tuples of (bridge/network, vlan) connected to  nic/bond """
         return chain(self.getBridgedNetworksAndVlansForIface(iface),
diff --git a/vdsm/configNetwork.py b/vdsm/configNetwork.py
index 2be811c..63e3f7e 100755
--- a/vdsm/configNetwork.py
+++ b/vdsm/configNetwork.py
@@ -414,9 +414,8 @@
     netEnt.remove()
 
     # We need to gather NetInfo again to refresh networks info from libvirt.
-    # The deleted bridge should never be up at this stage.
-    _netinfo = netinfo.NetInfo()
-    if network in _netinfo.networks:
+    # The deleted net should never exist at this stage.
+    if netinfo.networkExists(network):
         raise ConfigNetworkError(ne.ERR_USED_BRIDGE, 'delNetwork: bridge %s '
                                  'still exists' % network)
 
@@ -581,7 +580,10 @@
         a bonding, it's not necessary to specify its networks.
     """
     logger = logging.getLogger("setupNetworks")
-    _netinfo = netinfo.NetInfo()
+
+    libvirt_nets = netinfo.networks()
+    _netinfo = netinfo.NetInfo(_netinfo=netinfo.get(
+        netinfo._libvirtNets2vdsm(libvirt_nets)))
     networksAdded = set()
 
     logger.debug("Setting up network according to configuration: "
@@ -604,16 +606,17 @@
 
     logger.debug("Applying...")
     with ConfiguratorClass(options.get('_inRollback', False)) as configurator:
-        libvirt_nets = netinfo.networks()
         # Remove edited networks and networks with 'remove' attribute
         for network, networkAttrs in networks.items():
             if network in _netinfo.networks:
                 logger.debug("Removing network %r" % network)
                 delNetwork(network, configurator=configurator, force=force,
-                           implicitBonding=False)
+                           implicitBonding=False, netinfo=_netinfo)
                 if 'remove' in networkAttrs:
                     del networks[network]
                     del libvirt_nets[network]
+                _netinfo.updateDevices()
+                del _netinfo.networks[network]
             elif network in libvirt_nets:
                 # If the network was not in _netinfo but is in the networks
                 # returned by libvirt, it means that we are dealing with
@@ -624,6 +627,7 @@
                 if 'remove' in networkAttrs:
                     del networks[network]
                     del libvirt_nets[network]
+                _netinfo.updateDevices()
             elif 'remove' in networkAttrs:
                 raise ConfigNetworkError(ne.ERR_BAD_BRIDGE, "Cannot delete "
                                          "network %r: It doesn't exist in the "
@@ -632,13 +636,13 @@
                 networksAdded.add(network)
 
         _handleBondings(bondings, configurator)
+        _netinfo.updateDevices()
 
         # We need to use the newest host info
-        _ni = netinfo.NetInfo()
         for network, networkAttrs in networks.iteritems():
             d = dict(networkAttrs)
             if 'bonding' in d:
-                d.update(_buildBondOptions(d['bonding'], bondings, _ni))
+                d.update(_buildBondOptions(d['bonding'], bondings, _netinfo))
             else:
                 d['nics'] = [d.pop('nic')] if 'nic' in d else []
             d['force'] = force


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia32b0333052c05c92475bddcc5e9ce805fb82c94
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Antoni Segura Puimedon <asegurap at redhat.com>


More information about the vdsm-patches mailing list