CSIT-1121: Add "Get DPDK version" KW to DPDK tests
[csit.git] / resources / libraries / python / DPDK / DPDKTools.py
1 # Copyright (c) 2018 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
19 from resources.libraries.python.topology import NodeType, 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         :raises RuntimeError: If it fails to bind the interfaces to igb_uio.
41         """
42         if dut_node['type'] == NodeType.DUT:
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             arch = Topology.get_node_arch(dut_node)
50             cmd = '{fwdir}/tests/dpdk/dpdk_scripts/init_dpdk.sh '\
51                   '{pci1} {pci2} {arch}'.format(fwdir=Constants.REMOTE_FW_DIR,
52                                                 pci1=pci_address1,
53                                                 pci2=pci_address2,
54                                                 arch=arch)
55
56             ret_code, _, _ = ssh.exec_command_sudo(cmd, timeout=600)
57             if ret_code != 0:
58                 raise RuntimeError('Failed to bind the interfaces to igb_uio '
59                                    'at node {name}'.\
60                                     format(name=dut_node['host']))
61
62     @staticmethod
63     def cleanup_dpdk_environment(dut_node, dut_if1, dut_if2):
64         """
65         Cleanup the DPDK test environment on the DUT node.
66         Unbind the NIC from the igb_uio and bind them to the kernel driver.
67
68         :param dut_node: Will cleanup the DPDK on this node.
69         :param dut_if1: DUT interface name.
70         :param dut_if2: DUT interface name.
71         :type dut_node: dict
72         :type dut_if1: str
73         :type dut_if2: str
74         :raises RuntimeError: If it fails to cleanup the dpdk.
75         """
76         if dut_node['type'] == NodeType.DUT:
77             pci_address1 = Topology.get_interface_pci_addr(dut_node, dut_if1)
78             if1_driver = Topology.get_interface_driver(dut_node, dut_if1)
79             pci_address2 = Topology.get_interface_pci_addr(dut_node, dut_if2)
80             if2_driver = Topology.get_interface_driver(dut_node, dut_if2)
81
82             ssh = SSH()
83             ssh.connect(dut_node)
84
85             cmd = '{fwdir}/tests/dpdk/dpdk_scripts/cleanup_dpdk.sh ' \
86                   '{drv1} {pci1} {drv2} {pci2}'.\
87                   format(fwdir=Constants.REMOTE_FW_DIR, drv1=if1_driver,
88                          pci1=pci_address1, drv2=if2_driver, pci2=pci_address2)
89
90             ret_code, _, _ = ssh.exec_command_sudo(cmd, timeout=600)
91             if ret_code != 0:
92                 raise RuntimeError('Failed to cleanup the dpdk at node {name}'.
93                                    format(name=dut_node['host']))
94
95     @staticmethod
96     def install_dpdk_test(node):
97         """
98         Prepare the DPDK test environment
99
100         :param node: Dictionary created from topology
101         :type node: dict
102         :returns: nothing
103         :raises RuntimeError: If command returns nonzero return code.
104         """
105         arch = Topology.get_node_arch(node)
106
107         ssh = SSH()
108         ssh.connect(node)
109
110         ret_code, _, _ = ssh.exec_command(
111             '{fwdir}/tests/dpdk/dpdk_scripts/install_dpdk.sh {arch}'.
112             format(fwdir=Constants.REMOTE_FW_DIR, arch=arch), timeout=600)
113
114         if ret_code != 0:
115             raise RuntimeError('Install the DPDK failed')
116
117     @staticmethod
118     def install_dpdk_test_on_all_duts(nodes):
119         """
120         Prepare the DPDK test environment on all DUTs.
121
122         :param nodes: Nodes from topology file.
123         :type nodes: dict
124         :returns: nothing
125         """
126         for node in nodes.values():
127             if node['type'] == NodeType.DUT:
128                 DPDKTools.install_dpdk_test(node)