1bc1b4358e335050b8c2d3b352f846ee80f1d105
[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         """
150
151         with PapiExecutor(node) as papi_exec:
152             papi_resp = papi_exec.add('show_version').execute_should_pass()
153         data = papi_resp.reply[0]['api_reply']['show_version_reply']
154         version = ('VPP version:      {ver}\n'.
155                    format(ver=data['version'].rstrip('\0x00')))
156         if verbose:
157             version += ('Compile date:     {date}\n'
158                         'Compile location: {loc}\n '.
159                         format(date=data['build_date'].rstrip('\0x00'),
160                                loc=data['build_directory'].rstrip('\0x00')))
161         logger.info(version)
162
163     @staticmethod
164     def vpp_show_version_verbose(node):
165         """Run "show_version" API command and return verbose string of version
166         data.
167
168         :param node: Node to run command on.
169         :type node: dict
170         """
171         VPPUtil.vpp_show_version(node, verbose=True)
172
173     @staticmethod
174     def show_vpp_version_on_all_duts(nodes):
175         """Show VPP version on all DUTs.
176
177         :param nodes: VPP nodes.
178         :type nodes: dict
179         """
180         for node in nodes.values():
181             if node['type'] == NodeType.DUT:
182                 VPPUtil.vpp_show_version_verbose(node)
183
184     @staticmethod
185     def vpp_show_interfaces(node):
186         """Run "show interface" CLI command.
187
188         :param node: Node to run command on.
189         :type node: dict
190         """
191         vat = VatExecutor()
192         vat.execute_script("show_interface.vat", node, json_out=False)
193
194         try:
195             vat.script_should_have_passed()
196         except AssertionError:
197             raise RuntimeError('Failed to get VPP interfaces on host: {name}'.
198                                format(name=node['host']))
199
200     @staticmethod
201     def vpp_show_crypto_device_mapping(node):
202         """Run "show crypto device mapping" CLI command.
203
204         :param node: Node to run command on.
205         :type node: dict
206         """
207         vat = VatExecutor()
208         vat.execute_script("show_crypto_device_mapping.vat", node,
209                            json_out=False)
210
211     @staticmethod
212     def vpp_api_trace_dump(node):
213         """Run "api trace custom-dump" CLI command.
214
215         :param node: Node to run command on.
216         :type node: dict
217         """
218         vat = VatExecutor()
219         vat.execute_script("api_trace_dump.vat", node, json_out=False)
220
221     @staticmethod
222     def vpp_api_trace_save(node):
223         """Run "api trace save" CLI command.
224
225         :param node: Node to run command on.
226         :type node: dict
227         """
228         vat = VatExecutor()
229         vat.execute_script("api_trace_save.vat", node, json_out=False)
230
231     @staticmethod
232     def vpp_enable_traces_on_dut(node):
233         """Enable vpp packet traces on the DUT node.
234
235         :param node: DUT node to set up.
236         :type node: dict
237         """
238         vat = VatExecutor()
239         vat.execute_script("enable_dpdk_traces.vat", node, json_out=False)
240         vat.execute_script("enable_vhost_user_traces.vat", node, json_out=False)
241         vat.execute_script("enable_memif_traces.vat", node, json_out=False)
242
243     @staticmethod
244     def vpp_enable_traces_on_all_duts(nodes):
245         """Enable vpp packet traces on all DUTs in the given topology.
246
247         :param nodes: Nodes in the topology.
248         :type nodes: dict
249         """
250         for node in nodes.values():
251             if node['type'] == NodeType.DUT:
252                 VPPUtil.vpp_enable_traces_on_dut(node)
253
254     @staticmethod
255     def vpp_enable_elog_traces_on_dut(node):
256         """Enable API/CLI/Barrier traces on the DUT node.
257
258         :param node: DUT node to set up.
259         :type node: dict
260         """
261         vat = VatExecutor()
262         vat.execute_script("elog_trace_api_cli_barrier.vat", node,
263                            json_out=False)
264
265     @staticmethod
266     def vpp_enable_elog_traces_on_all_duts(nodes):
267         """Enable API/CLI/Barrier traces on all DUTs in the given topology.
268
269         :param nodes: Nodes in the topology.
270         :type nodes: dict
271         """
272         for node in nodes.values():
273             if node['type'] == NodeType.DUT:
274                 VPPUtil.vpp_enable_elog_traces_on_dut(node)
275
276     @staticmethod
277     def show_event_logger_on_dut(node):
278         """Show event logger on the DUT node.
279
280         :param node: DUT node to show traces on.
281         :type node: dict
282         """
283         vat = VatExecutor()
284         vat.execute_script("show_event_logger.vat", node, json_out=False)
285
286     @staticmethod
287     def show_event_logger_on_all_duts(nodes):
288         """Show event logger on all DUTs in the given topology.
289
290         :param nodes: Nodes in the topology.
291         :type nodes: dict
292         """
293         for node in nodes.values():
294             if node['type'] == NodeType.DUT:
295                 VPPUtil.show_event_logger_on_dut(node)
296
297     @staticmethod
298     def vpp_show_threads(node):
299         """Show VPP threads on node.
300
301         :param node: Node to run command on.
302         :type node: dict
303         :returns: VPP thread data.
304         :rtype: list
305         """
306
307         with PapiExecutor(node) as papi_exec:
308             resp = papi_exec.add('show_threads').execute_should_pass()
309         return resp.reply[0]['api_reply']['show_threads_reply']['thread_data']