def test__allocate_ips_for_port_2_slaac_pd_subnets(self): subnets = [ { 'cidr': constants.PROVISIONAL_IPV6_PD_PREFIX, 'enable_dhcp': True, 'gateway_ip': '::1', 'id': 'd1a28edd-bd83-480a-bd40-93d036c89f13', 'network_id': 'fbb9b578-95eb-4b79-a116-78e5c4927176', 'ip_version': 6, 'ipv6_address_mode': None, 'ipv6_ra_mode': 'slaac'}, { 'cidr': constants.PROVISIONAL_IPV6_PD_PREFIX, 'enable_dhcp': True, 'gateway_ip': '::1', 'id': 'dc813d3d-ed66-4184-8570-7325c8195e28', 'network_id': 'fbb9b578-95eb-4b79-a116-78e5c4927176', 'ip_version': 6, 'ipv6_address_mode': None, 'ipv6_ra_mode': 'slaac'}] port = {'port': { 'network_id': 'fbb9b578-95eb-4b79-a116-78e5c4927176', 'fixed_ips': n_const.ATTR_NOT_SPECIFIED, 'mac_address': '12:34:56:78:44:ab', 'device_owner': 'compute'}} expected = [] for subnet in subnets: addr = str(ipv6_utils.get_ipv6_addr_by_EUI64( subnet['cidr'], port['port']['mac_address'])) expected.append({'ip_address': addr, 'subnet_id': subnet['id']}) self._test__allocate_ips_for_port(subnets, port, expected)
def _select_dhcp_ips_for_network_ids(self, context, network_ids): if not network_ids: return {} query = context.session.query(models_v2.Port.mac_address, models_v2.Port.network_id, models_v2.IPAllocation.ip_address) query = query.join(models_v2.IPAllocation) query = query.filter(models_v2.Port.network_id.in_(network_ids)) owner = q_const.DEVICE_OWNER_DHCP query = query.filter(models_v2.Port.device_owner == owner) ips = {} for network_id in network_ids: ips[network_id] = [] for mac_address, network_id, ip in query: if (netaddr.IPAddress(ip).version == 6 and not netaddr.IPAddress(ip).is_link_local()): ip = str( ipv6.get_ipv6_addr_by_EUI64(q_const.IPV6_LLA_PREFIX, mac_address)) if ip not in ips[network_id]: ips[network_id].append(ip) return ips
def get_random_ip_address(version=4): if version == 4: ip_string = "10.%d.%d.%d" % (random.randint(3, 254), random.randint(3, 254), random.randint(3, 254)) return netaddr.IPAddress(ip_string) else: ip = ipv6_utils.get_ipv6_addr_by_EUI64("2001:db8::/64", get_random_mac()) return ip
def _calculate_ipv6_eui64_addr(self, context, subnet, mac_addr): prefix = subnet["cidr"] network_id = subnet["network_id"] ip_address = ipv6_utils.get_ipv6_addr_by_EUI64(prefix, mac_addr).format() if not self._check_unique_ip(context, network_id, subnet["id"], ip_address): raise n_exc.IpAddressInUse(net_id=network_id, ip_address=ip_address) return ip_address
def _generate_eui64_address(self, **kwargs): if set(kwargs) != set(["prefix", "mac"]): raise ipam_exc.AddressCalculationFailure( address_type="eui-64", reason=_("must provide exactly 2 arguments - cidr and MAC") ) prefix = kwargs["prefix"] mac_address = kwargs["mac"] return ipv6_utils.get_ipv6_addr_by_EUI64(prefix, mac_address)
def _build_ipv4v6_mac_ip_list(self, mac, ip_address, mac_ipv4_pairs, mac_ipv6_pairs): mac = str(netaddr.EUI(mac, dialect=mac_iptables)) if netaddr.IPNetwork(ip_address).version == 4: mac_ipv4_pairs.append((mac, ip_address)) else: mac_ipv6_pairs.append((mac, ip_address)) lla = str(ipv6_utils.get_ipv6_addr_by_EUI64(constants.IPv6_LLA_PREFIX, mac)) mac_ipv6_pairs.append((mac, lla))
def _generate_eui64_address(self, **kwargs): if set(kwargs) != set(['prefix', 'mac']): raise ipam_exc.AddressCalculationFailure( address_type='eui-64', reason=_('must provide exactly 2 arguments - cidr and MAC')) prefix = kwargs['prefix'] mac_address = kwargs['mac'] return ipv6_utils.get_ipv6_addr_by_EUI64(prefix, mac_address)
def test_automatic_address_request_eui64(self): subnet_cidr = '2607:f0d0:1002:51::/64' port_mac = 'aa:bb:cc:dd:ee:ff' eui_addr = str(ipv6_utils.get_ipv6_addr_by_EUI64( subnet_cidr, port_mac)) request = ipam_req.AutomaticAddressRequest(address_type=self.EUI64, prefix=subnet_cidr, mac=port_mac) self.assertEqual(request.address, netaddr.IPAddress(eui_addr))
def get_random_ip_address(version=4): if version == 4: ip_string = '10.%d.%d.%d' % (random.randint( 3, 254), random.randint(3, 254), random.randint(3, 254)) return netaddr.IPAddress(ip_string) else: ip = ipv6_utils.get_ipv6_addr_by_EUI64('2001:db8::/64', get_random_mac()) return ip
def test_automatic_address_request_eui64(self): subnet_cidr = '2607:f0d0:1002:51::/64' port_mac = 'aa:bb:cc:dd:ee:ff' eui_addr = str(ipv6_utils.get_ipv6_addr_by_EUI64(subnet_cidr, port_mac)) request = ipam_req.AutomaticAddressRequest( address_type=self.EUI64, prefix=subnet_cidr, mac=port_mac) self.assertEqual(request.address, netaddr.IPAddress(eui_addr))
def _build_ipv4v6_mac_ip_list(self, mac, ip_address, mac_ipv4_pairs, mac_ipv6_pairs): mac = str(netaddr.EUI(mac, dialect=mac_iptables)) if netaddr.IPNetwork(ip_address).version == 4: mac_ipv4_pairs.append((mac, ip_address)) else: mac_ipv6_pairs.append((mac, ip_address)) lla = str(ipv6_utils.get_ipv6_addr_by_EUI64( n_const.IPV6_LLA_PREFIX, mac)) mac_ipv6_pairs.append((mac, lla))
def _calculate_ipv6_eui64_addr(self, context, subnet, mac_addr): prefix = subnet['cidr'] network_id = subnet['network_id'] ip_address = ipv6_utils.get_ipv6_addr_by_EUI64(prefix, mac_addr).format() if not self._check_unique_ip(context, network_id, subnet['id'], ip_address): raise n_exc.IpAddressInUse(net_id=network_id, ip_address=ip_address) return ip_address
def __init__(self, port_dict, ovs_port, vlan_tag): self.id = port_dict["device"] self.vlan_tag = vlan_tag self.mac = ovs_port.vif_mac self.lla_address = str(ipv6_utils.get_ipv6_addr_by_EUI64(constants.IPV6_LLA_PREFIX, self.mac)) self.ofport = ovs_port.ofport self.sec_groups = list() self.fixed_ips = port_dict.get("fixed_ips", []) self.neutron_port_dict = port_dict.copy() self.allowed_pairs_v4 = self._get_allowed_pairs(port_dict, version=4) self.allowed_pairs_v6 = self._get_allowed_pairs(port_dict, version=6)
def __init__(self, port_dict, ovs_port): self.id = port_dict['device'] self.mac = ovs_port.vif_mac self.lla_address = str( ipv6_utils.get_ipv6_addr_by_EUI64(constants.IPV6_LLA_PREFIX, self.mac)) self.ofport = ovs_port.ofport self.sec_groups = list() self.fixed_ips = port_dict.get('fixed_ips', []) self.neutron_port_dict = port_dict.copy() self.allowed_pairs_v4 = self._get_allowed_pairs(port_dict, version=4) self.allowed_pairs_v6 = self._get_allowed_pairs(port_dict, version=6)
def _get_lla_gateway_ip_for_subnet(self, context, subnet): query = context.session.query(models_v2.Port.mac_address) query = query.join(models_v2.IPAllocation) query = query.filter(models_v2.IPAllocation.subnet_id == subnet["id"]) query = query.filter(models_v2.IPAllocation.ip_address == subnet["gateway_ip"]) query = query.filter(models_v2.Port.device_owner.in_(n_const.ROUTER_INTERFACE_OWNERS)) try: mac_address = query.one()[0] except (exc.NoResultFound, exc.MultipleResultsFound): LOG.warning(_LW("No valid gateway port on subnet %s is " "found for IPv6 RA"), subnet["id"]) return lla_ip = str(ipv6.get_ipv6_addr_by_EUI64(n_const.IPV6_LLA_PREFIX, mac_address)) return lla_ip
def test_allocate_eui64_ip(self): mocks = self._prepare_ipam() ip = {'subnet_id': self._gen_subnet_id(), 'subnet_cidr': '2001:470:abcd::/64', 'mac': '6c:62:6d:de:cf:49', 'eui64_address': True} eui64_ip = ipv6_utils.get_ipv6_addr_by_EUI64(ip['subnet_cidr'], ip['mac']) mocks['ipam']._ipam_allocate_ips(mock.ANY, mocks['driver'], mock.ANY, [ip]) request = mocks['subnet'].allocate.call_args[0][0] self.assertIsInstance(request, ipam_req.AutomaticAddressRequest) self.assertEqual(eui64_ip, request.address)
def _get_lla_gateway_ip_for_subnet(self, context, subnet): query = context.session.query(models_v2.Port) query = query.join(models_v2.IPAllocation) query = query.filter(models_v2.IPAllocation.subnet_id == subnet["id"]) query = query.filter(models_v2.IPAllocation.ip_address == subnet["gateway_ip"]) query = query.filter(models_v2.Port.device_owner == q_const.DEVICE_OWNER_ROUTER_INTF) try: gateway_port = query.one() except (exc.NoResultFound, exc.MultipleResultsFound): LOG.warn(_("No valid gateway port on subnet %s is " "found for IPv6 RA"), subnet["id"]) return mac_address = gateway_port["mac_address"] lla_ip = str(ipv6.get_ipv6_addr_by_EUI64(q_const.IPV6_LLA_PREFIX, mac_address)) return lla_ip
def test_allocate_eui64_ip(self): mocks = self._prepare_ipam() ip = { "subnet_id": self._gen_subnet_id(), "subnet_cidr": "2001:470:abcd::/64", "mac": "6c:62:6d:de:cf:49", "eui64_address": True, } eui64_ip = ipv6_utils.get_ipv6_addr_by_EUI64(ip["subnet_cidr"], ip["mac"]) mocks["ipam"]._ipam_allocate_ips(mock.ANY, mocks["driver"], mock.ANY, [ip]) request = mocks["subnet"].allocate.call_args[0][0] self.assertIsInstance(request, ipam_req.AutomaticAddressRequest) self.assertEqual(eui64_ip, request.address)
def test_allocate_eui64_ip(self): mocks = self._prepare_ipam() ip = {'subnet_id': self._gen_subnet_id(), 'subnet_cidr': '2001:470:abcd::/64', 'mac': '6c:62:6d:de:cf:49', 'eui64_address': True} eui64_ip = ipv6_utils.get_ipv6_addr_by_EUI64(ip['subnet_cidr'], ip['mac']) mocks['ipam']._ipam_allocate_ips(mock.ANY, mocks['driver'], mock.ANY, [ip]) request = mocks['subnets'].allocate.call_args[0][0] self.assertIsInstance(request, ipam_req.AutomaticAddressRequest) self.assertEqual(eui64_ip, request.address)
def _get_lla_gateway_ip_for_subnet(self, context, subnet): query = context.session.query(models_v2.Port.mac_address) query = query.join(models_v2.IPAllocation) query = query.filter(models_v2.IPAllocation.subnet_id == subnet['id']) query = query.filter( models_v2.IPAllocation.ip_address == subnet['gateway_ip']) query = query.filter( models_v2.Port.device_owner.in_(q_const.ROUTER_INTERFACE_OWNERS)) try: mac_address = query.one()[0] except (exc.NoResultFound, exc.MultipleResultsFound): LOG.warn( _LW('No valid gateway port on subnet %s is ' 'found for IPv6 RA'), subnet['id']) return lla_ip = str( ipv6.get_ipv6_addr_by_EUI64(q_const.IPV6_LLA_PREFIX, mac_address)) return lla_ip
def _get_lla_gateway_ip_for_subnet(self, context, subnet): query = context.session.query(models_v2.Port.mac_address) query = query.join(models_v2.IPAllocation) query = query.filter( models_v2.IPAllocation.subnet_id == subnet['id']) query = query.filter( models_v2.IPAllocation.ip_address == subnet['gateway_ip']) query = query.filter(or_(models_v2.Port.device_owner == q_const.DEVICE_OWNER_ROUTER_INTF, models_v2.Port.device_owner == q_const.DEVICE_OWNER_DVR_INTERFACE)) try: mac_address = query.one()[0] except (exc.NoResultFound, exc.MultipleResultsFound): LOG.warn(_LW('No valid gateway port on subnet %s is ' 'found for IPv6 RA'), subnet['id']) return lla_ip = str(ipv6.get_ipv6_addr_by_EUI64( q_const.IPV6_LLA_PREFIX, mac_address)) return lla_ip
def test_generate_IPv6_with_bad_mac(self): bad_mac = '00:16:3e:33:44:5Z' prefix = '2001:db8::' self.assertRaises( TypeError, lambda: ipv6_utils.get_ipv6_addr_by_EUI64(prefix, bad_mac))
def _get_lla(mac): lla = ipv6_utils.get_ipv6_addr_by_EUI64(l3_constants.IPV6_LLA_PREFIX, mac) return lla
def _get_lla(mac): lla = ipv6_utils.get_ipv6_addr_by_EUI64(n_const.IPv6_LLA_PREFIX, mac) return lla
def test_generate_IPv6_by_EUI64(self): addr = ipv6_utils.get_ipv6_addr_by_EUI64('2001:db8::', '00:16:3e:33:44:55') self.assertEqual('2001:db8::216:3eff:fe33:4455', addr.format())
def test_generate_IPv6_with_bad_mac(self): bad_mac = '00:16:3e:33:44:5Z' prefix = '2001:db8::' self.assertRaises(TypeError, lambda: ipv6_utils.get_ipv6_addr_by_EUI64(prefix, bad_mac))
def test_generate_IPv6_with_IPv4_prefix(self): ipv4_prefix = '10.0.8' mac = '00:16:3e:33:44:55' self.assertRaises(TypeError, lambda: ipv6_utils.get_ipv6_addr_by_EUI64(ipv4_prefix, mac))
def test_valid_eui64_addresses(self): ips = ('fffe::0cad:12ff:fe44:5566', ipv6_utils.get_ipv6_addr_by_EUI64('2001:db8::', '00:16:3e:33:44:55')) self._test_eui_64(ips, True)
def test_generate_IPv6_with_bad_prefix(self): mac = '00:16:3e:33:44:55' bad_prefix = 'bb' self.assertRaises(TypeError, lambda: ipv6_utils.get_ipv6_addr_by_EUI64(bad_prefix, mac))
def test_generate_IPv6_with_IPv4_prefix(self): ipv4_prefix = '10.0.8' mac = '00:16:3e:33:44:55' self.assertRaises( TypeError, lambda: ipv6_utils.get_ipv6_addr_by_EUI64(ipv4_prefix, mac))
def test_generate_IPv6_with_bad_prefix(self): mac = '00:16:3e:33:44:55' bad_prefix = 'bb' self.assertRaises( TypeError, lambda: ipv6_utils.get_ipv6_addr_by_EUI64(bad_prefix, mac))
def test_generate_IPv6_with_error_prefix_type(self): mac = '00:16:3e:33:44:55' prefix = 123 self.assertRaises( TypeError, lambda: ipv6_utils.get_ipv6_addr_by_EUI64(prefix, mac))
def test_generate_IPv6_with_error_prefix_type(self): mac = '00:16:3e:33:44:55' prefix = 123 self.assertRaises(TypeError, lambda: ipv6_utils.get_ipv6_addr_by_EUI64(prefix, mac))