1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright (c) 2023 Cisco Systems, Inc.
6 #include <vnet/ethernet/ethernet.h>
7 #include <vnet/dev/dev.h>
8 #include <vnet/dev/counters.h>
9 #include <vnet/dev/log.h>
10 #include <vnet/interface/rx_queue_funcs.h>
12 VLIB_REGISTER_LOG_CLASS (dev_log, static) = {
14 .subclass_name = "counters",
17 vnet_dev_counter_main_t *
18 vnet_dev_counters_alloc (vlib_main_t *vm, vnet_dev_counter_t *counters,
19 u16 n_counters, char *fmt, ...)
21 vnet_dev_counter_t *c;
22 vnet_dev_counter_main_t *cm;
25 alloc_sz = sizeof (*cm) + n_counters * sizeof (*c);
26 cm = clib_mem_alloc_aligned (alloc_sz, CLIB_CACHE_LINE_BYTES);
27 clib_memset (cm, 0, sizeof (*cm));
28 cm->n_counters = n_counters;
30 if (fmt && strlen (fmt))
34 cm->desc = va_format (0, fmt, &va);
38 for (u32 i = 0; i < n_counters; i++)
40 cm->counters[i] = counters[i];
41 cm->counters[i].index = i;
44 vec_validate_aligned (cm->counter_data, n_counters - 1,
45 CLIB_CACHE_LINE_BYTES);
46 vec_validate_aligned (cm->counter_start, n_counters - 1,
47 CLIB_CACHE_LINE_BYTES);
53 vnet_dev_counters_clear (vlib_main_t *vm, vnet_dev_counter_main_t *cm)
55 for (int i = 0; i < cm->n_counters; i++)
57 cm->counter_start[i] = cm->counter_data[i];
58 cm->counter_data[i] = 0;
63 vnet_dev_counters_free (vlib_main_t *vm, vnet_dev_counter_main_t *cm)
66 vec_free (cm->counter_data);
67 vec_free (cm->counter_start);
72 format_vnet_dev_counter_name (u8 *s, va_list *va)
74 vnet_dev_counter_t *c = va_arg (*va, vnet_dev_counter_t *);
76 char *std_counters[] = {
77 [VNET_DEV_CTR_TYPE_RX_BYTES] = "total bytes received",
78 [VNET_DEV_CTR_TYPE_TX_BYTES] = "total bytes transmitted",
79 [VNET_DEV_CTR_TYPE_RX_PACKETS] = "total packets received",
80 [VNET_DEV_CTR_TYPE_TX_PACKETS] = "total packets transmitted",
81 [VNET_DEV_CTR_TYPE_RX_DROPS] = "total drops received",
82 [VNET_DEV_CTR_TYPE_TX_DROPS] = "total drops transmitted",
85 char *directions[] = {
86 [VNET_DEV_CTR_DIR_RX] = "received",
87 [VNET_DEV_CTR_DIR_TX] = "sent",
90 [VNET_DEV_CTR_UNIT_BYTES] = "bytes",
91 [VNET_DEV_CTR_UNIT_PACKETS] = "packets",
94 if (c->type == VNET_DEV_CTR_TYPE_VENDOR)
96 s = format (s, "%s", c->name);
98 if (c->unit < ARRAY_LEN (units) && units[c->unit])
99 s = format (s, " %s", units[c->unit]);
101 if (c->dir < ARRAY_LEN (directions) && directions[c->dir])
102 s = format (s, " %s", directions[c->dir]);
104 else if (c->type < ARRAY_LEN (std_counters) && std_counters[c->type])
105 s = format (s, "%s", std_counters[c->type]);
113 format_vnet_dev_counters (u8 *s, va_list *va)
115 vnet_dev_format_args_t *a = va_arg (*va, vnet_dev_format_args_t *);
116 vnet_dev_counter_main_t *cm = va_arg (*va, vnet_dev_counter_main_t *);
117 u32 line = 0, indent = format_get_indent (s);
119 foreach_vnet_dev_counter (c, cm)
121 if (a->show_zero_counters == 0 && cm->counter_data[c->index] == 0)
125 s = format (s, "\n%U", format_white_space, indent);
127 s = format (s, "%-45U%lu", format_vnet_dev_counter_name, c,
128 cm->counter_data[c->index]);