76003e6003fe87a80a08bd9f8ec620635d7b5446
[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   vnet_main_t * vnm = ngm->vnet_main;
63   vnet_interface_main_t * im = &vnm->interface_main;
64   u32 last_tunnel_index = ~0;
65   nsh_vxlan_gpe_tunnel_key_t last_key;
66   u32 pkts_decapsulated = 0;
67   u32 cpu_index = os_get_cpu_number();
68   u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
69
70   memset (&last_key, 0xff, sizeof (last_key));
71
72   from = vlib_frame_vector_args (from_frame);
73   n_left_from = from_frame->n_vectors;
74
75   next_index = node->cached_next_index;
76   stats_sw_if_index = node->runtime_data[0];
77   stats_n_packets = stats_n_bytes = 0;
78
79   while (n_left_from > 0)
80     {
81       u32 n_left_to_next;
82
83       vlib_get_next_frame (vm, node, next_index,
84                            to_next, n_left_to_next);
85
86       while (n_left_from >= 4 && n_left_to_next >= 2)
87         {
88           u32 bi0, bi1;
89           vlib_buffer_t * b0, * b1;
90           u32 next0, next1;
91           ip4_vxlan_gpe_and_nsh_header_t * iuvn0, * iuvn1;
92           uword * p0, * p1;
93           u32 tunnel_index0, tunnel_index1;
94           nsh_vxlan_gpe_tunnel_t * t0, * t1;
95           nsh_vxlan_gpe_tunnel_key_t key0, key1;
96           u32 error0, error1;
97           u32 sw_if_index0, sw_if_index1, len0, len1;
98
99           /* Prefetch next iteration. */
100           {
101             vlib_buffer_t * p2, * p3;
102
103             p2 = vlib_get_buffer (vm, from[2]);
104             p3 = vlib_get_buffer (vm, from[3]);
105
106             vlib_prefetch_buffer_header (p2, LOAD);
107             vlib_prefetch_buffer_header (p3, LOAD);
108
109             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
110             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
111           }
112
113           bi0 = from[0];
114           bi1 = from[1];
115           to_next[0] = bi0;
116           to_next[1] = bi1;
117           from += 2;
118           to_next += 2;
119           n_left_to_next -= 2;
120           n_left_from -= 2;
121
122           b0 = vlib_get_buffer (vm, bi0);
123           b1 = vlib_get_buffer (vm, bi1);
124
125           /* udp leaves current_data pointing at the vxlan header */
126           vlib_buffer_advance 
127             (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
128           vlib_buffer_advance 
129             (b1, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
130
131           iuvn0 = vlib_buffer_get_current (b0);
132           iuvn1 = vlib_buffer_get_current (b1);
133
134           /* pop (ip, udp, vxlan, nsh) */
135           vlib_buffer_advance (b0, sizeof (*iuvn0));
136           vlib_buffer_advance (b1, sizeof (*iuvn1));
137
138           tunnel_index0 = ~0;
139           error0 = 0;
140           next0 = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
141
142           tunnel_index1 = ~0;
143           error1 = 0;
144           next1 = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
145
146           key0.src = iuvn0->ip4.src_address.as_u32;
147           key0.vni = iuvn0->vxlan.vni_res;
148           key0.spi_si = iuvn0->nsh.spi_si;
149           key0.pad = 0;
150
151           if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])
152                              || (key0.as_u64[1] != last_key.as_u64[1])))
153             {
154               p0 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key0);
155
156               if (p0 == 0)
157                 {
158                   error0 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
159                   goto trace0;
160                 }
161
162               last_key.as_u64[0] = key0.as_u64[0];
163               last_key.as_u64[1] = key0.as_u64[1];
164               tunnel_index0 = last_tunnel_index = p0[0];
165             }
166           else
167             tunnel_index0 = last_tunnel_index;
168
169           t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
170
171           next0 = t0->decap_next_index;
172           sw_if_index0 = t0->sw_if_index;
173           len0 = vlib_buffer_length_in_chain(vm, b0);
174
175           /* Required to make the l2 tag push / pop code work on l2 subifs */
176           vnet_update_l2_len (b0);
177
178           if (next0 == NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP)
179             {
180               /*
181                * Functioning as SFF (ie "half NSH tunnel mode")
182                * If ingress (we are in decap.c) with NSH header, and 'decap next nsh-vxlan-gpe' then "NSH switch"
183                * 1. Take DST, remap to SRC, remap other keys in place
184                * 2. Look up new t0 as per above
185                * 3. Set sw_if_index[VLIB_TX] to be t0->sw_if_index
186                */
187               uword * next_p0;
188               nsh_vxlan_gpe_tunnel_t  * next_t0;
189               nsh_vxlan_gpe_tunnel_key_t next_key0;
190
191               next_key0.src = iuvn0->ip4.dst_address.as_u32;
192               next_key0.vni = iuvn0->vxlan.vni_res;
193               next_key0.spi_si = iuvn0->nsh.spi_si;
194               next_key0.pad = 0;
195
196               next_p0 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &next_key0);
197
198               if (next_p0 == 0)
199                 {
200                   error0 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
201                   goto trace0;
202                 }
203               next_t0 = pool_elt_at_index (ngm->tunnels, next_p0[0]);
204               vnet_buffer(b0)->sw_if_index[VLIB_TX] = next_t0->sw_if_index;
205
206             }
207           else
208             {
209               /*
210                * ip[46] lookup in the configured FIB
211                * nsh-vxlan-gpe-encap, here's the encap tunnel sw_if_index
212                */
213               vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
214             }
215
216           pkts_decapsulated++;
217           stats_n_packets += 1;
218           stats_n_bytes += len0;
219
220           if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
221           {
222             stats_n_packets -= 1;
223             stats_n_bytes -= len0;
224             if (stats_n_packets)
225               vlib_increment_combined_counter(
226                   im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
227                   cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
228             stats_n_packets = 1;
229             stats_n_bytes = len0;
230             stats_sw_if_index = sw_if_index0;
231           }
232
233         trace0:
234           b0->error = error0 ? node->errors[error0] : 0;
235
236           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
237             {
238               nsh_vxlan_gpe_rx_trace_t *tr
239                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
240               tr->next_index = next0;
241               tr->error = error0;
242               tr->tunnel_index = tunnel_index0;
243               tr->h = iuvn0->nsh;
244             }
245
246           key1.src = iuvn1->ip4.src_address.as_u32;
247           key1.vni = iuvn1->vxlan.vni_res;
248           key1.spi_si = iuvn1->nsh.spi_si;
249           key1.pad = 0;
250
251           if (PREDICT_FALSE ((key1.as_u64[0] != last_key.as_u64[0])
252                              || (key1.as_u64[1] != last_key.as_u64[1])))
253             {
254               p1 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key1);
255
256               if (p1 == 0)
257                 {
258                   error1 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
259                   goto trace1;
260                 }
261
262               last_key.as_u64[0] = key1.as_u64[0];
263               last_key.as_u64[1] = key1.as_u64[1];
264               tunnel_index1 = last_tunnel_index = p1[0];
265             }
266           else
267             tunnel_index1 = last_tunnel_index;
268
269           t1 = pool_elt_at_index (ngm->tunnels, tunnel_index1);
270
271           next1 = t1->decap_next_index;
272           sw_if_index1 = t1->sw_if_index;
273           len1 = vlib_buffer_length_in_chain(vm, b1);
274
275           /* Required to make the l2 tag push / pop code work on l2 subifs */
276           vnet_update_l2_len (b1);
277
278           if (next1 == NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP)
279             {
280               /*
281                * Functioning as SFF (ie "half NSH tunnel mode")
282                * If ingress (we are in decap.c) with NSH header, and 'decap next nsh-vxlan-gpe' then "NSH switch"
283                * 1. Take DST, remap to SRC, remap other keys in place
284                * 2. Look up new t0 as per above
285                * 3. Set sw_if_index[VLIB_TX] to be t0->sw_if_index
286                */
287               uword * next_p1;
288               nsh_vxlan_gpe_tunnel_t  * next_t1;
289               nsh_vxlan_gpe_tunnel_key_t next_key1;
290
291               next_key1.src = iuvn1->ip4.dst_address.as_u32;
292               next_key1.vni = iuvn1->vxlan.vni_res;
293               next_key1.spi_si = iuvn1->nsh.spi_si;
294               next_key1.pad = 0;
295
296               next_p1 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &next_key1);
297
298               if (next_p1 == 0)
299                 {
300                   error1 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
301                   goto trace1;
302                 }
303               next_t1 = pool_elt_at_index (ngm->tunnels, next_p1[0]);
304               vnet_buffer(b1)->sw_if_index[VLIB_TX] = next_t1->sw_if_index;
305
306             }
307           else
308             {
309               /*
310                * ip[46] lookup in the configured FIB
311                * nsh-vxlan-gpe-encap, here's the encap tunnel sw_if_index
312                */
313               vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
314             }
315
316           pkts_decapsulated++;
317           stats_n_packets += 1;
318           stats_n_bytes += len1;
319           /* Batch stats increment on the same vxlan tunnel so counter
320            is not incremented per packet */
321           if (PREDICT_FALSE(sw_if_index1 != stats_sw_if_index))
322           {
323             stats_n_packets -= 1;
324             stats_n_bytes -= len1;
325             if (stats_n_packets)
326               vlib_increment_combined_counter(
327                   im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
328                   cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
329             stats_n_packets = 1;
330             stats_n_bytes = len1;
331             stats_sw_if_index = sw_if_index1;
332           }
333           vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
334
335         trace1:
336           b1->error = error1 ? node->errors[error1] : 0;
337
338           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
339             {
340               nsh_vxlan_gpe_rx_trace_t *tr
341                 = vlib_add_trace (vm, node, b1, sizeof (*tr));
342               tr->next_index = next1;
343               tr->error = error1;
344               tr->tunnel_index = tunnel_index1;
345               tr->h = iuvn1->nsh;
346             }
347
348           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
349                                            to_next, n_left_to_next,
350                                            bi0, bi1, next0, next1);
351         }
352     
353       while (n_left_from > 0 && n_left_to_next > 0)
354         {
355           u32 bi0;
356           vlib_buffer_t * b0;
357           u32 next0;
358           ip4_vxlan_gpe_and_nsh_header_t * iuvn0;
359           uword * p0;
360           u32 tunnel_index0;
361           nsh_vxlan_gpe_tunnel_t * t0;
362           nsh_vxlan_gpe_tunnel_key_t key0;
363           u32 error0;
364           u32 sw_if_index0, len0;
365
366           bi0 = from[0];
367           to_next[0] = bi0;
368           from += 1;
369           to_next += 1;
370           n_left_from -= 1;
371           n_left_to_next -= 1;
372
373           b0 = vlib_get_buffer (vm, bi0);
374
375           /* udp leaves current_data pointing at the vxlan header */
376           vlib_buffer_advance 
377             (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
378
379           iuvn0 = vlib_buffer_get_current (b0);
380
381           /* pop (ip, udp, vxlan, nsh) */
382           vlib_buffer_advance (b0, sizeof (*iuvn0));
383
384           tunnel_index0 = ~0;
385           error0 = 0;
386           next0 = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
387
388           key0.src = iuvn0->ip4.src_address.as_u32;
389           key0.vni = iuvn0->vxlan.vni_res;
390           key0.spi_si = iuvn0->nsh.spi_si;
391           key0.pad = 0;
392
393           if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])
394                              || (key0.as_u64[1] != last_key.as_u64[1])))
395             {
396               p0 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key0);
397           
398               if (p0 == 0)
399                 {
400                   error0 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
401                   goto trace00;
402                 }
403
404               last_key.as_u64[0] = key0.as_u64[0];
405               last_key.as_u64[1] = key0.as_u64[1];
406               tunnel_index0 = last_tunnel_index = p0[0];
407             }
408           else
409             tunnel_index0 = last_tunnel_index;
410
411           t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
412
413           next0 = t0->decap_next_index;
414           sw_if_index0 = t0->sw_if_index;
415           len0 = vlib_buffer_length_in_chain(vm, b0);
416
417           /* Required to make the l2 tag push / pop code work on l2 subifs */
418           vnet_update_l2_len (b0);
419
420           if (next0 == NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP)
421             {
422               /*
423                * Functioning as SFF (ie "half NSH tunnel mode")
424                * If ingress (we are in decap.c) with NSH header, and 'decap next nsh-vxlan-gpe' then "NSH switch"
425                * 1. Take DST, remap to SRC, remap other keys in place
426                * 2. Look up new t0 as per above
427                * 3. Set sw_if_index[VLIB_TX] to be t0->sw_if_index
428                */
429               uword * next_p0;
430               nsh_vxlan_gpe_tunnel_t  * next_t0;
431               nsh_vxlan_gpe_tunnel_key_t next_key0;
432
433               next_key0.src = iuvn0->ip4.dst_address.as_u32;
434               next_key0.vni = iuvn0->vxlan.vni_res;
435               next_key0.spi_si = iuvn0->nsh.spi_si;
436               next_key0.pad = 0;
437
438               next_p0 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &next_key0);
439
440               if (next_p0 == 0)
441                 {
442                   error0 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
443                   goto trace00;
444                 }
445               next_t0 = pool_elt_at_index (ngm->tunnels, next_p0[0]);
446               vnet_buffer(b0)->sw_if_index[VLIB_TX] = next_t0->sw_if_index;
447               
448             } 
449           else 
450             {
451               /*
452                * ip[46] lookup in the configured FIB
453                * nsh-vxlan-gpe-encap, here's the encap tunnel sw_if_index
454                */
455               vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
456             }
457
458           pkts_decapsulated ++;
459
460           stats_n_packets += 1;
461           stats_n_bytes += len0;
462
463           /* Batch stats increment on the same nsh-vxlan-gpe tunnel so counter
464            is not incremented per packet */
465           if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
466           {
467             stats_n_packets -= 1;
468             stats_n_bytes -= len0;
469             if (stats_n_packets)
470               vlib_increment_combined_counter(
471                   im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
472                   cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
473             stats_n_packets = 1;
474             stats_n_bytes = len0;
475             stats_sw_if_index = sw_if_index0;
476           }
477
478         trace00:
479           b0->error = error0 ? node->errors[error0] : 0;
480
481           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
482             {
483               nsh_vxlan_gpe_rx_trace_t *tr
484                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
485               tr->next_index = next0;
486               tr->error = error0;
487               tr->tunnel_index = tunnel_index0;
488               tr->h = iuvn0->nsh;
489             }
490           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
491                                            to_next, n_left_to_next,
492                                            bi0, next0);
493         }
494
495       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
496     }
497   vlib_node_increment_counter (vm, nsh_vxlan_gpe_input_node.index,
498                                NSH_VXLAN_GPE_ERROR_DECAPSULATED,
499                                pkts_decapsulated);
500   /* Increment any remaining batch stats */
501   if (stats_n_packets)
502   {
503     vlib_increment_combined_counter(
504         im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, cpu_index,
505         stats_sw_if_index, stats_n_packets, stats_n_bytes);
506     node->runtime_data[0] = stats_sw_if_index;
507   }
508   return from_frame->n_vectors;
509 }
510
511 static char * nsh_vxlan_gpe_error_strings[] = {
512 #define nsh_vxlan_gpe_error(n,s) s,
513 #include <vnet/nsh-vxlan-gpe/nsh_vxlan_gpe_error.def>
514 #undef nsh_vxlan_gpe_error
515 #undef _
516 };
517
518 VLIB_REGISTER_NODE (nsh_vxlan_gpe_input_node) = {
519   .function = nsh_vxlan_gpe_input,
520   .name = "nsh-vxlan-gpe-input",
521   /* Takes a vector of packets. */
522   .vector_size = sizeof (u32),
523
524   .n_errors = NSH_VXLAN_GPE_N_ERROR,
525   .error_strings = nsh_vxlan_gpe_error_strings,
526
527   .n_next_nodes = NSH_VXLAN_GPE_INPUT_N_NEXT,
528   .next_nodes = {
529 #define _(s,n) [NSH_VXLAN_GPE_INPUT_NEXT_##s] = n,
530     foreach_nsh_vxlan_gpe_input_next
531 #undef _
532   },
533
534   .format_buffer = format_nsh_header_with_length,
535   .format_trace = format_nsh_vxlan_gpe_rx_trace,
536   // $$$$ .unformat_buffer = unformat_nsh_vxlan_gpe_header,
537 };