def testNetmaskConversions(self): path = os.path.join(os.path.dirname(__file__), "netmaskconversions") with open(path) as netmaskFile: for line in netmaskFile: if line.startswith('#'): continue bitmask, address = [value.strip() for value in line.split()] self.assertEqual(addresses.prefix2netmask(int(bitmask)), address) self.assertRaises(ValueError, addresses.prefix2netmask, -1) self.assertRaises(ValueError, addresses.prefix2netmask, 33)
def _addNetwork(network, configurator, vlan=None, bonding=None, nic=None, ipaddr=None, netmask=None, prefix=None, mtu=None, gateway=None, dhcpv6=None, ipv6addr=None, ipv6gateway=None, ipv6autoconf=None, bridged=True, _netinfo=None, hostQos=None, defaultRoute=None, blockingdhcp=False, **options): if _netinfo is None: _netinfo = CachingNetInfo() if dhcpv6 is not None: dhcpv6 = utils.tobool(dhcpv6) if ipv6autoconf is not None: ipv6autoconf = utils.tobool(ipv6autoconf) if network == '': raise ConfigNetworkError(ne.ERR_BAD_BRIDGE, 'Empty network names are not valid') if prefix: if netmask: raise ConfigNetworkError(ne.ERR_BAD_PARAMS, 'Both PREFIX and NETMASK supplied') else: try: netmask = addresses.prefix2netmask(int(prefix)) except ValueError as ve: raise ConfigNetworkError(ne.ERR_BAD_ADDR, "Bad prefix: %s" % ve) logging.debug('validating network...') if network in _netinfo.networks: raise ConfigNetworkError( ne.ERR_USED_BRIDGE, 'Network already exists (%s)' % (network,)) if bonding: _validateInterNetworkCompatibility(_netinfo, vlan, bonding) elif nic: _validateInterNetworkCompatibility(_netinfo, vlan, nic) logging.info('Adding network %s with vlan=%s, bonding=%s, nic=%s, ' 'mtu=%s, bridged=%s, defaultRoute=%s, options=%s', network, vlan, bonding, nic, mtu, bridged, defaultRoute, options) bootproto = options.pop('bootproto', None) net_ent = _objectivizeNetwork( bridge=network if bridged else None, vlan_id=vlan, bonding=bonding, nic=nic, mtu=mtu, ipaddr=ipaddr, netmask=netmask, gateway=gateway, bootproto=bootproto, dhcpv6=dhcpv6, blockingdhcp=blockingdhcp, ipv6addr=ipv6addr, ipv6gateway=ipv6gateway, ipv6autoconf=ipv6autoconf, defaultRoute=defaultRoute, _netinfo=_netinfo, configurator=configurator, opts=options) if bridged and network in _netinfo.bridges: # The bridge already exists, update the configured entity to one level # below and update the mtu of the bridge. # The mtu is updated in the bridge configuration and on all the tap # devices attached to it (for the VMs). # (expecting the bridge running mtu to be updated by the kernel when # the device attached under it has its mtu updated) logging.info("Bridge %s already exists.", network) net_ent_to_configure = net_ent.port _update_mtu_for_an_existing_bridge(network, configurator, mtu) else: net_ent_to_configure = net_ent if net_ent_to_configure is not None: logging.info("Configuring device %s", net_ent_to_configure) net_ent_to_configure.configure(**options) configurator.configureLibvirtNetwork(network, net_ent) if hostQos is not None: configurator.configureQoS(hostQos, net_ent)
def _addNetwork(network, vlan=None, bonding=None, nics=None, ipaddr=None, netmask=None, prefix=None, mtu=None, gateway=None, dhcpv6=None, ipv6addr=None, ipv6gateway=None, ipv6autoconf=None, configurator=None, bondingOptions=None, bridged=True, _netinfo=None, hostQos=None, defaultRoute=None, blockingdhcp=False, **options): nics = nics or () if _netinfo is None: _netinfo = CachingNetInfo() bridged = utils.tobool(bridged) if dhcpv6 is not None: dhcpv6 = utils.tobool(dhcpv6) if ipv6autoconf is not None: ipv6autoconf = utils.tobool(ipv6autoconf) vlan = _vlanToInternalRepresentation(vlan) if network == '': raise ConfigNetworkError(ne.ERR_BAD_BRIDGE, 'Empty network names are not valid') if prefix: if netmask: raise ConfigNetworkError(ne.ERR_BAD_PARAMS, 'Both PREFIX and NETMASK supplied') else: try: netmask = addresses.prefix2netmask(int(prefix)) except ValueError as ve: raise ConfigNetworkError(ne.ERR_BAD_ADDR, "Bad prefix: %s" % ve) logging.debug('validating network...') if network in _netinfo.networks: raise ConfigNetworkError( ne.ERR_USED_BRIDGE, 'Network already exists (%s)' % (network,)) if bonding: _validateInterNetworkCompatibility(_netinfo, vlan, bonding) else: for nic in nics: _validateInterNetworkCompatibility(_netinfo, vlan, nic) # defaultRoute is set either explicitly by the client, OR if we're adding # the management network. # REQUIRED_FOR: clusterLevel<=3.3 # remove reference to constants.LEGACY_MANAGEMENT_NETWORKS if defaultRoute is None: defaultRoute = network in constants.LEGACY_MANAGEMENT_NETWORKS logging.info("Adding network %s with vlan=%s, bonding=%s, nics=%s," " bondingOptions=%s, mtu=%s, bridged=%s, defaultRoute=%s," "options=%s", network, vlan, bonding, nics, bondingOptions, mtu, bridged, defaultRoute, options) if configurator is None: configurator = ConfiguratorClass() bootproto = options.pop('bootproto', None) net_ent = _objectivizeNetwork( bridge=network if bridged else None, vlan_id=vlan, bonding=bonding, bondingOptions=bondingOptions, nics=nics, mtu=mtu, ipaddr=ipaddr, netmask=netmask, gateway=gateway, bootproto=bootproto, dhcpv6=dhcpv6, blockingdhcp=blockingdhcp, ipv6addr=ipv6addr, ipv6gateway=ipv6gateway, ipv6autoconf=ipv6autoconf, defaultRoute=defaultRoute, _netinfo=_netinfo, configurator=configurator, opts=options) if bridged and network in _netinfo.bridges: # The bridge already exists, update the configured entity to one level # below and update the mtu of the bridge. # The mtu is updated in the bridge configuration and on all the tap # devices attached to it (for the VMs). # (expecting the bridge running mtu to be updated by the kernel when # the device attached under it has its mtu updated) logging.info("Bridge %s already exists.", network) net_ent_to_configure = net_ent.port _update_mtu_for_an_existing_bridge(network, configurator, mtu) else: net_ent_to_configure = net_ent if net_ent_to_configure is not None: logging.info("Configuring device %s", net_ent_to_configure) net_ent_to_configure.configure(**options) configurator.configureLibvirtNetwork(network, net_ent) if hostQos is not None: configurator.configureQoS(hostQos, net_ent)