| 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 |
|
| 17 |
from networking_bagpipe._i18n import _ |
| 18 |
from networking_bagpipe.bagpipe_bgp.common import log_decorator |
| 19 |
from networking_bagpipe.bagpipe_bgp.common import looking_glass as lg |
| 20 |
from networking_bagpipe.bagpipe_bgp.common import utils |
| 21 |
from networking_bagpipe.bagpipe_bgp import constants |
| 22 |
from networking_bagpipe.bagpipe_bgp import engine |
| 23 |
from networking_bagpipe.bagpipe_bgp.engine import exa |
| 24 |
from networking_bagpipe.bagpipe_bgp.vpn import dataplane_drivers as dp_drivers |
| 25 |
from networking_bagpipe.bagpipe_bgp.vpn import vpn_instance |
| 26 |
|
| 27 |
|
| 28 |
class VPNInstanceDataplane(dp_drivers.VPNInstanceDataplane, |
| 29 |
metaclass=abc.ABCMeta): |
| 30 |
|
| 31 |
@abc.abstractmethod |
| 32 |
def add_dataplane_for_bum_endpoint(self, remote_pe, dpid, nlri, encaps): |
| 33 |
pass |
| 34 |
|
| 35 |
@abc.abstractmethod |
| 36 |
def remove_dataplane_for_bum_endpoint(self, remote_pe, dpid, nlri): |
| 37 |
pass |
| 38 |
|
| 39 |
@abc.abstractmethod |
| 40 |
def set_gateway_port(self, linuxif, gateway_ip): |
| 41 |
'''Set the IP gateway port |
| 42 |
|
| 43 |
Used to determine a port to which traffic at the destination of the |
| 44 |
IP gateway should be sent. This is used to plug an EVI into an IP VPN |
| 45 |
VRF. |
| 46 |
''' |
| 47 |
pass |
| 48 |
|
| 49 |
@abc.abstractmethod |
| 50 |
def gateway_port_down(self, linuxif): |
| 51 |
'''Unset the IP gateway port |
| 52 |
|
| 53 |
Used to revert the action done when set_gateway_port was called. |
| 54 |
Relevant only when an EVI had been plugged into an IP VPN VRF. |
| 55 |
''' |
| 56 |
pass |
| 57 |
|
| 58 |
|
| 59 |
class DummyVPNInstanceDataplane(dp_drivers.DummyVPNInstanceDataplane, |
| 60 |
VPNInstanceDataplane): |
| 61 |
'''Dummy, do-nothing dataplane driver''' |
| 62 |
|
| 63 |
@log_decorator.log_info |
| 64 |
def add_dataplane_for_bum_endpoint(self, *args, **kwargs): |
| 65 |
pass |
| 66 |
|
| 67 |
@log_decorator.log_info |
| 68 |
def remove_dataplane_for_bum_endpoint(self, *args, **kwargs): |
| 69 |
pass |
| 70 |
|
| 71 |
@log_decorator.log_info |
| 72 |
def set_gateway_port(self, *args, **kwargs): |
| 73 |
pass |
| 74 |
|
| 75 |
@log_decorator.log_info |
| 76 |
def gateway_port_down(self, *args, **kwargs): |
| 77 |
pass |
| 78 |
|
| 79 |
|
| 80 |
class DummyDataplaneDriver(dp_drivers.DummyDataplaneDriver): |
| 81 |
|
| 82 |
type = constants.EVPN |
| 83 |
|
| 84 |
dataplane_instance_class = DummyVPNInstanceDataplane |
| 85 |
encaps = [exa.Encapsulation(exa.Encapsulation.Type.VXLAN)] |
| 86 |
|
| 87 |
def __init__(self, *args, **kwargs): |
| 88 |
dp_drivers.DummyDataplaneDriver.__init__(self, *args, **kwargs) |
| 89 |
|
| 90 |
|
| 91 |
class EVI(vpn_instance.VPNInstance, lg.LookingGlassMixin): |
| 92 |
'''Implementation an E-VPN MAC-VRF instance (EVI) |
| 93 |
|
| 94 |
based on RFC7432 and draft-ietf-bess-evpn-overlay. |
| 95 |
''' |
| 96 |
|
| 97 |
type = constants.EVPN |
| 98 |
afi = exa.AFI.l2vpn |
| 99 |
safi = exa.SAFI.evpn |
| 100 |
|
| 101 |
@log_decorator.log |
| 102 |
def __init__(self, *args, **kwargs): |
| 103 |
|
| 104 |
vpn_instance.VPNInstance.__init__(self, *args, **kwargs) |
| 105 |
|
| 106 |
self.gw_port = None |
| 107 |
|
| 108 |
encaps = self.dp_driver.supported_encaps() |
| 109 |
if (exa.Encapsulation(exa.Encapsulation.Type.VXLAN) in encaps and |
| 110 |
any([ |
| 111 |
exa.Encapsulation(exa.Encapsulation.Type.MPLS) in encaps, |
| 112 |
exa.Encapsulation(exa.Encapsulation.Type.GRE) in encaps, |
| 113 |
exa.Encapsulation(exa.Encapsulation.Type.MPLS_UDP) in encaps |
| 114 |
])): |
| 115 |
raise Exception(_("The dataplane can't support both an MPLS encap " |
| 116 |
"and a VXLAN encapsulation")) |
| 117 |
|
| 118 |
# Advertise route to receive multi-destination traffic |
| 119 |
self.log.info("Generating BGP route for broadcast/multicast traffic") |
| 120 |
|
| 121 |
nlri = exa.EVPNMulticast( |
| 122 |
self.instance_rd, |
| 123 |
exa.EthernetTag(), |
| 124 |
exa.IP.create(self.bgp_manager.get_local_address()), |
| 125 |
None, |
| 126 |
exa.IP.create(self.bgp_manager.get_local_address())) |
| 127 |
|
| 128 |
attributes = exa.Attributes() |
| 129 |
|
| 130 |
attributes.add(self._gen_encap_extended_communities()) |
| 131 |
|
| 132 |
# add PMSI Tunnel Attribute route |
| 133 |
attributes.add( |
| 134 |
exa.PMSIIngressReplication(self.dp_driver.get_local_address(), |
| 135 |
raw_label=self.instance_label)) |
| 136 |
|
| 137 |
self.multicast_route_entry = engine.RouteEntry(nlri, self.export_rts, |
| 138 |
attributes) |
| 139 |
|
| 140 |
self._advertise_route(self.multicast_route_entry) |
| 141 |
|
| 142 |
def _vxlan_dp_driver(self): |
| 143 |
return (exa.Encapsulation(exa.Encapsulation.Type.VXLAN) in |
| 144 |
self.dp_driver.supported_encaps()) |
| 145 |
|
| 146 |
def generate_vif_bgp_route(self, mac_address, ip_prefix, plen, label, rd): |
| 147 |
# Generate BGP route and advertise it... |
| 148 |
|
| 149 |
if ip_prefix: |
| 150 |
assert plen == 32 |
| 151 |
|
| 152 |
if self._vxlan_dp_driver(): |
| 153 |
mpls_label_field = exa.Labels([], raw_labels=[self.instance_label]) |
| 154 |
else: |
| 155 |
mpls_label_field = exa.Labels([self.instance_label]) |
| 156 |
|
| 157 |
# label parameter ignored, we need to use instance label |
| 158 |
nlri = exa.EVPNMAC( |
| 159 |
rd, exa.ESI(), exa.EthernetTag(), exa.MAC(mac_address), 6 * 8, |
| 160 |
mpls_label_field, |
| 161 |
exa.IP.create(ip_prefix) if ip_prefix else None, None, |
| 162 |
exa.IP.create(self.dp_driver.get_local_address())) |
| 163 |
|
| 164 |
return engine.RouteEntry(nlri) |
| 165 |
|
| 166 |
@log_decorator.log |
| 167 |
def set_gateway_port(self, linuxif, ipvpn): |
| 168 |
self.dataplane.set_gateway_port(linuxif, ipvpn.gateway_ip) |
| 169 |
self.gw_port = (linuxif, ipvpn) |
| 170 |
|
| 171 |
@log_decorator.log |
| 172 |
def gateway_port_down(self, linuxif): |
| 173 |
self.dataplane.gateway_port_down(linuxif) |
| 174 |
self.gw_port = None |
| 175 |
|
| 176 |
def has_gateway_port(self): |
| 177 |
return (self.gw_port is not None) |
| 178 |
|
| 179 |
def _interpret_nlri_mpls_field(self, label_field): |
| 180 |
# interpret the MPLS field of an EVPN MAroute, based on |
| 181 |
# whether we assume this is a VXLAN VNI or not |
| 182 |
if self._vxlan_dp_driver(): |
| 183 |
return label_field.raw_labels[0] |
| 184 |
else: |
| 185 |
return label_field.labels[0] |
| 186 |
|
| 187 |
def _interpret_pta_label_field(self, pmsi_tunnel): |
| 188 |
# interpret the MPLS field of a PMSI tunnel attribute, based on |
| 189 |
# whether we assume this is a VXLAN VNI or not |
| 190 |
if self._vxlan_dp_driver(): |
| 191 |
return pmsi_tunnel.raw_label |
| 192 |
else: |
| 193 |
return pmsi_tunnel.label |
| 194 |
|
| 195 |
# TrackerWorker callbacks for BGP route updates ########################## |
| 196 |
|
| 197 |
def route_to_tracked_entry(self, route): |
| 198 |
if isinstance(route.nlri, exa.EVPNMAC): |
| 199 |
return (exa.EVPNMAC, route.nlri.mac) |
| 200 |
elif isinstance(route.nlri, exa.EVPNMulticast): |
| 201 |
return (exa.EVPNMulticast, (route.nlri.ip, route.nlri.rd)) |
| 202 |
elif isinstance(route.nlri, exa.EVPN): |
| 203 |
self.log.warning("Received EVPN route of unsupported subtype: %s", |
| 204 |
route.nlri.CODE) |
| 205 |
return None |
| 206 |
else: |
| 207 |
raise Exception(_("EVI %d should not receive routes of type %s" % |
| 208 |
(self.instance_id, type(route.nlri)))) |
| 209 |
|
| 210 |
@utils.synchronized |
| 211 |
@log_decorator.log |
| 212 |
def new_best_route(self, entry, new_route): |
| 213 |
(entry_class, info) = entry |
| 214 |
|
| 215 |
encaps = self._check_encaps(new_route) |
| 216 |
if not encaps: |
| 217 |
return |
| 218 |
|
| 219 |
if entry_class == exa.EVPNMAC: |
| 220 |
prefix = info |
| 221 |
|
| 222 |
remote_pe = new_route.nexthop |
| 223 |
|
| 224 |
dataplane_id = self._interpret_nlri_mpls_field( |
| 225 |
new_route.nlri.label) |
| 226 |
|
| 227 |
self.dataplane.setup_dataplane_for_remote_endpoint( |
| 228 |
prefix, remote_pe, dataplane_id, new_route.nlri, encaps) |
| 229 |
|
| 230 |
elif entry_class == exa.EVPNMulticast: |
| 231 |
remote_endpoint = info |
| 232 |
|
| 233 |
# check that the route is actually carrying an PMSITunnel of type |
| 234 |
# ingress replication |
| 235 |
pmsi_tunnel = new_route.attributes.get(exa.PMSI.ID) |
| 236 |
if not isinstance(pmsi_tunnel, exa.PMSIIngressReplication): |
| 237 |
self.log.warning("Received PMSITunnel of unsupported type: %s", |
| 238 |
type(pmsi_tunnel)) |
| 239 |
else: |
| 240 |
remote_endpoint = pmsi_tunnel.ip |
| 241 |
dataplane_id = self._interpret_pta_label_field(pmsi_tunnel) |
| 242 |
|
| 243 |
self.log.info("Setting up dataplane for new ingress " |
| 244 |
"replication destination %s", remote_endpoint) |
| 245 |
self.dataplane.add_dataplane_for_bum_endpoint( |
| 246 |
remote_endpoint, dataplane_id, new_route.nlri, encaps) |
| 247 |
else: |
| 248 |
self.log.warning("unsupported entry_class: %s", |
| 249 |
entry_class.__name__) |
| 250 |
|
| 251 |
@utils.synchronized |
| 252 |
@log_decorator.log |
| 253 |
def best_route_removed(self, entry, old_route, last): |
| 254 |
(entry_class, info) = entry |
| 255 |
|
| 256 |
if entry_class == exa.EVPNMAC: |
| 257 |
|
| 258 |
if self._skip_route_removal(last): |
| 259 |
self.log.debug("Skipping removal of non-last route because " |
| 260 |
"dataplane does not want it") |
| 261 |
return |
| 262 |
|
| 263 |
def ip_dpid_from_route(route): |
| 264 |
return (route.nexthop, |
| 265 |
self._interpret_nlri_mpls_field(route.nlri.label)) |
| 266 |
|
| 267 |
if self.equivalent_route_in_best_routes(old_route, |
| 268 |
ip_dpid_from_route): |
| 269 |
self.log.debug("Route for same dataplane is still in best " |
| 270 |
"routes, skipping removal") |
| 271 |
return |
| 272 |
|
| 273 |
prefix = info |
| 274 |
|
| 275 |
remote_pe, dataplane_id = ip_dpid_from_route(old_route) |
| 276 |
|
| 277 |
self.dataplane.remove_dataplane_for_remote_endpoint( |
| 278 |
prefix, remote_pe, dataplane_id, old_route.nlri) |
| 279 |
|
| 280 |
elif entry_class == exa.EVPNMulticast: |
| 281 |
remote_endpoint = info |
| 282 |
|
| 283 |
def ip_dpid_from_route(route): |
| 284 |
pmsi_tunnel = route.attributes.get(exa.PMSI.ID) |
| 285 |
remote_endpoint = pmsi_tunnel.ip |
| 286 |
dataplane_id = self._interpret_pta_label_field(pmsi_tunnel) |
| 287 |
return (remote_endpoint, dataplane_id) |
| 288 |
|
| 289 |
# check that the route is actually carrying an PMSITunnel of type |
| 290 |
# ingress replication |
| 291 |
pmsi_tunnel = old_route.attributes.get(exa.PMSI.ID) |
| 292 |
if not isinstance(pmsi_tunnel, exa.PMSIIngressReplication): |
| 293 |
self.log.warning("PMSITunnel of suppressed route is of" |
| 294 |
" unsupported type") |
| 295 |
return |
| 296 |
|
| 297 |
if self.equivalent_route_in_best_routes(old_route, |
| 298 |
ip_dpid_from_route): |
| 299 |
self.log.debug("Route for same dataplane is still in best " |
| 300 |
"routes, skipping removal") |
| 301 |
return |
| 302 |
|
| 303 |
remote_endpoint, dataplane_id = ip_dpid_from_route(old_route) |
| 304 |
self.log.info("Cleaning up dataplane for ingress replication " |
| 305 |
"destination %s", remote_endpoint) |
| 306 |
|
| 307 |
self.dataplane.remove_dataplane_for_bum_endpoint( |
| 308 |
remote_endpoint, dataplane_id, old_route.nlri) |
| 309 |
else: |
| 310 |
self.log.warning("unsupported entry_class: %s", |
| 311 |
entry_class.__name__) |
| 312 |
|
| 313 |
# Looking Glass #### |
| 314 |
|
| 315 |
def get_lg_local_info(self, path_prefix): |
| 316 |
if not self.gw_port: |
| 317 |
return {"gateway_port": None} |
| 318 |
else: |
| 319 |
(linuxif, ipvpn) = self.gw_port |
| 320 |
return {"gateway_port": { |
| 321 |
"interface": repr(linuxif), |
| 322 |
"ipvpn": {"href": |
| 323 |
lg.get_absolute_path( |
| 324 |
"VPN_INSTANCES", path_prefix, |
| 325 |
[ipvpn.external_instance_id]), |
| 326 |
"id": ipvpn.name, |
| 327 |
"external_instance_id": |
| 328 |
ipvpn.external_instance_id |
| 329 |
}, |
| 330 |
}} |