5b7728412e2fad4119419286e1f100228a4960bc
[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 from robot.api import logger
17
18 from resources.libraries.python.Constants import Constants
19 from resources.libraries.python.DUTSetup import DUTSetup
20 from resources.libraries.python.PapiExecutor import PapiExecutor
21 from resources.libraries.python.ssh import exec_cmd_no_error
22 from resources.libraries.python.topology import NodeType
23 from resources.libraries.python.VatExecutor import VatExecutor
24
25
26 class VPPUtil(object):
27     """General class for any VPP related methods/functions."""
28
29     @staticmethod
30     def show_vpp_settings(node, *additional_cmds):
31         """Print default VPP settings. In case others are needed, can be
32         accepted as next parameters (each setting one parameter), preferably
33         in form of a string.
34
35         :param node: VPP node.
36         :param additional_cmds: Additional commands that the vpp should print
37             settings for.
38         :type node: dict
39         :type additional_cmds: tuple
40         """
41         def_setting_tb_displayed = {
42             'IPv6 FIB': 'ip6 fib',
43             'IPv4 FIB': 'ip fib',
44             'Interface IP': 'int addr',
45             'Interfaces': 'int',
46             'ARP': 'ip arp',
47             'Errors': 'err'
48         }
49
50         if additional_cmds:
51             for cmd in additional_cmds:
52                 def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] = cmd
53
54         for _, cmd in def_setting_tb_displayed.items():
55             command = 'vppctl sh {cmd}'.format(cmd=cmd)
56             exec_cmd_no_error(node, command, timeout=30, sudo=True)
57
58     @staticmethod
59     def restart_vpp_service(node):
60         """Restart VPP service on the specified topology node.
61
62         :param node: Topology node.
63         :type node: dict
64         """
65         DUTSetup.restart_service(node, Constants.VPP_UNIT)
66
67     @staticmethod
68     def restart_vpp_service_on_all_duts(nodes):
69         """Restart VPP service on all DUT nodes.
70
71         :param nodes: Topology nodes.
72         :type nodes: dict
73         """
74         for node in nodes.values():
75             if node['type'] == NodeType.DUT:
76                 VPPUtil.restart_vpp_service(node)
77
78     @staticmethod
79     def stop_vpp_service(node):
80         """Stop VPP service on the specified topology node.
81
82         :param node: Topology node.
83         :type node: dict
84         """
85         DUTSetup.stop_service(node, Constants.VPP_UNIT)
86
87     @staticmethod
88     def stop_vpp_service_on_all_duts(nodes):
89         """Stop VPP service on all DUT nodes.
90
91         :param nodes: Topology nodes.
92         :type nodes: dict
93         """
94         for node in nodes.values():
95             if node['type'] == NodeType.DUT:
96                 VPPUtil.stop_vpp_service(node)
97
98     @staticmethod
99     def verify_vpp_installed(node):
100         """Verify that VPP is installed on the specified topology node.
101
102         :param node: Topology node.
103         :type node: dict
104         """
105         cmd = 'command -v vpp'
106         exec_cmd_no_error(
107             node, cmd, message='VPP is not installed!')
108
109     @staticmethod
110     def verify_vpp_started(node):
111         """Verify that VPP is started on the specified topology node.
112
113         :param node: Topology node.
114         :type node: dict
115         """
116         cmd = ('vppctl show pci 2>&1 | '
117                'fgrep -v "Connection refused" | '
118                'fgrep -v "No such file or directory"')
119         exec_cmd_no_error(
120             node, cmd, sudo=True, message='VPP failed to start!', retries=120)
121
122     @staticmethod
123     def verify_vpp(node):
124         """Verify that VPP is installed and started on the specified topology
125         node.
126
127         :param node: Topology node.
128         :type node: dict
129         :raises RuntimeError: If VPP service fails to start.
130         """
131         VPPUtil.verify_vpp_installed(node)
132         try:
133             # Verify responsivness of vppctl.
134             VPPUtil.verify_vpp_started(node)
135             # Verify responsivness of PAPI.
136             VPPUtil.show_log(node)
137         finally:
138             DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
139
140     @staticmethod
141     def verify_vpp_on_all_duts(nodes):
142         """Verify that VPP is installed and started on all DUT nodes.
143
144         :param nodes: Nodes in the topology.
145         :type nodes: dict
146         """
147         for node in nodes.values():
148             if node['type'] == NodeType.DUT:
149                 VPPUtil.verify_vpp(node)
150
151     @staticmethod
152     def vpp_show_version(node, verbose=True):
153         """Run "show_version" PAPI command.
154
155         :param node: Node to run command on.
156         :param verbose: Show version, compile date and compile location if True
157             otherwise show only version.
158         :type node: dict
159         :type verbose: bool
160         :returns: VPP version.
161         :rtype: str
162         """
163         with PapiExecutor(node) as papi_exec:
164             data = papi_exec.add('show_version').execute_should_pass().\
165                 verify_reply()
166         version = ('VPP version:      {ver}\n'.
167                    format(ver=data['version'].rstrip('\0x00')))
168         if verbose:
169             version += ('Compile date:     {date}\n'
170                         'Compile location: {cl}\n '.
171                         format(date=data['build_date'].rstrip('\0x00'),
172                                cl=data['build_directory'].rstrip('\0x00')))
173         logger.info(version)
174         return data['version'].rstrip('\0x00')
175
176     @staticmethod
177     def show_vpp_version_on_all_duts(nodes):
178         """Show VPP version verbose on all DUTs.
179
180         :param nodes: Nodes in the topology.
181         :type nodes: dict
182         """
183         for node in nodes.values():
184             if node['type'] == NodeType.DUT:
185                 VPPUtil.vpp_show_version(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_enable_traces_on_dut(node):
216         """Enable vpp packet traces on the DUT node.
217
218         :param node: DUT node to set up.
219         :type node: dict
220         """
221         vat = VatExecutor()
222         vat.execute_script("enable_dpdk_traces.vat", node, json_out=False)
223         vat.execute_script("enable_vhost_user_traces.vat", node, json_out=False)
224         vat.execute_script("enable_memif_traces.vat", node, json_out=False)
225
226     @staticmethod
227     def vpp_enable_traces_on_all_duts(nodes):
228         """Enable vpp packet traces on all DUTs in the given topology.
229
230         :param nodes: Nodes in the topology.
231         :type nodes: dict
232         """
233         for node in nodes.values():
234             if node['type'] == NodeType.DUT:
235                 VPPUtil.vpp_enable_traces_on_dut(node)
236
237     @staticmethod
238     def vpp_enable_elog_traces_on_dut(node):
239         """Enable API/CLI/Barrier traces on the DUT node.
240
241         :param node: DUT node to set up.
242         :type node: dict
243         """
244         vat = VatExecutor()
245         vat.execute_script("elog_trace_api_cli_barrier.vat", node,
246                            json_out=False)
247
248     @staticmethod
249     def vpp_enable_elog_traces_on_all_duts(nodes):
250         """Enable API/CLI/Barrier traces on all DUTs in the given topology.
251
252         :param nodes: Nodes in the topology.
253         :type nodes: dict
254         """
255         for node in nodes.values():
256             if node['type'] == NodeType.DUT:
257                 VPPUtil.vpp_enable_elog_traces_on_dut(node)
258
259     @staticmethod
260     def show_event_logger_on_dut(node):
261         """Show event logger on the DUT node.
262
263         :param node: DUT node to show traces on.
264         :type node: dict
265         """
266         vat = VatExecutor()
267         vat.execute_script("show_event_logger.vat", node, json_out=False)
268
269     @staticmethod
270     def show_event_logger_on_all_duts(nodes):
271         """Show event logger on all DUTs in the given topology.
272
273         :param nodes: Nodes in the topology.
274         :type nodes: dict
275         """
276         for node in nodes.values():
277             if node['type'] == NodeType.DUT:
278                 VPPUtil.show_event_logger_on_dut(node)
279
280     @staticmethod
281     def show_log(node):
282         """Show log on the specified topology node.
283
284         :param node: Topology node.
285         :type node: dict
286         :returns: VPP log data.
287         :rtype: list
288         """
289         with PapiExecutor(node) as papi_exec:
290             return papi_exec.add('cli_inband', cmd='show log').get_replies().\
291                 verify_reply()["reply"]
292
293     @staticmethod
294     def vpp_show_threads(node):
295         """Show VPP threads on node.
296
297         :param node: Node to run command on.
298         :type node: dict
299         :returns: VPP thread data.
300         :rtype: list
301         """
302         with PapiExecutor(node) as papi_exec:
303             return papi_exec.add('show_threads').execute_should_pass().\
304                 verify_reply()["thread_data"]