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."""
17 from topology import NodeType, Topology
18 from VatExecutor import VatExecutor, VatTerminal
19 from robot.api import logger
22 class VppCounters(object):
23 """VPP counters utilities."""
26 self._stats_table = None
28 def vpp_nodes_clear_interface_counters(self, nodes):
29 """Clear interface counters on all VPP nodes in topology.
31 :param nodes: Nodes in topology.
34 for node in nodes.values():
35 if node['type'] == NodeType.DUT:
36 self.vpp_clear_interface_counters(node)
39 def vpp_clear_interface_counters(node):
40 """Clear interface counters on VPP node.
42 :param node: Node to clear interface counters on.
46 vat.execute_script('clear_interface.vat', node)
47 vat.script_should_have_passed()
49 def vpp_dump_stats_table(self, node):
50 """Dump stats table on VPP node.
52 :param node: Node to dump stats table on.
56 vat = VatTerminal(node)
57 vat.vat_terminal_exec_cmd('want_stats enable')
58 for _ in range(0, 12):
59 stats_table = vat.vat_terminal_exec_cmd('dump_stats_table')
60 if_counters = stats_table['interface_counters']
61 if len(if_counters) > 0:
62 self._stats_table = stats_table
63 vat.vat_terminal_close()
67 vat.vat_terminal_close()
70 def vpp_get_ipv4_interface_counter(self, node, interface):
71 return self.vpp_get_ipv46_interface_counter(node, interface, False)
73 def vpp_get_ipv6_interface_counter(self, node, interface):
74 return self.vpp_get_ipv46_interface_counter(node, interface, True)
76 def vpp_get_ipv46_interface_counter(self, node, interface, is_ipv6=True):
77 """Return interface IPv4/IPv6 counter
79 :param node: Node to get interface IPv4/IPv6 counter on.
80 :param interface: Interface name.
83 :return: Interface IPv4/IPv6 counter.
84 :param is_ipv6: specify IP version
88 version = 'ip6' if is_ipv6 else 'ip4'
90 if_index = topo.get_interface_sw_index(node, interface)
92 logger.trace('{i} sw_index not found.'.format(i=interface))
95 if_counters = self._stats_table.get('interface_counters')
96 if if_counters is None or len(if_counters) == 0:
97 logger.trace('No interface counters.')
99 for counter in if_counters:
100 if counter['vnet_counter_type'] == version:
101 data = counter['data']
102 return data[if_index]
103 logger.trace('{i} {v} counter not found.'.format(i=interface,