2015-04-16 14:54:59 +03:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2016-06-14 06:58:02 +00:00
|
|
|
import abc
|
2016-03-24 21:24:48 -07:00
|
|
|
from datetime import datetime
|
|
|
|
import itertools
|
2017-07-05 02:16:08 -07:00
|
|
|
|
|
|
|
from eventlet import greenthread
|
2020-06-15 12:19:43 +00:00
|
|
|
from neutron import manager
|
2017-07-05 02:16:08 -07:00
|
|
|
from neutron.services.segments import db as segments_db
|
2017-01-23 17:50:49 +08:00
|
|
|
from neutron_lib.api.definitions import l3
|
2020-03-25 15:30:12 +00:00
|
|
|
from neutron_lib.api.definitions import segment as segment_def
|
2016-05-09 18:25:59 -07:00
|
|
|
from neutron_lib import constants
|
2017-01-18 13:56:46 -08:00
|
|
|
from neutron_lib import context
|
2017-09-22 11:06:35 +02:00
|
|
|
from neutron_lib import exceptions as n_exc
|
2017-06-27 13:07:26 -06:00
|
|
|
from neutron_lib.plugins import constants as plugin_constants
|
2016-11-15 16:04:10 -08:00
|
|
|
from neutron_lib.plugins import directory
|
2016-10-20 07:45:20 -07:00
|
|
|
from neutron_lib.utils import helpers
|
2015-04-16 14:54:59 +03:00
|
|
|
from oslo_log import log
|
2017-07-05 02:16:08 -07:00
|
|
|
import six
|
2015-04-16 14:54:59 +03:00
|
|
|
|
2016-05-25 12:23:01 -07:00
|
|
|
from networking_ovn.common import acl as acl_utils
|
2016-05-25 09:20:07 -05:00
|
|
|
from networking_ovn.common import config
|
2016-07-13 17:32:29 -07:00
|
|
|
from networking_ovn.common import constants as const
|
2017-05-10 14:51:10 +01:00
|
|
|
from networking_ovn.common import ovn_client
|
2015-04-16 14:54:59 +03:00
|
|
|
from networking_ovn.common import utils
|
|
|
|
|
|
|
|
LOG = log.getLogger(__name__)
|
|
|
|
|
|
|
|
SYNC_MODE_OFF = 'off'
|
|
|
|
SYNC_MODE_LOG = 'log'
|
|
|
|
SYNC_MODE_REPAIR = 'repair'
|
|
|
|
|
|
|
|
|
2016-06-14 06:58:02 +00:00
|
|
|
@six.add_metaclass(abc.ABCMeta)
|
|
|
|
class OvnDbSynchronizer(object):
|
2015-04-16 14:54:59 +03:00
|
|
|
|
2016-06-14 06:58:02 +00:00
|
|
|
def __init__(self, core_plugin, ovn_api, ovn_driver):
|
2016-05-18 08:39:37 -05:00
|
|
|
self.ovn_driver = ovn_driver
|
2015-04-16 14:54:59 +03:00
|
|
|
self.ovn_api = ovn_api
|
2016-06-14 06:58:02 +00:00
|
|
|
self.core_plugin = core_plugin
|
2015-04-16 14:54:59 +03:00
|
|
|
|
2017-05-05 10:36:58 +08:00
|
|
|
def sync(self, delay_seconds=10):
|
2017-09-25 11:52:42 -05:00
|
|
|
self._gt = greenthread.spawn_after_local(delay_seconds, self.do_sync)
|
2015-04-16 14:54:59 +03:00
|
|
|
|
2016-06-14 06:58:02 +00:00
|
|
|
@abc.abstractmethod
|
2017-05-05 10:36:58 +08:00
|
|
|
def do_sync(self):
|
2016-06-14 06:58:02 +00:00
|
|
|
"""Method to sync the OVN DB."""
|
|
|
|
|
2017-09-25 11:52:42 -05:00
|
|
|
def stop(self):
|
|
|
|
try:
|
|
|
|
self._gt.kill()
|
|
|
|
except AttributeError:
|
|
|
|
# Haven't started syncing
|
|
|
|
pass
|
|
|
|
|
2016-06-14 06:58:02 +00:00
|
|
|
|
|
|
|
class OvnNbSynchronizer(OvnDbSynchronizer):
|
|
|
|
"""Synchronizer class for NB."""
|
|
|
|
|
2018-01-03 13:37:28 +08:00
|
|
|
def __init__(self, core_plugin, ovn_api, sb_ovn, mode, ovn_driver):
|
2016-06-14 06:58:02 +00:00
|
|
|
super(OvnNbSynchronizer, self).__init__(
|
|
|
|
core_plugin, ovn_api, ovn_driver)
|
|
|
|
self.mode = mode
|
2017-06-27 13:07:26 -06:00
|
|
|
self.l3_plugin = directory.get_plugin(plugin_constants.L3)
|
2018-01-03 13:37:28 +08:00
|
|
|
self._ovn_client = ovn_client.OVNClient(ovn_api, sb_ovn)
|
2020-03-25 15:30:12 +00:00
|
|
|
self.segments_plugin = directory.get_plugin('segments')
|
2020-06-15 12:19:43 +00:00
|
|
|
if not self.segments_plugin:
|
|
|
|
self.segments_plugin = (
|
|
|
|
manager.NeutronManager.load_class_for_provider(
|
|
|
|
'neutron.service_plugins', 'segments')())
|
2016-06-14 06:58:02 +00:00
|
|
|
|
2017-09-25 11:52:42 -05:00
|
|
|
def stop(self):
|
2018-01-03 13:37:28 +08:00
|
|
|
if utils.is_ovn_l3(self.l3_plugin):
|
|
|
|
self.l3_plugin._ovn.ovsdb_connection.stop()
|
|
|
|
self.l3_plugin._sb_ovn.ovsdb_connection.stop()
|
2017-09-25 11:52:42 -05:00
|
|
|
super(OvnNbSynchronizer, self).stop()
|
|
|
|
|
2017-05-05 10:36:58 +08:00
|
|
|
def do_sync(self):
|
2015-04-16 14:54:59 +03:00
|
|
|
if self.mode == SYNC_MODE_OFF:
|
|
|
|
LOG.debug("Neutron sync mode is off")
|
|
|
|
return
|
|
|
|
LOG.debug("Starting OVN-Northbound DB sync process")
|
|
|
|
|
|
|
|
ctx = context.get_admin_context()
|
2018-05-23 15:04:07 +02:00
|
|
|
|
2018-09-03 15:45:11 +00:00
|
|
|
self.sync_port_groups(ctx)
|
2016-08-02 19:16:11 +05:30
|
|
|
self.sync_networks_ports_and_dhcp_opts(ctx)
|
2017-11-21 13:04:25 +05:30
|
|
|
self.sync_port_dns_records(ctx)
|
2016-03-24 21:24:48 -07:00
|
|
|
self.sync_acls(ctx)
|
2016-04-18 09:42:44 -07:00
|
|
|
self.sync_routers_and_rports(ctx)
|
2016-02-06 23:22:40 +05:30
|
|
|
|
2016-05-18 08:39:37 -05:00
|
|
|
def _create_port_in_ovn(self, ctx, port):
|
2016-07-26 16:59:35 -05:00
|
|
|
# Remove any old ACLs for the port to avoid creating duplicate ACLs.
|
|
|
|
self.ovn_api.delete_acl(
|
|
|
|
utils.ovn_name(port['network_id']),
|
|
|
|
port['id']).execute(check_error=True)
|
|
|
|
|
|
|
|
# Create the port in OVN. This will include ACL and Address Set
|
|
|
|
# updates as needed.
|
2017-06-02 18:17:20 +01:00
|
|
|
self._ovn_client.create_port(port)
|
2016-05-25 09:20:07 -05:00
|
|
|
|
2016-03-24 21:24:48 -07:00
|
|
|
def remove_common_acls(self, neutron_acls, nb_acls):
|
|
|
|
"""Take out common acls of the two acl dictionaries.
|
|
|
|
|
|
|
|
@param neutron_acls: neutron dictionary of port vs acls
|
|
|
|
@type neutron_acls: {}
|
|
|
|
@param nb_acls: nb dictionary of port vs acls
|
|
|
|
@type nb_acls: {}
|
|
|
|
@return: Nothing, original dictionary modified
|
|
|
|
"""
|
|
|
|
for port in neutron_acls.keys():
|
|
|
|
for acl in list(neutron_acls[port]):
|
|
|
|
if port in nb_acls and acl in nb_acls[port]:
|
|
|
|
neutron_acls[port].remove(acl)
|
|
|
|
nb_acls[port].remove(acl)
|
|
|
|
|
|
|
|
def get_acls(self, context):
|
|
|
|
"""create the list of ACLS in OVN.
|
|
|
|
|
2017-01-18 13:56:46 -08:00
|
|
|
@param context: neutron_lib.context
|
|
|
|
@type context: object of type neutron_lib.context.Context
|
2016-03-24 21:24:48 -07:00
|
|
|
@var lswitch_names: List of lswitch names
|
|
|
|
@var acl_list: List of NB acls
|
|
|
|
@var acl_list_dict: Dictionary of acl-lists based on lport as key
|
|
|
|
@return: acl_list-dict
|
|
|
|
"""
|
|
|
|
lswitch_names = set([])
|
2016-08-10 16:05:33 -05:00
|
|
|
for network in self.core_plugin.get_networks(context):
|
|
|
|
lswitch_names.add(network['id'])
|
2019-05-06 17:43:50 +08:00
|
|
|
acl_dict, ignore1, ignore2 = (
|
|
|
|
self.ovn_api.get_acls_for_lswitches(lswitch_names))
|
2017-01-22 12:29:24 +08:00
|
|
|
acl_list = list(itertools.chain(*acl_dict.values()))
|
2016-03-24 21:24:48 -07:00
|
|
|
acl_list_dict = {}
|
|
|
|
for acl in acl_list:
|
2018-07-31 15:02:41 +01:00
|
|
|
acl = acl_utils.filter_acl_dict(
|
|
|
|
acl, extra_fields=['lport', 'lswitch'])
|
2016-03-24 21:24:48 -07:00
|
|
|
key = acl['lport']
|
|
|
|
if key in acl_list_dict:
|
|
|
|
acl_list_dict[key].append(acl)
|
|
|
|
else:
|
|
|
|
acl_list_dict[key] = list([acl])
|
|
|
|
return acl_list_dict
|
|
|
|
|
2018-09-03 15:45:11 +00:00
|
|
|
def sync_port_groups(self, ctx):
|
|
|
|
"""Sync Port Groups between neutron and NB.
|
|
|
|
|
|
|
|
@param ctx: neutron_lib.context
|
|
|
|
@type ctx: object of type neutron_lib.context.Context
|
|
|
|
"""
|
|
|
|
|
|
|
|
neutron_sgs = {}
|
|
|
|
neutron_pgs = set()
|
|
|
|
with ctx.session.begin(subtransactions=True):
|
|
|
|
for sg in self.core_plugin.get_security_groups(ctx):
|
|
|
|
pg_name = utils.ovn_port_group_name(sg['id'])
|
|
|
|
neutron_pgs.add(pg_name)
|
|
|
|
neutron_sgs[pg_name] = sg['id']
|
|
|
|
neutron_pgs.add(const.OVN_DROP_PORT_GROUP_NAME)
|
|
|
|
|
|
|
|
ovn_pgs = set()
|
|
|
|
port_groups = self.ovn_api.db_list_rows('Port_Group').execute() or []
|
|
|
|
for pg in port_groups:
|
|
|
|
ovn_pgs.add(pg.name)
|
|
|
|
|
|
|
|
add_pgs = neutron_pgs.difference(ovn_pgs)
|
|
|
|
remove_pgs = ovn_pgs.difference(neutron_pgs)
|
|
|
|
|
|
|
|
LOG.debug('Port Groups added %d, removed %d',
|
|
|
|
len(add_pgs), len(remove_pgs))
|
|
|
|
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
LOG.debug('Port-Group-SYNC: transaction started @ %s',
|
|
|
|
str(datetime.now()))
|
|
|
|
if add_pgs:
|
|
|
|
db_ports = self.core_plugin.get_ports(ctx)
|
|
|
|
ovn_ports = set(p.name for p in
|
|
|
|
self.ovn_api.lsp_list().execute())
|
|
|
|
with self.ovn_api.transaction(check_error=True) as txn:
|
|
|
|
pg = const.OVN_DROP_PORT_GROUP_NAME
|
|
|
|
# Process default drop port group first
|
|
|
|
if pg in add_pgs:
|
|
|
|
txn.add(self.ovn_api.pg_add(name=pg, acls=[]))
|
|
|
|
add_pgs.remove(pg)
|
|
|
|
# Add ports to the drop port group. Only add those that
|
|
|
|
# already exists in OVN. The rest will be added during the
|
|
|
|
# ports sync operation later.
|
|
|
|
for n_port in db_ports:
|
|
|
|
if ((n_port['security_groups'] or
|
|
|
|
n_port['port_security_enabled']) and
|
|
|
|
n_port['id'] in ovn_ports):
|
|
|
|
txn.add(self.ovn_api.pg_add_ports(
|
|
|
|
pg, n_port['id']))
|
|
|
|
|
|
|
|
for pg in add_pgs:
|
|
|
|
# If it's a security group PG, add the ext id
|
|
|
|
ext_ids = {const.OVN_SG_EXT_ID_KEY: neutron_sgs[pg]}
|
|
|
|
txn.add(self.ovn_api.pg_add(name=pg, acls=[],
|
|
|
|
external_ids=ext_ids))
|
|
|
|
# Add the ports belonging to the SG to this port group
|
|
|
|
for n_port in db_ports:
|
|
|
|
if (neutron_sgs[pg] in n_port['security_groups'] and
|
|
|
|
n_port['id'] in ovn_ports):
|
|
|
|
txn.add(self.ovn_api.pg_add_ports(
|
|
|
|
pg, n_port['id']))
|
|
|
|
for pg in remove_pgs:
|
|
|
|
txn.add(self.ovn_api.pg_del(pg))
|
|
|
|
LOG.debug('Port-Group-SYNC: transaction finished @ %s',
|
|
|
|
str(datetime.now()))
|
|
|
|
|
|
|
|
def _get_acls_from_port_groups(self):
|
|
|
|
ovn_acls = []
|
2021-11-17 17:29:13 +00:00
|
|
|
acl_columns = (set(self.ovn_api._tables['ACL'].columns.keys()) &
|
|
|
|
set(const.ACL_EXPECTED_COLUMNS_NBDB))
|
|
|
|
acl_columns.discard('external_ids')
|
|
|
|
for pg in self.ovn_api.db_list_rows('Port_Group').execute():
|
2018-09-03 15:45:11 +00:00
|
|
|
acls = getattr(pg, 'acls', [])
|
|
|
|
for acl in acls:
|
2021-11-17 17:29:13 +00:00
|
|
|
acl_string = {k: getattr(acl, k) for k in acl_columns}
|
2018-09-03 15:45:11 +00:00
|
|
|
acl_string['port_group'] = pg.name
|
|
|
|
ovn_acls.append(acl_string)
|
|
|
|
return ovn_acls
|
|
|
|
|
2020-06-19 17:46:34 -04:00
|
|
|
def sync_acls(self, ctx):
|
|
|
|
"""Sync ACLs between neutron and NB.
|
|
|
|
|
|
|
|
@param ctx: neutron_lib.context
|
|
|
|
@type ctx: object of type neutron_lib.context.Context
|
|
|
|
@return: Nothing
|
|
|
|
"""
|
|
|
|
LOG.debug('ACL-SYNC: started @ %s', str(datetime.now()))
|
|
|
|
|
2018-09-03 15:45:11 +00:00
|
|
|
neutron_acls = []
|
|
|
|
for sgr in self.core_plugin.get_security_group_rules(ctx):
|
|
|
|
pg_name = utils.ovn_port_group_name(sgr['security_group_id'])
|
|
|
|
neutron_acls.append(acl_utils._add_sg_rule_acl_for_port_group(
|
2020-06-19 17:46:34 -04:00
|
|
|
pg_name, sgr))
|
2018-09-03 15:45:11 +00:00
|
|
|
neutron_acls += acl_utils.add_acls_for_drop_port_group(
|
|
|
|
const.OVN_DROP_PORT_GROUP_NAME)
|
|
|
|
|
|
|
|
ovn_acls = self._get_acls_from_port_groups()
|
|
|
|
|
|
|
|
# We need to remove also all the ACLs applied to Logical Switches
|
|
|
|
def get_num_acls(ovn_acls):
|
|
|
|
return len([item for sublist in ovn_acls for item in sublist[1]])
|
|
|
|
|
|
|
|
ovn_acls_from_ls = [(row.name, row.acls) for row in (
|
|
|
|
self.ovn_api._tables['Logical_Switch'].rows.values())]
|
|
|
|
num_acls_to_remove_from_ls = get_num_acls(ovn_acls_from_ls)
|
|
|
|
|
|
|
|
# Remove the common ones
|
|
|
|
for na in list(neutron_acls):
|
|
|
|
for ovn_a in ovn_acls:
|
|
|
|
if all(item in na.items() for item in ovn_a.items()):
|
|
|
|
neutron_acls.remove(na)
|
|
|
|
ovn_acls.remove(ovn_a)
|
|
|
|
break
|
|
|
|
|
|
|
|
num_acls_to_add = len(neutron_acls)
|
|
|
|
num_acls_to_remove = len(ovn_acls) + num_acls_to_remove_from_ls
|
|
|
|
if 0 != num_acls_to_add or 0 != num_acls_to_remove:
|
|
|
|
LOG.warning('ACLs-to-be-added %(add)d '
|
|
|
|
'ACLs-to-be-removed %(remove)d',
|
|
|
|
{'add': num_acls_to_add,
|
|
|
|
'remove': num_acls_to_remove})
|
|
|
|
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
with self.ovn_api.transaction(check_error=True) as txn:
|
|
|
|
for acla in neutron_acls:
|
|
|
|
LOG.warning('ACL found in Neutron but not in '
|
|
|
|
'OVN DB for port group %s', acla['port_group'])
|
2022-03-14 15:16:10 +01:00
|
|
|
txn.add(self.ovn_api.pg_acl_add(may_exist=True, **acla))
|
2018-09-03 15:45:11 +00:00
|
|
|
|
|
|
|
with self.ovn_api.transaction(check_error=True) as txn:
|
|
|
|
for aclr in ovn_acls:
|
|
|
|
LOG.warning('ACLs found in OVN DB but not in '
|
|
|
|
'Neutron for port group %s',
|
|
|
|
aclr['port_group'])
|
|
|
|
txn.add(self.ovn_api.pg_acl_del(aclr['port_group'],
|
|
|
|
aclr['direction'],
|
|
|
|
aclr['priority'],
|
|
|
|
aclr['match']))
|
|
|
|
for aclr in ovn_acls_from_ls:
|
|
|
|
# Remove all the ACLs from any Logical Switch if they have
|
|
|
|
# any. Elements are (lswitch_name, list_of_acls).
|
|
|
|
if len(aclr[1]) > 0:
|
|
|
|
LOG.warning('Removing ACLs from OVN from Logical '
|
|
|
|
'Switch %s', aclr[0])
|
|
|
|
txn.add(self.ovn_api.acl_del(aclr[0]))
|
|
|
|
|
2019-03-13 16:06:26 -04:00
|
|
|
LOG.debug('ACL-SYNC: finished @ %s', str(datetime.now()))
|
2016-03-24 21:24:48 -07:00
|
|
|
|
2017-12-08 16:12:33 +00:00
|
|
|
def _calculate_fips_differences(self, ovn_fips, db_fips):
|
|
|
|
to_add = []
|
|
|
|
to_remove = []
|
|
|
|
for db_fip in db_fips:
|
|
|
|
for ovn_fip in ovn_fips:
|
|
|
|
if (ovn_fip['logical_ip'] == db_fip['fixed_ip_address'] and
|
|
|
|
ovn_fip['external_ip'] == db_fip['floating_ip_address']):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
to_add.append(db_fip)
|
|
|
|
|
|
|
|
for ovn_fip in ovn_fips:
|
|
|
|
for db_fip in db_fips:
|
|
|
|
if (ovn_fip['logical_ip'] == db_fip['fixed_ip_address'] and
|
|
|
|
ovn_fip['external_ip'] == db_fip['floating_ip_address']):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
to_remove.append(ovn_fip)
|
|
|
|
|
|
|
|
return to_add, to_remove
|
|
|
|
|
2016-04-18 09:42:44 -07:00
|
|
|
def sync_routers_and_rports(self, ctx):
|
|
|
|
"""Sync Routers between neutron and NB.
|
|
|
|
|
2017-01-18 13:56:46 -08:00
|
|
|
@param ctx: neutron_lib.context
|
|
|
|
@type ctx: object of type neutron_lib.context.Context
|
2016-04-18 09:42:44 -07:00
|
|
|
@var db_routers: List of Routers from neutron DB
|
|
|
|
@var db_router_ports: List of Router ports from neutron DB
|
|
|
|
@var lrouters: NB dictionary of logical routers and
|
|
|
|
the corresponding logical router ports.
|
|
|
|
vs list-of-acls
|
|
|
|
@var del_lrouters_list: List of Routers that need to be
|
|
|
|
deleted from NB
|
|
|
|
@var del_lrouter_ports_list: List of Router ports that need to be
|
|
|
|
deleted from NB
|
|
|
|
@return: Nothing
|
|
|
|
"""
|
2018-01-03 13:37:28 +08:00
|
|
|
if not utils.is_ovn_l3(self.l3_plugin):
|
2016-05-25 09:20:07 -05:00
|
|
|
LOG.debug("OVN L3 mode is disabled, skipping "
|
|
|
|
"sync routers and router ports")
|
|
|
|
return
|
|
|
|
|
2019-03-13 16:06:26 -04:00
|
|
|
LOG.debug('OVN-NB Sync Routers and Router ports started @ %s',
|
2017-01-23 17:50:49 +08:00
|
|
|
str(datetime.now()))
|
|
|
|
|
2016-04-18 09:42:44 -07:00
|
|
|
db_routers = {}
|
2017-01-23 17:50:49 +08:00
|
|
|
db_extends = {}
|
2016-04-18 09:42:44 -07:00
|
|
|
db_router_ports = {}
|
2016-05-25 09:20:07 -05:00
|
|
|
for router in self.l3_plugin.get_routers(ctx):
|
2016-04-18 09:42:44 -07:00
|
|
|
db_routers[router['id']] = router
|
2017-01-23 17:50:49 +08:00
|
|
|
db_extends[router['id']] = {}
|
|
|
|
db_extends[router['id']]['routes'] = []
|
|
|
|
db_extends[router['id']]['snats'] = []
|
|
|
|
db_extends[router['id']]['fips'] = []
|
|
|
|
if not router.get(l3.EXTERNAL_GW_INFO):
|
|
|
|
continue
|
2018-12-17 12:10:58 +01:00
|
|
|
gateways = self._ovn_client._get_gw_info(ctx, router)
|
|
|
|
for gw_info in gateways:
|
|
|
|
prefix = (constants.IPv4_ANY if
|
|
|
|
gw_info.ip_version == constants.IP_VERSION_4 else
|
|
|
|
constants.IPv6_ANY)
|
|
|
|
if gw_info.gateway_ip:
|
|
|
|
db_extends[router['id']]['routes'].append(
|
|
|
|
{'destination': prefix,
|
|
|
|
'nexthop': gw_info.gateway_ip})
|
|
|
|
if gw_info.ip_version == constants.IP_VERSION_6:
|
|
|
|
continue
|
|
|
|
if gw_info.router_ip and utils.is_snat_enabled(router):
|
|
|
|
networks = (
|
|
|
|
self._ovn_client._get_v4_network_of_all_router_ports(
|
|
|
|
ctx, router['id']))
|
|
|
|
for network in networks:
|
|
|
|
db_extends[router['id']]['snats'].append({
|
|
|
|
'logical_ip': network,
|
|
|
|
'external_ip': gw_info.router_ip,
|
|
|
|
'type': 'snat'})
|
2017-01-23 17:50:49 +08:00
|
|
|
|
|
|
|
fips = self.l3_plugin.get_floatingips(
|
2017-02-20 14:30:45 +08:00
|
|
|
ctx, {'router_id': list(db_routers.keys())})
|
2017-01-23 17:50:49 +08:00
|
|
|
for fip in fips:
|
2017-12-08 16:12:33 +00:00
|
|
|
db_extends[fip['router_id']]['fips'].append(fip)
|
2017-01-23 17:50:49 +08:00
|
|
|
interfaces = self.l3_plugin._get_sync_interfaces(
|
2017-06-21 05:27:13 +05:30
|
|
|
ctx, list(db_routers.keys()),
|
|
|
|
[constants.DEVICE_OWNER_ROUTER_INTF,
|
|
|
|
constants.DEVICE_OWNER_ROUTER_GW,
|
|
|
|
constants.DEVICE_OWNER_DVR_INTERFACE,
|
2017-10-05 20:51:08 +05:30
|
|
|
constants.DEVICE_OWNER_ROUTER_HA_INTF,
|
|
|
|
constants.DEVICE_OWNER_HA_REPLICATED_INT])
|
2016-04-18 09:42:44 -07:00
|
|
|
for interface in interfaces:
|
|
|
|
db_router_ports[interface['id']] = interface
|
2017-12-19 23:36:54 +05:30
|
|
|
|
2016-04-18 09:42:44 -07:00
|
|
|
lrouters = self.ovn_api.get_all_logical_routers_with_rports()
|
2017-01-23 17:50:49 +08:00
|
|
|
|
2016-04-18 09:42:44 -07:00
|
|
|
del_lrouters_list = []
|
|
|
|
del_lrouter_ports_list = []
|
2016-05-28 21:46:26 -04:00
|
|
|
update_sroutes_list = []
|
2016-08-22 17:20:40 +08:00
|
|
|
update_lrport_list = []
|
2017-01-23 17:50:49 +08:00
|
|
|
update_snats_list = []
|
|
|
|
update_fips_list = []
|
2016-04-18 09:42:44 -07:00
|
|
|
for lrouter in lrouters:
|
|
|
|
if lrouter['name'] in db_routers:
|
2016-08-22 17:20:40 +08:00
|
|
|
for lrport, lrport_nets in lrouter['ports'].items():
|
2016-04-18 09:42:44 -07:00
|
|
|
if lrport in db_router_ports:
|
2017-12-19 23:36:54 +05:30
|
|
|
# We dont have to check for the networks and
|
|
|
|
# ipv6_ra_configs values. Lets add it to the
|
|
|
|
# update_lrport_list. If they are in sync, then
|
|
|
|
# update_router_port will be a no-op.
|
2018-01-18 15:23:37 +00:00
|
|
|
update_lrport_list.append(db_router_ports[lrport])
|
2016-04-18 09:42:44 -07:00
|
|
|
del db_router_ports[lrport]
|
|
|
|
else:
|
|
|
|
del_lrouter_ports_list.append(
|
|
|
|
{'port': lrport, 'lrouter': lrouter['name']})
|
2016-05-28 21:46:26 -04:00
|
|
|
if 'routes' in db_routers[lrouter['name']]:
|
|
|
|
db_routes = db_routers[lrouter['name']]['routes']
|
|
|
|
else:
|
|
|
|
db_routes = []
|
2017-01-23 17:50:49 +08:00
|
|
|
if 'routes' in db_extends[lrouter['name']]:
|
|
|
|
db_routes.extend(db_extends[lrouter['name']]['routes'])
|
|
|
|
|
2016-05-28 21:46:26 -04:00
|
|
|
ovn_routes = lrouter['static_routes']
|
2016-10-20 07:45:20 -07:00
|
|
|
add_routes, del_routes = helpers.diff_list_of_dict(
|
2016-05-28 21:46:26 -04:00
|
|
|
ovn_routes, db_routes)
|
|
|
|
update_sroutes_list.append({'id': lrouter['name'],
|
|
|
|
'add': add_routes,
|
|
|
|
'del': del_routes})
|
2017-01-23 17:50:49 +08:00
|
|
|
ovn_fips = lrouter['dnat_and_snats']
|
|
|
|
db_fips = db_extends[lrouter['name']]['fips']
|
2017-12-08 16:12:33 +00:00
|
|
|
add_fips, del_fips = self._calculate_fips_differences(
|
2017-01-23 17:50:49 +08:00
|
|
|
ovn_fips, db_fips)
|
|
|
|
update_fips_list.append({'id': lrouter['name'],
|
|
|
|
'add': add_fips,
|
|
|
|
'del': del_fips})
|
|
|
|
ovn_nats = lrouter['snats']
|
|
|
|
db_snats = db_extends[lrouter['name']]['snats']
|
|
|
|
add_snats, del_snats = helpers.diff_list_of_dict(
|
|
|
|
ovn_nats, db_snats)
|
|
|
|
update_snats_list.append({'id': lrouter['name'],
|
|
|
|
'add': add_snats,
|
|
|
|
'del': del_snats})
|
2016-04-18 09:42:44 -07:00
|
|
|
else:
|
|
|
|
del_lrouters_list.append(lrouter)
|
|
|
|
|
2020-06-26 16:42:35 +01:00
|
|
|
lrouters_names = {lr['name'] for lr in lrouters}
|
2016-04-18 09:42:44 -07:00
|
|
|
for r_id, router in db_routers.items():
|
2020-06-26 16:42:35 +01:00
|
|
|
if r_id in lrouters_names:
|
|
|
|
continue
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Router found in Neutron but not in "
|
|
|
|
"OVN DB, router id=%s", router['id'])
|
2016-04-18 09:42:44 -07:00
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
try:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Creating the router %s in OVN NB DB",
|
2016-04-18 09:42:44 -07:00
|
|
|
router['id'])
|
2017-10-11 15:36:06 +01:00
|
|
|
self._ovn_client.create_router(
|
|
|
|
router, add_external_gateway=False)
|
2016-05-28 21:46:26 -04:00
|
|
|
if 'routes' in router:
|
|
|
|
update_sroutes_list.append(
|
|
|
|
{'id': router['id'], 'add': router['routes'],
|
|
|
|
'del': []})
|
2017-01-23 17:50:49 +08:00
|
|
|
if 'routes' in db_extends[router['id']]:
|
|
|
|
update_sroutes_list.append(
|
|
|
|
{'id': router['id'],
|
|
|
|
'add': db_extends[router['id']]['routes'],
|
|
|
|
'del': []})
|
|
|
|
if 'snats' in db_extends[router['id']]:
|
|
|
|
update_snats_list.append(
|
|
|
|
{'id': router['id'],
|
|
|
|
'add': db_extends[router['id']]['snats'],
|
|
|
|
'del': []})
|
|
|
|
if 'fips' in db_extends[router['id']]:
|
|
|
|
update_fips_list.append(
|
|
|
|
{'id': router['id'],
|
|
|
|
'add': db_extends[router['id']]['fips'],
|
|
|
|
'del': []})
|
2016-04-18 09:42:44 -07:00
|
|
|
except RuntimeError:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Create router in OVN NB failed for router %s",
|
|
|
|
router['id'])
|
2016-04-18 09:42:44 -07:00
|
|
|
|
|
|
|
for rp_id, rrport in db_router_ports.items():
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Router Port found in Neutron but not in OVN "
|
|
|
|
"DB, router port_id=%s", rrport['id'])
|
2016-04-18 09:42:44 -07:00
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
try:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Creating the router port %s in OVN NB DB",
|
|
|
|
rrport['id'])
|
2020-06-26 16:42:35 +01:00
|
|
|
router = db_routers[rrport['device_id']]
|
2018-03-16 16:01:01 +00:00
|
|
|
self._ovn_client._create_lrouter_port(
|
2020-06-26 16:42:35 +01:00
|
|
|
router, rrport)
|
2016-04-18 09:42:44 -07:00
|
|
|
except RuntimeError:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Create router port in OVN "
|
|
|
|
"NB failed for router port %s", rrport['id'])
|
2016-04-18 09:42:44 -07:00
|
|
|
|
2018-01-18 15:23:37 +00:00
|
|
|
for rport in update_lrport_list:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Router Port port_id=%s needs to be updated "
|
|
|
|
"for networks changed",
|
2016-08-22 17:20:40 +08:00
|
|
|
rport['id'])
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
try:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning(
|
|
|
|
"Updating networks on router port %s in OVN NB DB",
|
|
|
|
rport['id'])
|
2018-01-18 15:23:37 +00:00
|
|
|
self._ovn_client.update_router_port(rport)
|
2016-08-22 17:20:40 +08:00
|
|
|
except RuntimeError:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Update router port networks in OVN "
|
|
|
|
"NB failed for router port %s", rport['id'])
|
2016-08-22 17:20:40 +08:00
|
|
|
|
2016-04-18 09:42:44 -07:00
|
|
|
with self.ovn_api.transaction(check_error=True) as txn:
|
|
|
|
for lrouter in del_lrouters_list:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Router found in OVN but not in "
|
|
|
|
"Neutron, router id=%s", lrouter['name'])
|
2016-04-18 09:42:44 -07:00
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Deleting the router %s from OVN NB DB",
|
2016-04-18 09:42:44 -07:00
|
|
|
lrouter['name'])
|
|
|
|
txn.add(self.ovn_api.delete_lrouter(
|
|
|
|
utils.ovn_name(lrouter['name'])))
|
|
|
|
|
|
|
|
for lrport_info in del_lrouter_ports_list:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Router Port found in OVN but not in "
|
|
|
|
"Neutron, port_id=%s", lrport_info['port'])
|
2016-04-18 09:42:44 -07:00
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Deleting the port %s from OVN NB DB",
|
2016-04-18 09:42:44 -07:00
|
|
|
lrport_info['port'])
|
|
|
|
txn.add(self.ovn_api.delete_lrouter_port(
|
|
|
|
utils.ovn_lrouter_port_name(lrport_info['port']),
|
|
|
|
utils.ovn_name(lrport_info['lrouter']),
|
|
|
|
if_exists=False))
|
2016-05-28 21:46:26 -04:00
|
|
|
for sroute in update_sroutes_list:
|
|
|
|
if sroute['add']:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Router %(id)s static routes %(route)s "
|
|
|
|
"found in Neutron but not in OVN",
|
2016-07-29 14:34:25 -05:00
|
|
|
{'id': sroute['id'], 'route': sroute['add']})
|
2016-05-28 21:46:26 -04:00
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Add static routes %s to OVN NB DB",
|
2016-05-28 21:46:26 -04:00
|
|
|
sroute['add'])
|
|
|
|
for route in sroute['add']:
|
|
|
|
txn.add(self.ovn_api.add_static_route(
|
|
|
|
utils.ovn_name(sroute['id']),
|
|
|
|
ip_prefix=route['destination'],
|
|
|
|
nexthop=route['nexthop']))
|
|
|
|
if sroute['del']:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Router %(id)s static routes %(route)s "
|
|
|
|
"found in OVN but not in Neutron",
|
2016-07-29 14:34:25 -05:00
|
|
|
{'id': sroute['id'], 'route': sroute['del']})
|
2016-05-28 21:46:26 -04:00
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Delete static routes %s from OVN NB DB",
|
|
|
|
sroute['del'])
|
2016-05-28 21:46:26 -04:00
|
|
|
for route in sroute['del']:
|
|
|
|
txn.add(self.ovn_api.delete_static_route(
|
|
|
|
utils.ovn_name(sroute['id']),
|
|
|
|
ip_prefix=route['destination'],
|
|
|
|
nexthop=route['nexthop']))
|
2017-01-23 17:50:49 +08:00
|
|
|
for fip in update_fips_list:
|
|
|
|
if fip['del']:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Router %(id)s floating ips %(fip)s "
|
|
|
|
"found in OVN but not in Neutron",
|
2017-01-23 17:50:49 +08:00
|
|
|
{'id': fip['id'], 'fip': fip['del']})
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning(
|
|
|
|
"Delete floating ips %s from OVN NB DB",
|
2017-01-23 17:50:49 +08:00
|
|
|
fip['del'])
|
|
|
|
for nat in fip['del']:
|
2017-12-08 16:12:33 +00:00
|
|
|
self._ovn_client._delete_floatingip(
|
2017-12-14 13:20:06 +00:00
|
|
|
nat, utils.ovn_name(fip['id']), txn=txn)
|
2017-01-23 17:50:49 +08:00
|
|
|
if fip['add']:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Router %(id)s floating ips %(fip)s "
|
|
|
|
"found in Neutron but not in OVN",
|
2017-01-23 17:50:49 +08:00
|
|
|
{'id': fip['id'], 'fip': fip['add']})
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Add floating ips %s to OVN NB DB",
|
2017-01-23 17:50:49 +08:00
|
|
|
fip['add'])
|
|
|
|
for nat in fip['add']:
|
2017-12-14 13:20:06 +00:00
|
|
|
self._ovn_client._create_or_update_floatingip(
|
|
|
|
nat, txn=txn)
|
2017-01-23 17:50:49 +08:00
|
|
|
for snat in update_snats_list:
|
|
|
|
if snat['del']:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Router %(id)s snat %(snat)s "
|
|
|
|
"found in OVN but not in Neutron",
|
2017-01-23 17:50:49 +08:00
|
|
|
{'id': snat['id'], 'snat': snat['del']})
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Delete snats %s from OVN NB DB",
|
2017-01-23 17:50:49 +08:00
|
|
|
snat['del'])
|
|
|
|
for nat in snat['del']:
|
|
|
|
txn.add(self.ovn_api.delete_nat_rule_in_lrouter(
|
|
|
|
utils.ovn_name(snat['id']),
|
|
|
|
logical_ip=nat['logical_ip'],
|
|
|
|
external_ip=nat['external_ip'],
|
|
|
|
type='snat'))
|
|
|
|
if snat['add']:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Router %(id)s snat %(snat)s "
|
|
|
|
"found in Neutron but not in OVN",
|
2017-01-23 17:50:49 +08:00
|
|
|
{'id': snat['id'], 'snat': snat['add']})
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Add snats %s to OVN NB DB",
|
2017-01-23 17:50:49 +08:00
|
|
|
snat['add'])
|
|
|
|
for nat in snat['add']:
|
|
|
|
txn.add(self.ovn_api.add_nat_rule_in_lrouter(
|
|
|
|
utils.ovn_name(snat['id']),
|
|
|
|
logical_ip=nat['logical_ip'],
|
|
|
|
external_ip=nat['external_ip'],
|
|
|
|
type='snat'))
|
2019-03-13 16:06:26 -04:00
|
|
|
LOG.debug('OVN-NB Sync routers and router ports finished %s',
|
2017-01-23 17:50:49 +08:00
|
|
|
str(datetime.now()))
|
2016-04-18 09:42:44 -07:00
|
|
|
|
2016-08-02 19:16:11 +05:30
|
|
|
def _sync_subnet_dhcp_options(self, ctx, db_networks,
|
|
|
|
ovn_subnet_dhcp_options):
|
|
|
|
LOG.debug('OVN-NB Sync DHCP options for Neutron subnets started')
|
|
|
|
|
|
|
|
db_subnets = {}
|
2016-06-08 14:43:45 +05:30
|
|
|
filters = {'enable_dhcp': [1]}
|
|
|
|
for subnet in self.core_plugin.get_subnets(ctx, filters=filters):
|
2019-08-08 16:53:22 -04:00
|
|
|
if (subnet['ip_version'] == constants.IP_VERSION_6 and
|
|
|
|
subnet.get('ipv6_address_mode') == constants.IPV6_SLAAC):
|
2016-06-08 14:43:45 +05:30
|
|
|
continue
|
|
|
|
db_subnets[subnet['id']] = subnet
|
2016-08-02 19:16:11 +05:30
|
|
|
|
|
|
|
del_subnet_dhcp_opts_list = []
|
|
|
|
for subnet_id, ovn_dhcp_opts in ovn_subnet_dhcp_options.items():
|
|
|
|
if subnet_id in db_subnets:
|
|
|
|
network = db_networks[utils.ovn_name(
|
|
|
|
db_subnets[subnet_id]['network_id'])]
|
2017-04-11 19:42:20 +08:00
|
|
|
if constants.IP_VERSION_6 == db_subnets[subnet_id][
|
|
|
|
'ip_version']:
|
|
|
|
server_mac = ovn_dhcp_opts['options'].get('server_id')
|
|
|
|
else:
|
|
|
|
server_mac = ovn_dhcp_opts['options'].get('server_mac')
|
2017-06-02 18:17:20 +01:00
|
|
|
dhcp_options = self._ovn_client._get_ovn_dhcp_options(
|
2017-11-21 15:01:49 +08:00
|
|
|
db_subnets[subnet_id], network, server_mac=server_mac)
|
2016-08-02 19:16:11 +05:30
|
|
|
# Verify that the cidr and options are also in sync.
|
|
|
|
if dhcp_options['cidr'] == ovn_dhcp_opts['cidr'] and (
|
|
|
|
dhcp_options['options'] == ovn_dhcp_opts['options']):
|
|
|
|
del db_subnets[subnet_id]
|
|
|
|
else:
|
|
|
|
db_subnets[subnet_id]['ovn_dhcp_options'] = dhcp_options
|
|
|
|
else:
|
|
|
|
del_subnet_dhcp_opts_list.append(ovn_dhcp_opts)
|
|
|
|
|
|
|
|
for subnet_id, subnet in db_subnets.items():
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning('DHCP options for subnet %s is present in '
|
|
|
|
'Neutron but out of sync for OVN', subnet_id)
|
2016-08-02 19:16:11 +05:30
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
try:
|
|
|
|
LOG.debug('Adding/Updating DHCP options for subnet %s in '
|
|
|
|
' OVN NB DB', subnet_id)
|
|
|
|
network = db_networks[utils.ovn_name(subnet['network_id'])]
|
2017-06-02 18:17:20 +01:00
|
|
|
# _ovn_client._add_subnet_dhcp_options doesn't create
|
2016-08-02 19:16:11 +05:30
|
|
|
# a new row in DHCP_Options if the row already exists.
|
|
|
|
# See commands.AddDHCPOptionsCommand.
|
2017-06-02 18:17:20 +01:00
|
|
|
self._ovn_client._add_subnet_dhcp_options(
|
2017-11-21 15:01:49 +08:00
|
|
|
subnet, network, subnet.get('ovn_dhcp_options'))
|
2016-08-02 19:16:11 +05:30
|
|
|
except RuntimeError:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning('Adding/Updating DHCP options for subnet '
|
|
|
|
'%s failed in OVN NB DB', subnet_id)
|
2016-08-02 19:16:11 +05:30
|
|
|
|
|
|
|
txn_commands = []
|
|
|
|
for dhcp_opt in del_subnet_dhcp_opts_list:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning('Out of sync subnet DHCP options for subnet %s '
|
|
|
|
'found in OVN NB DB which needs to be deleted',
|
2016-08-02 19:16:11 +05:30
|
|
|
dhcp_opt['external_ids']['subnet_id'])
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
LOG.debug('Deleting subnet DHCP options for subnet %s ',
|
|
|
|
dhcp_opt['external_ids']['subnet_id'])
|
|
|
|
txn_commands.append(self.ovn_api.delete_dhcp_options(
|
|
|
|
dhcp_opt['uuid']))
|
|
|
|
|
|
|
|
if txn_commands:
|
|
|
|
with self.ovn_api.transaction(check_error=True) as txn:
|
|
|
|
for cmd in txn_commands:
|
|
|
|
txn.add(cmd)
|
|
|
|
LOG.debug('OVN-NB Sync DHCP options for Neutron subnets finished')
|
|
|
|
|
|
|
|
def _sync_port_dhcp_options(self, ctx, ports_need_sync_dhcp_opts,
|
2016-06-08 14:43:45 +05:30
|
|
|
ovn_port_dhcpv4_opts, ovn_port_dhcpv6_opts):
|
2016-08-02 19:16:11 +05:30
|
|
|
LOG.debug('OVN-NB Sync DHCP options for Neutron ports with extra '
|
|
|
|
'dhcp options assigned started')
|
|
|
|
|
|
|
|
txn_commands = []
|
2016-06-08 14:43:45 +05:30
|
|
|
lsp_dhcp_key = {constants.IP_VERSION_4: 'dhcpv4_options',
|
|
|
|
constants.IP_VERSION_6: 'dhcpv6_options'}
|
|
|
|
ovn_port_dhcp_opts = {constants.IP_VERSION_4: ovn_port_dhcpv4_opts,
|
|
|
|
constants.IP_VERSION_6: ovn_port_dhcpv6_opts}
|
2016-08-02 19:16:11 +05:30
|
|
|
for port in ports_need_sync_dhcp_opts:
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
LOG.debug('Updating DHCP options for port %s in OVN NB DB',
|
|
|
|
port['id'])
|
2016-06-08 14:43:45 +05:30
|
|
|
set_lsp = {}
|
|
|
|
for ip_v in [constants.IP_VERSION_4, constants.IP_VERSION_6]:
|
2017-05-10 14:51:10 +01:00
|
|
|
dhcp_opts = (
|
2017-06-02 18:17:20 +01:00
|
|
|
self._ovn_client._get_port_dhcp_options(
|
2017-05-10 14:51:10 +01:00
|
|
|
port, ip_v))
|
enhance DHCP with improved transaction
Based on improved neutron ovsdb transaction, we can put two ovsdb commands
into to one transaction, when they have dependency on data structure.
Like, before we set port dhcp options for a port, or create a port with port
dhcp options, we need to insert the port dhcp options first, and get the
inserted UUID. And currently, we do these by separated transactions.
Separated transactions make orphan row can happen, and to clean orphan rows
related to ports left behind, we do full table scan to locate them, lose in
performance.
Since the patch will fix the orphan DHCP_Options row issue,
DelLSwitchPortCommand will be also enhanced to remove port dhcp options
referred by port within the same transaction.
Depends-on: I1781794958af1483dabc0f5d17f2df6fea828564
Closes-bug: #1626812
Closes-bug: #1626828
Change-Id: Id078d57fe84e1af8db061ae6b812f162fed02dcd
2016-09-22 07:24:25 +00:00
|
|
|
if not dhcp_opts or 'uuid' in dhcp_opts:
|
2016-06-08 14:43:45 +05:30
|
|
|
# If the Logical_Switch_Port.dhcpv4_options or
|
|
|
|
# dhcpv6_options no longer refers a port dhcp options
|
|
|
|
# created in DHCP_Options earlier, that port dhcp
|
|
|
|
# options will be deleted in the following
|
|
|
|
# ovn_port_dhcp_options handling.
|
2017-06-16 17:16:36 +08:00
|
|
|
set_lsp[lsp_dhcp_key[ip_v]] = [
|
|
|
|
dhcp_opts['uuid']] if dhcp_opts else []
|
enhance DHCP with improved transaction
Based on improved neutron ovsdb transaction, we can put two ovsdb commands
into to one transaction, when they have dependency on data structure.
Like, before we set port dhcp options for a port, or create a port with port
dhcp options, we need to insert the port dhcp options first, and get the
inserted UUID. And currently, we do these by separated transactions.
Separated transactions make orphan row can happen, and to clean orphan rows
related to ports left behind, we do full table scan to locate them, lose in
performance.
Since the patch will fix the orphan DHCP_Options row issue,
DelLSwitchPortCommand will be also enhanced to remove port dhcp options
referred by port within the same transaction.
Depends-on: I1781794958af1483dabc0f5d17f2df6fea828564
Closes-bug: #1626812
Closes-bug: #1626828
Change-Id: Id078d57fe84e1af8db061ae6b812f162fed02dcd
2016-09-22 07:24:25 +00:00
|
|
|
else:
|
2017-05-10 14:51:10 +01:00
|
|
|
# If port has extra port dhcp
|
|
|
|
# options, a command will returned by
|
2017-06-02 18:17:20 +01:00
|
|
|
# self._ovn_client._get_port_dhcp_options
|
2017-05-10 14:51:10 +01:00
|
|
|
# to add or update port dhcp options.
|
enhance DHCP with improved transaction
Based on improved neutron ovsdb transaction, we can put two ovsdb commands
into to one transaction, when they have dependency on data structure.
Like, before we set port dhcp options for a port, or create a port with port
dhcp options, we need to insert the port dhcp options first, and get the
inserted UUID. And currently, we do these by separated transactions.
Separated transactions make orphan row can happen, and to clean orphan rows
related to ports left behind, we do full table scan to locate them, lose in
performance.
Since the patch will fix the orphan DHCP_Options row issue,
DelLSwitchPortCommand will be also enhanced to remove port dhcp options
referred by port within the same transaction.
Depends-on: I1781794958af1483dabc0f5d17f2df6fea828564
Closes-bug: #1626812
Closes-bug: #1626828
Change-Id: Id078d57fe84e1af8db061ae6b812f162fed02dcd
2016-09-22 07:24:25 +00:00
|
|
|
ovn_port_dhcp_opts[ip_v].pop(port['id'], None)
|
|
|
|
dhcp_options = dhcp_opts['cmd']
|
|
|
|
txn_commands.append(dhcp_options)
|
|
|
|
set_lsp[lsp_dhcp_key[ip_v]] = dhcp_options
|
2016-06-08 14:43:45 +05:30
|
|
|
if set_lsp:
|
2016-08-02 19:16:11 +05:30
|
|
|
txn_commands.append(self.ovn_api.set_lswitch_port(
|
2016-06-08 14:43:45 +05:30
|
|
|
lport_name=port['id'], **set_lsp))
|
|
|
|
|
|
|
|
for ip_v in [constants.IP_VERSION_4, constants.IP_VERSION_6]:
|
|
|
|
for port_id, dhcp_opt in ovn_port_dhcp_opts[ip_v].items():
|
|
|
|
LOG.warning(
|
2017-04-27 10:20:54 -07:00
|
|
|
'Out of sync port DHCPv%(ip_version)d options for '
|
|
|
|
'(subnet %(subnet_id)s port %(port_id)s) found in OVN '
|
|
|
|
'NB DB which needs to be deleted',
|
2016-06-08 14:43:45 +05:30
|
|
|
{'ip_version': ip_v,
|
|
|
|
'subnet_id': dhcp_opt['external_ids']['subnet_id'],
|
|
|
|
'port_id': port_id})
|
2016-08-02 19:16:11 +05:30
|
|
|
|
2016-06-08 14:43:45 +05:30
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
LOG.debug('Deleting port DHCPv%d options for (subnet %s, '
|
|
|
|
'port %s)', ip_v,
|
|
|
|
dhcp_opt['external_ids']['subnet_id'], port_id)
|
|
|
|
txn_commands.append(self.ovn_api.delete_dhcp_options(
|
|
|
|
dhcp_opt['uuid']))
|
2016-08-02 19:16:11 +05:30
|
|
|
|
|
|
|
if txn_commands:
|
|
|
|
with self.ovn_api.transaction(check_error=True) as txn:
|
|
|
|
for cmd in txn_commands:
|
|
|
|
txn.add(cmd)
|
|
|
|
LOG.debug('OVN-NB Sync DHCP options for Neutron ports with extra '
|
|
|
|
'dhcp options assigned finished')
|
|
|
|
|
2017-10-13 13:49:05 +08:00
|
|
|
def _sync_metadata_ports(self, ctx, db_ports):
|
2017-09-22 11:06:35 +02:00
|
|
|
"""Ensure metadata ports in all Neutron networks.
|
|
|
|
|
|
|
|
This method will ensure that all networks have one and only one
|
2017-10-18 16:49:10 +02:00
|
|
|
metadata port.
|
2017-09-22 11:06:35 +02:00
|
|
|
"""
|
|
|
|
if not config.is_ovn_metadata_enabled():
|
|
|
|
return
|
|
|
|
LOG.debug('OVN sync metadata ports started')
|
|
|
|
for net in self.core_plugin.get_networks(ctx):
|
2021-05-02 13:40:38 +02:00
|
|
|
# Get only DHCP ports that don't belong to agent, it should return
|
|
|
|
# only OVN metadata ports
|
|
|
|
dhcp_ports = [
|
|
|
|
p for p in self.core_plugin.get_ports(
|
|
|
|
ctx, filters=dict(
|
|
|
|
network_id=[net['id']],
|
|
|
|
device_owner=[constants.DEVICE_OWNER_DHCP]))
|
|
|
|
if not utils.is_neutron_dhcp_agent_port(p)]
|
2018-11-21 10:59:09 +00:00
|
|
|
|
2017-09-22 11:06:35 +02:00
|
|
|
if not dhcp_ports:
|
|
|
|
LOG.warning('Missing metadata port found in Neutron for '
|
|
|
|
'network %s', net['id'])
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
try:
|
2017-10-18 16:49:10 +02:00
|
|
|
# Create the missing port in both Neutron and OVN.
|
2017-09-22 11:06:35 +02:00
|
|
|
LOG.warning('Creating missing metadadata port in '
|
2017-10-18 16:49:10 +02:00
|
|
|
'Neutron and OVN for network %s',
|
|
|
|
net['id'])
|
2017-09-22 11:06:35 +02:00
|
|
|
self._ovn_client.create_metadata_port(ctx, net)
|
|
|
|
except n_exc.IpAddressGenerationFailure:
|
|
|
|
LOG.error('Could not allocate IP addresses for '
|
|
|
|
'metadata port in network %s', net['id'])
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# Delete all but one DHCP ports. Only one is needed for
|
|
|
|
# metadata.
|
|
|
|
for port in dhcp_ports[1:]:
|
|
|
|
LOG.warning('Unnecessary DHCP port %s for network %s '
|
|
|
|
'found in Neutron', port['id'], net['id'])
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
LOG.warning('Deleting unnecessary DHCP port %s for '
|
|
|
|
'network %s', port['id'], net['id'])
|
|
|
|
self.core_plugin.delete_port(ctx, port['id'])
|
2017-10-13 13:49:05 +08:00
|
|
|
db_ports.pop(port['id'], None)
|
|
|
|
port = dhcp_ports[0]
|
|
|
|
if port['id'] in db_ports.keys():
|
|
|
|
LOG.warning('Metadata port %s for network %s found in '
|
|
|
|
'Neutron but not in OVN',
|
|
|
|
port['id'], net['id'])
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
LOG.warning('Creating metadata port %s for network '
|
|
|
|
'%s in OVN',
|
|
|
|
port['id'], net['id'])
|
|
|
|
self._create_port_in_ovn(ctx, port)
|
|
|
|
db_ports.pop(port['id'])
|
|
|
|
|
2017-09-22 11:06:35 +02:00
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
# Make sure that this port has an IP address in all the subnets
|
|
|
|
self._ovn_client.update_metadata_port(ctx, net['id'])
|
|
|
|
LOG.debug('OVN sync metadata ports finished')
|
|
|
|
|
2016-08-02 19:16:11 +05:30
|
|
|
def sync_networks_ports_and_dhcp_opts(self, ctx):
|
|
|
|
LOG.debug('OVN-NB Sync networks, ports and DHCP options started')
|
2016-02-06 23:22:40 +05:30
|
|
|
db_networks = {}
|
|
|
|
for net in self.core_plugin.get_networks(ctx):
|
|
|
|
db_networks[utils.ovn_name(net['id'])] = net
|
2015-04-16 14:54:59 +03:00
|
|
|
|
2017-02-17 16:47:45 +08:00
|
|
|
# Ignore the floating ip ports with device_owner set to
|
|
|
|
# constants.DEVICE_OWNER_FLOATINGIP
|
|
|
|
db_ports = {port['id']: port for port in
|
|
|
|
self.core_plugin.get_ports(ctx) if not
|
2017-11-28 17:17:20 +08:00
|
|
|
utils.is_lsp_ignored(port)}
|
2016-02-06 23:22:40 +05:30
|
|
|
|
2016-08-02 19:16:11 +05:30
|
|
|
ovn_all_dhcp_options = self.ovn_api.get_all_dhcp_options()
|
|
|
|
db_network_cache = dict(db_networks)
|
|
|
|
|
|
|
|
ports_need_sync_dhcp_opts = []
|
2016-02-06 23:22:40 +05:30
|
|
|
lswitches = self.ovn_api.get_all_logical_switches_with_ports()
|
|
|
|
del_lswitchs_list = []
|
|
|
|
del_lports_list = []
|
2017-03-17 17:23:33 +08:00
|
|
|
add_provnet_ports_list = []
|
2020-03-25 15:30:12 +00:00
|
|
|
del_provnet_ports_list = []
|
2016-02-06 23:22:40 +05:30
|
|
|
for lswitch in lswitches:
|
|
|
|
if lswitch['name'] in db_networks:
|
|
|
|
for lport in lswitch['ports']:
|
|
|
|
if lport in db_ports:
|
2017-10-13 13:25:07 +08:00
|
|
|
port = db_ports.pop(lport)
|
|
|
|
if not utils.is_network_device_port(port):
|
|
|
|
ports_need_sync_dhcp_opts.append(port)
|
2016-02-06 23:22:40 +05:30
|
|
|
else:
|
|
|
|
del_lports_list.append({'port': lport,
|
|
|
|
'lswitch': lswitch['name']})
|
2017-03-17 17:23:33 +08:00
|
|
|
db_network = db_networks[lswitch['name']]
|
2020-03-25 15:30:12 +00:00
|
|
|
db_segments = self.segments_plugin.get_segments(
|
|
|
|
ctx, filters={'network_id': [db_network['id']]})
|
|
|
|
segments_provnet_port_names = []
|
|
|
|
for db_segment in db_segments:
|
|
|
|
physnet = db_segment.get(segment_def.PHYSICAL_NETWORK)
|
|
|
|
pname = utils.ovn_provnet_port_name(db_segment['id'])
|
|
|
|
segments_provnet_port_names.append(pname)
|
|
|
|
if physnet and pname not in lswitch['provnet_ports']:
|
|
|
|
add_provnet_ports_list.append(
|
|
|
|
{'network': db_network,
|
|
|
|
'segment': db_segment,
|
|
|
|
'lswitch': lswitch['name']})
|
|
|
|
# Delete orhpaned provnet ports
|
|
|
|
for provnet_port in lswitch['provnet_ports']:
|
|
|
|
if provnet_port in segments_provnet_port_names:
|
|
|
|
continue
|
|
|
|
if provnet_port not in [
|
|
|
|
utils.ovn_provnet_port_name(v['segment'])
|
|
|
|
for v in add_provnet_ports_list]:
|
|
|
|
del_provnet_ports_list.append(
|
|
|
|
{'network': db_network,
|
|
|
|
'lport': provnet_port,
|
|
|
|
'lswitch': lswitch['name']})
|
2017-03-17 17:23:33 +08:00
|
|
|
|
2016-02-06 23:22:40 +05:30
|
|
|
del db_networks[lswitch['name']]
|
|
|
|
else:
|
|
|
|
del_lswitchs_list.append(lswitch)
|
|
|
|
|
|
|
|
for net_id, network in db_networks.items():
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Network found in Neutron but not in "
|
|
|
|
"OVN DB, network_id=%s", network['id'])
|
2016-02-06 23:22:40 +05:30
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
try:
|
|
|
|
LOG.debug('Creating the network %s in OVN NB DB',
|
|
|
|
network['id'])
|
2017-12-08 18:19:38 +08:00
|
|
|
self._ovn_client.create_network(network)
|
2016-02-06 23:22:40 +05:30
|
|
|
except RuntimeError:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Create network in OVN NB failed for "
|
|
|
|
"network %s", network['id'])
|
2016-02-06 23:22:40 +05:30
|
|
|
|
2017-10-13 13:49:05 +08:00
|
|
|
self._sync_metadata_ports(ctx, db_ports)
|
2017-09-22 11:06:35 +02:00
|
|
|
|
2016-08-02 19:16:11 +05:30
|
|
|
self._sync_subnet_dhcp_options(
|
|
|
|
ctx, db_network_cache, ovn_all_dhcp_options['subnets'])
|
|
|
|
|
2016-02-06 23:22:40 +05:30
|
|
|
for port_id, port in db_ports.items():
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Port found in Neutron but not in OVN "
|
|
|
|
"DB, port_id=%s", port['id'])
|
2016-02-06 23:22:40 +05:30
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
try:
|
|
|
|
LOG.debug('Creating the port %s in OVN NB DB',
|
|
|
|
port['id'])
|
|
|
|
self._create_port_in_ovn(ctx, port)
|
2016-06-08 14:43:45 +05:30
|
|
|
if port_id in ovn_all_dhcp_options['ports_v4']:
|
|
|
|
_, lsp_opts = utils.get_lsp_dhcp_opts(
|
|
|
|
port, constants.IP_VERSION_4)
|
|
|
|
if lsp_opts:
|
|
|
|
ovn_all_dhcp_options['ports_v4'].pop(port_id)
|
|
|
|
if port_id in ovn_all_dhcp_options['ports_v6']:
|
|
|
|
_, lsp_opts = utils.get_lsp_dhcp_opts(
|
|
|
|
port, constants.IP_VERSION_6)
|
2016-08-02 19:16:11 +05:30
|
|
|
if lsp_opts:
|
2016-06-08 14:43:45 +05:30
|
|
|
ovn_all_dhcp_options['ports_v6'].pop(port_id)
|
2016-02-06 23:22:40 +05:30
|
|
|
except RuntimeError:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Create port in OVN NB failed for"
|
|
|
|
" port %s", port['id'])
|
2016-02-06 23:22:40 +05:30
|
|
|
|
|
|
|
with self.ovn_api.transaction(check_error=True) as txn:
|
|
|
|
for lswitch in del_lswitchs_list:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Network found in OVN but not in "
|
|
|
|
"Neutron, network_id=%s", lswitch['name'])
|
2016-02-06 23:22:40 +05:30
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
LOG.debug('Deleting the network %s from OVN NB DB',
|
|
|
|
lswitch['name'])
|
2017-10-19 17:13:18 -05:00
|
|
|
txn.add(self.ovn_api.ls_del(lswitch['name']))
|
2016-02-06 23:22:40 +05:30
|
|
|
|
2017-03-17 17:23:33 +08:00
|
|
|
for provnet_port_info in add_provnet_ports_list:
|
|
|
|
network = provnet_port_info['network']
|
2020-03-25 15:30:12 +00:00
|
|
|
segment = provnet_port_info['segment']
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Provider network found in Neutron but "
|
|
|
|
"provider network port not found in OVN DB, "
|
2020-03-25 15:30:12 +00:00
|
|
|
"network_id=%(net)s segment_id=%(seg)s",
|
|
|
|
{'net': network['id'],
|
|
|
|
'seg': segment['id']})
|
2017-03-17 17:23:33 +08:00
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
LOG.debug('Creating the provnet port %s in OVN NB DB',
|
2020-03-25 15:30:12 +00:00
|
|
|
utils.ovn_provnet_port_name(segment['id']))
|
|
|
|
self._ovn_client.create_provnet_port(
|
|
|
|
network['id'], segment, txn=txn)
|
|
|
|
|
|
|
|
for provnet_port_info in del_provnet_ports_list:
|
|
|
|
network = provnet_port_info['network']
|
|
|
|
lport = provnet_port_info['lport']
|
|
|
|
lswitch = provnet_port_info['lswitch']
|
|
|
|
LOG.warning("Provider network port found in OVN DB, "
|
|
|
|
"but not in neutron network_id=%(net)s "
|
|
|
|
"port_name=%(lport)s",
|
|
|
|
{'net': network,
|
|
|
|
'seg': lport})
|
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
|
|
|
LOG.debug('Deleting the port %s from OVN NB DB',
|
|
|
|
lport)
|
|
|
|
txn.add(self.ovn_api.delete_lswitch_port(
|
|
|
|
lport_name=lport,
|
|
|
|
lswitch_name=lswitch))
|
2017-03-17 17:23:33 +08:00
|
|
|
|
2016-02-06 23:22:40 +05:30
|
|
|
for lport_info in del_lports_list:
|
2017-04-27 10:20:54 -07:00
|
|
|
LOG.warning("Port found in OVN but not in "
|
|
|
|
"Neutron, port_id=%s", lport_info['port'])
|
2015-04-16 14:54:59 +03:00
|
|
|
if self.mode == SYNC_MODE_REPAIR:
|
2016-02-06 23:22:40 +05:30
|
|
|
LOG.debug('Deleting the port %s from OVN NB DB',
|
|
|
|
lport_info['port'])
|
2016-06-13 15:29:26 -05:00
|
|
|
txn.add(self.ovn_api.delete_lswitch_port(
|
2016-02-06 23:22:40 +05:30
|
|
|
lport_name=lport_info['port'],
|
2016-06-13 15:29:26 -05:00
|
|
|
lswitch_name=lport_info['lswitch']))
|
2016-06-08 14:43:45 +05:30
|
|
|
if lport_info['port'] in ovn_all_dhcp_options['ports_v4']:
|
|
|
|
LOG.debug('Deleting port DHCPv4 options for (port %s)',
|
|
|
|
lport_info['port'])
|
|
|
|
txn.add(self.ovn_api.delete_dhcp_options(
|
|
|
|
ovn_all_dhcp_options['ports_v4'].pop(
|
|
|
|
lport_info['port'])['uuid']))
|
|
|
|
if lport_info['port'] in ovn_all_dhcp_options['ports_v6']:
|
|
|
|
LOG.debug('Deleting port DHCPv6 options for (port %s)',
|
2016-08-02 19:16:11 +05:30
|
|
|
lport_info['port'])
|
|
|
|
txn.add(self.ovn_api.delete_dhcp_options(
|
2016-06-08 14:43:45 +05:30
|
|
|
ovn_all_dhcp_options['ports_v6'].pop(
|
2016-08-02 19:16:11 +05:30
|
|
|
lport_info['port'])['uuid']))
|
|
|
|
|
|
|
|
self._sync_port_dhcp_options(ctx, ports_need_sync_dhcp_opts,
|
2016-06-08 14:43:45 +05:30
|
|
|
ovn_all_dhcp_options['ports_v4'],
|
|
|
|
ovn_all_dhcp_options['ports_v6'])
|
2016-08-02 19:16:11 +05:30
|
|
|
LOG.debug('OVN-NB Sync networks, ports and DHCP options finished')
|
2016-06-14 06:58:02 +00:00
|
|
|
|
2017-11-21 13:04:25 +05:30
|
|
|
def sync_port_dns_records(self, ctx):
|
|
|
|
if self.mode != SYNC_MODE_REPAIR:
|
|
|
|
return
|
|
|
|
LOG.debug('OVN-NB Sync port dns records')
|
|
|
|
# Ignore the floating ip ports with device_owner set to
|
|
|
|
# constants.DEVICE_OWNER_FLOATINGIP
|
|
|
|
db_ports = [port for port in
|
|
|
|
self.core_plugin.get_ports(ctx) if not
|
|
|
|
port.get('device_owner', '').startswith(
|
|
|
|
constants.DEVICE_OWNER_FLOATINGIP)]
|
|
|
|
dns_records = {}
|
|
|
|
for port in db_ports:
|
|
|
|
if self._ovn_client.is_dns_required_for_port(port):
|
|
|
|
port_dns_records = self._ovn_client.get_port_dns_records(port)
|
|
|
|
if port['network_id'] not in dns_records:
|
|
|
|
dns_records[port['network_id']] = {}
|
|
|
|
dns_records[port['network_id']].update(port_dns_records)
|
|
|
|
|
|
|
|
for network_id, port_dns_records in dns_records.items():
|
|
|
|
self._set_dns_records(network_id, port_dns_records)
|
|
|
|
|
|
|
|
def _set_dns_records(self, network_id, dns_records):
|
|
|
|
lswitch_name = utils.ovn_name(network_id)
|
|
|
|
ls, ls_dns_record = self.ovn_api.get_ls_and_dns_record(lswitch_name)
|
|
|
|
|
|
|
|
with self.ovn_api.transaction(check_error=True) as txn:
|
|
|
|
if not ls_dns_record:
|
|
|
|
dns_add_txn = txn.add(self.ovn_api.dns_add(
|
|
|
|
external_ids={'ls_name': ls.name}, records=dns_records))
|
|
|
|
txn.add(self.ovn_api.ls_set_dns_records(ls.uuid, dns_add_txn))
|
|
|
|
else:
|
|
|
|
txn.add(self.ovn_api.dns_set_records(ls_dns_record.uuid,
|
|
|
|
**dns_records))
|
|
|
|
|
2018-05-23 15:04:07 +02:00
|
|
|
def _delete_address_sets(self, ctx):
|
|
|
|
with self.ovn_api.transaction(check_error=True) as txn:
|
|
|
|
for sg in self.core_plugin.get_security_groups(ctx):
|
|
|
|
for ip_version in ['ip4', 'ip6']:
|
|
|
|
txn.add(self.ovn_api.delete_address_set(
|
|
|
|
utils.ovn_addrset_name(sg['id'], ip_version)))
|
|
|
|
|
|
|
|
def _delete_acls_from_lswitches(self, ctx):
|
|
|
|
with self.ovn_api.transaction(check_error=True) as txn:
|
|
|
|
for net in self.core_plugin.get_networks(ctx):
|
|
|
|
# Calling acl_del from ovsdbapp with no ACL will delete
|
|
|
|
# all the ACLs belonging to that Logical Switch.
|
|
|
|
txn.add(self.ovn_api.acl_del(utils.ovn_name(net['id'])))
|
|
|
|
|
|
|
|
def _create_sg_port_groups_and_acls(self, ctx, db_ports):
|
|
|
|
# Create a Port Group per Neutron Security Group
|
|
|
|
with self.ovn_api.transaction(check_error=True) as txn:
|
|
|
|
for sg in self.core_plugin.get_security_groups(ctx):
|
|
|
|
pg_name = utils.ovn_port_group_name(sg['id'])
|
|
|
|
if self.ovn_api.get_port_group(pg_name):
|
|
|
|
continue
|
|
|
|
ext_ids = {const.OVN_SG_EXT_ID_KEY: sg['id']}
|
|
|
|
txn.add(self.ovn_api.pg_add(
|
|
|
|
name=pg_name, acls=[], external_ids=ext_ids))
|
|
|
|
acl_utils.add_acls_for_sg_port_group(self.ovn_api, sg, txn)
|
|
|
|
for port in db_ports:
|
|
|
|
for sg in port['security_groups']:
|
|
|
|
txn.add(self.ovn_api.pg_add_ports(
|
|
|
|
utils.ovn_port_group_name(sg), port['id']))
|
|
|
|
|
|
|
|
def migrate_to_port_groups(self, ctx):
|
|
|
|
# This routine is responsible for migrating the current Security
|
|
|
|
# Groups and SG Rules to the new Port Groups implementation.
|
2020-06-19 17:46:34 -04:00
|
|
|
# 1. Create a Port Group for every existing Neutron Security Group and
|
2018-05-23 15:04:07 +02:00
|
|
|
# add all its Security Group Rules as ACLs to that Port Group.
|
2020-06-19 17:46:34 -04:00
|
|
|
# 2. Delete all existing Address Sets in NorthBound database which
|
2018-05-23 15:04:07 +02:00
|
|
|
# correspond to a Neutron Security Group.
|
2020-06-19 17:46:34 -04:00
|
|
|
# 3. Delete all the ACLs in every Logical Switch (Neutron network).
|
2018-08-30 19:08:33 +02:00
|
|
|
|
2020-06-19 17:46:34 -04:00
|
|
|
# If we've already migrated, return
|
|
|
|
if not self.ovn_api.get_address_sets():
|
2018-05-23 15:04:07 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
LOG.debug('Port Groups Migration task started')
|
|
|
|
|
|
|
|
# Ignore the floating ip ports with device_owner set to
|
|
|
|
# constants.DEVICE_OWNER_FLOATINGIP
|
|
|
|
db_ports = [port for port in
|
|
|
|
self.core_plugin.get_ports(ctx) if not
|
|
|
|
utils.is_lsp_ignored(port) and not
|
|
|
|
utils.is_lsp_trusted(port) and
|
|
|
|
utils.is_port_security_enabled(port)]
|
|
|
|
|
|
|
|
self._create_sg_port_groups_and_acls(ctx, db_ports)
|
|
|
|
self._delete_address_sets(ctx)
|
|
|
|
self._delete_acls_from_lswitches(ctx)
|
|
|
|
|
|
|
|
LOG.debug('Port Groups Migration task finished')
|
|
|
|
|
2016-06-14 06:58:02 +00:00
|
|
|
|
|
|
|
class OvnSbSynchronizer(OvnDbSynchronizer):
|
|
|
|
"""Synchronizer class for SB."""
|
|
|
|
|
2016-06-21 11:53:01 -07:00
|
|
|
def __init__(self, core_plugin, ovn_api, ovn_driver):
|
|
|
|
super(OvnSbSynchronizer, self).__init__(
|
|
|
|
core_plugin, ovn_api, ovn_driver)
|
2017-06-27 13:07:26 -06:00
|
|
|
self.l3_plugin = directory.get_plugin(plugin_constants.L3)
|
2016-06-21 11:53:01 -07:00
|
|
|
|
2017-05-05 10:36:58 +08:00
|
|
|
def do_sync(self):
|
2016-06-14 06:58:02 +00:00
|
|
|
"""Method to sync the OVN_Southbound DB with neutron DB.
|
|
|
|
|
|
|
|
OvnSbSynchronizer will sync data from OVN_Southbound to neutron. And
|
|
|
|
the synchronization will always be performed, no matter what mode it
|
|
|
|
is.
|
|
|
|
"""
|
|
|
|
LOG.debug("Starting OVN-Southbound DB sync process")
|
|
|
|
|
|
|
|
ctx = context.get_admin_context()
|
|
|
|
self.sync_hostname_and_physical_networks(ctx)
|
2018-01-03 13:37:28 +08:00
|
|
|
if utils.is_ovn_l3(self.l3_plugin):
|
2017-01-23 17:50:49 +08:00
|
|
|
self.l3_plugin.schedule_unhosted_gateways()
|
2016-06-14 06:58:02 +00:00
|
|
|
|
|
|
|
def sync_hostname_and_physical_networks(self, ctx):
|
|
|
|
LOG.debug('OVN-SB Sync hostname and physical networks started')
|
|
|
|
host_phynets_map = self.ovn_api.get_chassis_hostname_and_physnets()
|
2016-07-31 16:40:08 +08:00
|
|
|
current_hosts = set(host_phynets_map)
|
|
|
|
previous_hosts = segments_db.get_hosts_mapped_with_segments(ctx)
|
|
|
|
|
|
|
|
stale_hosts = previous_hosts - current_hosts
|
|
|
|
for host in stale_hosts:
|
|
|
|
LOG.debug('Stale host %s found in Neutron, but not in OVN SB DB. '
|
|
|
|
'Clear its SegmentHostMapping in Neutron', host)
|
|
|
|
self.ovn_driver.update_segment_host_mapping(host, [])
|
|
|
|
|
|
|
|
new_hosts = current_hosts - previous_hosts
|
|
|
|
for host in new_hosts:
|
|
|
|
LOG.debug('New host %s found in OVN SB DB, but not in Neutron. '
|
|
|
|
'Add its SegmentHostMapping in Neutron', host)
|
|
|
|
self.ovn_driver.update_segment_host_mapping(
|
|
|
|
host, host_phynets_map[host])
|
|
|
|
|
|
|
|
for host in current_hosts & previous_hosts:
|
|
|
|
LOG.debug('Host %s found both in OVN SB DB and Neutron. '
|
|
|
|
'Trigger updating its SegmentHostMapping in Neutron, '
|
|
|
|
'to keep OVN SB DB and Neutron have consistent data',
|
|
|
|
host)
|
|
|
|
self.ovn_driver.update_segment_host_mapping(
|
|
|
|
host, host_phynets_map[host])
|
|
|
|
|
2016-06-14 06:58:02 +00:00
|
|
|
LOG.debug('OVN-SB Sync hostname and physical networks finished')
|