diff --git a/files/neutron-openvswitch-networking-sriov.service b/files/neutron-openvswitch-networking-sriov.service deleted file mode 100644 index c78e5453..00000000 --- a/files/neutron-openvswitch-networking-sriov.service +++ /dev/null @@ -1,19 +0,0 @@ -[Unit] -Description=Configure SRIOV Virtual Functions -DefaultDependencies=no -Wants=network.target -After=local-fs.target network-pre.target apparmor.service systemd-sysctl.service systemd-modules-load.service -Before=network.target shutdown.target network-online.target -Conflicts=shutdown.target - -[Install] -WantedBy=multi-user.target -WantedBy=network-online.target - -[Service] -Type=oneshot -EnvironmentFile=-/etc/default/networking-sriov -ExecStart=/usr/local/bin/neutron-openvswitch-networking-sriov.sh systemd-start -ExecStop=/usr/local/bin/neutron-openvswitch-networking-sriov.sh systemd-stop -RemainAfterExit=true -TimeoutStartSec=5min diff --git a/files/neutron-openvswitch-networking-sriov.sh b/files/neutron-openvswitch-networking-sriov.sh deleted file mode 100755 index 7e1045ef..00000000 --- a/files/neutron-openvswitch-networking-sriov.sh +++ /dev/null @@ -1,73 +0,0 @@ -#!/bin/sh - -### BEGIN INIT INFO -# Provides: networking-sriov -# Required-Start: mountkernfs $local_fs -# Required-Stop: $local_fs -# Default-Start: S -# Default-Stop: 0 6 -# Short-Description: Configure SRIOV Virtual Functions -### END INIT INFO - -# Authors: Frode Nordahl - -DESC="Configure SRIOV Virtual Functions" - -. /lib/lsb/init-functions - -# Include defaults if available -if [ -f /etc/default/neutron-openvswitch-networking-sriov ] ; then - . /etc/default/neutron-openvswitch-networking-sriov -fi - -if [ -z "${ENABLE}" ]; then - ENABLE=0 -fi - -if [ -z "${VFS_BLANKET}" ]; then - VFS_BLANKET=auto -fi - -# Exit if feature is not enabled -[ $ENABLE -gt 0 ] || exit 0 - -do_start() { - /usr/local/bin/neutron_openvswitch_networking_sriov.py --start --vfs "${VFS_LIST}" --vfs-blanket "${VFS_BLANKET}" -} - -do_restart() { - /usr/local/bin/neutron_openvswitch_networking_sriov.py --restart --vfs "${VFS_LIST}" --vfs-blanket "${VFS_BLANKET}" -} - -do_stop() { - /usr/local/bin/neutron_openvswitch_networking_sriov.py --stop --vfs "${VFS_LIST}" --vfs-blanket "${VFS_BLANKET}" -} - - -case "$1" in - start) - log_daemon_msg "$DESC" - do_start - ;; - stop) - log_daemon_msg "Un-$DESC" - do_stop - ;; - systemd-start) - do_start - ;; - systemd-stop) - do_stop - ;; - restart) - log_daemon_msg "Re-$DESC" - do_stop - do_start - ;; - *) - N=/usr/local/bin/neutron-openvswitch-networking-sriov.sh - echo "Usage: $N {start|stop|restart|systemd-start|systemd-stop}" >&2 - ;; -esac - -exit 0 diff --git a/files/neutron_openvswitch_networking_sriov.py b/files/neutron_openvswitch_networking_sriov.py deleted file mode 100755 index 2ea03aef..00000000 --- a/files/neutron_openvswitch_networking_sriov.py +++ /dev/null @@ -1,220 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright 2019 Canonical Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License - -import argparse -import os -import sys -import time - - -def wait_for_vfs(device, numvfs): - """Wait for the VFs to be created. - - :param str device: path to the NIC - :param str numvfs: number of VFs to wait for - :returns: None - """ - numvfs = int(numvfs) - created_vfs_number = 0 - while created_vfs_number < numvfs: - created_vfs = [f for f in os.listdir(device) if f.startswith('virtfn')] - created_vfs_number = len(created_vfs) - print('Waiting for {numvfs} VFs to appear in {dev}'.format( - numvfs=numvfs, dev=device)) - time.sleep(0.05) - - -def get_totalvfs(device): - """Get the number of allowed VFs. - - :param str device: Path of the device - :returns: number of allowed VFs. - :rtype: str - """ - path = os.path.join(device, 'sriov_totalvfs') - with open(path) as f: - total_vfs = f.read() - return total_vfs - - -def find_sriov_devices(): - """Find SR-IOV devices. - - :returns: list of SR-IOV devices' paths - :rtype: list - """ - f_name = 'sriov_totalvfs' - devices = [] - for path, dirs, files in os.walk('/sys/devices'): - if f_name in files: - devices.append(path) - return devices - - -def write_sriov_numvfs(base_path, numvfs): - """Write the number of VFs to file. - - :param str base_path: Path of the device - :param str numvfs: Number of VFs - :returns: None - """ - path = os.path.join(base_path, 'sriov_numvfs') - print('Configuring {numvfs} VFs for {path}'.format( - numvfs=numvfs, path=path)) - try: - with open(path, 'w') as f: - f.write(numvfs) - wait_for_vfs(base_path, numvfs) - except OSError as err: - print( - 'Error while configuring VFs: {err}'.format(err=err)) - - -def configure_vfs(vfs, vfs_blanket='auto', stop=False): - """Configure the VFs. - - :param dict vfs: list of VFs as dict (e.g. {'eth0': '8', 'eth1': '4'} - :param str vfs_blanket: blanket config for the VFs - :param bool stop: If we are stopping - :returns: None - """ - if vfs: - for device, numvfs in vfs.items(): - if stop: - numvfs = '0' - base_path = '/sys/class/net/{dev}/device/'.format(dev=device) - write_sriov_numvfs(base_path, numvfs) - else: - sriov_devices = find_sriov_devices() - for device in sriov_devices: - total_vfs = get_totalvfs(device) - if stop: - vfs_blanket = '0' - if vfs_blanket == 'auto': - numvfs = total_vfs - elif int(vfs_blanket) > int(total_vfs): - numvfs = total_vfs - else: - numvfs = vfs_blanket - write_sriov_numvfs(device, numvfs) - - -def restart(vfs, vfs_blanket): - """Restart the VFs - - :param dict vfs: list of VFs as dict (e.g. {'eth0': '8', 'eth1': '4'} - :returns: None - """ - stop(vfs) - start(vfs, vfs_blanket) - - -def start(vfs, vfs_blanket): - """Start the VFs. - - :param dict vfs: list of VFs as dict (e.g. {'eth0': '8', 'eth1': '4'} - :returns: None - """ - configure_vfs(vfs, vfs_blanket) - - -def stop(vfs): - """Stop the VFs. - - :param dict vfs: list of VFs as dict (e.g. {'eth0': '8', 'eth1': '4'} - :returns: None - """ - configure_vfs(vfs, stop=True) - - -def parse_vfs(vfs=None): - """Parse VFs from string - - :param dict vfs: string containing the VFs - :returns: dict of VFs - :rtype: dict { - 'eth0': '8', - 'eth1': '2' - } - """ - if not vfs: - return {} - parsed_vfs = {} - for vf in vfs.split(): - k, v = vf.split(':') - parsed_vfs[k] = v - return parsed_vfs - - -def parse_args(args): - """Parse the arguments. - - : param list args: list of args - : returns: the parsed arguments - : rtype: Namespace - : raises SystemExit: if there are missing required args - """ - parser = argparse.ArgumentParser() - parser.add_argument( - '--start', - help='Start', - action='store_true', - default=False - ) - parser.add_argument( - '--stop', - help='Stop', - action='store_true', - default=False - ) - parser.add_argument( - '--restart', - help='Restart', - action='store_true', - default=False - ) - parser.add_argument( - '--vfs', - help='VFS List' - ) - parser.add_argument( - '--vfs-blanket', - help='VFS Blanket', - default='auto' - ) - return parser.parse_args(args) - - -def main(args): - """Main function. - - : param list args: list of arguments - : returns: 0 - : rtype: int - """ - args = parse_args(args) - vfs = parse_vfs(args.vfs) - if args.restart: - restart(vfs, args.vfs_blanket) - elif args.start: - start(vfs, args.vfs_blanket) - elif args.stop: - stop(vfs) - return 0 - - -if __name__ == '__main__': - sys.exit(main(sys.argv[1:])) diff --git a/hooks/neutron_ovs_utils.py b/hooks/neutron_ovs_utils.py index 0693fd0e..3e071d41 100644 --- a/hooks/neutron_ovs_utils.py +++ b/hooks/neutron_ovs_utils.py @@ -144,21 +144,6 @@ OVS_DEFAULT = '/etc/default/openvswitch-switch' DPDK_INTERFACES = '/etc/dpdk/interfaces' NEUTRON_SRIOV_AGENT_CONF = os.path.join(NEUTRON_CONF_DIR, 'plugins/ml2/sriov_agent.ini') -NEUTRON_SRIOV_INIT_SCRIPT = os.path.join('/usr/local/bin', - 'neutron-openvswitch-' - 'networking-sriov.sh') -NEUTRON_SRIOV_INIT_PY_SCRIPT = os.path.join('/usr/local/bin', - 'neutron_openvswitch_' - 'networking_sriov.py') -NEUTRON_SRIOV_INIT_DEFAULT = os.path.join('/etc/default', - 'neutron-openvswitch-' - 'networking-sriov') -NEUTRON_SRIOV_SYSTEMD_UNIT = os.path.join('/lib/systemd/system', - 'neutron-openvswitch-' - 'networking-sriov.service') -NEUTRON_SRIOV_UPSTART_CONF = os.path.join('/etc/init', - 'neutron-openvswitch-' - 'networking-sriov.conf') USE_FQDN_KEY = 'neutron-ovs-charm-use-fqdn' @@ -246,14 +231,6 @@ SRIOV_RESOURCE_MAP = OrderedDict([ 'services': ['neutron-sriov-agent'], 'contexts': [neutron_ovs_context.OVSPluginContext()], }), - (NEUTRON_SRIOV_INIT_DEFAULT, { - 'services': [], - 'contexts': [neutron_ovs_context.OVSPluginContext()], - }), - (NEUTRON_SRIOV_UPSTART_CONF, { - 'services': [], - 'contexts': [], - }), ]) TEMPLATES = 'templates/' diff --git a/unit_tests/test_neutron_openvswitch_networking_sriov.py b/unit_tests/test_neutron_openvswitch_networking_sriov.py deleted file mode 100644 index 91f49413..00000000 --- a/unit_tests/test_neutron_openvswitch_networking_sriov.py +++ /dev/null @@ -1,223 +0,0 @@ -# Copyright 2019 Canonical Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License - -import unittest -from unittest.mock import ( - call, - mock_open, - patch -) -import files.neutron_openvswitch_networking_sriov as net_sriov - - -class NeutronOVSNetworkingSriovTest(unittest.TestCase): - - def test_parse_args(self): - results = net_sriov.parse_args(['--start']) - self.assertEqual(results.start, True) - results = net_sriov.parse_args(['--stop']) - self.assertEqual(results.stop, True) - results = net_sriov.parse_args(['--restart']) - self.assertEqual(results.restart, True) - args = [ - '--start', - '--vfs', - 'eth0:8 eth1:4', - '--vfs-blanket', - '8' - ] - results = net_sriov.parse_args(args) - self.assertEqual(results.vfs, 'eth0:8 eth1:4') - self.assertEqual(results.vfs_blanket, '8') - - def test_parse_vfs(self): - vfs = '' - result = net_sriov.parse_vfs(vfs) - expected = {} - self.assertEqual(result, expected) - vfs = 'eth0:8 eth1:8' - result = net_sriov.parse_vfs(vfs) - expected = { - 'eth0': '8', - 'eth1': '8' - } - self.assertEqual(result, expected) - - @patch('files.neutron_openvswitch_networking_sriov.write_sriov_numvfs') - def test_configure_vfs_with_vfs(self, m_write_vfs): - vfs = { - 'eth0': '8' - } - net_sriov.configure_vfs(vfs) - m_write_vfs.assert_called_once_with( - '/sys/class/net/eth0/device/', - '8' - ) - - @patch('files.neutron_openvswitch_networking_sriov.get_totalvfs') - @patch('files.neutron_openvswitch_networking_sriov.find_sriov_devices') - @patch('files.neutron_openvswitch_networking_sriov.write_sriov_numvfs') - def test_configure_vfs_with_blanket_auto( - self, - m_write_vfs, - m_find, - m_totvfs): - m_find.return_value = [ - '/dev/one', - '/dev/two' - ] - m_totvfs.return_value = '8' - net_sriov.configure_vfs({}, 'auto') - m_write_vfs.assert_has_calls([ - call('/dev/one', '8'), - call('/dev/two', '8') - ]) - - @patch('files.neutron_openvswitch_networking_sriov.get_totalvfs') - @patch('files.neutron_openvswitch_networking_sriov.find_sriov_devices') - @patch('files.neutron_openvswitch_networking_sriov.write_sriov_numvfs') - def test_configure_vfs_with_blanket_too_big( - self, - m_write_vfs, - m_find, - m_totvfs): - m_find.return_value = [ - '/dev/one', - '/dev/two' - ] - m_totvfs.return_value = '2' - net_sriov.configure_vfs({}, '8') - m_write_vfs.assert_has_calls([ - call('/dev/one', '2'), - call('/dev/two', '2') - ]) - - @patch('files.neutron_openvswitch_networking_sriov.get_totalvfs') - @patch('files.neutron_openvswitch_networking_sriov.find_sriov_devices') - @patch('files.neutron_openvswitch_networking_sriov.write_sriov_numvfs') - def test_configure_vfs_with_blanket_smaller_than_totalvfs( - self, - m_write_vfs, - m_find, - m_totvfs): - m_find.return_value = [ - '/dev/one', - '/dev/two' - ] - m_totvfs.return_value = '8' - net_sriov.configure_vfs({}, '2') - m_write_vfs.assert_has_calls([ - call('/dev/one', '2'), - call('/dev/two', '2') - ]) - - @patch('time.sleep') - @patch('os.listdir') - def test_wait_for_vfs(self, m_listdir, m_sleep): - dev_list = [ - 'subsystem_device', - 'subsystem_vendor', - 'uevent', - 'vendor', - 'virtfn0', - 'virtfn1', - 'virtfn2', - 'virtfn3', - ] - m_listdir.return_value = dev_list - net_sriov.wait_for_vfs('dev', '4') - m_sleep.assert_called_once_with(0.05) - - @patch('os.walk') - def test_find_sriov_devices(self, m_walk): - m_walk.return_value = [ - ('/one', ('dir1', 'dir2'), ('file')), - ('/one/dir1', (), ('sriov_totalvfs')), - ('/one/dir2', (), ('sriov_totalvfs')) - ] - results = net_sriov.find_sriov_devices() - expected = [ - '/one/dir1', - '/one/dir2' - ] - self.assertEqual(results, expected) - - @patch('builtins.open', new_callable=mock_open) - def test_get_totalvfs(self, m_open): - net_sriov.get_totalvfs('/dev/some') - m_open.assert_called_once_with( - '/dev/some/sriov_totalvfs' - ) - - @patch('files.neutron_openvswitch_networking_sriov.wait_for_vfs') - @patch('builtins.open', new_callable=mock_open) - def test_write_sriov_numvfs(self, m_open, m_wait_vfs): - net_sriov.write_sriov_numvfs('/dev/sriov', '8') - m_open.assert_called_once_with( - '/dev/sriov/sriov_numvfs', - 'w' - ) - m_wait_vfs.assert_called_once_with( - '/dev/sriov', - '8' - ) - - @patch('files.neutron_openvswitch_networking_sriov.start') - @patch('files.neutron_openvswitch_networking_sriov.stop') - def test_restart(self, m_stop, m_start): - net_sriov.restart([], 'auto') - m_stop.assert_called_once_with([]) - m_start.assert_called_once_with([], 'auto') - - @patch('files.neutron_openvswitch_networking_sriov.configure_vfs') - def test_start(self, m_config): - net_sriov.start([], 'auto') - m_config.assert_called_once_with([], 'auto') - - @patch('files.neutron_openvswitch_networking_sriov.configure_vfs') - def test_stop(self, m_config): - net_sriov.stop([]) - m_config.assert_called_once_with([], stop=True) - - @patch('files.neutron_openvswitch_networking_sriov.restart') - def test_main_with_restart(self, m_restart): - args = ['--restart', '--vfs', 'eth0:8 eth1:2', '--vfs-blanket', 'auto'] - net_sriov.main(args) - vfs = { - 'eth0': '8', - 'eth1': '2' - } - vfs_blanket = 'auto' - m_restart.assert_called_once_with(vfs, vfs_blanket) - - @patch('files.neutron_openvswitch_networking_sriov.start') - def test_main_with_start(self, m_start): - args = ['--start', '--vfs', 'eth0:8 eth1:2', '--vfs-blanket', 'auto'] - net_sriov.main(args) - vfs = { - 'eth0': '8', - 'eth1': '2' - } - vfs_blanket = 'auto' - m_start.assert_called_once_with(vfs, vfs_blanket) - - @patch('files.neutron_openvswitch_networking_sriov.stop') - def test_main_with_stop(self, m_stop): - args = ['--stop', '--vfs', 'eth0:8 eth1:2', '--vfs-blanket', 'auto'] - net_sriov.main(args) - vfs = { - 'eth0': '8', - 'eth1': '2' - } - m_stop.assert_called_once_with(vfs)