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.PapiExecutor import PapiExecutor
23 from resources.libraries.python.PapiErrors import PapiError
24 from resources.libraries.python.ssh import exec_cmd, exec_cmd_no_error
25 from resources.libraries.python.topology import NodeType
26 from resources.libraries.python.VatExecutor import VatExecutor
29 class VPPUtil(object):
30 """General class for any VPP related methods/functions."""
33 def show_vpp_settings(node, *additional_cmds):
34 """Print default VPP settings. In case others are needed, can be
35 accepted as next parameters (each setting one parameter), preferably
38 :param node: VPP node.
39 :param additional_cmds: Additional commands that the vpp should print
42 :type additional_cmds: tuple
44 def_setting_tb_displayed = {
45 'IPv6 FIB': 'ip6 fib',
47 'Interface IP': 'int addr',
54 for cmd in additional_cmds:
55 def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] = cmd
57 for _, cmd in def_setting_tb_displayed.items():
58 command = 'vppctl sh {cmd}'.format(cmd=cmd)
59 exec_cmd_no_error(node, command, timeout=30, sudo=True)
62 def start_vpp_service(node, retries=60):
63 """Start VPP service on the specified node.
65 :param node: VPP node.
66 :param retries: Number of times (default 60) to re-try waiting.
69 :raises RuntimeError: If VPP service fails to start.
71 DUTSetup.start_service(node, Constants.VPP_UNIT)
72 # Sleep 1 second, up to <retry> times,
73 # and verify if VPP is running.
74 for _ in range(retries):
76 command = 'vppctl show pci'
77 ret, stdout, _ = exec_cmd(node, command, timeout=30, sudo=True)
78 if not ret and 'Connection refused' not in stdout:
81 raise RuntimeError('VPP failed to start on host {name}'.
82 format(name=node['host']))
83 DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
86 def start_vpp_service_on_all_duts(nodes):
87 """Start up the VPP service on all nodes.
89 :param nodes: Nodes in the topology.
92 for node in nodes.values():
93 if node['type'] == NodeType.DUT:
94 VPPUtil.start_vpp_service(node)
97 def stop_vpp_service(node):
98 """Stop VPP service on the specified node.
100 :param node: VPP node.
102 :raises RuntimeError: If VPP service fails to stop.
104 DUTSetup.stop_service(node, Constants.VPP_UNIT)
107 def stop_vpp_service_on_all_duts(nodes):
108 """Stop VPP service on all nodes.
110 :param nodes: Nodes in the topology.
113 for node in nodes.values():
114 if node['type'] == NodeType.DUT:
115 VPPUtil.stop_vpp_service(node)
118 def verify_vpp_on_dut(node):
119 """Verify that VPP is installed on DUT node.
121 :param node: DUT node.
123 :raises RuntimeError: If failed to restart VPP, get VPP version
124 or get VPP interfaces.
126 VPPUtil.vpp_show_version_verbose(node)
127 VPPUtil.vpp_show_interfaces(node)
130 def verify_vpp_on_all_duts(nodes):
131 """Verify that VPP is installed on all DUT nodes.
133 :param nodes: Nodes in the topology.
136 for node in nodes.values():
137 if node['type'] == NodeType.DUT:
138 VPPUtil.start_vpp_service(node)
139 VPPUtil.verify_vpp_on_dut(node)
142 def vpp_show_version(node, verbose=False):
143 """Run "show_version" API command.
145 :param node: Node to run command on.
146 :param verbose: Show version, compile date and compile location if True
147 otherwise show only version.
150 :raises PapiError: If no reply received for show_version API command.
152 # TODO: move composition of api data to separate method
154 api = dict(api_name='show_version')
156 api['api_args'] = api_args
160 with PapiExecutor(node) as papi_executor:
161 papi_executor.execute_papi(api_data)
163 papi_executor.papi_should_have_passed()
164 except AssertionError:
165 raise RuntimeError('Failed to get VPP version on host: {host}'.
166 format(host=node['host']))
167 api_reply = papi_executor.get_papi_reply()
169 if api_reply is not None:
170 version_data = api_reply[0]['api_reply']['show_version_reply']
171 ver = version_data['version'].rstrip('\0x00')
173 date = version_data['build_date'].rstrip('\0x00')
174 loc = version_data['build_directory'].rstrip('\0x00')
176 'VPP Version: {ver}\n' \
177 'Compile date: {date}\n' \
178 'Compile location: {loc}\n '\
179 .format(ver=ver, date=date, loc=loc)
181 version = 'VPP version:{ver}'.format(ver=ver)
184 raise PapiError('No reply received for show_version API command on '
185 'host {host}'.format(host=node['host']))
188 def vpp_show_version_verbose(node):
189 """Run "show_version" API command and return verbose string of version
192 :param node: Node to run command on.
195 VPPUtil.vpp_show_version(node, verbose=True)
198 def show_vpp_version_on_all_duts(nodes):
199 """Show VPP version on all DUTs.
201 :param nodes: VPP nodes.
204 for node in nodes.values():
205 if node['type'] == NodeType.DUT:
206 VPPUtil.vpp_show_version_verbose(node)
209 def vpp_show_interfaces(node):
210 """Run "show interface" CLI command.
212 :param node: Node to run command on.
216 vat.execute_script("show_interface.vat", node, json_out=False)
219 vat.script_should_have_passed()
220 except AssertionError:
221 raise RuntimeError('Failed to get VPP interfaces on host: {name}'.
222 format(name=node['host']))
225 def vpp_show_crypto_device_mapping(node):
226 """Run "show crypto device mapping" CLI command.
228 :param node: Node to run command on.
232 vat.execute_script("show_crypto_device_mapping.vat", node,
236 def vpp_api_trace_dump(node):
237 """Run "api trace custom-dump" CLI command.
239 :param node: Node to run command on.
243 vat.execute_script("api_trace_dump.vat", node, json_out=False)
246 def vpp_api_trace_save(node):
247 """Run "api trace save" CLI command.
249 :param node: Node to run command on.
253 vat.execute_script("api_trace_save.vat", node, json_out=False)
256 def vpp_enable_traces_on_dut(node):
257 """Enable vpp packet traces on the DUT node.
259 :param node: DUT node to set up.
263 vat.execute_script("enable_dpdk_traces.vat", node, json_out=False)
264 vat.execute_script("enable_vhost_user_traces.vat", node, json_out=False)
265 vat.execute_script("enable_memif_traces.vat", node, json_out=False)
268 def vpp_enable_traces_on_all_duts(nodes):
269 """Enable vpp packet traces on all DUTs in the given topology.
271 :param nodes: Nodes in the topology.
274 for node in nodes.values():
275 if node['type'] == NodeType.DUT:
276 VPPUtil.vpp_enable_traces_on_dut(node)
279 def vpp_enable_elog_traces_on_dut(node):
280 """Enable API/CLI/Barrier traces on the DUT node.
282 :param node: DUT node to set up.
286 vat.execute_script("elog_trace_api_cli_barrier.vat", node,
290 def vpp_enable_elog_traces_on_all_duts(nodes):
291 """Enable API/CLI/Barrier traces on all DUTs in the given topology.
293 :param nodes: Nodes in the topology.
296 for node in nodes.values():
297 if node['type'] == NodeType.DUT:
298 VPPUtil.vpp_enable_elog_traces_on_dut(node)
301 def show_event_logger_on_dut(node):
302 """Show event logger on the DUT node.
304 :param node: DUT node to show traces on.
308 vat.execute_script("show_event_logger.vat", node, json_out=False)
311 def show_event_logger_on_all_duts(nodes):
312 """Show event logger on all DUTs in the given topology.
314 :param nodes: Nodes in the topology.
317 for node in nodes.values():
318 if node['type'] == NodeType.DUT:
319 VPPUtil.show_event_logger_on_dut(node)