Mark Wu has uploaded a new change for review. Change subject: NetReload: netmodels for delNetwork ......................................................................
NetReload: netmodels for delNetwork This patch re-implements the delNetwork logic using the network entities defined in netmodels. Change-Id: I9e11fd923a4f5d07d069193278f3eadc94f327ec Signed-off-by: Mark Wu <wu...@linux.vnet.ibm.com> --- M vdsm/configNetwork.py M vdsm/netconf/ifcfg.py M vdsm/netmodels.py 3 files changed, 100 insertions(+), 75 deletions(-) git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/73/14873/1 diff --git a/vdsm/configNetwork.py b/vdsm/configNetwork.py index ef6c66f..816cd27 100755 --- a/vdsm/configNetwork.py +++ b/vdsm/configNetwork.py @@ -30,7 +30,6 @@ from neterrors import ConfigNetworkError from vdsm import define from vdsm import netinfo -from vdsm.netinfo import DEFAULT_MTU from netconf.ifcfg import ifup from netconf.ifcfg import ifdown from netconf.ifcfg import ConfigWriter @@ -51,7 +50,8 @@ def objectivizeNetwork(net, bridged=None, vlan=None, bonding=None, bondingOptions=None, nics=None, mtu=None, ipaddr=None, netmask=None, gateway=None, bootproto=None, - _netinfo=None, configurator=None, **opts): + _netinfo=None, configurator=None, onRemove=False, + destroyBonding=None, **opts): if configurator is None: configurator = Ifcfg() if _netinfo is None: @@ -62,7 +62,9 @@ topObj = None if bonding: # With explicit or implicit(from the bond) nics topObj = objectivizeBond(bonding, bondingOptions, nics, mtu, _netinfo, - configurator) + configurator, onRemove) + if destroyBonding: + topObj.destoryOnRemove = True elif nics: topObj = objectivizeNic(bonding, nics, mtu, _netinfo, configurator) @@ -78,12 +80,13 @@ def objectivizeBond(bonding, bondingOptions, nics, mtu, _netinfo, - configurator): + configurator, onRemove=False): if bonding and nics: slaves = [] bondMtu = netinfo.getMaxMtu(nics, mtu) for nic in nics: - _validateNicUnused(nic, _netinfo) + if not onRemove: + _validateNicUnused(nic, _netinfo) slaves.append(Nic(nic, configurator, mtu=bondMtu)) elif bonding in _netinfo.bondings: # Implicit bonding. nics = [nic for nic in _netinfo.getNicsForBonding(bonding)] @@ -290,6 +293,31 @@ implicitBonding=False, _netinfo=_netinfo) +def validateDelNetwork(network, vlan, bonding, nics, bridged, _netinfo): + if bonding: + Bond.validateName(bonding) + if set(nics) != set(_netinfo.bondings[bonding]["slaves"]): + raise ConfigNetworkError(ne.ERR_BAD_NIC, 'delNetwork: %s are ' + 'not all nics enslaved to %s' % + (nics, bonding)) + if vlan: + Vlan.validateTag(vlan) + if bridged: + assertBridgeClean(network, vlan, bonding, nics) + + +def _delNonVdsmNetwork(network, vlan, bonding, nics, configWriter): + if network in netinfo.bridges(): + configWriter.removeBridge(network) + else: + raise ConfigNetworkError(ne.ERR_BAD_BRIDGE, "Cannot delete network" + " %r: It doesn't exist in the system" % + network) + if vlan: + configWriter.removeVlan(vlan, bonding or nics[0]) + return + + def delNetwork(network, vlan=None, bonding=None, nics=None, force=False, configWriter=None, implicitBonding=True, _netinfo=None, **options): @@ -298,20 +326,11 @@ if configWriter is None: configWriter = ConfigWriter() + configurator = Ifcfg(configWriter) if network not in _netinfo.networks: logging.info("Network %r: doesn't exist in libvirt database", network) - if network in netinfo.bridges(): - configWriter.removeBridge(network) - else: - raise ConfigNetworkError(ne.ERR_BAD_BRIDGE, "Cannot delete network" - " %r: It doesn't exist in the system" % - network) - - if vlan: - configWriter.removeVlan(vlan, bonding or nics[0]) - - return + _delNonVdsmNetwork(network, vlan, bonding, nics) nics, vlan, bonding = _netinfo.getNicsVlanAndBondingForNetwork(network) bridged = _netinfo.networks[network]['bridged'] @@ -320,16 +339,7 @@ "options=%s" % (network, vlan, bonding, nics, options)) if not utils.tobool(force): - if bonding: - Bond.validateName(bonding) - if set(nics) != set(_netinfo.bondings[bonding]["slaves"]): - raise ConfigNetworkError(ne.ERR_BAD_NIC, 'delNetwork: %s are ' - 'not all nics enslaved to %s' % - (nics, bonding)) - if vlan: - Vlan.validateTag(vlan) - if bridged: - assertBridgeClean(network, vlan, bonding, nics) + validateDelNetwork(network, vlan, bonding, nics, bridged, _netinfo) configWriter.setNewMtu(network=network, bridged=bridged, _netinfo=_netinfo) configWriter.removeLibvirtNetwork(network) @@ -341,44 +351,11 @@ raise ConfigNetworkError(ne.ERR_USED_BRIDGE, 'delNetwork: bridge %s ' 'still exists' % network) - if network and bridged: - configWriter.removeBridge(network) - - nic = nics[0] if nics else None - iface = bonding if bonding else nic - if iface: - ifdown(iface) - if vlan: - configWriter.removeVlan(vlan, iface) - else: - cf = netinfo.NET_CONF_PREF + iface - if not bridged: - # When removing bridgeless non-VLANed network - # we need to remove IP/NETMASK from the cfg file - for key in ('IPADDR', 'NETMASK', 'GATEWAY', 'BOOTPROTO'): - configWriter._updateConfigValue(cf, key, '', True) - else: - # When removing bridged non-VLANed network - # we need to remove BRIDGE from the cfg file - configWriter._updateConfigValue(cf, 'BRIDGE', '', True) - - # Now we can restart changed interface - ifup(iface) - - # The (relatively) new setupNetwork verb allows to remove a network - # defined on top of an bonding device without break the bond itself. - if implicitBonding: - if bonding and not _netinfo.ifaceHasMultiUsers(bonding): - ifdown(bonding) - configWriter.removeBonding(bonding) - - _removeUnusedNics(nics, configWriter) - elif not bonding: - _removeUnusedNics(nics, configWriter) - elif not _netinfo.ifaceHasMultiUsers(bonding): - ifdown(bonding) - configWriter.setBondingMtu(bonding, DEFAULT_MTU) - ifup(bonding) + netEnt = objectivizeNetwork(net=network, bridged=bridged, vlan=vlan, + bonding=bonding, nics=nics, _netinfo=_netinfo, + configurator=configurator, onRemove=True, + destroyBonding=implicitBonding) + netEnt.remove() def clientSeen(timeout): diff --git a/vdsm/netconf/ifcfg.py b/vdsm/netconf/ifcfg.py index faf8108..84444b0 100644 --- a/vdsm/netconf/ifcfg.py +++ b/vdsm/netconf/ifcfg.py @@ -30,6 +30,7 @@ import threading from neterrors import ConfigNetworkError +from netmodels import Bond, Vlan from storage.misc import execCmd from vdsm import constants from vdsm import libvirtconnection @@ -144,6 +145,46 @@ if network: self.configWriter.createLibvirtNetwork(network, False, nic.name) self._libvirtAdded.add(network) + + def removeBridge(self, bridge): + ifdown(bridge.name) + execCmd([constants.EXT_BRCTL, 'delbr', bridge.name]) + self.configWriter.removeBridge(bridge.name) + for port in bridge.ports: + port.remove() + + def removeVlan(self, vlan): + ifdown(vlan.name) + self.configWriter.removeVlan(vlan.name) + vlan.device.remove() + + def _removeBond(self, bonding): + if hasattr(bonding, 'destroyOnRemove'): + self.configWriter.removeBonding(bonding.name) + for slave in bonding.slaves: + slave.remove() + else: + self.configWriter.restoreBareBonding(bonding.name) + + def removeIface(self, iface): + _netinfo = netinfo.NetInfo() + ifdown(iface.name) + if not _netinfo.ifaceHasMultiUsers(iface.name): + if isinstance(iface, Bond): + self._removeBond(iface) + else: + self.configWriter.removeNic(iface.name) + else: + if iface.master is None: + self.configWriter.removeIpConfig(iface.name) + if not hasattr(iface, 'destroyOnRemove'): + ifup(iface.name) + + def removeBond(self, bonding): + self.removeIface(bonding) + + def removeNic(self, nic): + self.removeIface(nic) class ConfigWriter(object): @@ -563,22 +604,29 @@ except IOError: pass - def removeVlan(self, vlan, iface): - vlandev = iface + '.' + vlan - ifdown(vlandev) - self._backup(netinfo.NET_CONF_PREF + vlandev) - self._removeFile(netinfo.NET_CONF_PREF + vlandev) + def removeVlan(self, vlan): + self._backup(netinfo.NET_CONF_PREF + vlan) + self._removeFile(netinfo.NET_CONF_PREF + vlan) def removeBonding(self, bonding): self._backup(netinfo.NET_CONF_PREF + bonding) self._removeFile(netinfo.NET_CONF_PREF + bonding) + def restoreBareBonding(self, bonding): + cf = netinfo.NET_CONF_PREF + bonding + self._updateConfigValue(cf, 'BRIDGE', '', True) + self.setBondingMtu(bonding, netinfo.DEFAULT_MTU) + self.removeIpConfig(bonding) + def removeBridge(self, bridge): - ifdown(bridge) - execCmd([constants.EXT_BRCTL, 'delbr', bridge]) self._backup(netinfo.NET_CONF_PREF + bridge) self._removeFile(netinfo.NET_CONF_PREF + bridge) + def removeIpConfig(self, iface): + cf = netinfo.NET_CONF_PREF + iface + for key in ('IPADDR', 'NETMASK', 'GATEWAY', 'BOOTPROTO'): + self._updateConfigValue(cf, key, '', True) + def _getConfigValue(self, conffile, entry): """ Get value from network configuration file diff --git a/vdsm/netmodels.py b/vdsm/netmodels.py index 4efd08b..4e34a22 100644 --- a/vdsm/netmodels.py +++ b/vdsm/netmodels.py @@ -47,8 +47,8 @@ self.configurator.configureNic(self, network=network, bridge=bridge, bonding=bonding, vlan=vlan, **opts) - def remove(self, network=None, bond=None): - self.configurator.removeNic(self, network=network, bond=bond) + def remove(self): + self.configurator.removeNic(self) def __str__(self): return self.name @@ -89,7 +89,7 @@ **opts) def remove(self, force=False): - self.configurator.removeVlan(self.name) + self.configurator.removeVlan(self) @classmethod def validateTag(cls, tag): @@ -181,7 +181,7 @@ def remove(self, force=False): logging.debug('Removing bond %r with nics = %s', self.name, self.slaves) - self.configurator.removeBond(self.name, self.slaves) + self.configurator.removeBond(self) @staticmethod def validateName(name): -- To view, visit http://gerrit.ovirt.org/14873 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I9e11fd923a4f5d07d069193278f3eadc94f327ec Gerrit-PatchSet: 1 Gerrit-Project: vdsm Gerrit-Branch: master Gerrit-Owner: Mark Wu <wu...@linux.vnet.ibm.com> _______________________________________________ vdsm-patches mailing list vdsm-patches@lists.fedorahosted.org https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches