Debian Codegraph @

networking_bagpipe/bagpipe_bgp/vpn/manager.py

raw
1 # Copyright 2014 Orange
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 implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import threading
16
17 from oslo_log import log as logging
18
19 from networking_bagpipe._i18n import _
20 from networking_bagpipe.bagpipe_bgp.common import exceptions as exc
21 from networking_bagpipe.bagpipe_bgp.common import log_decorator
22 from networking_bagpipe.bagpipe_bgp.common import looking_glass as lg
23 from networking_bagpipe.bagpipe_bgp.common import run_command
24 from networking_bagpipe.bagpipe_bgp.common import utils
25 from networking_bagpipe.bagpipe_bgp import constants
26 from networking_bagpipe.bagpipe_bgp.engine import bgp_manager
27 from networking_bagpipe.bagpipe_bgp.vpn import dataplane_drivers as dp_drivers
28 from networking_bagpipe.bagpipe_bgp.vpn import evpn
29 from networking_bagpipe.bagpipe_bgp.vpn import identifier_allocators
30 from networking_bagpipe.bagpipe_bgp.vpn import ipvpn
31
32 from neutron_lib import exceptions
33
34
35 LOG = logging.getLogger(__name__)
36
37 INSTANCE_ID_MAX = 2 ** 32 - 1
38
39
40 def redirect_instance_extid(instance_type, rt):
41 '''generate the ext intance id of a redirection VPN instance'''
42 return "redirect-to-{}-{}".format(instance_type, rt.replace(":", "_"))
43
44
45 class MaxInstanceIDReached(exceptions.NeutronException):
46 _message = "Could not create VPN instance: max instance id was reached"
47
48
49 class VPNManager(lg.LookingGlassMixin, utils.ClassReprMixin):
50 """VPN Manager
51
52 Creates, and keeps track of, VPN instances (VRFs and EVIs) and passes
53 plug/unplug calls to the right VPN instance.
54 """
55
56 _instance = None
57
58 type2class = {constants.IPVPN: ipvpn.VRF,
59 constants.EVPN: evpn.EVI
60 }
61
62 @log_decorator.log
63 def __init__(self):
64 LOG.debug("Instantiating VPN Manager...")
65
66 self.bgp_manager = bgp_manager.Manager()
67
68 self.dataplane_drivers = dp_drivers.instantiate_dataplane_drivers()
69
70 # VPN instance dict
71 self.vpn_instances = {}
72 self.next_vpn_instance_id = 1
73
74 LOG.debug("Creating label allocator")
75 self.label_allocator = identifier_allocators.LabelAllocator()
76
77 LOG.debug("Creating route distinguisher allocator")
78 self.rd_allocator = identifier_allocators.RDAllocator(
79 self.bgp_manager.get_local_address())
80
81 # dict containing info how an ipvpn is plugged
82 # from an evpn (keys: ipvpn instances)
83 self._evpn_ipvpn_ifs = {}
84
85 # keys: vni
86 # value: VPNInstance
87 self.vpn_instance_by_vni = {}
88
89 self.lock = threading.RLock()
90
91 def _run_command(self, *args, **kwargs):
92 run_command.run_command(LOG, *args, run_as_root=True, **kwargs)
93
94 @log_decorator.log_info
95 def _attach_evpn_2_ipvpn(self, localport, ipvpn_instance):
96 # Assuming localport indicates no real interface but only
97 # an EVPN, this method will create a pair of twin interfaces, one
98 # to plug in the EVPN, the other to plug in the IPVPN.
99 #
100 # The localport dict will be modified so that the 'linuxif' indicates
101 # the name of the interface to plug in the IPVPN.
102 #
103 # The EVPN instance will be notified so that it forwards traffic
104 # destinated to the gateway on the interface toward the IPVPN.
105
106 assert 'evpn' in localport
107
108 if 'id' not in localport['evpn']:
109 raise Exception(_("Missing parameter 'id' :an external EVPN "
110 "instance id must be specified for an EVPN "
111 "attachment"))
112
113 try:
114 evpn = self.vpn_instances[localport['evpn']['id']]
115 except Exception:
116 raise Exception(_("The specified evpn instance does not exist (%s)"
117 % localport['evpn']))
118
119 if evpn.type != constants.EVPN:
120 raise Exception(_("The specified instance to plug is not an evpn"
121 "instance (is %s instead)" % evpn.type))
122
123 if ipvpn_instance in self._evpn_ipvpn_ifs:
124 (evpn_if, ipvpn_if, evpn, managed) = \
125 self._evpn_ipvpn_ifs[ipvpn_instance]
126
127 if localport['evpn']['id'] != evpn.external_instance_id:
128 raise Exception(_("Trying to plug into an IPVPN a new E-VPN "
129 "while one is already plugged in"))
130 else:
131 # do nothing
132 LOG.warning('Trying to plug an E-VPN into an IPVPN, but it was'
133 ' already done')
134 localport['linuxif'] = ipvpn_if
135 return
136
137 # detect if this evpn is already plugged into an IPVPN
138 if evpn.has_gateway_port():
139 raise Exception(_("Trying to plug E-VPN into an IPVPN, but this "
140 "EVPN is already plugged into an IPVPN"))
141
142 if 'linuxif' in localport and localport['linuxif']:
143 raise Exception(_("Cannot specify an attachment with both a "
144 "linuxif and an evpn"))
145
146 if 'ovs_port_name' in localport['evpn']:
147 try:
148 assert localport['ovs']['plugged']
149 assert (localport['ovs']['port_name'] or
150 localport['ovs']['port_number'])
151 except Exception:
152 raise Exception(_("Using ovs_port_name in EVPN/IPVPN "
153 "attachment requires specifying the "
154 "corresponding OVS port, which must "
155 "also be pre-plugged"))
156
157 evpn_if = localport['evpn']['ovs_port_name']
158
159 # we assume in this case that the E-VPN interface is already
160 # plugged into the E-VPN bridge
161 managed = False
162 else:
163 evpn_if = "evpn%d-ipvpn%d" % (
164 evpn.instance_id, ipvpn_instance.instance_id)
165 ipvpn_if = "ipvpn%d-evpn%d" % (
166 ipvpn_instance.instance_id, evpn.instance_id)
167
168 # FIXME: do it only if not existing already...
169 LOG.info("Creating veth pair %s %s ", evpn_if, ipvpn_if)
170
171 # delete the interfaces if they exist already
172 self._run_command("ip link delete %s" % evpn_if,
173 acceptable_return_codes=[0, 1])
174 self._run_command("ip link delete %s" % ipvpn_if,
175 acceptable_return_codes=[0, 1])
176
177 self._run_command("ip link add %s type veth peer name %s"
178 " mtu 65535" % (evpn_if, ipvpn_if))
179
180 self._run_command("ip link set %s up" % evpn_if)
181 self._run_command("ip link set %s up" % ipvpn_if)
182 managed = True
183
184 localport['linuxif'] = ipvpn_if
185
186 evpn.set_gateway_port(evpn_if, ipvpn_instance)
187
188 self._evpn_ipvpn_ifs[ipvpn_instance] = (
189 evpn_if, ipvpn_if, evpn, managed)
190
191 @log_decorator.log_info
192 def _detach_evpn_2_ipvpn(self, ipvpn):
193 # Symmetric to _attach_evpn_2_ipvpn
194
195 (evpn_if, ipvpn_if, evpn_instance,
196 managed) = self._evpn_ipvpn_ifs[ipvpn]
197
198 if not ipvpn.has_enpoint(ipvpn_if):
199 # TODO(tmorin): check that this evpn instance is still up and
200 # running ?
201 evpn_instance.gateway_port_down(evpn_if)
202
203 # cleanup veth pair
204 if managed:
205 self._run_command("ip link delete %s" % evpn_if)
206
207 del self._evpn_ipvpn_ifs[ipvpn]
208
209 def _cleanup_evpn2ipvpn(self, ipvpn):
210 (_, ipvpn_if, _, managed) = self._evpn_ipvpn_ifs[ipvpn]
211
212 # cleanup veth pair
213 if managed:
214 self._run_command("ovs-vsctl del-port %s" % ipvpn_if)
215 self._run_command("ip link delete %s" % ipvpn_if)
216
217 @utils.synchronized
218 @log_decorator.log_info
219 def _get_vpn_instance(self, external_instance_id, instance_type,
220 import_rts, export_rts, gateway_ip, mask,
221 readvertise, attract_traffic, fallback=None,
222 **kwargs):
223 # Get an vpn_instance with this external_instance_id,
224 # if one already exists, check matching instance_type
225 # else create one with provided parameters and start it
226 # (unless create_if_none is False --> raise exc.VPNNotFound)
227 # returns True if an already started instance was found
228 # False if a new instance was created without starting it
229
230 LOG.info("Finding %s for external vpn_instance identifier %s",
231 instance_type, external_instance_id)
232
233 vpn_instance = self.vpn_instances.get(external_instance_id)
234
235 if vpn_instance:
236 if vpn_instance.type != instance_type:
237 raise Exception(_("Found an existing vpn_instance with "
238 "external id %s but a different type "
239 "(asked %s vs. already having %s)"
240 % (external_instance_id,
241 instance_type, vpn_instance.type)))
242 return vpn_instance, True
243
244 if not kwargs.pop('create_if_none', True):
245 raise exc.VPNNotFound(external_instance_id)
246
247 # if a vni is specified, check that no VPN instance with same VNI
248 # already exists...
249 if 'vni' in kwargs and kwargs['vni'] in self.vpn_instance_by_vni:
250 raise exc.APIAlreadyUsedVNI(kwargs['vni'])
251
252 vpn_instance_class = VPNManager.type2class[instance_type]
253 dataplane_driver = self.dataplane_drivers[instance_type]
254
255 # unique internal vpn instance id
256 instance_id = self.next_vpn_instance_id
257 if instance_id > INSTANCE_ID_MAX:
258 raise MaxInstanceIDReached()
259
260 self.next_vpn_instance_id += 1
261
262 vpn_instance = vpn_instance_class(self, dataplane_driver,
263 external_instance_id, instance_id,
264 import_rts, export_rts,
265 gateway_ip, mask,
266 readvertise, attract_traffic,
267 fallback, **kwargs)
268
269 self.register_vpn_instance(vpn_instance)
270
271 return vpn_instance, False
272
273 @utils.synchronized
274 @log_decorator.log_info
275 def register_vpn_instance(self, vpn_instance):
276 self.vpn_instances[vpn_instance.external_instance_id] = vpn_instance
277 if vpn_instance.forced_vni:
278 self.vpn_instance_by_vni[
279 vpn_instance.instance_label] = vpn_instance
280
281 @utils.synchronized
282 @log_decorator.log_info
283 def unregister_vpn_instance(self, vpn_instance):
284 del self.vpn_instances[vpn_instance.external_instance_id]
285 if vpn_instance.forced_vni:
286 del self.vpn_instance_by_vni[vpn_instance.instance_label]
287
288 def _check_instance_type(self, params):
289 if 'vpn_type' not in params:
290 raise exc.APIException("missing instance_type")
291
292 instance_type = params['vpn_type']
293 if instance_type not in self.type2class:
294 raise exc.APIException("unknown vpn_type: %s" % instance_type)
295
296 if instance_type not in self.dataplane_drivers:
297 LOG.error("No dataplane driver for VPN type %s", instance_type)
298 raise exc.APIException("No dataplane driver for VPN type %s" %
299 instance_type)
300
301 return instance_type
302
303 @log_decorator.log_info
304 def plug_vif_to_vpn(self, **params):
305
306 instance_type = self._check_instance_type(params)
307
308 vpn_instance_class = VPNManager.type2class[instance_type]
309 vpn_instance_class.validate_convert_attach_params(params)
310
311 external_instance_id = params.get('external_instance_id')
312 import_rts = params.get('import_rts')
313 export_rts = params.get('export_rts')
314 mac_address = params.get('mac_address')
315 gateway_ip = params.get('gateway_ip')
316 localport = params.get('localport')
317 linuxbr = params.get('linuxbr')
318 advertise_subnet = params.get('advertise_subnet')
319 readvertise = params.get('readvertise')
320 attract_traffic = params.get('attract_traffic')
321 lb_consistent_hash_order = params.get('lb_consistent_hash_order')
322 local_pref = params.get('local_pref')
323 fallback = params.get('fallback')
324 vni = params.get('vni')
325
326 ip_address_prefix = params.get('ip_address_prefix')
327 ip_address_plen = params.get('ip_address_plen')
328
329 # Convert route target string to RouteTarget dictionary
330 import_rts = utils.convert_route_targets(import_rts)
331 export_rts = utils.convert_route_targets(export_rts)
332
333 if readvertise:
334 try:
335 readvertise = {k: utils.convert_route_targets(readvertise[k])
336 for k in ['from_rt', 'to_rt']}
337 except KeyError as e:
338 raise Exception(_("Wrong 'readvertise' parameters: %s" % e))
339
340 if attract_traffic:
341 try:
342 attract_traffic['redirect_rts'] = (
343 utils.convert_route_targets(
344 attract_traffic['redirect_rts'])
345 )
346 except KeyError as e:
347 raise Exception(_("Wrong 'attract_traffic' parameters: %s" %
348 e))
349
350 kwargs = {}
351 if vni:
352 kwargs['vni'] = vni
353 if instance_type == constants.EVPN and linuxbr:
354 kwargs['linuxbr'] = linuxbr
355
356 vpn_instance, started = self._get_vpn_instance(
357 external_instance_id, instance_type, import_rts, export_rts,
358 gateway_ip, ip_address_plen, readvertise, attract_traffic,
359 fallback, **kwargs)
360
361 vpn_instance.description = params.get('instance_description')
362
363 vpn_instance.update_route_targets(import_rts, export_rts)
364 vpn_instance.update_fallback(fallback)
365
366 if instance_type == constants.IPVPN and 'evpn' in localport:
367 # special processing for the case where what we plug into
368 # the ipvpn is not an existing interface but an interface
369 # to create, connected to an existing evpn instance
370 self._attach_evpn_2_ipvpn(localport, vpn_instance)
371
372 plug_kwargs = {}
373 plug_kwargs['description'] = params.get('description')
374 plug_kwargs['direction'] = params.get('direction')
375
376 # Plug VIF to VPN instance
377 vpn_instance.vif_plugged(mac_address, ip_address_prefix, localport,
378 advertise_subnet, lb_consistent_hash_order,
379 local_pref, **plug_kwargs)
380
381 # delaying the start after the first vif_plugged allows to handle
382 # dataplane driver for which the first vif_plugged needs to happen
383 # before route advertisements can be processed
384 if not started:
385 vpn_instance.start()
386
387 @log_decorator.log_info
388 def unplug_vif_from_vpn(self, **params):
389
390 instance_type = self._check_instance_type(params)
391
392 vpn_instance_class = VPNManager.type2class[instance_type]
393 vpn_instance_class.validate_convert_detach_params(params)
394
395 external_instance_id = params.get('external_instance_id')
396 mac_address = params.get('mac_address')
397 localport = params.get('localport')
398 ip_address_prefix = params.get('ip_address_prefix')
399
400 # Retrieve VPN instance or raise exception if does not exist
401 try:
402 vpn_instance = self.vpn_instances[external_instance_id]
403 except KeyError:
404 LOG.error("Try to unplug VIF from non existing VPN instance %s",
405 external_instance_id)
406 raise exc.VPNNotFound(external_instance_id)
407
408 # Unplug VIF from VPN instance
409 vpn_instance.vif_unplugged(mac_address, ip_address_prefix)
410
411 if vpn_instance.type == constants.IPVPN and 'evpn' in localport:
412 self._detach_evpn_2_ipvpn(vpn_instance)
413
414 if vpn_instance.stop_if_empty():
415 self.unregister_vpn_instance(vpn_instance)
416
417 def redirect_instance_for_rt(self, redirected_type, redirect_rt,
418 stop=False):
419 external_instance_id = redirect_instance_extid(redirected_type,
420 redirect_rt)
421 LOG.info("Need VPN instance %s for traffic redirection to RT %s",
422 external_instance_id, redirect_rt)
423
424 # Convert route target string to RouteTarget dictionary
425 import_rts = utils.convert_route_targets([redirect_rt])
426
427 # Retrieve a redirect VPN instance or create a new one if none exists
428 # yet
429 try:
430 i, s = self._get_vpn_instance(external_instance_id,
431 redirected_type,
432 import_rts, [],
433 "127.0.0.1",
434 "24", None, None,
435 create_if_none=(not stop))
436 if not s:
437 i.start()
438 return i
439 except exc.VPNNotFound:
440 # (reached only in the 'stop' case)
441 LOG.error("Try to stop traffic redirection for an RT for which"
442 " no VPN instance exists (%s)", external_instance_id)
443 raise
444
445 @log_decorator.log_info
446 def redirect_traffic_to_vpn(self, redirected_id,
447 redirected_type, redirect_rt):
448 redirect_instance = self.redirect_instance_for_rt(redirected_type,
449 redirect_rt)
450 redirect_instance.register_redirected_instance(redirected_id)
451 return redirect_instance
452
453 @log_decorator.log_info
454 def stop_redirect_to_vpn(self, redirected_id,
455 redirected_type, redirect_rt):
456 redirect_instance = self.redirect_instance_for_rt(redirected_type,
457 redirect_rt,
458 stop=True)
459 redirect_instance.unregister_redirected_instance(redirected_id)
460 if redirect_instance.stop_if_no_redirected_instance():
461 self.unregister_vpn_instance(redirect_instance)
462
463 @log_decorator.log_info
464 def stop(self):
465 self.bgp_manager.stop()
466 for vpn_instance in self.vpn_instances.values():
467 vpn_instance.stop()
468 # Cleanup veth pair
469 if (vpn_instance.type == constants.IPVPN and
470 self._evpn_ipvpn_ifs.get(vpn_instance)):
471 self._cleanup_evpn2ipvpn(vpn_instance)
472 for vpn_instance in self.vpn_instances.values():
473 vpn_instance.join()
474 self.vpn_instances.clear()
475
476 @classmethod
477 @utils.oslo_synchronized('VPNManager')
478 def _create_instance(cls):
479 if not cls.has_instance():
480 cls._instance = cls()
481
482 @classmethod
483 def has_instance(cls):
484 return cls._instance is not None
485
486 @classmethod
487 def clear_instance(cls):
488 cls._instance = None
489
490 @classmethod
491 def get_instance(cls):
492 # double checked locking
493 if not cls.has_instance():
494 cls._create_instance()
495 return cls._instance
496
497 # Looking Glass hooks ####
498
499 def get_lg_map(self):
500 class DataplaneLGHook(lg.LookingGlassMixin):
501
502 def __init__(self, vpn_manager):
503 self.manager = vpn_manager
504
505 def get_lg_map(self):
506 return {
507 "drivers": (lg.COLLECTION, (
508 self.manager.get_lg_dataplanes_list,
509 self.manager.get_lg_dataplane_from_path_item)),
510 "ids": (lg.DELEGATE, self.manager.label_allocator)
511 }
512 dataplane_hook = DataplaneLGHook(self)
513 return {
514 "instances": (lg.COLLECTION, (self.get_lg_vpn_list,
515 self.get_lg_vpn_from_path_item)),
516 "dataplane": (lg.DELEGATE, dataplane_hook),
517 "instances_per_vni": (lg.SUBITEM, self.get_lg_instances_per_vni)
518 }
519
520 def get_lg_vpn_list(self):
521 return [instance.get_lg_summary()
522 for instance in list(self.vpn_instances.values())]
523
524 def get_lg_vpn_from_path_item(self, path_item):
525 return self.vpn_instances[path_item]
526
527 def get_vpn_instances_count(self):
528 return len(self.vpn_instances)
529
530 def get_lg_instances_per_vni(self):
531 return {vni: {'name': str(instance),
532 'external_instance_id': instance.external_instance_id
533 }
534 for vni, instance in self.vpn_instance_by_vni.items()}
535
536 def get_lg_dataplanes_list(self):
537 return [{"id": i} for i in self.dataplane_drivers.keys()]
538
539 def get_lg_dataplane_from_path_item(self, path_item):
540 return self.dataplane_drivers[path_item]