def showNetwork(bridge): _netinfo = NetInfo() if bridge not in _netinfo.networks: print "Bridge %r doesn't exist" % bridge return nics, vlan, bonding = _netinfo.getNicsVlanAndBondingForNetwork(bridge) print "Bridge %s: vlan=%s, bonding=%s, nics=%s" % (bridge, vlan, bonding, nics)
def delNetwork(network, force=False, configWriter=None, **options): _netinfo = NetInfo() validateBridgeName(network) if network not in networks().keys(): raise ConfigNetworkError(ne.ERR_BAD_BRIDGE, "Cannot delete network %r: It doesn't exist" % network) nics, vlan, bonding = _netinfo.getNicsVlanAndBondingForNetwork(network) bridged = networks()[network]['bridged'] logging.info("Removing network %s with vlan=%s, bonding=%s, nics=%s. options=%s"%(network, vlan, bonding, nics, options)) if not utils.tobool(force): if bonding: validateBondingName(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: #assertVlan(vlan) validateVlanId(vlan) if bridged: assertBridgeClean(network, vlan, bonding, nics) if configWriter is None: configWriter = ConfigWriter() if not utils.tobool(options.get('skipLibvirt', False)): removeLibvirtNetwork(network) if bridged: configWriter.setNewMtu(network) if network and bridged: ifdown(network) subprocess.call([constants.EXT_BRCTL, 'delbr', network]) configWriter.removeBridge(network) if vlan: vlandev = (bonding or nics[0]) + '.' + vlan ifdown(vlandev) subprocess.call([constants.EXT_VCONFIG, 'rem', vlandev], stderr=subprocess.PIPE) configWriter.removeVlan(vlan, bonding or nics[0]) if bonding: if not bridged or not bondingOtherUsers(network, vlan, bonding): ifdown(bonding) if not bridged or not bondingOtherUsers(network, vlan, bonding): configWriter.removeBonding(bonding) for nic in nics: if not bridged or not nicOtherUsers(network, vlan, bonding, nic): ifdown(nic) if bridged and nicOtherUsers(network, vlan, bonding, nic): continue configWriter.removeNic(nic)
def setNewMtu(self, bridge): """ Set new MTU value to bridge and its interfaces :param bridge: bridge name :type bridge: string Update MTU to devices (bridge, interfaces, bonds and vlans) Or added a new value, also set the bridge to the higher value if its under vlans or bond """ _netinfo = NetInfo() cf = self.NET_CONF_PREF + bridge currmtu = self._getConfigValue(cf, 'MTU') if currmtu is None: return nics, delvlan, bonding = _netinfo.getNicsVlanAndBondingForNetwork(bridge) if delvlan is None: return if bonding: _, vlans = _netinfo.getNetworksAndVlansForBonding(bonding) delvlan = bonding + '.' + delvlan else: vlans = _netinfo.getVlansForNic(nics[0]) delvlan = nics[0] + '.' + delvlan newmtu = None for vlan in vlans: if vlan == delvlan: continue cf = self.NET_CONF_PREF + vlan mtu = self._getConfigValue(cf, 'MTU') newmtu = max(newmtu, mtu) if newmtu != currmtu: if bonding: slaves = NetInfo.slaves(bonding) for slave in slaves: cf = self.NET_CONF_PREF + slave self._updateConfigValue(cf, 'MTU', newmtu, newmtu is None) else: cf = self.NET_CONF_PREF + nics[0] self._updateConfigValue(cf, 'MTU', newmtu, newmtu is None)
def _handle_setup(nets, bonds, running_config, nets_by_nic): commands = [] netinfo = NetInfo() for bond, attrs in six.iteritems(bonds): if 'remove' not in attrs: _validate_bond_configuration(attrs, netinfo) if bond in running_config.bonds: commands.extend(_edit_ovs_bond(bond, attrs, running_config)) else: commands.extend(_setup_ovs_bond(bond, attrs, running_config)) for net, attrs in six.iteritems(nets): if 'remove' not in attrs: _validate_net_configuration(net, attrs, running_config, netinfo) if net in running_config.networks: commands.extend(_edit_ovs_net(net, attrs, running_config, nets_by_nic)) else: commands.extend(_setup_ovs_net(net, attrs, running_config, nets_by_nic)) return commands
def _validateNetworkSetup(networks={}, bondings={}, explicitBonding=False): _netinfo = NetInfo() # Step 1: Initial validation (validate names, existence of params, etc.) for network, networkAttrs in networks.iteritems(): validateBridgeName(network) if networkAttrs.get('remove', False): if set(networkAttrs) - set(['remove']): raise ConfigNetworkError(ne.ERR_BAD_PARAMS, "Cannot specify any attribute when removing") if network not in _netinfo.networks: raise ConfigNetworkError(ne.ERR_BAD_BRIDGE, 'Cannot remove bridge %s: Doesn\'t exist' % network) continue vlan = networkAttrs.get('vlan', None) ipaddr = networkAttrs.get('ipaddr', None) netmask = networkAttrs.get('netmask', None) gateway = networkAttrs.get('gateway', None) if vlan: validateVlanId(vlan) # Check ip, netmask, gateway if ipaddr: if not netmask: raise ConfigNetworkError(ne.ERR_BAD_ADDR, "Must specify netmask to configure ip for bridge") validateIpAddress(ipaddr) validateNetmask(netmask) if gateway: validateGateway(gateway) else: if netmask or gateway: raise ConfigNetworkError(ne.ERR_BAD_ADDR, "Specified netmask or gateway but not ip") # check nic or bonding nic = networkAttrs.get('nic', None) bonding = networkAttrs.get('bonding', None) if nic and bonding: raise ConfigNetworkError(ne.ERR_BAD_PARAMS, "Don't specify both nic and bonding") if not nic and not bonding: raise ConfigNetworkError(ne.ERR_BAD_PARAMS, "Must specify either nic or bonding") if nic and nic not in _netinfo.nics: raise ConfigNetworkError(ne.ERR_BAD_NIC, "unknown nic: %r"%nic) for bonding, bondingAttrs in bondings.iteritems(): validateBondingName(bonding) if 'options' in bondingAttrs: validateBondingOptions(bonding, bondingAttrs['options']) if bondingAttrs.get('remove', False): if bonding not in _netinfo.bondings: raise ConfigNetworkError(ne.ERR_BAD_BONDING, 'Cannot remove bonding %s: Doesn\'t exist' % bonding) continue nics = bondingAttrs.get('nics', None) if not nics: raise ConfigNetworkError(ne.ERR_BAD_PARAMS, "Must specify nics for bonding") if not set(nics).issubset(set(_netinfo.nics)): raise ConfigNetworkError(ne.ERR_BAD_NIC, "Unknown nics in: %r"%list(nics)) # Step 2: Make sure we have complete information about the Setup, more validation # (if explicitBonding==False we complete the missing information ourselves, else we raise an exception) nics = defaultdict(lambda: {'networks':[], 'bonding':None}) for network, networkAttrs in networks.iteritems(): if networkAttrs.get('remove', False): continue if 'bonding' in networkAttrs: assert 'nic' not in networkAttrs bonding = networkAttrs['bonding'] if bonding not in bondings: if explicitBonding: raise ConfigNetworkError(ne.ERR_BAD_PARAMS, "Network %s requires unspecified bonding %s"%( network, bonding)) # fill in bonding info bondings[bonding] = {'nics':_netinfo.bondings[bonding]['slaves']} if '_networks' not in bondings[bonding]: bondings[bonding]['_networks'] = [] bondings[bonding]['_networks'].append( network ) else: assert 'nic' in networkAttrs nics[networkAttrs['nic']]['networks'].append( network ) for bonding, bondingAttrs in bondings.iteritems(): if bondingAttrs.get('remove', False): continue connectedNetworks = _netinfo.getNetworksForNic(bonding) for network in connectedNetworks: if network not in networks: if explicitBonding: raise ConfigNetworkError(ne.ERR_BAD_PARAMS, "Bonding %s is associated with unspecified network %s"%( bonding, network)) # fill in network info _, vlan, bonding2 = _netinfo.getNicsVlanAndBondingForNetwork(network) assert bonding == bonding2 networks[network] = {'bonding': bonding, 'vlan':vlan} for nic in bondingAttrs['nics']: if nics[nic]['bonding']: raise ConfigNetworkError(ne.ERR_BAD_BONDING, "Nic %s is attached to two different bondings in setup: %s, %s"%( nic, bonding, nics[nic]['bonding'])) nics[nic]['bonding'] = bonding # At this point the state may be contradictory. # Step 3: Apply removals (We're not iterating because we change the dictionary size) queue = [] for network, networkAttrs in networks.items(): if networkAttrs.get('remove', False): del networks[network] else: queue.append(('network', network, networkAttrs)) for bonding, bondingAttrs in bondings.items(): if bondingAttrs.get('remove', False): del bondings[bonding] else: queue.append(('bonding', bonding, bondingAttrs)) # Step 4: Verify Setup for nic, nicAttrs in nics.iteritems(): networks = nicAttrs['networks'] if networks and nicAttrs['bonding']: raise ConfigNetworkError(ne.ERR_USED_NIC, "Setup attached both network and bonding to nic %s"%(nic)) if len(networks) > 1: for network, networkAttrs in networks.iteritems(): if not networkAttrs.get('vlan', None): raise ConfigNetworkError(ne.ERR_USED_NIC, "Setup attached more than one network to nic %s, some of which aren't vlans"%(nic)) for bonding, bondingAttrs in bondings.iteritems(): networks = bondingAttrs['_networks'] if len(networks) > 1: for network, networkAttrs in networks.iteritems(): if not networkAttrs.get('vlan', None): raise ConfigNetworkError(ne.ERR_BAD_BONDING, "Setup attached more than one network to bonding %s, some of which aren't vlans"%(bonding))