def _bind_router_on_available_edge(self, context, router_id): conflict_network_ids, conflict_router_ids, intf_num = ( self._get_conflict_network_and_router_ids_by_intf(context, router_id)) conflict_network_ids_by_ext_net = ( self._get_conflict_network_ids_by_ext_net(context, router_id)) conflict_network_ids.extend(conflict_network_ids_by_ext_net) conflict_router_ids_by_ext_net = ( self._get_conflict_router_ids_by_ext_net(context, conflict_network_ids)) conflict_router_ids.extend(conflict_router_ids_by_ext_net) optional_router_ids, conflict_router_ids_by_gw = ( self._get_optional_and_conflict_router_ids_by_gw( context, router_id)) conflict_router_ids.extend(conflict_router_ids_by_gw) conflict_router_ids = list(set(conflict_router_ids)) new = self.edge_manager.bind_router_on_available_edge( context, router_id, optional_router_ids, conflict_router_ids, conflict_network_ids, intf_num) # configure metadata service on the router. metadata_proxy_handler = self.plugin.metadata_proxy_handler if metadata_proxy_handler and new: metadata_proxy_handler.configure_router_edge(router_id) edge_id = edge_utils.get_router_edge_id(context, router_id) with lockutils.lock(str(edge_id), lock_file_prefix=NSXV_ROUTER_RECONFIG, external=True): # add all internal interfaces of the router on edge intf_net_ids = self._get_internal_network_ids_by_router( context, router_id) for network_id in intf_net_ids: address_groups = self.plugin._get_address_groups( context, router_id, network_id) edge_utils.update_internal_interface( self.nsx_v, context, router_id, network_id, address_groups)
def remove_router_interface(self, context, router_id, interface_info): edge_id = edge_utils.get_router_edge_id(context, router_id) with locking.LockManager.get_lock( str(edge_id), lock_file_prefix=NSXV_ROUTER_RECONFIG, external=True): info = super( nsx_v.NsxVPluginV2, self.plugin).remove_router_interface( context, router_id, interface_info) subnet = self.plugin.get_subnet(context, info['subnet_id']) network_id = subnet['network_id'] router_ids = self.edge_manager.get_routers_on_same_edge( context, router_id) self._update_nat_rules_on_routers(context, router_id, router_ids) self._update_subnets_and_dnat_firewall_on_routers( context, router_id, router_ids, allow_external=True) ports = self.plugin._get_router_interface_ports_by_network( context, router_id, network_id) if not ports: edge_utils.delete_interface(self.nsx_v, context, router_id, network_id) # unbind all services if no interfaces attached to the router if not self.plugin._get_internal_network_ids_by_router( context, router_id): self._remove_router_services_on_edge(context, router_id) self._unbind_router_on_edge(context, router_id) else: address_groups = self.plugin._get_address_groups( context, router_id, network_id) edge_utils.update_internal_interface( self.nsx_v, context, router_id, network_id, address_groups) return info
def add_router_interface(self, context, router_id, interface_info): self.plugin._check_intf_number_of_router(context, router_id) info = super(nsx_v.NsxVPluginV2, self.plugin).add_router_interface( context, router_id, interface_info) router_db = self.plugin._get_router(context, router_id) subnet = self.plugin.get_subnet(context, info['subnet_id']) network_id = subnet['network_id'] address_groups = self.plugin._get_address_groups( context, router_id, network_id) edge_utils.update_internal_interface( self.nsx_v, context, router_id, network_id, address_groups) # Update edge's firewall rules to accept subnets flows. self.plugin._update_subnets_and_dnat_firewall(context, router_db) if router_db.gw_port and router_db.enable_snat: # Update Nat rules on external edge vnic self.plugin._update_nat_rules(context, router_db) return info
def remove_router_interface(self, context, router_id, interface_info): # If a loadbalancer is attached to this Edge appliance, we cannot # detach the subnet from the exclusive router. subnet = interface_info.get('subnet_id') if not subnet and interface_info.get('port_id'): port = self.plugin.get_port(context, interface_info['port_id']) port_subnets = [ fixed_ip['subnet_id'] for fixed_ip in port.get( 'fixed_ips', [])] subnet = port_subnets[0] if subnet and self.nsx_v.is_subnet_in_use(context, subnet): error = _('Cannot delete router interface while loadbalancers are ' 'provisioned on attached subnet') raise nsxv_exc.NsxPluginException(err_msg=error) info = super(nsx_v.NsxVPluginV2, self.plugin).remove_router_interface( context, router_id, interface_info) router_db = self.plugin._get_router(context, router_id) subnet = self.plugin.get_subnet(context, info['subnet_id']) network_id = subnet['network_id'] with locking.LockManager.get_lock( self._get_router_edge_id(context, router_id), external=True): if router_db.gw_port and router_db.enable_snat: # First update nat rules self.plugin._update_nat_rules(context, router_db) ports = self.plugin._get_router_interface_ports_by_network( context, router_id, network_id) self.plugin._update_subnets_and_dnat_firewall(context, router_db) # No subnet on the network connects to the edge vnic if not ports: edge_utils.delete_interface(self.nsx_v, context, router_id, network_id, dist=False) else: address_groups = self.plugin._get_address_groups( context, router_id, network_id) edge_utils.update_internal_interface(self.nsx_v, context, router_id, network_id, address_groups) return info
def configure_router_edge(self, rtr_id, context=None): # Connect router interface to inter-edge network port_data = { 'port': { 'network_id': self.internal_net, 'name': None, 'admin_state_up': True, 'device_id': rtr_id, 'device_owner': constants.DEVICE_OWNER_ROUTER_GW, 'fixed_ips': attr.ATTR_NOT_SPECIFIED, 'mac_address': attr.ATTR_NOT_SPECIFIED, 'port_security_enabled': False, 'tenant_id': None}} self.nsxv_plugin.create_port(self.context, port_data) address_groups = self._get_address_groups( self.context, self.internal_net, rtr_id, is_proxy=False) if context is None: context = self.context edge_utils.update_internal_interface( self.nsxv_plugin.nsx_v, context, rtr_id, self.internal_net, address_groups=address_groups) self._setup_metadata_lb(rtr_id, METADATA_IP_ADDR, METADATA_TCP_PORT, cfg.CONF.nsxv.nova_metadata_port, self.proxy_edge_ips, proxy_lb=False, context=context)
def remove_router_interface(self, context, router_id, interface_info): info = super(nsx_v.NsxVPluginV2, self.plugin).remove_router_interface( context, router_id, interface_info) router_db = self.plugin._get_router(context, router_id) subnet = self.plugin.get_subnet(context, info['subnet_id']) network_id = subnet['network_id'] if router_db.gw_port and router_db.enable_snat: # First update nat rules self.plugin._update_nat_rules(context, router_db) ports = self.plugin._get_router_interface_ports_by_network( context, router_id, network_id) self.plugin._update_subnets_and_dnat_firewall(context, router_db) # No subnet on the network connects to the edge vnic if not ports: edge_utils.delete_interface(self.nsx_v, context, router_id, network_id, dist=False) else: address_groups = self.plugin._get_address_groups( context, router_id, network_id) edge_utils.update_internal_interface(self.nsx_v, context, router_id, network_id, address_groups) return info
def _get_proxy_edge(self, rtr_ip): # If DB Entry already defined by another Neutron instance, retrieve # its IP address and exit try: nsxv_db.create_nsxv_internal_edge( self.context.session, rtr_ip, vcns_const.InternalEdgePurposes.INTER_EDGE_PURPOSE, None) except db_exc.DBDuplicateEntry: edge_ip = None ctr = 0 while edge_ip is None and ctr < EDGE_WAIT_INTERVAL: rtr_list = nsxv_db.get_nsxv_internal_edge( self.context.session, rtr_ip) if rtr_list: rtr_id = rtr_list[0]['router_id'] if rtr_id: edge_ip = self._get_edge_internal_ip(rtr_id) if edge_ip: return edge_ip self.context.session.expire_all() ctr += EDGE_CHECK_INTERVAL time.sleep(EDGE_CHECK_INTERVAL) error = _('Metadata proxy creation on other neutron instance ' 'timed out') raise nsxv_exc.NsxPluginException(err_msg=error) rtr_id = None try: router_data = { 'router': { 'name': 'metadata_proxy_router', 'admin_state_up': True, 'router_type': 'exclusive', 'tenant_id': None}} rtr = self.nsxv_plugin.create_router( self.context, router_data, allow_metadata=False) rtr_id = rtr['id'] binding = nsxv_db.get_nsxv_router_binding( self.context.session, rtr_id) self.nsxv_plugin.nsx_v.update_interface( rtr['id'], binding['edge_id'], vcns_const.EXTERNAL_VNIC_INDEX, cfg.CONF.nsxv.mgt_net_moid, address=rtr_ip, netmask=cfg.CONF.nsxv.mgt_net_proxy_netmask, secondary=[]) port_data = { 'port': { 'network_id': self.internal_net, 'name': None, 'admin_state_up': True, 'device_id': rtr_id, 'device_owner': constants.DEVICE_OWNER_ROUTER_INTF, 'fixed_ips': attr.ATTR_NOT_SPECIFIED, 'mac_address': attr.ATTR_NOT_SPECIFIED, 'port_security_enabled': False, 'tenant_id': None}} port = self.nsxv_plugin.create_port(self.context, port_data) address_groups = self._get_address_groups( self.context, self.internal_net, rtr_id, is_proxy=True) edge_ip = port['fixed_ips'][0]['ip_address'] edge_utils.update_internal_interface( self.nsxv_plugin.nsx_v, self.context, rtr_id, self.internal_net, address_groups) self._setup_metadata_lb(rtr_id, port['fixed_ips'][0]['ip_address'], cfg.CONF.nsxv.nova_metadata_port, cfg.CONF.nsxv.nova_metadata_port, cfg.CONF.nsxv.nova_metadata_ips, proxy_lb=True) firewall_rule = { 'action': 'allow', 'enabled': True, 'source_ip_address': [INTERNAL_SUBNET]} edge_utils.update_firewall( self.nsxv_plugin.nsx_v, self.context, rtr_id, {'firewall_rule_list': [firewall_rule]}, allow_external=False) if cfg.CONF.nsxv.mgt_net_default_gateway: self.nsxv_plugin._update_routes( self.context, rtr_id, cfg.CONF.nsxv.mgt_net_default_gateway) nsxv_db.update_nsxv_internal_edge( self.context.session, rtr_ip, rtr_id) return edge_ip except Exception as e: with excutils.save_and_reraise_exception(): nsxv_db.delete_nsxv_internal_edge( self.context.session, rtr_ip) if rtr_id: self.nsxv_plugin.delete_router(self.context, rtr_id) LOG.exception(_LE("Exception %s while creating internal edge " "for metadata service"), e)
def add_router_interface(self, context, router_id, interface_info): self.plugin._check_intf_number_of_router(context, router_id) edge_id = edge_utils.get_router_edge_id(context, router_id) router_db = self.plugin._get_router(context, router_id) if edge_id: is_migrated = False with locking.LockManager.get_lock( str(edge_id), lock_file_prefix=NSXV_ROUTER_RECONFIG, external=True): router_ids = self.edge_manager.get_routers_on_same_edge( context, router_id) info = super(nsx_v.NsxVPluginV2, self.plugin).add_router_interface( context, router_id, interface_info) subnet = self.plugin.get_subnet(context, info['subnet_id']) network_id = subnet['network_id'] # Collect all conflict networks whose cidr are overlapped # with networks attached to the router and conflct routers # which has same network with the router's. conflict_network_ids, conflict_router_ids, _ = ( self._get_conflict_network_and_router_ids_by_intf( context, router_id)) _, new_conflict_router_ids = ( self._get_available_and_conflicting_ids(context, router_id)) conflict_router_ids.extend(new_conflict_router_ids) conflict_router_ids = list(set(conflict_router_ids)) interface_ports = ( self.plugin._get_router_interface_ports_by_network( context, router_id, network_id)) # Consider whether another subnet of the same network # has been attached to the router. if len(interface_ports) > 1: is_conflict = self.edge_manager.is_router_conflict_on_edge( context, router_id, conflict_router_ids, conflict_network_ids, 0) else: is_conflict = self.edge_manager.is_router_conflict_on_edge( context, router_id, conflict_router_ids, conflict_network_ids, 1) if is_conflict: if len(interface_ports) > 1: self._remove_router_services_on_edge( context, router_id) else: self._remove_router_services_on_edge( context, router_id, network_id) self._unbind_router_on_edge(context, router_id) is_migrated = True else: address_groups = self.plugin._get_address_groups( context, router_id, network_id) edge_utils.update_internal_interface( self.nsx_v, context, router_id, network_id, address_groups, router_db.admin_state_up) if router_db.gw_port and router_db.enable_snat: self._update_nat_rules_on_routers( context, router_id, router_ids) self._update_subnets_and_dnat_firewall_on_routers( context, router_id, router_ids, allow_external=True) if is_migrated: self._bind_router_on_available_edge( context, router_id, router_db.admin_state_up) edge_id = edge_utils.get_router_edge_id(context, router_id) with locking.LockManager.get_lock( str(edge_id), lock_file_prefix=NSXV_ROUTER_RECONFIG, external=True): self._add_router_services_on_available_edge(context, router_id) else: info = super(nsx_v.NsxVPluginV2, self.plugin).add_router_interface( context, router_id, interface_info) # bind and configure routing servie on an availab edge self._bind_router_on_available_edge( context, router_id, router_db.admin_state_up) edge_id = edge_utils.get_router_edge_id(context, router_id) with locking.LockManager.get_lock( str(edge_id), lock_file_prefix=NSXV_ROUTER_RECONFIG, external=True): self._add_router_services_on_available_edge(context, router_id) return info
def _setup_new_proxy_edge(self, rtr_ext_ip): rtr_id = None try: router_data = { 'router': { 'name': 'metadata_proxy_router', 'admin_state_up': True, 'router_type': 'exclusive', 'tenant_id': None}} rtr = self.nsxv_plugin.create_router( self.context, router_data, allow_metadata=False) rtr_id = rtr['id'] edge_id = self._get_edge_id_by_rtr_id(rtr_id) self.nsxv_plugin.nsx_v.update_interface( rtr['id'], edge_id, vcns_const.EXTERNAL_VNIC_INDEX, cfg.CONF.nsxv.mgt_net_moid, address=rtr_ext_ip, netmask=cfg.CONF.nsxv.mgt_net_proxy_netmask, secondary=[]) port_data = { 'port': { 'network_id': self.internal_net, 'name': None, 'admin_state_up': True, 'device_id': rtr_id, 'device_owner': constants.DEVICE_OWNER_ROUTER_INTF, 'fixed_ips': attr.ATTR_NOT_SPECIFIED, 'mac_address': attr.ATTR_NOT_SPECIFIED, 'port_security_enabled': False, 'tenant_id': None}} port = self.nsxv_plugin.create_port(self.context, port_data) address_groups = self._get_address_groups( self.context, self.internal_net, rtr_id, is_proxy=True) edge_ip = port['fixed_ips'][0]['ip_address'] edge_utils.update_internal_interface( self.nsxv_plugin.nsx_v, self.context, rtr_id, self.internal_net, address_groups) self._setup_metadata_lb(rtr_id, port['fixed_ips'][0]['ip_address'], cfg.CONF.nsxv.nova_metadata_port, cfg.CONF.nsxv.nova_metadata_port, cfg.CONF.nsxv.nova_metadata_ips, proxy_lb=True) firewall_rules = [ DEFAULT_EDGE_FIREWALL_RULE, { 'action': 'allow', 'enabled': True, 'source_ip_address': [INTERNAL_SUBNET]}] edge_utils.update_firewall( self.nsxv_plugin.nsx_v, self.context, rtr_id, {'firewall_rule_list': firewall_rules}, allow_external=False) if cfg.CONF.nsxv.mgt_net_default_gateway: self.nsxv_plugin._update_routes( self.context, rtr_id, cfg.CONF.nsxv.mgt_net_default_gateway) nsxv_db.create_nsxv_internal_edge( self.context.session, rtr_ext_ip, vcns_const.InternalEdgePurposes.INTER_EDGE_PURPOSE, rtr_id) return edge_ip except Exception as e: LOG.exception(_LE("Exception %s while creating internal edge " "for metadata service"), e) ports = self.nsxv_plugin.get_ports( self.context, filters={'device_id': [rtr_id]}) for port in ports: self.nsxv_plugin.delete_port(self.context, port['id'], l3_port_check=True, nw_gw_port_check=True) nsxv_db.delete_nsxv_internal_edge( self.context.session, rtr_ext_ip) if rtr_id: self.nsxv_plugin.delete_router(self.context, rtr_id)