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