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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """VPP util library."""
16 from robot.api import logger
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
26 """General class for any VPP related methods/functions."""
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
34 :param node: VPP node.
35 :param additional_cmds: Additional commands that the vpp should print
38 :type additional_cmds: tuple
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",
50 for cmd in additional_cmds:
51 def_setting_tb_displayed[f"Custom Setting: {cmd}"] = cmd
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)
58 def restart_vpp_service(node, node_key=None):
59 """Restart VPP service on the specified topology node.
61 :param node: Topology node.
62 :param node_key: Topology node key.
66 DUTSetup.restart_service(node, Constants.VPP_UNIT)
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)
74 def restart_vpp_service_on_all_duts(nodes):
75 """Restart VPP service on all DUT nodes.
77 :param nodes: Topology nodes.
80 for node_key, node in nodes.items():
81 if node[u"type"] == NodeType.DUT:
82 VPPUtil.restart_vpp_service(node, node_key)
85 def stop_vpp_service(node, node_key=None):
86 """Stop VPP service on the specified topology node.
88 :param node: Topology node.
89 :param node_key: Topology node key.
93 DUTSetup.stop_service(node, Constants.VPP_UNIT)
95 Topology.del_node_socket_id(node, SocketType.PAPI, node_key)
96 Topology.del_node_socket_id(node, SocketType.STATS, node_key)
99 def stop_vpp_service_on_all_duts(nodes):
100 """Stop VPP service on all DUT nodes.
102 :param nodes: Topology nodes.
105 for node_key, node in nodes.items():
106 if node[u"type"] == NodeType.DUT:
107 VPPUtil.stop_vpp_service(node, node_key)
110 def verify_vpp_installed(node):
111 """Verify that VPP is installed on the specified topology node.
113 :param node: Topology node.
116 cmd = u"command -v vpp"
117 exec_cmd_no_error(node, cmd, message=u"VPP is not installed!")
120 def verify_vpp_started(node):
121 """Verify that VPP is started on the specified topology node.
123 :param node: Topology node.
126 cmd = u"echo \"show ver\" | sudo socat - UNIX-CONNECT:/run/vpp/cli.sock"
128 node, cmd, sudo=False, message=u"VPP failed to start!", retries=120
131 cmd = u"vppctl show ver 2>&1 | fgrep -v \"Connection refused\" | " \
132 u"fgrep -v \"No such file or directory\""
134 node, cmd, sudo=True, message=u"VPP failed to start!", retries=120
138 def verify_vpp(node):
139 """Verify that VPP is installed and started on the specified topology
142 :param node: Topology node.
144 :raises RuntimeError: If VPP service fails to start.
146 VPPUtil.verify_vpp_installed(node)
148 # Verify responsiveness of vppctl.
149 VPPUtil.verify_vpp_started(node)
150 # Verify responsiveness of PAPI.
151 VPPUtil.show_log(node)
152 VPPUtil.vpp_show_version(node)
154 DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
157 def verify_vpp_on_all_duts(nodes):
158 """Verify that VPP is installed and started on all DUT nodes.
160 :param nodes: Nodes in the topology.
163 for node in nodes.values():
164 if node[u"type"] == NodeType.DUT:
165 VPPUtil.verify_vpp(node)
168 def vpp_show_version(node):
169 """Run "show_version" PAPI command.
171 :param node: Node to run command on.
173 :returns: VPP version.
176 cmd = u"show_version"
177 with PapiSocketExecutor(node) as papi_exec:
178 reply = papi_exec.add(cmd).get_reply()
179 logger.info(f"VPP version: {reply[u'version']}\n")
180 return f"{reply[u'version']}"
183 def show_vpp_version_on_all_duts(nodes):
184 """Show VPP version verbose on all DUTs.
186 :param nodes: Nodes in the topology.
189 for node in nodes.values():
190 if node[u"type"] == NodeType.DUT:
191 VPPUtil.vpp_show_version(node)
194 def vpp_show_interfaces(node):
195 """Run "show interface" CLI command.
197 :param node: Node to run command on.
201 cmd = u"sw_interface_dump"
203 name_filter_valid=False,
206 err_msg = f"Failed to get interface dump on host {node[u'host']}"
207 with PapiSocketExecutor(node) as papi_exec:
208 details = papi_exec.add(cmd, **args).get_details(err_msg)
210 for if_dump in details:
211 if_dump[u"l2_address"] = str(if_dump[u"l2_address"])
212 if_dump[u"b_dmac"] = str(if_dump[u"b_dmac"])
213 if_dump[u"b_smac"] = str(if_dump[u"b_smac"])
214 if_dump[u"flags"] = if_dump[u"flags"].value
215 if_dump[u"type"] = if_dump[u"type"].value
216 if_dump[u"link_duplex"] = if_dump[u"link_duplex"].value
217 if_dump[u"sub_if_flags"] = if_dump[u"sub_if_flags"].value \
218 if hasattr(if_dump[u"sub_if_flags"], u"value") \
219 else int(if_dump[u"sub_if_flags"])
220 # TODO: return only base data
221 logger.trace(f"Interface data of host {node[u'host']}:\n{details}")
224 def vpp_enable_traces_on_dut(node, fail_on_error=False):
225 """Enable vpp packet traces on the DUT node.
227 :param node: DUT node to set up.
228 :param fail_on_error: If True, keyword fails if an error occurs,
231 :type fail_on_error: bool
234 u"trace add dpdk-input 50",
235 u"trace add vhost-user-input 50",
236 u"trace add memif-input 50",
237 u"trace add avf-input 50"
242 PapiSocketExecutor.run_cli_cmd_on_all_sockets(node, cmd)
243 except AssertionError:
248 def vpp_enable_traces_on_all_duts(nodes, fail_on_error=False):
249 """Enable vpp packet traces on all DUTs in the given topology.
251 :param nodes: Nodes in the topology.
252 :param fail_on_error: If True, keyword fails if an error occurs,
255 :type fail_on_error: bool
257 for node in nodes.values():
258 if node[u"type"] == NodeType.DUT:
259 VPPUtil.vpp_enable_traces_on_dut(node, fail_on_error)
262 def vpp_enable_elog_traces(node):
263 """Enable API/CLI/Barrier traces on the specified topology node.
265 :param node: Topology node.
268 PapiSocketExecutor.run_cli_cmd_on_all_sockets(
269 node, u"elog trace api cli barrier")
272 def vpp_enable_elog_traces_on_all_duts(nodes):
273 """Enable API/CLI/Barrier traces on all DUTs in the given topology.
275 :param nodes: Nodes in the topology.
278 for node in nodes.values():
279 if node[u"type"] == NodeType.DUT:
280 VPPUtil.vpp_enable_elog_traces(node)
283 def show_event_logger(node):
284 """Show event logger on the specified topology node.
286 :param node: Topology node.
289 PapiSocketExecutor.run_cli_cmd_on_all_sockets(
290 node, u"show event-logger")
293 def show_event_logger_on_all_duts(nodes):
294 """Show event logger on all DUTs in the given topology.
296 :param nodes: Nodes in the topology.
299 for node in nodes.values():
300 if node[u"type"] == NodeType.DUT:
301 VPPUtil.show_event_logger(node)
305 """Show logging on the specified topology node.
307 :param node: Topology node.
310 PapiSocketExecutor.run_cli_cmd(node, u"show logging")
313 def show_log_on_all_duts(nodes):
314 """Show logging on all DUTs in the given topology.
316 :param nodes: Nodes in the topology.
319 for node in nodes.values():
320 if node[u"type"] == NodeType.DUT:
321 VPPUtil.show_log(node)
324 def vpp_show_threads(node):
325 """Show VPP threads on node.
327 :param node: Node to run command on.
329 :returns: VPP thread data.
332 cmd = u"show_threads"
333 with PapiSocketExecutor(node) as papi_exec:
334 reply = papi_exec.add(cmd).get_reply()
336 threads_data = list()
337 for thread in reply[u"thread_data"]:
340 if isinstance(item, str):
341 item = item.rstrip('\x00')
342 thread_data.append(item)
343 threads_data.append(thread_data)
345 logger.trace(f"show threads:\n{threads_data}")