474e2e2a9a455b54c1ab69ea51712e646e5c659e
[vpp.git] / vnet / vnet / mpls-gre / node.c
1 /*
2  * node.c: mpls-o-gre decap processing
3  *
4  * Copyright (c) 2012-2014 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/pg/pg.h>
20 #include <vnet/mpls-gre/mpls.h>
21
22 typedef struct {
23   u32 next_index;
24   u32 decap_index;
25   u32 tx_fib_index;
26   u32 label_host_byte_order;
27 } mpls_rx_trace_t;
28
29 u8 * format_mpls_rx_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   mpls_rx_trace_t * t = va_arg (*args, mpls_rx_trace_t *);
34   char * next_name;
35
36   next_name = "BUG!";
37
38 #define _(a,b) if (t->next_index == MPLS_INPUT_NEXT_##a) next_name = b;
39   foreach_mpls_input_next;
40 #undef _
41   
42   s = format (s, "MPLS: next %s, lookup fib index %d, decap index %d\n",
43               next_name, t->next_index, t->tx_fib_index, t->decap_index);
44   if (t->decap_index != ~0)
45     {
46       s = format (s, "    label %d", 
47                   vnet_mpls_uc_get_label(t->label_host_byte_order));
48     }
49   return s;
50 }
51
52 vlib_node_registration_t mpls_input_node;
53
54 typedef struct {
55   u32 last_label;
56   u32 last_inner_fib_index;
57   u32 last_outer_fib_index;
58   mpls_main_t * mpls_main;
59 } mpls_input_runtime_t;
60
61 static inline uword
62 mpls_input_inline (vlib_main_t * vm,
63                    vlib_node_runtime_t * node,
64                    vlib_frame_t * from_frame, int is_mpls_o_gre)
65 {
66   u32 n_left_from, next_index, * from, * to_next;
67   ip4_main_t * im = &ip4_main;
68   from = vlib_frame_vector_args (from_frame);
69   n_left_from = from_frame->n_vectors;
70   mpls_input_runtime_t * rt;
71   mpls_main_t * mm;
72
73   rt = vlib_node_get_runtime_data (vm, mpls_input_node.index);
74   mm = rt->mpls_main;
75   /* 
76    * Force an initial lookup every time, in case the control-plane
77    * changed the label->FIB mapping.
78    */
79   rt->last_label = ~0;
80
81   next_index = node->cached_next_index;
82
83   while (n_left_from > 0)
84     {
85       u32 n_left_to_next;
86
87       vlib_get_next_frame (vm, node, next_index,
88                            to_next, n_left_to_next);
89
90 #if 0
91       while (n_left_from >= 4 && n_left_to_next >= 2)
92         {
93           u32 bi0, bi1;
94           vlib_buffer_t * b0, * b1;
95           mpls_unicast_header_t * h0, * h1;
96           int li0, li1;
97           u64 key0, key1;
98           u32 label0, label1;
99           u32 next0, next1;
100           uword * p0, * p1;
101           u32 fib_index0, fib_index1;
102
103           /* Prefetch next iteration. */
104           {
105             vlib_buffer_t * p2, * p3;
106
107             p2 = vlib_get_buffer (vm, from[2]);
108             p3 = vlib_get_buffer (vm, from[3]);
109
110             vlib_prefetch_buffer_header (p2, LOAD);
111             vlib_prefetch_buffer_header (p3, LOAD);
112
113             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
114             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
115           }
116
117           bi0 = from[0];
118           bi1 = from[1];
119           to_next[0] = bi0;
120           to_next[1] = bi1;
121           from += 2;
122           to_next += 2;
123           n_left_to_next -= 2;
124           n_left_from -= 2;
125
126           b0 = vlib_get_buffer (vm, bi0);
127           b1 = vlib_get_buffer (vm, bi1);
128
129           /* $$$$$ dual loop me */
130
131           vlib_buffer_advance (b0, sizeof (*h0));
132           vlib_buffer_advance (b1, sizeof (*h1));
133
134           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
135                                            to_next, n_left_to_next,
136                                            bi0, bi1, next0, next1);
137         }
138     
139 #endif
140
141       while (n_left_from > 0 && n_left_to_next > 0)
142         {
143           u32 bi0;
144           vlib_buffer_t * b0;
145           mpls_unicast_header_t * h0;
146           u32 label0;
147           u32 next0;
148           u64 key0;
149           uword * p0;
150           u32 rx_fib_index0;
151           mpls_decap_t *d0;
152
153           bi0 = from[0];
154           to_next[0] = bi0;
155           from += 1;
156           to_next += 1;
157           n_left_from -= 1;
158           n_left_to_next -= 1;
159
160           b0 = vlib_get_buffer (vm, bi0);
161           h0 = vlib_buffer_get_current (b0);
162
163           if (is_mpls_o_gre)
164             {
165               rx_fib_index0 = vec_elt (im->fib_index_by_sw_if_index, 
166                                        vnet_buffer(b0)->sw_if_index[VLIB_RX]);
167             }
168           else
169             {
170 #if 0
171               /* If separate RX numbering spaces are required... */
172               rx_fib_index0 = vec_elt (mm->fib_index_by_sw_if_index, 
173                                        vnet_buffer(b0)->sw_if_index[VLIB_RX]);
174 #endif
175               rx_fib_index0 = 0;
176             }
177           
178           next0 = ~0;
179           d0 = 0;
180
181           /* 
182            * Expect the control-plane team to squeal like pigs.
183            * If they don't program a decap label entry for each
184            * and every label in the stack, packets go into the trash...
185            */
186
187           do
188             {
189               label0 = clib_net_to_host_u32 (h0->label_exp_s_ttl);
190               /* TTL expired? */
191               if (PREDICT_FALSE(vnet_mpls_uc_get_ttl (label0) == 0))
192                 {
193                   next0 = MPLS_INPUT_NEXT_DROP;
194                   b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
195                   break;
196                 }
197               
198               key0 = ((u64)rx_fib_index0<<32) 
199                 | ((u64)vnet_mpls_uc_get_label (label0)<<12) 
200                 | ((u64)vnet_mpls_uc_get_s (label0)<<8);
201
202               /* 
203                * The architecture crew claims that we won't need
204                * separate ip4, ip6, mpls-o-ethernet label numbering
205                * spaces. Use the low 8 key bits as a discriminator.
206                */
207
208               p0 = hash_get (mm->mpls_decap_by_rx_fib_and_label, key0);
209               if (p0 == 0)
210                 {
211                   next0 = MPLS_INPUT_NEXT_DROP;
212                   b0->error = node->errors[MPLS_ERROR_BAD_LABEL];
213                   break;
214                 }
215               d0 = pool_elt_at_index (mm->decaps, p0[0]);
216               next0 = d0->next_index;
217               vnet_buffer(b0)->sw_if_index[VLIB_TX] = d0->tx_fib_index;
218               vlib_buffer_advance (b0, sizeof (*h0));
219               h0 = vlib_buffer_get_current (b0);
220             } while (!vnet_mpls_uc_get_s(label0));
221
222           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
223             {
224               mpls_rx_trace_t *tr = vlib_add_trace (vm, node, 
225                                                    b0, sizeof (*tr));
226               tr->next_index = next0;
227               tr->decap_index = d0 ? d0 - mm->decaps : ~0;
228               tr->tx_fib_index = vnet_buffer(b0)->sw_if_index[VLIB_TX];
229               tr->label_host_byte_order = label0;
230             }
231
232           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
233                                            to_next, n_left_to_next,
234                                            bi0, next0);
235         }
236
237       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
238     }
239   vlib_node_increment_counter (vm, mpls_input_node.index,
240                                MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors);
241   return from_frame->n_vectors;
242 }
243
244 static uword
245 mpls_input (vlib_main_t * vm,
246             vlib_node_runtime_t * node,
247             vlib_frame_t * from_frame)
248 {
249   return mpls_input_inline (vm, node, from_frame, 1 /* is mpls-o-gre */);
250 }
251
252 static char * mpls_error_strings[] = {
253 #define mpls_error(n,s) s,
254 #include "error.def"
255 #undef mpls_error
256 };
257
258 VLIB_REGISTER_NODE (mpls_input_node) = {
259   .function = mpls_input,
260   .name = "mpls-gre-input",
261   /* Takes a vector of packets. */
262   .vector_size = sizeof (u32),
263
264   .runtime_data_bytes = sizeof(mpls_input_runtime_t),
265
266   .n_errors = MPLS_N_ERROR,
267   .error_strings = mpls_error_strings,
268
269   .n_next_nodes = MPLS_INPUT_N_NEXT,
270   .next_nodes = {
271 #define _(s,n) [MPLS_INPUT_NEXT_##s] = n,
272     foreach_mpls_input_next
273 #undef _
274   },
275
276   .format_buffer = format_mpls_gre_header_with_length,
277   .format_trace = format_mpls_rx_trace,
278   .unformat_buffer = unformat_mpls_gre_header,
279 };
280
281 VLIB_NODE_FUNCTION_MULTIARCH (mpls_input_node, mpls_input)
282
283 static uword
284 mpls_ethernet_input (vlib_main_t * vm,
285                      vlib_node_runtime_t * node,
286                      vlib_frame_t * from_frame)
287 {
288   return mpls_input_inline (vm, node, from_frame, 0 /* is mpls-o-gre */);
289 }
290
291
292 VLIB_REGISTER_NODE (mpls_ethernet_input_node) = {
293   .function = mpls_ethernet_input,
294   .name = "mpls-ethernet-input",
295   /* Takes a vector of packets. */
296   .vector_size = sizeof (u32),
297
298   .runtime_data_bytes = sizeof(mpls_input_runtime_t),
299
300   .n_errors = MPLS_N_ERROR,
301   .error_strings = mpls_error_strings,
302
303   .n_next_nodes = MPLS_INPUT_N_NEXT,
304   .next_nodes = {
305 #define _(s,n) [MPLS_INPUT_NEXT_##s] = n,
306     foreach_mpls_input_next
307 #undef _
308   },
309
310   .format_buffer = format_mpls_eth_header_with_length,
311   .format_trace = format_mpls_rx_trace,
312   .unformat_buffer = unformat_mpls_gre_header,
313 };
314
315 VLIB_NODE_FUNCTION_MULTIARCH (mpls_ethernet_input_node, mpls_ethernet_input)
316
317 static void
318 mpls_setup_nodes (vlib_main_t * vm)
319 {
320   vlib_node_t * n = vlib_get_node (vm, mpls_input_node.index);
321   pg_node_t * pn = pg_get_node (mpls_input_node.index);
322   mpls_input_runtime_t * rt;
323
324   n->format_buffer = format_mpls_gre_header_with_length;
325   n->unformat_buffer = unformat_mpls_gre_header;
326   pn->unformat_edit = unformat_pg_mpls_header;
327
328   rt = vlib_node_get_runtime_data (vm, mpls_input_node.index);
329   rt->last_label = (u32) ~0;
330   rt->last_inner_fib_index = 0;
331   rt->last_outer_fib_index = 0;
332   rt->mpls_main = &mpls_main;
333
334   n = vlib_get_node (vm, mpls_ethernet_input_node.index);
335
336   n->format_buffer = format_mpls_eth_header_with_length;
337
338   n->unformat_buffer = 0; /* unformat_mpls_ethernet_header; */
339
340   rt = vlib_node_get_runtime_data (vm, mpls_ethernet_input_node.index);
341   rt->last_label = (u32) ~0;
342   rt->last_inner_fib_index = 0;
343   rt->last_outer_fib_index = 0;
344   rt->mpls_main = &mpls_main;
345
346   ethernet_register_input_type (vm, ETHERNET_TYPE_MPLS_UNICAST,
347                                 mpls_ethernet_input_node.index);
348 }
349
350 static clib_error_t * mpls_input_init (vlib_main_t * vm)
351 {
352   clib_error_t * error; 
353
354   error = vlib_call_init_function (vm, mpls_init);
355   if (error)
356     clib_error_report (error);
357
358   mpls_setup_nodes (vm);
359
360   return 0;
361 }
362
363 VLIB_INIT_FUNCTION (mpls_input_init);