Fix Tap failing tests
[csit.git] / resources / libraries / python / VPPUtil.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 """VPP util library."""
15
16 import time
17
18 from robot.api import logger
19
20 from resources.libraries.python.Constants import Constants
21 from resources.libraries.python.DUTSetup import DUTSetup
22 from resources.libraries.python.PapiExecutor import PapiExecutor
23 from resources.libraries.python.ssh import exec_cmd, exec_cmd_no_error
24 from resources.libraries.python.topology import NodeType
25 from resources.libraries.python.VatExecutor import VatExecutor
26
27
28 class VPPUtil(object):
29     """General class for any VPP related methods/functions."""
30
31     @staticmethod
32     def show_vpp_settings(node, *additional_cmds):
33         """Print default VPP settings. In case others are needed, can be
34         accepted as next parameters (each setting one parameter), preferably
35         in form of a string.
36
37         :param node: VPP node.
38         :param additional_cmds: Additional commands that the vpp should print
39             settings for.
40         :type node: dict
41         :type additional_cmds: tuple
42         """
43         def_setting_tb_displayed = {
44             'IPv6 FIB': 'ip6 fib',
45             'IPv4 FIB': 'ip fib',
46             'Interface IP': 'int addr',
47             'Interfaces': 'int',
48             'ARP': 'ip arp',
49             'Errors': 'err'
50         }
51
52         if additional_cmds:
53             for cmd in additional_cmds:
54                 def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] = cmd
55
56         for _, cmd in def_setting_tb_displayed.items():
57             command = 'vppctl sh {cmd}'.format(cmd=cmd)
58             exec_cmd_no_error(node, command, timeout=30, sudo=True)
59
60     @staticmethod
61     def start_vpp_service(node, retries=60):
62         """Start VPP service on the specified node.
63
64         :param node: VPP node.
65         :param retries: Number of times (default 60) to re-try waiting.
66         :type node: dict
67         :type retries: int
68         :raises RuntimeError: If VPP service fails to start.
69         """
70         DUTSetup.start_service(node, Constants.VPP_UNIT)
71         # Sleep 1 second, up to <retry> times,
72         # and verify if VPP is running.
73         for _ in range(retries):
74             time.sleep(1)
75             command = 'vppctl show pci'
76             ret, stdout, _ = exec_cmd(node, command, timeout=30, sudo=True)
77             if not ret and 'Connection refused' not in stdout:
78                 break
79         else:
80             raise RuntimeError('VPP failed to start on host {name}'.
81                                format(name=node['host']))
82         DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
83
84     @staticmethod
85     def start_vpp_service_on_all_duts(nodes):
86         """Start up the VPP service on all nodes.
87
88         :param nodes: Nodes in the topology.
89         :type nodes: dict
90         """
91         for node in nodes.values():
92             if node['type'] == NodeType.DUT:
93                 VPPUtil.start_vpp_service(node)
94
95     @staticmethod
96     def stop_vpp_service(node):
97         """Stop VPP service on the specified node.
98
99         :param node: VPP node.
100         :type node: dict
101         :raises RuntimeError: If VPP service fails to stop.
102         """
103         DUTSetup.stop_service(node, Constants.VPP_UNIT)
104
105     @staticmethod
106     def stop_vpp_service_on_all_duts(nodes):
107         """Stop VPP service on all 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                 VPPUtil.stop_vpp_service(node)
115
116     @staticmethod
117     def verify_vpp_on_dut(node):
118         """Verify that VPP is installed on DUT node.
119
120         :param node: DUT node.
121         :type node: dict
122         :raises RuntimeError: If failed to restart VPP, get VPP version
123             or get VPP interfaces.
124         """
125         VPPUtil.vpp_show_version_verbose(node)
126         VPPUtil.vpp_show_interfaces(node)
127
128     @staticmethod
129     def verify_vpp_on_all_duts(nodes):
130         """Verify that VPP is installed on all DUT nodes.
131
132         :param nodes: Nodes in the topology.
133         :type nodes: dict
134         """
135         for node in nodes.values():
136             if node['type'] == NodeType.DUT:
137                 VPPUtil.start_vpp_service(node)
138                 VPPUtil.verify_vpp_on_dut(node)
139
140     @staticmethod
141     def vpp_show_version(node, verbose=False):
142         """Run "show_version" PAPI command.
143
144         :param node: Node to run command on.
145         :param verbose: Show version, compile date and compile location if True
146             otherwise show only version.
147         :type node: dict
148         :type verbose: bool
149         :returns: VPP version.
150         :rtype: str
151         """
152
153         with PapiExecutor(node) as papi_exec:
154             data = papi_exec.add('show_version').execute_should_pass().\
155                 verify_reply()
156         version = ('VPP version:      {ver}\n'.
157                    format(ver=data['version'].rstrip('\0x00')))
158         if verbose:
159             version += ('Compile date:     {date}\n'
160                         'Compile location: {cl}\n '.
161                         format(date=data['build_date'].rstrip('\0x00'),
162                                cl=data['build_directory'].rstrip('\0x00')))
163         logger.info(version)
164         return data['version'].rstrip('\0x00')
165
166     @staticmethod
167     def vpp_show_version_verbose(node):
168         """Run "show_version" API command and return verbose string of version
169         data.
170
171         :param node: Node to run command on.
172         :type node: dict
173         """
174         VPPUtil.vpp_show_version(node, verbose=True)
175
176     @staticmethod
177     def show_vpp_version_on_all_duts(nodes):
178         """Show VPP version on all DUTs.
179
180         :param nodes: VPP nodes.
181         :type nodes: dict
182         """
183         for node in nodes.values():
184             if node['type'] == NodeType.DUT:
185                 VPPUtil.vpp_show_version_verbose(node)
186
187     @staticmethod
188     def vpp_show_interfaces(node):
189         """Run "show interface" CLI command.
190
191         :param node: Node to run command on.
192         :type node: dict
193         """
194         vat = VatExecutor()
195         vat.execute_script("show_interface.vat", node, json_out=False)
196
197         try:
198             vat.script_should_have_passed()
199         except AssertionError:
200             raise RuntimeError('Failed to get VPP interfaces on host: {name}'.
201                                format(name=node['host']))
202
203     @staticmethod
204     def vpp_show_crypto_device_mapping(node):
205         """Run "show crypto device mapping" CLI command.
206
207         :param node: Node to run command on.
208         :type node: dict
209         """
210         vat = VatExecutor()
211         vat.execute_script("show_crypto_device_mapping.vat", node,
212                            json_out=False)
213
214     @staticmethod
215     def vpp_api_trace_dump(node):
216         """Run "api trace custom-dump" CLI command.
217
218         :param node: Node to run command on.
219         :type node: dict
220         """
221         vat = VatExecutor()
222         vat.execute_script("api_trace_dump.vat", node, json_out=False)
223
224     @staticmethod
225     def vpp_api_trace_save(node):
226         """Run "api trace save" CLI command.
227
228         :param node: Node to run command on.
229         :type node: dict
230         """
231         vat = VatExecutor()
232         vat.execute_script("api_trace_save.vat", node, json_out=False)
233
234     @staticmethod
235     def vpp_enable_traces_on_dut(node):
236         """Enable vpp packet traces on the DUT node.
237
238         :param node: DUT node to set up.
239         :type node: dict
240         """
241         vat = VatExecutor()
242         vat.execute_script("enable_dpdk_traces.vat", node, json_out=False)
243         vat.execute_script("enable_vhost_user_traces.vat", node, json_out=False)
244         vat.execute_script("enable_memif_traces.vat", node, json_out=False)
245
246     @staticmethod
247     def vpp_enable_traces_on_all_duts(nodes):
248         """Enable vpp packet traces on all DUTs in the given topology.
249
250         :param nodes: Nodes in the topology.
251         :type nodes: dict
252         """
253         for node in nodes.values():
254             if node['type'] == NodeType.DUT:
255                 VPPUtil.vpp_enable_traces_on_dut(node)
256
257     @staticmethod
258     def vpp_enable_elog_traces_on_dut(node):
259         """Enable API/CLI/Barrier traces on the DUT node.
260
261         :param node: DUT node to set up.
262         :type node: dict
263         """
264         vat = VatExecutor()
265         vat.execute_script("elog_trace_api_cli_barrier.vat", node,
266                            json_out=False)
267
268     @staticmethod
269     def vpp_enable_elog_traces_on_all_duts(nodes):
270         """Enable API/CLI/Barrier traces on all DUTs in the given topology.
271
272         :param nodes: Nodes in the topology.
273         :type nodes: dict
274         """
275         for node in nodes.values():
276             if node['type'] == NodeType.DUT:
277                 VPPUtil.vpp_enable_elog_traces_on_dut(node)
278
279     @staticmethod
280     def show_event_logger_on_dut(node):
281         """Show event logger on the DUT node.
282
283         :param node: DUT node to show traces on.
284         :type node: dict
285         """
286         vat = VatExecutor()
287         vat.execute_script("show_event_logger.vat", node, json_out=False)
288
289     @staticmethod
290     def show_event_logger_on_all_duts(nodes):
291         """Show event logger on all DUTs in the given topology.
292
293         :param nodes: Nodes in the topology.
294         :type nodes: dict
295         """
296         for node in nodes.values():
297             if node['type'] == NodeType.DUT:
298                 VPPUtil.show_event_logger_on_dut(node)
299
300     @staticmethod
301     def vpp_show_threads(node):
302         """Show VPP threads on node.
303
304         :param node: Node to run command on.
305         :type node: dict
306         :returns: VPP thread data.
307         :rtype: list
308         """
309         with PapiExecutor(node) as papi_exec:
310             return papi_exec.add('show_threads').execute_should_pass().\
311                 verify_reply()["thread_data"]