blob: 969f89303504c3cc0b3584bf9b295e4e4ccd7313 [file] [log] [blame]
Neale Ranns871dc422018-03-29 01:28:09 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vlib/vlib.h>
17#include <vnet/vnet.h>
18#include <vppinfra/error.h>
19
20#include <vnet/feature/feature.h>
21#include <vnet/ethernet/ethernet.h>
22
23int
24vnet_sw_interface_stats_collect_enable_disable (u32 sw_if_index, u8 enable)
25{
26 ethernet_interface_t *eif;
27 vnet_sw_interface_t *si;
28 ethernet_main_t *em;
29 vnet_main_t *vnm;
30
31 vnm = vnet_get_main ();
32 em = &ethernet_main;
33 si = vnet_get_sw_interface (vnm, sw_if_index);
34
35 /*
36 * only ethernet HW interfaces are supported at this time
37 */
38 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
39 {
40 return (VNET_API_ERROR_INVALID_VALUE);
41 }
42
43 eif = ethernet_get_interface (em, si->hw_if_index);
44
45 if (!eif)
46 {
47 return (VNET_API_ERROR_FEATURE_DISABLED);
48 }
49
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);
54
55 return (0);
56}
57
58static u8 *
59format_stats_collect_trace (u8 * s, va_list * args)
60{
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 *);
63
64 return s;
65}
66
67#define inc_counter(ctype, rx_tx) \
68{ \
69}
70
71static_always_inline uword
72stats_collect_inline (vlib_main_t * vm,
73 vlib_node_runtime_t * node,
74 vlib_frame_t * frame, vlib_rx_or_tx_t rxtx)
75{
76 vnet_interface_counter_type_t ct;
77 u32 n_left_from, *from, *to_next;
78 u32 next_index;
79 u32 sw_if_index = 0;
80 u32 stats_n_packets[VNET_N_COMBINED_INTERFACE_COUNTER] = { 0 };
81 u64 stats_n_bytes[VNET_N_COMBINED_INTERFACE_COUNTER] = { 0 };
82
83 from = vlib_frame_vector_args (frame);
84 n_left_from = frame->n_vectors;
85 next_index = node->cached_next_index;
86
87 while (n_left_from > 0)
88 {
89 u32 n_left_to_next;
90
91 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
92
93 while (n_left_from > 0 && n_left_to_next > 0)
94 {
95 u32 bi0;
96 vlib_buffer_t *b0;
97 u32 next0 = 0;
98 int b0_ctype;
99
100 /* speculatively enqueue b0 to the current next frame */
101 to_next[0] = bi0 = from[0];
102 to_next += 1;
103 n_left_to_next -= 1;
104 from += 1;
105 n_left_from -= 1;
106
107 b0 = vlib_get_buffer (vm, bi0);
108 sw_if_index = vnet_buffer (b0)->sw_if_index[rxtx];
109
110 if (VLIB_RX == rxtx)
111 {
112 b0_ctype =
113 eh_dst_addr_to_rx_ctype (vlib_buffer_get_current (b0));
114 }
115 else
116 {
117 b0_ctype =
118 eh_dst_addr_to_tx_ctype (vlib_buffer_get_current (b0));
119 }
120
121 stats_n_bytes[b0_ctype] += vlib_buffer_length_in_chain (vm, b0);
122 stats_n_packets[b0_ctype] += 1;
123
Damjan Marion7d98a122018-07-19 20:42:08 +0200124 vnet_feature_next (&next0, b0);
Neale Ranns871dc422018-03-29 01:28:09 -0700125
126 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
127 n_left_to_next, bi0, next0);
128 }
129
130 if (VLIB_RX == rxtx)
131 {
132 foreach_rx_combined_interface_counter (ct)
133 {
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]);
138 }
139 }
140 else
141 {
142 foreach_tx_combined_interface_counter (ct)
143 {
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]);
148 }
149 }
150
151 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
152 }
153
154 return frame->n_vectors;
155}
156
157static uword
158stats_collect_rx (vlib_main_t * vm,
159 vlib_node_runtime_t * node, vlib_frame_t * frame)
160{
161 return stats_collect_inline (vm, node, frame, VLIB_RX);
162}
163
164static uword
165stats_collect_tx (vlib_main_t * vm,
166 vlib_node_runtime_t * node, vlib_frame_t * frame)
167{
168 return stats_collect_inline (vm, node, frame, VLIB_TX);
169}
170
171/* *INDENT-OFF* */
172VLIB_REGISTER_NODE (stats_collect_rx_node) = {
173 .vector_size = sizeof (u32),
174 .format_trace = format_stats_collect_trace,
175 .type = VLIB_NODE_TYPE_INTERNAL,
176 .n_errors = 0,
177 .n_next_nodes = 0,
178 .function = stats_collect_rx,
179 .name = "stats-collect-rx",
180};
181
182VLIB_REGISTER_NODE (stats_collect_tx_node) = {
183 .vector_size = sizeof (u32),
184 .format_trace = format_stats_collect_trace,
185 .type = VLIB_NODE_TYPE_INTERNAL,
186 .n_errors = 0,
187 .n_next_nodes = 0,
188 .function = stats_collect_tx,
189 .name = "stats-collect-tx",
190};
191
192VLIB_NODE_FUNCTION_MULTIARCH (stats_collect_rx_node, stats_collect_rx);
193VLIB_NODE_FUNCTION_MULTIARCH (stats_collect_tx_node, stats_collect_tx);
194
195VNET_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"),
199};
200
201VNET_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"),
205};
206
207/* *INDENT-ON* */
208
209static clib_error_t *
210stats_collect_init (vlib_main_t * vm)
211{
212 return 0;
213}
214
215VLIB_INIT_FUNCTION (stats_collect_init);
216
217
218/*
219 * fd.io coding-style-patch-verification: ON
220 *
221 * Local Variables:
222 * eval: (c-set-style "gnu")
223 * End:
224 */