| 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 |
from oslo_log import log as logging |
| 16 |
from pyroute2 import ndb as ndb_mod # pylint: disable=no-name-in-module |
| 17 |
|
| 18 |
from networking_bagpipe.bagpipe_bgp.common import log_decorator |
| 19 |
from networking_bagpipe.bagpipe_bgp import constants as consts |
| 20 |
from networking_bagpipe.bagpipe_bgp.vpn import evpn |
| 21 |
from networking_bagpipe.privileged import privileged_utils |
| 22 |
|
| 23 |
BRIDGE_NAME_PREFIX = "evpn---" |
| 24 |
VXLAN_INTERFACE_PREFIX = "vxlan--" |
| 25 |
LOG = logging.getLogger(__name__) |
| 26 |
|
| 27 |
|
| 28 |
class LinuxVXLANEVIDataplane(evpn.VPNInstanceDataplane): |
| 29 |
|
| 30 |
def __init__(self, *args, **kwargs): |
| 31 |
super().__init__(*args, **kwargs) |
| 32 |
|
| 33 |
if 'linuxbr' in kwargs: |
| 34 |
self.bridge_name = kwargs.get('linuxbr') |
| 35 |
else: |
| 36 |
self.bridge_name = ( |
| 37 |
BRIDGE_NAME_PREFIX + |
| 38 |
self.external_instance_id)[:consts.LINUX_DEV_LEN] |
| 39 |
|
| 40 |
if not self._interface_exists(self.bridge_name): |
| 41 |
self.log.debug("Starting bridge %s", self.bridge_name) |
| 42 |
|
| 43 |
# Create bridge |
| 44 |
privileged_utils.brctl('addbr %s' % self.bridge_name) |
| 45 |
privileged_utils.brctl('setfd %s 0' % self.bridge_name) |
| 46 |
privileged_utils.brctl('stp %s off' % self.bridge_name) |
| 47 |
self._run_command("ip link set %s up" % self.bridge_name, |
| 48 |
run_as_root=True) |
| 49 |
|
| 50 |
self.log.debug("Bridge %s created", self.bridge_name) |
| 51 |
|
| 52 |
self._create_and_plug_vxlan_if() |
| 53 |
|
| 54 |
self.log.debug("VXLAN interface %s plugged on bridge %s", |
| 55 |
self.vxlan_if_name, self.bridge_name) |
| 56 |
|
| 57 |
self._cleaning_up = False |
| 58 |
|
| 59 |
@log_decorator.log_info |
| 60 |
def cleanup(self): |
| 61 |
self.log.info("Cleaning EVI bridge and VXLAN interface %s", |
| 62 |
self.bridge_name) |
| 63 |
|
| 64 |
self._cleaning_up = True |
| 65 |
|
| 66 |
# removing the vxlan interface removes our routes, |
| 67 |
# but if we don't remove the vxlan if (if it was reused) then |
| 68 |
# cleanup will not happen, which is why we use cleanup assist |
| 69 |
# (see needs cleanup assist below) |
| 70 |
self._cleanup_vxlan_if() |
| 71 |
|
| 72 |
# Delete only EVPN Bridge (Created by dataplane driver) |
| 73 |
if BRIDGE_NAME_PREFIX in self.bridge_name: |
| 74 |
self._run_command("ip link set %s down" % self.bridge_name, |
| 75 |
run_as_root=True, |
| 76 |
raise_on_error=False) |
| 77 |
privileged_utils.brctl('delbr %s' % self.bridge_name, |
| 78 |
check_exit=False) |
| 79 |
|
| 80 |
def needs_cleanup_assist(self): |
| 81 |
# If we reused a vxlan interface we won't cleanup fdb entries |
| 82 |
# in cleanup(), so we need to have remove_dataplane_for_x |
| 83 |
# be called for reach state via cleanup assist |
| 84 |
return VXLAN_INTERFACE_PREFIX not in self.vxlan_if_name |
| 85 |
|
| 86 |
def _create_and_plug_vxlan_if(self): |
| 87 |
# if a VXLAN interface, with the VNI we want to use, is already plugged |
| 88 |
# in the bridge, we want to reuse it |
| 89 |
with ndb_mod.main.NDB() as ndb: |
| 90 |
# pylint: disable=no-member |
| 91 |
for port_id in ndb.interfaces[self.bridge_name].ports: |
| 92 |
port = ndb.interfaces[port_id] # pylint: disable=no-member |
| 93 |
if (port['kind'] == "vxlan" and |
| 94 |
port['vxlan_id'] == self.instance_label): |
| 95 |
self.log.info("reuse vxlan interface %s for VXLAN VNI %s", |
| 96 |
port['ifname'], self.instance_label) |
| 97 |
self.vxlan_if_name = port['ifname'] |
| 98 |
return |
| 99 |
|
| 100 |
self.vxlan_if_name = (VXLAN_INTERFACE_PREFIX + |
| 101 |
self.external_instance_id)[:consts.LINUX_DEV_LEN] |
| 102 |
|
| 103 |
self.log.debug("Creating and plugging VXLAN interface %s", |
| 104 |
self.vxlan_if_name) |
| 105 |
|
| 106 |
if self._interface_exists(self.vxlan_if_name): |
| 107 |
self._remove_vxlan_if() |
| 108 |
|
| 109 |
dst_port_spec = "" |
| 110 |
if self.driver.config.vxlan_dst_port: |
| 111 |
dst_port_spec = ("dstport %d" % |
| 112 |
self.driver.config.vxlan_dst_port) |
| 113 |
|
| 114 |
# Create VXLAN interface |
| 115 |
self._run_command( |
| 116 |
"ip link add %s type vxlan id %d local %s nolearning proxy %s" % |
| 117 |
(self.vxlan_if_name, self.instance_label, |
| 118 |
self.driver.get_local_address(), dst_port_spec), |
| 119 |
run_as_root=True |
| 120 |
) |
| 121 |
|
| 122 |
self._run_command("ip link set %s up" % self.vxlan_if_name, |
| 123 |
run_as_root=True) |
| 124 |
|
| 125 |
# Plug VXLAN interface into bridge |
| 126 |
privileged_utils.brctl('addif {} {}'.format(self.bridge_name, |
| 127 |
self.vxlan_if_name)) |
| 128 |
|
| 129 |
def _cleanup_vxlan_if(self): |
| 130 |
if VXLAN_INTERFACE_PREFIX not in self.vxlan_if_name: |
| 131 |
self.log.debug("we reused the VXLAN interface, don't cleanup") |
| 132 |
return |
| 133 |
|
| 134 |
if self._is_vxlan_if_on_bridge(): |
| 135 |
# Unplug VXLAN interface from Linux bridge |
| 136 |
self._unplug_from_bridge(self.vxlan_if_name) |
| 137 |
|
| 138 |
self._remove_vxlan_if() |
| 139 |
|
| 140 |
def _remove_vxlan_if(self): |
| 141 |
if not VXLAN_INTERFACE_PREFIX not in self.vxlan_if_name: |
| 142 |
self.log.debug("we reused the VXLAN interface, don't remove") |
| 143 |
return |
| 144 |
|
| 145 |
# Remove VXLAN interface |
| 146 |
self._run_command("ip link set %s down" % self.vxlan_if_name, |
| 147 |
run_as_root=True) |
| 148 |
self._run_command("ip link del %s" % self.vxlan_if_name, |
| 149 |
run_as_root=True) |
| 150 |
|
| 151 |
def _is_if_on_bridge(self, ifname): |
| 152 |
with ndb_mod.main.NDB() as ndb: |
| 153 |
try: |
| 154 |
# pylint: disable=no-member |
| 155 |
for port_id in ndb.interfaces[self.bridge_name].ports: |
| 156 |
port = ndb.interfaces[port_id] # pylint: disable=no-member |
| 157 |
if port.ifname == ifname: |
| 158 |
return True |
| 159 |
except KeyError: |
| 160 |
return False |
| 161 |
return False |
| 162 |
|
| 163 |
def _is_vxlan_if_on_bridge(self): |
| 164 |
return self._is_if_on_bridge(self.vxlan_if_name) |
| 165 |
|
| 166 |
def _interface_exists(self, interface): |
| 167 |
"""Check if interface exists.""" |
| 168 |
(_, exit_code) = self._run_command("ip link show dev %s" % interface, |
| 169 |
raise_on_error=False, |
| 170 |
acceptable_return_codes=[-1]) |
| 171 |
return (exit_code == 0) |
| 172 |
|
| 173 |
def _unplug_from_bridge(self, interface): |
| 174 |
if self._interface_exists(self.bridge_name): |
| 175 |
privileged_utils.brctl('delif {} {}'.format(self.bridge_name, |
| 176 |
interface), |
| 177 |
check_exit=[0, 1]) |
| 178 |
|
| 179 |
def set_gateway_port(self, linuxif, gw_ip): |
| 180 |
privileged_utils.brctl('addif {} {}'.format(self.bridge_name, linuxif), |
| 181 |
check_exit=False) |
| 182 |
|
| 183 |
self._fdb_dump() |
| 184 |
|
| 185 |
def gateway_port_down(self, linuxif): |
| 186 |
privileged_utils.brctl('delif {} {}'.format(self.bridge_name, |
| 187 |
linuxif), |
| 188 |
check_exit=False) |
| 189 |
# TODO(tmorin): need to cleanup bridge fdb and ip neigh ? |
| 190 |
|
| 191 |
def set_bridge_name(self, linuxbr): |
| 192 |
self.bridge_name = linuxbr |
| 193 |
|
| 194 |
@log_decorator.log_info |
| 195 |
def vif_plugged(self, mac_address, ip_address, localport, dpid, direction): |
| 196 |
# Plug localport only if bridge was created by us |
| 197 |
if BRIDGE_NAME_PREFIX in self.bridge_name: |
| 198 |
self.log.debug("Plugging localport %s into EVPN bridge %s", |
| 199 |
localport['linuxif'], self.bridge_name) |
| 200 |
privileged_utils.brctl( |
| 201 |
'addif {} {}'.format(self.bridge_name, localport['linuxif']), |
| 202 |
check_exit=False) |
| 203 |
|
| 204 |
privileged_utils.bridge( |
| 205 |
'fdb replace {} dev {}'.format(mac_address, localport['linuxif'])) |
| 206 |
|
| 207 |
self._fdb_dump() |
| 208 |
|
| 209 |
@log_decorator.log_info |
| 210 |
def vif_unplugged(self, mac_address, ip_address, localport, dpid, |
| 211 |
direction, last_endpoint=True): |
| 212 |
|
| 213 |
# remove local fdb entry, but only if tap interface is still here |
| 214 |
if self._is_if_on_bridge(localport['linuxif']): |
| 215 |
privileged_utils.bridge( |
| 216 |
'fdb delete {} dev {}'.format(mac_address, |
| 217 |
localport['linuxif'])) |
| 218 |
|
| 219 |
# unplug localport only if bridge was created by us |
| 220 |
if BRIDGE_NAME_PREFIX in self.bridge_name: |
| 221 |
self.log.debug("Unplugging localport %s from EVPN bridge %s", |
| 222 |
localport['linuxif'], self.bridge_name) |
| 223 |
self._unplug_from_bridge(localport['linuxif']) |
| 224 |
|
| 225 |
self._fdb_dump() |
| 226 |
|
| 227 |
@log_decorator.log |
| 228 |
def setup_dataplane_for_remote_endpoint(self, prefix, remote_pe, dpid, |
| 229 |
nlri, encaps): |
| 230 |
if self._cleaning_up: |
| 231 |
self.log.debug("setup_dataplane_for_remote_endpoint: instance" |
| 232 |
" cleaning up, do nothing") |
| 233 |
return |
| 234 |
|
| 235 |
mac = prefix |
| 236 |
ip = nlri.ip |
| 237 |
vni = dpid |
| 238 |
|
| 239 |
# populate bridge forwarding db |
| 240 |
privileged_utils.bridge( |
| 241 |
'fdb replace %s dev %s dst %s vni %s' % |
| 242 |
(mac, self.vxlan_if_name, remote_pe, vni)) |
| 243 |
|
| 244 |
# populate ARP cache |
| 245 |
if ip is not None: |
| 246 |
self._run_command("ip neighbor replace %s lladdr %s dev %s nud " |
| 247 |
"permanent" % (ip, mac, self.vxlan_if_name), |
| 248 |
run_as_root=True) |
| 249 |
else: |
| 250 |
self.log.trace("No IP in E-VPN route, no ARP proxy for %s" % mac) |
| 251 |
|
| 252 |
self._fdb_dump() |
| 253 |
|
| 254 |
@log_decorator.log |
| 255 |
def remove_dataplane_for_remote_endpoint(self, prefix, remote_pe, dpid, |
| 256 |
nlri): |
| 257 |
if self._cleaning_up: |
| 258 |
self.log.debug("setup_dataplane_for_remote_endpoint: instance" |
| 259 |
" cleaning up, do nothing") |
| 260 |
return |
| 261 |
|
| 262 |
mac = prefix |
| 263 |
ip = nlri.ip |
| 264 |
vni = dpid |
| 265 |
|
| 266 |
self._fdb_dump() |
| 267 |
|
| 268 |
# clear ARP proxy |
| 269 |
if ip is not None: |
| 270 |
self._run_command("ip neighbor del %s lladdr %s dev %s nud " |
| 271 |
"permanent" % (ip, mac, self.vxlan_if_name), |
| 272 |
run_as_root=True) |
| 273 |
|
| 274 |
privileged_utils.bridge( |
| 275 |
'fdb del %s dev %s dst %s vni %s' % |
| 276 |
(mac, self.vxlan_if_name, remote_pe, vni)) |
| 277 |
|
| 278 |
self._fdb_dump() |
| 279 |
|
| 280 |
@log_decorator.log |
| 281 |
def add_dataplane_for_bum_endpoint(self, remote_pe, dpid, nlri, encaps): |
| 282 |
if self._cleaning_up: |
| 283 |
self.log.debug("setup_dataplane_for_remote_endpoint: instance" |
| 284 |
" cleaning up, do nothing") |
| 285 |
return |
| 286 |
|
| 287 |
vni = dpid |
| 288 |
|
| 289 |
# 00:00:00:00:00 usable as default since kernel commit |
| 290 |
# 58e4c767046a35f11a55af6ce946054ddf4a8580 (2013-06-25) |
| 291 |
privileged_utils.bridge( |
| 292 |
'fdb append 00:00:00:00:00:00 dev %s dst %s vni %s' % |
| 293 |
(self.vxlan_if_name, remote_pe, vni)) |
| 294 |
|
| 295 |
self._fdb_dump() |
| 296 |
|
| 297 |
@log_decorator.log |
| 298 |
def remove_dataplane_for_bum_endpoint(self, remote_pe, dpid, nlri): |
| 299 |
if self._cleaning_up: |
| 300 |
self.log.debug("setup_dataplane_for_remote_endpoint: instance" |
| 301 |
" cleaning up, do nothing") |
| 302 |
return |
| 303 |
|
| 304 |
vni = dpid |
| 305 |
|
| 306 |
self._fdb_dump() |
| 307 |
|
| 308 |
privileged_utils.bridge( |
| 309 |
'fdb delete 00:00:00:00:00:00 dev %s dst %s vni %s' % |
| 310 |
(self.vxlan_if_name, remote_pe, vni)) |
| 311 |
|
| 312 |
self._fdb_dump() |
| 313 |
|
| 314 |
def _fdb_dump(self): |
| 315 |
if self.log.isEnabledFor(logging.DEBUG): |
| 316 |
self.log.debug("bridge fdb dump: %s", self._run_command( |
| 317 |
"fdb show br %s" % self.bridge_name, |
| 318 |
acceptable_return_codes=[0, 255], |
| 319 |
run_as_root=True)[0]) |
| 320 |
|
| 321 |
# Looking glass #### |
| 322 |
|
| 323 |
def get_lg_local_info(self, path_prefix): |
| 324 |
return { |
| 325 |
"linux_bridge": self.bridge_name, |
| 326 |
"vxlan_if": self.vxlan_if_name |
| 327 |
} |