Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright(c) 2022 Cisco Systems, Inc. |
| 3 | */ |
| 4 | |
| 5 | #include <vlib/vlib.h> |
| 6 | #include <vlib/unix/unix.h> |
| 7 | #include <vlib/stats/stats.h> |
| 8 | #include <vnet/vnet.h> |
| 9 | #include <vnet/devices/devices.h> /* vnet_get_aggregate_rx_packets */ |
| 10 | #include <vnet/interface.h> |
| 11 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 12 | vlib_stats_string_vector_t if_names = 0; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 13 | static u32 **dir_entry_indices = 0; |
| 14 | |
| 15 | static struct |
| 16 | { |
| 17 | char *prefix, *name; |
| 18 | u32 index; |
| 19 | } if_counters[] = { |
| 20 | #define _(e, n, p) { .prefix = #p, .name = #n }, |
| 21 | foreach_simple_interface_counter_name foreach_combined_interface_counter_name |
| 22 | #undef _ |
| 23 | }; |
| 24 | |
| 25 | static clib_error_t * |
| 26 | statseg_sw_interface_add_del (vnet_main_t *vnm, u32 sw_if_index, u32 is_add) |
| 27 | { |
Steven Luong | 116392b | 2023-07-25 12:32:01 -0700 | [diff] [blame] | 28 | u8 *name = 0; |
Steven Luong | ff27c9f | 2023-06-20 22:22:45 -0700 | [diff] [blame] | 29 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 30 | if (if_names == 0) |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 31 | { |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 32 | if_names = vlib_stats_add_string_vector ("/if/names"); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 33 | |
| 34 | for (int i = 0; i < ARRAY_LEN (if_counters); i++) |
| 35 | if_counters[i].index = vlib_stats_find_entry_index ( |
| 36 | "/%s/%s", if_counters[i].prefix, if_counters[i].name); |
| 37 | } |
| 38 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 39 | vec_validate (dir_entry_indices, sw_if_index); |
| 40 | |
| 41 | vlib_stats_segment_lock (); |
| 42 | |
| 43 | if (is_add) |
| 44 | { |
| 45 | vnet_sw_interface_t *si, *si_sup; |
| 46 | vnet_hw_interface_t *hi_sup; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 47 | |
| 48 | si = vnet_get_sw_interface (vnm, sw_if_index); |
| 49 | si_sup = vnet_get_sup_sw_interface (vnm, si->sw_if_index); |
| 50 | ASSERT (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE); |
| 51 | hi_sup = vnet_get_hw_interface (vnm, si_sup->hw_if_index); |
| 52 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 53 | name = format (0, "%v", hi_sup->name); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 54 | if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE) |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 55 | name = format (name, ".%d", si->sub.id); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 56 | |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 57 | vlib_stats_set_string_vector (&if_names, sw_if_index, "%v", name); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 58 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 59 | for (u32 index, i = 0; i < ARRAY_LEN (if_counters); i++) |
| 60 | { |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 61 | index = vlib_stats_add_symlink ( |
| 62 | if_counters[i].index, sw_if_index, "/interfaces/%U/%s", |
| 63 | format_vlib_stats_symlink, name, if_counters[i].name); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 64 | ASSERT (index != ~0); |
| 65 | vec_add1 (dir_entry_indices[sw_if_index], index); |
| 66 | } |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 67 | } |
| 68 | else |
| 69 | { |
Steven Luong | ff27c9f | 2023-06-20 22:22:45 -0700 | [diff] [blame] | 70 | name = format (0, "%s", "deleted"); |
| 71 | vlib_stats_set_string_vector (&if_names, sw_if_index, "%v", name); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 72 | for (u32 i = 0; i < vec_len (dir_entry_indices[sw_if_index]); i++) |
| 73 | vlib_stats_remove_entry (dir_entry_indices[sw_if_index][i]); |
| 74 | vec_free (dir_entry_indices[sw_if_index]); |
| 75 | } |
| 76 | |
Steven Luong | ff27c9f | 2023-06-20 22:22:45 -0700 | [diff] [blame] | 77 | vec_free (name); |
| 78 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 79 | vlib_stats_segment_unlock (); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | VNET_SW_INTERFACE_ADD_DEL_FUNCTION (statseg_sw_interface_add_del); |