| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from datetime import datetime |
| 14 |
|
| 15 |
from neutron.common.ovn import constants as ovn_const |
| 16 |
from neutron.common.ovn import utils as ovn_utils |
| 17 |
from neutron_lib import context |
| 18 |
from neutron_lib.db import api as db_api |
| 19 |
from neutron_lib.ovn import db_sync as db_sync_base |
| 20 |
from neutron_lib.plugins import directory |
| 21 |
from oslo_log import log |
| 22 |
|
| 23 |
from neutron_fwaas.common import fwaas_constants |
| 24 |
from neutron_fwaas.services.firewall.service_drivers.ovn import \ |
| 25 |
acl as ovn_acl |
| 26 |
from neutron_fwaas.services.firewall.service_drivers.ovn import \ |
| 27 |
constants as ovn_fw_const |
| 28 |
from neutron_fwaas.services.firewall.service_drivers.ovn import \ |
| 29 |
firewall_l3_driver as ovn_fwaas_driver |
| 30 |
|
| 31 |
|
| 32 |
LOG = log.getLogger(__name__) |
| 33 |
OVN_SERVICE_PROVIDER = ( |
| 34 |
"FIREWALL_V2:" |
| 35 |
"fwaas_db:neutron_fwaas.services.firewall." |
| 36 |
"service_drivers.ovn.firewall_l3_driver.OVNFwaasDriver:" |
| 37 |
"default" |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
class OvnNbDbSync(db_sync_base.BaseOvnDbSynchronizer): |
| 42 |
|
| 43 |
_required_service_plugins = [ |
| 44 |
"neutron_fwaas.services.firewall.fwaas_plugin_v2.FirewallPluginV2" |
| 45 |
] |
| 46 |
|
| 47 |
def __init__(self, core_plugin, ovn_driver, mode, is_maintenance=False): |
| 48 |
super().__init__(core_plugin, ovn_driver, mode, is_maintenance) |
| 49 |
self.fwaas_plugin = directory.get_plugin(fwaas_constants.FIREWALL_V2) |
| 50 |
self.fwaas_ovn_driver = self.fwaas_plugin.driver |
| 51 |
|
| 52 |
@classmethod |
| 53 |
def prepare_additional_configuration(cls, conf): |
| 54 |
|
| 55 |
conf.set_override( |
| 56 |
'service_provider', [OVN_SERVICE_PROVIDER], 'service_providers') |
| 57 |
|
| 58 |
def do_sync(self): |
| 59 |
if not isinstance(self.fwaas_ovn_driver, |
| 60 |
ovn_fwaas_driver.OVNFwaasDriver): |
| 61 |
LOG.warning("OVN FWaaS driver is required for OVN DB sync") |
| 62 |
return |
| 63 |
|
| 64 |
if self.mode == ovn_const.OVN_DB_SYNC_MODE_OFF: |
| 65 |
LOG.debug("Neutron sync mode is off, not checking OVN " |
| 66 |
"Northbound DB for consistency") |
| 67 |
return |
| 68 |
elif self.mode == ovn_const.OVN_DB_SYNC_MODE_MIGRATE: |
| 69 |
LOG.debug("Neutron FWaaS OVN-Northbound DB sync do not support " |
| 70 |
"migrate mode. Exiting...") |
| 71 |
return |
| 72 |
|
| 73 |
LOG.debug("FWaaS OVN-Northbound DB sync process started @ %s", |
| 74 |
str(datetime.now())) |
| 75 |
|
| 76 |
ctx = context.get_admin_context() |
| 77 |
self._sync_firewall_groups(ctx) |
| 78 |
|
| 79 |
LOG.debug("FWaaS OVN-Northbound DB sync process completed @ %s", |
| 80 |
str(datetime.now())) |
| 81 |
|
| 82 |
def _get_fw_port_groups_from_neutron_db(self, ctx): |
| 83 |
neutron_fwaas_groups = {} |
| 84 |
with db_api.CONTEXT_READER.using(ctx): |
| 85 |
for fwg in self.fwaas_ovn_driver.get_firewall_groups(ctx): |
| 86 |
if fwg['name'] == fwaas_constants.DEFAULT_FWG: |
| 87 |
continue |
| 88 |
neutron_fwaas_groups[fwg['id']] = fwg |
| 89 |
return neutron_fwaas_groups |
| 90 |
|
| 91 |
def _get_fw_port_groups_from_ovn_db(self): |
| 92 |
ovn_port_groups = {} |
| 93 |
for pg in self.ovn_nb_api.db_list_rows('Port_Group').execute() or []: |
| 94 |
fwaas_group_id = pg.external_ids.get( |
| 95 |
ovn_fw_const.OVN_FWG_EXT_ID_KEY) |
| 96 |
if fwaas_group_id: |
| 97 |
ovn_port_groups[fwaas_group_id] = { |
| 98 |
'id': fwaas_group_id, |
| 99 |
'name': pg.name, |
| 100 |
'acls': pg.acls, |
| 101 |
} |
| 102 |
return ovn_port_groups |
| 103 |
|
| 104 |
def _get_firewall_groups(self, ctx): |
| 105 |
neutron_fwaas_groups = self._get_fw_port_groups_from_neutron_db(ctx) |
| 106 |
ovn_port_groups = self._get_fw_port_groups_from_ovn_db() |
| 107 |
ids_to_remove = ( |
| 108 |
set(ovn_port_groups.keys()) - set(neutron_fwaas_groups.keys())) |
| 109 |
groups_to_remove = {id: ovn_port_groups[id] for id in ids_to_remove} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
return neutron_fwaas_groups, groups_to_remove |
| 118 |
|
| 119 |
def _sync_acls_for_firewall_group(self, ctx, txn, fw_group): |
| 120 |
if (fw_group['ingress_firewall_policy_id'] or |
| 121 |
fw_group['egress_firewall_policy_id']): |
| 122 |
with db_api.CONTEXT_READER.using(ctx): |
| 123 |
self.fwaas_plugin.driver._add_rules_for_firewall_group( |
| 124 |
ctx, txn, fw_group['id']) |
| 125 |
|
| 126 |
def _sync_firewall_groups(self, ctx): |
| 127 |
groups_to_sync, groups_to_remove = self._get_firewall_groups(ctx) |
| 128 |
if groups_to_sync or groups_to_remove: |
| 129 |
LOG.warning('Number of Port Groups to sync: %d, remove: %d', |
| 130 |
len(groups_to_sync), len(groups_to_remove)) |
| 131 |
LOG.warning('Port Groups to be synced in OVN: %s', groups_to_sync) |
| 132 |
LOG.warning('Port Groups to remove from OVN: %s', |
| 133 |
groups_to_remove) |
| 134 |
|
| 135 |
if self.mode != ovn_const.OVN_DB_SYNC_MODE_REPAIR: |
| 136 |
return |
| 137 |
|
| 138 |
|
| 139 |
with self.ovn_nb_api.transaction(check_error=True) as txn: |
| 140 |
for fw_group in groups_to_sync.values(): |
| 141 |
pg_name = ovn_utils.ovn_port_group_name(fw_group['id']) |
| 142 |
ovn_acl.create_pg_for_fwg(self.ovn_nb_api, fw_group['id']) |
| 143 |
ovn_acl.add_default_acls_for_pg( |
| 144 |
self.ovn_nb_api, txn, pg_name) |
| 145 |
self._sync_acls_for_firewall_group(ctx, txn, fw_group) |
| 146 |
if fw_group['ports']: |
| 147 |
ovn_acl.update_ports_for_pg( |
| 148 |
self.ovn_nb_api, txn, pg_name, fw_group['ports']) |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
with self.ovn_nb_api.transaction(check_error=True) as txn: |
| 153 |
for fw_group in groups_to_remove.values(): |
| 154 |
pg_name = ovn_utils.ovn_port_group_name(fw_group['id']) |
| 155 |
if self.ovn_nb_api.get_port_group(pg_name): |
| 156 |
txn.add( |
| 157 |
self.ovn_nb_api.pg_del(name=pg_name, if_exists=True)) |