| 1 |
# Copyright 2017 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 logging as python_logging |
| 16 |
import time |
| 17 |
import uuid |
| 18 |
|
| 19 |
from oslo_config import cfg |
| 20 |
from oslo_log import log as logging |
| 21 |
import pbr.version |
| 22 |
import pecan |
| 23 |
from pecan import request |
| 24 |
|
| 25 |
from networking_bagpipe.bagpipe_bgp.common import exceptions as exc |
| 26 |
from networking_bagpipe.bagpipe_bgp.common import looking_glass as lg |
| 27 |
from networking_bagpipe.bagpipe_bgp.common import utils |
| 28 |
from networking_bagpipe.bagpipe_bgp.vpn import manager as vpn_manager |
| 29 |
|
| 30 |
|
| 31 |
LOG = logging.getLogger(__name__) |
| 32 |
|
| 33 |
LOOKING_GLASS_BASE = "looking-glass" |
| 34 |
|
| 35 |
|
| 36 |
def expose(*args, **kwargs): |
| 37 |
"""Helper function so we don't have to specify json for everything.""" |
| 38 |
kwargs.setdefault('content_type', 'application/json') |
| 39 |
kwargs.setdefault('template', 'json') |
| 40 |
return pecan.expose(*args, **kwargs) |
| 41 |
|
| 42 |
|
| 43 |
def when(index, *args, **kwargs): |
| 44 |
"""Helper function so we don't have to specify json for everything.""" |
| 45 |
kwargs.setdefault('content_type', 'application/json') |
| 46 |
kwargs.setdefault('template', 'json') |
| 47 |
return index.when(*args, **kwargs) |
| 48 |
|
| 49 |
|
| 50 |
class PingController: |
| 51 |
|
| 52 |
def __init__(self): |
| 53 |
# Random generated sequence number |
| 54 |
self.sequence = int(uuid.uuid4()) |
| 55 |
|
| 56 |
@expose(generic=True) |
| 57 |
def index(self): |
| 58 |
return self.sequence |
| 59 |
|
| 60 |
|
| 61 |
class VPNManagerController: |
| 62 |
|
| 63 |
def __init__(self): |
| 64 |
self.manager = vpn_manager.VPNManager.get_instance() |
| 65 |
|
| 66 |
@staticmethod |
| 67 |
def stop(): |
| 68 |
vpn_manager.VPNManager.get_instance().stop() |
| 69 |
|
| 70 |
|
| 71 |
class AttachController(VPNManagerController): |
| 72 |
"""attach_localport parameters: |
| 73 |
|
| 74 |
'vpn_instance_id': external VPN instance identifier (all ports with same |
| 75 |
vpn_instance_id will be plugged in the same VPN |
| 76 |
instance |
| 77 |
'instance_description': free form description of VPN instance |
| 78 |
'description': free form description of attachment |
| 79 |
'vpn_type': type of the VPN instance ('ipvpn' or 'evpn') |
| 80 |
'import_rt': list of import Route Targets (or comma-separated string) |
| 81 |
'export_rt': list of export Route Targets (or comma-separated string) |
| 82 |
'gateway_ip': IP address of gateway for this VPN instance |
| 83 |
'mac_address': MAC address of endpoint to connect to the VPN instance |
| 84 |
'ip_address': IP/mask of endpoint to connect to the VPN instance |
| 85 |
'advertise_subnet': optional, if set to True then VRF will advertise |
| 86 |
the whole subnet (defaults to False, readvertise |
| 87 |
ip_address as a singleton (/32) |
| 88 |
'linuxbr': Name of a linux bridge to which the linuxif is already |
| 89 |
plugged-in (optional) |
| 90 |
'vni': VXLAN VNI to use (optional) |
| 91 |
'local_pref': BGP LOCAL_PREF for the route to this vif (optional) |
| 92 |
'direction': 'to-port' | 'from-port' | 'both' |
| 93 |
# specify local port traffic direction in VPN instance |
| 94 |
# (route advertisements are not done with from-port only) |
| 95 |
# to-port: only forward traffic to the VIF |
| 96 |
# from-port: only forward traffic to the VIF |
| 97 |
'local_port': local port to plug to the VPN instance |
| 98 |
should be a dict containing any of the following key,value pairs |
| 99 |
{ |
| 100 |
'linuxif': 'tap456abc', # name of a linux interface |
| 101 |
# - if OVS information is provided it |
| 102 |
# does not have to be an existing |
| 103 |
# interface |
| 104 |
# - not needed/not used if 'evpn' plug |
| 105 |
# is used |
| 106 |
'ovs': { # optional |
| 107 |
# whether or not interface is already plugged into the |
| 108 |
# OVS bridge: |
| 109 |
'plugged': True, |
| 110 |
# name of a linux interface to be plugged into the OVS |
| 111 |
# bridge (optional and ignored if port_number is |
| 112 |
# provided): |
| 113 |
'port_name': 'qvo456abc', |
| 114 |
# OVS port number (optional if 'port_name' provided): |
| 115 |
'port_number': '7', |
| 116 |
# the VLAN id for VM traffic (optional) |
| 117 |
'vlan': '42', |
| 118 |
# optional specification of a distinct port to send |
| 119 |
# traffic to the VM(only applies if a vlan is |
| 120 |
# specified) : |
| 121 |
'to_vm_port_number' |
| 122 |
'to_vm_port_name' |
| 123 |
}, |
| 124 |
'evpn': { # for an ipvpn attachment... |
| 125 |
'id': 'xyz' # specifies the vpn_instance_id of an evpn |
| 126 |
# that will be attached to the ipvpn |
| 127 |
'ovs_port_name': 'qvb456abc' # optional, if provided, |
| 128 |
# and if ovs/port_name is |
| 129 |
# also provided, then the |
| 130 |
# interface name will be |
| 131 |
# assumed as already plugged |
| 132 |
# into the evpn |
| 133 |
} |
| 134 |
} |
| 135 |
if local_port is not a list, it is assumed to be a name of a linux |
| 136 |
interface (string) |
| 137 |
'readvertise': { # optional, used to re-advertise addresses... |
| 138 |
'from_rt': [list of RTs] # ...received on these RTs |
| 139 |
'to_rt': [list of RTs] # ...toward these RTs |
| 140 |
} |
| 141 |
'attract_traffic': { # optional, will result in the generation of FlowSpec |
| 142 |
# routes, based on the specified classifier, |
| 143 |
# advertised to the readvertise:to_rt RTs, redirecting |
| 144 |
# traffic toward the "attract_to_rts" RT using a |
| 145 |
# redirect-to-VRF action action. |
| 146 |
# The prefixes for which FlowSpec routes are |
| 147 |
# advertised are: |
| 148 |
# - prefixes carrying one of the readvertise:from_rt |
| 149 |
# RTs, |
| 150 |
# - prefixes in static_destination_prefixes. |
| 151 |
# |
| 152 |
# When this is used, the routes that are advertised to |
| 153 |
# the readvertise:to_rt route targets are, instead of |
| 154 |
# the prefixes of the routes carrying an RT in |
| 155 |
# readvertise:from_rt, the prefix in |
| 156 |
# static_destination_prefixes if any, or else a |
| 157 |
# default0.0.0.0 route. These routes are advertised |
| 158 |
# for each locally attached interface, each time with |
| 159 |
# a distinct RD |
| 160 |
'classifier': { |
| 161 |
'sourcePrefix': IP/mask, |
| 162 |
'sourcePort': Port number or port range, |
| 163 |
'destinationPort': Port number or port range, |
| 164 |
'protocol': IP protocol |
| 165 |
}, |
| 166 |
'redirect_rts': [list of RTs] # RTs of generated FlowSpec routes |
| 167 |
'attract_to_rts': [list of RTs] # RTs of the redirect-to-VRF action of |
| 168 |
# the generated FlowSpec routes |
| 169 |
'static_destination_prefixes': [list of IP/mask] # When present, |
| 170 |
# FlowSpec routes will |
| 171 |
# be generated from |
| 172 |
# these prefixes (as |
| 173 |
# destination prefix); |
| 174 |
# this is done |
| 175 |
# additionally to |
| 176 |
# FlowSpec routes |
| 177 |
# generated from |
| 178 |
# routes carrying |
| 179 |
# readvertise:from_rt |
| 180 |
} |
| 181 |
'fallback': # (optional) if provided, on a VRF lookup miss, |
| 182 |
# the MAC destination address will be |
| 183 |
# rewritten to this MAC before being |
| 184 |
# sent back where it came from |
| 185 |
{ |
| 186 |
'src_mac': 'aa:bb:cc:dd:ee:ff' # new source MAC |
| 187 |
'dst_mac': 'aa:bb:cc:dd:ee:00' # new destination MAC |
| 188 |
'ovs_port_name': 'patch_foo' |
| 189 |
'ovs_port_number': 4 # (unsupported yet) |
| 190 |
'ovs_resubmit': '(<port>,<table>)' # (unsupported yet) |
| 191 |
} |
| 192 |
""" |
| 193 |
|
| 194 |
@expose(generic=True) |
| 195 |
def index(self): |
| 196 |
return {} |
| 197 |
|
| 198 |
@when(index, method='PUT') |
| 199 |
@when(index, method='DELETE') |
| 200 |
def not_supported(self): |
| 201 |
pecan.abort(405) |
| 202 |
|
| 203 |
@when(index, method='POST') |
| 204 |
def process(self): |
| 205 |
try: |
| 206 |
attach_params = request.json |
| 207 |
except Exception: |
| 208 |
LOG.error('attach_localport: No local port details received') |
| 209 |
pecan.abort(400, 'No local port details received') |
| 210 |
|
| 211 |
try: |
| 212 |
LOG.info('Local port attach received: %s', attach_params) |
| 213 |
|
| 214 |
self.manager.plug_vif_to_vpn(**attach_params) |
| 215 |
except exc.APIException as e: |
| 216 |
LOG.warning('attach_localport: API error: %s', e) |
| 217 |
pecan.abort(400, "API error: %s" % e) |
| 218 |
except Exception: |
| 219 |
LOG.exception('attach_localport: An error occurred during local ' |
| 220 |
'port plug to VPN') |
| 221 |
pecan.abort(500, 'An error occurred during local port plug to VPN') |
| 222 |
|
| 223 |
|
| 224 |
class DetachController(VPNManagerController): |
| 225 |
|
| 226 |
@expose(generic=True) |
| 227 |
def index(self): |
| 228 |
return {} |
| 229 |
|
| 230 |
@when(index, method='PUT') |
| 231 |
@when(index, method='DELETE') |
| 232 |
def not_supported(self): |
| 233 |
pecan.abort(405) |
| 234 |
|
| 235 |
@when(index, method='POST') |
| 236 |
def process(self): |
| 237 |
try: |
| 238 |
detach_params = request.json |
| 239 |
except Exception: |
| 240 |
LOG.error('detach_localport: No local port details received') |
| 241 |
pecan.abort(400, 'No local port details received') |
| 242 |
|
| 243 |
try: |
| 244 |
LOG.info('Local port detach received: %s', detach_params) |
| 245 |
self.manager.unplug_vif_from_vpn(**detach_params) |
| 246 |
except exc.APIException as e: |
| 247 |
LOG.warning('detach_localport: API error: %s', e) |
| 248 |
pecan.abort(400, "API error: %s" % e) |
| 249 |
except Exception: |
| 250 |
LOG.exception('detach_localport: An error occurred during local ' |
| 251 |
'port unplug from VPN') |
| 252 |
pecan.abort(500, 'An error occurred during local port unplug from ' |
| 253 |
'VPN') |
| 254 |
|
| 255 |
|
| 256 |
class LookingGlassController(VPNManagerController, |
| 257 |
lg.LookingGlassMixin): |
| 258 |
|
| 259 |
def __init__(self): |
| 260 |
super().__init__() |
| 261 |
|
| 262 |
self.start_time = time.time() |
| 263 |
|
| 264 |
lg.set_references_root(LOOKING_GLASS_BASE) |
| 265 |
lg.set_reference_path("BGP_WORKERS", ["bgp", "workers"]) |
| 266 |
lg.set_reference_path("VPN_INSTANCES", ["vpns", "instances"]) |
| 267 |
lg.set_reference_path("DATAPLANE_DRIVERS", |
| 268 |
["vpns", "dataplane", "drivers"]) |
| 269 |
|
| 270 |
self.catchall_lg_log_handler = lg.LookingGlassLogHandler() |
| 271 |
python_logging.getLogger().addHandler(self.catchall_lg_log_handler) |
| 272 |
|
| 273 |
@expose(generic=True) |
| 274 |
def index(self): |
| 275 |
return {} |
| 276 |
|
| 277 |
@when(index, method='GET') |
| 278 |
def process(self, *url_path_elements): |
| 279 |
|
| 280 |
path_prefix = "{}://{}/{}".format( |
| 281 |
request.scheme, # http |
| 282 |
request.host, |
| 283 |
LOOKING_GLASS_BASE, |
| 284 |
) |
| 285 |
|
| 286 |
try: |
| 287 |
lg_info = self.get_looking_glass_info(path_prefix, |
| 288 |
url_path_elements) |
| 289 |
if lg_info is None: |
| 290 |
raise lg.NoSuchLookingGlassObject(path_prefix, |
| 291 |
url_path_elements[0]) |
| 292 |
return lg_info |
| 293 |
except lg.NoSuchLookingGlassObject as e: |
| 294 |
LOG.info('looking_glass: %s', repr(e)) |
| 295 |
pecan.abort(404, repr(e)) |
| 296 |
except Exception: |
| 297 |
LOG.exception('looking_glass: an error occurred') |
| 298 |
pecan.abort(500, 'Server error') |
| 299 |
|
| 300 |
@when(index, method='DELETE') |
| 301 |
@when(index, method='POST') |
| 302 |
@when(index, method='PUT') |
| 303 |
def not_supported(self): |
| 304 |
pecan.abort(405) |
| 305 |
|
| 306 |
# Looking glass hooks ################# |
| 307 |
|
| 308 |
def get_lg_map(self): |
| 309 |
return { |
| 310 |
"summary": (lg.SUBITEM, self.get_lg_summary), |
| 311 |
"config": (lg.SUBTREE, self.get_lg_config), |
| 312 |
"bgp": (lg.DELEGATE, self.manager.bgp_manager), |
| 313 |
"vpns": (lg.DELEGATE, self.manager), |
| 314 |
"logs": (lg.SUBTREE, self.get_logs) |
| 315 |
} |
| 316 |
|
| 317 |
def get_lg_config(self, path_prefix): |
| 318 |
return {section: utils.osloconfig_json_serialize(cfg.CONF[section]) |
| 319 |
for section in ('COMMON', 'API', 'BGP', |
| 320 |
'DATAPLANE_DRIVER_IPVPN', |
| 321 |
'DATAPLANE_DRIVER_EVPN') |
| 322 |
} |
| 323 |
|
| 324 |
def get_lg_summary(self): |
| 325 |
return { |
| 326 |
"BGP_established_peers": |
| 327 |
self.manager.bgp_manager.get_established_peers_count(), |
| 328 |
"route_counts": self.manager.bgp_manager.get_lg_route_counts(), |
| 329 |
"vpn_instances_count": self.manager.get_vpn_instances_count(), |
| 330 |
"warnings_and_errors": len(self.catchall_lg_log_handler), |
| 331 |
"start_time": time.strftime("%Y-%m-%d %H:%M:%S", |
| 332 |
time.localtime(self.start_time)), |
| 333 |
"version": (pbr.version.VersionInfo('networking-bagpipe') |
| 334 |
.release_string()) |
| 335 |
} |
| 336 |
|
| 337 |
def get_logs(self, path_prefix): |
| 338 |
return [{'level': record.levelname, |
| 339 |
'time': |
| 340 |
self.catchall_lg_log_handler.formatter.formatTime(record), |
| 341 |
'name': record.name, |
| 342 |
'message': record.msg} |
| 343 |
for record in self.catchall_lg_log_handler.get_records()] |
| 344 |
|
| 345 |
|
| 346 |
class RootController: |
| 347 |
|
| 348 |
@expose(generic=True) |
| 349 |
def index(self): |
| 350 |
return {} |
| 351 |
|
| 352 |
@when(index, method='POST') |
| 353 |
@when(index, method='PUT') |
| 354 |
@when(index, method='DELETE') |
| 355 |
def not_supported(self): |
| 356 |
pecan.abort(405) |
| 357 |
|
| 358 |
ping = PingController() |
| 359 |
attach_localport = AttachController() |
| 360 |
detach_localport = DetachController() |
| 361 |
|
| 362 |
def stop(self): |
| 363 |
VPNManagerController.stop() |
| 364 |
|
| 365 |
|
| 366 |
# there is a '-' in the LOOKING_GLASS_BASE name, so we have to use pecan.route |
| 367 |
pecan.route(RootController, LOOKING_GLASS_BASE, LookingGlassController()) |