| 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 re |
| 16 |
|
| 17 |
import collections |
| 18 |
import netaddr |
| 19 |
from oslo_config import cfg |
| 20 |
from oslo_config import types |
| 21 |
from oslo_log import log as logging |
| 22 |
from oslo_utils import versionutils |
| 23 |
|
| 24 |
from neutron.common import utils as n_utils |
| 25 |
|
| 26 |
from networking_bagpipe._i18n import _ |
| 27 |
from networking_bagpipe.bagpipe_bgp.common import config |
| 28 |
from networking_bagpipe.bagpipe_bgp.common import dataplane_utils |
| 29 |
from networking_bagpipe.bagpipe_bgp.common import exceptions as exc |
| 30 |
from networking_bagpipe.bagpipe_bgp.common import log_decorator |
| 31 |
from networking_bagpipe.bagpipe_bgp.common import looking_glass as lg |
| 32 |
from networking_bagpipe.bagpipe_bgp.common import net_utils |
| 33 |
from networking_bagpipe.bagpipe_bgp import constants as consts |
| 34 |
from networking_bagpipe.bagpipe_bgp.engine import exa |
| 35 |
from networking_bagpipe.bagpipe_bgp.vpn import dataplane_drivers as dp_drivers |
| 36 |
from networking_bagpipe.bagpipe_bgp.vpn import identifier_allocators |
| 37 |
from networking_bagpipe.bagpipe_bgp.vpn import vpn_instance |
| 38 |
|
| 39 |
from neutron.agent.common import ovs_lib |
| 40 |
|
| 41 |
from neutron_lib.plugins.ml2 import ovs_constants as ovs_const |
| 42 |
|
| 43 |
# man ovs-ofctl /32768 |
| 44 |
DEFAULT_OVS_FLOW_PRIORITY = 0x8000 |
| 45 |
|
| 46 |
# we want to avoid having our flows having a lowest priority than |
| 47 |
# the default |
| 48 |
DEFAULT_RULE_PRIORITY = DEFAULT_OVS_FLOW_PRIORITY + 0x1000 |
| 49 |
|
| 50 |
# priorities for IP match flows |
| 51 |
# highest priority MAX_PREFIX_PRIORITY for MAX_PREFIX_LENGTH prefix |
| 52 |
# (MAX_PREFIX_PRIORITY - MAX_PREFIX_LENGTH) for a zero length prefix |
| 53 |
MAX_PREFIX_PRIORITY = DEFAULT_RULE_PRIORITY |
| 54 |
MAX_PREFIX_LENGTH = 0x80 # 128 |
| 55 |
|
| 56 |
# fallback flows get a priority even lower |
| 57 |
# (we round it for better readability of flow dumps) |
| 58 |
FALLBACK_PRIORITY = MAX_PREFIX_PRIORITY - MAX_PREFIX_LENGTH - 0x80 |
| 59 |
|
| 60 |
NO_MPLS_PHY_INTERFACE = -1 |
| 61 |
|
| 62 |
VXLAN_TUNNEL = "vxlan" |
| 63 |
|
| 64 |
OVS_DUMP_FLOW_FILTER = "| grep -v NXST_FLOW | perl -pe '" \ |
| 65 |
"s/ *cookie=[^,]+, duration=[^,]+, table=[^,]+, //;" \ |
| 66 |
"s/ *n_bytes=[^,]+, //; " \ |
| 67 |
"s/ *(hard|idle)_age=[^,]+, //g; " \ |
| 68 |
"s/n_packets=([0-9]),/packets=$1 /; " \ |
| 69 |
"s/n_packets=([0-9]{2}),/packets=$1 /; " \ |
| 70 |
"s/n_packets=([0-9]{3}),/packets=$1 /; " \ |
| 71 |
"s/n_packets=([0-9]+),/packets=$1 /; " \ |
| 72 |
"'" |
| 73 |
|
| 74 |
GATEWAY_MAC = "00:00:5e:00:43:64" |
| 75 |
|
| 76 |
ARP_RESPONDER_ACTIONS = ('move:NXM_OF_ETH_SRC[]->NXM_OF_ETH_DST[],' |
| 77 |
'mod_dl_src:%(mac)s,' |
| 78 |
'load:0x2->NXM_OF_ARP_OP[],' |
| 79 |
'move:NXM_NX_ARP_SHA[]->NXM_NX_ARP_THA[],' |
| 80 |
'push:NXM_OF_ARP_TPA[],' |
| 81 |
'move:NXM_OF_ARP_SPA[]->NXM_OF_ARP_TPA[],' |
| 82 |
'load:%(mac)#x->NXM_NX_ARP_SHA[],' |
| 83 |
'pop:NXM_OF_ARP_SPA[],' |
| 84 |
'%(vlan_action)soutput:%(in_port)d') |
| 85 |
|
| 86 |
VRF_REGISTER = 0 |
| 87 |
|
| 88 |
|
| 89 |
def _match_from_prefix(prefix): |
| 90 |
# A zero-length prefix is a default route, no nw_dst is needed/possible |
| 91 |
# in this case |
| 92 |
prefix_length = netaddr.IPNetwork(prefix).prefixlen |
| 93 |
return {'nw_dst': prefix} if prefix_length != 0 else {} |
| 94 |
|
| 95 |
|
| 96 |
def _priority_from_prefix(prefix): |
| 97 |
# FIXME: use a priority depending on the prefix len |
| 98 |
# to compensate the fact that "OpenFlow leaves behavior |
| 99 |
# undefined when two or more flows with the same priority |
| 100 |
# can match a single packet. Some users expect ``sensible'' |
| 101 |
# behavior, such as more specific flows taking precedence |
| 102 |
# over less specific flows, but OpenFlow does not specify |
| 103 |
# this and Open vSwitch does not implement it. Users should |
| 104 |
# therefore take care to use priorities to ensure the |
| 105 |
# behavior that they expect. |
| 106 |
prefix_length = netaddr.IPNetwork(prefix).prefixlen |
| 107 |
# to implement a longest-match lookup we give longest prefixes |
| 108 |
# the higher priority |
| 109 |
priority = MAX_PREFIX_PRIORITY - (MAX_PREFIX_LENGTH - prefix_length) |
| 110 |
return priority |
| 111 |
|
| 112 |
|
| 113 |
class MPLSOVSVRFDataplane(dp_drivers.VPNInstanceDataplane): |
| 114 |
|
| 115 |
def __init__(self, *args, **kwargs): |
| 116 |
super().__init__(*args, **kwargs) |
| 117 |
|
| 118 |
# Initialize dict where we store info on OVS ports (port numbers and |
| 119 |
# bound IP address) |
| 120 |
self._ovs_port_info = dict() |
| 121 |
|
| 122 |
self.bridge = self.driver.bridge |
| 123 |
|
| 124 |
self.nh_group_mgr = NextHopGroupManagerProxy( |
| 125 |
self.driver.nh_group_mgr, |
| 126 |
self.instance_id, |
| 127 |
self.driver.vrf_table, |
| 128 |
self._vrf_match(), |
| 129 |
self._cookie |
| 130 |
) |
| 131 |
|
| 132 |
self.fallback = None |
| 133 |
self.ovs_vlan = None |
| 134 |
|
| 135 |
@log_decorator.log_info |
| 136 |
def cleanup(self): |
| 137 |
if self._ovs_port_info: |
| 138 |
self.log.warning("OVS port numbers list for local ports plugged in" |
| 139 |
" VRF is not empty, clearing...") |
| 140 |
self._ovs_port_info.clear() |
| 141 |
|
| 142 |
# Remove all flows for this instance |
| 143 |
for table in self.driver.all_tables.values(): |
| 144 |
self.bridge.delete_flows(table=table, |
| 145 |
cookie=self._cookie(add=False)) |
| 146 |
|
| 147 |
# Remove all groups for this instance |
| 148 |
self.nh_group_mgr.clear_objects() |
| 149 |
|
| 150 |
@log_decorator.log |
| 151 |
def _extract_mac_address(self, output): |
| 152 |
"""Extract MAC address from command output""" |
| 153 |
|
| 154 |
return re.search(r"([0-9A-F]{2}[:-]){5}([0-9A-F]{2})", output, |
| 155 |
re.IGNORECASE).group() |
| 156 |
|
| 157 |
def _find_remote_mac_address(self, remote_ip): |
| 158 |
"""Find MAC address for a remote IP address""" |
| 159 |
|
| 160 |
# PING remote IP address |
| 161 |
(_, exit_code) = self._run_command("fping -r4 -t100 -q -I %s %s" % |
| 162 |
(self.bridge.br_name, remote_ip), |
| 163 |
raise_on_error=False, |
| 164 |
acceptable_return_codes=[-1]) |
| 165 |
if exit_code != 0: |
| 166 |
self.log.info("can't ping %s via %s, proceeding anyways", |
| 167 |
remote_ip, self.bridge.br_name) |
| 168 |
# we proceed even if the ping failed, since the ping was |
| 169 |
# just a way to trigger an ARP resolution which may have |
| 170 |
# succeeded even if the ping failed |
| 171 |
|
| 172 |
# Look in ARP cache to find remote MAC address |
| 173 |
(output, _) = self._run_command("ip neigh show to %s dev %s" % |
| 174 |
(remote_ip, self.bridge.br_name)) |
| 175 |
|
| 176 |
if (not output or |
| 177 |
"FAILED" in output[0] or |
| 178 |
"INCOMPLETE" in output[0]): |
| 179 |
raise exc.RemotePEMACAddressNotFound(remote_ip) |
| 180 |
|
| 181 |
try: |
| 182 |
return self._extract_mac_address(output[0]) |
| 183 |
except Exception: |
| 184 |
raise exc.RemotePEMACAddressNotFound(remote_ip) |
| 185 |
|
| 186 |
def _mtu_fixup(self, localport): |
| 187 |
# This is a hack, proper MTUs should actually be configured in the |
| 188 |
# hybrid vif driver |
| 189 |
# TODO(tmorin): obsolete |
| 190 |
|
| 191 |
mtu = self.config["ovsbr_interfaces_mtu"] |
| 192 |
if not mtu: |
| 193 |
self.log.debug("No ovsbr_interfaces_mtu specified in config file," |
| 194 |
" not trying to fixup MTU") |
| 195 |
return |
| 196 |
|
| 197 |
try: |
| 198 |
itf = localport['ovs']['port_name'] |
| 199 |
except KeyError: |
| 200 |
self.log.warning("No OVS port name provided, cannot fix MTU") |
| 201 |
return |
| 202 |
|
| 203 |
self.log.info("Will adjust %s if with MTU %s " |
| 204 |
"(ovsbr_interfaces_mtu specified in config)", itf, mtu) |
| 205 |
|
| 206 |
(_, exit_code) = self._run_command("ip link show %s" % itf, |
| 207 |
raise_on_error=False, |
| 208 |
acceptable_return_codes=[0, 1]) |
| 209 |
|
| 210 |
if exit_code != 0: |
| 211 |
self.log.warning("No %s if, not trying to fix MTU", itf) |
| 212 |
else: |
| 213 |
self._run_command("ip link set {} mtu {}".format(itf, mtu), |
| 214 |
run_as_root=True) |
| 215 |
|
| 216 |
def _get_ovs_port_specifics(self, localport): |
| 217 |
# Returns a tuple of: |
| 218 |
# - OVS port number for traffic to/from VMs |
| 219 |
# - OVS port name |
| 220 |
try: |
| 221 |
port_name = "" |
| 222 |
if ('ovs' in localport and localport['ovs']['plugged']): |
| 223 |
try: |
| 224 |
port = localport['ovs']['port_number'] |
| 225 |
except KeyError: |
| 226 |
self.log.info("No OVS port number provided, trying to use" |
| 227 |
" a port name") |
| 228 |
port = self.driver.find_ovs_port( |
| 229 |
localport['ovs']['port_name']) |
| 230 |
else: |
| 231 |
try: |
| 232 |
try: |
| 233 |
port_name = localport['ovs']['port_name'] |
| 234 |
except KeyError: |
| 235 |
port_name = localport['linuxif'] |
| 236 |
except Exception: |
| 237 |
raise Exception(_("Trying to find which port to plug," |
| 238 |
" but no portname was provided")) |
| 239 |
|
| 240 |
try: |
| 241 |
port = self.driver.find_ovs_port(port_name) |
| 242 |
except Exception: |
| 243 |
port = self.bridge.add_port(port_name) |
| 244 |
self.log.debug("Corresponding port number: %s", port) |
| 245 |
except KeyError as e: |
| 246 |
self.log.error("Incomplete port specification: %s", e) |
| 247 |
raise Exception(_("Incomplete port specification: %s" % e)) |
| 248 |
|
| 249 |
return (port, port_name) |
| 250 |
|
| 251 |
def _vlan_match(self): |
| 252 |
return {'dl_vlan': self.ovs_vlan} if self.ovs_vlan else {} |
| 253 |
|
| 254 |
def get_push_vlan_action(self): |
| 255 |
return ("push_vlan:0x8100,mod_vlan_vid:%d" % self.ovs_vlan |
| 256 |
if self.ovs_vlan else "") |
| 257 |
|
| 258 |
def get_strip_vlan_action(self): |
| 259 |
return "strip_vlan" if self.ovs_vlan else "" |
| 260 |
|
| 261 |
@log_decorator.log_info |
| 262 |
def update_fallback(self, fallback=None): |
| 263 |
if fallback: |
| 264 |
self.fallback = fallback |
| 265 |
|
| 266 |
if not self.fallback: |
| 267 |
return |
| 268 |
|
| 269 |
for param in ('src_mac', 'dst_mac', 'ovs_port_number'): |
| 270 |
if not self.fallback.get(param): |
| 271 |
self.log.error("fallback specified without '%s'", param) |
| 272 |
return |
| 273 |
|
| 274 |
# use priority -1 so that the route only hits when the packet |
| 275 |
# does not matches any VRF route |
| 276 |
self.bridge.add_flow_extended( |
| 277 |
flow_matches=[dict(table=self.driver.vrf_table, |
| 278 |
cookie=self._cookie(add=True), |
| 279 |
priority=FALLBACK_PRIORITY), |
| 280 |
self._vrf_match()], |
| 281 |
actions=[self.get_push_vlan_action(), |
| 282 |
'mod_dl_src:%s' % self.fallback.get('src_mac'), |
| 283 |
'mod_dl_dst:%s' % self.fallback.get('dst_mac'), |
| 284 |
'output:%d' % self.fallback.get('ovs_port_number')]) |
| 285 |
|
| 286 |
@log_decorator.log_info |
| 287 |
def setup_arp_responder(self, ovs_port): |
| 288 |
if not self.driver.config.arp_responder: |
| 289 |
return |
| 290 |
|
| 291 |
self.bridge.add_flow_extended( |
| 292 |
flow_matches=[dict(table=self.driver.vrf_table, |
| 293 |
cookie=self._cookie(add=True), |
| 294 |
priority=DEFAULT_RULE_PRIORITY, |
| 295 |
proto='arp', |
| 296 |
dl_dst='ff:ff:ff:ff:ff:ff', |
| 297 |
arp_op='0x1'), |
| 298 |
self._vrf_match(), |
| 299 |
# Respond to all IP addresses if proxy ARP is |
| 300 |
# enabled, otherwise only for gateway |
| 301 |
{'arp_tpa': self.gateway_ip} |
| 302 |
if not self.driver.config.proxy_arp else {}], |
| 303 |
actions=[ARP_RESPONDER_ACTIONS % { |
| 304 |
'mac': netaddr.EUI(GATEWAY_MAC, dialect=netaddr.mac_unix), |
| 305 |
'vlan_action': self.get_push_vlan_action(), |
| 306 |
'in_port': ovs_port |
| 307 |
}]) |
| 308 |
|
| 309 |
@log_decorator.log_info |
| 310 |
def remove_arp_responder(self): |
| 311 |
self.bridge.delete_flows_extended( |
| 312 |
flow_matches=[dict(table=self.driver.vrf_table, |
| 313 |
cookie=self._cookie(add=False), |
| 314 |
proto='arp'), |
| 315 |
self._vrf_match()]) |
| 316 |
|
| 317 |
def _check_vlan_use(self, localport): |
| 318 |
# checks that if a vlan_action is used, it is the same |
| 319 |
# for all interfaces plugged into the VRF |
| 320 |
# on first plug we update |
| 321 |
try: |
| 322 |
ovs_vlan = int(localport['ovs']['vlan']) |
| 323 |
except KeyError: |
| 324 |
return |
| 325 |
|
| 326 |
if self.ovs_vlan is None: |
| 327 |
self.ovs_vlan = ovs_vlan |
| 328 |
else: |
| 329 |
# on a subsequent plug, we check |
| 330 |
if self.ovs_vlan != ovs_vlan: |
| 331 |
self.log.error("different VLAN for different interfaces: " |
| 332 |
"%s vs %s", self.ovs_vlan, |
| 333 |
ovs_vlan) |
| 334 |
raise Exception(_("can't specify a different VLAN for " |
| 335 |
"different interfaces")) |
| 336 |
|
| 337 |
@log_decorator.log |
| 338 |
def vif_plugged(self, mac_address, ip_address, localport, label, |
| 339 |
direction): |
| 340 |
|
| 341 |
(ovs_port, ovs_port_name) = self._get_ovs_port_specifics(localport) |
| 342 |
|
| 343 |
self._check_vlan_use(localport) |
| 344 |
|
| 345 |
# need to update fallback, in case it was called before the |
| 346 |
# first vifPlugged call could define the push_vlan action |
| 347 |
self.update_fallback() |
| 348 |
|
| 349 |
# This is a hack used with previous versions of Openstack |
| 350 |
# proper MTUs should actually be configured in the hybrid vif driver |
| 351 |
# Please consider this obsolete until it gets clean'd up |
| 352 |
self._mtu_fixup(localport) |
| 353 |
|
| 354 |
if vpn_instance.forward_from_port(direction): |
| 355 |
# Map traffic from plugged port to VRF |
| 356 |
|
| 357 |
# Note that we first reset the in_port so that OVS will allow the |
| 358 |
# packet to eventually go back to this port after a VRF lookup |
| 359 |
# (the case where that happens is where in port is a patch-port on |
| 360 |
# which different VLANs are used to reach different networks): |
| 361 |
# patch port vlan X -- VRFX --- VRF Y -- patch-port vlan Y |
| 362 |
# |
| 363 |
# ( see http://docs.openvswitch.org/en/latest/faq/openflow/ |
| 364 |
# "Q: I added a flow to send packets out the ingress port..." ) |
| 365 |
for proto in ('ip', 'arp'): |
| 366 |
self.bridge.add_flow_extended( |
| 367 |
flow_matches=[dict(table=self.driver.input_table, |
| 368 |
cookie=self._cookie(add=True), |
| 369 |
priority=DEFAULT_RULE_PRIORITY, |
| 370 |
proto=proto, |
| 371 |
in_port=ovs_port), |
| 372 |
self._vlan_match()], |
| 373 |
actions=[self.get_strip_vlan_action(), |
| 374 |
'load:0->NXM_OF_IN_PORT[],', |
| 375 |
'set_field:%d->reg%d,' % (self.instance_id, |
| 376 |
VRF_REGISTER), |
| 377 |
'resubmit(,%d)' % self.driver.vrf_table]) |
| 378 |
|
| 379 |
# Map ARP responder if necessary |
| 380 |
if not self._ovs_port_info: |
| 381 |
self.setup_arp_responder(ovs_port) |
| 382 |
|
| 383 |
if vpn_instance.forward_to_port(direction): |
| 384 |
# Map incoming MPLS traffic going to the VM port |
| 385 |
incoming_actions = [self.get_push_vlan_action(), |
| 386 |
"mod_dl_src:{},mod_dl_dst:{}".format( |
| 387 |
GATEWAY_MAC, |
| 388 |
mac_address), |
| 389 |
"output:%s" % ovs_port] |
| 390 |
|
| 391 |
self.bridge.add_flow_extended( |
| 392 |
flow_matches=[dict(table=self.driver.encap_in_table, |
| 393 |
cookie=self._cookie(add=True), |
| 394 |
priority=DEFAULT_RULE_PRIORITY, |
| 395 |
proto="mpls", |
| 396 |
mpls_label=label, |
| 397 |
mpls_bos=1)], |
| 398 |
actions=["pop_mpls:0x0800"] + incoming_actions) |
| 399 |
|
| 400 |
# additional incoming traffic rule for VXLAN |
| 401 |
if self.driver.vxlan_encap: |
| 402 |
self.bridge.add_flow_extended( |
| 403 |
flow_matches=[ |
| 404 |
dict(table=self.driver.encap_in_table, |
| 405 |
cookie=self._cookie(add=True), |
| 406 |
priority=DEFAULT_RULE_PRIORITY, |
| 407 |
in_port=self.driver.vxlan_tunnel_port_number, |
| 408 |
tun_id=label)], |
| 409 |
actions=incoming_actions) |
| 410 |
|
| 411 |
# Add OVS port number in list for local port plugged in VRF |
| 412 |
# FIXME: check check check, is linuxif the right key?? |
| 413 |
self.log.debug("Adding OVS port %s with port %s for address " |
| 414 |
"%s, to the list of ports plugged in VRF", |
| 415 |
localport['linuxif'], ovs_port, ip_address) |
| 416 |
self._ovs_port_info[localport['linuxif']] = { |
| 417 |
"ovs_port": ovs_port, |
| 418 |
"ovs_port_name": ovs_port_name, |
| 419 |
} |
| 420 |
|
| 421 |
@log_decorator.log |
| 422 |
def vif_unplugged(self, mac_address, ip_address, localport, label, |
| 423 |
direction, last_endpoint=True): |
| 424 |
|
| 425 |
ovs_port = self._ovs_port_info[localport['linuxif']]['ovs_port'] |
| 426 |
ovs_port_name = self._ovs_port_info[ |
| 427 |
localport['linuxif']]['ovs_port_name'] |
| 428 |
|
| 429 |
if vpn_instance.forward_to_port(direction): |
| 430 |
# Unmap incoming MPLS traffic going to the VM port |
| 431 |
self.bridge.delete_flows(table=self.driver.encap_in_table, |
| 432 |
cookie=self._cookie(add=False), |
| 433 |
proto="mpls", |
| 434 |
mpls_label=label, |
| 435 |
mpls_bos=1) |
| 436 |
|
| 437 |
# Unmap incoming VXLAN traffic... |
| 438 |
if self.driver.vxlan_encap: |
| 439 |
self.bridge.delete_flows( |
| 440 |
table=self.driver.encap_in_table, |
| 441 |
cookie=self._cookie(add=False), |
| 442 |
in_port=self.driver.vxlan_tunnel_port_number, |
| 443 |
tun_id=label) |
| 444 |
|
| 445 |
if last_endpoint: |
| 446 |
if vpn_instance.forward_from_port(direction): |
| 447 |
# Unmap all traffic from plugged port |
| 448 |
self.bridge.delete_flows_extended( |
| 449 |
flow_matches=[dict(table=self.driver.input_table, |
| 450 |
cookie=self._cookie(add=False), |
| 451 |
in_port=ovs_port), |
| 452 |
self._vlan_match()]) |
| 453 |
|
| 454 |
# Unmap ARP responder |
| 455 |
self.remove_arp_responder() |
| 456 |
|
| 457 |
# Run port unplug action if necessary (OVS port delete) |
| 458 |
if ovs_port_name: |
| 459 |
self.bridge.delete_port(ovs_port_name) |
| 460 |
|
| 461 |
# Remove OVS port number from list for local port plugged in VRF |
| 462 |
del self._ovs_port_info[localport['linuxif']] |
| 463 |
|
| 464 |
def _get_label_action(self, label, encaps): |
| 465 |
if (self.driver.vxlan_encap and |
| 466 |
exa.Encapsulation(exa.Encapsulation.Type.VXLAN) in encaps): |
| 467 |
return "set_field:%d->tunnel_id" % label |
| 468 |
else: |
| 469 |
return "push_mpls:0x8847,set_mpls_label:%d" % label |
| 470 |
|
| 471 |
def _get_output_action(self, remote_pe, encaps): |
| 472 |
# Check if prefix is from a local VRF |
| 473 |
if self.driver.get_local_address() == remote_pe: |
| 474 |
self.log.debug("Local route, using a resubmit action") |
| 475 |
# For local traffic, we have to use a resubmit action |
| 476 |
if (self.driver.vxlan_encap and |
| 477 |
exa.Encapsulation(exa.Encapsulation.Type.VXLAN) in encaps): |
| 478 |
return ("resubmit(%d,%d)" % |
| 479 |
(self.driver.vxlan_tunnel_port_number, |
| 480 |
self.driver.encap_in_table)) |
| 481 |
else: |
| 482 |
return "resubmit(%d,%d)" % (self.driver.mpls_in_port(), |
| 483 |
self.driver.encap_in_table) |
| 484 |
else: |
| 485 |
if (self.driver.vxlan_encap and |
| 486 |
exa.Encapsulation(exa.Encapsulation.Type.VXLAN) in encaps): |
| 487 |
self.log.debug("Will use a VXLAN encap for this destination") |
| 488 |
return "set_field:{}->tun_dst,output:{}".format( |
| 489 |
remote_pe, self.driver.vxlan_tunnel_port_number) |
| 490 |
elif self.driver.use_gre: |
| 491 |
self.log.debug("Using MPLS/GRE encap") |
| 492 |
return "set_field:{}->tun_dst,output:{}".format( |
| 493 |
remote_pe, self.driver.gre_tunnel_port_number) |
| 494 |
else: |
| 495 |
self.log.debug("Using bare MPLS encap") |
| 496 |
# Find remote router MAC address |
| 497 |
try: |
| 498 |
remote_pe_mac_address = self._find_remote_mac_address( |
| 499 |
remote_pe) |
| 500 |
self.log.debug("MAC address found for remote router " |
| 501 |
"%(remote_pe)s: %(remote_pe_mac_address)s", |
| 502 |
locals()) |
| 503 |
except exc.RemotePEMACAddressNotFound as e: |
| 504 |
self.log.error("An error occured during setupDataplaneFor" |
| 505 |
"RemoteEndpoint: %s", e) |
| 506 |
raise |
| 507 |
|
| 508 |
# Map traffic to remote IP address as MPLS on ethX to remote |
| 509 |
# router MAC address |
| 510 |
return "mod_dl_src:{},mod_dl_dst:{},output:{}".format( |
| 511 |
self.driver.mpls_if_mac_address, remote_pe_mac_address, |
| 512 |
self.driver.ovs_mpls_if_port_number) |
| 513 |
|
| 514 |
def _cookie(self, add=False): |
| 515 |
mask = "" |
| 516 |
if not add: |
| 517 |
mask = "/-1" |
| 518 |
return "%d%s" % (self.instance_id, mask) |
| 519 |
|
| 520 |
def _vrf_match(self): |
| 521 |
return {'reg%d' % VRF_REGISTER: self.instance_id} |
| 522 |
|
| 523 |
@log_decorator.log_info |
| 524 |
def setup_dataplane_for_remote_endpoint(self, prefix, remote_pe, label, |
| 525 |
nlri, encaps, |
| 526 |
lb_consistent_hash_order=0): |
| 527 |
nexthop = NextHop(label, remote_pe, encaps, lb_consistent_hash_order) |
| 528 |
|
| 529 |
if self.nh_group_mgr.is_object_user(prefix, nexthop): |
| 530 |
self.log.debug("Dataplane already in place for %s, %s, skipping", |
| 531 |
prefix, nexthop) |
| 532 |
return |
| 533 |
|
| 534 |
dec_ttl = (netaddr.IPNetwork(prefix) not in netaddr.IPNetwork( |
| 535 |
"{}/{}".format(self.gateway_ip, self.network_plen))) |
| 536 |
label_action = self._get_label_action(nexthop.label, |
| 537 |
nexthop.encaps) |
| 538 |
output_action = self._get_output_action(nexthop.remote_pe, |
| 539 |
nexthop.encaps) |
| 540 |
self.nh_group_mgr.new_nexthop(prefix, nexthop, |
| 541 |
actions=["dec_ttl" if dec_ttl else "", |
| 542 |
label_action, |
| 543 |
output_action]) |
| 544 |
|
| 545 |
@log_decorator.log_info |
| 546 |
def remove_dataplane_for_remote_endpoint(self, prefix, remote_pe, label, |
| 547 |
nlri, encaps, |
| 548 |
lb_consistent_hash_order=0): |
| 549 |
|
| 550 |
nexthop = NextHop(label, remote_pe, encaps, lb_consistent_hash_order) |
| 551 |
|
| 552 |
if self.nh_group_mgr.is_object_user(prefix, nexthop): |
| 553 |
self.nh_group_mgr.del_nexthop(prefix, nexthop) |
| 554 |
else: |
| 555 |
self.log.debug("remove_dataplane_for_remote_endpoint called, " |
| 556 |
"for %s and %s, but we don't know about this " |
| 557 |
"prefix and next-hop", prefix, nexthop.__dict__) |
| 558 |
|
| 559 |
def _get_port_range_from_classifier(self, classifier_port): |
| 560 |
if classifier_port: |
| 561 |
if isinstance(classifier_port, tuple): |
| 562 |
port_min, port_max = classifier_port |
| 563 |
else: |
| 564 |
port_min = port_max = classifier_port |
| 565 |
|
| 566 |
return port_min, port_max |
| 567 |
|
| 568 |
def _create_port_range_flow_matches(self, classifier_match, classifier): |
| 569 |
flow_matches = [] |
| 570 |
src_port_match = f'{classifier.protocol:s}_src' |
| 571 |
|
| 572 |
if classifier.source_port: |
| 573 |
if isinstance(classifier.source_port, tuple): |
| 574 |
src_port_min, src_port_max = classifier.source_port |
| 575 |
else: |
| 576 |
src_port_min = src_port_max = classifier.source_port |
| 577 |
|
| 578 |
dst_port_match = f'{classifier.protocol:s}_dst' |
| 579 |
|
| 580 |
if classifier.destination_port: |
| 581 |
if isinstance(classifier.destination_port, tuple): |
| 582 |
dst_port_min, dst_port_max = classifier.destination_port |
| 583 |
else: |
| 584 |
dst_port_min = dst_port_max = classifier.destination_port |
| 585 |
|
| 586 |
dst_port_range = [] |
| 587 |
if dst_port_min and dst_port_max: |
| 588 |
dst_port_range = n_utils.port_rule_masking(dst_port_min, |
| 589 |
dst_port_max) |
| 590 |
src_port_range = [] |
| 591 |
if src_port_min and src_port_max: |
| 592 |
src_port_range = n_utils.port_rule_masking(src_port_min, |
| 593 |
src_port_max) |
| 594 |
for port in src_port_range: |
| 595 |
flow_match = classifier_match.copy() |
| 596 |
flow_match[src_port_match] = port |
| 597 |
if dst_port_range: |
| 598 |
for port in dst_port_range: |
| 599 |
dst_flow = flow_match.copy() |
| 600 |
dst_flow[dst_port_match] = port |
| 601 |
flow_matches.append(dst_flow) |
| 602 |
else: |
| 603 |
flow_matches.append(flow_match) |
| 604 |
else: |
| 605 |
for port in dst_port_range: |
| 606 |
flow_match = classifier_match.copy() |
| 607 |
flow_match[dst_port_match] = port |
| 608 |
flow_matches.append(flow_match) |
| 609 |
|
| 610 |
return flow_matches |
| 611 |
|
| 612 |
def _create_classifier_flow_matches(self, classifier): |
| 613 |
classifier_match = dict(proto=classifier.protocol) |
| 614 |
|
| 615 |
if classifier.source_pfx: |
| 616 |
classifier_match.update({'nw_src': classifier.source_pfx}) |
| 617 |
|
| 618 |
if classifier.destination_pfx: |
| 619 |
classifier_match.update({'nw_dst': classifier.destination_pfx}) |
| 620 |
|
| 621 |
return self._create_port_range_flow_matches(classifier_match, |
| 622 |
classifier) |
| 623 |
|
| 624 |
@log_decorator.log_info |
| 625 |
def add_dataplane_for_traffic_classifier(self, classifier, |
| 626 |
redirect_to_instance_id): |
| 627 |
classifier_matches = self._create_classifier_flow_matches(classifier) |
| 628 |
|
| 629 |
# Add traffic redirection to redirection VRF for classifier matches |
| 630 |
for classifier_match in classifier_matches: |
| 631 |
self.bridge.add_flow_extended( |
| 632 |
flow_matches=[dict(table=self.driver.vrf_table, |
| 633 |
cookie=self._cookie(add=True), |
| 634 |
priority=DEFAULT_RULE_PRIORITY), |
| 635 |
self._vrf_match(), |
| 636 |
classifier_match], |
| 637 |
actions=['set_field:%d->reg%d' % (redirect_to_instance_id, |
| 638 |
VRF_REGISTER), |
| 639 |
'resubmit(,%d)' % self.driver.vrf_table]) |
| 640 |
|
| 641 |
@log_decorator.log_info |
| 642 |
def remove_dataplane_for_traffic_classifier(self, classifier): |
| 643 |
classifier_matches = self._create_classifier_flow_matches(classifier) |
| 644 |
|
| 645 |
# Remove traffic redirection to redirection VRF for classifier matches |
| 646 |
for classifier_match in classifier_matches: |
| 647 |
self.bridge.delete_flows_extended( |
| 648 |
flow_matches=[dict(table=self.driver.vrf_table, |
| 649 |
cookie=self._cookie(add=False)), |
| 650 |
self._vrf_match(), |
| 651 |
classifier_match]) |
| 652 |
|
| 653 |
def get_lg_map(self): |
| 654 |
return { |
| 655 |
"flows": (lg.SUBTREE, self.get_lg_ovs_flows) |
| 656 |
} |
| 657 |
|
| 658 |
def get_lg_ovs_flows(self, path_prefix): |
| 659 |
return self.driver.get_lg_ovs_flows( |
| 660 |
path_prefix, 'cookie=%s' % self._cookie(add=False)) |
| 661 |
|
| 662 |
|
| 663 |
class OVSGroupAllocator(identifier_allocators.IDAllocator): |
| 664 |
|
| 665 |
MAX = 2 ** 32 - 1 |
| 666 |
|
| 667 |
|
| 668 |
class OVSBucketAllocator(identifier_allocators.IDAllocator): |
| 669 |
|
| 670 |
# Values greater than 0xFFFFFF00 are reserved |
| 671 |
MAX = 2 ** 32 - 2 ** 8 - 1 |
| 672 |
|
| 673 |
|
| 674 |
class NextHop: |
| 675 |
|
| 676 |
def __init__(self, label, remote_pe, encaps, lb_consistent_hash_order): |
| 677 |
self.label = label |
| 678 |
self.remote_pe = str(remote_pe) |
| 679 |
self.encaps = frozenset(encaps) |
| 680 |
self.lb_consistent_hash_order = lb_consistent_hash_order |
| 681 |
|
| 682 |
def __eq__(self, other): |
| 683 |
return ((self.label, self.remote_pe, self.encaps) == |
| 684 |
(other.label, other.remote_pe, other.encaps)) |
| 685 |
|
| 686 |
def __hash__(self): |
| 687 |
return hash((self.label, |
| 688 |
self.remote_pe, |
| 689 |
self.encaps)) |
| 690 |
|
| 691 |
def __repr__(self): |
| 692 |
return "NextHop({},{},{},{})".format(self.label, |
| 693 |
self.remote_pe, |
| 694 |
self.encaps, |
| 695 |
self.lb_consistent_hash_order) |
| 696 |
|
| 697 |
|
| 698 |
class NextHopGroupManager(dataplane_utils.ObjectLifecycleManager): |
| 699 |
|
| 700 |
def __init__(self, bridge, hash_method, hash_method_param, hash_fields): |
| 701 |
super().__init__() |
| 702 |
|
| 703 |
self.bridge = bridge |
| 704 |
self.hash_method = hash_method |
| 705 |
self.hash_method_param = hash_method_param |
| 706 |
self.hash_fields = hash_fields |
| 707 |
|
| 708 |
self.group_allocator = OVSGroupAllocator() |
| 709 |
|
| 710 |
def get_selection_method(self): |
| 711 |
selection_method = self.hash_method |
| 712 |
if self.hash_fields and self.hash_method == 'hash': |
| 713 |
selection_method += ",fields(%s)" % ','.join(self.hash_fields) |
| 714 |
|
| 715 |
return selection_method |
| 716 |
|
| 717 |
@log_decorator.log_info |
| 718 |
def create_object(self, prefix, *args, **kwargs): |
| 719 |
buckets = ( |
| 720 |
{'buckets': kwargs['buckets']} if kwargs.get('buckets') else {} |
| 721 |
) |
| 722 |
|
| 723 |
group_id = self.group_allocator.get_new_id("Group ID for prefix %s" % |
| 724 |
str(prefix)) |
| 725 |
|
| 726 |
self.bridge.add_group(group_id=group_id, |
| 727 |
type='select', |
| 728 |
selection_method=self.get_selection_method(), |
| 729 |
selection_method_param=self.hash_method_param, |
| 730 |
**buckets) |
| 731 |
|
| 732 |
return group_id |
| 733 |
|
| 734 |
@log_decorator.log_info |
| 735 |
def delete_object(self, group_id): |
| 736 |
self.bridge.delete_group(group_id=group_id) |
| 737 |
self.group_allocator.release(group_id) |
| 738 |
|
| 739 |
|
| 740 |
class NextHopGroupManagerProxy(dataplane_utils.ObjectLifecycleManagerProxy): |
| 741 |
|
| 742 |
def __init__(self, manager, parent_key, vrf_table, vrf_match, cookie_func): |
| 743 |
super().__init__(manager, parent_key) |
| 744 |
|
| 745 |
self.vrf_table = vrf_table |
| 746 |
self.vrf_match = vrf_match |
| 747 |
self.cookie_func = cookie_func |
| 748 |
|
| 749 |
self.bucket_allocators = ( |
| 750 |
collections.defaultdict(OVSBucketAllocator) |
| 751 |
) |
| 752 |
|
| 753 |
self.prefix_nexthop_2_bucket = dict() |
| 754 |
|
| 755 |
def _update_group_buckets(self, group_id, prefix): |
| 756 |
buckets = [] |
| 757 |
for prefix_nh, bucket in self.prefix_nexthop_2_bucket.items(): |
| 758 |
if prefix == prefix_nh[0]: |
| 759 |
buckets.append('bucket=bucket_id=%d,%s' % ( |
| 760 |
bucket[0], dataplane_utils.join_s(*bucket[1]))) |
| 761 |
|
| 762 |
self.manager.bridge.mod_group( |
| 763 |
group_id=group_id, |
| 764 |
type='select', |
| 765 |
selection_method=self.manager.get_selection_method(), |
| 766 |
selection_method_param=self.manager.hash_method_param, |
| 767 |
buckets=','.join(buckets)) |
| 768 |
|
| 769 |
def new_nexthop(self, prefix, nexthop, actions=None): |
| 770 |
if actions is None: |
| 771 |
actions = [] |
| 772 |
bucket_allocator = self.bucket_allocators[prefix] |
| 773 |
bucket_id = bucket_allocator.get_new_id( |
| 774 |
"Bucket ID for prefix {} and nexthop {}".format( |
| 775 |
str(prefix), nexthop), |
| 776 |
hint_value=nexthop.lb_consistent_hash_order |
| 777 |
) |
| 778 |
bucket = 'bucket=bucket_id=%d,%s' % (bucket_id, |
| 779 |
dataplane_utils.join_s(*actions)) |
| 780 |
|
| 781 |
self.prefix_nexthop_2_bucket[(prefix, nexthop)] = (bucket_id, actions) |
| 782 |
|
| 783 |
group_id, first = self.get_object(prefix, nexthop, buckets=bucket) |
| 784 |
if first: |
| 785 |
self.manager.bridge.add_flow_extended( |
| 786 |
flow_matches=[dict(table=self.vrf_table, |
| 787 |
cookie=self.cookie_func(add=True), |
| 788 |
priority=_priority_from_prefix(prefix), |
| 789 |
proto='ip'), |
| 790 |
self.vrf_match, |
| 791 |
_match_from_prefix(prefix)], |
| 792 |
actions=["group:%d" % group_id]) |
| 793 |
else: |
| 794 |
self._update_group_buckets(group_id, prefix) |
| 795 |
|
| 796 |
def del_nexthop(self, prefix, nexthop): |
| 797 |
group_id = self.find_object(prefix) |
| 798 |
|
| 799 |
bucket_id, _ = self.prefix_nexthop_2_bucket.pop((prefix, nexthop)) |
| 800 |
|
| 801 |
bucket_allocator = self.bucket_allocators[prefix] |
| 802 |
bucket_allocator.release(bucket_id) |
| 803 |
|
| 804 |
self._update_group_buckets(group_id, prefix) |
| 805 |
|
| 806 |
last = self.free_object(prefix, nexthop) |
| 807 |
|
| 808 |
if last: |
| 809 |
self.manager.bridge.delete_flows_extended( |
| 810 |
flow_matches=[dict(strict=True, |
| 811 |
table=self.vrf_table, |
| 812 |
cookie=self.cookie_func(add=False), |
| 813 |
priority=_priority_from_prefix(prefix), |
| 814 |
proto='ip'), |
| 815 |
self.vrf_match, |
| 816 |
_match_from_prefix(prefix)]) |
| 817 |
|
| 818 |
del self.bucket_allocators[prefix] |
| 819 |
|
| 820 |
|
| 821 |
class MPLSOVSDataplaneDriver(dp_drivers.DataplaneDriver): |
| 822 |
|
| 823 |
"""Dataplane driver using OpenVSwitch |
| 824 |
|
| 825 |
Based on an OpenVSwitch 2.4 MPLS kernel dataplane implementation. |
| 826 |
|
| 827 |
This driver was successfully tested with the OVS 2.4 DKMS module. |
| 828 |
|
| 829 |
This driver uses MPLS-over-GRE by default. However, note well that current |
| 830 |
OVS implementation of MPLS-over-GRE is not yet conformant with RFC4023, |
| 831 |
because of an intermediate Eth header (MPLS-over-Eth-over-GRE). |
| 832 |
|
| 833 |
If MPLS-over-GRE is disabled (with mpls_over_gre=False), this driver |
| 834 |
currently requires that the OVS bridge be associated to the address used as |
| 835 |
the local_address in bgp.conf, to allow the linux IP stack to use the same |
| 836 |
physical interface as the one on which MPLS packets are forwarded. This |
| 837 |
requires to configure the OVS bridge so that it passes packets from the |
| 838 |
physical interface to the linux IP stack if they are not MPLS, and packets |
| 839 |
from the linux IP stack to the physical device. |
| 840 |
|
| 841 |
Howto allow the use of the OVS bridge interface also as an IP |
| 842 |
interface of the Linux kernel IP stack: |
| 843 |
ovs-ofctl del-flows br-int |
| 844 |
ovs-ofctl add-flow br-int in_port=LOCAL,action=output:1 |
| 845 |
ovs-ofctl add-flow br-int in_port=1,action=output:LOCAL |
| 846 |
|
| 847 |
(on a debian or ubuntu system, this can be done part of the ovs bridge |
| 848 |
definition in /etc/network/interfaces, as post-up commands) |
| 849 |
|
| 850 |
The 'vrf_table' (resp. 'input_table') config parameters can be |
| 851 |
used to specify which OVS table will host the rules for traffic from VRFs |
| 852 |
(resp. for incoming traffic). Beware, this dataplane driver will |
| 853 |
*not* take care of setting up rules so that MPLS traffic or the traffic |
| 854 |
from attached ports is matched against rules in these tables. |
| 855 |
""" |
| 856 |
|
| 857 |
dataplane_instance_class = MPLSOVSVRFDataplane |
| 858 |
type = consts.IPVPN |
| 859 |
ecmp_support = True |
| 860 |
required_ovs_version = "2.8.0" |
| 861 |
|
| 862 |
driver_opts = [ |
| 863 |
cfg.StrOpt("mpls_interface", |
| 864 |
help=("Interface used to send/receive MPLS traffic. " |
| 865 |
"Use '*gre*' to choose automatic creation of a tunnel" |
| 866 |
" port for MPLS/GRE encap")), |
| 867 |
cfg.StrOpt("mpls_over_gre", |
| 868 |
choices=['auto', 'True', 'False'], |
| 869 |
default="auto", |
| 870 |
advanced=True, |
| 871 |
help=("Force the use of MPLS/GRE even with " |
| 872 |
"mpls_interface specified")), |
| 873 |
cfg.BoolOpt("proxy_arp", default=False, |
| 874 |
advanced=True, |
| 875 |
help=("Activate ARP responder per VRF for any IP " |
| 876 |
"address")), |
| 877 |
cfg.BoolOpt("arp_responder", default=False, |
| 878 |
advanced=True, |
| 879 |
help=("ARP responder per VRF")), |
| 880 |
cfg.BoolOpt("vxlan_encap", default=False, |
| 881 |
advanced=True, |
| 882 |
help=("Be ready to receive VPN traffic as VXLAN, and to " |
| 883 |
"preferrably send traffic as VXLAN when advertised " |
| 884 |
"by the remote end")), |
| 885 |
cfg.StrOpt("ovs_bridge", default="br-mpls", advanced=True, |
| 886 |
help=("Name of the OVS bridge to use, this has to be the " |
| 887 |
"same as the tunneling bridge of the Neutron OVS " |
| 888 |
"agent.")), |
| 889 |
cfg.IntOpt("input_table", default=0, advanced=True, |
| 890 |
help=("Specifies which OVS table will host the rules " |
| 891 |
"for traffic from VRFs")), |
| 892 |
cfg.IntOpt("ovs_table_id_start", default=1, advanced=True, |
| 893 |
help=("The starting OVS table number to use for " |
| 894 |
"additional tables. The values for the encap-in " |
| 895 |
"and VRF tables are derived from this. " |
| 896 |
"NOTE: This must be different than input_table " |
| 897 |
"config option.")), |
| 898 |
cfg.StrOpt("gre_tunnel", default="mpls_gre", advanced=True, |
| 899 |
help="OVS interface name for MPLS/GRE encap"), |
| 900 |
cfg.ListOpt("gre_tunnel_options", default=[], |
| 901 |
item_type=types.String(), |
| 902 |
help=("Options, comma-separated, passed to OVS for GRE " |
| 903 |
"tunnel port creation (e.g. 'packet_type=legacy_l3" |
| 904 |
", ...') that will be added as OVS tunnel " |
| 905 |
"interface options (e.g. 'options:packet_type=" |
| 906 |
"legacy_l3 options:...')")), |
| 907 |
cfg.IntOpt("ovsbr_interfaces_mtu", advanced=True, |
| 908 |
help=("MTU used for OVS bridge interfaces.")), |
| 909 |
cfg.StrOpt("hash_method", |
| 910 |
choices=["hash", "dp_hash"], |
| 911 |
default="dp_hash", |
| 912 |
advanced=True, |
| 913 |
help=("Can be used to control the OVS group bucket " |
| 914 |
"selection method (mapped to ovs " |
| 915 |
"'selection_method')")), |
| 916 |
cfg.IntOpt("hash_method_param", |
| 917 |
default=0, |
| 918 |
min=0, max=2**64 - 1, |
| 919 |
advanced=True, |
| 920 |
help=("Can be used to control the OVS group bucket " |
| 921 |
"selection method (mapped to ovs " |
| 922 |
"'selection_method_param')")), |
| 923 |
cfg.ListOpt("hash_fields", default=[], advanced=True, |
| 924 |
help=("Can be used to control the fields used by the OVS " |
| 925 |
"group bucket selection method (mapped to ovs " |
| 926 |
"'fields')")) |
| 927 |
] |
| 928 |
|
| 929 |
def __init__(self): |
| 930 |
super().__init__() |
| 931 |
|
| 932 |
config.set_default_root_helper() |
| 933 |
config.setup_privsep() |
| 934 |
|
| 935 |
try: |
| 936 |
(o, _) = self._run_command("ovs-ofctl -V | head -1 |" |
| 937 |
" awk '{print $4}'") |
| 938 |
self.ovs_release = o[0] |
| 939 |
self.log.info("OVS version: %s", self.ovs_release) |
| 940 |
except Exception: |
| 941 |
self.log.warning("Could not determine OVS release") |
| 942 |
self.ovs_release = None |
| 943 |
|
| 944 |
self.mpls_interface = self.config.mpls_interface |
| 945 |
|
| 946 |
if self.config.mpls_over_gre != "auto": |
| 947 |
self.use_gre = True |
| 948 |
else: |
| 949 |
self.use_gre = not (self.mpls_interface and |
| 950 |
self.mpls_interface != "*gre*") |
| 951 |
|
| 952 |
if not self.mpls_interface: |
| 953 |
if not self.use_gre: |
| 954 |
raise Exception(_("mpls_over_gre force-disabled, but no " |
| 955 |
"mpls_interface specified")) |
| 956 |
else: |
| 957 |
self.use_gre = True |
| 958 |
self.log.info("Defaulting to use of MPLS-over-GRE (no " |
| 959 |
"mpls_interface specified)") |
| 960 |
elif self.mpls_interface == "*gre*": |
| 961 |
if not self.use_gre: |
| 962 |
raise Exception(_("mpls_over_gre force-disabled, but " |
| 963 |
"mpls_interface set to '*gre', cannot " |
| 964 |
"use bare MPLS")) |
| 965 |
else: |
| 966 |
self.log.info("mpls_interface is '*gre*', will thus use " |
| 967 |
"MPLS-over-GRE") |
| 968 |
self.use_gre = True |
| 969 |
self.mpls_interface = None |
| 970 |
else: |
| 971 |
if self.use_gre: |
| 972 |
self.log.warning("mpls_over_gre set to True, " |
| 973 |
"ignoring mpls_interface parameter") |
| 974 |
self.mpls_interface = None |
| 975 |
else: |
| 976 |
self.log.info("Will use bare MPLS on interface %s", |
| 977 |
self.mpls_interface) |
| 978 |
|
| 979 |
self.input_table = self.config.input_table |
| 980 |
|
| 981 |
if self.config.ovs_table_id_start == self.input_table: |
| 982 |
raise Exception(_("invalid ovs_table_id_start (%d): can't use" |
| 983 |
"tables same as input table (%d)" % ( |
| 984 |
self.config.ovs_table_id_start, |
| 985 |
self.config.input_table))) |
| 986 |
|
| 987 |
self.encap_in_table = self.config.ovs_table_id_start |
| 988 |
self.vrf_table = self.config.ovs_table_id_start + 1 |
| 989 |
|
| 990 |
self.all_tables = {'incoming': self.input_table, |
| 991 |
'vrf': self.vrf_table, |
| 992 |
'encap_in': self.encap_in_table} |
| 993 |
|
| 994 |
# Used to control whether this VRF will support |
| 995 |
# receiving traffic as VXLAN |
| 996 |
self.vxlan_encap = self.config.vxlan_encap |
| 997 |
|
| 998 |
# check OVS version |
| 999 |
ovs_release_version = versionutils.convert_version_to_tuple( |
| 1000 |
self.ovs_release) |
| 1001 |
required_ovs_version_tuple = versionutils.convert_version_to_tuple( |
| 1002 |
self.required_ovs_version) |
| 1003 |
if (not self.vxlan_encap and |
| 1004 |
ovs_release_version < required_ovs_version_tuple): |
| 1005 |
self.log.warning("%s requires at least OVS %s" |
| 1006 |
" (you are running %s)", |
| 1007 |
self.__class__.__name__, |
| 1008 |
self.required_ovs_version, |
| 1009 |
self.ovs_release) |
| 1010 |
|
| 1011 |
# unless useGRE is enabled, check that fping is installed |
| 1012 |
if not self.use_gre: |
| 1013 |
self._run_command("fping -v", raise_on_error=True) |
| 1014 |
|
| 1015 |
self.bridge = dataplane_utils.OVSBridgeWithGroups( |
| 1016 |
dataplane_utils.OVSExtendedBridge(self.config.ovs_bridge) |
| 1017 |
) |
| 1018 |
# Check if OVS bridge exist |
| 1019 |
if not self.bridge.bridge_exists(self.bridge.br_name): |
| 1020 |
raise exc.OVSBridgeNotFound(self.bridge.br_name) |
| 1021 |
|
| 1022 |
self.bridge.use_at_least_protocol(ovs_const.OPENFLOW15) |
| 1023 |
|
| 1024 |
self.nh_group_mgr = NextHopGroupManager(self.bridge, |
| 1025 |
self.config.hash_method, |
| 1026 |
self.config.hash_method_param, |
| 1027 |
self.config.hash_fields) |
| 1028 |
|
| 1029 |
if not self.use_gre: |
| 1030 |
self.log.info("Will not force the use of GRE/MPLS, trying to bind " |
| 1031 |
"physical interface %s", self.mpls_interface) |
| 1032 |
# Check if MPLS interface is attached to OVS bridge |
| 1033 |
if not self.bridge.port_exists(self.mpls_interface): |
| 1034 |
raise Exception(_("Specified mpls_interface %s is not plugged" |
| 1035 |
" to OVS bridge %s" % (self.mpls_interface, |
| 1036 |
self.bridge.br_name))) |
| 1037 |
else: |
| 1038 |
self.ovs_mpls_if_port_number = self.bridge.get_port_ofport( |
| 1039 |
self.mpls_interface) |
| 1040 |
|
| 1041 |
def supported_encaps(self): |
| 1042 |
if self.use_gre: |
| 1043 |
yield exa.Encapsulation(exa.Encapsulation.Type.GRE) |
| 1044 |
yield exa.Encapsulation(exa.Encapsulation.Type.DEFAULT) |
| 1045 |
# we will accept routes with no encap |
| 1046 |
# specified and force the use of GRE |
| 1047 |
else: |
| 1048 |
yield exa.Encapsulation(exa.Encapsulation.Type.MPLS) |
| 1049 |
# we also accept route with no encap specified |
| 1050 |
yield exa.Encapsulation(exa.Encapsulation.Type.DEFAULT) |
| 1051 |
|
| 1052 |
if self.vxlan_encap: |
| 1053 |
yield exa.Encapsulation(exa.Encapsulation.Type.VXLAN) |
| 1054 |
|
| 1055 |
def mpls_in_port(self): |
| 1056 |
if self.use_gre: |
| 1057 |
return self.gre_tunnel_port_number |
| 1058 |
else: |
| 1059 |
return self.ovs_mpls_if_port_number |
| 1060 |
|
| 1061 |
@log_decorator.log_info |
| 1062 |
def reset_state(self): |
| 1063 |
# Flush all MPLS and ARP flows, all groups, if bridge exists |
| 1064 |
|
| 1065 |
if self.bridge.bridge_exists(self.bridge.br_name): |
| 1066 |
self.log.info("Cleaning up OVS rules") |
| 1067 |
|
| 1068 |
self.bridge.delete_flows(table=self.input_table, |
| 1069 |
cookie=ovs_lib.COOKIE_ANY, |
| 1070 |
proto='mpls') |
| 1071 |
if self.vxlan_encap: |
| 1072 |
try: |
| 1073 |
self.bridge.delete_flows( |
| 1074 |
table=self.input_table, |
| 1075 |
in_port=self.find_ovs_port(VXLAN_TUNNEL)) |
| 1076 |
except Exception: |
| 1077 |
self.log.info("no VXLAN tunnel port, nothing to clean up") |
| 1078 |
# the above won't clean up flows if the vxlan_tunnel interface |
| 1079 |
# has changed... |
| 1080 |
self.bridge.delete_flows(table=self.input_table, |
| 1081 |
cookie=ovs_lib.COOKIE_ANY, |
| 1082 |
tun_id='2/1') |
| 1083 |
self.bridge.delete_flows(table=self.input_table, |
| 1084 |
cookie=ovs_lib.COOKIE_ANY, |
| 1085 |
tun_id='1/1') |
| 1086 |
|
| 1087 |
# clean input_table rule for plugged ports |
| 1088 |
# NOTE(tmorin): would be cleaner using a cookie |
| 1089 |
self.bridge.delete_flows(table=self.input_table, |
| 1090 |
cookie=ovs_lib.COOKIE_ANY, |
| 1091 |
proto='ip') |
| 1092 |
self.bridge.delete_flows(table=self.input_table, |
| 1093 |
cookie=ovs_lib.COOKIE_ANY, |
| 1094 |
proto='arp') |
| 1095 |
|
| 1096 |
self.bridge.delete_flows(table=self.encap_in_table, |
| 1097 |
cookie=ovs_lib.COOKIE_ANY) |
| 1098 |
self.bridge.delete_flows(table=self.vrf_table, |
| 1099 |
cookie=ovs_lib.COOKIE_ANY) |
| 1100 |
|
| 1101 |
# clean all groups |
| 1102 |
self.bridge.delete_group() |
| 1103 |
|
| 1104 |
if self.log.isEnabledFor(logging.DEBUG): |
| 1105 |
self.log.debug("All our rules have been flushed:\n%s", |
| 1106 |
'\n'.join(self.bridge.dump_all_flows())) |
| 1107 |
self.log.debug("All groups have been flushed:\n%s", |
| 1108 |
self.bridge.run_ofctl("dump-groups", [])) |
| 1109 |
else: |
| 1110 |
self.log.info("No OVS bridge (%s), no need to cleanup OVS rules", |
| 1111 |
self.bridge.br_name) |
| 1112 |
|
| 1113 |
def initialize(self): |
| 1114 |
if self.use_gre: |
| 1115 |
self.log.info("Setting up tunnel for MPLS/GRE (%s)", |
| 1116 |
self.config.gre_tunnel) |
| 1117 |
|
| 1118 |
self.bridge.delete_port(self.config.gre_tunnel) |
| 1119 |
|
| 1120 |
gre_tunnel_options = dict( |
| 1121 |
o.split('=') for o in self.config.gre_tunnel_options) |
| 1122 |
gre_tunnel_attrs = [ |
| 1123 |
('type', 'gre'), |
| 1124 |
('options', dict({'local_ip': self.get_local_address(), |
| 1125 |
'remote_ip': 'flow'}, |
| 1126 |
**gre_tunnel_options)) |
| 1127 |
] |
| 1128 |
|
| 1129 |
self.gre_tunnel_port_number = ( |
| 1130 |
self.bridge.add_port(self.config.gre_tunnel, *gre_tunnel_attrs) |
| 1131 |
) |
| 1132 |
self.mpls_if_mac_address = None |
| 1133 |
else: |
| 1134 |
# Find ethX MPLS interface MAC address |
| 1135 |
try: |
| 1136 |
self.mpls_if_mac_address = net_utils.get_device_mac( |
| 1137 |
self._run_command, |
| 1138 |
self.mpls_interface) |
| 1139 |
except Exception: |
| 1140 |
# Interface without MAC address (patch port case), use MPLS |
| 1141 |
# bridge MAC address instead |
| 1142 |
self.mpls_if_mac_address = net_utils.get_device_mac( |
| 1143 |
self._run_command, |
| 1144 |
self.bridge.br_name) |
| 1145 |
|
| 1146 |
self.bridge.add_flow(table=self.input_table, |
| 1147 |
priority=DEFAULT_RULE_PRIORITY, |
| 1148 |
in_port=self.mpls_in_port(), |
| 1149 |
proto='mpls', |
| 1150 |
actions="resubmit(,%d)" % self.encap_in_table) |
| 1151 |
|
| 1152 |
if self.vxlan_encap: |
| 1153 |
self.log.info("Enabling VXLAN encapsulation") |
| 1154 |
self.bridge.delete_port(VXLAN_TUNNEL) |
| 1155 |
|
| 1156 |
vxlan_tunnel_attrs = [ |
| 1157 |
('type', 'vxlan'), |
| 1158 |
('options', {'local_ip': self.get_local_address(), |
| 1159 |
'remote_ip': 'flow', |
| 1160 |
'key': 'flow'}) |
| 1161 |
] |
| 1162 |
self.vxlan_tunnel_port_number = ( |
| 1163 |
self.bridge.add_port(VXLAN_TUNNEL, *vxlan_tunnel_attrs) |
| 1164 |
) |
| 1165 |
|
| 1166 |
self.bridge.add_flow(table=self.input_table, |
| 1167 |
priority=DEFAULT_RULE_PRIORITY, |
| 1168 |
in_port=self.vxlan_tunnel_port_number, |
| 1169 |
actions="resubmit(,%d)" % self.encap_in_table) |
| 1170 |
|
| 1171 |
def validate_directions(self, direction): |
| 1172 |
# this driver supports all combinations of directions |
| 1173 |
pass |
| 1174 |
|
| 1175 |
def find_ovs_port(self, dev_name): |
| 1176 |
"""Find OVS port number from port name""" |
| 1177 |
|
| 1178 |
ofport = self.bridge.get_port_ofport(dev_name) |
| 1179 |
|
| 1180 |
if ofport is None: |
| 1181 |
raise Exception(_("OVS port not found for device %s" % dev_name)) |
| 1182 |
|
| 1183 |
return ofport |
| 1184 |
|
| 1185 |
# Looking glass code #### |
| 1186 |
|
| 1187 |
def get_lg_map(self): |
| 1188 |
return { |
| 1189 |
"flows": (lg.SUBTREE, self.get_lg_ovs_flows), |
| 1190 |
"ports": (lg.SUBTREE, self.get_lg_ovs_ports) |
| 1191 |
} |
| 1192 |
|
| 1193 |
def get_lg_local_info(self, path_prefix): |
| 1194 |
d = { |
| 1195 |
"ovs_bridge": self.bridge.br_name, |
| 1196 |
"mpls_interface": self.mpls_interface, |
| 1197 |
"gre": {'enabled': self.use_gre}, |
| 1198 |
"vxlan": {'enabled': self.vxlan_encap}, |
| 1199 |
"ovs_version": self.ovs_release, |
| 1200 |
"tables": self.all_tables |
| 1201 |
} |
| 1202 |
|
| 1203 |
if self.use_gre: |
| 1204 |
d["gre"].update({'gre_tunnel_port': self.config.gre_tunnel}) |
| 1205 |
if self.vxlan_encap: |
| 1206 |
d["gre"].update({'vxlan_tunnel_port': VXLAN_TUNNEL}) |
| 1207 |
return d |
| 1208 |
|
| 1209 |
def get_lg_ovs_flows(self, path_prefix, cookie_spec=None): |
| 1210 |
output = {} |
| 1211 |
for (table_name, table_id) in self.all_tables.items(): |
| 1212 |
output.update({ |
| 1213 |
"%s (%d)" % (table_name, table_id): self._run_command( |
| 1214 |
"ovs-ofctl -O {} dump-flows --names {} '{}' {}".format( |
| 1215 |
ovs_const.OPENFLOW15, |
| 1216 |
self.bridge.br_name, |
| 1217 |
dataplane_utils.join_s("table=%d" % table_id, |
| 1218 |
cookie_spec), |
| 1219 |
OVS_DUMP_FLOW_FILTER), |
| 1220 |
run_as_root=True, shell=True |
| 1221 |
)[0] |
| 1222 |
}) |
| 1223 |
return output |
| 1224 |
|
| 1225 |
def get_lg_ovs_ports(self, path_prefix): |
| 1226 |
(output, _) = self._run_command( |
| 1227 |
"ovs-ofctl -O {} show {} |grep addr".format(ovs_const.OPENFLOW15, |
| 1228 |
self.bridge.br_name), |
| 1229 |
run_as_root=True, |
| 1230 |
acceptable_return_codes=[0, 1], |
| 1231 |
shell=True) |
| 1232 |
# FIXME: does it properly show the GRE tunnel interface ? |
| 1233 |
return output |