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.L2Util import L2Util
21 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
22 from resources.libraries.python.ssh import exec_cmd_no_error
23 from resources.libraries.python.topology import NodeType
26 class VPPUtil(object):
27 """General class for any VPP related methods/functions."""
30 def show_vpp_settings(node, *additional_cmds):
31 """Print default VPP settings. In case others are needed, can be
32 accepted as next parameters (each setting one parameter), preferably
35 :param node: VPP node.
36 :param additional_cmds: Additional commands that the vpp should print
39 :type additional_cmds: tuple
41 def_setting_tb_displayed = {
42 'IPv6 FIB': 'ip6 fib',
44 'Interface IP': 'int addr',
51 for cmd in additional_cmds:
52 def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] = cmd
54 for _, cmd in def_setting_tb_displayed.items():
55 command = 'vppctl sh {cmd}'.format(cmd=cmd)
56 exec_cmd_no_error(node, command, timeout=30, sudo=True)
59 def restart_vpp_service(node):
60 """Restart VPP service on the specified topology node.
62 :param node: Topology node.
65 DUTSetup.restart_service(node, Constants.VPP_UNIT)
68 def restart_vpp_service_on_all_duts(nodes):
69 """Restart VPP service on all DUT nodes.
71 :param nodes: Topology nodes.
74 for node in nodes.values():
75 if node['type'] == NodeType.DUT:
76 VPPUtil.restart_vpp_service(node)
79 def stop_vpp_service(node):
80 """Stop VPP service on the specified topology node.
82 :param node: Topology node.
85 DUTSetup.stop_service(node, Constants.VPP_UNIT)
88 def stop_vpp_service_on_all_duts(nodes):
89 """Stop VPP service on all DUT nodes.
91 :param nodes: Topology nodes.
94 for node in nodes.values():
95 if node['type'] == NodeType.DUT:
96 VPPUtil.stop_vpp_service(node)
99 def verify_vpp_installed(node):
100 """Verify that VPP is installed on the specified topology node.
102 :param node: Topology node.
105 cmd = 'command -v vpp'
107 node, cmd, message='VPP is not installed!')
110 def verify_vpp_started(node):
111 """Verify that VPP is started on the specified topology node.
113 :param node: Topology node.
116 cmd = ('vppctl show pci 2>&1 | '
117 'fgrep -v "Connection refused" | '
118 'fgrep -v "No such file or directory"')
120 node, cmd, sudo=True, message='VPP failed to start!', retries=120)
123 def verify_vpp(node):
124 """Verify that VPP is installed and started on the specified topology
127 :param node: Topology node.
129 :raises RuntimeError: If VPP service fails to start.
131 VPPUtil.verify_vpp_installed(node)
133 # Verify responsivness of vppctl.
134 VPPUtil.verify_vpp_started(node)
135 # Verify responsivness of PAPI.
136 VPPUtil.show_log(node)
137 VPPUtil.vpp_show_version(node)
139 DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
142 def verify_vpp_on_all_duts(nodes):
143 """Verify that VPP is installed and started on all DUT nodes.
145 :param nodes: Nodes in the topology.
148 for node in nodes.values():
149 if node['type'] == NodeType.DUT:
150 VPPUtil.verify_vpp(node)
153 def vpp_show_version(node, verbose=True):
154 """Run "show_version" PAPI command.
156 :param node: Node to run command on.
157 :param verbose: Show version, compile date and compile location if True
158 otherwise show only version.
161 :returns: VPP version.
164 with PapiSocketExecutor(node) as papi_exec:
165 reply = papi_exec.add('show_version').get_reply()
166 return_version = reply['version'].rstrip('\0x00')
167 version = 'VPP version: {ver}\n'.format(ver=return_version)
169 version += ('Compile date: {date}\n'
170 'Compile location: {cl}\n'.
171 format(date=reply['build_date'].rstrip('\0x00'),
172 cl=reply['build_directory'].rstrip('\0x00')))
174 return return_version
177 def show_vpp_version_on_all_duts(nodes):
178 """Show VPP version verbose on all DUTs.
180 :param nodes: Nodes in the topology.
183 for node in nodes.values():
184 if node['type'] == NodeType.DUT:
185 VPPUtil.vpp_show_version(node)
188 def vpp_show_interfaces(node):
189 """Run "show interface" CLI command.
191 :param node: Node to run command on.
195 cmd = 'sw_interface_dump'
196 args = dict(name_filter_valid=0, name_filter='')
197 err_msg = 'Failed to get interface dump on host {host}'.format(
199 with PapiSocketExecutor(node) as papi_exec:
200 details = papi_exec.add(cmd, **args).get_details(err_msg)
202 for if_dump in details:
203 if_dump['interface_name'] = if_dump['interface_name'].rstrip('\x00')
204 if_dump['tag'] = if_dump['tag'].rstrip('\x00')
205 if_dump['l2_address'] = L2Util.bin_to_mac(if_dump['l2_address'])
206 # TODO: return only base data
207 logger.trace('Interface data of host {host}:\n{details}'.format(
208 host=node['host'], details=details))
211 def vpp_enable_traces_on_dut(node, fail_on_error=False):
212 """Enable vpp packet traces on the DUT node.
214 :param node: DUT node to set up.
215 :param fail_on_error: If True, keyword fails if an error occurs,
218 :type fail_on_error: bool
221 "trace add dpdk-input 50",
222 "trace add vhost-user-input 50",
223 "trace add memif-input 50"
228 PapiSocketExecutor.run_cli_cmd(node, cmd)
229 except AssertionError:
234 def vpp_enable_traces_on_all_duts(nodes, fail_on_error=False):
235 """Enable vpp packet traces on all DUTs in the given topology.
237 :param nodes: Nodes in the topology.
238 :param fail_on_error: If True, keyword fails if an error occurs,
241 :type fail_on_error: bool
243 for node in nodes.values():
244 if node['type'] == NodeType.DUT:
245 VPPUtil.vpp_enable_traces_on_dut(node, fail_on_error)
248 def vpp_enable_elog_traces_on_dut(node):
249 """Enable API/CLI/Barrier traces on the DUT node.
251 :param node: DUT node to set up.
254 PapiSocketExecutor.run_cli_cmd(node, "elog trace api cli barrier")
257 def vpp_enable_elog_traces_on_all_duts(nodes):
258 """Enable API/CLI/Barrier traces on all DUTs in the given topology.
260 :param nodes: Nodes in the topology.
263 for node in nodes.values():
264 if node['type'] == NodeType.DUT:
265 VPPUtil.vpp_enable_elog_traces_on_dut(node)
268 def show_event_logger_on_dut(node):
269 """Show event logger on the DUT node.
271 :param node: DUT node to show traces on.
274 PapiSocketExecutor.run_cli_cmd(node, "show event-logger")
277 def show_event_logger_on_all_duts(nodes):
278 """Show event logger on all DUTs in the given topology.
280 :param nodes: Nodes in the topology.
283 for node in nodes.values():
284 if node['type'] == NodeType.DUT:
285 VPPUtil.show_event_logger_on_dut(node)
289 """Show log on the specified topology node.
291 :param node: Topology node.
293 :returns: VPP log data.
296 return PapiSocketExecutor.run_cli_cmd(node, "show log")
299 def vpp_show_threads(node):
300 """Show VPP threads on node.
302 :param node: Node to run command on.
304 :returns: VPP thread data.
307 with PapiSocketExecutor(node) as papi_exec:
308 reply = papi_exec.add('show_threads').get_reply()
310 threads_data = list()
311 for thread in reply["thread_data"]:
314 if isinstance(item, unicode):
315 item = item.rstrip('\x00')
316 thread_data.append(item)
317 threads_data.append(thread_data)
319 logger.info("show threads:\n{threads}".format(threads=threads_data))