| 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 errno |
| 16 |
import socket |
| 17 |
|
| 18 |
import netaddr |
| 19 |
from oslo_config import cfg |
| 20 |
from oslo_serialization import jsonutils |
| 21 |
import pyroute2 |
| 22 |
from pyroute2 import common as pr_common # pylint: disable=no-name-in-module |
| 23 |
from pyroute2 import netlink # pylint: disable=no-name-in-module |
| 24 |
|
| 25 |
from networking_bagpipe._i18n import _ |
| 26 |
|
| 27 |
from networking_bagpipe.bagpipe_bgp.common import log_decorator |
| 28 |
from networking_bagpipe.bagpipe_bgp.common import looking_glass as lg |
| 29 |
from networking_bagpipe.bagpipe_bgp import constants as consts |
| 30 |
from networking_bagpipe.bagpipe_bgp.engine import exa |
| 31 |
from networking_bagpipe.bagpipe_bgp.vpn import dataplane_drivers as dp_drivers |
| 32 |
from networking_bagpipe.privileged import privileged_utils |
| 33 |
|
| 34 |
|
| 35 |
ipr = pyroute2.IPRoute() # pylint: disable=no-member |
| 36 |
|
| 37 |
VRF_INTERFACE_PREFIX = "bvrf-" |
| 38 |
|
| 39 |
RT_TABLE_BASE = 1000 |
| 40 |
|
| 41 |
RT_PROT_BAGPIPE = 19 |
| 42 |
|
| 43 |
|
| 44 |
# NOTE(tmorin): can this be removed ? (is jsonutils to_primitive() enough?) |
| 45 |
def json_set_default(obj): |
| 46 |
if isinstance(obj, set): |
| 47 |
return list(obj) |
| 48 |
raise TypeError |
| 49 |
|
| 50 |
|
| 51 |
def sysctl(sysctl_path, val): |
| 52 |
privileged_utils.sysctl(knob='.'.join(sysctl_path,), value=val) |
| 53 |
|
| 54 |
|
| 55 |
def proxy_arp(ifname, enable): |
| 56 |
sysctl(['net', 'ipv4', 'conf', ifname, 'proxy_arp'], int(enable)) |
| 57 |
sysctl(['net', 'ipv4', 'conf', ifname, 'proxy_arp_pvlan'], int(enable)) |
| 58 |
|
| 59 |
|
| 60 |
class MPLSLinuxVRFDataplane(dp_drivers.VPNInstanceDataplane): |
| 61 |
|
| 62 |
def __init__(self, *args, **kwargs): |
| 63 |
super().__init__(*args, **kwargs) |
| 64 |
|
| 65 |
# FIXME: maybe not thread safe ? |
| 66 |
self.ip = self.driver.ip |
| 67 |
|
| 68 |
self.vrf_if = ("%s%d" % (VRF_INTERFACE_PREFIX, |
| 69 |
self.instance_id))[:consts.LINUX_DEV_LEN] |
| 70 |
|
| 71 |
self.rt_table = RT_TABLE_BASE + self.instance_id |
| 72 |
|
| 73 |
self.log.info("Initializing VRF interface %s", self.vrf_if) |
| 74 |
|
| 75 |
self.flush_routes() |
| 76 |
|
| 77 |
self.log.debug("Creating VRF interface...") |
| 78 |
|
| 79 |
# Create VRF interface |
| 80 |
with self.ip.create(kind='vrf', |
| 81 |
ifname=self.vrf_if, |
| 82 |
vrf_table=self.rt_table, |
| 83 |
reuse=True) as i: |
| 84 |
i.up() |
| 85 |
|
| 86 |
self.vrf_if_idx = i.index |
| 87 |
|
| 88 |
# Create ip rule for VRF route table |
| 89 |
# TODO(tmorin): do in IPDB, when possible (check: what if |
| 90 |
# rule exist, but is not known by IPDB?) |
| 91 |
for family in socket.AF_INET, socket.AF_INET6: |
| 92 |
ipr.flush_rules(family=family, |
| 93 |
iifname=self.vrf_if) |
| 94 |
ipr.flush_rules(family=family, |
| 95 |
oifname=self.vrf_if) |
| 96 |
|
| 97 |
ipr.rule('add', family=family, |
| 98 |
priority=100, iifname=self.vrf_if, table=self.rt_table, |
| 99 |
action='FR_ACT_TO_TBL') |
| 100 |
ipr.rule('add', family=family, |
| 101 |
priority=100, oifname=self.vrf_if, table=self.rt_table, |
| 102 |
|
| 103 |
action='FR_ACT_TO_TBL') |
| 104 |
|
| 105 |
# if VRF traffic does not match any route, |
| 106 |
# lookups must *not* fallback to main/default |
| 107 |
# routing table |
| 108 |
ipr.rule('add', family=family, |
| 109 |
priority=101, iifname=self.vrf_if, |
| 110 |
action='FR_ACT_UNREACHABLE') |
| 111 |
ipr.rule('add', family=family, |
| 112 |
priority=101, oifname=self.vrf_if, |
| 113 |
action='FR_ACT_UNREACHABLE') |
| 114 |
|
| 115 |
def add_route(self, route): |
| 116 |
route.update({'proto': RT_PROT_BAGPIPE}) |
| 117 |
self.ip.routes.add(route).commit() |
| 118 |
|
| 119 |
def flush_routes(self): |
| 120 |
ipr.flush_routes(table=self.rt_table) |
| 121 |
|
| 122 |
@log_decorator.log_info |
| 123 |
def cleanup(self): |
| 124 |
# bring down and disconnect all interfaces from vrf interface |
| 125 |
with self.ip.interfaces[self.vrf_if_idx] as vrf: |
| 126 |
for interface in vrf.ports: |
| 127 |
with self.ip.interfaces[interface] as i: |
| 128 |
i.down() |
| 129 |
vrf.del_port(interface) |
| 130 |
vrf.remove() |
| 131 |
ipr.flush_rules(iifname=self.vrf_if) |
| 132 |
ipr.flush_rules(oifname=self.vrf_if) |
| 133 |
self.flush_routes() |
| 134 |
|
| 135 |
@log_decorator.log_info |
| 136 |
def vif_plugged(self, mac_address, ip_address, localport, label, |
| 137 |
direction): |
| 138 |
|
| 139 |
interface = localport['linuxif'] |
| 140 |
|
| 141 |
if interface not in self.ip.interfaces: |
| 142 |
self.log.warning("interface %s not in interfaces, ignoring plug", |
| 143 |
interface) |
| 144 |
return |
| 145 |
|
| 146 |
# ip link set dev localport master vrf_interface |
| 147 |
with self.ip.interfaces[self.vrf_if_idx] as vrf: |
| 148 |
vrf.add_port(interface) |
| 149 |
|
| 150 |
with self.ip.interfaces[interface] as i: |
| 151 |
i.up() |
| 152 |
|
| 153 |
# add static ARP entry toward interface (because we can) |
| 154 |
ipr.neigh('add', |
| 155 |
dst=ip_address, |
| 156 |
lladdr=mac_address, |
| 157 |
ifindex=self.ip.interfaces[interface].index, |
| 158 |
state=netlink.rtnl.ndmsg.states['permanent']) |
| 159 |
|
| 160 |
# Setup ARP proxying |
| 161 |
proxy_arp(interface, True) |
| 162 |
|
| 163 |
# Configure gateway address on this port |
| 164 |
# FIXME: that would need to be per vif port |
| 165 |
# Retrieve broadcast IP address |
| 166 |
broadcast_ip = str(netaddr.IPNetwork("{}/{}".format(self.gateway_ip, |
| 167 |
self.network_plen) |
| 168 |
).broadcast) |
| 169 |
|
| 170 |
try: |
| 171 |
ipr.addr('add', index=self.ip.interfaces[interface].index, |
| 172 |
address=self.gateway_ip, mask=self.network_plen, |
| 173 |
broadcast=broadcast_ip) |
| 174 |
except netlink.exceptions.NetlinkError as x: |
| 175 |
if x.code == errno.EEXIST: |
| 176 |
# the route already exists, fine |
| 177 |
self.log.debug("route %s already exists on %s", |
| 178 |
self.gateway_ip, |
| 179 |
interface) |
| 180 |
else: |
| 181 |
raise |
| 182 |
|
| 183 |
# Configure mapping for incoming MPLS traffic |
| 184 |
# with this port label |
| 185 |
req = {'family': pr_common.AF_MPLS, |
| 186 |
'oif': self.ip.interfaces[interface].index, |
| 187 |
'dst': label, # FIXME how to check for BoS? |
| 188 |
'via': {'family': socket.AF_INET, |
| 189 |
'addr': ip_address} |
| 190 |
} |
| 191 |
|
| 192 |
try: |
| 193 |
self.add_route(req) |
| 194 |
except netlink.exceptions.NetlinkError as x: |
| 195 |
if x.code == errno.EEXIST: |
| 196 |
# the route already exists, fine |
| 197 |
self.log.warning("MPLS state for %d already exists", label) |
| 198 |
else: |
| 199 |
raise |
| 200 |
|
| 201 |
@log_decorator.log_info |
| 202 |
def vif_unplugged(self, mac_address, ip_address, localport, label, |
| 203 |
direction, last_endpoint=True): |
| 204 |
interface = localport['linuxif'] |
| 205 |
|
| 206 |
if interface not in self.ip.interfaces: |
| 207 |
self.log.warning("interface %s not in interfaces, ignoring plug", |
| 208 |
interface) |
| 209 |
return |
| 210 |
|
| 211 |
# bring the interface down, we don't want |
| 212 |
# traffic from this interface to leak out of the VRF |
| 213 |
with self.ip.interfaces[interface] as i: |
| 214 |
i.down() |
| 215 |
|
| 216 |
# Disable ARP proxying |
| 217 |
proxy_arp(interface, False) |
| 218 |
|
| 219 |
# ip link set dev localport master vrf_interface |
| 220 |
with self.ip.interfaces[self.vrf_if_idx] as i: |
| 221 |
i.del_port(interface) |
| 222 |
|
| 223 |
# Unconfigure gateway address on this port |
| 224 |
# FIXME: that would need to be per vif port |
| 225 |
# Retrieve broadcast IP address |
| 226 |
ip = netaddr.IPNetwork( |
| 227 |
"{}/{}".format(self.gateway_ip, self.network_plen)) |
| 228 |
broadcast_ip = str(ip.broadcast) |
| 229 |
|
| 230 |
ipr.addr('del', index=self.ip.interfaces[interface].index, |
| 231 |
address=self.gateway_ip, mask=self.network_plen, |
| 232 |
broadcast=broadcast_ip) |
| 233 |
|
| 234 |
with self.ip.routes.tables['mpls'][label] as r: |
| 235 |
r.remove() |
| 236 |
|
| 237 |
def _read_mpls_in(self, label): |
| 238 |
routes = [r for r in self.ip.routes.tables["mpls"] |
| 239 |
if r['dst'] == label] |
| 240 |
assert len(routes) == 1 |
| 241 |
res = (routes[0]['oif'], routes[0]['via']['addr']) |
| 242 |
self.log.debug("Found %s for %d with IPDB", res, label) |
| 243 |
return res |
| 244 |
|
| 245 |
def _nh(self, remote_pe, label, encaps): |
| 246 |
mpls = True |
| 247 |
if str(remote_pe) == self.driver.get_local_address(): |
| 248 |
# FIXME: does not work yet, from this table, |
| 249 |
# 'gateway' is considered unreachable |
| 250 |
# we could drop 'gateway' and just keep oif |
| 251 |
# but this would only work for connected routes |
| 252 |
# if remote_pe is ourselves |
| 253 |
# we lookup the route for the label and deliver directly |
| 254 |
# to this oif/gateway |
| 255 |
# (oif, gateway) = self._read_mpls_in(label) |
| 256 |
# mpls = False |
| 257 |
gateway = '127.0.0.1' |
| 258 |
oif = self.ip.interfaces['lo'].index |
| 259 |
else: |
| 260 |
gateway = remote_pe |
| 261 |
oif = self.driver.mpls_interface_index |
| 262 |
|
| 263 |
nh = {'oif': oif} |
| 264 |
if gateway: |
| 265 |
nh['gateway'] = gateway |
| 266 |
|
| 267 |
if mpls: |
| 268 |
nh['encap'] = {'type': 'mpls', |
| 269 |
'labels': [{'bos': 1, |
| 270 |
'label': label}]} |
| 271 |
self.log.debug("nh: %s", nh) |
| 272 |
return nh |
| 273 |
|
| 274 |
def _get_route(self, prefix): |
| 275 |
return self.ip.routes.tables[self.rt_table][prefix] |
| 276 |
|
| 277 |
@log_decorator.log_info |
| 278 |
def setup_dataplane_for_remote_endpoint(self, prefix, remote_pe, label, |
| 279 |
nlri, encaps, |
| 280 |
lb_consistent_hash_order=0): |
| 281 |
prefix = str(prefix) |
| 282 |
|
| 283 |
if prefix == "0.0.0.0/0": |
| 284 |
prefix = 'default' |
| 285 |
|
| 286 |
try: |
| 287 |
with self._get_route(prefix) as r: |
| 288 |
self.log.debug("a route to %s already exists, adding nexthop", |
| 289 |
prefix) |
| 290 |
r.add_nh(self._nh(remote_pe, label, encaps)) |
| 291 |
except KeyError: |
| 292 |
self.log.debug("no route to %s yet, creating", prefix) |
| 293 |
req = {'table': self.rt_table, |
| 294 |
'dst': prefix, |
| 295 |
'multipath': [self._nh(remote_pe, label, encaps)]} |
| 296 |
self.log.debug("adding route: %s", req) |
| 297 |
self.add_route(req) |
| 298 |
|
| 299 |
@log_decorator.log_info |
| 300 |
def remove_dataplane_for_remote_endpoint(self, prefix, remote_pe, label, |
| 301 |
nlri, encaps, |
| 302 |
lb_consistent_hash_order=0): |
| 303 |
prefix = str(prefix) |
| 304 |
|
| 305 |
if prefix == "0.0.0.0/0": |
| 306 |
prefix = 'default' |
| 307 |
|
| 308 |
try: |
| 309 |
with self._get_route(prefix) as r: |
| 310 |
# FIXME: encap info is missing here |
| 311 |
if r['multipath']: |
| 312 |
r.del_nh(self._nh(remote_pe, label, None)) |
| 313 |
else: # last route |
| 314 |
r.remove() |
| 315 |
|
| 316 |
except KeyError: |
| 317 |
self.log.warning("no route found on remove_dataplane_for" |
| 318 |
"_remote_endpoint for %s", prefix) |
| 319 |
|
| 320 |
# Looking Glass ## |
| 321 |
|
| 322 |
def get_lg_map(self): |
| 323 |
return {"routes": (lg.SUBTREE, self.get_lg_routes), |
| 324 |
"route_table": (lg.VALUE, self.rt_table), |
| 325 |
"vrf_if": (lg.VALUE, self.vrf_if), |
| 326 |
} |
| 327 |
|
| 328 |
@log_decorator.log_info |
| 329 |
def get_lg_routes(self, path_prefix): |
| 330 |
routes = self.ip.routes.tables[self.rt_table] |
| 331 |
return [{r['dst']: |
| 332 |
jsonutils.loads(jsonutils.dumps(r, default=json_set_default))} |
| 333 |
for r in routes] |
| 334 |
|
| 335 |
|
| 336 |
class MPLSLinuxDataplaneDriver(dp_drivers.DataplaneDriver): |
| 337 |
|
| 338 |
"""Dataplane driver relying on the MPLS stack in the Linux kernel |
| 339 |
|
| 340 |
This dataplane driver relies on the MPLS stack in the Linux kernel, |
| 341 |
and on linux vrf interfaces. |
| 342 |
""" |
| 343 |
|
| 344 |
required_kernel = "4.4" |
| 345 |
dataplane_instance_class = MPLSLinuxVRFDataplane |
| 346 |
type = consts.IPVPN |
| 347 |
ecmp_support = True |
| 348 |
|
| 349 |
driver_opts = [ |
| 350 |
cfg.StrOpt("mpls_interface", |
| 351 |
help=("Interface used to send/receive MPLS traffic. " |
| 352 |
"Use '*gre*' to choose automatic creation of a tunnel" |
| 353 |
" interface for MPLS/GRE encap")), |
| 354 |
] |
| 355 |
|
| 356 |
def __init__(self): |
| 357 |
super().__init__() |
| 358 |
|
| 359 |
privileged_utils.modprobe('mpls_router') |
| 360 |
privileged_utils.modprobe('mpls_gso') |
| 361 |
privileged_utils.modprobe('mpls_iptunnel') |
| 362 |
privileged_utils.modprobe('vrf') |
| 363 |
|
| 364 |
self.ip = pyroute2.IPDB() # pylint: disable=no-member |
| 365 |
|
| 366 |
@log_decorator.log_info |
| 367 |
def reset_state(self): |
| 368 |
# remove all VRF interfaces |
| 369 |
for itf in self.ip.interfaces.keys(): |
| 370 |
if isinstance(itf, str) and itf.startswith(VRF_INTERFACE_PREFIX): |
| 371 |
# bring the interface vrf slave interfaces down, |
| 372 |
# we don't want traffic from these interfaces |
| 373 |
# to leak out of the VRF after removal of VRF interface |
| 374 |
for index in self.ip.interfaces[itf].ports: |
| 375 |
with self.ip.interfaces[index] as port: |
| 376 |
port.down() |
| 377 |
|
| 378 |
ipr.link('del', ifname=itf) |
| 379 |
# Flush all routes setup by us in past runs |
| 380 |
ipr.flush_routes(proto=RT_PROT_BAGPIPE) |
| 381 |
# Flush all MPLS routes redirecting traffic to network namespaces |
| 382 |
# (just in case, should be covered by the above) |
| 383 |
ipr.flush_routes(family=pr_common.AF_MPLS) |
| 384 |
|
| 385 |
@log_decorator.log_info |
| 386 |
def initialize(self): |
| 387 |
sysctl('net.mpls.platform_labels', 2 ** 20 - 1) |
| 388 |
|
| 389 |
if "*gre*" in self.config["mpls_interface"]: |
| 390 |
self.mpls_interface = "gre_wildcard" |
| 391 |
raise Exception(_("MPLS/GRE not supported yet")) |
| 392 |
else: |
| 393 |
self.mpls_interface = self.config["mpls_interface"] |
| 394 |
|
| 395 |
sysctl('net.mpls.conf.%s.input' % self.mpls_interface, 1) |
| 396 |
|
| 397 |
self.mpls_interface_index = self.ip.interfaces[self.mpls_interface |
| 398 |
].index |
| 399 |
# for traffic from ourselves: |
| 400 |
sysctl('net.mpls.conf.lo.input', 1) |
| 401 |
|
| 402 |
# enable forwarding |
| 403 |
sysctl('net.ipv4.ip_forward', 1) |
| 404 |
|
| 405 |
def supported_encaps(self): |
| 406 |
yield exa.Encapsulation(exa.Encapsulation.Type.MPLS) |
| 407 |
# we also accept route with no encap specified |
| 408 |
yield exa.Encapsulation(exa.Encapsulation.Type.DEFAULT) |
| 409 |
|
| 410 |
# Looking glass #### |
| 411 |
|
| 412 |
def get_lg_map(self): |
| 413 |
return {"mpls": (lg.SUBTREE, self.get_lg_mpls_routes), |
| 414 |
} |
| 415 |
|
| 416 |
def get_lg_mpls_routes(self, path_prefix): |
| 417 |
return [{r['dst']: |
| 418 |
jsonutils.loads(jsonutils.dumps(r, default=json_set_default))} |
| 419 |
for r in self.ip.routes.tables['mpls']] |