Initial commit of vpp code.
[vpp.git] / vnet / vnet / l2 / l2_input_vtr.c
1 /*
2  * l2_input_vtr.c : layer 2 input vlan tag rewrite processing
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ethernet/packet.h>
22 #include <vnet/l2/l2_input.h>
23 #include <vnet/l2/feat_bitmap.h>
24 #include <vnet/l2/l2_vtr.h>
25 #include <vnet/l2/l2_input_vtr.h>
26 #include <vnet/l2/l2_output.h>
27
28 #include <vppinfra/error.h>
29 #include <vppinfra/cache.h>
30
31
32 typedef struct {
33   /* per-pkt trace data */ 
34   u8  src[6];
35   u8  dst[6];
36   u8  raw[12]; // raw data (vlans)
37   u32 sw_if_index;
38 } l2_invtr_trace_t;
39
40 /* packet trace format function */
41 static u8 * format_l2_invtr_trace (u8 * s, va_list * args)
42 {
43   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
44   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
45   l2_invtr_trace_t * t = va_arg (*args, l2_invtr_trace_t *);
46   
47   s = format (s, "l2-input-vtr: sw_if_index %d dst %U src %U data "
48               "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
49               t->sw_if_index,
50               format_ethernet_address, t->dst,
51               format_ethernet_address, t->src,
52               t->raw[0], t->raw[1], t->raw[2], t->raw[3], t->raw[4], t->raw[5],
53               t->raw[6], t->raw[7], t->raw[8], t->raw[9], t->raw[10], t->raw[11]);
54   return s;
55 }
56
57 l2_invtr_main_t l2_invtr_main;
58
59 static vlib_node_registration_t l2_invtr_node;
60
61 #define foreach_l2_invtr_error                  \
62 _(L2_INVTR,    "L2 inverter packets")           \
63 _(DROP,        "L2 input tag rewrite drops")
64
65 typedef enum {
66 #define _(sym,str) L2_INVTR_ERROR_##sym,
67   foreach_l2_invtr_error
68 #undef _
69   L2_INVTR_N_ERROR,
70 } l2_invtr_error_t;
71
72 static char * l2_invtr_error_strings[] = {
73 #define _(sym,string) string,
74   foreach_l2_invtr_error
75 #undef _
76 };
77
78 typedef enum {
79   L2_INVTR_NEXT_DROP,
80   L2_INVTR_N_NEXT,
81 } l2_invtr_next_t;
82
83
84 static uword
85 l2_invtr_node_fn (vlib_main_t * vm,
86                   vlib_node_runtime_t * node,
87                   vlib_frame_t * frame)
88 {
89   u32 n_left_from, * from, * to_next;
90   l2_invtr_next_t next_index;
91   l2_invtr_main_t * msm = &l2_invtr_main;
92   // vlib_node_t *n = vlib_get_node (vm, l2_invtr_node.index);
93   // u32 node_counter_base_index = n->error_heap_index;
94   // vlib_error_main_t * em = &vm->error_main;
95
96   from = vlib_frame_vector_args (frame);
97   n_left_from = frame->n_vectors; /* number of packets to process */
98   next_index = node->cached_next_index;
99
100   while (n_left_from > 0)
101     {
102       u32 n_left_to_next;
103
104       /* get space to enqueue frame to graph node "next_index" */
105       vlib_get_next_frame (vm, node, next_index,
106                            to_next, n_left_to_next);
107
108       while (n_left_from >= 6 && n_left_to_next >= 2)
109         {
110           u32 bi0, bi1;
111           vlib_buffer_t * b0, * b1;
112           u32 next0, next1;
113           u32 sw_if_index0, sw_if_index1;
114           u32 feature_bitmap0, feature_bitmap1;
115          
116           /* Prefetch next iteration. */
117           {
118             vlib_buffer_t * p2, * p3, * p4, * p5;
119             u32 sw_if_index2, sw_if_index3;
120             
121             p2 = vlib_get_buffer (vm, from[2]);
122             p3 = vlib_get_buffer (vm, from[3]);
123             p4 = vlib_get_buffer (vm, from[4]);
124             p5 = vlib_get_buffer (vm, from[5]);
125              
126             // Prefetch the buffer header and packet for the N+2 loop iteration
127             vlib_prefetch_buffer_header (p4, LOAD);
128             vlib_prefetch_buffer_header (p5, LOAD);
129
130             CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
131             CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
132
133             // Prefetch the input config for the N+1 loop iteration
134             // This depends on the buffer header above
135             sw_if_index2 = vnet_buffer(p2)->sw_if_index[VLIB_RX];
136             sw_if_index3 = vnet_buffer(p3)->sw_if_index[VLIB_RX];
137             CLIB_PREFETCH (vec_elt_at_index(l2output_main.configs, sw_if_index2), CLIB_CACHE_LINE_BYTES, LOAD);
138             CLIB_PREFETCH (vec_elt_at_index(l2output_main.configs, sw_if_index3), CLIB_CACHE_LINE_BYTES, LOAD);
139           }
140
141           /* speculatively enqueue b0 and b1 to the current next frame */
142           /* bi is "buffer index", b is pointer to the buffer */
143           to_next[0] = bi0 = from[0];
144           to_next[1] = bi1 = from[1];
145           from += 2;
146           to_next += 2;
147           n_left_from -= 2;
148           n_left_to_next -= 2;
149
150           b0 = vlib_get_buffer (vm, bi0);
151           b1 = vlib_get_buffer (vm, bi1);
152  
153           /* RX interface handles */
154           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
155           sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
156
157           // process 2 packets
158           // em->counters[node_counter_base_index + L2_INVTR_ERROR_L2_INVTR] += 2;
159
160           // Remove ourself from the feature bitmap
161           feature_bitmap0 = vnet_buffer(b0)->l2.feature_bitmap & ~L2INPUT_FEAT_VTR;
162           feature_bitmap1 = vnet_buffer(b1)->l2.feature_bitmap & ~L2INPUT_FEAT_VTR;
163
164           // save for next feature graph nodes
165           vnet_buffer(b0)->l2.feature_bitmap = feature_bitmap0;
166           vnet_buffer(b1)->l2.feature_bitmap = feature_bitmap1;
167
168           // Determine the next node
169           next0 = feat_bitmap_get_next_node_index(msm->feat_next_node_index,
170                                                   feature_bitmap0);
171           next1 = feat_bitmap_get_next_node_index(msm->feat_next_node_index,
172                                                   feature_bitmap1);
173
174           // perform the tag rewrite on two packets
175           if (l2_vtr_process(b0, &(vec_elt_at_index(l2output_main.configs, sw_if_index0)->input_vtr))) {
176             // Drop packet
177             next0 = L2_INVTR_NEXT_DROP;
178             b0->error = node->errors[L2_INVTR_ERROR_DROP];
179           }
180           if (l2_vtr_process(b1, &(vec_elt_at_index(l2output_main.configs, sw_if_index1)->input_vtr))) {
181             // Drop packet
182             next1 = L2_INVTR_NEXT_DROP;
183             b1->error = node->errors[L2_INVTR_ERROR_DROP];
184           }
185
186           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE))) {
187             if (b0->flags & VLIB_BUFFER_IS_TRACED) {
188               l2_invtr_trace_t *t = 
189                vlib_add_trace (vm, node, b0, sizeof (*t));
190               ethernet_header_t * h0 = vlib_buffer_get_current (b0);
191               t->sw_if_index = sw_if_index0;
192               memcpy(t->src, h0->src_address, 6);
193               memcpy(t->dst, h0->dst_address, 6);
194               memcpy(t->raw, &h0->type, sizeof(t->raw));
195             }
196             if (b1->flags & VLIB_BUFFER_IS_TRACED) {
197               l2_invtr_trace_t *t = 
198                vlib_add_trace (vm, node, b1, sizeof (*t));
199               ethernet_header_t * h1 = vlib_buffer_get_current (b1);
200               t->sw_if_index = sw_if_index0;
201               memcpy(t->src, h1->src_address, 6);
202               memcpy(t->dst, h1->dst_address, 6);
203               memcpy(t->raw, &h1->type, sizeof(t->raw));
204             }
205           }
206
207           /* verify speculative enqueues, maybe switch current next frame */
208           /* if next0==next1==next_index then nothing special needs to be done */
209           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
210                                            to_next, n_left_to_next,
211                                            bi0, bi1, next0, next1);
212         }
213       
214       while (n_left_from > 0 && n_left_to_next > 0)
215         {
216           u32 bi0;
217           vlib_buffer_t * b0;
218           u32 next0;
219           u32 sw_if_index0;
220           u32 feature_bitmap0;
221
222           /* speculatively enqueue b0 to the current next frame */
223           bi0 = from[0];
224           to_next[0] = bi0;
225           from += 1;
226           to_next += 1;
227           n_left_from -= 1;
228           n_left_to_next -= 1;
229
230           b0 = vlib_get_buffer (vm, bi0);
231
232           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
233
234           // process 1 packet
235           // em->counters[node_counter_base_index + L2_INVTR_ERROR_L2_INVTR] += 1;
236
237           // Remove ourself from the feature bitmap
238           feature_bitmap0 = vnet_buffer(b0)->l2.feature_bitmap & ~L2INPUT_FEAT_VTR;
239
240           // save for next feature graph nodes
241           vnet_buffer(b0)->l2.feature_bitmap = feature_bitmap0;
242
243           // Determine the next node
244           next0 = feat_bitmap_get_next_node_index(msm->feat_next_node_index,
245                                                   feature_bitmap0);
246
247           // perform the tag rewrite on one packet
248           if (l2_vtr_process(b0, &(vec_elt_at_index(l2output_main.configs, sw_if_index0)->input_vtr))) {
249             // Drop packet
250             next0 = L2_INVTR_NEXT_DROP;
251             b0->error = node->errors[L2_INVTR_ERROR_DROP];
252           }
253
254           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
255                             && (b0->flags & VLIB_BUFFER_IS_TRACED))) {
256             l2_invtr_trace_t *t = 
257                vlib_add_trace (vm, node, b0, sizeof (*t));
258             ethernet_header_t * h0 = vlib_buffer_get_current (b0);
259             t->sw_if_index = sw_if_index0;
260             memcpy(t->src, h0->src_address, 6);
261             memcpy(t->dst, h0->dst_address, 6);
262             memcpy(t->raw, &h0->type, sizeof(t->raw));
263           }
264
265           /* verify speculative enqueue, maybe switch current next frame */
266           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
267                                            to_next, n_left_to_next,
268                                            bi0, next0);
269         }
270
271       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
272     }
273
274   return frame->n_vectors;
275 }
276
277
278 VLIB_REGISTER_NODE (l2_invtr_node,static) = {
279   .function = l2_invtr_node_fn,
280   .name = "l2-input-vtr",
281   .vector_size = sizeof (u32),
282   .format_trace = format_l2_invtr_trace,
283   .type = VLIB_NODE_TYPE_INTERNAL,
284   
285   .n_errors = ARRAY_LEN(l2_invtr_error_strings),
286   .error_strings = l2_invtr_error_strings,
287
288   .n_next_nodes = L2_INVTR_N_NEXT,
289
290   /* edit / add dispositions here */
291   .next_nodes = {
292        [L2_INVTR_NEXT_DROP]  = "error-drop",
293   },
294 };
295
296 clib_error_t *l2_invtr_init (vlib_main_t *vm)
297 {
298   l2_invtr_main_t * mp = &l2_invtr_main;
299  
300   mp->vlib_main = vm;
301   mp->vnet_main = vnet_get_main();
302
303   // Initialize the feature next-node indexes
304   feat_bitmap_init_next_nodes(vm,
305                               l2_invtr_node.index,
306                               L2INPUT_N_FEAT,
307                               l2input_get_feat_names(),
308                               mp->feat_next_node_index);
309
310   return 0;
311 }
312
313 VLIB_INIT_FUNCTION (l2_invtr_init);
314