| 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 shlex |
| 16 |
import socket |
| 17 |
|
| 18 |
from oslo_config import cfg |
| 19 |
from oslo_config import types |
| 20 |
from oslo_privsep import priv_context |
| 21 |
from pyroute2 import ndb as ndb_module |
| 22 |
|
| 23 |
from networking_bagpipe._i18n import _ |
| 24 |
|
| 25 |
|
| 26 |
class InterfaceAddress(types.ConfigType): |
| 27 |
# Option type for a config entry accepting whether an IP address |
| 28 |
# or an interface from which to derive the IP address |
| 29 |
|
| 30 |
# convert from IP version (4 or 6) to family number |
| 31 |
FAMILY_MAP = { |
| 32 |
4: socket.AF_INET, |
| 33 |
6: socket.AF_INET6, |
| 34 |
} |
| 35 |
|
| 36 |
def __init__(self, type_name="interface address value", version=4): |
| 37 |
super().__init__(type_name=type_name) |
| 38 |
self.version = version |
| 39 |
self.family = self.FAMILY_MAP[version] |
| 40 |
self.ip_address = types.IPAddress(version) |
| 41 |
|
| 42 |
def __call__(self, value): |
| 43 |
try: |
| 44 |
return self.ip_address(value) |
| 45 |
except ValueError: |
| 46 |
# pyroute2 call to take the first address of this interface having |
| 47 |
# the right IP version (family) |
| 48 |
with ndb_module.main.NDB() as ndb: |
| 49 |
# pylint: disable=no-member |
| 50 |
try: |
| 51 |
interface = ndb.interfaces[value] |
| 52 |
except KeyError: |
| 53 |
raise ValueError(_("interface %s does not exist" % value)) |
| 54 |
|
| 55 |
for iface_addr in interface.ipaddr: |
| 56 |
if iface_addr['family'] == self.family: |
| 57 |
return self.ip_address(iface_addr['address']) |
| 58 |
|
| 59 |
raise ValueError(_("no IPv%s address found on interface %s" % |
| 60 |
(self.version, value))) |
| 61 |
|
| 62 |
def _formatter(self, value): |
| 63 |
address = self(value) |
| 64 |
return "{}({})".format(address, value) |
| 65 |
|
| 66 |
def __repr__(self): |
| 67 |
return "InterfaceAddress" |
| 68 |
|
| 69 |
def __eq__(self, other): |
| 70 |
return self.__class__ == other.__class__ |
| 71 |
|
| 72 |
|
| 73 |
bgp_opts = [ |
| 74 |
cfg.Opt('local_address', required=True, |
| 75 |
type=InterfaceAddress(), |
| 76 |
help="IP address used for BGP peerings"), |
| 77 |
cfg.ListOpt('peers', default=[], |
| 78 |
item_type=types.HostAddress(version=4), |
| 79 |
help="IP addresses of BGP peers"), |
| 80 |
cfg.IntOpt('my_as', min=1, max=2 ** 16 - 1, required=True, |
| 81 |
help="Our BGP Autonomous System"), |
| 82 |
cfg.BoolOpt('enable_rtc', default=True, |
| 83 |
help="Enable RT Constraint (RFC4684)"), |
| 84 |
cfg.PortOpt('bgp_port', default=179, |
| 85 |
help="TCP port of connections to BGP peers") |
| 86 |
] |
| 87 |
|
| 88 |
|
| 89 |
def register(): |
| 90 |
cfg.CONF.register_opts(bgp_opts, "BGP") |
| 91 |
|
| 92 |
|
| 93 |
def set_default_root_helper(): |
| 94 |
# copy bagpipe-bgp root helper configuration into neutron's config, so |
| 95 |
# that neutron classes find the right configuration to execute commands |
| 96 |
cfg.CONF.set_default('root_helper', |
| 97 |
cfg.CONF.COMMON.root_helper, |
| 98 |
group="AGENT") |
| 99 |
cfg.CONF.set_default('root_helper_daemon', |
| 100 |
cfg.CONF.COMMON.root_helper_daemon, |
| 101 |
group="AGENT") |
| 102 |
|
| 103 |
|
| 104 |
def setup_privsep(): |
| 105 |
priv_context.init(root_helper=shlex.split(cfg.CONF.COMMON.root_helper)) |