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
20 from resources.libraries.python.topology import NodeType, Topology
21 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
24 class VppCounters(object):
25 """VPP counters utilities."""
28 self._stats_table = None
30 def vpp_nodes_clear_interface_counters(self, nodes):
31 """Clear interface counters on all VPP nodes in topology.
33 :param nodes: Nodes in topology.
36 for node in nodes.values():
37 if node['type'] == NodeType.DUT:
38 self.vpp_clear_interface_counters(node)
41 def vpp_show_errors(node):
42 """Run "show errors" debug CLI command.
44 :param node: Node to run command on.
48 vat.execute_script("show_errors.vat", node, json_out=False)
51 def vpp_show_errors_verbose(node):
52 """Run "show errors verbose" debug CLI command.
54 :param node: Node to run command on.
58 vat.execute_script("show_errors_verbose.vat", node, json_out=False)
61 def vpp_show_errors_on_all_duts(nodes, verbose=False):
62 """Show errors on all DUTs.
64 :param nodes: VPP nodes
65 :param verbose: If True show verbose output.
70 for node in nodes.values():
71 if node['type'] == NodeType.DUT:
73 VppCounters.vpp_show_errors_verbose(node)
75 VppCounters.vpp_show_errors(node)
78 def vpp_show_runtime(node):
79 """Run "show runtime" CLI command.
81 :param node: Node to run command on.
85 vat.execute_script("show_runtime.vat", node, json_out=False)
88 def vpp_show_runtime_verbose(node):
89 """Run "show runtime verbose" CLI command.
91 :param node: Node to run command on.
95 vat.execute_script("show_runtime_verbose.vat", node, json_out=False)
98 def vpp_show_hardware_detail(node):
99 """Run "show hardware-interfaces detail" debug CLI command.
101 :param node: Node to run command on.
105 vat.execute_script("show_hardware_detail.vat", node, json_out=False)
108 def vpp_clear_runtime(node):
109 """Run "clear runtime" CLI command.
111 :param node: Node to run command on.
115 vat.execute_script("clear_runtime.vat", node, json_out=False)
118 def vpp_clear_interface_counters(node):
119 """Clear interface counters on VPP node.
121 :param node: Node to clear interface counters on.
125 vat.execute_script('clear_interface.vat', node)
126 vat.script_should_have_passed()
129 def vpp_clear_hardware_counters(node):
130 """Clear interface hardware counters on VPP node.
132 :param node: Node to clear hardware counters on.
136 vat.execute_script('clear_hardware.vat', node)
137 vat.script_should_have_passed()
140 def vpp_clear_errors_counters(node):
141 """Clear errors counters on VPP node.
143 :param node: Node to clear errors counters on.
147 vat.execute_script('clear_errors.vat', node)
148 vat.script_should_have_passed()
150 def vpp_dump_stats_table(self, node):
151 """Dump stats table on VPP node.
153 :param node: Node to dump stats table on.
155 :returns: Stats table.
157 with VatTerminal(node) as vat:
158 vat.vat_terminal_exec_cmd('want_stats enable')
159 for _ in range(0, 12):
160 stats_table = vat.vat_terminal_exec_cmd('dump_stats_table')
161 if_counters = stats_table['interface_counters']
162 if len(if_counters) > 0:
163 self._stats_table = stats_table
168 def vpp_get_ipv4_interface_counter(self, node, interface):
171 :param node: Node to get interface IPv4 counter on.
172 :param interface: Interface name.
175 :returns: Interface IPv4 counter.
178 return self.vpp_get_ipv46_interface_counter(node, interface, False)
180 def vpp_get_ipv6_interface_counter(self, node, interface):
183 :param node: Node to get interface IPv6 counter on.
184 :param interface: Interface name.
187 :returns: Interface IPv6 counter.
190 return self.vpp_get_ipv46_interface_counter(node, interface, True)
192 def vpp_get_ipv46_interface_counter(self, node, interface, is_ipv6=True):
193 """Return interface IPv4/IPv6 counter.
195 :param node: Node to get interface IPv4/IPv6 counter on.
196 :param interface: Interface name.
197 :param is_ipv6: Specify IP version.
201 :returns: Interface IPv4/IPv6 counter.
204 version = 'ip6' if is_ipv6 else 'ip4'
206 if_index = topo.get_interface_sw_index(node, interface)
208 logger.trace('{i} sw_index not found.'.format(i=interface))
211 if_counters = self._stats_table.get('interface_counters')
212 if if_counters is None or len(if_counters) == 0:
213 logger.trace('No interface counters.')
215 for counter in if_counters:
216 if counter['vnet_counter_type'] == version:
217 data = counter['data']
218 return data[if_index]
219 logger.trace('{i} {v} counter not found.'.format(i=interface,