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 counters utilities library."""
18 from pprint import pformat
20 from robot.api import logger
21 from resources.libraries.python.PapiExecutor import PapiExecutor
22 from resources.libraries.python.topology import NodeType, Topology
25 class VppCounters(object):
26 """VPP counters utilities."""
29 self._stats_table = None
32 def _run_cli_cmd(node, cmd, log=True):
35 :param node: Node to run command on.
36 :param cmd: The CLI command to be run on the node.
37 :param log: If True, the response is logged.
41 :returns: Verified data from PAPI response.
46 err_msg = "Failed to run 'cli_inband {cmd}' PAPI command on host " \
47 "{host}".format(host=node['host'], cmd=cmd)
49 with PapiExecutor(node) as papi_exec:
50 data = papi_exec.add(cli, **args).get_replies(err_msg). \
51 verify_reply(err_msg=err_msg)
54 logger.info("{cmd}:\n{data}".format(cmd=cmd, data=data["reply"]))
59 def _get_non_zero_items(data):
60 """Extract and return non-zero items from the input data.
62 :param data: Data to filter.
64 :returns: Dictionary with non-zero items.
67 return {k: data[k] for k in data.keys() if sum(data[k])}
70 def vpp_show_errors(node):
71 """Run "show errors" debug CLI command.
73 :param node: Node to run command on.
76 VppCounters._run_cli_cmd(node, 'show errors')
79 def vpp_show_errors_verbose(node):
80 """Run "show errors verbose" debug CLI command.
82 :param node: Node to run command on.
85 VppCounters._run_cli_cmd(node, 'show errors verbose')
88 def vpp_show_errors_on_all_duts(nodes, verbose=False):
89 """Show errors on all DUTs.
91 :param nodes: VPP nodes.
92 :param verbose: If True show verbose output.
96 for node in nodes.values():
97 if node['type'] == NodeType.DUT:
99 VppCounters.vpp_show_errors_verbose(node)
101 VppCounters.vpp_show_errors(node)
104 def vpp_show_runtime(node, log_zeros=False):
105 """Run "show runtime" CLI command.
107 :param node: Node to run command on.
108 :param log_zeros: Log also items with zero values.
110 :type log_zeros: bool
112 args = dict(path='^/sys/node')
113 with PapiExecutor(node) as papi_exec:
114 stats = papi_exec.add("vpp-stats", **args).get_stats()[0]
116 names = stats['/sys/node/names']
122 runtime_non_zero = []
125 runtime.append({'name': name})
127 for idx, runtime_item in enumerate(runtime):
130 for thread in stats['/sys/node/calls']:
131 calls_th.append(thread[idx])
132 runtime_item["calls"] = calls_th
135 for thread in stats['/sys/node/vectors']:
136 vectors_th.append(thread[idx])
137 runtime_item["vectors"] = vectors_th
140 for thread in stats['/sys/node/suspends']:
141 suspends_th.append(thread[idx])
142 runtime_item["suspends"] = suspends_th
145 for thread in stats['/sys/node/clocks']:
146 clocks_th.append(thread[idx])
147 runtime_item["clocks"] = clocks_th
149 if (sum(calls_th) or sum(vectors_th) or
150 sum(suspends_th) or sum(clocks_th)):
151 runtime_non_zero.append(runtime_item)
154 logger.info("Runtime:\n{runtime}".format(
155 runtime=pformat(runtime)))
157 logger.info("Runtime:\n{runtime}".format(
158 runtime=pformat(runtime_non_zero)))
161 def vpp_show_runtime_verbose(node):
162 """Run "show runtime verbose" CLI command.
165 Only verbose output is possible to get using VPPStats.
167 :param node: Node to run command on.
170 VppCounters.vpp_show_runtime(node)
173 def show_runtime_counters_on_all_duts(nodes):
174 """Clear VPP runtime counters on all DUTs.
176 :param nodes: VPP nodes.
179 for node in nodes.values():
180 if node['type'] == NodeType.DUT:
181 VppCounters.vpp_show_runtime(node)
184 def vpp_show_hardware_detail(node):
185 """Run "show hardware-interfaces detail" debug CLI command.
187 :param node: Node to run command on.
190 VppCounters._run_cli_cmd(node, 'show hardware detail')
193 def vpp_clear_runtime(node):
194 """Run "clear runtime" CLI command.
196 :param node: Node to run command on.
198 :returns: Verified data from PAPI response.
201 return VppCounters._run_cli_cmd(node, 'clear runtime', log=False)
204 def clear_runtime_counters_on_all_duts(nodes):
205 """Run "clear runtime" CLI command on all DUTs.
207 :param nodes: VPP nodes.
210 for node in nodes.values():
211 if node['type'] == NodeType.DUT:
212 VppCounters.vpp_clear_runtime(node)
215 def vpp_clear_interface_counters(node):
216 """Run "clear interfaces" CLI command.
218 :param node: Node to run command on.
220 :returns: Verified data from PAPI response.
223 return VppCounters._run_cli_cmd(node, 'clear interfaces', log=False)
226 def clear_interface_counters_on_all_duts(nodes):
227 """Clear interface counters on all DUTs.
229 :param nodes: VPP nodes.
232 for node in nodes.values():
233 if node['type'] == NodeType.DUT:
234 VppCounters.vpp_clear_interface_counters(node)
237 def vpp_clear_hardware_counters(node):
238 """Run "clear hardware" CLI command.
240 :param node: Node to run command on.
242 :returns: Verified data from PAPI response.
245 return VppCounters._run_cli_cmd(node, 'clear hardware', log=False)
248 def clear_hardware_counters_on_all_duts(nodes):
249 """Clear hardware counters on all DUTs.
251 :param nodes: VPP nodes.
254 for node in nodes.values():
255 if node['type'] == NodeType.DUT:
256 VppCounters.vpp_clear_hardware_counters(node)
259 def vpp_clear_errors_counters(node):
260 """Run "clear errors" CLI command.
262 :param node: Node to run command on.
264 :returns: Verified data from PAPI response.
267 return VppCounters._run_cli_cmd(node, 'clear errors', log=False)
270 def clear_error_counters_on_all_duts(nodes):
271 """Clear VPP errors counters on all DUTs.
273 :param nodes: VPP nodes.
276 for node in nodes.values():
277 if node['type'] == NodeType.DUT:
278 VppCounters.vpp_clear_errors_counters(node)
280 def vpp_get_ipv4_interface_counter(self, node, interface):
283 :param node: Node to get interface IPv4 counter on.
284 :param interface: Interface name.
287 :returns: Interface IPv4 counter.
290 return self.vpp_get_ipv46_interface_counter(node, interface, False)
292 def vpp_get_ipv6_interface_counter(self, node, interface):
295 :param node: Node to get interface IPv6 counter on.
296 :param interface: Interface name.
299 :returns: Interface IPv6 counter.
302 return self.vpp_get_ipv46_interface_counter(node, interface, True)
304 def vpp_get_ipv46_interface_counter(self, node, interface, is_ipv6=True):
305 """Return interface IPv4/IPv6 counter.
307 :param node: Node to get interface IPv4/IPv6 counter on.
308 :param interface: Interface name.
309 :param is_ipv6: Specify IP version.
313 :returns: Interface IPv4/IPv6 counter.
316 version = 'ip6' if is_ipv6 else 'ip4'
318 if_index = topo.get_interface_sw_index(node, interface)
320 logger.trace('{i} sw_index not found.'.format(i=interface))
323 if_counters = self._stats_table.get('interface_counters')
325 logger.trace('No interface counters.')
327 for counter in if_counters:
328 if counter['vnet_counter_type'] == version:
329 data = counter['data']
330 return data[if_index]
331 logger.trace('{i} {v} counter not found.'.format(
332 i=interface, v=version))
336 def show_vpp_statistics(node):
337 """Show [error, hardware, interface] stats.
339 :param node: VPP node.
342 VppCounters.vpp_show_errors(node)
343 VppCounters.vpp_show_hardware_detail(node)
344 VppCounters.vpp_show_runtime(node)
347 def show_statistics_on_all_duts(nodes, sleeptime=5):
348 """Show VPP statistics on all DUTs.
350 :param nodes: VPP nodes.
352 :param sleeptime: Time to wait for traffic to arrive back to TG.
355 logger.trace('Waiting for statistics to be collected')
356 time.sleep(sleeptime)
357 for node in nodes.values():
358 if node['type'] == NodeType.DUT:
359 VppCounters.show_vpp_statistics(node)