def port_bind(endpoint_id, port, subnets, network=None, vm_port=None, segmentation_id=None): """Binds the Neutron port to the network interface on the host. :param endpoint_id: the ID of the endpoint as string :param port: the container Neutron port dictionary as returned by python-neutronclient :param subnets: an iterable of all the Neutron subnets which the endpoint is trying to join :param network: the Neutron network which the endpoint is trying to join :param vm_port: the Nova instance port dictionary, as returned by python-neutronclient. Container is running inside this instance (either ipvlan/macvlan or a subport) :param segmentation_id: ID of the segment for container traffic isolation) :returns: the tuple of the names of the veth pair and the tuple of stdout and stderr returned by processutils.execute invoked with the executable script for binding :raises: kuryr.common.exceptions.VethCreationFailure, processutils.ProcessExecutionError """ ip = utils.get_ipdb() port_id = port['id'] _, devname = utils.get_veth_pair_names(port_id) link_iface = nested.get_link_iface(vm_port) with ip.create(ifname=devname, kind=KIND, link=ip.interfaces[link_iface], address=port.get(utils.MAC_ADDRESS_KEY), vlan_id=segmentation_id) as container_iface: utils._configure_container_iface( container_iface, subnets, fixed_ips=port.get(utils.FIXED_IP_KEY)) return None, devname, ('', None)
def port_bind(endpoint_id, port, subnets, network=None, vm_port=None, segmentation_id=None, **kwargs): """Binds the Neutron port to the network interface on the host. :param endpoint_id: the ID of the endpoint as string :param port: the container Neutron port dictionary as returned by python-neutronclient :param subnets: an iterable of all the Neutron subnets which the endpoint is trying to join :param network: the Neutron network which the endpoint is trying to join :param vm_port: the Nova instance port dictionary, as returned by python-neutronclient. Container port under binding is running inside this instance (either ipvlan/macvlan or a subport) :param segmentation_id: ID of the segment for container traffic isolation) :param kwargs: Additional driver-specific arguments :returns: the tuple of the names of the veth pair and the tuple of stdout and stderr returned by processutils.execute invoked with the executable script for binding :raises: kuryr.common.exceptions.VethCreationFailure, processutils.ProcessExecutionError """ ip = utils.get_ipdb() port_id = port['id'] host_ifname, container_ifname = utils.get_veth_pair_names(port_id) mtu = utils.get_mtu_from_network(network) try: with ip.create(ifname=host_ifname, kind=KIND, reuse=True, peer=container_ifname) as host_veth: if not utils.is_up(host_veth): host_veth.up() with ip.interfaces[container_ifname] as container_veth: utils._configure_container_iface( container_veth, subnets, fixed_ips=port.get(utils.FIXED_IP_KEY), mtu=mtu, hwaddr=port[utils.MAC_ADDRESS_KEY].lower()) except pyroute2.CreateException: raise exceptions.VethCreationFailure( 'Virtual device creation failed.') except pyroute2.CommitException: raise exceptions.VethCreationFailure( 'Could not configure the container virtual device networking.') try: stdout, stderr = _configure_host_iface( host_ifname, endpoint_id, port_id, port['network_id'], port.get('project_id') or port['tenant_id'], port[utils.MAC_ADDRESS_KEY], kind=port.get(constants.VIF_TYPE_KEY), details=port.get(constants.VIF_DETAILS_KEY)) except Exception: with excutils.save_and_reraise_exception(): utils.remove_device(host_ifname) return host_ifname, container_ifname, (stdout, stderr)
def port_bind(endpoint_id, port, subnets, network=None, vm_port=None, segmentation_id=None): """Binds the Neutron port to the network interface on the host. :param endpoint_id: the ID of the endpoint as string :param port: the container Neutron port dictionary as returned by python-neutronclient :param subnets: an iterable of all the Neutron subnets which the endpoint is trying to join :param network: the Neutron network which the endpoint is trying to join :param vm_port: the Nova instance dictionary, as returned by python-neutronclient. Container port under binding is running inside this instance (either ipvlan/macvlan or a subport) :param segmentation_id: ID of the segment for container traffic isolation) :returns: the tuple of the names of the veth pair and the tuple of stdout and stderr returned by processutils.execute invoked with the executable script for binding :raises: kuryr.common.exceptions.VethCreationFailure, processutils.ProcessExecutionError """ ip = utils.get_ipdb() port_id = port['id'] host_ifname, container_ifname = utils.get_veth_pair_names(port_id) mtu = utils.get_mtu_from_network(network) try: with ip.create(ifname=host_ifname, kind=KIND, reuse=True, peer=container_ifname) as host_veth: if not utils.is_up(host_veth): host_veth.up() with ip.interfaces[container_ifname] as container_veth: utils._configure_container_iface( container_veth, subnets, fixed_ips=port.get(utils.FIXED_IP_KEY), mtu=mtu, hwaddr=port[utils.MAC_ADDRESS_KEY].lower()) except pyroute2.CreateException: raise exceptions.VethCreationFailure( 'Virtual device creation failed.') except pyroute2.CommitException: raise exceptions.VethCreationFailure( 'Could not configure the container virtual device networking.') try: stdout, stderr = _configure_host_iface( host_ifname, endpoint_id, port_id, port['network_id'], port.get('project_id') or port['tenant_id'], port[utils.MAC_ADDRESS_KEY], kind=port.get(constants.VIF_TYPE_KEY), details=port.get(constants.VIF_DETAILS_KEY)) except Exception: with excutils.save_and_reraise_exception(): utils.remove_device(host_ifname) return host_ifname, container_ifname, (stdout, stderr)
def test__configure_container_iface(self, addrs, subnet_ids, already_up, mtu, mac, mock_is_up): subnets = [{ 'allocation_pools': [{ 'end': '10.11.0.254', 'start': '10.11.0.2' }], 'cidr': '10.11.0.0/26', 'created_at': '2016-09-27T07:55:12', 'description': '', 'dns_nameservers': [], 'enable_dhcp': True, 'gateway_ip': '10.11.0.1', 'host_routes': [], 'id': '0a6eab28-9dc1-46c0-997c-cb9f66f6081f', 'ip_version': 4, 'ipv6_address_mode': None, 'ipv6_ra_mode': None, 'name': 'subtest', 'network_id': '90146ed2-c3ce-4001-866e-e97e513530a3', 'revision': 2, 'service_types': [], 'subnetpool_id': None, 'tenant_id': '0c0d1f46fa8d485d9534ea0e35f37bd3', 'updated_at': '2016-09-27T07:55:12' }, { 'allocation_pools': [{ 'end': '10.10.0.254', 'start': '10.10.0.2' }], 'cidr': '10.10.0.0/24', 'created_at': '2016-09-27T08:57:13', 'description': '', 'dns_nameservers': [], 'enable_dhcp': True, 'gateway_ip': '10.10.0.1', 'host_routes': [], 'id': '384ac9fc-eefa-4399-8d88-1181433e33b1', 'ip_version': 4, 'ipv6_address_mode': None, 'ipv6_ra_mode': None, 'name': '10.10.0.0/24', 'network_id': 'bfb2f525-bedf-48ed-b125-102ee7920253', 'revision': 2, 'service_types': [], 'subnetpool_id': None, 'tenant_id': '51b66b97a12f42a990452967d2c555ac', 'updated_at': '2016-09-27T08:57:13' }] fake_iface = mock.Mock(spec=pyroute2.ipdb.interfaces.Interface) _set_mtu = mock.Mock() _set_address = mock.Mock() fake_iface.attach_mock(_set_mtu, 'set_mtu') fake_iface.attach_mock(_set_address, 'set_address') mock_is_up.return_value = already_up fixed_ips = [] for ip, subnet_id in zip(addrs, subnet_ids): fixed_ips.append({ utils.IP_ADDRESS_KEY: ip, utils.SUBNET_ID_KEY: subnet_id }) utils._configure_container_iface(fake_iface, subnets, fixed_ips, mtu=mtu, hwaddr=mac) subnets_prefix_by_id = dict( (subnet['id'], int(subnet['cidr'].split('/')[1])) for subnet in subnets) for ip, subnet_id in zip(addrs, subnet_ids): fake_iface.add_ip.assert_any_call(ip, subnets_prefix_by_id[subnet_id]) if already_up: fake_iface.up.assert_not_called() else: fake_iface.up.assert_called_once() if mtu is None: fake_iface.set_mtu.assert_not_called() else: fake_iface.set_mtu.assert_called_with(mtu) if mac is None: fake_iface.set_address.assert_not_called() else: fake_iface.set_address.assert_called_with(mac)
def port_bind(endpoint_id, port, subnets, network=None, nested_port=None): """Binds the Neutron port to the network interface on the host. :param endpoint_id: the ID of the endpoint as string :param port: the instance Neutron port dictionary as returned by python-neutronclient :param subnets: an iterable of all the Neutron subnets which the endpoint is trying to join :param network: the Neutron network which the endpoint is trying to join :param nested_port: the dictionary, as returned by python-neutronclient, of the port that that is used when running inside another instance (either ipvlan/macvlan or a subport) :returns: the tuple of the names of the veth pair and the tuple of stdout and stderr returned by processutils.execute invoked with the executable script for binding :raises: kuryr.common.exceptions.VethCreationFailure, processutils.ProcessExecutionError """ ip = utils.get_ipdb() port_id = port['id'] host_ifname, container_ifname = utils.get_veth_pair_names(port_id) if network is None: mtu = DEFAULT_NETWORK_MTU else: mtu = network.get('mtu', DEFAULT_NETWORK_MTU) try: with ip.create(ifname=host_ifname, kind=KIND, reuse=True, peer=container_ifname) as host_veth: if not utils.is_up(host_veth): host_veth.up() with ip.interfaces[container_ifname] as container_veth: utils._configure_container_iface( container_veth, subnets, fixed_ips=port.get(utils.FIXED_IP_KEY), mtu=mtu, hwaddr=port[utils.MAC_ADDRESS_KEY].lower()) except pyroute2.CreateException: raise exceptions.VethCreationFailure('Virtual device creation failed.') except pyroute2.CommitException: raise exceptions.VethCreationFailure( 'Could not configure the container virtual device networking.') try: stdout, stderr = _configure_host_iface( host_ifname, endpoint_id, port_id, port['network_id'], port['tenant_id'], port[utils.MAC_ADDRESS_KEY], kind=port.get(VIF_TYPE_KEY), details=port.get(VIF_DETAILS_KEY)) except Exception: with excutils.save_and_reraise_exception(): utils.remove_device(host_ifname) return host_ifname, container_ifname, (stdout, stderr)
def test__configure_container_iface( self, addrs, subnet_ids, already_up, mtu, mac, mock_is_up): subnets = [{ 'allocation_pools': [{'end': '10.11.0.254', 'start': '10.11.0.2'}], 'cidr': '10.11.0.0/26', 'created_at': '2016-09-27T07:55:12', 'description': '', 'dns_nameservers': [], 'enable_dhcp': True, 'gateway_ip': '10.11.0.1', 'host_routes': [], 'id': '0a6eab28-9dc1-46c0-997c-cb9f66f6081f', 'ip_version': 4, 'ipv6_address_mode': None, 'ipv6_ra_mode': None, 'name': 'subtest', 'network_id': '90146ed2-c3ce-4001-866e-e97e513530a3', 'revision': 2, 'service_types': [], 'subnetpool_id': None, 'tenant_id': '0c0d1f46fa8d485d9534ea0e35f37bd3', 'updated_at': '2016-09-27T07:55:12' }, { 'allocation_pools': [{'end': '10.10.0.254', 'start': '10.10.0.2'}], 'cidr': '10.10.0.0/24', 'created_at': '2016-09-27T08:57:13', 'description': '', 'dns_nameservers': [], 'enable_dhcp': True, 'gateway_ip': '10.10.0.1', 'host_routes': [], 'id': '384ac9fc-eefa-4399-8d88-1181433e33b1', 'ip_version': 4, 'ipv6_address_mode': None, 'ipv6_ra_mode': None, 'name': '10.10.0.0/24', 'network_id': 'bfb2f525-bedf-48ed-b125-102ee7920253', 'revision': 2, 'service_types': [], 'subnetpool_id': None, 'tenant_id': '51b66b97a12f42a990452967d2c555ac', 'updated_at': '2016-09-27T08:57:13'}] fake_iface = mock.Mock(spec=pyroute2.ipdb.interface.Interface) _set_mtu = mock.Mock() _set_address = mock.Mock() fake_iface.attach_mock(_set_mtu, 'set_mtu') fake_iface.attach_mock(_set_address, 'set_address') mock_is_up.return_value = already_up fixed_ips = [] for ip, subnet_id in zip(addrs, subnet_ids): fixed_ips.append({ utils.IP_ADDRESS_KEY: ip, utils.SUBNET_ID_KEY: subnet_id}) utils._configure_container_iface( fake_iface, subnets, fixed_ips, mtu=mtu, hwaddr=mac) subnets_prefix_by_id = dict( (subnet['id'], int(subnet['cidr'].split('/')[1])) for subnet in subnets) for ip, subnet_id in zip(addrs, subnet_ids): fake_iface.add_ip.assert_any_call( ip, subnets_prefix_by_id[subnet_id]) if already_up: fake_iface.up.assert_not_called() else: fake_iface.up.assert_called_once() if mtu is None: fake_iface.set_mtu.assert_not_called() else: fake_iface.set_mtu.assert_called_with(mtu) if mac is None: fake_iface.set_address.assert_not_called() else: fake_iface.set_address.assert_called_with(mac)