Change in vdsm[master]: Add a new class NetworkParams to represent the pramaters of ...

wudxw at linux.vnet.ibm.com wudxw at linux.vnet.ibm.com
Thu Jan 24 09:33:15 UTC 2013


Mark Wu has uploaded a new change for review.

Change subject: Add a new class NetworkParams to represent the pramaters of a network
......................................................................

Add a new class NetworkParams to represent the pramaters of a network

This patch tries to split the logic of adding new network from the
configuration approach related code.

Change-Id: I7df2db451298b5e0c1dfbbf1cf09f69e0e9a9ea5
Signed-off-by: Mark Wu <wudxw at linux.vnet.ibm.com>
---
M vdsm/configNetwork.py
1 file changed, 100 insertions(+), 84 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/59/11359/1

diff --git a/vdsm/configNetwork.py b/vdsm/configNetwork.py
index b6fc218..2815a33 100755
--- a/vdsm/configNetwork.py
+++ b/vdsm/configNetwork.py
@@ -865,118 +865,133 @@
             _validateInterNetworkCompatibility(_netinfo, vlan, nic, bridged)
 
 
-def addNetwork(network, vlan=None, bonding=None, nics=None, ipaddr=None,
-               netmask=None, mtu=None, gateway=None, force=False,
-               configWriter=None, bondingOptions=None, bridged=True,
-               **options):
-    nics = nics or ()
-    _netinfo = netinfo.NetInfo()
-    bridged = utils.tobool(bridged)
+class NetworkParams(object):
 
-    if mtu:
-        mtu = int(mtu)
+    def __init__(self, network, vlan=None, bonding=None, nics=None,
+                 ipaddr=None, netmask=None, mtu=None, gateway=None,
+                 bondingOptions=None, bridged=True, **options):
+        self.netinfo = netinfo.NetInfo()
 
-    prefix = options.get('prefix')
-    if prefix is not None:
-        if netmask is None:
-            netmask = netinfo.prefix2netmask(int(prefix))
-            del options['prefix']
+        if not utils.tobool(options.pop('force', None)):
+            logging.debug('validating network...')
+            _addNetworkValidation(self.netinfo, network=network, vlan=vlan,
+                                  bonding=bonding, nics=nics, ipaddr=ipaddr,
+                                  netmask=netmask, gateway=gateway,
+                                  bondingOptions=bondingOptions,
+                                  bridged=bridged, **options)
+
+        logging.info("Adding network %s with vlanId=%s, bonding=%s, nics=%s,"
+                     " bondingOptions=%s, mtu=%s, bridged=%s, options=%s",
+                     network, vlan, bonding, nics, bondingOptions,
+                     mtu, bridged, options)
+
+        self.network = network
+        self.bridged = utils.tobool(bridged)
+        self.vlanId = vlan
+        self.bonding = bonding
+        self.nics = nics or ()
+        self.nic = self.nics[0] if self.nics else None
+
+        if mtu:
+            self.mtu = int(mtu)
+            self.newMaxMtu = max(self.mtu, netinfo.getMaxMtu(self.nics))
         else:
-            raise ConfigNetworkError(ne.ERR_BAD_PARAMS,
-                                     'Both PREFIX and NETMASK supplied')
+            self.mtu = self.newMaxMtu = None
 
-    # Validation
-    if not utils.tobool(force):
-        logging.debug('validating network...')
-        _addNetworkValidation(_netinfo, network=network, vlan=vlan,
-                              bonding=bonding, nics=nics, ipaddr=ipaddr,
-                              netmask=netmask, gateway=gateway,
-                              bondingOptions=bondingOptions, bridged=bridged,
-                              **options)
+        bootproto = options.pop('bootproto', None)
+        if ipaddr or bootproto:
+            self.l3info = {'ipaddr': ipaddr,
+                           'netmask': self._getNetmask(netmask, options),
+                           'gateway': gateway, 'bootproto': bootproto}
+        else:
+            self.l3info = None
+        self.asyncFlag = (bootproto == 'dhcp' and
+                          not utils.tobool(options.pop('blockingdhcp', None)))
 
-    logging.info("Adding network %s with vlan=%s, bonding=%s, nics=%s,"
-                 " bondingOptions=%s, mtu=%s, bridged=%s, options=%s",
-                 network, vlan, bonding, nics, bondingOptions,
-                 mtu, bridged, options)
+        if self.vlanId:
+            self.vlan = (self.bonding or self.nic) + '.' + self.vlanId
+        else:
+            self.vlan = None
+        self.bridge = self.network if self.bridged else None
+        self.allIfaces = filter(None, [self.bridge, self.vlan, self.bonding,
+                                       self.nic])
+        self.topIface = self.allIfaces[0]
+        self.dependentIfaces = reversed(self.allIfaces[1:])
+        self._buildIfaceParams(bondingOptions, options)
+
+    def _getNetmask(self, netmask, options):
+        prefix = options.pop('prefix', None)
+        if netmask:
+            if prefix:
+                raise ConfigNetworkError(ne.ERR_BAD_PARAMS,
+                                         'Both PREFIX and NETMASK supplied')
+            return netmask
+        elif prefix is not None:
+            return netinfo.prefix2netmask(int(prefix))
+
+    def _buildIfaceParams(self, bondingOptions, options):
+        if self.bridge:
+            self.bridgeParams = dict({'mtu': self.mtu}, **options)
+        if self.vlan:
+            self.vlanParams = dict({'bridge': self.bridge, 'mtu': self.mtu},
+                                   **options)
+        if self.bonding:
+            bridgeForBond = self.bridge if not self.vlan else None
+            self.bondingParams = dict({'bondingOptions': bondingOptions,
+                                       'bridge': bridgeForBond,
+                                       'mtu': self.newMaxMtu},
+                                      **options)
+            bridgeForNic = (self.bridge if not (self.vlan or self.bonding)
+                            else None)
+        self.nicParams = dict({'bonding': self.bonding, 'bridge': bridgeForNic,
+                               'mtu': self.newMaxMtu}, **options)
+
+
+def addNetwork(netParams, configWriter=None):
 
     if configWriter is None:
         configWriter = ConfigWriter()
 
-    prevmtu = None
-    if mtu:
-        prevmtu = netinfo.getMaxMtu(nics)
-
-    nic = nics[0] if nics else None
-    iface = bonding or nic
-    if bridged:
-        topIface = network
-    elif vlan:
-        topIface = iface + '.' + vlan
-    else:
-        topIface = iface
-
-    bootproto = options.get('bootproto')
-    blockingDhcp = utils.tobool(options.get('blockingdhcp'))
-
     # take down nics that need to be changed
-    vlanedIfaces = [v['iface'] for v in _netinfo.vlans.values()]
-    if bonding not in vlanedIfaces:
-        for nic in nics:
+    vlanedIfaces = [v['iface'] for v in netParams.netinfo.vlans.values()]
+    if netParams.bonding not in vlanedIfaces:
+        for nic in netParams.nics:
             if nic not in vlanedIfaces:
                 ifdown(nic)
 
-    if bridged:
-        configWriter.addBridge(network, mtu=mtu, **options)
-        ifdown(network)
+    if netParams.bridged:
+        configWriter.addBridge(netParams.bridge, **netParams.bridgeParams)
+        ifdown(netParams.bridge)
 
     # For VLAN we should attach bridge only to the VLAN device
     # rather than to underlying NICs or bond
-    brName = network if bridged else None
-    bridgeForNic = None if vlan else brName
-
     # We want to create config files (ifcfg-*) in top-down order
     # (bridge->vlan->bond->nic) to be able to handle IP/NETMASK
     # correctly for bridgeless networks
-    if vlan:
+    if netParams.vlan:
         # don't ifup VLAN interface here, it should be done last,
         # after the bond and nic up
-        iface += '.' + vlan
-        configWriter.addVlan(iface, bridge=brName, mtu=mtu, **options)
+        configWriter.addVlan(netParams.vlan, **netParams.vlanParams)
 
     # First we need to prepare all conf files
-    if bonding:
-        configWriter.addBonding(bonding, bridge=bridgeForNic,
-                                bondingOptions=bondingOptions,
-                                mtu=max(prevmtu, mtu), **options)
+    if netParams.bonding:
+        configWriter.addBonding(netParams.bonding, **netParams.bondingParams)
 
-    for nic in nics:
-        configWriter.addNic(nic, bonding=bonding,
-                            bridge=bridgeForNic if not bonding else None,
-                            mtu=max(prevmtu, mtu), **options)
+    for nic in netParams.nics:
+        configWriter.addNic(nic, **netParams.nicParams)
 
-    if ipaddr or bootproto:
-        configWriter.updateTopIface(topIface, ipaddr=ipaddr, netmask=netmask,
-                                    gateway=gateway, bootproto=bootproto)
-
-    def _isAsync(iface):
-        return iface == topIface and bootproto == 'dhcp' and not blockingDhcp
+    if netParams.l3info:
+        configWriter.updateTopIface(netParams.topIface, **netParams.l3info)
 
     # Now we can run ifup for all interfaces
-    if bonding:
-        ifup(bonding, _isAsync(bonding))
-    else:
-        for nic in nics:
-            ifup(nic, _isAsync(nic))
+    for iface in netParams.dependentIfaces:
+        ifup(iface)
 
-    # Now we can ifup VLAN interface, because bond and nic already up
-    if vlan:
-        ifup(iface, _isAsync(iface))
-
-    if bridged:
-        ifup(network, _isAsync(network))
+    ifup(netParams.topIface, netParams.asyncFlag)
 
     # add libvirt network
-    configWriter.createLibvirtNetwork(network, bridged, iface)
+    configWriter.createLibvirtNetwork(netParams.network, netParams.bridged,
+                                      netParams.topIface)
 
 
 def assertBridgeClean(bridge, vlan, bonding, nics):
@@ -1362,10 +1377,11 @@
                 else:
                     d['nics'] = [d.pop('nic')]
                 d['force'] = force
+                d['implicitBonding'] = True
 
                 logger.debug("Adding network %r" % network)
-                addNetwork(network, configWriter=configWriter,
-                           implicitBonding=True, **d)
+                addNetwork(NetworkParams(network, **d),
+                           configWriter=configWriter)
 
             if utils.tobool(options.get('connectivityCheck', True)):
                 logger.debug('Checking connectivity...')


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7df2db451298b5e0c1dfbbf1cf09f69e0e9a9ea5
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Mark Wu <wudxw at linux.vnet.ibm.com>


More information about the vdsm-patches mailing list