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.PapiExecutor import PapiSocketExecutor
23 from resources.libraries.python.topology import NodeType, Topology
26 class VppCounters(object):
27 """VPP counters utilities."""
30 self._stats_table = None
33 def _get_non_zero_items(data):
34 """Extract and return non-zero items from the input data.
36 :param data: Data to filter.
38 :returns: Dictionary with non-zero items.
41 return {k: data[k] for k in data.keys() if sum(data[k])}
44 def vpp_show_errors(node):
45 """Run "show errors" debug CLI command.
47 :param node: Node to run command on.
50 PapiSocketExecutor.run_cli_cmd(node, 'show errors')
53 def vpp_show_errors_verbose(node):
54 """Run "show errors verbose" debug CLI command.
56 :param node: Node to run command on.
59 PapiSocketExecutor.run_cli_cmd(node, 'show errors verbose')
62 def vpp_show_errors_on_all_duts(nodes, verbose=False):
63 """Show errors on all DUTs.
65 :param nodes: VPP nodes.
66 :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, log_zeros=False):
79 """Run "show runtime" CLI command.
81 :param node: Node to run command on.
82 :param log_zeros: Log also items with zero values.
86 args = dict(path='^/sys/node')
87 with PapiExecutor(node) as papi_exec:
88 stats = papi_exec.add("vpp-stats", **args).get_stats()[0]
89 # TODO: Introduce get_stat?
91 names = stats['/sys/node/names']
100 runtime.append({'name': name})
102 for idx, runtime_item in enumerate(runtime):
105 for thread in stats['/sys/node/calls']:
106 calls_th.append(thread[idx])
107 runtime_item["calls"] = calls_th
110 for thread in stats['/sys/node/vectors']:
111 vectors_th.append(thread[idx])
112 runtime_item["vectors"] = vectors_th
115 for thread in stats['/sys/node/suspends']:
116 suspends_th.append(thread[idx])
117 runtime_item["suspends"] = suspends_th
120 for thread in stats['/sys/node/clocks']:
121 clocks_th.append(thread[idx])
122 runtime_item["clocks"] = clocks_th
124 if (sum(calls_th) or sum(vectors_th) or
125 sum(suspends_th) or sum(clocks_th)):
126 runtime_non_zero.append(runtime_item)
129 logger.info("Runtime:\n{runtime}".format(
130 runtime=pformat(runtime)))
132 logger.info("Runtime:\n{runtime}".format(
133 runtime=pformat(runtime_non_zero)))
136 def vpp_show_runtime_verbose(node):
137 """Run "show runtime verbose" CLI command.
140 Only verbose output is possible to get using VPPStats.
142 :param node: Node to run command on.
145 VppCounters.vpp_show_runtime(node)
148 def show_runtime_counters_on_all_duts(nodes):
149 """Clear VPP runtime counters on all DUTs.
151 :param nodes: VPP nodes.
154 for node in nodes.values():
155 if node['type'] == NodeType.DUT:
156 VppCounters.vpp_show_runtime(node)
159 def vpp_show_hardware_verbose(node):
160 """Run "show hardware-interfaces verbose" debug CLI command.
162 :param node: Node to run command on.
165 PapiSocketExecutor.run_cli_cmd(node, 'show hardware verbose')
168 def vpp_show_memory(node):
169 """Run "show memory" debug CLI command.
171 Currently, every flag is hardcoded, giving the longest output.
173 :param node: Node to run command on.
176 PapiSocketExecutor.run_cli_cmd(
177 node, 'show memory verbose api-segment stats-segment main-heap')
180 def vpp_clear_runtime(node):
181 """Run "clear runtime" CLI command.
183 :param node: Node to run command on.
185 :returns: Verified data from PAPI response.
188 return PapiSocketExecutor.run_cli_cmd(node, 'clear runtime', log=False)
191 def clear_runtime_counters_on_all_duts(nodes):
192 """Run "clear runtime" CLI command on all DUTs.
194 :param nodes: VPP nodes.
197 for node in nodes.values():
198 if node['type'] == NodeType.DUT:
199 VppCounters.vpp_clear_runtime(node)
202 def vpp_clear_interface_counters(node):
203 """Run "clear interfaces" CLI command.
205 :param node: Node to run command on.
207 :returns: Verified data from PAPI response.
210 return PapiSocketExecutor.run_cli_cmd(
211 node, 'clear interfaces', log=False)
214 def clear_interface_counters_on_all_duts(nodes):
215 """Clear interface counters on all DUTs.
217 :param nodes: VPP nodes.
220 for node in nodes.values():
221 if node['type'] == NodeType.DUT:
222 VppCounters.vpp_clear_interface_counters(node)
225 def vpp_clear_hardware_counters(node):
226 """Run "clear hardware" CLI command.
228 :param node: Node to run command on.
230 :returns: Verified data from PAPI response.
233 return PapiSocketExecutor.run_cli_cmd(node, 'clear hardware', log=False)
236 def clear_hardware_counters_on_all_duts(nodes):
237 """Clear hardware counters on all DUTs.
239 :param nodes: VPP nodes.
242 for node in nodes.values():
243 if node['type'] == NodeType.DUT:
244 VppCounters.vpp_clear_hardware_counters(node)
247 def vpp_clear_errors_counters(node):
248 """Run "clear errors" CLI command.
250 :param node: Node to run command on.
252 :returns: Verified data from PAPI response.
255 return PapiSocketExecutor.run_cli_cmd(node, 'clear errors', log=False)
258 def clear_error_counters_on_all_duts(nodes):
259 """Clear VPP errors counters on all DUTs.
261 :param nodes: VPP nodes.
264 for node in nodes.values():
265 if node['type'] == NodeType.DUT:
266 VppCounters.vpp_clear_errors_counters(node)
268 def vpp_get_ipv4_interface_counter(self, node, interface):
271 :param node: Node to get interface IPv4 counter on.
272 :param interface: Interface name.
275 :returns: Interface IPv4 counter.
278 return self.vpp_get_ipv46_interface_counter(node, interface, False)
280 def vpp_get_ipv6_interface_counter(self, node, interface):
283 :param node: Node to get interface IPv6 counter on.
284 :param interface: Interface name.
287 :returns: Interface IPv6 counter.
290 return self.vpp_get_ipv46_interface_counter(node, interface, True)
292 def vpp_get_ipv46_interface_counter(self, node, interface, is_ipv6=True):
293 """Return interface IPv4/IPv6 counter.
295 :param node: Node to get interface IPv4/IPv6 counter on.
296 :param interface: Interface name.
297 :param is_ipv6: Specify IP version.
301 :returns: Interface IPv4/IPv6 counter.
304 version = 'ip6' if is_ipv6 else 'ip4'
306 sw_if_index = topo.get_interface_sw_index(node, interface)
307 if sw_if_index is None:
308 logger.trace('{i} sw_if_index not found.'.format(i=interface))
311 if_counters = self._stats_table.get('interface_counters')
313 logger.trace('No interface counters.')
315 for counter in if_counters:
316 if counter['vnet_counter_type'] == version:
317 data = counter['data']
318 return data[sw_if_index]
319 logger.trace('{i} {v} counter not found.'.format(
320 i=interface, v=version))
324 def show_vpp_statistics(node):
325 """Show [error, hardware, interface] stats.
327 :param node: VPP node.
330 VppCounters.vpp_show_errors(node)
331 VppCounters.vpp_show_hardware_verbose(node)
332 VppCounters.vpp_show_runtime(node)
333 VppCounters.vpp_show_memory(node)
336 def show_statistics_on_all_duts(nodes, sleeptime=5):
337 """Show VPP statistics on all DUTs.
339 :param nodes: VPP nodes.
341 :param sleeptime: Time to wait for traffic to arrive back to TG.
344 logger.trace('Waiting for statistics to be collected')
345 time.sleep(sleeptime)
346 for node in nodes.values():
347 if node['type'] == NodeType.DUT:
348 VppCounters.show_vpp_statistics(node)