1 # Copyright (c) 2016 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 counters utilities library."""
18 from robot.api import logger
19 from resources.libraries.python.topology import NodeType, Topology
20 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
23 class VppCounters(object):
24 """VPP counters utilities."""
27 self._stats_table = None
30 def vpp_show_errors(node):
31 """Run "show errors" debug CLI command.
33 :param node: Node to run command on.
37 vat.execute_script("show_errors.vat", node, json_out=False)
38 vat.script_should_have_passed()
41 def vpp_show_errors_verbose(node):
42 """Run "show errors verbose" debug CLI command.
44 :param node: Node to run command on.
48 vat.execute_script("show_errors_verbose.vat", node, json_out=False)
49 vat.script_should_have_passed()
52 def vpp_show_errors_on_all_duts(nodes, verbose=False):
53 """Show errors on all DUTs.
55 :param nodes: VPP nodes.
56 :param verbose: If True show verbose output.
61 for node in nodes.values():
62 if node['type'] == NodeType.DUT:
64 VppCounters.vpp_show_errors_verbose(node)
66 VppCounters.vpp_show_errors(node)
69 def vpp_show_runtime(node):
70 """Run "show runtime" CLI command.
72 :param node: Node to run command on.
76 vat.execute_script("show_runtime.vat", node, json_out=False)
77 vat.script_should_have_passed()
80 def show_runtime_counters_on_all_duts(nodes):
81 """Clear VPP runtime counters on all DUTs.
83 :param nodes: VPP nodes.
86 for node in nodes.values():
87 if node['type'] == NodeType.DUT:
88 VppCounters.vpp_show_runtime(node)
91 def vpp_show_runtime_verbose(node):
92 """Run "show runtime verbose" CLI command.
94 :param node: Node to run command on.
98 vat.execute_script("show_runtime_verbose.vat", node, json_out=False)
99 vat.script_should_have_passed()
102 def vpp_show_hardware_detail(node):
103 """Run "show hardware-interfaces detail" debug CLI command.
105 :param node: Node to run command on.
109 vat.execute_script("show_hardware_detail.vat", node, json_out=False)
110 vat.script_should_have_passed()
113 def vpp_clear_runtime(node):
114 """Run "clear runtime" CLI command.
116 :param node: Node to run command on.
120 vat.execute_script("clear_runtime.vat", node, json_out=False)
121 vat.script_should_have_passed()
124 def clear_runtime_counters_on_all_duts(nodes):
125 """Run "clear runtime" CLI command on all DUTs.
127 :param nodes: VPP nodes.
130 for node in nodes.values():
131 if node['type'] == NodeType.DUT:
132 VppCounters.vpp_clear_runtime(node)
135 def vpp_clear_interface_counters(node):
136 """Clear interface counters on VPP node.
138 :param node: Node to clear interface counters on.
142 vat.execute_script('clear_interface.vat', node)
143 vat.script_should_have_passed()
146 def clear_interface_counters_on_all_duts(nodes):
147 """Clear interface counters on all DUTs.
149 :param nodes: VPP nodes.
152 for node in nodes.values():
153 if node['type'] == NodeType.DUT:
154 VppCounters.vpp_clear_interface_counters(node)
157 def vpp_clear_hardware_counters(node):
158 """Clear interface hardware counters on VPP node.
160 :param node: Node to clear hardware counters on.
164 vat.execute_script('clear_hardware.vat', node)
165 vat.script_should_have_passed()
168 def clear_hardware_counters_on_all_duts(nodes):
169 """Clear hardware counters on all DUTs.
171 :param nodes: VPP nodes.
174 for node in nodes.values():
175 if node['type'] == NodeType.DUT:
176 VppCounters.vpp_clear_hardware_counters(node)
179 def vpp_clear_errors_counters(node):
180 """Clear errors counters on VPP node.
182 :param node: Node to clear errors counters on.
186 vat.execute_script('clear_errors.vat', node)
187 vat.script_should_have_passed()
190 def clear_error_counters_on_all_duts(nodes):
191 """Clear VPP errors counters on all DUTs.
193 :param nodes: VPP nodes.
196 for node in nodes.values():
197 if node['type'] == NodeType.DUT:
198 VppCounters.vpp_clear_errors_counters(node)
200 def vpp_dump_stats_table(self, node):
201 """Dump stats table on VPP node.
203 :param node: Node to dump stats table on.
205 :returns: Stats table.
207 with VatTerminal(node) as vat:
208 vat.vat_terminal_exec_cmd('want_stats enable')
209 for _ in range(0, 12):
210 stats_table = vat.vat_terminal_exec_cmd('dump_stats_table')
211 if_counters = stats_table['interface_counters']
212 if len(if_counters) > 0:
213 self._stats_table = stats_table
218 def vpp_get_ipv4_interface_counter(self, node, interface):
221 :param node: Node to get interface IPv4 counter on.
222 :param interface: Interface name.
225 :returns: Interface IPv4 counter.
228 return self.vpp_get_ipv46_interface_counter(node, interface, False)
230 def vpp_get_ipv6_interface_counter(self, node, interface):
233 :param node: Node to get interface IPv6 counter on.
234 :param interface: Interface name.
237 :returns: Interface IPv6 counter.
240 return self.vpp_get_ipv46_interface_counter(node, interface, True)
242 def vpp_get_ipv46_interface_counter(self, node, interface, is_ipv6=True):
243 """Return interface IPv4/IPv6 counter.
245 :param node: Node to get interface IPv4/IPv6 counter on.
246 :param interface: Interface name.
247 :param is_ipv6: Specify IP version.
251 :returns: Interface IPv4/IPv6 counter.
254 version = 'ip6' if is_ipv6 else 'ip4'
256 if_index = topo.get_interface_sw_index(node, interface)
258 logger.trace('{i} sw_index not found.'.format(i=interface))
261 if_counters = self._stats_table.get('interface_counters')
262 if if_counters is None or len(if_counters) == 0:
263 logger.trace('No interface counters.')
265 for counter in if_counters:
266 if counter['vnet_counter_type'] == version:
267 data = counter['data']
268 return data[if_index]
269 logger.trace('{i} {v} counter not found.'.format(i=interface,
274 def show_vpp_statistics(node):
275 """Show [error, hardware, interface] stats.
277 :param node: VPP node.
280 VppCounters.vpp_show_errors(node)
281 VppCounters.vpp_show_hardware_detail(node)
282 VppCounters.vpp_show_runtime(node)
285 def show_statistics_on_all_duts(nodes, sleeptime=5):
286 """Show VPP statistics on all DUTs.
288 :param nodes: VPP nodes.
290 :param sleeptime: Time to wait for traffic to arrive back to TG.
293 logger.trace('Waiting for statistics to be collected')
294 time.sleep(sleeptime)
295 for node in nodes.values():
296 if node['type'] == NodeType.DUT:
297 VppCounters.show_vpp_statistics(node)