| 1 |
# Copyright (C) 2018 AT&T |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# 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, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
|
| 16 |
# |
| 17 |
# This class implements a utility functions for SRIOV NIC Switch Driver |
| 18 |
# |
| 19 |
|
| 20 |
import copy |
| 21 |
import re |
| 22 |
from unittest import mock |
| 23 |
|
| 24 |
from neutron_taas.common import utils as common_utils |
| 25 |
from neutron_taas.services.taas.drivers.linux import sriov_nic_exceptions \ |
| 26 |
as taas_exc |
| 27 |
from neutron_taas.services.taas.drivers.linux import sriov_nic_utils |
| 28 |
from neutron_taas.tests import base |
| 29 |
|
| 30 |
FAKE_SRIOV_PORT = { |
| 31 |
'id': 'fake_1', 'mac_address': "52:54:00:12:35:02", |
| 32 |
'binding:profile': { |
| 33 |
'pci_slot': None}, 'binding:vif_details': {'vlan': 20} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
class TestSriovNicUtils(base.TaasTestCase): |
| 38 |
def setUp(self): |
| 39 |
super().setUp() |
| 40 |
|
| 41 |
def test_get_sysfs_netdev_path_with_pf_interface(self): |
| 42 |
self.assertEqual( |
| 43 |
"/sys/bus/pci/devices/12/physfn/net", |
| 44 |
sriov_nic_utils.SriovNicUtils(). |
| 45 |
_get_sysfs_netdev_path(12, True)) |
| 46 |
|
| 47 |
def test_get_sysfs_netdev_path_without_pf_interface(self): |
| 48 |
self.assertEqual( |
| 49 |
"/sys/bus/pci/devices/12/net", |
| 50 |
sriov_nic_utils.SriovNicUtils(). |
| 51 |
_get_sysfs_netdev_path(12, False)) |
| 52 |
|
| 53 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 54 |
def test_get_ifname_by_pci_address(self, mock_os): |
| 55 |
mock_os.listdir.return_value = ['random1', 'random2'] |
| 56 |
self.assertEqual(sriov_nic_utils.SriovNicUtils(). |
| 57 |
get_ifname_by_pci_address(12, False), 'random2') |
| 58 |
|
| 59 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 60 |
def test_get_ifname_by_pci_address_no_dev_info(self, mock_os): |
| 61 |
mock_os.listdir.return_value = list() |
| 62 |
self.assertRaises( |
| 63 |
taas_exc.PciDeviceNotFoundById, |
| 64 |
sriov_nic_utils.SriovNicUtils().get_ifname_by_pci_address, 12, 9) |
| 65 |
|
| 66 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 67 |
@mock.patch.object(sriov_nic_utils, 'open', create=True) |
| 68 |
def test_get_mac_by_pci_address(self, mock_open, mock_os): |
| 69 |
mock_os.listdir.return_value = ['random1', 'random2'] |
| 70 |
mock_os.path.join.return_value = 'random' |
| 71 |
fake_file_handle = ["52:54:00:12:35:02"] |
| 72 |
fake_file_iter = fake_file_handle.__iter__() |
| 73 |
mock_open.return_value.__enter__.return_value = fake_file_iter |
| 74 |
self.assertEqual( |
| 75 |
"52:54:00:12:35:02", sriov_nic_utils.SriovNicUtils(). |
| 76 |
get_mac_by_pci_address(12, False)) |
| 77 |
|
| 78 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 79 |
@mock.patch.object(sriov_nic_utils, 'open', create=True) |
| 80 |
def test_get_mac_by_pci_address_no_content(self, mock_open, mock_os): |
| 81 |
mock_os.listdir.return_value = ['random1', 'random2'] |
| 82 |
mock_os.path.join.return_value = 'random' |
| 83 |
fake_file_handle = [] |
| 84 |
fake_file_iter = fake_file_handle.__iter__() |
| 85 |
mock_open.return_value.__enter__.return_value = fake_file_iter |
| 86 |
self.assertRaises( |
| 87 |
taas_exc.PciDeviceNotFoundById, |
| 88 |
sriov_nic_utils.SriovNicUtils().get_mac_by_pci_address, 12, False) |
| 89 |
|
| 90 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 91 |
def test_get_mac_by_pci_address_wrong_dev_path(self, mock_os): |
| 92 |
mock_os.listdir.return_value = ['random1', 'random2'] |
| 93 |
mock_os.path.join.return_value = 'random' |
| 94 |
self.assertRaises( |
| 95 |
taas_exc.PciDeviceNotFoundById, |
| 96 |
sriov_nic_utils.SriovNicUtils().get_mac_by_pci_address, 12, False) |
| 97 |
|
| 98 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 99 |
@mock.patch.object(sriov_nic_utils, 'open', create=True) |
| 100 |
def test_get_net_name_by_vf_pci_address(self, mock_open, mock_os): |
| 101 |
mock_os.listdir.return_value = ['enp0s3', 'enp0s2'] |
| 102 |
mock_os.path.join.return_value = 'random' |
| 103 |
fake_file_handle = ["52:54:00:12:35:02"] |
| 104 |
fake_file_iter = fake_file_handle.__iter__() |
| 105 |
mock_open.return_value.__enter__.return_value = fake_file_iter |
| 106 |
self.assertEqual( |
| 107 |
'net_enp0s3_52_54_00_12_35_02', |
| 108 |
sriov_nic_utils.SriovNicUtils(). |
| 109 |
get_net_name_by_vf_pci_address(12)) |
| 110 |
|
| 111 |
def _common_merge_utility(self, value): |
| 112 |
output_list = list() |
| 113 |
for v in value: |
| 114 |
output_list.append(v) |
| 115 |
return output_list |
| 116 |
|
| 117 |
def test_get_ranges_str_from_list(self): |
| 118 |
input_list = [4, 11, 12, 13, 25, 26, 27] |
| 119 |
self.assertEqual("4,11-13,25-27", common_utils. |
| 120 |
get_ranges_str_from_list(input_list)) |
| 121 |
|
| 122 |
def test_get_list_from_ranges_str(self): |
| 123 |
input_str = "4,6,10-13,25-27" |
| 124 |
expected_output = [4, 6, 10, 11, 12, 13, 25, 26, 27] |
| 125 |
self.assertEqual(expected_output, common_utils. |
| 126 |
get_list_from_ranges_str(input_str)) |
| 127 |
|
| 128 |
def test_get_vf_num_by_pci_address_neg(self): |
| 129 |
self.assertRaises( |
| 130 |
taas_exc.PciDeviceNotFoundById, |
| 131 |
sriov_nic_utils.SriovNicUtils().get_vf_num_by_pci_address, 12) |
| 132 |
|
| 133 |
@mock.patch.object(sriov_nic_utils, 'glob') |
| 134 |
@mock.patch.object(sriov_nic_utils, 're') |
| 135 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 136 |
def test_get_vf_num_by_pci_address(self, mock_os, mock_re, mock_glob): |
| 137 |
mock_glob.iglob.return_value = ['file1'] |
| 138 |
mock_os.readlink.return_value = 12 |
| 139 |
mock_re.compile().search.return_value = re.match(r"(\d+)", "89") |
| 140 |
self.assertEqual( |
| 141 |
'89', sriov_nic_utils.SriovNicUtils(). |
| 142 |
get_vf_num_by_pci_address(12)) |
| 143 |
|
| 144 |
@mock.patch.object(sriov_nic_utils, 'glob') |
| 145 |
@mock.patch.object(sriov_nic_utils, 're') |
| 146 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 147 |
@mock.patch.object(sriov_nic_utils, 'open', create=True) |
| 148 |
@mock.patch.object(sriov_nic_utils, 'portbindings') |
| 149 |
def test_get_sriov_port_params(self, mock_port_bindings, mock_open, |
| 150 |
mock_os, mock_re, mock_glob): |
| 151 |
sriov_port = copy.deepcopy(FAKE_SRIOV_PORT) |
| 152 |
fake_profile = mock_port_bindings.PROFILE = 'binding:profile' |
| 153 |
mock_port_bindings.VIF_DETAILS = 'binding:vif_details' |
| 154 |
sriov_port[fake_profile]['pci_slot'] = 3 |
| 155 |
mock_glob.iglob.return_value = ['file1'] |
| 156 |
mock_os.readlink.return_value = 12 |
| 157 |
mock_re.compile().search.return_value = re.match(r"(\d+)", "89") |
| 158 |
mock_os.listdir.return_value = ['net_enp0s2_52_54_00_12_35_02', |
| 159 |
'net_enp0s3_52_54_00_12_35_02'] |
| 160 |
mock_os.path.join.return_value = 'random' |
| 161 |
fake_file_handle = ["52:54:00:12:35:02"] |
| 162 |
fake_file_iter = fake_file_handle.__iter__() |
| 163 |
mock_open.return_value.__enter__.return_value = fake_file_iter |
| 164 |
expected_output = { |
| 165 |
'mac': '52:54:00:12:35:02', 'pci_slot': 3, 'vf_index': '89', |
| 166 |
'pf_device': 'net_enp0s3_52_54_00_12_35_02', 'src_vlans': 20} |
| 167 |
self.assertEqual( |
| 168 |
expected_output, sriov_nic_utils.SriovNicUtils(). |
| 169 |
get_sriov_port_params(sriov_port)) |
| 170 |
|
| 171 |
@mock.patch.object(sriov_nic_utils, 'glob') |
| 172 |
@mock.patch.object(sriov_nic_utils, 're') |
| 173 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 174 |
@mock.patch.object(sriov_nic_utils, 'open', create=True) |
| 175 |
@mock.patch.object(sriov_nic_utils, 'portbindings') |
| 176 |
def test_get_sriov_port_params_no_pci_slot(self, mock_port_bindings, |
| 177 |
mock_open, mock_os, mock_re, |
| 178 |
mock_glob): |
| 179 |
sriov_port = copy.deepcopy(FAKE_SRIOV_PORT) |
| 180 |
mock_port_bindings.PROFILE = 'binding:profile' |
| 181 |
mock_port_bindings.VIF_DETAILS = 'binding:vif_details' |
| 182 |
mock_glob.iglob.return_value = ['file1'] |
| 183 |
mock_os.readlink.return_value = 12 |
| 184 |
mock_re.compile().search.return_value = re.match(r"(\d+)", "89") |
| 185 |
mock_os.listdir.return_value = ['enp0s3', 'enp0s2'] |
| 186 |
mock_os.path.join.return_value = 'random' |
| 187 |
fake_file_handle = ["52:54:00:12:35:02"] |
| 188 |
fake_file_iter = fake_file_handle.__iter__() |
| 189 |
mock_open.return_value.__enter__.return_value = fake_file_iter |
| 190 |
self.assertIsNone(sriov_nic_utils.SriovNicUtils(). |
| 191 |
get_sriov_port_params(sriov_port)) |
| 192 |
|
| 193 |
@mock.patch.object(sriov_nic_utils, 'utils') |
| 194 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 195 |
def test_execute_sysfs_command_egress_add(self, mock_os, |
| 196 |
mock_neutron_utils): |
| 197 |
sriov_nic_utils.SriovNicUtils().execute_sysfs_command( |
| 198 |
'add', {'pf_device': 'p2p1', 'vf_index': '9'}, {'vf_index': '18'}, |
| 199 |
"4,11-13", True, "OUT") |
| 200 |
egress_cmd = ['i40e_sysfs_command', 'p2p1', '18', |
| 201 |
'egress_mirror', 'add', '9'] |
| 202 |
mock_neutron_utils.execute.assert_called_once_with( |
| 203 |
egress_cmd, run_as_root=True, privsep_exec=True) |
| 204 |
|
| 205 |
@mock.patch.object(sriov_nic_utils, 'utils') |
| 206 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 207 |
def test_execute_sysfs_command_ingress_add(self, mock_os, |
| 208 |
mock_neutron_utils): |
| 209 |
sriov_nic_utils.SriovNicUtils().execute_sysfs_command( |
| 210 |
'add', {'pf_device': 'p2p1', 'vf_index': '9'}, {'vf_index': '18'}, |
| 211 |
"4,11-13", True, "IN") |
| 212 |
ingress_cmd = ['i40e_sysfs_command', 'p2p1', '18', |
| 213 |
'ingress_mirror', 'add', '9'] |
| 214 |
mock_neutron_utils.execute.assert_called_once_with( |
| 215 |
ingress_cmd, run_as_root=True, privsep_exec=True) |
| 216 |
|
| 217 |
@mock.patch.object(sriov_nic_utils, 'utils') |
| 218 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 219 |
def test_execute_sysfs_command_both_add( |
| 220 |
self, mock_os, mock_neutron_utils): |
| 221 |
sriov_nic_utils.SriovNicUtils().execute_sysfs_command( |
| 222 |
'add', {'pf_device': 'p2p1', 'vf_index': '9'}, {'vf_index': '18'}, |
| 223 |
"4,11-13", True, "BOTH") |
| 224 |
self.assertEqual(2, mock_neutron_utils.execute.call_count) |
| 225 |
|
| 226 |
@mock.patch.object(sriov_nic_utils, 'utils') |
| 227 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 228 |
def test_execute_sysfs_command_egress_rem(self, mock_os, |
| 229 |
mock_neutron_utils): |
| 230 |
sriov_nic_utils.SriovNicUtils().execute_sysfs_command( |
| 231 |
'rem', {'pf_device': 'p2p1', 'vf_index': '9'}, {'vf_index': '18'}, |
| 232 |
"4,11-13", True, "OUT") |
| 233 |
egress_cmd = ['i40e_sysfs_command', 'p2p1', '18', |
| 234 |
'egress_mirror', 'rem', '9'] |
| 235 |
mock_neutron_utils.execute.assert_called_once_with( |
| 236 |
egress_cmd, run_as_root=True, privsep_exec=True) |
| 237 |
|
| 238 |
@mock.patch.object(sriov_nic_utils, 'utils') |
| 239 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 240 |
def test_execute_sysfs_command_ingress_rem(self, mock_os, |
| 241 |
mock_neutron_utils): |
| 242 |
sriov_nic_utils.SriovNicUtils().execute_sysfs_command( |
| 243 |
'rem', {'pf_device': 'p2p1', 'vf_index': '9'}, {'vf_index': '18'}, |
| 244 |
"4,11-13", True, "IN") |
| 245 |
ingress_cmd = ['i40e_sysfs_command', 'p2p1', '18', |
| 246 |
'ingress_mirror', 'rem', '9'] |
| 247 |
mock_neutron_utils.execute.assert_called_once_with( |
| 248 |
ingress_cmd, run_as_root=True, privsep_exec=True) |
| 249 |
|
| 250 |
@mock.patch.object(sriov_nic_utils, 'utils') |
| 251 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 252 |
def test_execute_sysfs_command_both_rem( |
| 253 |
self, mock_os, mock_neutron_utils): |
| 254 |
sriov_nic_utils.SriovNicUtils().execute_sysfs_command( |
| 255 |
'rem', {'pf_device': 'p2p1', 'vf_index': '9'}, {'vf_index': '18'}, |
| 256 |
"4,11-13", True, "BOTH") |
| 257 |
self.assertEqual(2, mock_neutron_utils.execute.call_count) |
| 258 |
|
| 259 |
@mock.patch.object(sriov_nic_utils, 'utils') |
| 260 |
@mock.patch.object(sriov_nic_utils, 'os') |
| 261 |
def test_execute_sysfs_command_not_both_vf_to_vf_all_vlans_False( |
| 262 |
self, mock_os, mock_neutron_utils): |
| 263 |
cmd = ['i40e_sysfs_command', 'p2p1', '9', |
| 264 |
'vlan_mirror', 'rem', '4,11-13'] |
| 265 |
sriov_nic_utils.SriovNicUtils().execute_sysfs_command( |
| 266 |
'rem', {'pf_device': 'p2p1', 'vf_index': '9'}, {'vf_index': '18'}, |
| 267 |
"4,11-13", False, "FAKE") |
| 268 |
mock_neutron_utils.execute.assert_called_once_with( |
| 269 |
cmd, run_as_root=True, privsep_exec=True) |