1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright (c) 2023 Cisco Systems, Inc.
5 #ifndef _VNET_DEV_COUNTERS_H_
6 #define _VNET_DEV_COUNTERS_H_
8 #include <vnet/dev/dev.h>
15 } __clib_packed vnet_dev_counter_direction_t;
19 VNET_DEV_CTR_TYPE_RX_BYTES,
20 VNET_DEV_CTR_TYPE_RX_PACKETS,
21 VNET_DEV_CTR_TYPE_RX_DROPS,
22 VNET_DEV_CTR_TYPE_TX_BYTES,
23 VNET_DEV_CTR_TYPE_TX_PACKETS,
24 VNET_DEV_CTR_TYPE_TX_DROPS,
25 VNET_DEV_CTR_TYPE_VENDOR,
26 } __clib_packed vnet_dev_counter_type_t;
31 VNET_DEV_CTR_UNIT_BYTES,
32 VNET_DEV_CTR_UNIT_PACKETS,
33 } __clib_packed vnet_dev_counter_unit_t;
35 typedef struct vnet_dev_counter
39 vnet_dev_counter_type_t type;
40 vnet_dev_counter_direction_t dir;
41 vnet_dev_counter_unit_t unit;
45 typedef struct vnet_dev_counter_main
51 vnet_dev_counter_t counters[];
52 } vnet_dev_counter_main_t;
54 #define VNET_DEV_CTR_RX_BYTES(p, ...) \
56 .type = VNET_DEV_CTR_TYPE_RX_BYTES, .dir = VNET_DEV_CTR_DIR_RX, \
57 .unit = VNET_DEV_CTR_UNIT_BYTES, .user_data = (p), __VA_ARGS__ \
59 #define VNET_DEV_CTR_TX_BYTES(p, ...) \
61 .type = VNET_DEV_CTR_TYPE_TX_BYTES, .dir = VNET_DEV_CTR_DIR_TX, \
62 .unit = VNET_DEV_CTR_UNIT_BYTES, .user_data = (p), __VA_ARGS__ \
64 #define VNET_DEV_CTR_RX_PACKETS(p, ...) \
66 .type = VNET_DEV_CTR_TYPE_RX_PACKETS, .dir = VNET_DEV_CTR_DIR_RX, \
67 .unit = VNET_DEV_CTR_UNIT_PACKETS, .user_data = (p), __VA_ARGS__ \
69 #define VNET_DEV_CTR_TX_PACKETS(p, ...) \
71 .type = VNET_DEV_CTR_TYPE_TX_PACKETS, .dir = VNET_DEV_CTR_DIR_TX, \
72 .unit = VNET_DEV_CTR_UNIT_PACKETS, .user_data = (p), __VA_ARGS__ \
74 #define VNET_DEV_CTR_RX_DROPS(p, ...) \
76 .type = VNET_DEV_CTR_TYPE_RX_DROPS, .dir = VNET_DEV_CTR_DIR_RX, \
77 .unit = VNET_DEV_CTR_UNIT_PACKETS, .user_data = (p), __VA_ARGS__ \
79 #define VNET_DEV_CTR_TX_DROPS(p, ...) \
81 .type = VNET_DEV_CTR_TYPE_TX_DROPS, .dir = VNET_DEV_CTR_DIR_TX, \
82 .unit = VNET_DEV_CTR_UNIT_PACKETS, .user_data = (p), __VA_ARGS__ \
84 #define VNET_DEV_CTR_VENDOR(p, d, u, n, ...) \
86 .type = VNET_DEV_CTR_TYPE_VENDOR, .user_data = (p), .name = n, \
87 .dir = VNET_DEV_CTR_DIR_##d, .unit = VNET_DEV_CTR_UNIT_##u, __VA_ARGS__ \
90 vnet_dev_counter_main_t *vnet_dev_counters_alloc (vlib_main_t *,
91 vnet_dev_counter_t *, u16,
93 void vnet_dev_counters_clear (vlib_main_t *, vnet_dev_counter_main_t *);
94 void vnet_dev_counters_free (vlib_main_t *, vnet_dev_counter_main_t *);
96 format_function_t format_vnet_dev_counters;
97 format_function_t format_vnet_dev_counters_all;
99 static_always_inline vnet_dev_counter_main_t *
100 vnet_dev_counter_get_main (vnet_dev_counter_t *counter)
102 return (vnet_dev_counter_main_t *) ((u8 *) (counter - counter->index) -
104 vnet_dev_counter_main_t, counters));
107 static_always_inline void
108 vnet_dev_counter_value_add (vlib_main_t *vm, vnet_dev_counter_t *counter,
111 vnet_dev_counter_main_t *cm = vnet_dev_counter_get_main (counter);
112 cm->counter_data[counter->index] += val;
115 static_always_inline void
116 vnet_dev_counter_value_update (vlib_main_t *vm, vnet_dev_counter_t *counter,
119 vnet_dev_counter_main_t *cm = vnet_dev_counter_get_main (counter);
120 cm->counter_data[counter->index] = val - cm->counter_start[counter->index];
123 #define foreach_vnet_dev_counter(c, cm) \
125 for (typeof (*(cm)->counters) *(c) = (cm)->counters; \
126 (c) < (cm)->counters + (cm)->n_counters; (c)++)
128 #endif /* _VNET_DEV_COUNTERS_H_ */