Initial commit of vpp code.
[vpp.git] / vnet / vnet / nsh-vxlan-gpe / decap.c
1 /*
2  * nsh.c: nsh packet 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/pg/pg.h>
20 #include <vnet/nsh-vxlan-gpe/nsh_vxlan_gpe.h>
21
22 vlib_node_registration_t nsh_vxlan_gpe_input_node;
23
24 /* From nsh-gre */
25 u8 * format_nsh_header_with_length (u8 * s, va_list * args);
26
27 typedef struct {
28   u32 next_index;
29   u32 tunnel_index;
30   u32 error;
31   nsh_header_t h;
32 } nsh_vxlan_gpe_rx_trace_t;
33
34 static u8 * format_nsh_vxlan_gpe_rx_trace (u8 * s, va_list * args)
35 {
36   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
37   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
38   nsh_vxlan_gpe_rx_trace_t * t = va_arg (*args, nsh_vxlan_gpe_rx_trace_t *);
39
40   if (t->tunnel_index != ~0)
41     {
42       s = format (s, "NSH-VXLAN: tunnel %d next %d error %d", t->tunnel_index, 
43                   t->next_index, t->error);
44     }
45   else
46     {
47       s = format (s, "NSH-VXLAN: no tunnel next %d error %d\n", t->next_index, 
48                   t->error);
49     }
50   s = format (s, "\n  %U", format_nsh_header_with_length, &t->h, 
51               (u32) sizeof (t->h) /* max size */);
52   return s;
53 }
54
55 static uword
56 nsh_vxlan_gpe_input (vlib_main_t * vm,
57                      vlib_node_runtime_t * node,
58                      vlib_frame_t * from_frame)
59 {
60   u32 n_left_from, next_index, * from, * to_next;
61   nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
62   u32 last_tunnel_index = ~0;
63   nsh_vxlan_gpe_tunnel_key_t last_key;
64   u32 pkts_decapsulated = 0;
65
66   memset (&last_key, 0xff, sizeof (last_key));
67
68   from = vlib_frame_vector_args (from_frame);
69   n_left_from = from_frame->n_vectors;
70
71   next_index = node->cached_next_index;
72
73   while (n_left_from > 0)
74     {
75       u32 n_left_to_next;
76
77       vlib_get_next_frame (vm, node, next_index,
78                            to_next, n_left_to_next);
79
80       while (n_left_from >= 4 && n_left_to_next >= 2)
81         {
82           u32 bi0, bi1;
83           vlib_buffer_t * b0, * b1;
84           u32 next0, next1;
85           ip4_vxlan_gpe_and_nsh_header_t * iuvn0, * iuvn1;
86           uword * p0, * p1;
87           u32 tunnel_index0, tunnel_index1;
88           nsh_vxlan_gpe_tunnel_t * t0, * t1;
89           nsh_vxlan_gpe_tunnel_key_t key0, key1;
90           u32 error0, error1;
91
92           /* Prefetch next iteration. */
93           {
94             vlib_buffer_t * p2, * p3;
95
96             p2 = vlib_get_buffer (vm, from[2]);
97             p3 = vlib_get_buffer (vm, from[3]);
98
99             vlib_prefetch_buffer_header (p2, LOAD);
100             vlib_prefetch_buffer_header (p3, LOAD);
101
102             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
103             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
104           }
105
106           bi0 = from[0];
107           bi1 = from[1];
108           to_next[0] = bi0;
109           to_next[1] = bi1;
110           from += 2;
111           to_next += 2;
112           n_left_to_next -= 2;
113           n_left_from -= 2;
114
115           b0 = vlib_get_buffer (vm, bi0);
116           b1 = vlib_get_buffer (vm, bi1);
117
118           /* udp leaves current_data pointing at the vxlan header */
119           vlib_buffer_advance 
120             (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
121           vlib_buffer_advance 
122             (b1, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
123
124           iuvn0 = vlib_buffer_get_current (b0);
125           iuvn1 = vlib_buffer_get_current (b1);
126
127           /* pop (ip, udp, vxlan, nsh) */
128           vlib_buffer_advance (b0, sizeof (*iuvn0));
129           vlib_buffer_advance (b1, sizeof (*iuvn1));
130
131           tunnel_index0 = ~0;
132           error0 = 0;
133           next0 = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
134
135           tunnel_index1 = ~0;
136           error1 = 0;
137           next1 = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
138
139           key0.src = iuvn0->ip4.src_address.as_u32;
140           key0.vni = iuvn0->vxlan.vni_res;
141           key0.spi_si = iuvn0->nsh.spi_si;
142           key0.pad = 0;
143
144           if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])
145                              || (key0.as_u64[1] != last_key.as_u64[1])))
146             {
147               p0 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key0);
148
149               if (p0 == 0)
150                 {
151                   error0 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
152                   goto trace0;
153                 }
154
155               last_key.as_u64[0] = key0.as_u64[0];
156               last_key.as_u64[1] = key0.as_u64[1];
157               tunnel_index0 = last_tunnel_index = p0[0];
158             }
159           else
160             tunnel_index0 = last_tunnel_index;
161
162           t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
163
164           next0 = t0->decap_next_index;
165
166           /* Required to make the l2 tag push / pop code work on l2 subifs */
167           vnet_update_l2_len (b0);
168
169           /* 
170            * ip[46] lookup in the configured FIB
171            * nsh-vxlan-gpe-encap, here's the encap tunnel sw_if_index
172            */
173           vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
174
175         trace0:
176           b0->error = error0 ? node->errors[error0] : 0;
177
178           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
179             {
180               nsh_vxlan_gpe_rx_trace_t *tr 
181                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
182               tr->next_index = next0;
183               tr->error = error0;
184               tr->tunnel_index = tunnel_index0;
185               tr->h = iuvn0->nsh;
186             }
187
188           key1.src = iuvn1->ip4.src_address.as_u32;
189           key1.vni = iuvn1->vxlan.vni_res;
190           key1.spi_si = iuvn1->nsh.spi_si;
191           key1.pad = 0;
192
193           if (PREDICT_FALSE ((key1.as_u64[0] != last_key.as_u64[0])
194                              || (key1.as_u64[1] != last_key.as_u64[1])))
195             {
196               p1 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key1);
197
198               if (p1 == 0)
199                 {
200                   error1 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
201                   goto trace1;
202                 }
203
204               last_key.as_u64[0] = key1.as_u64[0];
205               last_key.as_u64[1] = key1.as_u64[1];
206               tunnel_index1 = last_tunnel_index = p1[0];
207             }
208           else
209             tunnel_index1 = last_tunnel_index;
210
211           t1 = pool_elt_at_index (ngm->tunnels, tunnel_index1);
212
213           next1 = t1->decap_next_index;
214
215           /* Required to make the l2 tag push / pop code work on l2 subifs */
216           vnet_update_l2_len (b1);
217
218           /* 
219            * ip[46] lookup in the configured FIB
220            * nsh-vxlan-gpe-encap, here's the encap tunnel sw_if_index
221            */
222           vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
223           pkts_decapsulated += 2;
224
225         trace1:
226           b1->error = error1 ? node->errors[error1] : 0;
227
228           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) 
229             {
230               nsh_vxlan_gpe_rx_trace_t *tr 
231                 = vlib_add_trace (vm, node, b1, sizeof (*tr));
232               tr->next_index = next1;
233               tr->error = error1;
234               tr->tunnel_index = tunnel_index1;
235               tr->h = iuvn1->nsh;
236             }
237
238           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
239                                            to_next, n_left_to_next,
240                                            bi0, bi1, next0, next1);
241         }
242     
243       while (n_left_from > 0 && n_left_to_next > 0)
244         {
245           u32 bi0;
246           vlib_buffer_t * b0;
247           u32 next0;
248           ip4_vxlan_gpe_and_nsh_header_t * iuvn0;
249           uword * p0;
250           u32 tunnel_index0;
251           nsh_vxlan_gpe_tunnel_t * t0;
252           nsh_vxlan_gpe_tunnel_key_t key0;
253           u32 error0;
254
255           bi0 = from[0];
256           to_next[0] = bi0;
257           from += 1;
258           to_next += 1;
259           n_left_from -= 1;
260           n_left_to_next -= 1;
261
262           b0 = vlib_get_buffer (vm, bi0);
263
264           /* udp leaves current_data pointing at the vxlan header */
265           vlib_buffer_advance 
266             (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
267
268           iuvn0 = vlib_buffer_get_current (b0);
269
270           /* pop (ip, udp, vxlan, nsh) */
271           vlib_buffer_advance (b0, sizeof (*iuvn0));
272
273           tunnel_index0 = ~0;
274           error0 = 0;
275           next0 = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
276
277           key0.src = iuvn0->ip4.src_address.as_u32;
278           key0.vni = iuvn0->vxlan.vni_res;
279           key0.spi_si = iuvn0->nsh.spi_si;
280           key0.pad = 0;
281
282           if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])
283                              || (key0.as_u64[1] != last_key.as_u64[1])))
284             {
285               p0 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key0);
286
287               if (p0 == 0)
288                 {
289                   error0 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
290                   goto trace00;
291                 }
292
293               last_key.as_u64[0] = key0.as_u64[0];
294               last_key.as_u64[1] = key0.as_u64[1];
295               tunnel_index0 = last_tunnel_index = p0[0];
296             }
297           else
298             tunnel_index0 = last_tunnel_index;
299
300           t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
301
302           next0 = t0->decap_next_index;
303
304           /* Required to make the l2 tag push / pop code work on l2 subifs */
305           vnet_update_l2_len (b0);
306
307           /* 
308            * ip[46] lookup in the configured FIB
309            * nsh-vxlan-gpe-encap, here's the encap tunnel sw_if_index
310            */
311           vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
312           pkts_decapsulated ++;
313
314         trace00:
315           b0->error = error0 ? node->errors[error0] : 0;
316
317           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
318             {
319               nsh_vxlan_gpe_rx_trace_t *tr 
320                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
321               tr->next_index = next0;
322               tr->error = error0;
323               tr->tunnel_index = tunnel_index0;
324               tr->h = iuvn0->nsh;
325             }
326           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
327                                            to_next, n_left_to_next,
328                                            bi0, next0);
329         }
330
331       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
332     }
333   vlib_node_increment_counter (vm, nsh_vxlan_gpe_input_node.index,
334                                NSH_VXLAN_GPE_ERROR_DECAPSULATED, 
335                                pkts_decapsulated);
336   return from_frame->n_vectors;
337 }
338
339 static char * nsh_vxlan_gpe_error_strings[] = {
340 #define nsh_vxlan_gpe_error(n,s) s,
341 #include <vnet/nsh-vxlan-gpe/nsh_vxlan_gpe_error.def>
342 #undef nsh_vxlan_gpe_error
343 #undef _
344 };
345
346 VLIB_REGISTER_NODE (nsh_vxlan_gpe_input_node) = {
347   .function = nsh_vxlan_gpe_input,
348   .name = "nsh-vxlan-gpe-input",
349   /* Takes a vector of packets. */
350   .vector_size = sizeof (u32),
351
352   .n_errors = NSH_VXLAN_GPE_N_ERROR,
353   .error_strings = nsh_vxlan_gpe_error_strings,
354
355   .n_next_nodes = NSH_VXLAN_GPE_INPUT_N_NEXT,
356   .next_nodes = {
357 #define _(s,n) [NSH_VXLAN_GPE_INPUT_NEXT_##s] = n,
358     foreach_nsh_vxlan_gpe_input_next
359 #undef _
360   },
361
362   .format_buffer = format_nsh_header_with_length,
363   .format_trace = format_nsh_vxlan_gpe_rx_trace,
364   // $$$$ .unformat_buffer = unformat_nsh_vxlan_gpe_header,
365 };