def port_unbind(endpoint_id, neutron_port): """Unbinds the Neutron port from the network interface on the host. :param endpoint_id: the ID of the Docker container as string :param neutron_port: a port dictionary returned from python-neutronclient :returns: the tuple of stdout and stderr returned by processutils.execute invoked with the executable script for unbinding :raises: processutils.ProcessExecutionError, pyroute2.NetlinkError """ vif_type = neutron_port.get(VIF_TYPE_KEY, FALLBACK_VIF_TYPE) vif_details = utils.string_mappings(neutron_port.get(VIF_DETAILS_KEY)) unbinding_exec_path = os.path.join(cfg.CONF.bindir, vif_type) port_id = neutron_port['id'] ifname, _ = utils.get_veth_pair_names(port_id) mac_address = neutron_port['mac_address'] stdout, stderr = processutils.execute( unbinding_exec_path, UNBINDING_SUBCOMMAND, port_id, ifname, endpoint_id, mac_address, vif_details, run_as_root=True) try: cleanup_veth(ifname) except pyroute2.NetlinkError: raise exceptions.VethDeleteionFailure( 'Deleting the veth pair failed.') return (stdout, stderr)
def port_unbind(endpoint_id, neutron_port, **kwargs): """Unbinds the Neutron port from the network interface on the host. :param endpoint_id: the ID of the Docker container as string :param neutron_port: a port dictionary returned from python-neutronclient :param kwargs: Additional driver-specific arguments :returns: the tuple of stdout and stderr returned by processutils.execute invoked with the executable script for unbinding :raises: processutils.ProcessExecutionError, pyroute2.NetlinkError """ vif_type = neutron_port.get(constants.VIF_TYPE_KEY, constants.FALLBACK_VIF_TYPE) vif_details = lib_utils.string_mappings(neutron_port.get( constants.VIF_DETAILS_KEY)) unbinding_exec_path = os.path.join(cfg.CONF.bindir, vif_type) port_id = neutron_port['id'] ifname, _ = utils.get_veth_pair_names(port_id) mac_address = neutron_port['mac_address'] network_id = neutron_port['network_id'] stdout, stderr = processutils.execute( unbinding_exec_path, constants.UNBINDING_SUBCOMMAND, port_id, ifname, endpoint_id, mac_address, vif_details, network_id, run_as_root=True) try: utils.remove_device(ifname) except pyroute2.NetlinkError: raise exceptions.VethDeletionFailure( 'Deleting the veth pair failed.') return (stdout, stderr)
def _configure_host_iface(ifname, endpoint_id, port_id, net_id, project_id, hwaddr, kind=None, details=None): """Configures the interface that is placed on the default net ns :param ifname: the name of the interface to configure :param endpoint_id: the identifier of the endpoint :param port_id: the Neutron uuid of the port to which this interface is to be bound :param net_id: the Neutron uuid of the network the port is part of :param project_id: the Keystone project the binding is made for :param hwaddr: the interface hardware address :param kind: the Neutorn port vif_type :param details: Neutron vif details """ if kind is None: kind = constants.FALLBACK_VIF_TYPE binding_exec_path = os.path.join(cfg.CONF.bindir, kind) if not os.path.exists(binding_exec_path): raise exceptions.BindingNotSupportedFailure( "vif_type({0}) is not supported. A binding script for this type " "can't be found".format(kind)) stdout, stderr = processutils.execute( binding_exec_path, constants.BINDING_SUBCOMMAND, port_id, ifname, endpoint_id, hwaddr, net_id, project_id, lib_utils.string_mappings(details), run_as_root=True) return stdout, stderr
def test_string_mappings(self): fake_mapping_list = {'fake_key': 'fake_value'} fake_result = '"{\'fake_key\': \'fake_value\'}"' self.assertEqual(fake_result, utils.string_mappings(fake_mapping_list))
def port_bind(endpoint_id, neutron_port, neutron_subnets, neutron_network=None): """Binds the Neutron port to the network interface on the host. :param endpoint_id: the ID of the endpoint as string :param neutron_port: a port dictionary returned from python-neutronclient :param neutron_subnets: a list of all subnets under network to which this endpoint is trying to join :param neutron_network: network which this endpoint is trying to join :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 = get_ipdb() port_id = neutron_port['id'] ifname, peer_name = utils.get_veth_pair_names(port_id) subnets_dict = {subnet['id']: subnet for subnet in neutron_subnets} if neutron_network is None: mtu = DEFAULT_NETWORK_MTU else: mtu = neutron_network.get('mtu', DEFAULT_NETWORK_MTU) try: with ip.create(ifname=ifname, kind=KIND_VETH, reuse=True, peer=peer_name) as host_veth: if not _is_up(host_veth): host_veth.up() with ip.interfaces[peer_name] as peer_veth: fixed_ips = neutron_port.get(FIXED_IP_KEY, []) if not fixed_ips and (IP_ADDRESS_KEY in neutron_port): peer_veth.add_ip(neutron_port[IP_ADDRESS_KEY]) for fixed_ip in fixed_ips: if IP_ADDRESS_KEY in fixed_ip and (SUBNET_ID_KEY in fixed_ip): subnet_id = fixed_ip[SUBNET_ID_KEY] subnet = subnets_dict[subnet_id] cidr = netaddr.IPNetwork(subnet['cidr']) peer_veth.add_ip(fixed_ip[IP_ADDRESS_KEY], cidr.prefixlen) peer_veth.set_mtu(mtu) peer_veth.address = neutron_port[MAC_ADDRESS_KEY].lower() if not _is_up(peer_veth): peer_veth.up() except pyroute2.CreateException: raise exceptions.VethCreationFailure( 'Creating the veth pair was failed.') except pyroute2.CommitException: raise exceptions.VethCreationFailure( 'Could not configure the veth endpoint for the container.') vif_type = neutron_port.get(VIF_TYPE_KEY, FALLBACK_VIF_TYPE) vif_details = utils.string_mappings(neutron_port.get(VIF_DETAILS_KEY)) binding_exec_path = os.path.join(cfg.CONF.bindir, vif_type) if not os.path.exists(binding_exec_path): cleanup_veth(ifname) raise exceptions.BindingNotSupportedFailure( "vif_type({0}) is not supported. A binding script for " "this type can't be found.".format(vif_type)) port_id = neutron_port['id'] network_id = neutron_port['network_id'] tenant_id = neutron_port['tenant_id'] mac_address = neutron_port['mac_address'] try: stdout, stderr = processutils.execute( binding_exec_path, BINDING_SUBCOMMAND, port_id, ifname, endpoint_id, mac_address, network_id, tenant_id, vif_details, run_as_root=True) except processutils.ProcessExecutionError: with excutils.save_and_reraise_exception(): cleanup_veth(ifname) return (ifname, peer_name, (stdout, stderr))