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."""
18 from robot.api import logger
20 from resources.libraries.python.Constants import Constants
21 from resources.libraries.python.DUTSetup import DUTSetup
22 from resources.libraries.python.L2Util import L2Util
23 from resources.libraries.python.PapiExecutor import PapiExecutor
24 from resources.libraries.python.ssh import exec_cmd_no_error
25 from resources.libraries.python.topology import NodeType
28 class VPPUtil(object):
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 'IPv6 FIB': 'ip6 fib',
46 'Interface IP': 'int addr',
53 for cmd in additional_cmds:
54 def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] = cmd
56 for _, cmd in def_setting_tb_displayed.items():
57 command = 'vppctl sh {cmd}'.format(cmd=cmd)
58 exec_cmd_no_error(node, command, timeout=30, sudo=True)
61 def restart_vpp_service(node):
62 """Restart VPP service on the specified topology node.
64 :param node: Topology node.
67 DUTSetup.restart_service(node, Constants.VPP_UNIT)
70 def restart_vpp_service_on_all_duts(nodes):
71 """Restart VPP service on all DUT nodes.
73 :param nodes: Topology nodes.
76 for node in nodes.values():
77 if node['type'] == NodeType.DUT:
78 VPPUtil.restart_vpp_service(node)
81 def stop_vpp_service(node):
82 """Stop VPP service on the specified topology node.
84 :param node: Topology node.
87 DUTSetup.stop_service(node, Constants.VPP_UNIT)
90 def stop_vpp_service_on_all_duts(nodes):
91 """Stop VPP service on all DUT nodes.
93 :param nodes: Topology nodes.
96 for node in nodes.values():
97 if node['type'] == NodeType.DUT:
98 VPPUtil.stop_vpp_service(node)
101 def verify_vpp_installed(node):
102 """Verify that VPP is installed on the specified topology node.
104 :param node: Topology node.
107 cmd = 'command -v vpp'
109 node, cmd, message='VPP is not installed!')
112 def verify_vpp_started(node):
113 """Verify that VPP is started on the specified topology node.
115 :param node: Topology node.
118 cmd = ('vppctl show pci 2>&1 | '
119 'fgrep -v "Connection refused" | '
120 'fgrep -v "No such file or directory"')
122 node, cmd, sudo=True, message='VPP failed to start!', retries=120)
125 def verify_vpp(node):
126 """Verify that VPP is installed and started on the specified topology
129 :param node: Topology node.
131 :raises RuntimeError: If VPP service fails to start.
133 VPPUtil.verify_vpp_installed(node)
135 # Verify responsivness of vppctl.
136 VPPUtil.verify_vpp_started(node)
137 # Verify responsivness of PAPI.
138 VPPUtil.show_log(node)
140 DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
143 def verify_vpp_on_all_duts(nodes):
144 """Verify that VPP is installed and started on all DUT nodes.
146 :param nodes: Nodes in the topology.
149 for node in nodes.values():
150 if node['type'] == NodeType.DUT:
151 VPPUtil.verify_vpp(node)
154 def vpp_show_version(node, verbose=True):
155 """Run "show_version" PAPI command.
157 :param node: Node to run command on.
158 :param verbose: Show version, compile date and compile location if True
159 otherwise show only version.
162 :returns: VPP version.
165 with PapiExecutor(node) as papi_exec:
166 reply = papi_exec.add('show_version').get_reply()
167 return_version = reply['version'].rstrip('\0x00')
168 version = 'VPP version: {ver}\n'.format(ver=return_version)
170 version += ('Compile date: {date}\n'
171 'Compile location: {cl}\n'.
172 format(date=reply['build_date'].rstrip('\0x00'),
173 cl=reply['build_directory'].rstrip('\0x00')))
175 return return_version
178 def show_vpp_version_on_all_duts(nodes):
179 """Show VPP version verbose on all DUTs.
181 :param nodes: Nodes in the topology.
184 for node in nodes.values():
185 if node['type'] == NodeType.DUT:
186 VPPUtil.vpp_show_version(node)
189 def vpp_show_interfaces(node):
190 """Run "show interface" CLI command.
192 :param node: Node to run command on.
196 cmd = 'sw_interface_dump'
197 args = dict(name_filter_valid=0, name_filter='')
198 err_msg = 'Failed to get interface dump on host {host}'.format(
200 with PapiExecutor(node) as papi_exec:
201 details = papi_exec.add(cmd, **args).get_details(err_msg)
203 for if_dump in details:
204 if_dump['interface_name'] = if_dump['interface_name'].rstrip('\x00')
205 if_dump['tag'] = if_dump['tag'].rstrip('\x00')
206 if_dump['l2_address'] = L2Util.bin_to_mac(if_dump['l2_address'])
207 # TODO: return only base data
208 logger.trace('Interface data of host {host}:\n{details}'.format(
209 host=node['host'], details=details))
212 def vpp_enable_traces_on_dut(node, fail_on_error=False):
213 """Enable vpp packet traces on the DUT node.
215 :param node: DUT node to set up.
216 :param fail_on_error: If True, keyword fails if an error occurs,
219 :type fail_on_error: bool
222 "trace add dpdk-input 50",
223 "trace add vhost-user-input 50",
224 "trace add memif-input 50"
229 PapiExecutor.run_cli_cmd(node, cmd)
230 except AssertionError:
235 def vpp_enable_traces_on_all_duts(nodes, fail_on_error=False):
236 """Enable vpp packet traces on all DUTs in the given topology.
238 :param nodes: Nodes in the topology.
239 :param fail_on_error: If True, keyword fails if an error occurs,
242 :type fail_on_error: bool
244 for node in nodes.values():
245 if node['type'] == NodeType.DUT:
246 VPPUtil.vpp_enable_traces_on_dut(node, fail_on_error)
249 def vpp_enable_elog_traces_on_dut(node):
250 """Enable API/CLI/Barrier traces on the DUT node.
252 :param node: DUT node to set up.
255 PapiExecutor.run_cli_cmd(node, "elog trace api cli barrier")
258 def vpp_enable_elog_traces_on_all_duts(nodes):
259 """Enable API/CLI/Barrier traces on all DUTs in the given topology.
261 :param nodes: Nodes in the topology.
264 for node in nodes.values():
265 if node['type'] == NodeType.DUT:
266 VPPUtil.vpp_enable_elog_traces_on_dut(node)
269 def show_event_logger_on_dut(node):
270 """Show event logger on the DUT node.
272 :param node: DUT node to show traces on.
275 PapiExecutor.run_cli_cmd(node, "show event-logger")
278 def show_event_logger_on_all_duts(nodes):
279 """Show event logger on all DUTs in the given topology.
281 :param nodes: Nodes in the topology.
284 for node in nodes.values():
285 if node['type'] == NodeType.DUT:
286 VPPUtil.show_event_logger_on_dut(node)
290 """Show log on the specified topology node.
292 :param node: Topology node.
294 :returns: VPP log data.
297 return PapiExecutor.run_cli_cmd(node, "show log")
300 def vpp_show_threads(node):
301 """Show VPP threads on node.
303 :param node: Node to run command on.
305 :returns: VPP thread data.
308 with PapiExecutor(node) as papi_exec:
309 reply = papi_exec.add('show_threads').get_reply()
311 threads_data = list()
312 for thread in reply["thread_data"]:
315 if isinstance(item, unicode):
316 item = item.rstrip('\x00')
317 thread_data.append(item)
318 threads_data.append(thread_data)
320 logger.info("show threads:\n{threads}".format(threads=threads_data))