def _get_representor(self, vif): if isinstance(vif, obj.vif.VIFHostDevice): pci_addr = vif.dev_address elif 'datapath_offload' in vif.port_profile \ and hasattr(vif.port_profile, 'datapath_offload') \ and isinstance(vif.port_profile.datapath_offload, obj.vif.DatapathOffloadRepresentor): pci_addr = vif.port_profile.datapath_offload.representor_address elif 'representor_address' in vif.port_profile \ and hasattr(vif.port_profile, 'representor_address'): pci_addr = vif.port_profile.representor_address else: # TODO(jangutter): New exception is needed raise exception.WrongPortProfile( profile=vif.port_profile.__class__.__name__) vf_num = linux_net.get_vf_num_by_pci_address(pci_addr) pf_ifname = linux_net.get_ifname_by_pci_address(pci_addr, pf_interface=True, switchdev=True) representor = linux_net.get_representor_port(pf_ifname, vf_num) return { 'address': pci_addr, 'name': representor, 'pfname': pf_ifname, 'vfno': vf_num }
def test_get_representor_port_2_pfs( self, mock_get_function_by_ifname, mock_listdir, mock_isfile, mock_open): mock_listdir.return_value = [ 'pf_ifname1', 'pf_ifname2', 'rep_pf1_vf_1', 'rep_pf1_vf_2', 'rep_pf2_vf_1', 'rep_pf2_vf_2', ] mock_isfile.side_effect = [True, True, True, True] mock_open.return_value.__enter__ = lambda s: s readline_mock = mock_open.return_value.readline readline_mock.side_effect = ( ['pf_sw_id', 'pf_sw_id', 'VF1@PF1', 'pf_sw_id', 'vf2@pf1', 'pf_sw_id', 'pf2vf1', 'pf_sw_id', 'pf2vf2']) # PCI IDs mocked: # PF1: 0000:0a:00.1 PF2: 0000:0a:00.2 # PF1VF1: 0000:0a:02.1 PF1VF2: 0000:0a:02.2 # PF2VF1: 0000:0a:04.1 PF2VF2: 0000:0a:04.2 mock_get_function_by_ifname.side_effect = ( [("0000:0a:00.1", True), ("0000:0a:00.2", True), ("0000:0a:02.1", False), ("0000:0a:00.2", True), ("0000:0a:02.2", False), ("0000:0a:00.2", True), ("0000:0a:04.1", False), ("0000:0a:00.2", True), ("0000:0a:04.2", False), ("0000:0a:00.2", True)]) ifname = linux_net.get_representor_port('pf_ifname2', '2') self.assertEqual('rep_pf2_vf_2', ifname)
def test_get_representor_port(self, mock_listdir, mock_isfile, mock_open): mock_listdir.return_value = ['pf_ifname', 'rep_vf_1', 'rep_vf_2'] mock_isfile.side_effect = [True, True] mock_open.return_value.__enter__ = lambda s: s readline_mock = mock_open.return_value.readline readline_mock.side_effect = ([ 'pf_sw_id', 'pf_sw_id', '1', 'pf_sw_id', 'pf0vf2' ]) open_calls = ([ mock.call('/sys/class/net/pf_ifname/phys_switch_id', 'r'), mock.call().readline(), mock.call().__exit__(None, None, None), mock.call('/sys/class/net/rep_vf_1/phys_switch_id', 'r'), mock.call().readline(), mock.call().__exit__(None, None, None), mock.call('/sys/class/net/rep_vf_1/phys_port_name', 'r'), mock.call().readline(), mock.call().__exit__(None, None, None), mock.call('/sys/class/net/rep_vf_2/phys_switch_id', 'r'), mock.call().readline(), mock.call().__exit__(None, None, None), mock.call('/sys/class/net/rep_vf_2/phys_port_name', 'r'), mock.call().readline(), mock.call().__exit__(None, None, None) ]) ifname = linux_net.get_representor_port('pf_ifname', '2') mock_open.assert_has_calls(open_calls) self.assertEqual('rep_vf_2', ifname)
def test_get_representor_port(self, mock__get_phys_switch_id, mock__get_phys_port_name, mock__get_pf_func, mock_listdir): mock_listdir.return_value = ['pf_ifname', 'rep_vf_1', 'rep_vf_2'] mock__get_phys_switch_id.return_value = 'pf_sw_id' mock__get_pf_func.return_value = "0" mock__get_phys_port_name.side_effect = (['1', "pf0vf1", "pf0vf2"]) ifname = linux_net.get_representor_port('pf_ifname', '2') self.assertEqual('rep_vf_2', ifname)
def _plug_vf_passthrough(self, vif, instance_info): linux_net.ensure_ovs_bridge(vif.network.bridge, constants.OVS_DATAPATH_SYSTEM) pci_slot = vif.dev_address pf_ifname = linux_net.get_ifname_by_pci_address(pci_slot, pf_interface=True, switchdev=True) vf_num = linux_net.get_vf_num_by_pci_address(pci_slot) representor = linux_net.get_representor_port(pf_ifname, vf_num) linux_net.set_interface_state(representor, 'up') self._create_vif_port(vif, representor, instance_info)
def _unplug_vf_passthrough(self, vif, instance_info): """Remove port from OVS.""" pci_slot = vif.dev_address pf_ifname = linux_net.get_ifname_by_pci_address(pci_slot, pf_interface=True, switchdev=True) vf_num = linux_net.get_vf_num_by_pci_address(pci_slot) representor = linux_net.get_representor_port(pf_ifname, vf_num) # The representor interface can't be deleted because it bind the # SR-IOV VF, therefore we just need to remove it from the ovs bridge # and set the status to down linux_net.delete_ovs_vif_port(vif.network.bridge, representor, delete_netdev=False) linux_net.set_interface_state(representor, 'down')
def test_get_representor_port_2_pfs(self, mock__get_phys_switch_id, mock__get_phys_port_name, mock__get_pf_func, mock_listdir): mock_listdir.return_value = [ 'pf_ifname1', 'pf_ifname2', 'rep_pf1_vf_1', 'rep_pf1_vf_2', 'rep_pf2_vf_1', 'rep_pf2_vf_2', ] mock__get_phys_switch_id.return_value = 'pf_sw_id' mock__get_pf_func.return_value = "2" mock__get_phys_port_name.side_effect = ([ "p1", "p2", "VF1@PF1", "pf2vf1", "vf2@pf1", "pf2vf2" ]) ifname = linux_net.get_representor_port('pf_ifname2', '2') self.assertEqual('rep_pf2_vf_2', ifname)
def test_get_representor_port_2_pfs(self, mock_listdir, mock_isfile, mock_open): mock_listdir.return_value = [ 'pf_ifname1', 'pf_ifname2', 'rep_pf1_vf_1', 'rep_pf1_vf_2', 'rep_pf2_vf_1', 'rep_pf2_vf_2', ] mock_isfile.side_effect = [True, True, True, True] mock_open.return_value.__enter__ = lambda s: s readline_mock = mock_open.return_value.readline readline_mock.side_effect = ([ 'pf1_sw_id', 'pf1_sw_id', 'pf2_sw_id', '1', 'pf1_sw_id', '2' ]) ifname = linux_net.get_representor_port('pf_ifname1', '2') self.assertEqual('rep_pf1_vf_2', ifname)
def _unplug_vf(self, vif): """Remove port from OVS.""" datapath = self._get_vif_datapath_type(vif) if datapath == constants.OVS_DATAPATH_SYSTEM: pci_slot = vif.dev_address pf_ifname = linux_net.get_ifname_by_pci_address( pci_slot, pf_interface=True, switchdev=True) vf_num = linux_net.get_vf_num_by_pci_address(pci_slot) representor = linux_net.get_representor_port(pf_ifname, vf_num) else: representor = linux_net.get_dpdk_representor_port_name( vif.id) # The representor interface can't be deleted because it bind the # SR-IOV VF, therefore we just need to remove it from the ovs bridge # and set the status to down self.ovsdb.delete_ovs_vif_port( vif.network.bridge, representor, delete_netdev=False) if datapath == constants.OVS_DATAPATH_SYSTEM: linux_net.set_interface_state(representor, 'down')
def _plug_vf(self, vif, instance_info): datapath = self._get_vif_datapath_type(vif) self.ovsdb.ensure_ovs_bridge(vif.network.bridge, datapath) pci_slot = vif.dev_address vf_num = linux_net.get_vf_num_by_pci_address(pci_slot) args = [] kwargs = {} if datapath == constants.OVS_DATAPATH_SYSTEM: pf_ifname = linux_net.get_ifname_by_pci_address( pci_slot, pf_interface=True, switchdev=True) representor = linux_net.get_representor_port(pf_ifname, vf_num) linux_net.set_interface_state(representor, 'up') args = [vif, representor, instance_info] else: representor = linux_net.get_dpdk_representor_port_name( vif.id) pf_pci = linux_net.get_pf_pci_from_vf(pci_slot) args = [vif, representor, instance_info] kwargs = {'interface_type': constants.OVS_DPDK_INTERFACE_TYPE, 'pf_pci': pf_pci, 'vf_num': vf_num} self._create_vif_port(*args, **kwargs)
def test_get_representor_port(self, mock_get_function_by_ifname, mock_listdir, mock_isfile, mock_open): mock_listdir.return_value = [ 'pf_ifname', 'rep_vf_1', 'rep_vf_2' ] mock_isfile.side_effect = [True, True] mock_open.return_value.__enter__ = lambda s: s readline_mock = mock_open.return_value.readline readline_mock.side_effect = ( ['pf_sw_id', 'pf_sw_id', '1', 'pf_sw_id', 'pf0vf2']) # PCI IDs mocked: # PF0: 0000:0a:00.0 # PF0VF1: 0000:0a:02.1 PF0VF2: 0000:0a:02.2 mock_get_function_by_ifname.side_effect = ( [("0000:0a:00.0", True), ("0000:0a:02.1", False), ("0000:0a:02.2", False), ("0000:0a:00.0", True)]) open_calls = ( [mock.call('/sys/class/net/pf_ifname/phys_switch_id', 'r'), mock.call().readline(), mock.call().__exit__(None, None, None), mock.call('/sys/class/net/rep_vf_1/phys_switch_id', 'r'), mock.call().readline(), mock.call().__exit__(None, None, None), mock.call('/sys/class/net/rep_vf_1/phys_port_name', 'r'), mock.call().readline(), mock.call().__exit__(None, None, None), mock.call('/sys/class/net/rep_vf_2/phys_switch_id', 'r'), mock.call().readline(), mock.call().__exit__(None, None, None), mock.call('/sys/class/net/rep_vf_2/phys_port_name', 'r'), mock.call().readline(), mock.call().__exit__(None, None, None)]) ifname = linux_net.get_representor_port('pf_ifname', '2') mock_open.assert_has_calls(open_calls) self.assertEqual('rep_vf_2', ifname)