2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vppinfra/error.h>
20 #include <vnet/feature/feature.h>
21 #include <vnet/ethernet/ethernet.h>
24 vnet_sw_interface_stats_collect_enable_disable (u32 sw_if_index, u8 enable)
26 ethernet_interface_t *eif;
27 vnet_sw_interface_t *si;
31 vnm = vnet_get_main ();
33 si = vnet_get_sw_interface (vnm, sw_if_index);
36 * only ethernet HW interfaces are supported at this time
38 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
40 return (VNET_API_ERROR_INVALID_VALUE);
43 eif = ethernet_get_interface (em, si->hw_if_index);
47 return (VNET_API_ERROR_FEATURE_DISABLED);
50 vnet_feature_enable_disable ("device-input", "stats-collect-rx",
51 sw_if_index, enable, 0, 0);
52 vnet_feature_enable_disable ("interface-output", "stats-collect-tx",
53 sw_if_index, enable, 0, 0);
59 format_stats_collect_trace (u8 * s, va_list * args)
61 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
62 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
67 #define inc_counter(ctype, rx_tx) \
71 static_always_inline uword
72 stats_collect_inline (vlib_main_t * vm,
73 vlib_node_runtime_t * node,
74 vlib_frame_t * frame, vlib_rx_or_tx_t rxtx)
76 vnet_interface_counter_type_t ct;
77 u32 n_left_from, *from, *to_next;
80 u32 stats_n_packets[VNET_N_COMBINED_INTERFACE_COUNTER] = { 0 };
81 u64 stats_n_bytes[VNET_N_COMBINED_INTERFACE_COUNTER] = { 0 };
83 from = vlib_frame_vector_args (frame);
84 n_left_from = frame->n_vectors;
85 next_index = node->cached_next_index;
87 while (n_left_from > 0)
91 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
93 while (n_left_from > 0 && n_left_to_next > 0)
100 /* speculatively enqueue b0 to the current next frame */
101 to_next[0] = bi0 = from[0];
107 b0 = vlib_get_buffer (vm, bi0);
108 sw_if_index = vnet_buffer (b0)->sw_if_index[rxtx];
113 eh_dst_addr_to_rx_ctype (vlib_buffer_get_current (b0));
118 eh_dst_addr_to_tx_ctype (vlib_buffer_get_current (b0));
121 stats_n_bytes[b0_ctype] += vlib_buffer_length_in_chain (vm, b0);
122 stats_n_packets[b0_ctype] += 1;
124 vnet_feature_next (sw_if_index, &next0, b0);
126 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
127 n_left_to_next, bi0, next0);
132 foreach_rx_combined_interface_counter (ct)
134 vlib_increment_combined_counter
135 (vnet_main.interface_main.combined_sw_if_counters + ct,
136 vlib_get_thread_index (),
137 sw_if_index, stats_n_packets[ct], stats_n_bytes[ct]);
142 foreach_tx_combined_interface_counter (ct)
144 vlib_increment_combined_counter
145 (vnet_main.interface_main.combined_sw_if_counters + ct,
146 vlib_get_thread_index (),
147 sw_if_index, stats_n_packets[ct], stats_n_bytes[ct]);
151 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
154 return frame->n_vectors;
158 stats_collect_rx (vlib_main_t * vm,
159 vlib_node_runtime_t * node, vlib_frame_t * frame)
161 return stats_collect_inline (vm, node, frame, VLIB_RX);
165 stats_collect_tx (vlib_main_t * vm,
166 vlib_node_runtime_t * node, vlib_frame_t * frame)
168 return stats_collect_inline (vm, node, frame, VLIB_TX);
172 VLIB_REGISTER_NODE (stats_collect_rx_node) = {
173 .vector_size = sizeof (u32),
174 .format_trace = format_stats_collect_trace,
175 .type = VLIB_NODE_TYPE_INTERNAL,
178 .function = stats_collect_rx,
179 .name = "stats-collect-rx",
182 VLIB_REGISTER_NODE (stats_collect_tx_node) = {
183 .vector_size = sizeof (u32),
184 .format_trace = format_stats_collect_trace,
185 .type = VLIB_NODE_TYPE_INTERNAL,
188 .function = stats_collect_tx,
189 .name = "stats-collect-tx",
192 VLIB_NODE_FUNCTION_MULTIARCH (stats_collect_rx_node, stats_collect_rx);
193 VLIB_NODE_FUNCTION_MULTIARCH (stats_collect_tx_node, stats_collect_tx);
195 VNET_FEATURE_INIT (stats_collect_rx_node, static) = {
196 .arc_name = "device-input",
197 .node_name = "stats-collect-rx",
198 .runs_before = VNET_FEATURES ("ethernet-input"),
201 VNET_FEATURE_INIT (stats_collect_tx_node, static) = {
202 .arc_name = "interface-output",
203 .node_name = "stats-collect-tx",
204 .runs_before = VNET_FEATURES ("interface-tx"),
209 static clib_error_t *
210 stats_collect_init (vlib_main_t * vm)
215 VLIB_INIT_FUNCTION (stats_collect_init);
219 * fd.io coding-style-patch-verification: ON
222 * eval: (c-set-style "gnu")