blob: eb69c39c917ef6cf330de96183e483d2a065219c [file] [log] [blame]
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +01001/*
2 * Copyright (c) 2016 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/span/span.h>
21
22#include <vppinfra/error.h>
23#include <vppinfra/elog.h>
24
25vlib_node_registration_t span_node;
26
27/* packet trace format function */
28u8 *
29format_span_trace (u8 * s, va_list * args)
30{
31 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33 span_trace_t *t = va_arg (*args, span_trace_t *);
34
35 vnet_main_t *vnm = &vnet_main;
36 s = format (s, "SPAN: mirrored %U -> %U",
37 format_vnet_sw_if_index_name, vnm, t->src_sw_if_index,
38 format_vnet_sw_if_index_name, vnm, t->mirror_sw_if_index);
39
40 return s;
41}
42
43#define foreach_span_error \
44_(HITS, "SPAN incomming packets processed")
45
46typedef enum
47{
48#define _(sym,str) SPAN_ERROR_##sym,
49 foreach_span_error
50#undef _
51 SPAN_N_ERROR,
52} span_error_t;
53
54static char *span_error_strings[] = {
55#define _(sym,string) string,
56 foreach_span_error
57#undef _
58};
59
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010060static_always_inline void
Pavel Kotucek077d6ae2017-01-24 08:33:38 +010061span_mirror (vlib_main_t * vm, vlib_node_runtime_t * node, u32 sw_if_index0,
62 vlib_buffer_t * b0, vlib_frame_t ** mirror_frames, int is_rx)
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010063{
64 vlib_buffer_t *c0;
Pavel Kotucek077d6ae2017-01-24 08:33:38 +010065 span_main_t *sm = &span_main;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010066 vnet_main_t *vnm = &vnet_main;
Pavel Kotucek077d6ae2017-01-24 08:33:38 +010067 span_interface_t *si0 = 0;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010068 u32 *to_mirror_next = 0;
69 u32 i;
70
Pavel Kotucek077d6ae2017-01-24 08:33:38 +010071 si0 = vec_elt_at_index (sm->interfaces, sw_if_index0);
72 if (!si0)
73 return;
74
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010075 if (is_rx != 0 && si0->num_rx_mirror_ports == 0)
76 return;
77
78 if (is_rx == 0 && si0->num_tx_mirror_ports == 0)
79 return;
80
81 /* Don't do it again */
82 if (PREDICT_FALSE (b0->flags & VNET_BUFFER_SPAN_CLONE))
83 return;
84
85 /* *INDENT-OFF* */
86 clib_bitmap_foreach (i, is_rx ? si0->rx_mirror_ports : si0->tx_mirror_ports, (
87 {
88 if (mirror_frames[i] == 0)
89 mirror_frames[i] = vnet_get_frame_to_sw_interface (vnm, i);
90 to_mirror_next = vlib_frame_vector_args (mirror_frames[i]);
91 to_mirror_next += mirror_frames[i]->n_vectors;
Dave Barach26cd8c12017-02-23 17:11:26 -050092 /* This can fail */
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010093 c0 = vlib_buffer_copy (vm, b0);
Dave Barach26cd8c12017-02-23 17:11:26 -050094 if (PREDICT_TRUE(c0 != 0))
95 {
96 vnet_buffer (c0)->sw_if_index[VLIB_TX] = i;
97 c0->flags |= VNET_BUFFER_SPAN_CLONE;
98 to_mirror_next[0] = vlib_get_buffer_index (vm, c0);
99 mirror_frames[i]->n_vectors++;
Pavel Kotucek077d6ae2017-01-24 08:33:38 +0100100 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
101 {
102 span_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
103 t->src_sw_if_index = sw_if_index0;
104 t->mirror_sw_if_index = i;
105 }
Dave Barach26cd8c12017-02-23 17:11:26 -0500106 }
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100107 }));
108 /* *INDENT-ON* */
109}
110
111static_always_inline uword
112span_node_inline_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
113 vlib_frame_t * frame, int is_rx)
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100114{
115 span_main_t *sm = &span_main;
116 vnet_main_t *vnm = &vnet_main;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100117 u32 n_left_from, *from, *to_next;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100118 u32 n_span_packets = 0;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100119 u32 next_index;
120 u32 sw_if_index;
121 static __thread vlib_frame_t **mirror_frames = 0;
122 vlib_rx_or_tx_t rxtx = is_rx ? VLIB_RX : VLIB_TX;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100123
124 from = vlib_frame_vector_args (frame);
125 n_left_from = frame->n_vectors;
126 next_index = node->cached_next_index;
127
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100128 vec_validate_aligned (mirror_frames, sm->max_sw_if_index,
129 CLIB_CACHE_LINE_BYTES);
130
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100131 while (n_left_from > 0)
132 {
133 u32 n_left_to_next;
134
135 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
136
137 while (n_left_from >= 4 && n_left_to_next >= 2)
138 {
139 u32 bi0;
140 u32 bi1;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100141 vlib_buffer_t *b0;
142 vlib_buffer_t *b1;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100143 u32 sw_if_index0;
Pavel Kotuceke2e95ce2016-11-29 11:03:37 +0100144 u32 next0 = 0;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100145 u32 sw_if_index1;
Pavel Kotuceke2e95ce2016-11-29 11:03:37 +0100146 u32 next1 = 0;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100147
148 /* speculatively enqueue b0, b1 to the current next frame */
149 to_next[0] = bi0 = from[0];
150 to_next[1] = bi1 = from[1];
151 to_next += 2;
152 n_left_to_next -= 2;
153 from += 2;
154 n_left_from -= 2;
155
156 b0 = vlib_get_buffer (vm, bi0);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100157 b1 = vlib_get_buffer (vm, bi1);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100158 sw_if_index0 = vnet_buffer (b0)->sw_if_index[rxtx];
159 sw_if_index1 = vnet_buffer (b1)->sw_if_index[rxtx];
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100160
Pavel Kotucek077d6ae2017-01-24 08:33:38 +0100161 span_mirror (vm, node, sw_if_index0, b0, mirror_frames, is_rx);
162 span_mirror (vm, node, sw_if_index1, b1, mirror_frames, is_rx);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100163
164 vnet_feature_next (sw_if_index0, &next0, b0);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100165 vnet_feature_next (sw_if_index1, &next1, b1);
166
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100167 /* verify speculative enqueue, maybe switch current next frame */
168 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
169 to_next, n_left_to_next,
170 bi0, bi1, next0, next1);
171 }
172 while (n_left_from > 0 && n_left_to_next > 0)
173 {
174 u32 bi0;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100175 vlib_buffer_t *b0;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100176 u32 sw_if_index0;
Pavel Kotuceke2e95ce2016-11-29 11:03:37 +0100177 u32 next0 = 0;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100178
179 /* speculatively enqueue b0 to the current next frame */
180 to_next[0] = bi0 = from[0];
181 to_next += 1;
182 n_left_to_next -= 1;
183 from += 1;
184 n_left_from -= 1;
185
186 b0 = vlib_get_buffer (vm, bi0);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100187 sw_if_index0 = vnet_buffer (b0)->sw_if_index[rxtx];
Pavel Kotucek077d6ae2017-01-24 08:33:38 +0100188
189 span_mirror (vm, node, sw_if_index0, b0, mirror_frames, is_rx);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100190
191 vnet_feature_next (sw_if_index0, &next0, b0);
192
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100193 /* verify speculative enqueue, maybe switch current next frame */
194 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
195 n_left_to_next, bi0, next0);
196 }
197
198 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
199 }
200
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100201
202 for (sw_if_index = 0; sw_if_index < vec_len (mirror_frames); sw_if_index++)
203 {
204 if (mirror_frames[sw_if_index] == 0)
205 continue;
206
207 vnet_put_frame_to_sw_interface (vnm, sw_if_index,
208 mirror_frames[sw_if_index]);
209 mirror_frames[sw_if_index] = 0;
210 }
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100211 vlib_node_increment_counter (vm, span_node.index, SPAN_ERROR_HITS,
212 n_span_packets);
213
214 return frame->n_vectors;
215}
216
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100217static uword
218span_input_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
219 vlib_frame_t * frame)
220{
221 return span_node_inline_fn (vm, node, frame, 1);
222}
223
224static uword
225span_output_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
226 vlib_frame_t * frame)
227{
228 return span_node_inline_fn (vm, node, frame, 0);
229}
230
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100231/* *INDENT-OFF* */
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100232VLIB_REGISTER_NODE (span_input_node) = {
233 .function = span_input_node_fn,
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100234 .name = "span-input",
235 .vector_size = sizeof (u32),
236 .format_trace = format_span_trace,
237 .type = VLIB_NODE_TYPE_INTERNAL,
238
239 .n_errors = ARRAY_LEN(span_error_strings),
240 .error_strings = span_error_strings,
241
242 .n_next_nodes = 0,
243
244 /* edit / add dispositions here */
245 .next_nodes = {
246 [0] = "error-drop",
247 },
248};
249
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100250VLIB_NODE_FUNCTION_MULTIARCH (span_input_node, span_input_node_fn)
251
252VLIB_REGISTER_NODE (span_output_node) = {
253 .function = span_output_node_fn,
254 .name = "span-output",
255 .vector_size = sizeof (u32),
256 .format_trace = format_span_trace,
257 .type = VLIB_NODE_TYPE_INTERNAL,
258
259 .n_errors = ARRAY_LEN(span_error_strings),
260 .error_strings = span_error_strings,
261
262 .n_next_nodes = 0,
263
264 /* edit / add dispositions here */
265 .next_nodes = {
266 [0] = "error-drop",
267 },
268};
269
270VLIB_NODE_FUNCTION_MULTIARCH (span_output_node, span_output_node_fn)
271
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100272/* *INDENT-ON* */
273
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100274/*
275 * fd.io coding-style-patch-verification: ON
276 *
277 * Local Variables:
278 * eval: (c-set-style "gnu")
279 * End:
280 */