FIX: Single memif tests failing
[csit.git] / resources / libraries / python / VPPUtil.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 """VPP util library."""
15 from resources.libraries.python.constants import Constants
16 from resources.libraries.python.DUTSetup import DUTSetup
17 from resources.libraries.python.ssh import exec_cmd_no_error
18 from resources.libraries.python.topology import NodeType
19 from resources.libraries.python.VatExecutor import VatExecutor
20
21
22 class VPPUtil(object):
23     """General class for any VPP related methods/functions."""
24
25     @staticmethod
26     def show_vpp_settings(node, *additional_cmds):
27         """Print default VPP settings. In case others are needed, can be
28         accepted as next parameters (each setting one parameter), preferably
29         in form of a string.
30
31         :param node: VPP node.
32         :param additional_cmds: Additional commands that the vpp should print
33             settings for.
34         :type node: dict
35         :type additional_cmds: tuple
36         """
37         def_setting_tb_displayed = {
38             'IPv6 FIB': 'ip6 fib',
39             'IPv4 FIB': 'ip fib',
40             'Interface IP': 'int addr',
41             'Interfaces': 'int',
42             'ARP': 'ip arp',
43             'Errors': 'err'
44         }
45
46         if additional_cmds:
47             for cmd in additional_cmds:
48                 def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] = cmd
49
50         for _, cmd in def_setting_tb_displayed.items():
51             command = 'vppctl sh {cmd}'.format(cmd=cmd)
52             exec_cmd_no_error(node, command, timeout=30, sudo=True)
53
54     @staticmethod
55     def start_vpp_service(node):
56         """Start VPP service on the specified node.
57
58         :param node: VPP node.
59         :type node: dict
60         :raises RuntimeError: If VPP service fails to start.
61         """
62         DUTSetup.start_service(node, Constants.VPP_UNIT)
63         DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
64
65     @staticmethod
66     def start_vpp_service_on_all_duts(nodes):
67         """Start up the VPP service on all nodes.
68
69         :param nodes: Nodes in the topology.
70         :type nodes: dict
71         """
72         DUTSetup.start_service_on_all_duts(nodes, Constants.VPP_UNIT)
73
74     @staticmethod
75     def stop_vpp_service(node):
76         """Stop VPP service on the specified node.
77
78         :param node: VPP node.
79         :type node: dict
80         :raises RuntimeError: If VPP service fails to stop.
81         """
82         DUTSetup.stop_service(node, Constants.VPP_UNIT)
83
84     @staticmethod
85     def stop_vpp_service_on_all_duts(nodes):
86         """Stop VPP service on all nodes.
87
88         :param nodes: Nodes in the topology.
89         :type nodes: dict
90         """
91         DUTSetup.stop_service_on_all_duts(nodes, Constants.VPP_UNIT)
92
93     @staticmethod
94     def verify_vpp_on_dut(node):
95         """Verify that VPP is installed on DUT node.
96
97         :param node: DUT node.
98         :type node: dict
99         :raises RuntimeError: If failed to restart VPP, get VPP version
100             or get VPP interfaces.
101         """
102         VPPUtil.vpp_show_version_verbose(node)
103         VPPUtil.vpp_show_interfaces(node)
104
105     @staticmethod
106     def verify_vpp_on_all_duts(nodes):
107         """Verify that VPP is installed on all DUT nodes.
108
109         :param nodes: Nodes in the topology.
110         :type nodes: dict
111         """
112         for node in nodes.values():
113             if node['type'] == NodeType.DUT:
114                 DUTSetup.start_service(node, Constants.VPP_UNIT)
115                 VPPUtil.vpp_show_version_verbose(node)
116                 VPPUtil.vpp_show_interfaces(node)
117
118     @staticmethod
119     def vpp_show_version_verbose(node):
120         """Run "show version verbose" CLI command.
121
122         :param node: Node to run command on.
123         :type node: dict
124         """
125         vat = VatExecutor()
126         vat.execute_script("show_version_verbose.vat", node, json_out=False)
127
128         try:
129             vat.script_should_have_passed()
130         except AssertionError:
131             raise RuntimeError('Failed to get VPP version on host: {name}'.
132                                format(name=node['host']))
133
134     @staticmethod
135     def show_vpp_version_on_all_duts(nodes):
136         """Show VPP version verbose on all DUTs.
137
138         :param nodes: VPP nodes.
139         :type nodes: dict
140         """
141         for node in nodes.values():
142             if node['type'] == NodeType.DUT:
143                 VPPUtil.vpp_show_version_verbose(node)
144
145     @staticmethod
146     def vpp_show_interfaces(node):
147         """Run "show interface" CLI command.
148
149         :param node: Node to run command on.
150         :type node: dict
151         """
152         vat = VatExecutor()
153         vat.execute_script("show_interface.vat", node, json_out=False)
154
155         try:
156             vat.script_should_have_passed()
157         except AssertionError:
158             raise RuntimeError('Failed to get VPP interfaces on host: {name}'.
159                                format(name=node['host']))
160
161     @staticmethod
162     def vpp_show_crypto_device_mapping(node):
163         """Run "show crypto device mapping" CLI command.
164
165         :param node: Node to run command on.
166         :type node: dict
167         """
168         vat = VatExecutor()
169         vat.execute_script("show_crypto_device_mapping.vat", node,
170                            json_out=False)
171
172     @staticmethod
173     def vpp_api_trace_dump(node):
174         """Run "api trace custom-dump" CLI command.
175
176         :param node: Node to run command on.
177         :type node: dict
178         """
179         vat = VatExecutor()
180         vat.execute_script("api_trace_dump.vat", node, json_out=False)
181
182     @staticmethod
183     def vpp_api_trace_save(node):
184         """Run "api trace save" CLI command.
185
186         :param node: Node to run command on.
187         :type node: dict
188         """
189         vat = VatExecutor()
190         vat.execute_script("api_trace_save.vat", node, json_out=False)
191
192     @staticmethod
193     def vpp_enable_traces_on_dut(node):
194         """Enable vpp packet traces on the DUT node.
195
196         :param node: DUT node to set up.
197         :type node: dict
198         """
199         vat = VatExecutor()
200         vat.execute_script("enable_dpdk_traces.vat", node, json_out=False)
201         vat.execute_script("enable_vhost_user_traces.vat", node, json_out=False)
202         vat.execute_script("enable_memif_traces.vat", node, json_out=False)
203
204     @staticmethod
205     def vpp_enable_traces_on_all_duts(nodes):
206         """Enable vpp packet traces on all DUTs in the given topology.
207
208         :param nodes: Nodes in the topology.
209         :type nodes: dict
210         """
211         for node in nodes.values():
212             if node['type'] == NodeType.DUT:
213                 VPPUtil.vpp_enable_traces_on_dut(node)