1 # Copyright (c) 2018 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 resources.libraries.python.constants import Constants
19 from resources.libraries.python.DUTSetup import DUTSetup
20 from resources.libraries.python.ssh import exec_cmd, exec_cmd_no_error
21 from resources.libraries.python.topology import NodeType
22 from resources.libraries.python.VatExecutor import VatExecutor
25 class VPPUtil(object):
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 'IPv6 FIB': 'ip6 fib',
43 'Interface IP': 'int addr',
50 for cmd in additional_cmds:
51 def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] = cmd
53 for _, cmd in def_setting_tb_displayed.items():
54 command = 'vppctl sh {cmd}'.format(cmd=cmd)
55 exec_cmd_no_error(node, command, timeout=30, sudo=True)
58 def start_vpp_service(node, retries=60):
59 """Start VPP service on the specified node.
61 :param node: VPP node.
62 :param retries: Number of times (default 60) to re-try waiting.
65 :raises RuntimeError: If VPP service fails to start.
67 DUTSetup.start_service(node, Constants.VPP_UNIT)
68 # Sleep 1 second, up to <retry> times,
69 # and verify if VPP is running.
70 for _ in range(retries):
72 command = 'vppctl show pci'
73 ret, stdout, _ = exec_cmd(node, command, timeout=30, sudo=True)
74 if not ret and 'Connection refused' not in stdout:
77 raise RuntimeError('VPP failed to start on host {name}'.
78 format(name=node['host']))
79 DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
82 def start_vpp_service_on_all_duts(nodes):
83 """Start up the VPP service on all nodes.
85 :param nodes: Nodes in the topology.
88 for node in nodes.values():
89 if node['type'] == NodeType.DUT:
90 VPPUtil.start_vpp_service(node)
93 def stop_vpp_service(node):
94 """Stop VPP service on the specified node.
96 :param node: VPP node.
98 :raises RuntimeError: If VPP service fails to stop.
100 DUTSetup.stop_service(node, Constants.VPP_UNIT)
103 def stop_vpp_service_on_all_duts(nodes):
104 """Stop VPP service on all nodes.
106 :param nodes: Nodes in the topology.
109 for node in nodes.values():
110 if node['type'] == NodeType.DUT:
111 VPPUtil.stop_vpp_service(node)
114 def verify_vpp_on_dut(node):
115 """Verify that VPP is installed on DUT node.
117 :param node: DUT node.
119 :raises RuntimeError: If failed to restart VPP, get VPP version
120 or get VPP interfaces.
122 VPPUtil.vpp_show_version_verbose(node)
123 VPPUtil.vpp_show_interfaces(node)
126 def verify_vpp_on_all_duts(nodes):
127 """Verify that VPP is installed on all DUT nodes.
129 :param nodes: Nodes in the topology.
132 for node in nodes.values():
133 if node['type'] == NodeType.DUT:
134 VPPUtil.start_vpp_service(node)
135 VPPUtil.verify_vpp_on_dut(node)
138 def vpp_show_version_verbose(node):
139 """Run "show version verbose" CLI command.
141 :param node: Node to run command on.
145 vat.execute_script("show_version_verbose.vat", node, json_out=False)
148 vat.script_should_have_passed()
149 except AssertionError:
150 raise RuntimeError('Failed to get VPP version on host: {name}'.
151 format(name=node['host']))
154 def show_vpp_version_on_all_duts(nodes):
155 """Show VPP version verbose on all DUTs.
157 :param nodes: VPP nodes.
160 for node in nodes.values():
161 if node['type'] == NodeType.DUT:
162 VPPUtil.vpp_show_version_verbose(node)
165 def vpp_show_interfaces(node):
166 """Run "show interface" CLI command.
168 :param node: Node to run command on.
172 vat.execute_script("show_interface.vat", node, json_out=False)
175 vat.script_should_have_passed()
176 except AssertionError:
177 raise RuntimeError('Failed to get VPP interfaces on host: {name}'.
178 format(name=node['host']))
181 def vpp_show_crypto_device_mapping(node):
182 """Run "show crypto device mapping" CLI command.
184 :param node: Node to run command on.
188 vat.execute_script("show_crypto_device_mapping.vat", node,
192 def vpp_api_trace_dump(node):
193 """Run "api trace custom-dump" CLI command.
195 :param node: Node to run command on.
199 vat.execute_script("api_trace_dump.vat", node, json_out=False)
202 def vpp_api_trace_save(node):
203 """Run "api trace save" CLI command.
205 :param node: Node to run command on.
209 vat.execute_script("api_trace_save.vat", node, json_out=False)
212 def vpp_enable_traces_on_dut(node):
213 """Enable vpp packet traces on the DUT node.
215 :param node: DUT node to set up.
219 vat.execute_script("enable_dpdk_traces.vat", node, json_out=False)
220 vat.execute_script("enable_vhost_user_traces.vat", node, json_out=False)
221 vat.execute_script("enable_memif_traces.vat", node, json_out=False)
224 def vpp_enable_traces_on_all_duts(nodes):
225 """Enable vpp packet traces on all DUTs in the given topology.
227 :param nodes: Nodes in the topology.
230 for node in nodes.values():
231 if node['type'] == NodeType.DUT:
232 VPPUtil.vpp_enable_traces_on_dut(node)
235 def vpp_enable_elog_traces_on_dut(node):
236 """Enable API/CLI/Barrier traces on the DUT node.
238 :param node: DUT node to set up.
242 vat.execute_script("elog_trace_api_cli_barrier.vat", node,
246 def vpp_enable_elog_traces_on_all_duts(nodes):
247 """Enable API/CLI/Barrier traces on all DUTs in the given topology.
249 :param nodes: Nodes in the topology.
252 for node in nodes.values():
253 if node['type'] == NodeType.DUT:
254 VPPUtil.vpp_enable_elog_traces_on_dut(node)
257 def show_event_logger_on_dut(node):
258 """Show event logger on the DUT node.
260 :param node: DUT node to show traces on.
264 vat.execute_script("show_event_logger.vat", node, json_out=False)
267 def show_event_logger_on_all_duts(nodes):
268 """Show event logger on all DUTs in the given topology.
270 :param nodes: Nodes in the topology.
273 for node in nodes.values():
274 if node['type'] == NodeType.DUT:
275 VPPUtil.show_event_logger_on_dut(node)