Python3: resources and libraries
[csit.git] / resources / libraries / python / DPDK / DPDKTools.py
1 # Copyright (c) 2019 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 robot.api import logger
18
19 from resources.libraries.python.Constants import Constants
20 from resources.libraries.python.ssh import SSH, exec_cmd_no_error
21 from resources.libraries.python.topology import NodeType, Topology
22
23
24 class DPDKTools:
25     """This class implements:
26     - Initialization of DPDK environment,
27     - Cleanup of DPDK environment.
28     """
29
30     @staticmethod
31     def initialize_dpdk_environment(dut_node, dut_if1, dut_if2):
32         """
33         Initialize the DPDK test environment on the dut_node.
34         Load the module uio and igb_uio, then bind the test NIC to the igb_uio.
35
36         :param dut_node: Will init the DPDK on this node.
37         :param dut_if1: DUT interface name.
38         :param dut_if2: DUT interface name.
39         :type dut_node: dict
40         :type dut_if1: str
41         :type dut_if2: str
42         :raises RuntimeError: If it fails to bind the interfaces to igb_uio.
43         """
44         if dut_node[u"type"] == NodeType.DUT:
45             pci_address1 = Topology.get_interface_pci_addr(dut_node, dut_if1)
46             pci_address2 = Topology.get_interface_pci_addr(dut_node, dut_if2)
47
48             ssh = SSH()
49             ssh.connect(dut_node)
50
51             arch = Topology.get_node_arch(dut_node)
52             cmd = f"{Constants.REMOTE_FW_DIR}/tests/dpdk/dpdk_scripts" \
53                 f"/init_dpdk.sh {pci_address1} {pci_address2} {arch}"
54
55             ret_code, _, _ = ssh.exec_command_sudo(cmd, timeout=600)
56             if ret_code != 0:
57                 raise RuntimeError(
58                     f"Failed to bind the interfaces to igb_uio at node "
59                     f"{dut_node['host']}"
60                 )
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[u"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 = f"{Constants.REMOTE_FW_DIR}/tests/dpdk/dpdk_scripts" \
86                 f"/cleanup_dpdk.sh {if1_driver} {pci_address1} {if2_driver} " \
87                 f"{pci_address2}"
88
89             ret_code, _, _ = ssh.exec_command_sudo(cmd, timeout=600)
90             if ret_code != 0:
91                 raise RuntimeError(
92                     f"Failed to cleanup the dpdk at node {dut_node[u'host']}"
93                 )
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         command = f"{Constants.REMOTE_FW_DIR}/tests/dpdk/dpdk_scripts" \
108             f"/install_dpdk.sh {arch}"
109         message = u"Install the DPDK failed!"
110         exec_cmd_no_error(node, command, timeout=600, message=message)
111
112         command = f"cat {Constants.REMOTE_FW_DIR}/dpdk*/VERSION"
113         message = u"Get DPDK version failed!"
114         stdout, _ = exec_cmd_no_error(node, command, message=message)
115
116         logger.info(f"DPDK Version: {stdout}")
117
118     @staticmethod
119     def install_dpdk_test_on_all_duts(nodes):
120         """
121         Prepare the DPDK test environment on all DUTs.
122
123         :param nodes: Nodes from topology file.
124         :type nodes: dict
125         :returns: nothing
126         """
127         for node in list(nodes.values()):
128             if node[u"type"] == NodeType.DUT:
129                 DPDKTools.install_dpdk_test(node)