1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2022 Cisco Systems, Inc.
6 #include <vlib/unix/unix.h>
7 #include <vlib/stats/stats.h>
9 #include <vnet/devices/devices.h> /* vnet_get_aggregate_rx_packets */
10 #include <vnet/interface.h>
12 static u32 if_names_stats_entry_index = ~0;
13 static u32 **dir_entry_indices = 0;
20 #define _(e, n, p) { .prefix = #p, .name = #n },
21 foreach_simple_interface_counter_name foreach_combined_interface_counter_name
26 statseg_sw_interface_add_del (vnet_main_t *vnm, u32 sw_if_index, u32 is_add)
28 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
29 vlib_stats_entry_t *e;
32 if (if_names_stats_entry_index == ~0)
34 if_names_stats_entry_index = vlib_stats_add_string_vector ("/if/names");
36 for (int i = 0; i < ARRAY_LEN (if_counters); i++)
37 if_counters[i].index = vlib_stats_find_entry_index (
38 "/%s/%s", if_counters[i].prefix, if_counters[i].name);
41 e = sm->directory_vector + if_names_stats_entry_index;
43 vec_validate (dir_entry_indices, sw_if_index);
45 vlib_stats_segment_lock ();
49 vnet_sw_interface_t *si, *si_sup;
50 vnet_hw_interface_t *hi_sup;
53 si = vnet_get_sw_interface (vnm, sw_if_index);
54 si_sup = vnet_get_sup_sw_interface (vnm, si->sw_if_index);
55 ASSERT (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
56 hi_sup = vnet_get_hw_interface (vnm, si_sup->hw_if_index);
58 oldheap = clib_mem_set_heap (sm->heap);
59 s = format (0, "%v", hi_sup->name);
60 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
61 s = format (s, ".%d", si->sub.id);
62 s = format (s, "%c", 0);
64 vec_validate (e->string_vector, sw_if_index);
66 ASSERT (e->string_vector[sw_if_index] == 0);
67 e->string_vector[sw_if_index] = s;
68 clib_mem_set_heap (oldheap);
70 s = format (0, "/interfaces/%U", format_vlib_stats_symlink, s);
71 for (u32 index, i = 0; i < ARRAY_LEN (if_counters); i++)
73 index = vlib_stats_add_symlink (if_counters[i].index, sw_if_index,
74 "%v/%s", s, if_counters[i].name);
76 vec_add1 (dir_entry_indices[sw_if_index], index);
82 oldheap = clib_mem_set_heap (sm->heap);
83 vec_free (e->string_vector[sw_if_index]);
84 clib_mem_set_heap (oldheap);
85 for (u32 i = 0; i < vec_len (dir_entry_indices[sw_if_index]); i++)
86 vlib_stats_remove_entry (dir_entry_indices[sw_if_index][i]);
87 vec_free (dir_entry_indices[sw_if_index]);
90 vlib_stats_segment_unlock ();
95 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (statseg_sw_interface_add_del);