def assertStaticIPv4(self, netattrs, ipinfo): requires_ipaddress() address = netattrs['ipaddr'] netmask = (netattrs.get('netmask') or prefix2netmask(int(netattrs.get('prefix')))) self.assertEqual(address, ipinfo['addr']) self.assertEqual(netmask, ipinfo['netmask']) ipv4 = ipaddress.IPv4Interface(u'{}/{}'.format(address, netmask)) self.assertIn(str(ipv4.with_prefixlen), ipinfo['ipv4addrs'])
def _normalize_address(config_copy): for net_name, net_attr in six.iteritems(config_copy.networks): prefix = net_attr.pop('prefix', None) if prefix is not None: net_attr['netmask'] = addresses.prefix2netmask(int(prefix)) if 'ipv6addr' in net_attr: net_attr['ipv6addr'] = [net_attr['ipv6addr']] if 'defaultRoute' not in net_attr: net_attr['defaultRoute'] = net_name in \ constants.LEGACY_MANAGEMENT_NETWORKS
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 _add_network(network, configurator, nameservers, 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: _validate_inter_network_compatibility(_netinfo, vlan, bonding) elif nic: _validate_inter_network_compatibility(_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 = _objectivize_network( 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, nameservers=nameservers, _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 _add_network(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: _validate_inter_network_compatibility(_netinfo, vlan, bonding) elif nic: _validate_inter_network_compatibility(_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 = _objectivize_network( 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)