Debian Codegraph @

networking_bagpipe/bagpipe_bgp/vpn/ipvpn/__init__.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 abc
16 import itertools
17
18 from networking_bagpipe._i18n import _
19
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 utils
24 from networking_bagpipe.bagpipe_bgp import constants
25 from networking_bagpipe.bagpipe_bgp import engine
26 from networking_bagpipe.bagpipe_bgp.engine import exa
27 from networking_bagpipe.bagpipe_bgp.engine import flowspec
28 from networking_bagpipe.bagpipe_bgp.engine import ipvpn as ipvpn_routes
29 from networking_bagpipe.bagpipe_bgp.vpn import dataplane_drivers as dp_drivers
30 from networking_bagpipe.bagpipe_bgp.vpn import vpn_instance
31
32 DEFAULT_ADDR_PREFIX = '0.0.0.0/0'
33
34
35 class VPNInstanceDataplane(dp_drivers.VPNInstanceDataplane,
36 metaclass=abc.ABCMeta):
37
38 @abc.abstractmethod
39 def add_dataplane_for_traffic_classifier(self, classifier,
40 redirect_to_instance_id):
41 pass
42
43 @abc.abstractmethod
44 def remove_dataplane_for_traffic_classifier(self, classifier):
45 pass
46
47
48 class DummyVPNInstanceDataplane(dp_drivers.DummyVPNInstanceDataplane,
49 VPNInstanceDataplane):
50
51 def add_dataplane_for_traffic_classifier(self, *args, **kwargs):
52 raise Exception(_("not implemented"))
53
54 def remove_dataplane_for_traffic_classifier(self, *args, **kwargs):
55 raise Exception(_("not implemented"))
56
57
58 class DummyDataplaneDriver(dp_drivers.DummyDataplaneDriver):
59 type = constants.IPVPN
60 dataplane_instance_class = DummyVPNInstanceDataplane
61
62
63 class VRF(vpn_instance.VPNInstance, lg.LookingGlassMixin):
64 # component managing a VRF:
65 # - calling a driver to instantiate the dataplane
66 # - registering to receive routes for the needed route targets
67 # - calling the driver to setup/update/remove routes in the dataplane
68 # - cleanup: calling the driver, unregistering for BGP routes
69
70 type = constants.IPVPN
71 afi = exa.AFI.ipv4
72 safi = exa.SAFI.mpls_vpn
73
74 @log_decorator.log
75 def __init__(self, *args, **kwargs):
76 vpn_instance.VPNInstance.__init__(self, *args, **kwargs)
77 self.readvertised = set()
78
79 def needs_cleanup_assist(self, afi, safi):
80 if (afi, safi) == (self.afi, exa.SAFI.flow_vpn):
81 return True
82 else:
83 return self.dataplane.needs_cleanup_assist()
84
85 @classmethod
86 def validate_convert_params(cls, params, also_mandatory=()):
87 return super().validate_convert_params(
88 params,
89 also_mandatory=tuple(set(also_mandatory) | {'ip_address'}))
90
91 @classmethod
92 def validate_convert_attach_params(cls, params):
93 super().validate_convert_attach_params(params)
94 if 'gateway_ip' not in params:
95 raise exc.APIMissingParameterException('gateway_ip')
96
97 def _nlri_from(self, prefix, label, rd):
98 assert rd is not None
99
100 return ipvpn_routes.IPVPNRouteFactory(
101 self.afi, prefix, label, rd,
102 self.dp_driver.get_local_address())
103
104 def generate_vif_bgp_route(self, mac_address, ip_prefix, plen, label, rd):
105 # Generate BGP route and advertise it...
106 nlri = self._nlri_from("{}/{}".format(ip_prefix, plen), label, rd)
107
108 return engine.RouteEntry(nlri)
109
110 def _get_local_labels(self):
111 for port_data in self.mac_2_localport_data.values():
112 yield port_data['label']
113
114 def _get_route_params_for_endpoint(self, endpoint):
115 port_data = self.mac_2_localport_data[endpoint[0]]
116 label = port_data['label']
117 lb_consistent_hash_order = port_data[
118 'lb_consistent_hash_order']
119 rd = self.endpoint_2_rd[endpoint]
120
121 return (label, rd, lb_consistent_hash_order)
122
123 def _imported(self, route):
124 return len(set(route.route_targets).intersection(set(self.import_rts))
125 ) > 0
126
127 def _to_readvertise(self, route):
128 rt_records = route.ecoms(exa.RTRecord)
129 self.log.debug("RTRecords: %s (readvertise_to_rts:%s)",
130 rt_records,
131 self.readvertise_to_rts)
132
133 readvertise_targets_as_records = [exa.RTRecord.from_rt(rt)
134 for rt in self.readvertise_to_rts]
135
136 if self.attract_traffic:
137 readvertise_targets_as_records += [exa.RTRecord.from_rt(rt)
138 for rt in self.attract_rts]
139
140 if set(readvertise_targets_as_records).intersection(set(rt_records)):
141 self.log.debug("not to re-advertise because one of the readvertise"
142 " or attract-redirect RTs is in RTRecords: %s",
143 set(readvertise_targets_as_records)
144 .intersection(set(rt_records)))
145 return False
146
147 return len(set(route.route_targets).intersection(
148 set(self.readvertise_from_rts)
149 )) > 0
150
151 def _gen_rtrecords_extended_community(self, ecoms):
152 # new RTRecord = original RTRecord (if any) + orig RTs converted into
153 # RTRecord attributes
154 orig_rtrecords = ecoms(exa.RTRecord)
155 rts = ecoms(exa.RTExtCom)
156 add_rtrecords = [exa.RTRecord.from_rt(rt) for rt in rts]
157
158 return list(set(orig_rtrecords) | set(add_rtrecords))
159
160 def _route_for_readvertisement(self, route, endpoint):
161 label, rd, lb_consistent_hash_order = (
162 self._get_route_params_for_endpoint(endpoint)
163 )
164 self.log.debug("Prefix %s (re-)advertisement with label %s and route "
165 "distinguisher %s", route.nlri.cidr.prefix(), label, rd)
166 nlri = self._nlri_from(route.nlri.cidr.prefix(), label, rd)
167
168 attributes = exa.Attributes()
169
170 ecoms = self._gen_encap_extended_communities()
171 ecoms.communities += (
172 self._gen_rtrecords_extended_community(route.ecoms)
173 )
174 ecoms.communities.append(
175 exa.ConsistentHashSortOrder(lb_consistent_hash_order))
176 attributes.add(ecoms)
177
178 entry = engine.RouteEntry(nlri, self.readvertise_to_rts, attributes)
179 self.log.debug("RouteEntry for (re-)advertisement: %s", entry)
180 return entry
181
182 def _default_route_for_advertisement(self, endpoint):
183 label, rd, lb_consistent_hash_order = (
184 self._get_route_params_for_endpoint(endpoint)
185 )
186 self.log.debug("Default route (re-)advertisement with label %s and "
187 "route distinguisher %s", label, rd)
188 nlri = self._nlri_from(DEFAULT_ADDR_PREFIX, label, rd)
189
190 attributes = exa.Attributes()
191
192 ecoms = self._gen_encap_extended_communities()
193 ecoms.communities.append(
194 exa.ConsistentHashSortOrder(lb_consistent_hash_order))
195 attributes.add(ecoms)
196
197 entry = engine.RouteEntry(nlri, self.readvertise_to_rts, attributes)
198 self.log.debug("RouteEntry for default prefix advertisement: %s",
199 entry)
200 return entry
201
202 @log_decorator.log
203 def _routes_for_attract_static_dest_prefixes(self, endpoint):
204 if not self.attract_static_dest_prefixes:
205 return
206
207 label, rd, _ = self._get_route_params_for_endpoint(endpoint)
208
209 for prefix in self.attract_static_dest_prefixes:
210 nlri = self._nlri_from(prefix, label, rd)
211
212 entry = engine.RouteEntry(nlri, self.readvertise_to_rts)
213 self.log.debug("RouteEntry for attract static destination prefix: "
214 "%s", entry)
215 yield entry
216
217 @log_decorator.log
218 def _route_for_redirect_prefix(self, prefix):
219 prefix_classifier = utils.dict_camelcase_to_underscore(
220 self.attract_classifier)
221 prefix_classifier['destination_prefix'] = prefix
222
223 traffic_classifier = vpn_instance.TrafficClassifier(
224 **prefix_classifier)
225 self.log.debug("Advertising prefix %s for redirection based on "
226 "traffic classifier %s", prefix, traffic_classifier)
227 rules = traffic_classifier.map_traffic_classifier_2_redirect_rules()
228
229 return self.synthesize_redirect_bgp_route(rules)
230
231 def _redirect_route_for_readvertisement(self, route):
232 # Create a FlowSpec NLRI with distinct RD and a copy of rules from
233 # FlowSpec route to readvertise
234 nlri = flowspec.FlowRouteFactory(self.afi, self.instance_rd)
235 nlri.rules = route.nlri.rules
236
237 attributes = exa.Attributes()
238
239 ecoms = exa.ExtendedCommunities()
240 ecoms.communities += (
241 self._gen_rtrecords_extended_community(route.ecoms)
242 )
243 assert len(self.attract_rts) == 1
244 rt = self.attract_rts[0]
245 ecoms.communities.append(
246 exa.TrafficRedirect(exa.ASN(int(rt.asn)), int(rt.number))
247 )
248 attributes.add(ecoms)
249
250 entry = engine.RouteEntry(nlri, self.readvertise_to_rts, attributes)
251 self.log.debug("RouteEntry for redirect (re-)advertisement: %s", entry)
252 return entry
253
254 @log_decorator.log
255 def _readvertise(self, route):
256 nlri = route.nlri
257
258 self.log.debug("Start re-advertising %s from VRF", nlri)
259 if self.attract_traffic:
260 # Start advertising default route only when the first route to
261 # readvertise appears
262 if not self.readvertised:
263 for endpoint in self.all_endpoints():
264 self.log.debug("Start advertising default route from VRF "
265 "%d to redirection VRF", self.instance_id)
266 self._advertise_route(
267 self._default_route_for_advertisement(endpoint)
268 )
269
270 if isinstance(nlri, flowspec.Flow):
271 # Readvertise FlowSpec route
272 self._advertise_route(
273 self._redirect_route_for_readvertisement(route)
274 )
275 else:
276 # Advertise FlowSpec route for prefix
277 self._advertise_route(
278 self._route_for_redirect_prefix(nlri.cidr.prefix())
279 )
280
281 else:
282 for endpoint in self.all_endpoints():
283 self.log.debug("Start re-advertising %s from endpoint %s",
284 nlri.cidr.prefix(), endpoint)
285 self._advertise_route(
286 self._route_for_readvertisement(route, endpoint)
287 )
288
289 self.readvertised.add(route)
290
291 @log_decorator.log
292 def _readvertise_stop(self, route, last):
293 nlri = route.nlri
294
295 if last:
296 self.log.debug("Stop re-advertising %s from VRF", nlri)
297 if self.attract_traffic:
298 # Stop advertising default route only if the withdrawn route is
299 # the last of the routes to readvertise
300 if len(self.readvertised) == 1:
301 for endpoint in self.all_endpoints():
302 self.log.debug("Stop advertising default route from "
303 "VRF to redirection VRF")
304 self._withdraw_route(
305 self._default_route_for_advertisement(endpoint)
306 )
307
308 if isinstance(nlri, flowspec.Flow):
309 # Withdraw readvertised FlowSpec route
310 self._withdraw_route(
311 self._redirect_route_for_readvertisement(route)
312 )
313 else:
314 # Withdraw FlowSpec route for prefix
315 self._withdraw_route(
316 self._route_for_redirect_prefix(nlri.cidr.prefix())
317 )
318 else:
319 for endpoint in self.all_endpoints():
320 self.log.debug("Stop re-advertising %s from endpoint %s",
321 nlri.cidr.prefix(), endpoint)
322 route_entry = (
323 self._route_for_readvertisement(route, endpoint)
324 )
325 self._withdraw_route(route_entry)
326
327 self.readvertised.remove(route)
328
329 @log_decorator.log_info
330 def vif_plugged(self, mac_address, ip_address_prefix, localport,
331 advertise_subnet=False, lb_consistent_hash_order=0,
332 local_pref=None, **kwargs):
333 super().vif_plugged(mac_address, ip_address_prefix,
334 localport, advertise_subnet,
335 lb_consistent_hash_order, local_pref,
336 **kwargs)
337
338 if vpn_instance.forward_to_port(kwargs.get('direction')):
339 endpoint = (mac_address, ip_address_prefix)
340 for route in itertools.chain(
341 self.readvertised,
342 self._routes_for_attract_static_dest_prefixes(endpoint)):
343 self.log.debug("Re-advertising %s with this port as next hop",
344 route.nlri)
345 if self.attract_traffic:
346 self._advertise_route(
347 self._default_route_for_advertisement(endpoint)
348 )
349
350 if self.has_only_one_endpoint():
351 self._advertise_route(self._route_for_redirect_prefix(
352 route.nlri.cidr.prefix()))
353 else:
354 self._advertise_route(
355 self._route_for_readvertisement(route, endpoint)
356 )
357
358 @log_decorator.log_info
359 def vif_unplugged(self, mac_address, ip_address_prefix):
360 endpoint = (mac_address, ip_address_prefix)
361 direction = self.endpoint_2_direction[endpoint]
362 if vpn_instance.forward_to_port(direction):
363 for route in itertools.chain(
364 self.readvertised,
365 self._routes_for_attract_static_dest_prefixes(endpoint)):
366 self.log.debug("Stop re-advertising %s", route.nlri)
367 if self.attract_traffic:
368 self._withdraw_route(
369 self._default_route_for_advertisement(endpoint)
370 )
371
372 if self.has_only_one_endpoint():
373 self._withdraw_route(self._route_for_redirect_prefix(
374 route.nlri.cidr.prefix()))
375 else:
376 self._withdraw_route(
377 self._route_for_readvertisement(route, endpoint)
378 )
379
380 super().vif_unplugged(mac_address, ip_address_prefix)
381
382 # Callbacks for BGP route updates (TrackerWorker) ########################
383
384 def route_to_tracked_entry(self, route):
385 if isinstance(route.nlri, ipvpn_routes.IPVPN):
386 return route.nlri.cidr.prefix()
387 elif isinstance(route.nlri, flowspec.Flow):
388 return (flowspec.Flow, route.nlri._rules())
389 else:
390 self.log.error("We should not receive routes of type %s",
391 type(route.nlri))
392 return None
393
394 @utils.synchronized
395 @log_decorator.log
396 def new_best_route(self, entry, new_route):
397
398 if self.readvertise:
399 # check if this is a route we need to re-advertise
400 self.log.debug("route RTs: %s", new_route.route_targets)
401 self.log.debug("readv from RTs: %s", self.readvertise_from_rts)
402 if self._to_readvertise(new_route):
403 self.log.debug("Need to re-advertise %s", entry)
404 self._readvertise(new_route)
405
406 if not self._imported(new_route):
407 self.log.debug("No need to setup dataplane for:%s", entry)
408 return
409
410 if isinstance(new_route.nlri, flowspec.Flow):
411 if len(new_route.ecoms(exa.TrafficRedirect)) == 1:
412 traffic_redirect = new_route.ecoms(exa.TrafficRedirect)
413 redirect_rt = "{}:{}".format(traffic_redirect[0].asn,
414 traffic_redirect[0].target)
415
416 self.start_redirect_traffic(redirect_rt,
417 new_route.nlri.rules)
418 else:
419 self.log.warning("FlowSpec action or multiple traffic "
420 "redirect actions not supported: %s",
421 new_route.ecoms())
422 else:
423 prefix = entry
424
425 encaps = self._check_encaps(new_route)
426 if not encaps:
427 return
428
429 assert len(new_route.nlri.labels.labels) == 1
430
431 lb_consistent_hash_order = 0
432 if new_route.ecoms(exa.ConsistentHashSortOrder):
433 lb_consistent_hash_order = new_route.ecoms(
434 exa.ConsistentHashSortOrder)[0].order
435
436 self.dataplane.setup_dataplane_for_remote_endpoint(
437 prefix, new_route.nexthop,
438 new_route.nlri.labels.labels[0], new_route.nlri, encaps,
439 lb_consistent_hash_order)
440
441 @utils.synchronized
442 @log_decorator.log
443 def best_route_removed(self, entry, old_route, last):
444
445 if self.readvertise:
446 # check if this is a route we were re-advertising
447 if self._to_readvertise(old_route):
448 self.log.debug("Need to stop re-advertising %s", entry)
449 self._readvertise_stop(old_route, last)
450
451 if isinstance(old_route.nlri, flowspec.Flow):
452 if self._imported(old_route):
453 if len(old_route.ecoms(exa.TrafficRedirect)) == 1:
454 if last:
455 traffic_redirect = old_route.ecoms(
456 exa.TrafficRedirect)
457 redirect_rt = "{}:{}".format(
458 traffic_redirect[0].asn,
459 traffic_redirect[0].target)
460
461 self.stop_redirect_traffic(redirect_rt,
462 old_route.nlri.rules)
463 else:
464 self.log.warning("FlowSpec action or multiple traffic "
465 "redirect actions not supported: %s",
466 old_route.ecoms())
467 else:
468 prefix = entry
469
470 # NOTE(tmorin): On new best routes, we only trigger dataplane
471 # update events after checking with self._imported(...) that the
472 # route was imported (and not a route that we receive because the
473 # VRF should readvertise ir). On best_route_removed, we can't do
474 # that because we could end up in a situation where:
475 # - initially import_rts contains RT X
476 # - we receive a route for RT X and install dataplane state
477 # - the import_rts list is later updated and RT X is not anymore
478 # part of the imported RTs, and the VRF unsubscribes from RT X
479 # - we receive the best_route_removed callbacks corresponding to
480 # the unsubscribe, but since the route is for no RT that is in
481 # import_rts, we don't update the dataplane
482 # The result would be to fail to remove dataplane state for this
483 # route, so we're better not optimizing this case and remove
484 # dataplane state, including possibly for routes that we did
485 # not install in it.
486
487 if self._skip_route_removal(last):
488 self.log.debug("Skipping removal of non-last route because "
489 "dataplane does not want it")
490 return
491
492 # if we still have a route with same dataplane properties in
493 # best routes, then we don't want to clear the dataplane entry
494 if self.equivalent_route_in_best_routes(
495 old_route,
496 lambda r: (r.nexthop, r.nlri.labels.labels[0])):
497 self.log.debug("Route for same dataplane is still in best "
498 "routes, skipping removal")
499 return
500
501 encaps = self._check_encaps(old_route)
502 if not encaps:
503 return
504
505 assert len(old_route.nlri.labels.labels) == 1
506
507 lb_consistent_hash_order = 0
508 if old_route.ecoms(exa.ConsistentHashSortOrder):
509 lb_consistent_hash_order = old_route.ecoms(
510 exa.ConsistentHashSortOrder)[0].order
511
512 self.dataplane.remove_dataplane_for_remote_endpoint(
513 prefix, old_route.nexthop,
514 old_route.nlri.labels.labels[0], old_route.nlri, encaps,
515 lb_consistent_hash_order)
516
517 # Looking glass ###
518
519 def get_lg_map(self):
520 return {
521 "readvertised": (lg.SUBTREE, self.get_lg_readvertised_routes),
522 }
523
524 def get_lg_readvertised_routes(self, path_prefix):
525 return [route.get_lg_local_info(path_prefix)
526 for route in self.readvertised]