1 # Copyright (c) 2021 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.model.ExportResult import (
22 export_dut_type_and_version
24 from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
25 from resources.libraries.python.topology import Topology, SocketType, NodeType
29 """General class for any VPP related methods/functions."""
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
37 :param node: VPP node.
38 :param additional_cmds: Additional commands that the vpp should print
41 :type additional_cmds: tuple
43 def_setting_tb_displayed = {
44 u"IPv6 FIB": u"ip6 fib",
45 u"IPv4 FIB": u"ip fib",
46 u"Interface IP": u"int addr",
47 u"Interfaces": u"int",
53 for cmd in additional_cmds:
54 def_setting_tb_displayed[f"Custom Setting: {cmd}"] = cmd
56 for _, cmd in def_setting_tb_displayed.items():
57 command = f"vppctl sh {cmd}"
58 exec_cmd_no_error(node, command, timeout=30, sudo=True)
61 def restart_vpp_service(node, node_key=None):
62 """Restart VPP service on the specified topology node.
64 Disconnect possibly connected PAPI executor.
66 :param node: Topology node.
67 :param node_key: Topology node key.
71 # Containers have a separate lifecycle, but better be safe.
72 PapiSocketExecutor.disconnect_all_sockets_by_node(node)
73 DUTSetup.restart_service(node, Constants.VPP_UNIT)
75 Topology.add_new_socket(
76 node, SocketType.PAPI, node_key, Constants.SOCKSVR_PATH)
77 Topology.add_new_socket(
78 node, SocketType.STATS, node_key, Constants.SOCKSTAT_PATH)
81 def restart_vpp_service_on_all_duts(nodes):
82 """Restart VPP service on all DUT nodes.
84 :param nodes: Topology nodes.
87 for node_key, node in nodes.items():
88 if node[u"type"] == NodeType.DUT:
89 VPPUtil.restart_vpp_service(node, node_key)
92 def stop_vpp_service(node, node_key=None):
93 """Stop VPP service on the specified topology node.
95 Disconnect possibly connected PAPI executor.
97 :param node: Topology node.
98 :param node_key: Topology node key.
102 # Containers have a separate lifecycle, but better be safe.
103 PapiSocketExecutor.disconnect_all_sockets_by_node(node)
104 DUTSetup.stop_service(node, Constants.VPP_UNIT)
106 Topology.del_node_socket_id(node, SocketType.PAPI, node_key)
107 Topology.del_node_socket_id(node, SocketType.STATS, node_key)
110 def stop_vpp_service_on_all_duts(nodes):
111 """Stop VPP service on all DUT nodes.
113 :param nodes: Topology nodes.
116 for node_key, node in nodes.items():
117 if node[u"type"] == NodeType.DUT:
118 VPPUtil.stop_vpp_service(node, node_key)
121 def verify_vpp_installed(node):
122 """Verify that VPP is installed on the specified topology node.
124 :param node: Topology node.
127 DUTSetup.verify_program_installed(node, u"vpp")
130 def adjust_privileges(node):
131 """Adjust privileges to control VPP without sudo.
133 :param node: Topology node.
136 cmd = u"chmod -R o+rwx /run/vpp"
138 node, cmd, sudo=True, message=u"Failed to adjust privileges!",
142 def verify_vpp_started(node):
143 """Verify that VPP is started on the specified topology node.
145 :param node: Topology node.
148 cmd = u"echo \"show pci\" | sudo socat - UNIX-CONNECT:/run/vpp/cli.sock"
150 node, cmd, sudo=False, message=u"VPP failed to start!", retries=120
153 cmd = u"vppctl show pci 2>&1 | fgrep -v \"Connection refused\" | " \
154 u"fgrep -v \"No such file or directory\""
156 node, cmd, sudo=True, message=u"VPP failed to start!", retries=120
159 # Properly enable cards in case they were disabled. This will be
160 # followed in https://jira.fd.io/browse/VPP-1934.
161 cmd = u"for i in $(sudo vppctl sho int | grep Eth | cut -d' ' -f1); do"\
162 u" sudo vppctl set int sta $i up; done"
163 exec_cmd(node, cmd, sudo=False)
166 def verify_vpp(node):
167 """Verify that VPP is installed and started on the specified topology
168 node. Adjust privileges so user can connect without sudo.
170 :param node: Topology node.
172 :raises RuntimeError: If VPP service fails to start.
174 DUTSetup.verify_program_installed(node, 'vpp')
176 # Verify responsiveness of vppctl.
177 VPPUtil.verify_vpp_started(node)
179 VPPUtil.adjust_privileges(node)
180 # Verify responsiveness of PAPI.
181 VPPUtil.show_log(node)
182 VPPUtil.vpp_show_version(node)
184 DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
187 def verify_vpp_on_all_duts(nodes):
188 """Verify that VPP is installed and started on all DUT nodes.
190 :param nodes: Nodes in the topology.
193 for node in nodes.values():
194 if node[u"type"] == NodeType.DUT:
195 VPPUtil.verify_vpp(node)
198 def vpp_show_version(
199 node, remote_vpp_socket=Constants.SOCKSVR_PATH, log=True):
200 """Run "show_version" PAPI command.
202 Socket is configurable, so VPP inside container can be accessed.
203 The result is exported to JSON UTI output as "dut-version".
205 :param node: Node to run command on.
206 :param remote_vpp_socket: Path to remote socket to target VPP.
207 :param log: If true, show the result in Robot log.
209 :type remote_vpp_socket: str
211 :returns: VPP version.
213 :raises RuntimeError: If PAPI connection fails.
214 :raises AssertionError: If PAPI retcode is nonzero.
216 cmd = u"show_version"
217 with PapiSocketExecutor(node, remote_vpp_socket) as papi_exec:
218 reply = papi_exec.add(cmd).get_reply()
220 logger.info(f"VPP version: {reply[u'version']}\n")
221 version = f"{reply[u'version']}"
222 export_dut_type_and_version(u"VPP", version)
226 def show_vpp_version_on_all_duts(nodes):
227 """Show VPP version verbose on all DUTs.
229 :param nodes: Nodes in the topology.
232 for node in nodes.values():
233 if node[u"type"] == NodeType.DUT:
234 VPPUtil.vpp_show_version(node)
237 def vpp_show_interfaces(node):
238 """Run "show interface" CLI command.
240 :param node: Node to run command on.
244 cmd = u"sw_interface_dump"
246 name_filter_valid=False,
249 err_msg = f"Failed to get interface dump on host {node[u'host']}"
250 with PapiSocketExecutor(node) as papi_exec:
251 details = papi_exec.add(cmd, **args).get_details(err_msg)
253 for if_dump in details:
254 if_dump[u"l2_address"] = str(if_dump[u"l2_address"])
255 if_dump[u"b_dmac"] = str(if_dump[u"b_dmac"])
256 if_dump[u"b_smac"] = str(if_dump[u"b_smac"])
257 if_dump[u"flags"] = if_dump[u"flags"].value
258 if_dump[u"type"] = if_dump[u"type"].value
259 if_dump[u"link_duplex"] = if_dump[u"link_duplex"].value
260 if_dump[u"sub_if_flags"] = if_dump[u"sub_if_flags"].value \
261 if hasattr(if_dump[u"sub_if_flags"], u"value") \
262 else int(if_dump[u"sub_if_flags"])
263 # TODO: return only base data
264 logger.trace(f"Interface data of host {node[u'host']}:\n{details}")
267 def vpp_enable_traces_on_dut(node, fail_on_error=False):
268 """Enable vpp packet traces on the DUT node.
270 :param node: DUT node to set up.
271 :param fail_on_error: If True, keyword fails if an error occurs,
274 :type fail_on_error: bool
277 u"trace add dpdk-input 50",
278 u"trace add vhost-user-input 50",
279 u"trace add memif-input 50",
280 u"trace add avf-input 50"
285 PapiSocketExecutor.run_cli_cmd_on_all_sockets(node, cmd)
286 except AssertionError:
291 def vpp_enable_traces_on_all_duts(nodes, fail_on_error=False):
292 """Enable vpp packet traces on all DUTs in the given topology.
294 :param nodes: Nodes in the topology.
295 :param fail_on_error: If True, keyword fails if an error occurs,
298 :type fail_on_error: bool
300 for node in nodes.values():
301 if node[u"type"] == NodeType.DUT:
302 VPPUtil.vpp_enable_traces_on_dut(node, fail_on_error)
305 def vpp_enable_elog_traces(node):
306 """Enable API/CLI/Barrier traces on the specified topology node.
308 :param node: Topology node.
312 PapiSocketExecutor.run_cli_cmd_on_all_sockets(
313 node, u"event-logger trace api cli barrier")
314 except AssertionError:
315 # Perhaps an older VPP build is tested.
316 PapiSocketExecutor.run_cli_cmd_on_all_sockets(
317 node, u"elog trace api cli barrier")
320 def vpp_enable_elog_traces_on_all_duts(nodes):
321 """Enable API/CLI/Barrier traces on all DUTs in the given topology.
323 :param nodes: Nodes in the topology.
326 for node in nodes.values():
327 if node[u"type"] == NodeType.DUT:
328 VPPUtil.vpp_enable_elog_traces(node)
331 def show_event_logger(node):
332 """Show event logger on the specified topology node.
334 :param node: Topology node.
337 PapiSocketExecutor.run_cli_cmd_on_all_sockets(
338 node, u"show event-logger")
341 def show_event_logger_on_all_duts(nodes):
342 """Show event logger on all DUTs in the given topology.
344 :param nodes: Nodes in the topology.
347 for node in nodes.values():
348 if node[u"type"] == NodeType.DUT:
349 VPPUtil.show_event_logger(node)
353 """Show logging on the specified topology node.
355 :param node: Topology node.
358 PapiSocketExecutor.run_cli_cmd(node, u"show logging")
361 def show_log_on_all_duts(nodes):
362 """Show logging on all DUTs in the given topology.
364 :param nodes: Nodes in the topology.
367 for node in nodes.values():
368 if node[u"type"] == NodeType.DUT:
369 VPPUtil.show_log(node)
372 def vpp_show_threads(node):
373 """Show VPP threads on node.
375 :param node: Node to run command on.
377 :returns: VPP thread data.
380 cmd = u"show_threads"
381 with PapiSocketExecutor(node) as papi_exec:
382 reply = papi_exec.add(cmd).get_reply()
384 threads_data = reply[u"thread_data"]
385 logger.trace(f"show threads:\n{threads_data}")
390 def vpp_add_graph_node_next(node, graph_node_name, graph_next_name):
391 """Set the next node for a given node.
393 :param node: Node to run command on.
394 :param graph_node_name: Graph node to add the next node on.
395 :param graph_next_name: Graph node to add as the next node.
397 :type graph_node_name: str
398 :type graph_next_name: str
399 :returns: The index of the next graph node.
402 cmd = u"add_node_next"
404 node_name=graph_node_name,
405 next_name=graph_next_name
407 with PapiSocketExecutor(node) as papi_exec:
408 reply = papi_exec.add(cmd, **args).get_reply()
410 return reply[u"next_index"]