dev: new device driver infra
[vpp.git] / src / vnet / dev / counters.c
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright (c) 2023 Cisco Systems, Inc.
3  */
4
5 #include <vnet/vnet.h>
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>
11
12 VLIB_REGISTER_LOG_CLASS (dev_log, static) = {
13   .class_name = "dev",
14   .subclass_name = "counters",
15 };
16
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, ...)
20 {
21   vnet_dev_counter_t *c;
22   vnet_dev_counter_main_t *cm;
23   u32 alloc_sz;
24
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;
29
30   if (fmt && strlen (fmt))
31     {
32       va_list va;
33       va_start (va, fmt);
34       cm->desc = va_format (0, fmt, &va);
35       va_end (va);
36     }
37
38   for (u32 i = 0; i < n_counters; i++)
39     {
40       cm->counters[i] = counters[i];
41       cm->counters[i].index = i;
42     }
43
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);
48
49   return cm;
50 }
51
52 void
53 vnet_dev_counters_clear (vlib_main_t *vm, vnet_dev_counter_main_t *cm)
54 {
55   for (int i = 0; i < cm->n_counters; i++)
56     {
57       cm->counter_start[i] = cm->counter_data[i];
58       cm->counter_data[i] = 0;
59     }
60 }
61
62 void
63 vnet_dev_counters_free (vlib_main_t *vm, vnet_dev_counter_main_t *cm)
64 {
65   vec_free (cm->desc);
66   vec_free (cm->counter_data);
67   vec_free (cm->counter_start);
68   clib_mem_free (cm);
69 }
70
71 u8 *
72 format_vnet_dev_counter_name (u8 *s, va_list *va)
73 {
74   vnet_dev_counter_t *c = va_arg (*va, vnet_dev_counter_t *);
75
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",
83   };
84
85   char *directions[] = {
86     [VNET_DEV_CTR_DIR_RX] = "received",
87     [VNET_DEV_CTR_DIR_TX] = "sent",
88   };
89   char *units[] = {
90     [VNET_DEV_CTR_UNIT_BYTES] = "bytes",
91     [VNET_DEV_CTR_UNIT_PACKETS] = "packets",
92   };
93
94   if (c->type == VNET_DEV_CTR_TYPE_VENDOR)
95     {
96       s = format (s, "%s", c->name);
97
98       if (c->unit < ARRAY_LEN (units) && units[c->unit])
99         s = format (s, " %s", units[c->unit]);
100
101       if (c->dir < ARRAY_LEN (directions) && directions[c->dir])
102         s = format (s, " %s", directions[c->dir]);
103     }
104   else if (c->type < ARRAY_LEN (std_counters) && std_counters[c->type])
105     s = format (s, "%s", std_counters[c->type]);
106   else
107     ASSERT (0);
108
109   return s;
110 }
111
112 u8 *
113 format_vnet_dev_counters (u8 *s, va_list *va)
114 {
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);
118
119   foreach_vnet_dev_counter (c, cm)
120     {
121       if (a->show_zero_counters == 0 && cm->counter_data[c->index] == 0)
122         continue;
123
124       if (line++)
125         s = format (s, "\n%U", format_white_space, indent);
126
127       s = format (s, "%-45U%lu", format_vnet_dev_counter_name, c,
128                   cm->counter_data[c->index]);
129     }
130
131   return s;
132 }