Revert "vpp-device: GENEVE tunnel test, l3 mode"
[csit.git] / resources / libraries / python / VPPUtil.py
1 # Copyright (c) 2020 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, exec_cmd
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         DUTSetup.verify_program_installed(node, u"vpp")
117
118     @staticmethod
119     def adjust_privileges(node):
120         """Adjust privileges to control VPP without sudo.
121
122         :param node: Topology node.
123         :type node: dict
124         """
125         cmd = u"chmod -R o+rwx /run/vpp"
126         exec_cmd_no_error(
127             node, cmd, sudo=True, message=u"Failed to adjust privileges!",
128             retries=120)
129
130     @staticmethod
131     def verify_vpp_started(node):
132         """Verify that VPP is started on the specified topology node.
133
134         :param node: Topology node.
135         :type node: dict
136         """
137         cmd = u"echo \"show pci\" | sudo socat - UNIX-CONNECT:/run/vpp/cli.sock"
138         exec_cmd_no_error(
139             node, cmd, sudo=False, message=u"VPP failed to start!", retries=120
140         )
141
142         cmd = u"vppctl show pci 2>&1 | fgrep -v \"Connection refused\" | " \
143               u"fgrep -v \"No such file or directory\""
144         exec_cmd_no_error(
145             node, cmd, sudo=True, message=u"VPP failed to start!", retries=120
146         )
147
148         # Properly enable cards in case they were disabled. This will be
149         # followed in https://jira.fd.io/browse/VPP-1934.
150         cmd = u"for i in $(sudo vppctl sho int | grep Eth | cut -d' ' -f1); do"\
151               u" sudo vppctl set int sta $i up; done"
152         exec_cmd(node, cmd, sudo=False)
153
154     @staticmethod
155     def verify_vpp(node):
156         """Verify that VPP is installed and started on the specified topology
157         node. Adjust privileges so user can connect without sudo.
158
159         :param node: Topology node.
160         :type node: dict
161         :raises RuntimeError: If VPP service fails to start.
162         """
163         DUTSetup.verify_program_installed(node, 'vpp')
164         try:
165             # Verify responsiveness of vppctl.
166             VPPUtil.verify_vpp_started(node)
167             # Adjust privileges.
168             VPPUtil.adjust_privileges(node)
169             # Verify responsiveness of PAPI.
170             VPPUtil.show_log(node)
171             VPPUtil.vpp_show_version(node)
172         finally:
173             DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
174
175     @staticmethod
176     def verify_vpp_on_all_duts(nodes):
177         """Verify that VPP is installed and started on all DUT nodes.
178
179         :param nodes: Nodes in the topology.
180         :type nodes: dict
181         """
182         for node in nodes.values():
183             if node[u"type"] == NodeType.DUT:
184                 VPPUtil.verify_vpp(node)
185
186     @staticmethod
187     def vpp_show_version(node):
188         """Run "show_version" PAPI command.
189
190         :param node: Node to run command on.
191         :type node: dict
192         :returns: VPP version.
193         :rtype: str
194         """
195         cmd = u"show_version"
196         with PapiSocketExecutor(node) as papi_exec:
197             reply = papi_exec.add(cmd).get_reply()
198         logger.info(f"VPP version: {reply[u'version']}\n")
199         return f"{reply[u'version']}"
200
201     @staticmethod
202     def show_vpp_version_on_all_duts(nodes):
203         """Show VPP version verbose on all DUTs.
204
205         :param nodes: Nodes in the topology.
206         :type nodes: dict
207         """
208         for node in nodes.values():
209             if node[u"type"] == NodeType.DUT:
210                 VPPUtil.vpp_show_version(node)
211
212     @staticmethod
213     def vpp_show_interfaces(node):
214         """Run "show interface" CLI command.
215
216         :param node: Node to run command on.
217         :type node: dict
218         """
219
220         cmd = u"sw_interface_dump"
221         args = dict(
222             name_filter_valid=False,
223             name_filter=u""
224         )
225         err_msg = f"Failed to get interface dump on host {node[u'host']}"
226         with PapiSocketExecutor(node) as papi_exec:
227             details = papi_exec.add(cmd, **args).get_details(err_msg)
228
229         for if_dump in details:
230             if_dump[u"l2_address"] = str(if_dump[u"l2_address"])
231             if_dump[u"b_dmac"] = str(if_dump[u"b_dmac"])
232             if_dump[u"b_smac"] = str(if_dump[u"b_smac"])
233             if_dump[u"flags"] = if_dump[u"flags"].value
234             if_dump[u"type"] = if_dump[u"type"].value
235             if_dump[u"link_duplex"] = if_dump[u"link_duplex"].value
236             if_dump[u"sub_if_flags"] = if_dump[u"sub_if_flags"].value \
237                 if hasattr(if_dump[u"sub_if_flags"], u"value") \
238                 else int(if_dump[u"sub_if_flags"])
239         # TODO: return only base data
240         logger.trace(f"Interface data of host {node[u'host']}:\n{details}")
241
242     @staticmethod
243     def vpp_enable_traces_on_dut(node, fail_on_error=False):
244         """Enable vpp packet traces on the DUT node.
245
246         :param node: DUT node to set up.
247         :param fail_on_error: If True, keyword fails if an error occurs,
248             otherwise passes.
249         :type node: dict
250         :type fail_on_error: bool
251         """
252         cmds = [
253             u"trace add dpdk-input 50",
254             u"trace add vhost-user-input 50",
255             u"trace add memif-input 50",
256             u"trace add avf-input 50"
257         ]
258
259         for cmd in cmds:
260             try:
261                 PapiSocketExecutor.run_cli_cmd_on_all_sockets(node, cmd)
262             except AssertionError:
263                 if fail_on_error:
264                     raise
265
266     @staticmethod
267     def vpp_enable_traces_on_all_duts(nodes, fail_on_error=False):
268         """Enable vpp packet traces on all DUTs in the given topology.
269
270         :param nodes: Nodes in the topology.
271         :param fail_on_error: If True, keyword fails if an error occurs,
272             otherwise passes.
273         :type nodes: dict
274         :type fail_on_error: bool
275         """
276         for node in nodes.values():
277             if node[u"type"] == NodeType.DUT:
278                 VPPUtil.vpp_enable_traces_on_dut(node, fail_on_error)
279
280     @staticmethod
281     def vpp_enable_elog_traces(node):
282         """Enable API/CLI/Barrier traces on the specified topology node.
283
284         :param node: Topology node.
285         :type node: dict
286         """
287         try:
288             PapiSocketExecutor.run_cli_cmd_on_all_sockets(
289                 node, u"event-logger trace api cli barrier")
290         except AssertionError:
291             # Perhaps an older VPP build is tested.
292             PapiSocketExecutor.run_cli_cmd_on_all_sockets(
293                 node, u"elog trace api cli barrier")
294
295     @staticmethod
296     def vpp_enable_elog_traces_on_all_duts(nodes):
297         """Enable API/CLI/Barrier traces on all DUTs in the given topology.
298
299         :param nodes: Nodes in the topology.
300         :type nodes: dict
301         """
302         for node in nodes.values():
303             if node[u"type"] == NodeType.DUT:
304                 VPPUtil.vpp_enable_elog_traces(node)
305
306     @staticmethod
307     def show_event_logger(node):
308         """Show event logger on the specified topology node.
309
310         :param node: Topology node.
311         :type node: dict
312         """
313         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
314             node, u"show event-logger")
315
316     @staticmethod
317     def show_event_logger_on_all_duts(nodes):
318         """Show event logger on all DUTs in the given topology.
319
320         :param nodes: Nodes in the topology.
321         :type nodes: dict
322         """
323         for node in nodes.values():
324             if node[u"type"] == NodeType.DUT:
325                 VPPUtil.show_event_logger(node)
326
327     @staticmethod
328     def show_log(node):
329         """Show logging on the specified topology node.
330
331         :param node: Topology node.
332         :type node: dict
333         """
334         PapiSocketExecutor.run_cli_cmd(node, u"show logging")
335
336     @staticmethod
337     def show_log_on_all_duts(nodes):
338         """Show logging on all DUTs in the given topology.
339
340         :param nodes: Nodes in the topology.
341         :type nodes: dict
342         """
343         for node in nodes.values():
344             if node[u"type"] == NodeType.DUT:
345                 VPPUtil.show_log(node)
346
347     @staticmethod
348     def vpp_show_threads(node):
349         """Show VPP threads on node.
350
351         :param node: Node to run command on.
352         :type node: dict
353         :returns: VPP thread data.
354         :rtype: list
355         """
356         cmd = u"show_threads"
357         with PapiSocketExecutor(node) as papi_exec:
358             reply = papi_exec.add(cmd).get_reply()
359
360         threads_data = reply[u"thread_data"]
361         logger.trace(f"show threads:\n{threads_data}")
362
363         return threads_data