Telemetry: Add more operational data
[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 PapiSocketExecutor
21 from resources.libraries.python.ssh import exec_cmd_no_error
22 from resources.libraries.python.topology import Topology, SocketType, NodeType
23
24
25 class VPPUtil:
26     """General class for any VPP related methods/functions."""
27
28     @staticmethod
29     def show_vpp_settings(node, *additional_cmds):
30         """Print default VPP settings. In case others are needed, can be
31         accepted as next parameters (each setting one parameter), preferably
32         in form of a string.
33
34         :param node: VPP node.
35         :param additional_cmds: Additional commands that the vpp should print
36             settings for.
37         :type node: dict
38         :type additional_cmds: tuple
39         """
40         def_setting_tb_displayed = {
41             u"IPv6 FIB": u"ip6 fib",
42             u"IPv4 FIB": u"ip fib",
43             u"Interface IP": u"int addr",
44             u"Interfaces": u"int",
45             u"ARP": u"ip arp",
46             u"Errors": u"err"
47         }
48
49         if additional_cmds:
50             for cmd in additional_cmds:
51                 def_setting_tb_displayed[f"Custom Setting: {cmd}"] = cmd
52
53         for _, cmd in def_setting_tb_displayed.items():
54             command = f"vppctl sh {cmd}"
55             exec_cmd_no_error(node, command, timeout=30, sudo=True)
56
57     @staticmethod
58     def restart_vpp_service(node, node_key=None):
59         """Restart VPP service on the specified topology node.
60
61         :param node: Topology node.
62         :param node_key: Topology node key.
63         :type node: dict
64         :type node_key: str
65         """
66         DUTSetup.restart_service(node, Constants.VPP_UNIT)
67         if node_key:
68             Topology.add_new_socket(
69                 node, SocketType.PAPI, node_key, Constants.SOCKSVR_PATH)
70             Topology.add_new_socket(
71                 node, SocketType.STATS, node_key, Constants.SOCKSTAT_PATH)
72
73     @staticmethod
74     def restart_vpp_service_on_all_duts(nodes):
75         """Restart VPP service on all DUT nodes.
76
77         :param nodes: Topology nodes.
78         :type nodes: dict
79         """
80         for node_key, node in nodes.items():
81             if node[u"type"] == NodeType.DUT:
82                 VPPUtil.restart_vpp_service(node, node_key)
83
84     @staticmethod
85     def stop_vpp_service(node, node_key=None):
86         """Stop VPP service on the specified topology node.
87
88         :param node: Topology node.
89         :param node_key: Topology node key.
90         :type node: dict
91         :type node_key: str
92         """
93         DUTSetup.stop_service(node, Constants.VPP_UNIT)
94         if node_key:
95             Topology.del_node_socket_id(node, SocketType.PAPI, node_key)
96             Topology.del_node_socket_id(node, SocketType.STATS, node_key)
97
98     @staticmethod
99     def stop_vpp_service_on_all_duts(nodes):
100         """Stop VPP service on all DUT nodes.
101
102         :param nodes: Topology nodes.
103         :type nodes: dict
104         """
105         for node_key, node in nodes.items():
106             if node[u"type"] == NodeType.DUT:
107                 VPPUtil.stop_vpp_service(node, node_key)
108
109     @staticmethod
110     def verify_vpp_installed(node):
111         """Verify that VPP is installed on the specified topology node.
112
113         :param node: Topology node.
114         :type node: dict
115         """
116         cmd = u"command -v vpp"
117         exec_cmd_no_error(node, cmd, message=u"VPP is not installed!")
118
119     @staticmethod
120     def adjust_privileges(node):
121         """Adjust privileges to control VPP without sudo.
122
123         :param node: Topology node.
124         :type node: dict
125         """
126         cmd = u"chmod -R o+rwx /run/vpp"
127         exec_cmd_no_error(
128             node, cmd, sudo=True, message=u"Failed to adjust privileges!",
129             retries=120)
130
131     @staticmethod
132     def verify_vpp_started(node):
133         """Verify that VPP is started on the specified topology node.
134
135         :param node: Topology node.
136         :type node: dict
137         """
138         cmd = u"echo \"show ver\" | sudo socat - UNIX-CONNECT:/run/vpp/cli.sock"
139         exec_cmd_no_error(
140             node, cmd, sudo=False, message=u"VPP failed to start!", retries=120
141         )
142
143         cmd = u"vppctl show ver 2>&1 | fgrep -v \"Connection refused\" | " \
144               u"fgrep -v \"No such file or directory\""
145         exec_cmd_no_error(
146             node, cmd, sudo=True, message=u"VPP failed to start!", retries=120
147         )
148
149     @staticmethod
150     def verify_vpp(node):
151         """Verify that VPP is installed and started on the specified topology
152         node. Adjust privileges so user can connect without sudo.
153
154         :param node: Topology node.
155         :type node: dict
156         :raises RuntimeError: If VPP service fails to start.
157         """
158         VPPUtil.verify_vpp_installed(node)
159         try:
160             # Verify responsiveness of vppctl.
161             VPPUtil.verify_vpp_started(node)
162             # Adjust privileges.
163             VPPUtil.adjust_privileges(node)
164             # Verify responsiveness of PAPI.
165             VPPUtil.show_log(node)
166             VPPUtil.vpp_show_version(node)
167         finally:
168             DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
169
170     @staticmethod
171     def verify_vpp_on_all_duts(nodes):
172         """Verify that VPP is installed and started on all DUT nodes.
173
174         :param nodes: Nodes in the topology.
175         :type nodes: dict
176         """
177         for node in nodes.values():
178             if node[u"type"] == NodeType.DUT:
179                 VPPUtil.verify_vpp(node)
180
181     @staticmethod
182     def vpp_show_version(node):
183         """Run "show_version" PAPI command.
184
185         :param node: Node to run command on.
186         :type node: dict
187         :returns: VPP version.
188         :rtype: str
189         """
190         cmd = u"show_version"
191         with PapiSocketExecutor(node) as papi_exec:
192             reply = papi_exec.add(cmd).get_reply()
193         logger.info(f"VPP version: {reply[u'version']}\n")
194         return f"{reply[u'version']}"
195
196     @staticmethod
197     def show_vpp_version_on_all_duts(nodes):
198         """Show VPP version verbose on all DUTs.
199
200         :param nodes: Nodes in the topology.
201         :type nodes: dict
202         """
203         for node in nodes.values():
204             if node[u"type"] == NodeType.DUT:
205                 VPPUtil.vpp_show_version(node)
206
207     @staticmethod
208     def vpp_show_interfaces(node):
209         """Run "show interface" CLI command.
210
211         :param node: Node to run command on.
212         :type node: dict
213         """
214
215         cmd = u"sw_interface_dump"
216         args = dict(
217             name_filter_valid=False,
218             name_filter=u""
219         )
220         err_msg = f"Failed to get interface dump on host {node[u'host']}"
221         with PapiSocketExecutor(node) as papi_exec:
222             details = papi_exec.add(cmd, **args).get_details(err_msg)
223
224         for if_dump in details:
225             if_dump[u"l2_address"] = str(if_dump[u"l2_address"])
226             if_dump[u"b_dmac"] = str(if_dump[u"b_dmac"])
227             if_dump[u"b_smac"] = str(if_dump[u"b_smac"])
228             if_dump[u"flags"] = if_dump[u"flags"].value
229             if_dump[u"type"] = if_dump[u"type"].value
230             if_dump[u"link_duplex"] = if_dump[u"link_duplex"].value
231             if_dump[u"sub_if_flags"] = if_dump[u"sub_if_flags"].value \
232                 if hasattr(if_dump[u"sub_if_flags"], u"value") \
233                 else int(if_dump[u"sub_if_flags"])
234         # TODO: return only base data
235         logger.trace(f"Interface data of host {node[u'host']}:\n{details}")
236
237     @staticmethod
238     def vpp_enable_traces_on_dut(node, fail_on_error=False):
239         """Enable vpp packet traces on the DUT node.
240
241         :param node: DUT node to set up.
242         :param fail_on_error: If True, keyword fails if an error occurs,
243             otherwise passes.
244         :type node: dict
245         :type fail_on_error: bool
246         """
247         cmds = [
248             u"trace add dpdk-input 50",
249             u"trace add vhost-user-input 50",
250             u"trace add memif-input 50",
251             u"trace add avf-input 50"
252         ]
253
254         for cmd in cmds:
255             try:
256                 PapiSocketExecutor.run_cli_cmd_on_all_sockets(node, cmd)
257             except AssertionError:
258                 if fail_on_error:
259                     raise
260
261     @staticmethod
262     def vpp_enable_traces_on_all_duts(nodes, fail_on_error=False):
263         """Enable vpp packet traces on all DUTs in the given topology.
264
265         :param nodes: Nodes in the topology.
266         :param fail_on_error: If True, keyword fails if an error occurs,
267             otherwise passes.
268         :type nodes: dict
269         :type fail_on_error: bool
270         """
271         for node in nodes.values():
272             if node[u"type"] == NodeType.DUT:
273                 VPPUtil.vpp_enable_traces_on_dut(node, fail_on_error)
274
275     @staticmethod
276     def vpp_enable_elog_traces(node):
277         """Enable API/CLI/Barrier traces on the specified topology node.
278
279         :param node: Topology node.
280         :type node: dict
281         """
282         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
283             node, u"elog trace api cli barrier")
284
285     @staticmethod
286     def vpp_enable_elog_traces_on_all_duts(nodes):
287         """Enable API/CLI/Barrier traces on all DUTs in the given topology.
288
289         :param nodes: Nodes in the topology.
290         :type nodes: dict
291         """
292         for node in nodes.values():
293             if node[u"type"] == NodeType.DUT:
294                 VPPUtil.vpp_enable_elog_traces(node)
295
296     @staticmethod
297     def show_event_logger(node):
298         """Show event logger on the specified topology node.
299
300         :param node: Topology node.
301         :type node: dict
302         """
303         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
304             node, u"show event-logger")
305
306     @staticmethod
307     def show_event_logger_on_all_duts(nodes):
308         """Show event logger on all DUTs in the given topology.
309
310         :param nodes: Nodes in the topology.
311         :type nodes: dict
312         """
313         for node in nodes.values():
314             if node[u"type"] == NodeType.DUT:
315                 VPPUtil.show_event_logger(node)
316
317     @staticmethod
318     def show_log(node):
319         """Show logging on the specified topology node.
320
321         :param node: Topology node.
322         :type node: dict
323         """
324         PapiSocketExecutor.run_cli_cmd(node, u"show logging")
325
326     @staticmethod
327     def show_log_on_all_duts(nodes):
328         """Show logging on all DUTs in the given topology.
329
330         :param nodes: Nodes in the topology.
331         :type nodes: dict
332         """
333         for node in nodes.values():
334             if node[u"type"] == NodeType.DUT:
335                 VPPUtil.show_log(node)
336
337     @staticmethod
338     def vpp_show_threads(node):
339         """Show VPP threads on node.
340
341         :param node: Node to run command on.
342         :type node: dict
343         :returns: VPP thread data.
344         :rtype: list
345         """
346         cmd = u"show_threads"
347         with PapiSocketExecutor(node) as papi_exec:
348             reply = papi_exec.add(cmd).get_reply()
349
350         threads_data = list()
351         for thread in reply[u"thread_data"]:
352             thread_data = list()
353             for item in thread:
354                 if isinstance(item, str):
355                     item = item.rstrip('\x00')
356                 thread_data.append(item)
357             threads_data.append(thread_data)
358
359         logger.trace(f"show threads:\n{threads_data}")
360
361         return threads_data