Fix: DPDK paths after code refactor
[csit.git] / resources / libraries / python / DPDK / DPDKTools.py
1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14
15 """This module implements initialization and cleanup of DPDK environment."""
16
17 from resources.libraries.python.ssh import SSH
18 from resources.libraries.python.constants import Constants as con
19 from resources.libraries.python.topology import Topology
20
21
22 class DPDKTools(object):
23     """This class implements:
24     - Initialization of DPDK environment,
25     - Cleanup of DPDK environment.
26     """
27
28     @staticmethod
29     def initialize_dpdk_environment(dut_node, dut_if1, dut_if2):
30         """
31         Initialize the DPDK test environment on the dut_node.
32         Load the module uio and igb_uio, then bind the test NIC to the igb_uio.
33
34         :param dut_node: Will init the DPDK on this node.
35         :param dut_if1: DUT interface name.
36         :param dut_if2: DUT interface name.
37         :type dut_node: dict
38         :type dut_if1: str
39         :type dut_if2: str
40         :returns: none
41         :raises RuntimeError: If it fails to bind the interfaces to igb_uio.
42         """
43         pci_address1 = Topology.get_interface_pci_addr(dut_node, dut_if1)
44         pci_address2 = Topology.get_interface_pci_addr(dut_node, dut_if2)
45
46         ssh = SSH()
47         ssh.connect(dut_node)
48
49         cmd = 'cd {0}/tests/dpdk/dpdk_scripts/ && sudo ./init_dpdk.sh {1} {2}' \
50               .format(con.REMOTE_FW_DIR, pci_address1, pci_address2)
51
52         (ret_code, _, _) = ssh.exec_command(cmd, timeout=600)
53         if ret_code != 0:
54             raise RuntimeError('Failed to bind the interfaces to igb_uio at '
55                                'node {0}'.format(dut_node['host']))
56
57     @staticmethod
58     def cleanup_dpdk_environment(dut_node, dut_if1, dut_if2):
59         """
60         Cleanup the DPDK test environment on the DUT node.
61         Unbind the NIC from the igb_uio and bind them to the kernel driver.
62
63         :param dut_node: Will cleanup the DPDK on this node.
64         :param dut_if1: DUT interface name.
65         :param dut_if2: DUT interface name.
66         :type dut_node: dict
67         :type dut_if1: str
68         :type dut_if2: str
69         :returns: none
70         :raises RuntimeError: If it fails to cleanup the dpdk.
71         """
72         pci_address1 = Topology.get_interface_pci_addr(dut_node, dut_if1)
73         if1_driver = Topology.get_interface_driver(dut_node, dut_if1)
74         pci_address2 = Topology.get_interface_pci_addr(dut_node, dut_if2)
75         if2_driver = Topology.get_interface_driver(dut_node, dut_if2)
76
77         ssh = SSH()
78         ssh.connect(dut_node)
79
80         cmd = 'cd {0}/tests/dpdk/dpdk_scripts/ && sudo ./cleanup_dpdk.sh ' \
81               '{1} {2} {3} {4}'.format(con.REMOTE_FW_DIR, if1_driver,
82                                        pci_address1, if2_driver, pci_address2)
83
84         (ret_code, _, _) = ssh.exec_command(cmd, timeout=600)
85         if ret_code != 0:
86             raise RuntimeError('Failed to cleanup the dpdk at node {0}'
87                                .format(dut_node['host']))