span: add feature (rx only) (VPP-185)
[vpp.git] / vnet / vnet / span / node.c
1 /*
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
25 vlib_node_registration_t span_node;
26
27 /* packet trace format function */
28 u8 *
29 format_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
46 typedef enum
47 {
48 #define _(sym,str) SPAN_ERROR_##sym,
49   foreach_span_error
50 #undef _
51     SPAN_N_ERROR,
52 } span_error_t;
53
54 static char *span_error_strings[] = {
55 #define _(sym,string) string,
56   foreach_span_error
57 #undef _
58 };
59
60 static uword
61 span_node_fn (vlib_main_t * vm,
62               vlib_node_runtime_t * node, vlib_frame_t * frame)
63 {
64   span_main_t *sm = &span_main;
65   vnet_main_t *vnm = &vnet_main;
66   u32 n_left_from, *from, *to_next, *to_mirror_next = 0;
67   u32 n_span_packets = 0;
68   u32 next_index, mirror_sw_if_index0, mirror_sw_if_index1;
69   u32 last_mirror_sw_if_index = ~0;
70   vlib_frame_t *mirror_frame = 0;
71
72   from = vlib_frame_vector_args (frame);
73   n_left_from = frame->n_vectors;
74   next_index = node->cached_next_index;
75
76   /* TODO dual loop */
77   while (n_left_from > 0)
78     {
79       u32 n_left_to_next;
80
81       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
82
83       while (n_left_from >= 4 && n_left_to_next >= 2)
84         {
85           u32 bi0;
86           u32 bi1;
87           vlib_buffer_t *b0, *c0;
88           vlib_buffer_t *b1, *c1;
89           u32 sw_if_index0;
90           u32 next0 = 0;        //SPAN_NEXT_ORIG_ETHERNET;
91           u32 sw_if_index1;
92           u32 next1 = 1;        //SPAN_NEXT_ORIG_ETHERNET;
93
94           /* speculatively enqueue b0, b1 to the current next frame */
95           to_next[0] = bi0 = from[0];
96           to_next[1] = bi1 = from[1];
97           to_next += 2;
98           n_left_to_next -= 2;
99           from += 2;
100           n_left_from -= 2;
101
102           b0 = vlib_get_buffer (vm, bi0);
103           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
104           mirror_sw_if_index0 = sm->dst_by_src_sw_if_index[sw_if_index0];
105           b1 = vlib_get_buffer (vm, bi1);
106           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
107           mirror_sw_if_index1 = sm->dst_by_src_sw_if_index[sw_if_index1];
108
109           /* get frame to mirror interface */
110           if (PREDICT_FALSE
111               ((last_mirror_sw_if_index != mirror_sw_if_index0)
112                || mirror_frame == 0))
113             {
114               if (mirror_frame)
115                 vnet_put_frame_to_sw_interface (vnm, last_mirror_sw_if_index,
116                                                 mirror_frame);
117               last_mirror_sw_if_index = mirror_sw_if_index0;
118               mirror_frame =
119                 vnet_get_frame_to_sw_interface (vnm, mirror_sw_if_index0);
120               to_mirror_next = vlib_frame_vector_args (mirror_frame);
121             }
122           /* get frame to mirror interface */
123           if (PREDICT_FALSE
124               ((last_mirror_sw_if_index != mirror_sw_if_index1)
125                || mirror_frame == 0))
126             {
127               if (mirror_frame)
128                 vnet_put_frame_to_sw_interface (vnm, last_mirror_sw_if_index,
129                                                 mirror_frame);
130               last_mirror_sw_if_index = mirror_sw_if_index1;
131               mirror_frame =
132                 vnet_get_frame_to_sw_interface (vnm, mirror_sw_if_index0);
133               to_mirror_next = vlib_frame_vector_args (mirror_frame);
134             }
135           c0 = vlib_buffer_copy (vm, b0);
136           vnet_buffer (c0)->sw_if_index[VLIB_TX] = mirror_sw_if_index0;
137           to_mirror_next[0] = vlib_get_buffer_index (vm, c0);
138           to_mirror_next += 1;
139           mirror_frame->n_vectors++;
140
141           vnet_feature_next (sw_if_index0, &next0, b0);
142
143           c1 = vlib_buffer_copy (vm, b1);
144           vnet_buffer (c1)->sw_if_index[VLIB_TX] = mirror_sw_if_index1;
145           to_mirror_next[0] = vlib_get_buffer_index (vm, c1);
146           to_mirror_next += 1;
147           mirror_frame->n_vectors++;
148
149           vnet_feature_next (sw_if_index1, &next1, b1);
150
151           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
152             {
153               span_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
154               t->src_sw_if_index = sw_if_index0;
155               t->mirror_sw_if_index =
156                 sm->dst_by_src_sw_if_index[sw_if_index0];
157             }
158
159           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
160             {
161               span_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
162               t->src_sw_if_index = sw_if_index1;
163               t->mirror_sw_if_index =
164                 sm->dst_by_src_sw_if_index[sw_if_index1];
165             }
166           /* verify speculative enqueue, maybe switch current next frame */
167           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
168                                            to_next, n_left_to_next,
169                                            bi0, bi1, next0, next1);
170         }
171       while (n_left_from > 0 && n_left_to_next > 0)
172         {
173           u32 bi0;
174           vlib_buffer_t *b0, *c0;
175           u32 sw_if_index0;
176           u32 next0 = 0;        //SPAN_NEXT_ORIG_ETHERNET;
177
178           /* speculatively enqueue b0 to the current next frame */
179           to_next[0] = bi0 = from[0];
180           to_next += 1;
181           n_left_to_next -= 1;
182           from += 1;
183           n_left_from -= 1;
184
185           b0 = vlib_get_buffer (vm, bi0);
186           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
187           mirror_sw_if_index0 = sm->dst_by_src_sw_if_index[sw_if_index0];
188
189           /* get frame to mirror interface */
190           if (PREDICT_FALSE
191               ((last_mirror_sw_if_index != mirror_sw_if_index0)
192                || mirror_frame == 0))
193             {
194               if (mirror_frame)
195                 vnet_put_frame_to_sw_interface (vnm, last_mirror_sw_if_index,
196                                                 mirror_frame);
197               last_mirror_sw_if_index = mirror_sw_if_index0;
198               mirror_frame =
199                 vnet_get_frame_to_sw_interface (vnm, mirror_sw_if_index0);
200               to_mirror_next = vlib_frame_vector_args (mirror_frame);
201             }
202           c0 = vlib_buffer_copy (vm, b0);
203           vnet_buffer (c0)->sw_if_index[VLIB_TX] = mirror_sw_if_index0;
204           to_mirror_next[0] = vlib_get_buffer_index (vm, c0);
205           to_mirror_next += 1;
206           mirror_frame->n_vectors++;
207
208           vnet_feature_next (sw_if_index0, &next0, b0);
209
210           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
211             {
212               span_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
213               t->src_sw_if_index = sw_if_index0;
214               t->mirror_sw_if_index =
215                 sm->dst_by_src_sw_if_index[sw_if_index0];
216             }
217           /* verify speculative enqueue, maybe switch current next frame */
218           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
219                                            n_left_to_next, bi0, next0);
220         }
221
222       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
223     }
224
225   vnet_put_frame_to_sw_interface (vnm, last_mirror_sw_if_index, mirror_frame);
226   vlib_node_increment_counter (vm, span_node.index, SPAN_ERROR_HITS,
227                                n_span_packets);
228
229   return frame->n_vectors;
230 }
231
232 /* *INDENT-OFF* */
233 VLIB_REGISTER_NODE (span_node) = {
234   .function = span_node_fn,
235   .name = "span-input",
236   .vector_size = sizeof (u32),
237   .format_trace = format_span_trace,
238   .type = VLIB_NODE_TYPE_INTERNAL,
239
240   .n_errors = ARRAY_LEN(span_error_strings),
241   .error_strings = span_error_strings,
242
243   .n_next_nodes = 0,
244
245   /* edit / add dispositions here */
246   .next_nodes = {
247     [0] = "error-drop",
248   },
249 };
250
251 /* *INDENT-ON* */
252
253 VLIB_NODE_FUNCTION_MULTIARCH (span_node, span_node_fn)
254 /*
255  * fd.io coding-style-patch-verification: ON
256  *
257  * Local Variables:
258  * eval: (c-set-style "gnu")
259  * End:
260  */