From 9ecc15f0edf2460c2eb2cac8e9ccca8cbb8d3808 Mon Sep 17 00:00:00 2001 From: Feng Pan Date: Mon, 17 Jul 2017 16:25:25 -0400 Subject: Merge Newton private changes - Add interface mapping data to hiera - Add support for VPP interfaces - Continue bringing up interfaces even if one fails - Change hiera to json format List of commits: https://github.com/openstack/os-net-config/compare/stable/newton...trozet:stable/danube Change-Id: If7a2c6119bf613f1fc8846237b077cd8f0e26015 Signed-off-by: Feng Pan --- etc/os-net-config/samples/vpp_interface.json | 14 ++ etc/os-net-config/samples/vpp_interface.yaml | 18 ++ os_net_config/__init__.py | 28 +++- os_net_config/cli.py | 3 + os_net_config/impl_eni.py | 8 + os_net_config/impl_ifcfg.py | 53 +++++- os_net_config/objects.py | 58 +++++++ os_net_config/tests/test_impl_eni.py | 31 ++++ os_net_config/tests/test_impl_ifcfg.py | 31 ++++ os_net_config/tests/test_objects.py | 16 ++ os_net_config/tests/test_utils.py | 34 +++- os_net_config/utils.py | 239 ++++++++++++++++++++++++++- 12 files changed, 523 insertions(+), 10 deletions(-) create mode 100644 etc/os-net-config/samples/vpp_interface.json create mode 100644 etc/os-net-config/samples/vpp_interface.yaml diff --git a/etc/os-net-config/samples/vpp_interface.json b/etc/os-net-config/samples/vpp_interface.json new file mode 100644 index 0000000..5d2f82a --- /dev/null +++ b/etc/os-net-config/samples/vpp_interface.json @@ -0,0 +1,14 @@ +{ "network_config": [ + { + "type": "vpp_interface", + "name": "nic2", + "addresses": [ + { + "ip_netmask": "192.0.2.1/24" + } + ], + "uio_driver": "uio_pci_generic", + "options": "vlan-strip-offload off" + } + ] +} diff --git a/etc/os-net-config/samples/vpp_interface.yaml b/etc/os-net-config/samples/vpp_interface.yaml new file mode 100644 index 0000000..de790d5 --- /dev/null +++ b/etc/os-net-config/samples/vpp_interface.yaml @@ -0,0 +1,18 @@ +network_config: + - + type: vpp_interface + name: nic2 + addresses: + - + ip_netmask: 192.0.2.1/24 + # DPDK poll-mode driver name. Defaults to 'vfio-pci', other possible value + # is 'uio_pci_generic'. It is also possible to specify other driver names + # such as 'igb_uio', however, it is assumed that any required kernel + # modules for those drivers are already loaded when os-net-config is + # invoked. + uio_driver: uio_pci_generic + # Interface options such as vlan stripping and tx/rx transmit queues + # specification. Reference for those configurations can + # be found at https://wiki.fd.io/view/VPP/Command-line_Arguments + # Example: 'vlan-strip-offload on num-rx-queues 3' + #options: "vlan-strip-offload off" diff --git a/os_net_config/__init__.py b/os_net_config/__init__.py index faa5e92..609d5dc 100644 --- a/os_net_config/__init__.py +++ b/os_net_config/__init__.py @@ -30,6 +30,10 @@ class NotImplemented(Exception): pass +class ConfigurationError(Exception): + pass + + class NetConfig(object): """Common network config methods class.""" @@ -37,6 +41,7 @@ class NetConfig(object): self.noop = noop self.log_prefix = "NOOP: " if noop else "" self.root_dir = root_dir + self.errors = [] def add_object(self, obj): """Convenience method to add any type of object to the network config. @@ -95,6 +100,8 @@ class NetConfig(object): self.add_ovs_dpdk_port(obj) elif isinstance(obj, objects.OvsDpdkBond): self.add_ovs_dpdk_bond(obj) + elif isinstance(obj, objects.VppInterface): + self.add_vpp_interface(obj) def add_interface(self, interface): """Add an Interface object to the net config object. @@ -201,6 +208,13 @@ class NetConfig(object): """ raise NotImplementedError("add_ovs_dpdk_bond is not implemented.") + def add_vpp_interface(self, vpp_interface): + """Add a VppInterface object to the net config object. + + :param vpp_interface: The VppInterface object to add. + """ + raise NotImplementedError("add_vpp_interface is not implemented.") + def apply(self, cleanup=False): """Apply the network configuration. @@ -240,8 +254,20 @@ class NetConfig(object): self.execute(msg, '/sbin/ifdown', interface, check_exit_code=False) def ifup(self, interface, iftype='interface'): + """Run 'ifup' on the specified interface + + If a failure occurs when bringing up the interface it will be saved + to self.errors for later handling. This allows callers to continue + trying to bring up interfaces even if one fails. + + :param interface: The name of the interface to be started. + :param iftype: The type of the interface. + """ msg = 'running ifup on %s: %s' % (iftype, interface) - self.execute(msg, '/sbin/ifup', interface) + try: + self.execute(msg, '/sbin/ifup', interface) + except processutils.ProcessExecutionError as e: + self.errors.append(e) def ifrename(self, oldname, newname): msg = 'renaming %s to %s: ' % (oldname, newname) diff --git a/os_net_config/cli.py b/os_net_config/cli.py index 479b3a3..c1676d0 100644 --- a/os_net_config/cli.py +++ b/os_net_config/cli.py @@ -126,6 +126,9 @@ def main(argv=sys.argv): logger.info('Using mapping file at: %s' % opts.mapping_file) iface_array = [] + if os.path.isfile('/root/dpdk_bind_lock'): + return 0 + provider = None if opts.provider: if opts.provider == 'ifcfg': diff --git a/os_net_config/impl_eni.py b/os_net_config/impl_eni.py index 360d8c8..944c4e9 100644 --- a/os_net_config/impl_eni.py +++ b/os_net_config/impl_eni.py @@ -241,6 +241,14 @@ class ENINetConfig(os_net_config.NetConfig): for interface in self.interfaces.keys(): self.ifup(interface) + + if self.errors: + message = 'Failure(s) occurred when applying configuration' + logger.error(message) + for e in self.errors: + logger.error('stdout: %s, stderr: %s', e.stdout, + e.stderr) + raise os_net_config.ConfigurationError(message) else: logger.info('No interface changes are required.') diff --git a/os_net_config/impl_ifcfg.py b/os_net_config/impl_ifcfg.py index cf3b257..43682ea 100644 --- a/os_net_config/impl_ifcfg.py +++ b/os_net_config/impl_ifcfg.py @@ -54,6 +54,10 @@ def nfvswitch_config_path(): return "/etc/sysconfig/nfvswitch" +def vpp_config_path(): + return "/etc/vpp/startup.conf" + + def route_config_path(name): return "/etc/sysconfig/network-scripts/route-%s" % name @@ -116,6 +120,7 @@ class IfcfgNetConfig(os_net_config.NetConfig): self.linuxbond_data = {} self.ib_interface_data = {} self.linuxteam_data = {} + self.vpp_interface_data = {} self.member_names = {} self.renamed_interfaces = {} self.bond_primary_ifaces = {} @@ -626,6 +631,21 @@ class IfcfgNetConfig(os_net_config.NetConfig): if ovs_dpdk_bond.routes: self._add_routes(ovs_dpdk_bond.name, ovs_dpdk_bond.routes) + def add_vpp_interface(self, vpp_interface): + """Add a VppInterface object to the net config object + + :param vpp_inteface: The VppInterface object to add + """ + vpp_interface.pci_dev = utils.get_pci_address(vpp_interface.name, + False) + vpp_interface.hwaddr = utils.interface_mac(vpp_interface.name) + if not self.noop: + self.ifdown(vpp_interface.name) + remove_ifcfg_config(vpp_interface.name) + logger.info('adding vpp interface: %s %s' + % (vpp_interface.name, vpp_interface.pci_dev)) + self.vpp_interface_data[vpp_interface.name] = vpp_interface + def generate_ivs_config(self, ivs_uplinks, ivs_interfaces): """Generate configuration content for ivs.""" @@ -690,6 +710,7 @@ class IfcfgNetConfig(os_net_config.NetConfig): restart_bridges = [] restart_linux_bonds = [] restart_linux_teams = [] + restart_vpp = False update_files = {} all_file_names = [] ivs_uplinks = [] # ivs physical uplinks @@ -698,6 +719,7 @@ class IfcfgNetConfig(os_net_config.NetConfig): nfvswitch_internal_ifaces = [] # nfvswitch internal/management ports stop_dhclient_interfaces = [] ovs_needs_restart = False + vpp_interfaces = self.vpp_interface_data.values() for interface_name, iface_data in self.interface_data.items(): route_data = self.route_data.get(interface_name, '') @@ -906,6 +928,16 @@ class IfcfgNetConfig(os_net_config.NetConfig): logger.info('No changes required for InfiniBand iface: %s' % interface_name) + if self.vpp_interface_data: + vpp_path = self.root_dir + vpp_config_path() + vpp_config = utils.generate_vpp_config(vpp_path, vpp_interfaces) + if utils.diff(vpp_path, vpp_config): + restart_vpp = True + update_files[vpp_path] = vpp_config + else: + restart_vpp = False + logger.info('No changes required for VPP') + if cleanup: for ifcfg_file in glob.iglob(cleanup_pattern()): if ifcfg_file not in all_file_names: @@ -932,7 +964,10 @@ class IfcfgNetConfig(os_net_config.NetConfig): for bridge in restart_bridges: self.ifdown(bridge, iftype='bridge') - for oldname, newname in self.renamed_interfaces.items(): + for vpp_interface in vpp_interfaces: + self.ifdown(vpp_interface.name) + + for oldname, newname in self.renamed_interfaces.iteritems(): self.ifrename(oldname, newname) # DPDK initialization is done before running os-net-config, to make @@ -1009,4 +1044,20 @@ class IfcfgNetConfig(os_net_config.NetConfig): for vlan in restart_vlans: self.ifup(vlan) + if not self.noop: + if restart_vpp: + logger.info('Restarting VPP') + utils.restart_vpp(vpp_interfaces) + + if self.vpp_interface_data: + logger.info('Updating VPP mapping') + utils.update_vpp_mapping(vpp_interfaces) + + if self.errors: + message = 'Failure(s) occurred when applying configuration' + logger.error(message) + for e in self.errors: + logger.error('stdout: %s, stderr: %s', e.stdout, e.stderr) + raise os_net_config.ConfigurationError(message) + return update_files diff --git a/os_net_config/objects.py b/os_net_config/objects.py index 9b6a06f..9287601 100644 --- a/os_net_config/objects.py +++ b/os_net_config/objects.py @@ -68,6 +68,8 @@ def object_from_json(json): return OvsDpdkPort.from_json(json) elif obj_type == "ovs_dpdk_bond": return OvsDpdkBond.from_json(json) + elif obj_type == "vpp_interface": + return VppInterface.from_json(json) def _get_required_field(json, name, object_name): @@ -198,6 +200,7 @@ class _BaseOpts(object): routes = routes or [] dns_servers = dns_servers or [] mapped_nic_names = _mapped_nics(nic_mapping) + utils.write_hiera(utils.HIERADATA_FILE, mapped_nic_names) self.hwaddr = None self.hwname = None self.renamed = False @@ -346,6 +349,7 @@ class Vlan(_BaseOpts): self.vlan_id = int(vlan_id) mapped_nic_names = _mapped_nics(nic_mapping) + utils.write_hiera(utils.HIERADATA_FILE, mapped_nic_names) if device in mapped_nic_names: self.device = mapped_nic_names[device] else: @@ -1129,3 +1133,57 @@ class OvsDpdkBond(_BaseOpts): defroute=defroute, dhclient_args=dhclient_args, dns_servers=dns_servers, nm_controlled=nm_controlled) + + +class VppInterface(_BaseOpts): + """Base class for VPP Interface. + + Vector Packet Processing (VPP) is a high performance packet processing + stack that runs in user space in Linux. VPP is used as an alternative to + kernel networking stack for accelerated network data path. VPP uses DPDK + poll-mode drivers to bind system interfaces rather than kernel drivers. + VPP bound interfacees are not visible to kernel networking stack, so we + must handle them separately. + + The following parameters can be specified in addition to base Interface: + - uio_driver: DPDK poll-mode driver name. Defaults to 'vfio-pci', valid + values are 'uio_pci_generic' and 'vfio-pci'. + - options: Interface options such as vlan stripping and tx/rx transmit + queues specification. Defaults to None. Reference for those + configurations can be found at + https://wiki.fd.io/view/VPP/Command-line_Arguments + Example: 'vlan-strip-offload on num-rx-queues 3' + + Note that 'name' attribute is used to indicate the kernel nic that should + be bound to VPP. Once VPP binds the interface, a mapping file will be + updated with the interface's information, and this file will be used in + subsequent runs of os-net-config. + """ + def __init__(self, name, use_dhcp=False, use_dhcpv6=False, addresses=None, + routes=None, mtu=None, primary=False, nic_mapping=None, + persist_mapping=False, defroute=True, dhclient_args=None, + dns_servers=None, nm_controlled=False, + uio_driver='vfio-pci', options=None): + addresses = addresses or [] + + super(VppInterface, self).__init__(name, use_dhcp, use_dhcpv6, + addresses, routes, mtu, primary, + nic_mapping, persist_mapping, + defroute, dhclient_args, + dns_servers, nm_controlled) + self.uio_driver = uio_driver + self.options = options + # pci_dev contains pci address for the interface, it will be populated + # when interface is added to config object. It will be determined + # either through ethtool or by looking up the dpdk mapping file. + self.pci_dev = None + + @staticmethod + def from_json(json): + name = _get_required_field(json, 'name', 'VppInterface') + uio_driver = json.get('uio_driver', 'vfio-pci') + options = json.get('options', '') + + opts = _BaseOpts.base_opts_from_json(json) + return VppInterface(name, *opts, uio_driver=uio_driver, + options=options) diff --git a/os_net_config/tests/test_impl_eni.py b/os_net_config/tests/test_impl_eni.py index 4911cb9..51354f8 100644 --- a/os_net_config/tests/test_impl_eni.py +++ b/os_net_config/tests/test_impl_eni.py @@ -18,6 +18,7 @@ import tempfile from oslo_concurrency import processutils +import os_net_config from os_net_config import impl_eni from os_net_config import objects from os_net_config.tests import base @@ -360,3 +361,33 @@ class TestENINetConfigApply(base.TestCase): self.assertEqual((_OVS_BRIDGE_DHCP + _OVS_PORT_IFACE), iface_data) self.assertIn('eth0', self.ifup_interface_names) self.assertIn('br0', self.ifup_interface_names) + + def _failed_execute(*args, **kwargs): + if kwargs.get('check_exit_code', True): + raise processutils.ProcessExecutionError('Test stderr', + 'Test stdout', + str(kwargs)) + + def test_interface_failure(self): + self.stubs.Set(processutils, 'execute', self._failed_execute) + v4_addr = objects.Address('192.168.1.2/24') + interface = objects.Interface('em1', addresses=[v4_addr]) + self.provider.add_interface(interface) + + self.assertRaises(os_net_config.ConfigurationError, + self.provider.apply) + self.assertEqual(1, len(self.provider.errors)) + + def test_interface_failure_multiple(self): + self.stubs.Set(processutils, 'execute', self._failed_execute) + v4_addr = objects.Address('192.168.1.2/24') + interface = objects.Interface('em1', addresses=[v4_addr]) + v4_addr2 = objects.Address('192.168.2.2/24') + interface2 = objects.Interface('em2', addresses=[v4_addr2]) + self.provider.add_interface(interface) + self.provider.add_interface(interface2) + + self.assertRaises(os_net_config.ConfigurationError, + self.provider.apply) + # Even though the first one failed, we should have attempted both + self.assertEqual(2, len(self.provider.errors)) diff --git a/os_net_config/tests/test_impl_ifcfg.py b/os_net_config/tests/test_impl_ifcfg.py index 82ca116..fcf2d15 100644 --- a/os_net_config/tests/test_impl_ifcfg.py +++ b/os_net_config/tests/test_impl_ifcfg.py @@ -19,6 +19,7 @@ import tempfile from oslo_concurrency import processutils +import os_net_config from os_net_config import impl_ifcfg from os_net_config import NetConfig from os_net_config import objects @@ -1230,3 +1231,33 @@ class TestIfcfgNetConfigApply(base.TestCase): self.provider.add_interface(interface) self.provider.apply() self.assertNotIn('Restart openvswitch', execute_strings) + + def _failed_execute(*args, **kwargs): + if kwargs.get('check_exit_code', True): + raise processutils.ProcessExecutionError('Test stderr', + 'Test stdout', + str(kwargs)) + + def test_interface_failure(self): + self.stubs.Set(processutils, 'execute', self._failed_execute) + v4_addr = objects.Address('192.168.1.2/24') + interface = objects.Interface('em1', addresses=[v4_addr]) + self.provider.add_interface(interface) + + self.assertRaises(os_net_config.ConfigurationError, + self.provider.apply) + self.assertEqual(1, len(self.provider.errors)) + + def test_interface_failure_multiple(self): + self.stubs.Set(processutils, 'execute', self._failed_execute) + v4_addr = objects.Address('192.168.1.2/24') + interface = objects.Interface('em1', addresses=[v4_addr]) + v4_addr2 = objects.Address('192.168.2.2/24') + interface2 = objects.Interface('em2', addresses=[v4_addr2]) + self.provider.add_interface(interface) + self.provider.add_interface(interface2) + + self.assertRaises(os_net_config.ConfigurationError, + self.provider.apply) + # Even though the first one failed, we should have attempted both + self.assertEqual(2, len(self.provider.errors)) diff --git a/os_net_config/tests/test_objects.py b/os_net_config/tests/test_objects.py index 438deef..b29bae4 100644 --- a/os_net_config/tests/test_objects.py +++ b/os_net_config/tests/test_objects.py @@ -993,3 +993,19 @@ class TestOvsDpdkBond(base.TestCase): self.assertEqual("vfio-pci", dpdk_port1.driver) iface2 = dpdk_port1.members[0] self.assertEqual("eth2", iface2.name) + + +class TestVppInterface(base.TestCase): + def test_vpp_interface_from_json(self): + data = """{ +"type": "vpp_interface", +"name": "em1", +"uio_driver": "uio_pci_generic", +"options": "vlan-strip-offload off" +} +""" + + vpp_interface = objects.object_from_json(json.loads(data)) + self.assertEqual("em1", vpp_interface.name) + self.assertEqual("uio_pci_generic", vpp_interface.uio_driver) + self.assertEqual("vlan-strip-offload off", vpp_interface.options) diff --git a/os_net_config/tests/test_utils.py b/os_net_config/tests/test_utils.py index 9e516b6..8a78946 100644 --- a/os_net_config/tests/test_utils.py +++ b/os_net_config/tests/test_utils.py @@ -21,6 +21,7 @@ import shutil import tempfile import yaml +from os_net_config import objects from os_net_config.tests import base from os_net_config import utils @@ -38,6 +39,33 @@ supports-register-dump: yes supports-priv-flags: no ''' +_VPPCTL_OUTPUT = ''' + Name Idx State Counter Count +GigabitEthernet0/9/0 1 down +local0 0 down + +''' + +_INITIAL_VPP_CONFIG = ''' +unix { + nodaemon + log /tmp/vpp.log + full-coredump +} + + +api-trace { + on +} + +api-segment { + gid vpp +} + +dpdk { +} +''' + class TestUtils(base.TestCase): @@ -83,7 +111,7 @@ class TestUtils(base.TestCase): out = _PCI_OUTPUT return out, None self.stubs.Set(processutils, 'execute', test_execute) - pci = utils._get_pci_address('nic2', False) + pci = utils.get_pci_address('nic2', False) self.assertEqual('0000:00:19.0', pci) def test_get_pci_address_exception(self): @@ -91,7 +119,7 @@ class TestUtils(base.TestCase): if 'ethtool' in name: raise processutils.ProcessExecutionError self.stubs.Set(processutils, 'execute', test_execute) - pci = utils._get_pci_address('nic2', False) + pci = utils.get_pci_address('nic2', False) self.assertEqual(None, pci) def test_get_pci_address_error(self): @@ -99,7 +127,7 @@ class TestUtils(base.TestCase): if 'ethtool' in name: return None, 'Error' self.stubs.Set(processutils, 'execute', test_execute) - pci = utils._get_pci_address('nic2', False) + pci = utils.get_pci_address('nic2', False) self.assertEqual(None, pci) def test_bind_dpdk_interfaces(self): diff --git a/os_net_config/utils.py b/os_net_config/utils.py index 84e719e..a406a13 100644 --- a/os_net_config/utils.py +++ b/os_net_config/utils.py @@ -19,6 +19,7 @@ import logging import os import re import yaml +import json from oslo_concurrency import processutils @@ -35,11 +36,21 @@ _SYS_CLASS_NET = '/sys/class/net' # driver: vfio-pci _DPDK_MAPPING_FILE = '/var/lib/os-net-config/dpdk_mapping.yaml' +# VPP startup operational configuration file. The content of this file will +# be executed when VPP starts as if typed from CLI. +_VPP_EXEC_FILE = '/etc/vpp/vpp-exec' + +HIERADATA_FILE = '/etc/puppet/hieradata/common.json' + class OvsDpdkBindException(ValueError): pass +class VppException(ValueError): + pass + + def write_config(filename, data): with open(filename, 'w') as f: f.write(str(data)) @@ -50,6 +61,10 @@ def write_yaml_config(filepath, data): with open(filepath, 'w') as f: yaml.dump(data, f, default_flow_style=False) +def write_json_config(filepath, data): + ensure_directory_presence(filepath) + with open(filepath, 'w') as f: + json.dump(data, f, indent=2) def ensure_directory_presence(filepath): dir_path = os.path.dirname(filepath) @@ -124,7 +139,11 @@ def _is_available_nic(interface_name, check_active=True): # ignored. vf_path_check = _SYS_CLASS_NET + '/%s/device/physfn' % interface_name is_sriov_vf = os.path.isdir(vf_path_check) - if is_sriov_vf: + + if (has_device_dir and operstate == 'up' and address and + not is_sriov_vf): + return True + else: return False # nic is available @@ -205,7 +224,7 @@ def diff(filename, data): def bind_dpdk_interfaces(ifname, driver, noop): - pci_address = _get_pci_address(ifname, noop) + pci_address = get_pci_address(ifname, noop) if not noop: if pci_address: # modbprobe of the driver has to be done before binding. @@ -236,7 +255,7 @@ def bind_dpdk_interfaces(ifname, driver, noop): {'name': ifname, 'driver': driver}) -def _get_pci_address(ifname, noop): +def get_pci_address(ifname, noop): # TODO(skramaja): Validate if the given interface supports dpdk if not noop: try: @@ -262,8 +281,7 @@ def _get_pci_address(ifname, noop): # is stored persistently in a file and is used to for nic numbering on # subsequent runs of os-net-config. def _update_dpdk_map(ifname, pci_address, mac_address, driver): - contents = get_file_data(_DPDK_MAPPING_FILE) - dpdk_map = yaml.load(contents) if contents else [] + dpdk_map = _get_dpdk_map() for item in dpdk_map: if item['pci_address'] == pci_address: item['name'] = ifname @@ -281,9 +299,220 @@ def _update_dpdk_map(ifname, pci_address, mac_address, driver): write_yaml_config(_DPDK_MAPPING_FILE, dpdk_map) +def _get_dpdk_map(): + contents = get_file_data(_DPDK_MAPPING_FILE) + dpdk_map = yaml.load(contents) if contents else [] + return dpdk_map + + def _get_dpdk_mac_address(name): contents = get_file_data(_DPDK_MAPPING_FILE) dpdk_map = yaml.load(contents) if contents else [] for item in dpdk_map: if item['name'] == name: return item['mac_address'] + + +def write_hiera(filename, data): + """Writes a dictionary as hiera variables to a file. + + If file already exists, the data will be appended to the file. + :param filename: file to write the hiera data to + :param data: dictionary of key,value pairs to write as hiera variables + :return: None + """ + if not isinstance(data, dict): + raise TypeError('data type must be dictionary') + + current_content = get_file_data(filename) or "{}" + current_data = json.loads(current_content) + current_data.update(data) + write_json_config(HIERADATA_FILE, current_data) + + +def restart_vpp(vpp_interfaces): + for vpp_int in vpp_interfaces: + if 'vfio-pci' in vpp_int.uio_driver: + processutils.execute('modprobe', 'vfio-pci') + logger.info('Restarting VPP') + processutils.execute('systemctl', 'restart', 'vpp') + + +def _get_vpp_interface_name(pci_addr): + """Get VPP interface name from a given PCI address + + From a running VPP instance, attempt to find the interface name from + a given PCI address of a NIC. + + :param pci_addr: PCI address to lookup, in the form of DDDD:BB:SS.F, where + - DDDD = Domain + - BB = Bus Number + - SS = Slot number + - F = Function + :return: VPP interface name. None if an interface is not found. + """ + if not pci_addr: + return None + + try: + processutils.execute('systemctl', 'is-active', 'vpp') + out, err = processutils.execute('vppctl', 'show', 'interface') + m = re.search(r':([0-9a-fA-F]{2}):([0-9a-fA-F]{2}).([0-9a-fA-F])', + pci_addr) + if m: + formatted_pci = "%x/%x/%x" % (int(m.group(1), 16), + int(m.group(2), 16), + int(m.group(3), 16)) + else: + raise VppException('Invalid PCI address format: %s' % pci_addr) + + m = re.search(r'^(\w+%s)\s+' % formatted_pci, out, re.MULTILINE) + if m: + logger.debug('VPP interface found: %s' % m.group(1)) + return m.group(1) + else: + logger.debug('Interface with pci address %s not bound to VPP' + % pci_addr) + return None + except processutils.ProcessExecutionError: + logger.debug('Interface with pci address %s not bound to vpp' % + pci_addr) + + +def generate_vpp_config(vpp_config_path, vpp_interfaces): + """Generate configuration content for VPP + + Generate interface related configuration content for VPP. Current + configuration will be preserved, with interface related configurations + updated or inserted. The config only affects dpdk section of VPP config + file, and only those lines affecting interfaces, specifically, lines + containing the following: + dpdk { + ... + dev {} + uio_driver + ... + } + + :param vpp_config_path: VPP Configuration file path + :param vpp_interfaces: List of VPP interface objects + :return: updated VPP config content. + """ + + data = get_file_data(vpp_config_path) + + # Add interface config to dpdk section + for vpp_interface in vpp_interfaces: + if vpp_interface.pci_dev: + logger.info('vpp interface %s pci dev: %s' + % (vpp_interface.name, vpp_interface.pci_dev)) + + if vpp_interface.options: + int_cfg = '%s {%s}' % (vpp_interface.pci_dev, + vpp_interface.options) + else: + int_cfg = vpp_interface.pci_dev + + # make sure dpk section exists in the config + if not re.search(r'^\s*dpdk\s*\{', data, re.MULTILINE): + data += "\ndpdk {\n}\n" + + m = re.search(r'^\s*dev\s+%s\s*(\{[^}]*\})?\s*' + % vpp_interface.pci_dev, data, + re.IGNORECASE | re.MULTILINE) + if m: + data = re.sub(m.group(0), ' dev %s\n' % int_cfg, data) + else: + data = re.sub(r'(^\s*dpdk\s*\{)', + r'\1\n dev %s\n' % int_cfg, + data, + flags=re.MULTILINE) + + if vpp_interface.uio_driver: + m = re.search(r'^\s*uio-driver.*$', data, re.MULTILINE) + if m: + data = re.sub(m.group(0), r' uio-driver %s' + % vpp_interface.uio_driver, data) + else: + data = re.sub(r'(^\s*dpdk\s*\{)', + r'\1\n uio-driver %s' + % vpp_interface.uio_driver, + data, + flags=re.MULTILINE) + else: + logger.debug('pci address not found for interface %s, may have' + 'already been bound to vpp' % vpp_interface.name) + + # Add start up script for VPP to config. This script will be executed by + # VPP on service start. + if not re.search(r'^\s*unix\s*\{', data, re.MULTILINE): + data += "\nunix {\n}\n" + + m = re.search(r'^\s*(exec|startup-config).*$', + data, + re.IGNORECASE | re.MULTILINE) + if m: + data = re.sub(m.group(0), ' exec %s' % _VPP_EXEC_FILE, data) + else: + data = re.sub(r'(^\s*unix\s*\{)', + r'\1\n exec %s' % _VPP_EXEC_FILE, + data, + flags=re.MULTILINE) + # Make sure startup script exists to avoid VPP startup failure. + open(_VPP_EXEC_FILE, 'a').close() + + return data + + +def update_vpp_mapping(vpp_interfaces): + """Verify VPP interface binding and update mapping file + + VppException will be raised if interfaces are not properly bound. + + :param vpp_interfaces: List of VPP interface objects + """ + vpp_start_cli = "" + + for vpp_int in vpp_interfaces: + if not vpp_int.pci_dev: + dpdk_map = _get_dpdk_map() + for dpdk_int in dpdk_map: + if dpdk_int['name'] == vpp_int.name: + vpp_int.pci_dev = dpdk_int['pci_address'] + break + else: + raise VppException('Interface %s has no PCI address and is not' + ' found in mapping file' % vpp_int.name) + + for i in range(2): + vpp_name = _get_vpp_interface_name(vpp_int.pci_dev) + if not vpp_name: + restart_vpp(vpp_interfaces) + else: + break + else: + raise VppException('Interface %s with pci address %s not ' + 'bound to vpp' + % (vpp_int.name, vpp_int.pci_dev)) + + # Generate content of startup script for VPP + for address in vpp_int.addresses: + vpp_start_cli += 'set interface state %s up\n' % vpp_name + vpp_start_cli += 'set interface ip address %s %s/%s\n' \ + % (vpp_name, address.ip, address.prefixlen) + + logger.info('Updating mapping for vpp interface %s:' + 'pci_dev: %s mac address: %s uio driver: %s' + % (vpp_int.name, vpp_int.pci_dev, vpp_int.hwaddr, + vpp_int.uio_driver)) + _update_dpdk_map(vpp_int.name, vpp_int.pci_dev, vpp_int.hwaddr, + vpp_int.uio_driver) + write_hiera(HIERADATA_FILE, {vpp_int.name: vpp_name}) + + # Enable VPP service to make the VPP interface configuration + # persistent. + processutils.execute('systemctl', 'enable', 'vpp') + + if diff(_VPP_EXEC_FILE, vpp_start_cli): + write_config(_VPP_EXEC_FILE, vpp_start_cli) + restart_vpp(vpp_interfaces) -- cgit 1.2.3-korg