Initial commit of vpp code.
[vpp.git] / vnet / vnet / lisp-gpe / decap.c
1 /*
2  * decap.c: lisp-gpe decap processing
3  *
4  * Copyright (c) 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/lisp-gpe/lisp_gpe.h>
21
22 typedef struct {
23   u32 next_index;
24   u32 tunnel_index;
25   u32 error;
26   lisp_gpe_header_t h;
27 } lisp_gpe_rx_trace_t;
28
29 static u8 * format_lisp_gpe_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   lisp_gpe_rx_trace_t * t = va_arg (*args, lisp_gpe_rx_trace_t *);
34
35   if (t->tunnel_index != ~0)
36     {
37       s = format (s, "NSH-VXLAN: tunnel %d next %d error %d", t->tunnel_index, 
38                   t->next_index, t->error);
39     }
40   else
41     {
42       s = format (s, "NSH-VXLAN: no tunnel next %d error %d\n", t->next_index, 
43                   t->error);
44     }
45   s = format (s, "\n  %U", format_lisp_gpe_header_with_length, &t->h, 
46               (u32) sizeof (t->h) /* max size */);
47   return s;
48 }
49
50 static uword
51 lisp_gpe_input (vlib_main_t * vm,
52                      vlib_node_runtime_t * node,
53                      vlib_frame_t * from_frame)
54 {
55   u32 n_left_from, next_index, * from, * to_next;
56   lisp_gpe_main_t * ngm = &lisp_gpe_main;
57   u32 last_tunnel_index = ~0;
58   lisp_gpe_tunnel_key_t last_key;
59   u32 pkts_decapsulated = 0;
60
61   memset (&last_key, 0xff, sizeof (last_key));
62
63   from = vlib_frame_vector_args (from_frame);
64   n_left_from = from_frame->n_vectors;
65
66   next_index = node->cached_next_index;
67
68   while (n_left_from > 0)
69     {
70       u32 n_left_to_next;
71
72       vlib_get_next_frame (vm, node, next_index,
73                            to_next, n_left_to_next);
74
75 #if 0
76       while (n_left_from >= 4 && n_left_to_next >= 2)
77         {
78           u32 bi0, bi1;
79           vlib_buffer_t * b0, * b1;
80           nsh_unicast_header_t * h0, * h1;
81           u32 label0, label1;
82           u32 next0, next1;
83           uword * p0, * p1;
84
85           /* Prefetch next iteration. */
86           {
87             vlib_buffer_t * p2, * p3;
88
89             p2 = vlib_get_buffer (vm, from[2]);
90             p3 = vlib_get_buffer (vm, from[3]);
91
92             vlib_prefetch_buffer_header (p2, LOAD);
93             vlib_prefetch_buffer_header (p3, LOAD);
94
95             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
96             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
97           }
98
99           bi0 = from[0];
100           bi1 = from[1];
101           to_next[0] = bi0;
102           to_next[1] = bi1;
103           from += 2;
104           to_next += 2;
105           n_left_to_next -= 2;
106           n_left_from -= 2;
107
108           b0 = vlib_get_buffer (vm, bi0);
109           b1 = vlib_get_buffer (vm, bi1);
110
111           h0 = vlib_buffer_get_current (b0);
112           h1 = vlib_buffer_get_current (b1);
113           
114           next0 = next1 = LISP_GPE_INPUT_NEXT_IP4_INPUT;
115
116           label0 = clib_net_to_host_u32 (h0->label_exp_s_ttl);
117           label1 = clib_net_to_host_u32 (h1->label_exp_s_ttl);
118
119           /* 
120            * Translate label contents into a fib index.
121            * This is a decent sanity check, and guarantees
122            * a sane FIB for the downstream lookup
123            */
124           label0 = vnet_nsh_uc_get_label (label0);
125           label1 = vnet_nsh_uc_get_label (label1);
126
127           /* If 2xlabels match, and match the 1-wide cache, use it */
128           if (label0 == label1 && rt->last_label == label0)
129             {
130               vnet_buffer(b0)->sw_if_index[VLIB_TX] = rt->last_fib_index;
131               vnet_buffer(b1)->sw_if_index[VLIB_TX] = rt->last_fib_index;
132             }
133           else
134             {
135               p0 = hash_get (rt->mm->fib_index_by_nsh_label, label0);
136               if (PREDICT_FALSE (p0 == 0))
137                 {
138                   next0 = LISP_GPE_INPUT_NEXT_DROP;
139                   b0->error = node->errors[NSH_ERROR_BAD_LABEL];
140                 }
141               else
142                 vnet_buffer(b0)->sw_if_index[VLIB_TX] = p0[0];
143               
144               p1 = hash_get (rt->mm->fib_index_by_nsh_label, label1);
145               if (PREDICT_FALSE (p1 == 0))
146                 {
147                   next1 = LISP_GPE_INPUT_NEXT_DROP;
148                   b1->error = node->errors[NSH_ERROR_BAD_LABEL];
149                 }
150               else
151                 {
152                   vnet_buffer(b1)->sw_if_index[VLIB_TX] = p1[0];
153                   rt->last_fib_index = p1[0];
154                   rt->last_label = label1;
155                 }
156             }
157
158           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
159             {
160               nsh_rx_trace_t *tr = vlib_add_trace (vm, node, 
161                                                    b0, sizeof (*tr));
162               tr->label_exp_s_ttl = label0;
163             }
164           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) 
165             {
166               nsh_rx_trace_t *tr = vlib_add_trace (vm, node, 
167                                                    b1, sizeof (*tr));
168               tr->label_exp_s_ttl = label1;
169             }
170
171           vlib_buffer_advance (b0, sizeof (*h0));
172           vlib_buffer_advance (b1, sizeof (*h1));
173
174           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
175                                            to_next, n_left_to_next,
176                                            bi0, bi1, next0, next1);
177         }
178 #endif 
179     
180       while (n_left_from > 0 && n_left_to_next > 0)
181         {
182           u32 bi0;
183           vlib_buffer_t * b0;
184           u32 next0;
185           ip4_udp_lisp_gpe_header_t * iul0;
186           uword * p0;
187           u32 tunnel_index0;
188           lisp_gpe_tunnel_t * t0;
189           lisp_gpe_tunnel_key_t key0;
190           u32 error0;
191
192           bi0 = from[0];
193           to_next[0] = bi0;
194           from += 1;
195           to_next += 1;
196           n_left_from -= 1;
197           n_left_to_next -= 1;
198
199           b0 = vlib_get_buffer (vm, bi0);
200
201           /* udp leaves current_data pointing at the lisp header */
202           vlib_buffer_advance 
203             (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
204
205           iul0 = vlib_buffer_get_current (b0);
206
207           /* pop (ip, udp, lisp-gpe) */
208           vlib_buffer_advance (b0, sizeof (*iul0));
209
210           tunnel_index0 = ~0;
211           error0 = 0;
212           next0 = LISP_GPE_INPUT_NEXT_DROP;
213
214           key0.src = iul0->ip4.src_address.as_u32;
215           key0.iid = iul0->lisp.iid;
216
217           if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])))
218             {
219               p0 = hash_get_mem (ngm->lisp_gpe_tunnel_by_key, &key0);
220
221               if (p0 == 0)
222                 {
223                   error0 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
224                   goto trace0;
225                 }
226
227               last_key.as_u64[0] = key0.as_u64[0];
228               tunnel_index0 = last_tunnel_index = p0[0];
229             }
230           else
231             tunnel_index0 = last_tunnel_index;
232
233           t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
234
235           next0 = t0->decap_next_index;
236
237           /* Required to make the l2 tag push / pop code work on l2 subifs */
238           vnet_update_l2_len (b0);
239
240           /* 
241            * ip[46] lookup in the configured FIB
242            * lisp-gpe-encap, here's the encap tunnel sw_if_index
243            */
244           vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
245           pkts_decapsulated ++;
246
247         trace0:
248           b0->error = error0 ? node->errors[error0] : 0;
249
250           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
251             {
252               lisp_gpe_rx_trace_t *tr 
253                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
254               tr->next_index = next0;
255               tr->error = error0;
256               tr->tunnel_index = tunnel_index0;
257               tr->h = iul0->lisp;
258             }
259           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
260                                            to_next, n_left_to_next,
261                                            bi0, next0);
262         }
263
264       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
265     }
266   vlib_node_increment_counter (vm, lisp_gpe_input_node.index,
267                                LISP_GPE_ERROR_DECAPSULATED, 
268                                pkts_decapsulated);
269   return from_frame->n_vectors;
270 }
271
272 static char * lisp_gpe_error_strings[] = {
273 #define lisp_gpe_error(n,s) s,
274 #include <vnet/lisp-gpe/lisp_gpe_error.def>
275 #undef lisp_gpe_error
276 #undef _
277 };
278
279 VLIB_REGISTER_NODE (lisp_gpe_input_node) = {
280   .function = lisp_gpe_input,
281   .name = "lisp-gpe-input",
282   /* Takes a vector of packets. */
283   .vector_size = sizeof (u32),
284
285   .n_errors = LISP_GPE_N_ERROR,
286   .error_strings = lisp_gpe_error_strings,
287
288   .n_next_nodes = LISP_GPE_INPUT_N_NEXT,
289   .next_nodes = {
290 #define _(s,n) [LISP_GPE_INPUT_NEXT_##s] = n,
291     foreach_lisp_gpe_input_next
292 #undef _
293   },
294
295   .format_buffer = format_lisp_gpe_header_with_length,
296   .format_trace = format_lisp_gpe_rx_trace,
297   // $$$$ .unformat_buffer = unformat_lisp_gpe_header,
298 };