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 _get_non_zero_items(data):
33 """Extract and return non-zero items from the input data.
35 :param data: Data to filter.
37 :returns: Dictionary with non-zero items.
40 return {k: data[k] for k in data.keys() if sum(data[k])}
43 def vpp_show_errors(node):
44 """Run "show errors" debug CLI command.
46 :param node: Node to run command on.
49 PapiExecutor.run_cli_cmd(node, 'show errors')
52 def vpp_show_errors_verbose(node):
53 """Run "show errors verbose" debug CLI command.
55 :param node: Node to run command on.
58 PapiExecutor.run_cli_cmd(node, 'show errors verbose')
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.
69 for node in nodes.values():
70 if node['type'] == NodeType.DUT:
72 VppCounters.vpp_show_errors_verbose(node)
74 VppCounters.vpp_show_errors(node)
77 def vpp_show_runtime(node, log_zeros=False):
78 """Run "show runtime" CLI command.
80 :param node: Node to run command on.
81 :param log_zeros: Log also items with zero values.
85 args = dict(path='^/sys/node')
86 with PapiExecutor(node) as papi_exec:
87 stats = papi_exec.add("vpp-stats", **args).get_stats()[0]
88 # TODO: Introduce get_stat?
90 names = stats['/sys/node/names']
99 runtime.append({'name': name})
101 for idx, runtime_item in enumerate(runtime):
104 for thread in stats['/sys/node/calls']:
105 calls_th.append(thread[idx])
106 runtime_item["calls"] = calls_th
109 for thread in stats['/sys/node/vectors']:
110 vectors_th.append(thread[idx])
111 runtime_item["vectors"] = vectors_th
114 for thread in stats['/sys/node/suspends']:
115 suspends_th.append(thread[idx])
116 runtime_item["suspends"] = suspends_th
119 for thread in stats['/sys/node/clocks']:
120 clocks_th.append(thread[idx])
121 runtime_item["clocks"] = clocks_th
123 if (sum(calls_th) or sum(vectors_th) or
124 sum(suspends_th) or sum(clocks_th)):
125 runtime_non_zero.append(runtime_item)
128 logger.info("Runtime:\n{runtime}".format(
129 runtime=pformat(runtime)))
131 logger.info("Runtime:\n{runtime}".format(
132 runtime=pformat(runtime_non_zero)))
135 def vpp_show_runtime_verbose(node):
136 """Run "show runtime verbose" CLI command.
139 Only verbose output is possible to get using VPPStats.
141 :param node: Node to run command on.
144 VppCounters.vpp_show_runtime(node)
147 def show_runtime_counters_on_all_duts(nodes):
148 """Clear VPP runtime counters on all DUTs.
150 :param nodes: VPP nodes.
153 for node in nodes.values():
154 if node['type'] == NodeType.DUT:
155 VppCounters.vpp_show_runtime(node)
158 def vpp_show_hardware_detail(node):
159 """Run "show hardware-interfaces detail" debug CLI command.
161 :param node: Node to run command on.
164 PapiExecutor.run_cli_cmd(node, 'show hardware detail')
167 def vpp_clear_runtime(node):
168 """Run "clear runtime" CLI command.
170 :param node: Node to run command on.
172 :returns: Verified data from PAPI response.
175 return PapiExecutor.run_cli_cmd(node, 'clear runtime', log=False)
178 def clear_runtime_counters_on_all_duts(nodes):
179 """Run "clear runtime" CLI command on all DUTs.
181 :param nodes: VPP nodes.
184 for node in nodes.values():
185 if node['type'] == NodeType.DUT:
186 VppCounters.vpp_clear_runtime(node)
189 def vpp_clear_interface_counters(node):
190 """Run "clear interfaces" CLI command.
192 :param node: Node to run command on.
194 :returns: Verified data from PAPI response.
197 return PapiExecutor.run_cli_cmd(node, 'clear interfaces', log=False)
200 def clear_interface_counters_on_all_duts(nodes):
201 """Clear interface counters on all DUTs.
203 :param nodes: VPP nodes.
206 for node in nodes.values():
207 if node['type'] == NodeType.DUT:
208 VppCounters.vpp_clear_interface_counters(node)
211 def vpp_clear_hardware_counters(node):
212 """Run "clear hardware" CLI command.
214 :param node: Node to run command on.
216 :returns: Verified data from PAPI response.
219 return PapiExecutor.run_cli_cmd(node, 'clear hardware', log=False)
222 def clear_hardware_counters_on_all_duts(nodes):
223 """Clear hardware counters on all DUTs.
225 :param nodes: VPP nodes.
228 for node in nodes.values():
229 if node['type'] == NodeType.DUT:
230 VppCounters.vpp_clear_hardware_counters(node)
233 def vpp_clear_errors_counters(node):
234 """Run "clear errors" CLI command.
236 :param node: Node to run command on.
238 :returns: Verified data from PAPI response.
241 return PapiExecutor.run_cli_cmd(node, 'clear errors', log=False)
244 def clear_error_counters_on_all_duts(nodes):
245 """Clear VPP errors counters on all DUTs.
247 :param nodes: VPP nodes.
250 for node in nodes.values():
251 if node['type'] == NodeType.DUT:
252 VppCounters.vpp_clear_errors_counters(node)
254 def vpp_get_ipv4_interface_counter(self, node, interface):
257 :param node: Node to get interface IPv4 counter on.
258 :param interface: Interface name.
261 :returns: Interface IPv4 counter.
264 return self.vpp_get_ipv46_interface_counter(node, interface, False)
266 def vpp_get_ipv6_interface_counter(self, node, interface):
269 :param node: Node to get interface IPv6 counter on.
270 :param interface: Interface name.
273 :returns: Interface IPv6 counter.
276 return self.vpp_get_ipv46_interface_counter(node, interface, True)
278 def vpp_get_ipv46_interface_counter(self, node, interface, is_ipv6=True):
279 """Return interface IPv4/IPv6 counter.
281 :param node: Node to get interface IPv4/IPv6 counter on.
282 :param interface: Interface name.
283 :param is_ipv6: Specify IP version.
287 :returns: Interface IPv4/IPv6 counter.
290 version = 'ip6' if is_ipv6 else 'ip4'
292 sw_if_index = topo.get_interface_sw_index(node, interface)
293 if sw_if_index is None:
294 logger.trace('{i} sw_if_index not found.'.format(i=interface))
297 if_counters = self._stats_table.get('interface_counters')
299 logger.trace('No interface counters.')
301 for counter in if_counters:
302 if counter['vnet_counter_type'] == version:
303 data = counter['data']
304 return data[sw_if_index]
305 logger.trace('{i} {v} counter not found.'.format(
306 i=interface, v=version))
310 def show_vpp_statistics(node):
311 """Show [error, hardware, interface] stats.
313 :param node: VPP node.
316 VppCounters.vpp_show_errors(node)
317 VppCounters.vpp_show_hardware_detail(node)
318 VppCounters.vpp_show_runtime(node)
321 def show_statistics_on_all_duts(nodes, sleeptime=5):
322 """Show VPP statistics on all DUTs.
324 :param nodes: VPP nodes.
326 :param sleeptime: Time to wait for traffic to arrive back to TG.
329 logger.trace('Waiting for statistics to be collected')
330 time.sleep(sleeptime)
331 for node in nodes.values():
332 if node['type'] == NodeType.DUT:
333 VppCounters.show_vpp_statistics(node)