| 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 collections |
| 17 |
import copy |
| 18 |
import itertools |
| 19 |
import re |
| 20 |
import socket |
| 21 |
import threading |
| 22 |
|
| 23 |
import netaddr |
| 24 |
from oslo_log import log as logging |
| 25 |
|
| 26 |
from networking_bagpipe._i18n import _ |
| 27 |
|
| 28 |
from networking_bagpipe.bagpipe_bgp.common import exceptions as exc |
| 29 |
from networking_bagpipe.bagpipe_bgp.common import log_decorator |
| 30 |
from networking_bagpipe.bagpipe_bgp.common import looking_glass as lg |
| 31 |
from networking_bagpipe.bagpipe_bgp.common import utils |
| 32 |
from networking_bagpipe.bagpipe_bgp import constants |
| 33 |
from networking_bagpipe.bagpipe_bgp import engine |
| 34 |
from networking_bagpipe.bagpipe_bgp.engine import exa |
| 35 |
from networking_bagpipe.bagpipe_bgp.engine import flowspec |
| 36 |
from networking_bagpipe.bagpipe_bgp.engine import tracker_worker |
| 37 |
|
| 38 |
LOG = logging.getLogger(__name__) |
| 39 |
|
| 40 |
DEFAULT_LOCAL_PREF = 100 |
| 41 |
|
| 42 |
|
| 43 |
def forward_to_port(direction): |
| 44 |
return direction in (None, constants.BOTH, constants.TO_PORT) |
| 45 |
|
| 46 |
|
| 47 |
def forward_from_port(direction): |
| 48 |
return direction in (None, constants.BOTH, constants.FROM_PORT) |
| 49 |
|
| 50 |
|
| 51 |
class TrafficClassifier: |
| 52 |
|
| 53 |
def __init__(self, source_prefix=None, destination_prefix=None, |
| 54 |
source_port=None, destination_port=None, protocol='tcp'): |
| 55 |
self.source_pfx = source_prefix |
| 56 |
self.destination_pfx = destination_prefix |
| 57 |
|
| 58 |
if source_port: |
| 59 |
if ":" in source_port: |
| 60 |
self.source_port = tuple( |
| 61 |
[int(p) for p in source_port.split(":")] |
| 62 |
) |
| 63 |
else: |
| 64 |
self.source_port = int(source_port) |
| 65 |
else: |
| 66 |
self.source_port = source_port |
| 67 |
|
| 68 |
if destination_port: |
| 69 |
if ":" in destination_port: |
| 70 |
self.destination_port = tuple( |
| 71 |
[int(p) for p in destination_port.split(":")] |
| 72 |
) |
| 73 |
else: |
| 74 |
self.destination_port = int(destination_port) |
| 75 |
else: |
| 76 |
self.destination_port = destination_port |
| 77 |
|
| 78 |
self.protocol = protocol |
| 79 |
|
| 80 |
def __repr__(self): |
| 81 |
return "traffic-classifier:%s" % str(self) |
| 82 |
|
| 83 |
def __str__(self): |
| 84 |
return "{}-{},{}-{},{}".format(self.source_pfx or "*", |
| 85 |
self.source_port or "*", |
| 86 |
self.destination_pfx or "*", |
| 87 |
self.destination_port or "*", |
| 88 |
self.protocol) |
| 89 |
|
| 90 |
def __eq__(self, other): |
| 91 |
return (isinstance(other, self.__class__) and |
| 92 |
self.__dict__ == other.__dict__) |
| 93 |
|
| 94 |
def __hash__(self): |
| 95 |
return hash((self.source_pfx, self.source_port, |
| 96 |
self.destination_pfx, self.destination_port, |
| 97 |
self.protocol)) |
| 98 |
|
| 99 |
def _interpret_port_rule(self, rule): |
| 100 |
if len(rule) == 1: |
| 101 |
op = rule[0].operations |
| 102 |
port = rule[0].value |
| 103 |
if op == '>=': |
| 104 |
port_range = (int(port), 65535) |
| 105 |
elif op == '<=': |
| 106 |
port_range = (0, int(port)) |
| 107 |
elif op == '>': |
| 108 |
port_range = (int(port) + 1, 65535) |
| 109 |
elif op == '<': |
| 110 |
port_range = (0, int(port) - 1) |
| 111 |
else: |
| 112 |
port_range = int(port) |
| 113 |
else: |
| 114 |
port_range = () |
| 115 |
for elem in rule: |
| 116 |
op = elem.operations |
| 117 |
port = elem.value |
| 118 |
if op == '>=' or op == '<=': |
| 119 |
port_range += int(port) |
| 120 |
elif op == '>': |
| 121 |
port_range += int(port) + 1 |
| 122 |
elif op == '<': |
| 123 |
port_range += int(port) - 1 |
| 124 |
|
| 125 |
return port_range |
| 126 |
|
| 127 |
@log_decorator.log |
| 128 |
def _construct_port_rules(self, port_range, flow_object): |
| 129 |
LOG.info("Construct port rules with %s (type %s) on %s object", |
| 130 |
port_range, type(port_range), flow_object) |
| 131 |
port_rules = list() |
| 132 |
|
| 133 |
# Check if port range or port number |
| 134 |
if isinstance(port_range, tuple): |
| 135 |
port_min, port_max = port_range |
| 136 |
|
| 137 |
if int(port_min) == 0: |
| 138 |
# Operator < |
| 139 |
port_rules.append(flow_object(exa.flow.NumericOperator.LT, |
| 140 |
port_max)) |
| 141 |
elif int(port_max) == 65535: |
| 142 |
# Operator > |
| 143 |
port_rules.append(flow_object(exa.flow.NumericOperator.GT, |
| 144 |
port_min)) |
| 145 |
else: |
| 146 |
# Operator >= |
| 147 |
gt_eq_op = (exa.flow.NumericOperator.GT | |
| 148 |
exa.flow.NumericOperator.EQ) |
| 149 |
# Operator &<= |
| 150 |
lt_eq_op = (exa.flow.NumericOperator.AND | |
| 151 |
exa.flow.NumericOperator.LT | |
| 152 |
exa.flow.NumericOperator.EQ) |
| 153 |
port_rules.append(flow_object(gt_eq_op, port_min)) |
| 154 |
port_rules.append(flow_object(lt_eq_op, port_max)) |
| 155 |
else: |
| 156 |
port_rules.append(flow_object(exa.flow.NumericOperator.EQ, |
| 157 |
port_range)) |
| 158 |
|
| 159 |
return port_rules |
| 160 |
|
| 161 |
def _get_source_prefix(self, rule): |
| 162 |
self.source_pfx = rule[0].cidr.prefix() |
| 163 |
|
| 164 |
def _get_destination_prefix(self, rule): |
| 165 |
self.destination_pfx = rule[0].cidr.prefix() |
| 166 |
|
| 167 |
def _parse_any_port(self, rule): |
| 168 |
self.source_port = self._interpret_port_rule(rule) |
| 169 |
self.destination_port = list(self.source_port) |
| 170 |
|
| 171 |
def _parse_source_port(self, rule): |
| 172 |
self.source_port = self._interpret_port_rule(rule) |
| 173 |
|
| 174 |
def _parse_destination_port(self, rule): |
| 175 |
self.destination_port = self._interpret_port_rule(rule) |
| 176 |
|
| 177 |
def _get_protocol(self, rule): |
| 178 |
self.protocol = exa.Protocol.names[rule[0].value] |
| 179 |
|
| 180 |
def map_traffic_classifier_2_redirect_rules(self): |
| 181 |
rules = list() |
| 182 |
if self.source_pfx: |
| 183 |
ip, mask = self.source_pfx.split('/') |
| 184 |
if netaddr.IPNetwork(self.source_pfx).version == 4: |
| 185 |
rules.append( |
| 186 |
exa.flow.Flow4Source(socket.inet_pton(socket.AF_INET, ip), |
| 187 |
int(mask))) |
| 188 |
elif netaddr.IPNetwork(self.source_pfx).version == 6: |
| 189 |
# TODO(xxx): IPv6 offset ?? |
| 190 |
rules.append( |
| 191 |
exa.flow.Flow6Source(socket.inet_pton(socket.AF_INET6, ip), |
| 192 |
int(mask), 0)) |
| 193 |
|
| 194 |
if self.destination_pfx: |
| 195 |
ip, mask = self.destination_pfx.split('/') |
| 196 |
if netaddr.IPNetwork(self.destination_pfx).version == 4: |
| 197 |
rules.append( |
| 198 |
exa.flow.Flow4Destination(socket.inet_pton(socket.AF_INET, |
| 199 |
ip), |
| 200 |
int(mask)) |
| 201 |
) |
| 202 |
elif netaddr.IPNetwork(self.destination_pfx).version == 6: |
| 203 |
# TODO(xxx): IPv6 offset ?? |
| 204 |
rules.append( |
| 205 |
exa.flow.Flow6Destination(socket.inet_pton(socket.AF_INET6, |
| 206 |
ip), |
| 207 |
int(mask), 0) |
| 208 |
) |
| 209 |
|
| 210 |
if self.source_port: |
| 211 |
rules += self._construct_port_rules(self.source_port, |
| 212 |
exa.flow.FlowSourcePort) |
| 213 |
|
| 214 |
if self.destination_port: |
| 215 |
rules += self._construct_port_rules(self.destination_port, |
| 216 |
exa.flow.FlowDestinationPort) |
| 217 |
|
| 218 |
if self.protocol: |
| 219 |
rules.append(exa.flow.FlowIPProtocol(exa.flow.NumericOperator.EQ, |
| 220 |
exa.Protocol.named( |
| 221 |
self.protocol) |
| 222 |
)) |
| 223 |
|
| 224 |
return rules |
| 225 |
|
| 226 |
def map_redirect_rules_2_traffic_classifier(self, rules): |
| 227 |
components = {1: self._get_destination_prefix, # FlowDestinationPrefix |
| 228 |
2: self._get_source_prefix, # FlowSourcePrefix |
| 229 |
3: self._get_protocol, # FlowIPProtocol |
| 230 |
4: self._parse_any_port, # FlowAnyPort |
| 231 |
5: self._parse_destination_port, # FLowDestinationPort |
| 232 |
6: self._parse_source_port} # FlowSourcePort |
| 233 |
|
| 234 |
for ID, rule in rules.items(): |
| 235 |
components[ID](rule) |
| 236 |
|
| 237 |
|
| 238 |
class VPNInstance(tracker_worker.TrackerWorker, |
| 239 |
threading.Thread, |
| 240 |
lg.LookingGlassLocalLogger, |
| 241 |
metaclass=abc.ABCMeta): |
| 242 |
|
| 243 |
type = None # set by subclasses: 'ipvpn', 'evpn', etc. |
| 244 |
afi = None |
| 245 |
safi = None |
| 246 |
|
| 247 |
@log_decorator.log |
| 248 |
def __init__(self, vpn_manager, dataplane_driver, |
| 249 |
external_instance_id, instance_id, import_rts, export_rts, |
| 250 |
gateway_ip, ip_address_plen, readvertise, attract_traffic, |
| 251 |
fallback=None, **kwargs): |
| 252 |
|
| 253 |
self.manager = vpn_manager |
| 254 |
|
| 255 |
self.instance_type = self.__class__.__name__ |
| 256 |
self.instance_id = instance_id |
| 257 |
|
| 258 |
self.description = None |
| 259 |
|
| 260 |
threading.Thread.__init__(self) |
| 261 |
self.setDaemon(True) |
| 262 |
|
| 263 |
if dataplane_driver.ecmp_support: |
| 264 |
compare_routes = tracker_worker.compare_ecmp |
| 265 |
else: |
| 266 |
compare_routes = tracker_worker.compare_no_ecmp |
| 267 |
|
| 268 |
tracker_worker.TrackerWorker.__init__( |
| 269 |
self, |
| 270 |
self.manager.bgp_manager, repr(self), |
| 271 |
compare_routes) |
| 272 |
|
| 273 |
lg.LookingGlassLocalLogger.__init__(self, repr(self)) |
| 274 |
self.lock = threading.RLock() |
| 275 |
|
| 276 |
self.import_rts = import_rts |
| 277 |
self.export_rts = export_rts |
| 278 |
self.external_instance_id = external_instance_id |
| 279 |
self.gateway_ip = gateway_ip |
| 280 |
self.network_plen = ip_address_plen |
| 281 |
|
| 282 |
self.fallback = None |
| 283 |
|
| 284 |
self.afi = self.__class__.afi |
| 285 |
self.safi = self.__class__.safi |
| 286 |
|
| 287 |
self.dp_driver = dataplane_driver |
| 288 |
|
| 289 |
if 'vni' in kwargs: |
| 290 |
self.instance_label = kwargs.pop('vni') |
| 291 |
self.forced_vni = True |
| 292 |
else: |
| 293 |
self.instance_label = self.manager.label_allocator.get_new_label( |
| 294 |
"Incoming traffic for %s" % self) |
| 295 |
self.forced_vni = False |
| 296 |
|
| 297 |
self.instance_rd = self.manager.rd_allocator.get_new_rd( |
| 298 |
"Default route distinguisher for %s" % self) |
| 299 |
|
| 300 |
self.localport_data = dict() |
| 301 |
|
| 302 |
# One local port -> set of endpoints (MAC and IP addresses tuple) |
| 303 |
self.localport_2_endpoints = collections.defaultdict(set) |
| 304 |
# One endpoint (MAC and IP addresses tuple) -> One route distinguisher |
| 305 |
self.endpoint_2_rd = dict() |
| 306 |
# endpoint (MAC and IP addresses tuple) -> route entry |
| 307 |
self.endpoint_2_route = dict() |
| 308 |
# One MAC address -> One local port |
| 309 |
self.mac_2_localport_data = dict() |
| 310 |
# One IP address prefix -> Multiple MAC address |
| 311 |
self.ip_address_2_mac = collections.defaultdict(set) |
| 312 |
|
| 313 |
# One endpoint (MAC and IP addresses tuple) -> BGP local_pref |
| 314 |
self.endpoint_2_lp = dict() |
| 315 |
|
| 316 |
# endpoint (MAC and IP addresses tuple) -> description |
| 317 |
self.endpoint_2_desc = dict() |
| 318 |
|
| 319 |
# endpoint (MAC and IP addresses tuple) -> direction |
| 320 |
self.endpoint_2_direction = dict() |
| 321 |
|
| 322 |
# MAC -> set of IP addresses |
| 323 |
self.mac_2_ips = collections.defaultdict(set) |
| 324 |
|
| 325 |
# Redirected instances list from which traffic is attracted (based on |
| 326 |
# FlowSpec 5-tuple classification) |
| 327 |
self.redirected_instances = set() |
| 328 |
|
| 329 |
self.dataplane = self.dp_driver.initialize_dataplane_instance( |
| 330 |
self.instance_id, self.external_instance_id, |
| 331 |
self.gateway_ip, self.network_plen, self.instance_label, **kwargs) |
| 332 |
|
| 333 |
for rt in self.import_rts: |
| 334 |
self._subscribe(self.afi, self.safi, rt) |
| 335 |
# Subscribe to FlowSpec routes |
| 336 |
# FIXME(tmorin): this maybe isn't applicable yet to E-VPN yet |
| 337 |
self._subscribe(self.afi, exa.SAFI.flow_vpn, rt) |
| 338 |
|
| 339 |
if readvertise: |
| 340 |
self.readvertise = True |
| 341 |
try: |
| 342 |
self.readvertise_to_rts = readvertise['to_rt'] |
| 343 |
except KeyError: |
| 344 |
raise exc.APIException("'readvertise' specified with no " |
| 345 |
"'to_rt'") |
| 346 |
self.readvertise_from_rts = readvertise.get('from_rt', []) |
| 347 |
self.log.debug("readvertise enabled, from RT:%s, to %s", |
| 348 |
self.readvertise_from_rts, self.readvertise_to_rts) |
| 349 |
for rt in self.readvertise_from_rts: |
| 350 |
self._subscribe(self.afi, self.safi, rt) |
| 351 |
else: |
| 352 |
self.log.debug("readvertise not enabled") |
| 353 |
self.readvertise = False |
| 354 |
|
| 355 |
self.attract_static_dest_prefixes = None |
| 356 |
|
| 357 |
if (attract_traffic and (self.readvertise or |
| 358 |
attract_traffic.get('to_rt'))): |
| 359 |
# Convert route target string to RouteTarget dictionary |
| 360 |
attract_to_rts = attract_traffic.get('to_rt') |
| 361 |
|
| 362 |
if attract_to_rts: |
| 363 |
converted_to_rts = ( |
| 364 |
utils.convert_route_targets(attract_to_rts) |
| 365 |
) |
| 366 |
if self.readvertise: |
| 367 |
if converted_to_rts != self.readvertise_to_rts: |
| 368 |
raise exc.APIException("if both are set, then " |
| 369 |
"'attract_traffic/to_rt' and " |
| 370 |
"'readvertise/to_rt' have to " |
| 371 |
"be equal") |
| 372 |
else: |
| 373 |
self.readvertise_to_rts = converted_to_rts |
| 374 |
|
| 375 |
try: |
| 376 |
self.attract_static_dest_prefixes = ( |
| 377 |
attract_traffic['static_destination_prefixes'] |
| 378 |
) |
| 379 |
except KeyError: |
| 380 |
raise exc.APIException("'attract_traffic/to_rt' specified " |
| 381 |
"without " |
| 382 |
"'static_destination_prefixes'") |
| 383 |
|
| 384 |
if len(self.readvertise_to_rts) != 1: |
| 385 |
raise exc.APIException("attract_traffic requires exactly one " |
| 386 |
"RT to be provided in " |
| 387 |
"'readvertise/to_rt' and " |
| 388 |
"'attract_traffic/to_rt'") |
| 389 |
self.attract_traffic = True |
| 390 |
self.attract_rts = attract_traffic['redirect_rts'] |
| 391 |
try: |
| 392 |
self.attract_classifier = attract_traffic['classifier'] |
| 393 |
except KeyError: |
| 394 |
raise exc.APIException("'attract_traffic' specified with no " |
| 395 |
"'classifier'") |
| 396 |
self.log.debug("Attract traffic enabled with RT: %s and " |
| 397 |
"classifier: %s", self.attract_rts, |
| 398 |
self.attract_classifier) |
| 399 |
else: |
| 400 |
self.log.debug("attract traffic not enabled") |
| 401 |
self.attract_traffic = False |
| 402 |
|
| 403 |
self.dataplane.update_fallback(fallback) |
| 404 |
|
| 405 |
def needs_cleanup_assist(self, afi, safi): |
| 406 |
return self.dataplane.needs_cleanup_assist() |
| 407 |
|
| 408 |
def __repr__(self): |
| 409 |
return "{}-{}".format(self.instance_type, self.instance_id) |
| 410 |
|
| 411 |
@utils.synchronized |
| 412 |
@log_decorator.log |
| 413 |
def stop(self): |
| 414 |
if self._was_stopped: |
| 415 |
LOG.debug("was already stopped, nothing to do to stop") |
| 416 |
return |
| 417 |
self.stop_event_loop() |
| 418 |
|
| 419 |
for afi in (self.afi,): |
| 420 |
for safi in (self.safi, exa.SAFI.flow_vpn): |
| 421 |
if self.needs_cleanup_assist(afi, safi): |
| 422 |
self.log.debug("Dataplane driver needs cleanup assistance " |
| 423 |
"for AFI(%s)/SAFI(%s)", afi, safi) |
| 424 |
self.synthesize_withdraw_all(afi, safi) |
| 425 |
|
| 426 |
self.dataplane.cleanup() |
| 427 |
if not self.forced_vni: |
| 428 |
self.manager.label_allocator.release(self.instance_label) |
| 429 |
|
| 430 |
# this makes sure that the thread will be stopped, and any remaining |
| 431 |
# routes/subscriptions are released: |
| 432 |
tracker_worker.TrackerWorker.stop(self) |
| 433 |
|
| 434 |
@utils.synchronized |
| 435 |
@log_decorator.log |
| 436 |
def stop_if_empty(self): |
| 437 |
self.log.debug("localport_2_endpoints: %s", self.localport_2_endpoints) |
| 438 |
if self.is_empty(): |
| 439 |
self.stop() |
| 440 |
return True |
| 441 |
|
| 442 |
return False |
| 443 |
|
| 444 |
def is_empty(self): |
| 445 |
return not self.localport_2_endpoints |
| 446 |
|
| 447 |
def has_enpoint(self, linuxif): |
| 448 |
return self.localport_2_endpoints.get(linuxif) is not None |
| 449 |
|
| 450 |
def has_only_one_endpoint(self): |
| 451 |
return (len(self.localport_2_endpoints) == 1 and |
| 452 |
len(next(iter(self.localport_2_endpoints.values()))) == 1) |
| 453 |
|
| 454 |
def all_endpoints(self): |
| 455 |
return itertools.chain(*self.localport_2_endpoints.values()) |
| 456 |
|
| 457 |
@log_decorator.log |
| 458 |
def update_route_targets(self, new_import_rts, new_export_rts): |
| 459 |
added_import_rt = set(new_import_rts) - set(self.import_rts) |
| 460 |
removed_import_rt = set(self.import_rts) - set(new_import_rts) |
| 461 |
|
| 462 |
self.log.debug("Added Import RTs: %s", added_import_rt) |
| 463 |
self.log.debug("Removed Import RTs: %s", removed_import_rt) |
| 464 |
|
| 465 |
# Unregister from BGP with these route targets |
| 466 |
for rt in removed_import_rt: |
| 467 |
self._unsubscribe(self.afi, self.safi, rt) |
| 468 |
self._unsubscribe(self.afi, exa.SAFI.flow_vpn, rt) |
| 469 |
|
| 470 |
# Update import and export route targets |
| 471 |
# (needs to be done before subscribe or we get a race where |
| 472 |
# VRF._imported rejects route that it's supposed to use) |
| 473 |
self.import_rts = new_import_rts |
| 474 |
|
| 475 |
# Register to BGP with these route targets |
| 476 |
for rt in added_import_rt: |
| 477 |
self._subscribe(self.afi, self.safi, rt) |
| 478 |
self._subscribe(self.afi, exa.SAFI.flow_vpn, rt) |
| 479 |
|
| 480 |
# Re-advertise all routes with new export RTs |
| 481 |
self.log.debug("Exports RTs: %s -> %s", self.export_rts, |
| 482 |
new_export_rts) |
| 483 |
if frozenset(new_export_rts) != frozenset(self.export_rts): |
| 484 |
self.log.debug("Will re-export routes with new RTs") |
| 485 |
self.export_rts = new_export_rts |
| 486 |
for route_entry in self.endpoint_2_route.values(): |
| 487 |
if new_export_rts: |
| 488 |
self.log.info("Re-advertising route %s with updated RTs " |
| 489 |
"(%s)", route_entry.nlri, new_export_rts) |
| 490 |
|
| 491 |
updated_route_entry = engine.RouteEntry( |
| 492 |
route_entry.nlri, |
| 493 |
None, |
| 494 |
copy.copy(route_entry.attributes)) |
| 495 |
# reset the route_targets, overwriting RTs originally |
| 496 |
# present in route_entry.attributes |
| 497 |
updated_route_entry.set_route_targets(self.export_rts) |
| 498 |
|
| 499 |
self.log.debug(" updated route: %s", updated_route_entry) |
| 500 |
|
| 501 |
self._advertise_route(updated_route_entry) |
| 502 |
else: |
| 503 |
self._withdraw_route(route_entry) |
| 504 |
|
| 505 |
def update_fallback(self, fallback): |
| 506 |
if self.fallback != fallback and fallback is not None: |
| 507 |
self.log.info("update fallback: %s", fallback) |
| 508 |
self.fallback = fallback |
| 509 |
self.dataplane.update_fallback(fallback) |
| 510 |
|
| 511 |
def _parse_ipaddress_prefix(self, ip_address_prefix): |
| 512 |
if ip_address_prefix is None: |
| 513 |
return (None, 0) |
| 514 |
net = netaddr.IPNetwork(ip_address_prefix) |
| 515 |
return (str(net.ip), net.prefixlen) |
| 516 |
|
| 517 |
def _gen_encap_extended_communities(self): |
| 518 |
ecommunities = exa.extcoms.ExtendedCommunities() |
| 519 |
for encap in self.dp_driver.supported_encaps(): |
| 520 |
if not isinstance(encap, exa.Encapsulation): |
| 521 |
raise Exception(_("dp_driver.supported_encaps() should " |
| 522 |
"return a list of Encapsulation objects " |
| 523 |
"(%s)" % type(encap))) |
| 524 |
|
| 525 |
if encap != exa.Encapsulation( |
| 526 |
exa.Encapsulation.Type.DEFAULT): |
| 527 |
ecommunities.communities.append(encap) |
| 528 |
# FIXME: si DEFAULT + xxx => adv MPLS |
| 529 |
return ecommunities |
| 530 |
|
| 531 |
@abc.abstractmethod |
| 532 |
def generate_vif_bgp_route(self, mac_address, ip_prefix, plen, label, rd): |
| 533 |
'''returns a RouteEntry''' |
| 534 |
pass |
| 535 |
|
| 536 |
def synthesize_vif_bgp_route(self, mac_address, ip_prefix, plen, label, |
| 537 |
lb_consistent_hash_order, |
| 538 |
route_distinguisher=None, |
| 539 |
local_pref=None): |
| 540 |
rd = route_distinguisher if route_distinguisher else self.instance_rd |
| 541 |
route_entry = self.generate_vif_bgp_route(mac_address, ip_prefix, plen, |
| 542 |
label, rd) |
| 543 |
assert isinstance(route_entry, engine.RouteEntry) |
| 544 |
|
| 545 |
route_entry.attributes.add(self._gen_encap_extended_communities()) |
| 546 |
route_entry.set_route_targets(self.export_rts) |
| 547 |
|
| 548 |
ecommunities = exa.ExtendedCommunities() |
| 549 |
ecommunities.communities.append( |
| 550 |
exa.ConsistentHashSortOrder(lb_consistent_hash_order)) |
| 551 |
route_entry.attributes.add(ecommunities) |
| 552 |
|
| 553 |
route_entry.attributes.add(exa.LocalPreference(local_pref or |
| 554 |
DEFAULT_LOCAL_PREF)) |
| 555 |
|
| 556 |
self.log.debug("Synthesized Vif route entry: %s", route_entry) |
| 557 |
return route_entry |
| 558 |
|
| 559 |
def synthesize_redirect_bgp_route(self, rules): |
| 560 |
self.log.info("synthesize_redirect_bgp_route called for rules %s", |
| 561 |
rules) |
| 562 |
nlri = flowspec.FlowRouteFactory(self.afi, self.instance_rd) |
| 563 |
for rule in rules: |
| 564 |
nlri.add(rule) |
| 565 |
|
| 566 |
route_entry = engine.RouteEntry(nlri) |
| 567 |
|
| 568 |
assert isinstance(route_entry, engine.RouteEntry) |
| 569 |
|
| 570 |
ecommunities = exa.ExtendedCommunities() |
| 571 |
|
| 572 |
# checked at __init__: |
| 573 |
assert len(self.readvertise_to_rts) == 1 |
| 574 |
rt = self.readvertise_to_rts[0] |
| 575 |
ecommunities.communities.append( |
| 576 |
exa.TrafficRedirect(exa.ASN(int(rt.asn)), int(rt.number)) |
| 577 |
) |
| 578 |
|
| 579 |
route_entry.attributes.add(ecommunities) |
| 580 |
route_entry.set_route_targets(self.attract_rts) |
| 581 |
|
| 582 |
self.log.debug("Synthesized redirect route entry: %s", route_entry) |
| 583 |
return route_entry |
| 584 |
|
| 585 |
@classmethod |
| 586 |
def validate_convert_params(cls, params, also_mandatory=()): |
| 587 |
|
| 588 |
for param in ('vpn_instance_id', 'mac_address', |
| 589 |
'local_port') + also_mandatory: |
| 590 |
if param not in params: |
| 591 |
raise exc.APIMissingParameterException(param) |
| 592 |
|
| 593 |
# if local_port is not a dict, then assume it designates a linux |
| 594 |
# interface |
| 595 |
if isinstance(params['local_port'], str): |
| 596 |
params['local_port'] = {'linuxif': params['local_port']} |
| 597 |
|
| 598 |
for param in ('import_rt', 'export_rt'): |
| 599 |
if param not in params: |
| 600 |
continue |
| 601 |
|
| 602 |
# if import_rt or export_rt are strings, convert them into lists |
| 603 |
if isinstance(params[param], str): |
| 604 |
try: |
| 605 |
params[param] = re.split(',+ *', params[param]) |
| 606 |
except Exception: |
| 607 |
raise exc.APIException("Unable to parse RT string into " |
| 608 |
" a list: '%s'" % params[param]) |
| 609 |
# remove duplicates |
| 610 |
params[param] = list(set(params[param])) |
| 611 |
|
| 612 |
if not ('linuxif' in params['local_port'] or |
| 613 |
'evpn' in params['local_port']): |
| 614 |
raise exc.APIException("Mandatory key is missing in local_port " |
| 615 |
"parameter (linuxif, or evpn)") |
| 616 |
|
| 617 |
# validate mac_address |
| 618 |
try: |
| 619 |
netaddr.EUI(params['mac_address']) |
| 620 |
except netaddr.core.AddrFormatError: |
| 621 |
raise exc.MalformedMACAddress(params['mac_address']) |
| 622 |
|
| 623 |
# validate ip_address if present |
| 624 |
if 'ip_address' in params: |
| 625 |
try: |
| 626 |
net = netaddr.IPNetwork(params['ip_address']) |
| 627 |
params['ip_address_prefix'] = params['ip_address'] |
| 628 |
params['ip_address_plen'] = net.prefixlen |
| 629 |
except netaddr.core.AddrFormatError: |
| 630 |
# Try again assuming ip_address is an IP without a prefix |
| 631 |
# (implicitly using /32) |
| 632 |
try: |
| 633 |
netaddr.IPAddress(params['ip_address']) |
| 634 |
params['ip_address_prefix'] = params['ip_address'] + "/32" |
| 635 |
params['ip_address_plen'] = 32 |
| 636 |
except netaddr.core.AddrFormatError: |
| 637 |
raise exc.MalformedIPAddress(params['ip_address']) |
| 638 |
else: |
| 639 |
params['ip_address_prefix'] = None |
| 640 |
params['ip_address_plen'] = None |
| 641 |
|
| 642 |
if not isinstance(params.get('advertise_subnet', False), bool): |
| 643 |
raise exc.APIException("'advertise_subnet' must be a boolean") |
| 644 |
|
| 645 |
@classmethod |
| 646 |
def translate_api_internal(cls, params): |
| 647 |
# some API parameters have different internal names |
| 648 |
_TRANSLATE_API_TO_INTERNAL = { |
| 649 |
'vpn_instance_id': 'external_instance_id', |
| 650 |
'local_port': 'localport', |
| 651 |
'import_rt': 'import_rts', # API name is singular, hence wrong... |
| 652 |
'export_rt': 'export_rts', # API name is singular, hence wrong... |
| 653 |
} |
| 654 |
|
| 655 |
for api_param_name in list(params): |
| 656 |
internal_name = _TRANSLATE_API_TO_INTERNAL.get(api_param_name) |
| 657 |
if internal_name: |
| 658 |
params[internal_name] = params.pop(api_param_name) |
| 659 |
|
| 660 |
@classmethod |
| 661 |
def validate_convert_attach_params(cls, params): |
| 662 |
cls.validate_convert_params( |
| 663 |
params, |
| 664 |
also_mandatory=('import_rt', 'export_rt') |
| 665 |
) |
| 666 |
|
| 667 |
params['advertise_subnet'] = params.get('advertise_subnet', False) |
| 668 |
params['lb_consistent_hash_order'] = params.get( |
| 669 |
'lb_consistent_hash_order', 0) |
| 670 |
params['vni'] = params.get('vni', 0) |
| 671 |
|
| 672 |
params['direction'] = params.get('direction') or constants.BOTH |
| 673 |
if params['direction'] not in constants.ALL_DIRECTIONS: |
| 674 |
raise exc.APIException("direction should be one of: %s" % |
| 675 |
', '.join(constants.ALL_DIRECTIONS)) |
| 676 |
|
| 677 |
cls.translate_api_internal(params) |
| 678 |
|
| 679 |
@classmethod |
| 680 |
def validate_convert_detach_params(cls, params): |
| 681 |
cls.validate_convert_params(params) |
| 682 |
cls.translate_api_internal(params) |
| 683 |
|
| 684 |
def _rd_for_endpoint(self, endpoint, message): |
| 685 |
# endpoint is a (mac, ip_address_prefix) tuple |
| 686 |
rd = self.endpoint_2_rd.get(endpoint) |
| 687 |
if not rd: |
| 688 |
rd = self.manager.rd_allocator.get_new_rd(message) |
| 689 |
self.endpoint_2_rd[endpoint] = rd |
| 690 |
return rd |
| 691 |
|
| 692 |
def _raise_if_mac2ip_inconsistency(self, mac_address, ip_address_prefix): |
| 693 |
if not ip_address_prefix: |
| 694 |
return |
| 695 |
|
| 696 |
if mac_address not in self.ip_address_2_mac[ip_address_prefix]: |
| 697 |
raise exc.APIException( |
| 698 |
"Inconsistent endpoint info: IP %s already bound to a MAC " |
| 699 |
"address different from %s (%s)" % |
| 700 |
(ip_address_prefix, mac_address, |
| 701 |
self.ip_address_2_mac[ip_address_prefix])) |
| 702 |
|
| 703 |
def _check_ip_mac(self, mac_address, ip_address_prefix, advertise_subnet): |
| 704 |
(ip_prefix, plen) = self._parse_ipaddress_prefix(ip_address_prefix) |
| 705 |
|
| 706 |
# Special case where no IP was provided |
| 707 |
if ip_prefix is None: |
| 708 |
refresh_only = mac_address in self.mac_2_localport_data |
| 709 |
return (None, None, refresh_only) |
| 710 |
|
| 711 |
if not advertise_subnet and plen != 32: |
| 712 |
self.log.debug("Using /32 instead of /%s", plen) |
| 713 |
plen = 32 |
| 714 |
|
| 715 |
# - Verify (MAC address, IP address) tuple consistency |
| 716 |
refresh_only = False |
| 717 |
if ip_address_prefix in self.ip_address_2_mac and plen == 32: |
| 718 |
self._raise_if_mac2ip_inconsistency(mac_address, ip_address_prefix) |
| 719 |
|
| 720 |
LOG.debug("IP/MAC already plugged, only updating route") |
| 721 |
refresh_only = True |
| 722 |
|
| 723 |
return ip_prefix, plen, refresh_only |
| 724 |
|
| 725 |
@utils.synchronized |
| 726 |
@log_decorator.log_info |
| 727 |
def vif_plugged(self, mac_address, ip_address_prefix, localport, |
| 728 |
advertise_subnet=False, |
| 729 |
lb_consistent_hash_order=0, local_pref=None, **kwargs): |
| 730 |
linuxif = localport['linuxif'] |
| 731 |
endpoint = (mac_address, ip_address_prefix) |
| 732 |
# Check if this port has already been plugged |
| 733 |
# - Verify port informations consistency |
| 734 |
if mac_address in self.mac_2_localport_data: |
| 735 |
self.log.debug("MAC address already plugged, checking port " |
| 736 |
"consistency") |
| 737 |
pdata = self.mac_2_localport_data[mac_address] |
| 738 |
|
| 739 |
if pdata.get("port_info") != localport: |
| 740 |
raise exc.APIException("Port information is not consistent. " |
| 741 |
"MAC address cannot be bound to two " |
| 742 |
"different ports. Previous plug for " |
| 743 |
"port %s (%s != %s)" % |
| 744 |
(linuxif, pdata.get("port_info"), |
| 745 |
localport)) |
| 746 |
|
| 747 |
try: |
| 748 |
self.endpoint_2_desc[endpoint] = kwargs.get('description') |
| 749 |
|
| 750 |
(ip_prefix, plen, refresh_only) = self._check_ip_mac( |
| 751 |
mac_address, ip_address_prefix, advertise_subnet) |
| 752 |
|
| 753 |
self.log.debug("Plugging port (%s)", ip_prefix) |
| 754 |
|
| 755 |
pdata = self.mac_2_localport_data.get(mac_address, dict()) |
| 756 |
if not pdata: |
| 757 |
pdata['label'] = self.manager.label_allocator.get_new_label( |
| 758 |
"Incoming traffic for %s, interface %s, endpoint %s" % |
| 759 |
(self, linuxif, endpoint) |
| 760 |
) |
| 761 |
pdata["port_info"] = localport |
| 762 |
pdata["lb_consistent_hash_order"] = lb_consistent_hash_order |
| 763 |
|
| 764 |
direction = kwargs.get('direction') |
| 765 |
|
| 766 |
if not refresh_only: |
| 767 |
self.dp_driver.validate_directions(direction) |
| 768 |
# Call driver to setup the dataplane for incoming traffic |
| 769 |
self.dataplane.vif_plugged(mac_address, ip_prefix, |
| 770 |
localport, pdata['label'], |
| 771 |
direction) |
| 772 |
|
| 773 |
if forward_to_port(direction): |
| 774 |
endpoint_rd = self._rd_for_endpoint( |
| 775 |
endpoint, |
| 776 |
"Route distinguisher for %s, interface %s, endpoint %s" % |
| 777 |
(self, linuxif, endpoint) |
| 778 |
) |
| 779 |
|
| 780 |
rd = self.instance_rd if plen == 32 else endpoint_rd |
| 781 |
|
| 782 |
self.log.info("Synthesizing and advertising BGP route for VIF " |
| 783 |
"%s endpoint %s", linuxif, endpoint) |
| 784 |
route_entry = self.synthesize_vif_bgp_route( |
| 785 |
mac_address, ip_prefix, plen, |
| 786 |
pdata['label'], lb_consistent_hash_order, rd, local_pref) |
| 787 |
|
| 788 |
self._advertise_route(route_entry) |
| 789 |
self.endpoint_2_route[endpoint] = route_entry |
| 790 |
self.endpoint_2_rd[endpoint] = endpoint_rd |
| 791 |
|
| 792 |
self.localport_2_endpoints[linuxif].add(endpoint) |
| 793 |
self.endpoint_2_lp[endpoint] = local_pref |
| 794 |
self.endpoint_2_direction[endpoint] = direction |
| 795 |
self.mac_2_localport_data[mac_address] = pdata |
| 796 |
|
| 797 |
if ip_address_prefix: |
| 798 |
self.ip_address_2_mac[ip_address_prefix].add(mac_address) |
| 799 |
self.mac_2_ips[mac_address].add(ip_address_prefix) |
| 800 |
|
| 801 |
# we've a non-wildcard plug, so if a wildcard IP was plugged |
| 802 |
# for this MAC, consider it replaced: |
| 803 |
# - withdraw the corresponding route |
| 804 |
# - cleanup the wildcard endpoint records |
| 805 |
wildcard_endpoint = (mac_address, None) |
| 806 |
if wildcard_endpoint in self.localport_2_endpoints[linuxif]: |
| 807 |
self._withdraw_route(self.endpoint_2_route.pop( |
| 808 |
wildcard_endpoint)) |
| 809 |
self.localport_2_endpoints[linuxif].remove( |
| 810 |
wildcard_endpoint) |
| 811 |
del self.endpoint_2_desc[wildcard_endpoint] |
| 812 |
|
| 813 |
except Exception as e: |
| 814 |
self.log.error("Error in vif_plugged: %s", e) |
| 815 |
if linuxif in self.localport_2_endpoints: |
| 816 |
self.localport_2_endpoints[linuxif].discard(endpoint) |
| 817 |
if not self.localport_2_endpoints[linuxif]: |
| 818 |
del self.localport_2_endpoints[linuxif] |
| 819 |
if mac_address in self.mac_2_localport_data: |
| 820 |
del self.mac_2_localport_data[mac_address] |
| 821 |
if ip_address_prefix in self.ip_address_2_mac: |
| 822 |
self.ip_address_2_mac[ip_address_prefix].discard(mac_address) |
| 823 |
|
| 824 |
raise |
| 825 |
|
| 826 |
self.log.debug("localport_2_endpoints: %s", self.localport_2_endpoints) |
| 827 |
self.log.debug("endpoint_2_rd: %s", self.endpoint_2_rd) |
| 828 |
self.log.debug("mac_2_localport_data: %s", self.mac_2_localport_data) |
| 829 |
self.log.debug("ip_address_2_mac: %s", self.ip_address_2_mac) |
| 830 |
|
| 831 |
@utils.synchronized |
| 832 |
@log_decorator.log_info |
| 833 |
def vif_unplugged(self, mac_address, ip_address_prefix): |
| 834 |
if ip_address_prefix is None: |
| 835 |
# wildcard IP address case... |
| 836 |
errors = False |
| 837 |
for ip_addr_pfx in list(self.mac_2_ips[mac_address]): |
| 838 |
try: |
| 839 |
self.vif_unplugged_real(mac_address, ip_addr_pfx) |
| 840 |
except Exception: |
| 841 |
LOG.exception("Exception while unplugging (%s, %s)", |
| 842 |
mac_address, ip_addr_pfx) |
| 843 |
errors = True |
| 844 |
if errors: |
| 845 |
raise Exception(_("There were errors on at least one of the " |
| 846 |
"unplug resulting from the wildcard " |
| 847 |
"unplug")) |
| 848 |
# remove the wildcard endpoint itself, if any: |
| 849 |
if (mac_address, None) in self.endpoint_2_rd: |
| 850 |
try: |
| 851 |
self.vif_unplugged_real(mac_address, None) |
| 852 |
except exc.APINotPluggedYet: |
| 853 |
LOG.debug("Wildcard unplug failed because not-plugged-yet," |
| 854 |
" ignoring.") |
| 855 |
else: |
| 856 |
self.vif_unplugged_real(mac_address, ip_address_prefix) |
| 857 |
|
| 858 |
@log_decorator.log_info |
| 859 |
def vif_unplugged_real(self, mac_address, ip_address_prefix): |
| 860 |
# NOTE(tmorin): move this as a vif_unplugged_precheck, so that |
| 861 |
# in ipvpn.VRF this is done before readvertised route withdrawal |
| 862 |
endpoint = (mac_address, ip_address_prefix) |
| 863 |
# Verify port and endpoint (MAC address, IP address) tuple consistency |
| 864 |
pdata = self.mac_2_localport_data.get(mac_address) |
| 865 |
|
| 866 |
if not pdata: |
| 867 |
raise exc.APINotPluggedYet(endpoint) |
| 868 |
|
| 869 |
self._raise_if_mac2ip_inconsistency(mac_address, ip_address_prefix) |
| 870 |
|
| 871 |
if endpoint not in self.endpoint_2_rd: |
| 872 |
raise Exception(_("Endpoint record missing: {}".format(endpoint))) |
| 873 |
|
| 874 |
# Finding label and local port informations |
| 875 |
label = pdata.get('label') |
| 876 |
localport = pdata.get('port_info') |
| 877 |
linuxif = localport['linuxif'] |
| 878 |
direction = self.endpoint_2_direction[endpoint] |
| 879 |
if not label or not localport: |
| 880 |
self.log.error("vif_unplugged called for endpoint (%s, %s), but " |
| 881 |
"port data (%s, %s) is incomplete", |
| 882 |
mac_address, ip_address_prefix, label, localport) |
| 883 |
raise Exception(_("Inconsistent informations for port, bug ?")) |
| 884 |
|
| 885 |
if linuxif in self.localport_2_endpoints: |
| 886 |
# Parse address/mask |
| 887 |
(ip_prefix, _unused) = self._parse_ipaddress_prefix( |
| 888 |
ip_address_prefix) |
| 889 |
|
| 890 |
self.log.info("Withdrawing BGP route for VIF %s endpoint %s", |
| 891 |
linuxif, endpoint) |
| 892 |
|
| 893 |
self._withdraw_route(self.endpoint_2_route.pop(endpoint)) |
| 894 |
|
| 895 |
last_endpoint = len(self.localport_2_endpoints[linuxif]) <= 1 |
| 896 |
|
| 897 |
if forward_to_port(direction): |
| 898 |
# Unplug endpoint from data plane |
| 899 |
self.dataplane.vif_unplugged( |
| 900 |
mac_address, ip_prefix, localport, label, direction, |
| 901 |
last_endpoint) |
| 902 |
|
| 903 |
# Forget data for this port if last endpoint |
| 904 |
if last_endpoint: |
| 905 |
self.log.info("Last endpoint, freeing label %s", label) |
| 906 |
# Free label to the allocator |
| 907 |
self.manager.label_allocator.release(label) |
| 908 |
|
| 909 |
del self.localport_2_endpoints[linuxif] |
| 910 |
else: |
| 911 |
self.localport_2_endpoints[linuxif].remove(endpoint) |
| 912 |
|
| 913 |
# Free route distinguisher to the allocator |
| 914 |
if forward_to_port(direction): |
| 915 |
self.manager.rd_allocator.release( |
| 916 |
self.endpoint_2_rd.pop(endpoint)) |
| 917 |
|
| 918 |
if not last_endpoint: |
| 919 |
if not any([ep[0] == mac_address |
| 920 |
for ep |
| 921 |
in self.localport_2_endpoints[linuxif]] |
| 922 |
): |
| 923 |
del self.mac_2_localport_data[mac_address] |
| 924 |
else: |
| 925 |
del self.mac_2_localport_data[mac_address] |
| 926 |
|
| 927 |
if ip_address_prefix: |
| 928 |
self.ip_address_2_mac[ip_address_prefix].discard(mac_address) |
| 929 |
if not self.ip_address_2_mac[ip_address_prefix]: |
| 930 |
del self.ip_address_2_mac[ip_address_prefix] |
| 931 |
|
| 932 |
self.mac_2_ips[mac_address].discard(ip_address_prefix) |
| 933 |
if not self.mac_2_ips[mac_address]: |
| 934 |
del self.mac_2_ips[mac_address] |
| 935 |
|
| 936 |
del self.endpoint_2_desc[endpoint] |
| 937 |
else: |
| 938 |
self.log.error("vif_unplugged called for endpoint %s, but" |
| 939 |
" port data is incomplete", endpoint) |
| 940 |
raise Exception(_("bagpipe-bgp bug, check its logs")) |
| 941 |
|
| 942 |
self.log.debug("localport_2_endpoints: %s", self.localport_2_endpoints) |
| 943 |
self.log.debug("endpoint_2_rd: %s", self.endpoint_2_rd) |
| 944 |
self.log.debug("mac_2_localport_data: %s", self.mac_2_localport_data) |
| 945 |
self.log.debug("ip_address_2_mac: %s", self.ip_address_2_mac) |
| 946 |
|
| 947 |
@utils.synchronized |
| 948 |
def register_redirected_instance(self, instance_id): |
| 949 |
self.redirected_instances.add(instance_id) |
| 950 |
|
| 951 |
@utils.synchronized |
| 952 |
def unregister_redirected_instance(self, instance_id): |
| 953 |
self.redirected_instances.remove(instance_id) |
| 954 |
|
| 955 |
@utils.synchronized |
| 956 |
@log_decorator.log |
| 957 |
def stop_if_no_redirected_instance(self): |
| 958 |
self.log.debug("redirected_instances: %s", self.redirected_instances) |
| 959 |
if not self.redirected_instances: |
| 960 |
self.stop() |
| 961 |
return True |
| 962 |
|
| 963 |
return False |
| 964 |
|
| 965 |
@log_decorator.log |
| 966 |
def start_redirect_traffic(self, redirect_rt, rules): |
| 967 |
self.log.debug("Start redirecting traffic to VPN instance importing " |
| 968 |
"route target %s based on rules %s", redirect_rt, rules) |
| 969 |
# Create redirection instance if first FlowSpec route for this |
| 970 |
# redirect route target |
| 971 |
redirect_instance = self.manager.redirect_traffic_to_vpn( |
| 972 |
self.external_instance_id, self.type, redirect_rt |
| 973 |
) |
| 974 |
# Create traffic classifier from FlowSpec rules |
| 975 |
classifier = TrafficClassifier() |
| 976 |
classifier.map_redirect_rules_2_traffic_classifier(rules) |
| 977 |
|
| 978 |
self.dataplane.add_dataplane_for_traffic_classifier( |
| 979 |
classifier, |
| 980 |
redirect_instance.instance_id) |
| 981 |
|
| 982 |
if not hasattr(self, 'redirect_rt_2_classifiers'): |
| 983 |
# One redirect route target -> Multiple traffic classifiers (One |
| 984 |
# per prefix) |
| 985 |
# Can be inverted to one traffic classifier -> Multiple redirect |
| 986 |
# route targets |
| 987 |
self.redirect_rt_2_classifiers = collections.defaultdict(set) |
| 988 |
|
| 989 |
self.redirect_rt_2_classifiers[redirect_rt].add(classifier) |
| 990 |
|
| 991 |
@log_decorator.log |
| 992 |
def stop_redirect_traffic(self, redirect_rt, rules): |
| 993 |
self.log.debug("Stop redirecting traffic to VPN instance importing " |
| 994 |
"route target %s based on rules %s", redirect_rt, rules) |
| 995 |
# Create traffic classifier from FlowSpec rule |
| 996 |
classifier = TrafficClassifier() |
| 997 |
classifier.map_redirect_rules_2_traffic_classifier(rules) |
| 998 |
|
| 999 |
if (redirect_rt in self.redirect_rt_2_classifiers and |
| 1000 |
classifier in self.redirect_rt_2_classifiers[redirect_rt]): |
| 1001 |
self.redirect_rt_2_classifiers[redirect_rt].remove(classifier) |
| 1002 |
|
| 1003 |
if not utils.invert_dict_of_sets( |
| 1004 |
self.redirect_rt_2_classifiers)[classifier]: |
| 1005 |
self.dataplane.remove_dataplane_for_traffic_classifier( |
| 1006 |
classifier) |
| 1007 |
|
| 1008 |
if not self.redirect_rt_2_classifiers[redirect_rt]: |
| 1009 |
self.manager.stop_redirect_to_vpn( |
| 1010 |
self.external_instance_id, self.type, redirect_rt |
| 1011 |
) |
| 1012 |
del self.redirect_rt_2_classifiers[redirect_rt] |
| 1013 |
else: |
| 1014 |
self.log.error("stop_redirect_traffic called for redirect route " |
| 1015 |
"target %s and classifier %s, but doesn't exist", |
| 1016 |
redirect_rt, classifier) |
| 1017 |
raise Exception(_("bagpipe-bgp bug, check its logs")) |
| 1018 |
|
| 1019 |
def _check_encaps(self, route): |
| 1020 |
'''check that encaps of a route |
| 1021 |
|
| 1022 |
returns a list of encaps supported by both the dataplane driver and the |
| 1023 |
advertized route (based on BGP Encapsulation community) |
| 1024 |
|
| 1025 |
logs a warning if there is no common encap |
| 1026 |
''' |
| 1027 |
adv_encaps = None |
| 1028 |
try: |
| 1029 |
adv_encaps = route.ecoms(exa.Encapsulation) |
| 1030 |
self.log.debug("Advertized Encaps: %s", adv_encaps) |
| 1031 |
except KeyError: |
| 1032 |
self.log.debug("no encap advertized, let's use default") |
| 1033 |
|
| 1034 |
if not adv_encaps: |
| 1035 |
adv_encaps = [exa.Encapsulation( |
| 1036 |
exa.Encapsulation.Type.DEFAULT)] |
| 1037 |
|
| 1038 |
good_encaps = set(adv_encaps) & set( |
| 1039 |
self.dp_driver.supported_encaps()) |
| 1040 |
|
| 1041 |
if not good_encaps: |
| 1042 |
self.log.warning("No encap supported by dataplane driver for route" |
| 1043 |
" %s, advertized: %s, dataplane supports: {%s}", |
| 1044 |
route, adv_encaps, |
| 1045 |
", ".join([repr(encap) for encap in |
| 1046 |
self.dp_driver.supported_encaps()] |
| 1047 |
)) |
| 1048 |
|
| 1049 |
return good_encaps |
| 1050 |
|
| 1051 |
def _skip_route_removal(self, last): |
| 1052 |
'''check if a route withdraw should be skipped |
| 1053 |
|
| 1054 |
returns true if the removal of the route should be skipped, based |
| 1055 |
whether or not the route removed is the last one and depending on |
| 1056 |
the desired behavior for the dataplane driver |
| 1057 |
''' |
| 1058 |
if last: |
| 1059 |
# never skip the last route |
| 1060 |
return False |
| 1061 |
|
| 1062 |
# if the driver supports ECMP, then it needs to see all the route |
| 1063 |
# withdraws |
| 1064 |
return not self.dp_driver.ecmp_support |
| 1065 |
|
| 1066 |
# Callbacks for BGP route updates (TrackerWorker) ######################## |
| 1067 |
|
| 1068 |
@utils.synchronized |
| 1069 |
@log_decorator.log |
| 1070 |
def new_best_route(self, entry, new_route): |
| 1071 |
pass |
| 1072 |
|
| 1073 |
@utils.synchronized |
| 1074 |
@log_decorator.log |
| 1075 |
def best_route_removed(self, entry, old_route, last): |
| 1076 |
pass |
| 1077 |
|
| 1078 |
# Looking Glass #### |
| 1079 |
|
| 1080 |
def get_lg_map(self): |
| 1081 |
return { |
| 1082 |
"instance_type": (lg.VALUE, self.instance_type), |
| 1083 |
"external_instance_id": (lg.VALUE, self.external_instance_id), |
| 1084 |
"description": (lg.VALUE, self.description), |
| 1085 |
"dataplane": (lg.DELEGATE, self.dataplane), |
| 1086 |
"route_targets": (lg.SUBITEM, self.get_rts), |
| 1087 |
"gateway_ip": (lg.VALUE, self.gateway_ip), |
| 1088 |
"subnet_prefix_length": (lg.VALUE, self.network_plen), |
| 1089 |
"instance_dataplane_id": (lg.VALUE, self.instance_label), |
| 1090 |
"ports": (lg.SUBTREE, self.get_lg_local_port_data), |
| 1091 |
"readvertise": (lg.SUBITEM, self.get_lg_readvertise), |
| 1092 |
"attract_traffic": (lg.SUBITEM, self.get_lg_attract_traffic), |
| 1093 |
"fallback": (lg.VALUE, self.fallback) |
| 1094 |
} |
| 1095 |
|
| 1096 |
def get_lg_local_port_data(self, path_prefix): |
| 1097 |
r = {} |
| 1098 |
for (port, endpoints) in self.localport_2_endpoints.items(): |
| 1099 |
eps = [] |
| 1100 |
for endpoint in endpoints: |
| 1101 |
mac, ip = endpoint |
| 1102 |
eps.append({ |
| 1103 |
'label': self.mac_2_localport_data[mac]['label'], |
| 1104 |
'mac_address': mac, |
| 1105 |
'ip_address': ip, |
| 1106 |
'local_pref': self.endpoint_2_lp.get(endpoint), |
| 1107 |
'rd': repr(self.endpoint_2_rd[endpoint]), |
| 1108 |
'description': self.endpoint_2_desc.get(endpoint), |
| 1109 |
'direction': self.endpoint_2_direction.get(endpoint) |
| 1110 |
}) |
| 1111 |
|
| 1112 |
r[port] = { |
| 1113 |
'endpoints': eps |
| 1114 |
} |
| 1115 |
return r |
| 1116 |
|
| 1117 |
def get_rts(self): |
| 1118 |
return { |
| 1119 |
"import": [repr(rt) for rt in self.import_rts], |
| 1120 |
"export": [repr(rt) for rt in self.export_rts] |
| 1121 |
} |
| 1122 |
|
| 1123 |
def get_lg_readvertise(self): |
| 1124 |
r = {} |
| 1125 |
if self.readvertise: |
| 1126 |
r = { |
| 1127 |
'from': [repr(rt) for rt in self.readvertise_from_rts], |
| 1128 |
'to': [repr(rt) for rt in self.readvertise_to_rts] |
| 1129 |
} |
| 1130 |
return r |
| 1131 |
|
| 1132 |
def get_lg_attract_traffic(self): |
| 1133 |
r = {} |
| 1134 |
if self.attract_traffic: |
| 1135 |
r = { |
| 1136 |
'redirect_rts': [repr(rt) for rt in self.attract_rts], |
| 1137 |
'classifier': self.attract_classifier, |
| 1138 |
} |
| 1139 |
|
| 1140 |
if not hasattr(self, 'attract_static_dest_prefixes'): |
| 1141 |
return r |
| 1142 |
|
| 1143 |
r.update({ |
| 1144 |
'to': [repr(rt) for rt in self.readvertise_to_rts], |
| 1145 |
'static_destination_prefixes': |
| 1146 |
self.attract_static_dest_prefixes |
| 1147 |
}) |
| 1148 |
|
| 1149 |
return r |
| 1150 |
|
| 1151 |
def get_lg_summary(self): |
| 1152 |
# used from VPNManager looking glass |
| 1153 |
entry = {"id": self.external_instance_id, |
| 1154 |
"name": self.name} |
| 1155 |
if self.description: |
| 1156 |
entry['description'] = self.description |
| 1157 |
return entry |