Debian Codegraph @

neutron_vpnaas/agent/ovn/vpn/agent.py

raw
1 # Copyright 2017 Red Hat, Inc.
2 # Copyright 2023 SysEleven GmbH
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import uuid
17
18 from neutron.agent.linux import external_process
19 from neutron.common.ovn import utils as ovn_utils
20 from neutron.conf.plugins.ml2.drivers.ovn import ovn_conf as config
21 from oslo_log import log as logging
22 from oslo_service import service
23 from ovsdbapp.backend.ovs_idl import event as row_event
24 from ovsdbapp.backend.ovs_idl import vlog
25
26 from neutron_vpnaas.agent.ovn.vpn import ovsdb
27 from neutron_vpnaas.services.vpn.common import constants
28 from neutron_vpnaas.services.vpn import vpn_service
29
30 LOG = logging.getLogger(__name__)
31
32 OVN_VPNAGENT_UUID_NAMESPACE = uuid.UUID('e1ce3b12-b1e0-4c81-ba27-07c0fec9c12b')
33
34
35 class ChassisCreateEventBase(row_event.RowEvent):
36 """Row create event - Chassis name == our_chassis.
37
38 On connection, we get a dump of all chassis so if we catch a creation
39 of our own chassis it has to be a reconnection. In this case, we need
40 to do a full sync to make sure that we capture all changes while the
41 connection to OVSDB was down.
42 """
43 table = None
44
45 def __init__(self, vpn_agent):
46 self.agent = vpn_agent
47 self.first_time = True
48 events = (self.ROW_CREATE,)
49 super().__init__(
50 events, self.table, (('name', '=', self.agent.chassis),))
51 self.event_name = self.__class__.__name__
52
53 def run(self, event, row, old):
54 if self.first_time:
55 self.first_time = False
56 else:
57 # NOTE(lucasagomes): Re-register the ovn vpn agent
58 # with the local chassis in case its entry was re-created
59 # (happens when restarting the ovn-controller)
60 self.agent.register_vpn_agent()
61 LOG.info("Connection to OVSDB established, doing a full sync")
62 self.agent.sync()
63
64
65 class ChassisCreateEvent(ChassisCreateEventBase):
66 table = 'Chassis'
67
68
69 class ChassisPrivateCreateEvent(ChassisCreateEventBase):
70 table = 'Chassis_Private'
71
72
73 class SbGlobalUpdateEvent(row_event.RowEvent):
74 """Row update event on SB_Global table."""
75
76 def __init__(self, vpn_agent):
77 self.agent = vpn_agent
78 table = 'SB_Global'
79 events = (self.ROW_UPDATE,)
80 super().__init__(events, table, None)
81 self.event_name = self.__class__.__name__
82
83 def run(self, event, row, old):
84 table = ('Chassis_Private' if self.agent.has_chassis_private
85 else 'Chassis')
86 external_ids = {constants.OVN_AGENT_VPN_SB_CFG_KEY: str(row.nb_cfg)}
87 self.agent.sb_idl.db_set(
88 table, self.agent.chassis,
89 ('external_ids', external_ids)).execute()
90
91
92 class OvnVpnAgent(service.Service):
93 def __init__(self, conf):
94 super().__init__()
95 self.conf = conf
96 vlog.use_python_logger(max_level=config.get_ovn_ovsdb_log_level())
97 self._process_monitor = external_process.ProcessMonitor(
98 config=self.conf,
99 resource_type='ipsec')
100
101 self.service = vpn_service.VPNService(self)
102 self.device_drivers = self.service.load_device_drivers(self.conf.host)
103
104 def _load_config(self):
105 self.chassis = self._get_own_chassis_name()
106 try:
107 self.chassis_id = uuid.UUID(self.chassis)
108 except ValueError:
109 # OVS system-id could be a non UUID formatted string.
110 self.chassis_id = uuid.uuid5(OVN_VPNAGENT_UUID_NAMESPACE,
111 self.chassis)
112 LOG.debug("Loaded chassis name %s (UUID: %s).",
113 self.chassis, self.chassis_id)
114
115 def start(self):
116 super().start()
117
118 self.ovs_idl = ovsdb.VPNAgentOvsIdl().start()
119 self._load_config()
120
121 tables = ('SB_Global', 'Chassis')
122 events = (SbGlobalUpdateEvent(self), )
123 # TODO(lucasagomes): Remove this in the future. Try to register
124 # the Chassis_Private table, if not present, fallback to the normal
125 # Chassis table.
126 # Open the connection to OVN SB database.
127 self.has_chassis_private = False
128 try:
129 self.sb_idl = ovsdb.VPNAgentOvnSbIdl(
130 chassis=self.chassis, tables=tables + ('Chassis_Private', ),
131 events=events + (ChassisPrivateCreateEvent(self), )).start()
132 self.has_chassis_private = True
133 except AssertionError:
134 self.sb_idl = ovsdb.VPNAgentOvnSbIdl(
135 chassis=self.chassis, tables=tables,
136 events=events + (ChassisCreateEvent(self), )).start()
137
138 # Register the agent with its corresponding Chassis
139 self.register_vpn_agent()
140
141 # Do the initial sync.
142 self.sync()
143
144 def sync(self):
145 for driver in self.device_drivers:
146 driver.sync(driver.context, [])
147
148 @ovn_utils.retry()
149 def register_vpn_agent(self):
150 # NOTE(lucasagomes): db_add() will not overwrite the UUID if
151 # it's already set.
152 table = ('Chassis_Private' if self.has_chassis_private else 'Chassis')
153 # Generate unique, but consistent vpn agent id for chassis name
154 agent_id = uuid.uuid5(self.chassis_id, 'vpn_agent')
155 ext_ids = {constants.OVN_AGENT_VPN_ID_KEY: str(agent_id)}
156 self.sb_idl.db_add(table, self.chassis, 'external_ids',
157 ext_ids).execute(check_error=True)
158
159 def _get_own_chassis_name(self):
160 """Return the external_ids:system-id value of the Open_vSwitch table.
161
162 As long as ovn-controller is running on this node, the key is
163 guaranteed to exist and will include the chassis name.
164 """
165 ext_ids = self.ovs_idl.db_get(
166 'Open_vSwitch', '.', 'external_ids').execute()
167 return ext_ids['system-id']