Debian Codegraph @

networking_l2gw/services/l2gateway/ovsdb/data.py

raw
1 # Copyright (c) 2015 OpenStack Foundation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 from networking_l2gw.db.l2gateway import l2gateway_db
17 from networking_l2gw.db.l2gateway.ovsdb import lib as db
18 from networking_l2gw.services.l2gateway.common import constants as n_const
19 from networking_l2gw.services.l2gateway.common import ovsdb_schema
20 from networking_l2gw.services.l2gateway.common import topics
21 from networking_l2gw.services.l2gateway.common import tunnel_calls
22 from networking_l2gw.services.l2gateway import exceptions as l2gw_exc
23 from networking_l2gw.services.l2gateway.service_drivers import agent_api
24
25 from neutron_lib.api.definitions import portbindings
26 from neutron_lib import constants
27 from neutron_lib.plugins import directory
28 from oslo_config import cfg
29 from oslo_log import log as logging
30 import oslo_messaging as messaging
31
32 LOG = logging.getLogger(__name__)
33
34
35 class L2GatewayOVSDBCallbacks:
36 """Implement the rpc call back functions from OVSDB."""
37
38 target = messaging.Target(version='1.0')
39
40 def __init__(self, plugin):
41 super().__init__()
42 self.plugin = plugin
43 self.ovsdb = None
44
45 def update_ovsdb_changes(self, context, activity, ovsdb_data):
46 """RPC to update the changes from OVSDB in the database."""
47 self.ovsdb = self.get_ovsdbdata_object(
48 ovsdb_data.get(n_const.OVSDB_IDENTIFIER))
49 self.ovsdb.update_ovsdb_changes(context, activity, ovsdb_data)
50
51 def notify_ovsdb_states(self, context, ovsdb_states):
52 """RPC to notify the OVSDB servers connection state."""
53 if ovsdb_states:
54 self.ovsdb = self.get_ovsdbdata_object(
55 list(ovsdb_states.keys())[0])
56 if self.ovsdb:
57 LOG.debug("ovsdb_states = %s", ovsdb_states)
58 self.ovsdb.notify_ovsdb_states(context, ovsdb_states)
59
60 def get_ovsdbdata_object(self, ovsdb_identifier):
61 return OVSDBData(ovsdb_identifier)
62
63
64 class OVSDBData:
65 """Process the data coming from OVSDB."""
66
67 def __init__(self, ovsdb_identifier=None):
68 self.ovsdb_identifier = ovsdb_identifier
69 self._setup_entry_table()
70 self.agent_rpc = agent_api.L2gatewayAgentApi(
71 topics.L2GATEWAY_AGENT, cfg.CONF.host)
72 self.l2gw_mixin = l2gateway_db.L2GatewayMixin()
73 self.core_plugin = directory.get_plugin()
74 self.tunnel_call = tunnel_calls.Tunnel_Calls()
75
76 def _cleanup_all_ovsdb_tables(self, context, ovsdb_identifier):
77 db.delete_all_physical_locators_by_ovsdb_identifier(
78 context, ovsdb_identifier)
79 db.delete_all_physical_switches_by_ovsdb_identifier(
80 context, ovsdb_identifier)
81 db.delete_all_physical_ports_by_ovsdb_identifier(
82 context, ovsdb_identifier)
83 db.delete_all_logical_switches_by_ovsdb_identifier(
84 context, ovsdb_identifier)
85 db.delete_all_ucast_macs_locals_by_ovsdb_identifier(
86 context, ovsdb_identifier)
87 db.delete_all_ucast_macs_remotes_by_ovsdb_identifier(
88 context, ovsdb_identifier)
89 db.delete_all_vlan_bindings_by_ovsdb_identifier(
90 context, ovsdb_identifier)
91
92 def update_ovsdb_changes(self, context, activity, ovsdb_data):
93 """RPC to update the changes from OVSDB in the database."""
94 ovsdb_identifier = ovsdb_data.get('ovsdb_identifier')
95 if not activity:
96 self._cleanup_all_ovsdb_tables(context, ovsdb_identifier)
97 for item, value in ovsdb_data.items():
98 lookup = self.entry_table.get(item, None)
99 if lookup:
100 lookup(context, value)
101 if ovsdb_data.get('new_remote_macs'):
102 self._handle_l2pop(context, ovsdb_data.get('new_remote_macs'))
103
104 def notify_ovsdb_states(self, context, ovsdb_states):
105 """RPC to notify the OVSDB servers connection state."""
106 for ovsdb_identifier, state in ovsdb_states.items():
107 if state == 'connected':
108 pending_recs = db.get_all_pending_remote_macs_in_asc_order(
109 context, ovsdb_identifier)
110 if pending_recs:
111 for pending_mac in pending_recs:
112 logical_switch_uuid = pending_mac['logical_switch_uuid'
113 ]
114 mac = pending_mac['mac']
115 operation = pending_mac['operation']
116 try:
117 if operation == 'insert' or operation == 'update':
118 l_switch = ovsdb_schema.LogicalSwitch(
119 logical_switch_uuid, None, None, None)
120 locator_uuid = pending_mac.get(
121 'locator_uuid', None)
122 dst_ip = pending_mac.get(
123 'dst_ip', None)
124 locator = ovsdb_schema.PhysicalLocator(
125 locator_uuid, dst_ip)
126 mac_remote = ovsdb_schema.UcastMacsRemote(
127 pending_mac.get('uuid', None),
128 mac,
129 logical_switch_uuid,
130 locator_uuid,
131 pending_mac['vm_ip'])
132 if operation == 'insert':
133 self.agent_rpc.add_vif_to_gateway(
134 context,
135 ovsdb_identifier,
136 l_switch.__dict__,
137 locator.__dict__,
138 mac_remote.__dict__)
139 else:
140 # update operation
141 self.agent_rpc.update_vif_to_gateway(
142 context,
143 ovsdb_identifier,
144 locator.__dict__,
145 mac_remote.__dict__)
146 else:
147 self.agent_rpc.delete_vif_from_gateway(
148 context, ovsdb_identifier,
149 logical_switch_uuid, [mac])
150
151 # As the pending operation is over, delete the
152 # record from the pending_ucast_mac_remote table
153 db.delete_pending_ucast_mac_remote(
154 context,
155 operation,
156 ovsdb_identifier,
157 logical_switch_uuid,
158 mac)
159 except Exception as ex:
160 LOG.exception("Exception occurred = %s",
161 str(ex))
162
163 def _setup_entry_table(self):
164 self.entry_table = {'new_logical_switches':
165 self._process_new_logical_switches,
166 'new_physical_ports':
167 self._process_new_physical_ports,
168 'new_physical_switches':
169 self._process_new_physical_switches,
170 'new_physical_locators':
171 self._process_new_physical_locators,
172 'new_local_macs':
173 self._process_new_local_macs,
174 'new_remote_macs':
175 self._process_new_remote_macs,
176 'modified_remote_macs':
177 self._process_modified_remote_macs,
178 'modified_physical_ports':
179 self._process_modified_physical_ports,
180 'deleted_logical_switches':
181 self._process_deleted_logical_switches,
182 'deleted_physical_ports':
183 self._process_deleted_physical_ports,
184 'deleted_physical_switches':
185 self._process_deleted_physical_switches,
186 'deleted_physical_locators':
187 self._process_deleted_physical_locators,
188 'deleted_local_macs':
189 self._process_deleted_local_macs,
190 'deleted_remote_macs':
191 self._process_deleted_remote_macs,
192 'modified_physical_switches':
193 self._process_modified_physical_switches,
194 }
195
196 return
197
198 def _process_new_logical_switches(self,
199 context,
200 new_logical_switches):
201 for logical_switch in new_logical_switches:
202 ls_dict = logical_switch
203 ls_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
204 l_switch = db.get_logical_switch(context, ls_dict)
205 if not l_switch:
206 db.add_logical_switch(context, ls_dict)
207
208 def _process_new_physical_switches(self,
209 context,
210 new_physical_switches):
211 for physical_switch in new_physical_switches:
212 ps_dict = physical_switch
213 ps_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
214 if (ps_dict.get('tunnel_ip'))[0] == 'set':
215 ps_dict['tunnel_ip'] = None
216 p_switch = db.get_physical_switch(context, ps_dict)
217 if not p_switch:
218 db.add_physical_switch(context, ps_dict)
219
220 def _process_new_physical_ports(self,
221 context,
222 new_physical_ports):
223 for physical_port in new_physical_ports:
224 pp_dict = physical_port
225 pp_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
226 p_port = db.get_physical_port(context, pp_dict)
227 if not p_port:
228 db.add_physical_port(context, pp_dict)
229 if pp_dict.get('vlan_bindings'):
230 for vlan_binding in pp_dict.get('vlan_bindings'):
231 vlan_binding[
232 n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
233 vlan_binding['port_uuid'] = pp_dict.get('uuid')
234 v_binding = db.get_vlan_binding(context, vlan_binding)
235 if not v_binding:
236 db.add_vlan_binding(context, vlan_binding)
237
238 def _process_new_physical_locators(self,
239 context,
240 new_physical_locators):
241 for physical_locator in new_physical_locators:
242 pl_dict = physical_locator
243 pl_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
244 p_locator = db.get_physical_locator(context, pl_dict)
245 if not p_locator:
246 db.add_physical_locator(context, pl_dict)
247
248 def _process_new_local_macs(self,
249 context,
250 new_local_macs):
251 for local_mac in new_local_macs:
252 lm_dict = local_mac
253 lm_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
254 lm_dict['logical_switch_uuid'] = local_mac.get('logical_switch_id')
255 l_mac = db.get_ucast_mac_local(context, lm_dict)
256 if not l_mac:
257 db.add_ucast_mac_local(context, lm_dict)
258
259 def _process_new_remote_macs(self,
260 context,
261 new_remote_macs):
262 for remote_mac in new_remote_macs:
263 rm_dict = remote_mac
264 rm_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
265 r_mac = db.get_ucast_mac_remote(context, rm_dict)
266 if not r_mac:
267 db.add_ucast_mac_remote(context, rm_dict)
268
269 def _process_modified_remote_macs(self,
270 context,
271 modified_remote_macs):
272 for remote_mac in modified_remote_macs:
273 remote_mac[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
274 db.update_ucast_mac_remote(context, remote_mac)
275
276 def _get_physical_switch_ips(self, context, mac):
277 physical_switch_ips = set()
278 record_dict = {n_const.OVSDB_IDENTIFIER: self.ovsdb_identifier}
279 vlan_bindings = db.get_all_vlan_bindings_by_logical_switch(
280 context, mac)
281 for vlan_binding in vlan_bindings:
282 record_dict['uuid'] = vlan_binding.get('port_uuid')
283 physical_port = db.get_physical_port(context, record_dict)
284 record_dict['uuid'] = physical_port.get('physical_switch_id')
285 physical_switch = db.get_physical_switch(context, record_dict)
286 physical_switch_ips.add(physical_switch.get('tunnel_ip'))
287 return list(physical_switch_ips)
288
289 def _get_agent_by_mac(self, context, mac):
290 host = None
291 mac_addr = mac.get('mac')
292 port = self._get_port_by_mac(context, mac_addr)
293 for port_dict in port:
294 host = port_dict[portbindings.HOST_ID]
295 agent_l2_pop_enabled = self._get_agent_details_by_host(context, host)
296 return agent_l2_pop_enabled
297
298 def _get_port_by_mac(self, context, mac_addr):
299 port = self.core_plugin.get_ports(
300 context, filters={'mac_address': [mac_addr]})
301 return port
302
303 def _get_agent_details_by_host(self, context, host):
304 l2_agent = None
305 agent_l2_pop_enabled = None
306 agents = self.core_plugin.get_agents(
307 context, filters={'host': [host]})
308 for agent in agents:
309 agent_tunnel_type = agent['configurations'].get('tunnel_types', [])
310 agent_l2_pop_enabled = agent['configurations'].get('l2_population',
311 None)
312 if n_const.VXLAN in agent_tunnel_type:
313 l2_agent = agent
314 break
315 if not l2_agent:
316 raise l2gw_exc.L2AgentNotFoundByHost(
317 host=host)
318 return agent_l2_pop_enabled
319
320 def _handle_l2pop(self, context, new_remote_macs):
321 """handle vxlan tunnel creation based on l2pop is enabled or not.
322
323 if l2pop is enabled in L2 agent on a host to which port belongs, then
324 call add_fdb_entries. otherwise, call tunnel_sync.
325 """
326 for mac in new_remote_macs:
327 try:
328 agent_l2_pop_enabled = self._get_agent_by_mac(context, mac)
329 except l2gw_exc.L2AgentNotFoundByHost as e:
330 LOG.debug(e.message)
331 continue
332 physical_switches = self._get_physical_switch_ips(context, mac)
333 for physical_switch in physical_switches:
334 other_fdb_entries = self._get_fdb_entries(
335 context, physical_switch, mac.get('logical_switch_id'))
336 if agent_l2_pop_enabled:
337 self.tunnel_call.trigger_l2pop_sync(context,
338 other_fdb_entries)
339 else:
340 self.tunnel_call.trigger_tunnel_sync(context,
341 physical_switch)
342
343 def _process_modified_physical_ports(self,
344 context,
345 modified_physical_ports):
346 for physical_port in modified_physical_ports:
347 pp_dict = physical_port
348 pp_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
349 modified_port = db.get_physical_port(context, pp_dict)
350 if modified_port:
351 db.update_physical_ports_status(context, pp_dict)
352 port_vlan_bindings = physical_port.get('vlan_bindings')
353 vlan_bindings = db.get_all_vlan_bindings_by_physical_port(
354 context, pp_dict)
355 for vlan_binding in vlan_bindings:
356 db.delete_vlan_binding(context, vlan_binding)
357 for port_vlan_binding in port_vlan_bindings:
358 port_vlan_binding['port_uuid'] = pp_dict['uuid']
359 port_vlan_binding[
360 n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
361 db.add_vlan_binding(context, port_vlan_binding)
362 else:
363 db.add_physical_port(context, pp_dict)
364
365 def _process_modified_physical_switches(self, context,
366 modified_physical_switches):
367 for physical_switch in modified_physical_switches:
368 db.update_physical_switch_status(context, physical_switch)
369
370 def _process_deleted_logical_switches(self,
371 context,
372 deleted_logical_switches):
373 for logical_switch in deleted_logical_switches:
374 ls_dict = logical_switch
375 ls_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
376 db.delete_logical_switch(context, ls_dict)
377
378 def _process_deleted_physical_switches(self,
379 context,
380 deleted_physical_switches):
381 for physical_switch in deleted_physical_switches:
382 ps_dict = physical_switch
383 ps_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
384 db.delete_physical_switch(context, ps_dict)
385 physical_switches = db.get_all_physical_switches_by_ovsdb_id(
386 context, self.ovsdb_identifier)
387 if not physical_switches:
388 logical_switches = db.get_all_logical_switches_by_ovsdb_id(
389 context, self.ovsdb_identifier)
390 if logical_switches:
391 for logical_switch in logical_switches:
392 self.agent_rpc.delete_network(
393 context, self.ovsdb_identifier,
394 logical_switch.get('uuid'))
395
396 def _process_deleted_physical_ports(self,
397 context,
398 deleted_physical_ports):
399 for physical_port in deleted_physical_ports:
400 pp_dict = physical_port
401 pp_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
402 port_name = pp_dict['name']
403 p_port = db.get_physical_port(context, pp_dict)
404 if not p_port:
405 raise l2gw_exc.L2GatewayInterfaceNotFound(
406 interface_id=port_name)
407 p_switch_id = p_port.get('physical_switch_id')
408 switch_dict = {}
409 switch_dict['uuid'] = p_switch_id
410 switch_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
411 switch_db = db.get_physical_switch(context, switch_dict)
412 if not switch_db:
413 raise l2gw_exc.L2GatewayDeviceNotFound(
414 device_id=p_switch_id)
415 switch_name = switch_db.get('name')
416 l2gw_id_list = self.l2gw_mixin._get_l2gw_ids_by_interface_switch(
417 context, port_name, switch_name)
418 if l2gw_id_list:
419 for l2gw_id in l2gw_id_list:
420 self.l2gw_mixin._delete_connection_by_l2gw_id(context,
421 l2gw_id)
422 vlan_bindings = db.get_all_vlan_bindings_by_physical_port(
423 context, pp_dict)
424 ls_set = set()
425 for vlan_binding in vlan_bindings:
426 vlan_binding['logical_switch_id'] = vlan_binding.get(
427 'logical_switch_uuid')
428 if vlan_binding.get('logical_switch_uuid') in ls_set:
429 db.delete_vlan_binding(context, vlan_binding)
430 continue
431 bindings = db.get_all_vlan_bindings_by_logical_switch(
432 context, vlan_binding)
433 if bindings and len(bindings) == 1:
434 self._delete_macs_from_ovsdb(
435 context,
436 vlan_binding.get('logical_switch_uuid'),
437 self.ovsdb_identifier)
438 elif bindings and len(bindings) > 1:
439 flag = True
440 for binding in bindings:
441 if binding[
442 'ovsdb_identifier'] == self.ovsdb_identifier:
443 flag = False
444 break
445 if flag:
446 self._delete_macs_from_ovsdb(
447 context,
448 vlan_binding.get('logical_switch_uuid'),
449 self.ovsdb_identifier)
450 ls_set.add(vlan_binding.get('logical_switch_uuid'))
451 db.delete_vlan_binding(context, vlan_binding)
452 db.delete_physical_port(context, pp_dict)
453
454 def _delete_macs_from_ovsdb(self, context, logical_switch_id,
455 ovsdb_identifier):
456 mac_list = []
457 ls_dict = {'logical_switch_id': logical_switch_id,
458 'ovsdb_identifier': ovsdb_identifier}
459 macs = db.get_all_ucast_mac_remote_by_ls(context, ls_dict)
460 for mac in macs:
461 mac_list.append(mac.get('mac'))
462 self.agent_rpc.delete_vif_from_gateway(
463 context, ovsdb_identifier,
464 logical_switch_id, mac_list)
465
466 def _process_deleted_physical_locators(self,
467 context,
468 deleted_physical_locators):
469 physical_switch_ips = []
470 logical_switch_ids = self._get_logical_switch_ids(context)
471 physical_switches = db.get_all_physical_switches_by_ovsdb_id(
472 context, self.ovsdb_identifier)
473 for physical_switch in physical_switches:
474 physical_switch_ips.append(
475 physical_switch.get('tunnel_ip'))
476 tunneling_ip_dict = self._get_agent_ips(context)
477 for physical_locator in deleted_physical_locators:
478 pl_dict = physical_locator
479 pl_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
480 agent_ip = physical_locator.get('dst_ip')
481 if agent_ip in tunneling_ip_dict.keys():
482 for logical_switch_id in logical_switch_ids:
483 for physical_switch_ip in physical_switch_ips:
484 other_fdb_entries = self._get_fdb_entries(
485 context, physical_switch_ip, logical_switch_id)
486 agent_host = tunneling_ip_dict.get(agent_ip)
487 self.tunnel_call.trigger_l2pop_delete(
488 context, other_fdb_entries, agent_host)
489 else:
490 for logical_switch_id in logical_switch_ids:
491 other_fdb_entries = self._get_fdb_entries(
492 context, agent_ip, logical_switch_id)
493 self.tunnel_call.trigger_l2pop_delete(
494 context, other_fdb_entries)
495 db.delete_physical_locator(context, pl_dict)
496
497 def _process_deleted_local_macs(self,
498 context,
499 deleted_local_macs):
500 for local_mac in deleted_local_macs:
501 lm_dict = local_mac
502 lm_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
503 db.delete_ucast_mac_local(context, lm_dict)
504
505 def _process_deleted_remote_macs(self,
506 context,
507 deleted_remote_macs):
508 for remote_mac in deleted_remote_macs:
509 rm_dict = remote_mac
510 rm_dict[n_const.OVSDB_IDENTIFIER] = self.ovsdb_identifier
511 db.delete_ucast_mac_remote(context, rm_dict)
512
513 def _get_logical_switch_ids(self, context):
514 logical_switch_ids = set()
515 logical_switches = db.get_all_logical_switches_by_ovsdb_id(
516 context, self.ovsdb_identifier)
517 for logical_switch in logical_switches:
518 logical_switch_ids.add(logical_switch.get('uuid'))
519 return list(logical_switch_ids)
520
521 def _get_agent_ips(self, context):
522 agent_ip_dict = {}
523 agents = self.core_plugin.get_agents(
524 context)
525 for agent in agents:
526 conf_dict_tunnel_type = agent['configurations'].get(
527 "tunnel_types", [])
528 if n_const.VXLAN in conf_dict_tunnel_type:
529 tunnel_ip = agent['configurations'].get('tunneling_ip')
530 agent_ip_dict[tunnel_ip] = agent.get('host')
531 return agent_ip_dict
532
533 def _get_fdb_entries(self, context, agent_ip, logical_switch_uuid):
534 ls_dict = {'uuid': logical_switch_uuid,
535 n_const.OVSDB_IDENTIFIER: self.ovsdb_identifier}
536 logical_switch = db.get_logical_switch(context, ls_dict)
537 network_id = logical_switch.get('name')
538 segment_id = logical_switch.get('key')
539 port_fdb_entries = constants.FLOODING_ENTRY
540 other_fdb_entries = {network_id: {'segment_id': segment_id,
541 'network_type': 'vxlan',
542 'ports': {agent_ip:
543 [port_fdb_entries]
544 }}}
545 return other_fdb_entries