misc: Purge unused pg includes
[vpp.git] / src / vnet / vxlan / decap.c
1 /*
2  * decap.c: vxlan tunnel decap 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/vxlan/vxlan.h>
20
21 #ifndef CLIB_MARCH_VARIANT
22 vlib_node_registration_t vxlan4_input_node;
23 vlib_node_registration_t vxlan6_input_node;
24 #endif
25
26 typedef struct
27 {
28   u32 next_index;
29   u32 tunnel_index;
30   u32 error;
31   u32 vni;
32 } vxlan_rx_trace_t;
33
34 static u8 *
35 format_vxlan_rx_trace (u8 * s, va_list * args)
36 {
37   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
38   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
39   vxlan_rx_trace_t *t = va_arg (*args, vxlan_rx_trace_t *);
40
41   if (t->tunnel_index == ~0)
42     return format (s, "VXLAN decap error - tunnel for vni %d does not exist",
43                    t->vni);
44   return format (s, "VXLAN decap from vxlan_tunnel%d vni %d next %d error %d",
45                  t->tunnel_index, t->vni, t->next_index, t->error);
46 }
47
48 typedef vxlan4_tunnel_key_t last_tunnel_cache4;
49
50 static const vxlan_decap_info_t decap_not_found = {
51   .sw_if_index = ~0,
52   .next_index = VXLAN_INPUT_NEXT_DROP,
53   .error = VXLAN_ERROR_NO_SUCH_TUNNEL
54 };
55
56 static const vxlan_decap_info_t decap_bad_flags = {
57   .sw_if_index = ~0,
58   .next_index = VXLAN_INPUT_NEXT_DROP,
59   .error = VXLAN_ERROR_BAD_FLAGS
60 };
61
62 always_inline vxlan_decap_info_t
63 vxlan4_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache4 * cache,
64                     u32 fib_index, ip4_header_t * ip4_0,
65                     vxlan_header_t * vxlan0, u32 * stats_sw_if_index)
66 {
67   if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
68     return decap_bad_flags;
69
70   /* Make sure VXLAN tunnel exist according to packet S/D IP, VRF, and VNI */
71   u32 dst = ip4_0->dst_address.as_u32;
72   u32 src = ip4_0->src_address.as_u32;
73   vxlan4_tunnel_key_t key4 = {
74     .key[0] = ((u64) dst << 32) | src,
75     .key[1] = ((u64) fib_index << 32) | vxlan0->vni_reserved,
76   };
77
78   if (PREDICT_TRUE
79       (key4.key[0] == cache->key[0] && key4.key[1] == cache->key[1]))
80     {
81       /* cache hit */
82       vxlan_decap_info_t di = {.as_u64 = cache->value };
83       *stats_sw_if_index = di.sw_if_index;
84       return di;
85     }
86
87   int rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
88   if (PREDICT_TRUE (rv == 0))
89     {
90       *cache = key4;
91       vxlan_decap_info_t di = {.as_u64 = key4.value };
92       *stats_sw_if_index = di.sw_if_index;
93       return di;
94     }
95
96   /* try multicast */
97   if (PREDICT_TRUE (!ip4_address_is_multicast (&ip4_0->dst_address)))
98     return decap_not_found;
99
100   /* search for mcast decap info by mcast address */
101   key4.key[0] = dst;
102   rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
103   if (rv != 0)
104     return decap_not_found;
105
106   /* search for unicast tunnel using the mcast tunnel local(src) ip */
107   vxlan_decap_info_t mdi = {.as_u64 = key4.value };
108   key4.key[0] = ((u64) mdi.local_ip.as_u32 << 32) | src;
109   rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
110   if (PREDICT_FALSE (rv != 0))
111     return decap_not_found;
112
113   /* mcast traffic does not update the cache */
114   *stats_sw_if_index = mdi.sw_if_index;
115   vxlan_decap_info_t di = {.as_u64 = key4.value };
116   return di;
117 }
118
119 typedef vxlan6_tunnel_key_t last_tunnel_cache6;
120
121 always_inline vxlan_decap_info_t
122 vxlan6_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache6 * cache,
123                     u32 fib_index, ip6_header_t * ip6_0,
124                     vxlan_header_t * vxlan0, u32 * stats_sw_if_index)
125 {
126   if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
127     return decap_bad_flags;
128
129   /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
130   vxlan6_tunnel_key_t key6 = {
131     .key[0] = ip6_0->src_address.as_u64[0],
132     .key[1] = ip6_0->src_address.as_u64[1],
133     .key[2] = (((u64) fib_index) << 32) | vxlan0->vni_reserved,
134   };
135
136   if (PREDICT_FALSE
137       (clib_bihash_key_compare_24_8 (key6.key, cache->key) == 0))
138     {
139       int rv =
140         clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6);
141       if (PREDICT_FALSE (rv != 0))
142         return decap_not_found;
143
144       *cache = key6;
145     }
146   vxlan_tunnel_t *t0 = pool_elt_at_index (vxm->tunnels, cache->value);
147
148   /* Validate VXLAN tunnel SIP against packet DIP */
149   if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address, &t0->src.ip6)))
150     *stats_sw_if_index = t0->sw_if_index;
151   else
152     {
153       /* try multicast */
154       if (PREDICT_TRUE (!ip6_address_is_multicast (&ip6_0->dst_address)))
155         return decap_not_found;
156
157       /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */
158       key6.key[0] = ip6_0->dst_address.as_u64[0];
159       key6.key[1] = ip6_0->dst_address.as_u64[1];
160       int rv =
161         clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6);
162       if (PREDICT_FALSE (rv != 0))
163         return decap_not_found;
164
165       vxlan_tunnel_t *mcast_t0 = pool_elt_at_index (vxm->tunnels, key6.value);
166       *stats_sw_if_index = mcast_t0->sw_if_index;
167     }
168
169   vxlan_decap_info_t di = {
170     .sw_if_index = t0->sw_if_index,
171     .next_index = t0->decap_next_index,
172   };
173   return di;
174 }
175
176 always_inline uword
177 vxlan_input (vlib_main_t * vm,
178              vlib_node_runtime_t * node,
179              vlib_frame_t * from_frame, u32 is_ip4)
180 {
181   vxlan_main_t *vxm = &vxlan_main;
182   vnet_main_t *vnm = vxm->vnet_main;
183   vnet_interface_main_t *im = &vnm->interface_main;
184   vlib_combined_counter_main_t *rx_counter =
185     im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX;
186   last_tunnel_cache4 last4;
187   last_tunnel_cache6 last6;
188   u32 pkts_dropped = 0;
189   u32 thread_index = vlib_get_thread_index ();
190
191   if (is_ip4)
192     clib_memset (&last4, 0xff, sizeof last4);
193   else
194     clib_memset (&last6, 0xff, sizeof last6);
195
196   u32 *from = vlib_frame_vector_args (from_frame);
197   u32 n_left_from = from_frame->n_vectors;
198
199   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
200   vlib_get_buffers (vm, from, bufs, n_left_from);
201
202   u32 stats_if0 = ~0, stats_if1 = ~0;
203   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
204   while (n_left_from >= 4)
205     {
206       /* Prefetch next iteration. */
207       vlib_prefetch_buffer_header (b[2], LOAD);
208       vlib_prefetch_buffer_header (b[3], LOAD);
209
210       /* udp leaves current_data pointing at the vxlan header */
211       void *cur0 = vlib_buffer_get_current (b[0]);
212       void *cur1 = vlib_buffer_get_current (b[1]);
213       vxlan_header_t *vxlan0 = cur0;
214       vxlan_header_t *vxlan1 = cur1;
215
216
217       ip4_header_t *ip4_0, *ip4_1;
218       ip6_header_t *ip6_0, *ip6_1;
219       if (is_ip4)
220         {
221           ip4_0 = cur0 - sizeof (udp_header_t) - sizeof (ip4_header_t);
222           ip4_1 = cur1 - sizeof (udp_header_t) - sizeof (ip4_header_t);
223         }
224       else
225         {
226           ip6_0 = cur0 - sizeof (udp_header_t) - sizeof (ip6_header_t);
227           ip6_1 = cur1 - sizeof (udp_header_t) - sizeof (ip6_header_t);
228         }
229
230       /* pop vxlan */
231       vlib_buffer_advance (b[0], sizeof *vxlan0);
232       vlib_buffer_advance (b[1], sizeof *vxlan1);
233
234       u32 fi0 = vlib_buffer_get_ip_fib_index (b[0], is_ip4);
235       u32 fi1 = vlib_buffer_get_ip_fib_index (b[1], is_ip4);
236
237       vxlan_decap_info_t di0 = is_ip4 ?
238         vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_if0) :
239         vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_if0);
240       vxlan_decap_info_t di1 = is_ip4 ?
241         vxlan4_find_tunnel (vxm, &last4, fi1, ip4_1, vxlan1, &stats_if1) :
242         vxlan6_find_tunnel (vxm, &last6, fi1, ip6_1, vxlan1, &stats_if1);
243
244       /* Prefetch next iteration. */
245       CLIB_PREFETCH (b[2]->data, CLIB_CACHE_LINE_BYTES, LOAD);
246       CLIB_PREFETCH (b[3]->data, CLIB_CACHE_LINE_BYTES, LOAD);
247
248       u32 len0 = vlib_buffer_length_in_chain (vm, b[0]);
249       u32 len1 = vlib_buffer_length_in_chain (vm, b[1]);
250
251       next[0] = di0.next_index;
252       next[1] = di1.next_index;
253
254       u8 any_error = di0.error | di1.error;
255       if (PREDICT_TRUE (any_error == 0))
256         {
257           /* Required to make the l2 tag push / pop code work on l2 subifs */
258           vnet_update_l2_len (b[0]);
259           vnet_update_l2_len (b[1]);
260           /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
261           vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
262           vnet_buffer (b[1])->sw_if_index[VLIB_RX] = di1.sw_if_index;
263           vlib_increment_combined_counter (rx_counter, thread_index,
264                                            stats_if0, 1, len0);
265           vlib_increment_combined_counter (rx_counter, thread_index,
266                                            stats_if1, 1, len1);
267         }
268       else
269         {
270           if (di0.error == 0)
271             {
272               vnet_update_l2_len (b[0]);
273               vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
274               vlib_increment_combined_counter (rx_counter, thread_index,
275                                                stats_if0, 1, len0);
276             }
277           else
278             {
279               b[0]->error = node->errors[di0.error];
280               pkts_dropped++;
281             }
282
283           if (di1.error == 0)
284             {
285               vnet_update_l2_len (b[1]);
286               vnet_buffer (b[1])->sw_if_index[VLIB_RX] = di1.sw_if_index;
287               vlib_increment_combined_counter (rx_counter, thread_index,
288                                                stats_if1, 1, len1);
289             }
290           else
291             {
292               b[1]->error = node->errors[di1.error];
293               pkts_dropped++;
294             }
295         }
296
297       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
298         {
299           vxlan_rx_trace_t *tr =
300             vlib_add_trace (vm, node, b[0], sizeof (*tr));
301           tr->next_index = next[0];
302           tr->error = di0.error;
303           tr->tunnel_index = di0.sw_if_index == ~0 ?
304             ~0 : vxm->tunnel_index_by_sw_if_index[di0.sw_if_index];
305           tr->vni = vnet_get_vni (vxlan0);
306         }
307       if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
308         {
309           vxlan_rx_trace_t *tr =
310             vlib_add_trace (vm, node, b[1], sizeof (*tr));
311           tr->next_index = next[1];
312           tr->error = di1.error;
313           tr->tunnel_index = di1.sw_if_index == ~0 ?
314             ~0 : vxm->tunnel_index_by_sw_if_index[di1.sw_if_index];
315           tr->vni = vnet_get_vni (vxlan1);
316         }
317       b += 2;
318       next += 2;
319       n_left_from -= 2;
320     }
321
322   while (n_left_from > 0)
323     {
324       /* udp leaves current_data pointing at the vxlan header */
325       void *cur0 = vlib_buffer_get_current (b[0]);
326       vxlan_header_t *vxlan0 = cur0;
327       ip4_header_t *ip4_0;
328       ip6_header_t *ip6_0;
329       if (is_ip4)
330         ip4_0 = cur0 - sizeof (udp_header_t) - sizeof (ip4_header_t);
331       else
332         ip6_0 = cur0 - sizeof (udp_header_t) - sizeof (ip6_header_t);
333
334       /* pop (ip, udp, vxlan) */
335       vlib_buffer_advance (b[0], sizeof (*vxlan0));
336
337       u32 fi0 = vlib_buffer_get_ip_fib_index (b[0], is_ip4);
338
339       vxlan_decap_info_t di0 = is_ip4 ?
340         vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_if0) :
341         vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_if0);
342
343       uword len0 = vlib_buffer_length_in_chain (vm, b[0]);
344
345       next[0] = di0.next_index;
346
347       /* Validate VXLAN tunnel encap-fib index against packet */
348       if (di0.error == 0)
349         {
350           /* Required to make the l2 tag push / pop code work on l2 subifs */
351           vnet_update_l2_len (b[0]);
352
353           /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
354           vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
355
356           vlib_increment_combined_counter (rx_counter, thread_index,
357                                            stats_if0, 1, len0);
358         }
359       else
360         {
361           b[0]->error = node->errors[di0.error];
362           pkts_dropped++;
363         }
364
365       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
366         {
367           vxlan_rx_trace_t *tr
368             = vlib_add_trace (vm, node, b[0], sizeof (*tr));
369           tr->next_index = next[0];
370           tr->error = di0.error;
371           tr->tunnel_index = di0.sw_if_index == ~0 ?
372             ~0 : vxm->tunnel_index_by_sw_if_index[di0.sw_if_index];
373           tr->vni = vnet_get_vni (vxlan0);
374         }
375       b += 1;
376       next += 1;
377       n_left_from -= 1;
378     }
379   vlib_buffer_enqueue_to_next (vm, node, from, nexts, from_frame->n_vectors);
380   /* Do we still need this now that tunnel tx stats is kept? */
381   u32 node_idx = is_ip4 ? vxlan4_input_node.index : vxlan6_input_node.index;
382   vlib_node_increment_counter (vm, node_idx, VXLAN_ERROR_DECAPSULATED,
383                                from_frame->n_vectors - pkts_dropped);
384
385   return from_frame->n_vectors;
386 }
387
388 VLIB_NODE_FN (vxlan4_input_node) (vlib_main_t * vm,
389                                   vlib_node_runtime_t * node,
390                                   vlib_frame_t * from_frame)
391 {
392   return vxlan_input (vm, node, from_frame, /* is_ip4 */ 1);
393 }
394
395 VLIB_NODE_FN (vxlan6_input_node) (vlib_main_t * vm,
396                                   vlib_node_runtime_t * node,
397                                   vlib_frame_t * from_frame)
398 {
399   return vxlan_input (vm, node, from_frame, /* is_ip4 */ 0);
400 }
401
402 static char *vxlan_error_strings[] = {
403 #define vxlan_error(n,s) s,
404 #include <vnet/vxlan/vxlan_error.def>
405 #undef vxlan_error
406 };
407
408 /* *INDENT-OFF* */
409 VLIB_REGISTER_NODE (vxlan4_input_node) =
410 {
411   .name = "vxlan4-input",
412   .vector_size = sizeof (u32),
413   .n_errors = VXLAN_N_ERROR,
414   .error_strings = vxlan_error_strings,
415   .n_next_nodes = VXLAN_INPUT_N_NEXT,
416   .format_trace = format_vxlan_rx_trace,
417   .next_nodes = {
418 #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
419     foreach_vxlan_input_next
420 #undef _
421   },
422 };
423
424 VLIB_REGISTER_NODE (vxlan6_input_node) =
425 {
426   .name = "vxlan6-input",
427   .vector_size = sizeof (u32),
428   .n_errors = VXLAN_N_ERROR,
429   .error_strings = vxlan_error_strings,
430   .n_next_nodes = VXLAN_INPUT_N_NEXT,
431   .next_nodes = {
432 #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
433     foreach_vxlan_input_next
434 #undef _
435   },
436   .format_trace = format_vxlan_rx_trace,
437 };
438 /* *INDENT-ON* */
439
440 typedef enum
441 {
442   IP_VXLAN_BYPASS_NEXT_DROP,
443   IP_VXLAN_BYPASS_NEXT_VXLAN,
444   IP_VXLAN_BYPASS_N_NEXT,
445 } ip_vxlan_bypass_next_t;
446
447 always_inline uword
448 ip_vxlan_bypass_inline (vlib_main_t * vm,
449                         vlib_node_runtime_t * node,
450                         vlib_frame_t * frame, u32 is_ip4)
451 {
452   vxlan_main_t *vxm = &vxlan_main;
453   u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
454   vlib_node_runtime_t *error_node =
455     vlib_node_get_runtime (vm, ip4_input_node.index);
456   vtep4_key_t last_vtep4;       /* last IPv4 address / fib index
457                                    matching a local VTEP address */
458   vtep6_key_t last_vtep6;       /* last IPv6 address / fib index
459                                    matching a local VTEP address */
460   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
461
462 #ifdef CLIB_HAVE_VEC512
463   vtep4_cache_t vtep4_u512;
464   clib_memset (&vtep4_u512, 0, sizeof (vtep4_u512));
465 #endif
466
467   from = vlib_frame_vector_args (frame);
468   n_left_from = frame->n_vectors;
469   next_index = node->cached_next_index;
470
471   vlib_get_buffers (vm, from, bufs, n_left_from);
472
473   if (node->flags & VLIB_NODE_FLAG_TRACE)
474     ip4_forward_next_trace (vm, node, frame, VLIB_TX);
475
476   if (is_ip4)
477     vtep4_key_init (&last_vtep4);
478   else
479     vtep6_key_init (&last_vtep6);
480
481   while (n_left_from > 0)
482     {
483       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
484
485       while (n_left_from >= 4 && n_left_to_next >= 2)
486         {
487           vlib_buffer_t *b0, *b1;
488           ip4_header_t *ip40, *ip41;
489           ip6_header_t *ip60, *ip61;
490           udp_header_t *udp0, *udp1;
491           u32 bi0, ip_len0, udp_len0, flags0, next0;
492           u32 bi1, ip_len1, udp_len1, flags1, next1;
493           i32 len_diff0, len_diff1;
494           u8 error0, good_udp0, proto0;
495           u8 error1, good_udp1, proto1;
496
497           /* Prefetch next iteration. */
498           {
499             vlib_prefetch_buffer_header (b[2], LOAD);
500             vlib_prefetch_buffer_header (b[3], LOAD);
501
502             CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
503             CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
504           }
505
506           bi0 = to_next[0] = from[0];
507           bi1 = to_next[1] = from[1];
508           from += 2;
509           n_left_from -= 2;
510           to_next += 2;
511           n_left_to_next -= 2;
512
513           b0 = b[0];
514           b1 = b[1];
515           b += 2;
516           if (is_ip4)
517             {
518               ip40 = vlib_buffer_get_current (b0);
519               ip41 = vlib_buffer_get_current (b1);
520             }
521           else
522             {
523               ip60 = vlib_buffer_get_current (b0);
524               ip61 = vlib_buffer_get_current (b1);
525             }
526
527           /* Setup packet for next IP feature */
528           vnet_feature_next (&next0, b0);
529           vnet_feature_next (&next1, b1);
530
531           if (is_ip4)
532             {
533               /* Treat IP frag packets as "experimental" protocol for now
534                  until support of IP frag reassembly is implemented */
535               proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol;
536               proto1 = ip4_is_fragment (ip41) ? 0xfe : ip41->protocol;
537             }
538           else
539             {
540               proto0 = ip60->protocol;
541               proto1 = ip61->protocol;
542             }
543
544           /* Process packet 0 */
545           if (proto0 != IP_PROTOCOL_UDP)
546             goto exit0;         /* not UDP packet */
547
548           if (is_ip4)
549             udp0 = ip4_next_header (ip40);
550           else
551             udp0 = ip6_next_header (ip60);
552
553           if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
554             goto exit0;         /* not VXLAN packet */
555
556           /* Validate DIP against VTEPs */
557           if (is_ip4)
558             {
559 #ifdef CLIB_HAVE_VEC512
560               if (!vtep4_check_vector
561                   (&vxm->vtep_table, b0, ip40, &last_vtep4, &vtep4_u512))
562 #else
563               if (!vtep4_check (&vxm->vtep_table, b0, ip40, &last_vtep4))
564 #endif
565                 goto exit0;     /* no local VTEP for VXLAN packet */
566             }
567           else
568             {
569               if (!vtep6_check (&vxm->vtep_table, b0, ip60, &last_vtep6))
570                 goto exit0;     /* no local VTEP for VXLAN packet */
571             }
572
573           flags0 = b0->flags;
574           good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
575
576           /* Don't verify UDP checksum for packets with explicit zero checksum. */
577           good_udp0 |= udp0->checksum == 0;
578
579           /* Verify UDP length */
580           if (is_ip4)
581             ip_len0 = clib_net_to_host_u16 (ip40->length);
582           else
583             ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
584           udp_len0 = clib_net_to_host_u16 (udp0->length);
585           len_diff0 = ip_len0 - udp_len0;
586
587           /* Verify UDP checksum */
588           if (PREDICT_FALSE (!good_udp0))
589             {
590               if (is_ip4)
591                 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
592               else
593                 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
594               good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
595             }
596
597           if (is_ip4)
598             {
599               error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
600               error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
601             }
602           else
603             {
604               error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
605               error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
606             }
607
608           next0 = error0 ?
609             IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
610           b0->error = error0 ? error_node->errors[error0] : 0;
611
612           /* vxlan-input node expect current at VXLAN header */
613           if (is_ip4)
614             vlib_buffer_advance (b0,
615                                  sizeof (ip4_header_t) +
616                                  sizeof (udp_header_t));
617           else
618             vlib_buffer_advance (b0,
619                                  sizeof (ip6_header_t) +
620                                  sizeof (udp_header_t));
621
622         exit0:
623           /* Process packet 1 */
624           if (proto1 != IP_PROTOCOL_UDP)
625             goto exit1;         /* not UDP packet */
626
627           if (is_ip4)
628             udp1 = ip4_next_header (ip41);
629           else
630             udp1 = ip6_next_header (ip61);
631
632           if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
633             goto exit1;         /* not VXLAN packet */
634
635           /* Validate DIP against VTEPs */
636           if (is_ip4)
637             {
638 #ifdef CLIB_HAVE_VEC512
639               if (!vtep4_check_vector
640                   (&vxm->vtep_table, b1, ip41, &last_vtep4, &vtep4_u512))
641 #else
642               if (!vtep4_check (&vxm->vtep_table, b1, ip41, &last_vtep4))
643 #endif
644                 goto exit1;     /* no local VTEP for VXLAN packet */
645             }
646           else
647             {
648               if (!vtep6_check (&vxm->vtep_table, b1, ip61, &last_vtep6))
649                 goto exit1;     /* no local VTEP for VXLAN packet */
650             }
651
652           flags1 = b1->flags;
653           good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
654
655           /* Don't verify UDP checksum for packets with explicit zero checksum. */
656           good_udp1 |= udp1->checksum == 0;
657
658           /* Verify UDP length */
659           if (is_ip4)
660             ip_len1 = clib_net_to_host_u16 (ip41->length);
661           else
662             ip_len1 = clib_net_to_host_u16 (ip61->payload_length);
663           udp_len1 = clib_net_to_host_u16 (udp1->length);
664           len_diff1 = ip_len1 - udp_len1;
665
666           /* Verify UDP checksum */
667           if (PREDICT_FALSE (!good_udp1))
668             {
669               if (is_ip4)
670                 flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
671               else
672                 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
673               good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
674             }
675
676           if (is_ip4)
677             {
678               error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM;
679               error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH;
680             }
681           else
682             {
683               error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM;
684               error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH;
685             }
686
687           next1 = error1 ?
688             IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
689           b1->error = error1 ? error_node->errors[error1] : 0;
690
691           /* vxlan-input node expect current at VXLAN header */
692           if (is_ip4)
693             vlib_buffer_advance (b1,
694                                  sizeof (ip4_header_t) +
695                                  sizeof (udp_header_t));
696           else
697             vlib_buffer_advance (b1,
698                                  sizeof (ip6_header_t) +
699                                  sizeof (udp_header_t));
700
701         exit1:
702           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
703                                            to_next, n_left_to_next,
704                                            bi0, bi1, next0, next1);
705         }
706
707       while (n_left_from > 0 && n_left_to_next > 0)
708         {
709           vlib_buffer_t *b0;
710           ip4_header_t *ip40;
711           ip6_header_t *ip60;
712           udp_header_t *udp0;
713           u32 bi0, ip_len0, udp_len0, flags0, next0;
714           i32 len_diff0;
715           u8 error0, good_udp0, proto0;
716
717           bi0 = to_next[0] = from[0];
718           from += 1;
719           n_left_from -= 1;
720           to_next += 1;
721           n_left_to_next -= 1;
722
723           b0 = b[0];
724           b++;
725           if (is_ip4)
726             ip40 = vlib_buffer_get_current (b0);
727           else
728             ip60 = vlib_buffer_get_current (b0);
729
730           /* Setup packet for next IP feature */
731           vnet_feature_next (&next0, b0);
732
733           if (is_ip4)
734             /* Treat IP4 frag packets as "experimental" protocol for now
735                until support of IP frag reassembly is implemented */
736             proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol;
737           else
738             proto0 = ip60->protocol;
739
740           if (proto0 != IP_PROTOCOL_UDP)
741             goto exit;          /* not UDP packet */
742
743           if (is_ip4)
744             udp0 = ip4_next_header (ip40);
745           else
746             udp0 = ip6_next_header (ip60);
747
748           if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
749             goto exit;          /* not VXLAN packet */
750
751           /* Validate DIP against VTEPs */
752           if (is_ip4)
753             {
754 #ifdef CLIB_HAVE_VEC512
755               if (!vtep4_check_vector
756                   (&vxm->vtep_table, b0, ip40, &last_vtep4, &vtep4_u512))
757 #else
758               if (!vtep4_check (&vxm->vtep_table, b0, ip40, &last_vtep4))
759 #endif
760                 goto exit;      /* no local VTEP for VXLAN packet */
761             }
762           else
763             {
764               if (!vtep6_check (&vxm->vtep_table, b0, ip60, &last_vtep6))
765                 goto exit;      /* no local VTEP for VXLAN packet */
766             }
767
768           flags0 = b0->flags;
769           good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
770
771           /* Don't verify UDP checksum for packets with explicit zero checksum. */
772           good_udp0 |= udp0->checksum == 0;
773
774           /* Verify UDP length */
775           if (is_ip4)
776             ip_len0 = clib_net_to_host_u16 (ip40->length);
777           else
778             ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
779           udp_len0 = clib_net_to_host_u16 (udp0->length);
780           len_diff0 = ip_len0 - udp_len0;
781
782           /* Verify UDP checksum */
783           if (PREDICT_FALSE (!good_udp0))
784             {
785               if (is_ip4)
786                 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
787               else
788                 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
789               good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
790             }
791
792           if (is_ip4)
793             {
794               error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
795               error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
796             }
797           else
798             {
799               error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
800               error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
801             }
802
803           next0 = error0 ?
804             IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
805           b0->error = error0 ? error_node->errors[error0] : 0;
806
807           /* vxlan-input node expect current at VXLAN header */
808           if (is_ip4)
809             vlib_buffer_advance (b0,
810                                  sizeof (ip4_header_t) +
811                                  sizeof (udp_header_t));
812           else
813             vlib_buffer_advance (b0,
814                                  sizeof (ip6_header_t) +
815                                  sizeof (udp_header_t));
816
817         exit:
818           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
819                                            to_next, n_left_to_next,
820                                            bi0, next0);
821         }
822
823       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
824     }
825
826   return frame->n_vectors;
827 }
828
829 VLIB_NODE_FN (ip4_vxlan_bypass_node) (vlib_main_t * vm,
830                                       vlib_node_runtime_t * node,
831                                       vlib_frame_t * frame)
832 {
833   return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 1);
834 }
835
836 /* *INDENT-OFF* */
837 VLIB_REGISTER_NODE (ip4_vxlan_bypass_node) =
838 {
839   .name = "ip4-vxlan-bypass",
840   .vector_size = sizeof (u32),
841   .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
842   .next_nodes = {
843           [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
844           [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan4-input",
845   },
846   .format_buffer = format_ip4_header,
847   .format_trace = format_ip4_forward_next_trace,
848 };
849
850 /* *INDENT-ON* */
851
852 /* Dummy init function to get us linked in. */
853 static clib_error_t *
854 ip4_vxlan_bypass_init (vlib_main_t * vm)
855 {
856   return 0;
857 }
858
859 VLIB_INIT_FUNCTION (ip4_vxlan_bypass_init);
860
861 VLIB_NODE_FN (ip6_vxlan_bypass_node) (vlib_main_t * vm,
862                                       vlib_node_runtime_t * node,
863                                       vlib_frame_t * frame)
864 {
865   return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 0);
866 }
867
868 /* *INDENT-OFF* */
869 VLIB_REGISTER_NODE (ip6_vxlan_bypass_node) =
870 {
871   .name = "ip6-vxlan-bypass",
872   .vector_size = sizeof (u32),
873   .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
874   .next_nodes = {
875     [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
876     [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan6-input",
877   },
878   .format_buffer = format_ip6_header,
879   .format_trace = format_ip6_forward_next_trace,
880 };
881
882 /* *INDENT-ON* */
883
884 /* Dummy init function to get us linked in. */
885 static clib_error_t *
886 ip6_vxlan_bypass_init (vlib_main_t * vm)
887 {
888   return 0;
889 }
890
891 VLIB_INIT_FUNCTION (ip6_vxlan_bypass_init);
892
893 #define foreach_vxlan_flow_input_next        \
894 _(DROP, "error-drop")                           \
895 _(L2_INPUT, "l2-input")
896
897 typedef enum
898 {
899 #define _(s,n) VXLAN_FLOW_NEXT_##s,
900   foreach_vxlan_flow_input_next
901 #undef _
902     VXLAN_FLOW_N_NEXT,
903 } vxlan_flow_input_next_t;
904
905 #define foreach_vxlan_flow_error                                        \
906   _(NONE, "no error")                                                   \
907   _(IP_CHECKSUM_ERROR, "Rx ip checksum errors")                         \
908   _(IP_HEADER_ERROR, "Rx ip header errors")                             \
909   _(UDP_CHECKSUM_ERROR, "Rx udp checksum errors")                               \
910   _(UDP_LENGTH_ERROR, "Rx udp length errors")
911
912 typedef enum
913 {
914 #define _(f,s) VXLAN_FLOW_ERROR_##f,
915   foreach_vxlan_flow_error
916 #undef _
917     VXLAN_FLOW_N_ERROR,
918 } vxlan_flow_error_t;
919
920 static char *vxlan_flow_error_strings[] = {
921 #define _(n,s) s,
922   foreach_vxlan_flow_error
923 #undef _
924 };
925
926
927 static_always_inline u8
928 vxlan_validate_udp_csum (vlib_main_t * vm, vlib_buffer_t * b)
929 {
930   u32 flags = b->flags;
931   enum
932   { offset =
933       sizeof (ip4_header_t) + sizeof (udp_header_t) + sizeof (vxlan_header_t),
934   };
935
936   /* Verify UDP checksum */
937   if ((flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
938     {
939       vlib_buffer_advance (b, -offset);
940       flags = ip4_tcp_udp_validate_checksum (vm, b);
941       vlib_buffer_advance (b, offset);
942     }
943
944   return (flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
945 }
946
947 static_always_inline u8
948 vxlan_check_udp_csum (vlib_main_t * vm, vlib_buffer_t * b)
949 {
950   ip4_vxlan_header_t *hdr = vlib_buffer_get_current (b) - sizeof *hdr;
951   udp_header_t *udp = &hdr->udp;
952   /* Don't verify UDP checksum for packets with explicit zero checksum. */
953   u8 good_csum = (b->flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0 ||
954     udp->checksum == 0;
955
956   return !good_csum;
957 }
958
959 static_always_inline u8
960 vxlan_check_ip (vlib_buffer_t * b, u16 payload_len)
961 {
962   ip4_vxlan_header_t *hdr = vlib_buffer_get_current (b) - sizeof *hdr;
963   u16 ip_len = clib_net_to_host_u16 (hdr->ip4.length);
964   u16 expected = payload_len + sizeof *hdr;
965   return ip_len > expected || hdr->ip4.ttl == 0
966     || hdr->ip4.ip_version_and_header_length != 0x45;
967 }
968
969 static_always_inline u8
970 vxlan_check_ip_udp_len (vlib_buffer_t * b)
971 {
972   ip4_vxlan_header_t *hdr = vlib_buffer_get_current (b) - sizeof *hdr;
973   u16 ip_len = clib_net_to_host_u16 (hdr->ip4.length);
974   u16 udp_len = clib_net_to_host_u16 (hdr->udp.length);
975   return udp_len > ip_len;
976 }
977
978 static_always_inline u8
979 vxlan_err_code (u8 ip_err0, u8 udp_err0, u8 csum_err0)
980 {
981   u8 error0 = VXLAN_FLOW_ERROR_NONE;
982   if (ip_err0)
983     error0 = VXLAN_FLOW_ERROR_IP_HEADER_ERROR;
984   if (udp_err0)
985     error0 = VXLAN_FLOW_ERROR_UDP_LENGTH_ERROR;
986   if (csum_err0)
987     error0 = VXLAN_FLOW_ERROR_UDP_CHECKSUM_ERROR;
988   return error0;
989 }
990
991 VLIB_NODE_FN (vxlan4_flow_input_node) (vlib_main_t * vm,
992                                        vlib_node_runtime_t * node,
993                                        vlib_frame_t * f)
994 {
995   enum
996   { payload_offset = sizeof (ip4_vxlan_header_t) };
997
998   vxlan_main_t *vxm = &vxlan_main;
999   vnet_interface_main_t *im = &vnet_main.interface_main;
1000   vlib_combined_counter_main_t *rx_counter[VXLAN_FLOW_N_NEXT] = {
1001     [VXLAN_FLOW_NEXT_DROP] =
1002       im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_DROP,
1003     [VXLAN_FLOW_NEXT_L2_INPUT] =
1004       im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
1005   };
1006   u32 thread_index = vlib_get_thread_index ();
1007
1008   u32 *from = vlib_frame_vector_args (f);
1009   u32 n_left_from = f->n_vectors;
1010   u32 next_index = VXLAN_FLOW_NEXT_L2_INPUT;
1011
1012   while (n_left_from > 0)
1013     {
1014       u32 n_left_to_next, *to_next;
1015
1016       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1017
1018       while (n_left_from > 3 && n_left_to_next > 3)
1019         {
1020           u32 bi0 = to_next[0] = from[0];
1021           u32 bi1 = to_next[1] = from[1];
1022           u32 bi2 = to_next[2] = from[2];
1023           u32 bi3 = to_next[3] = from[3];
1024           from += 4;
1025           n_left_from -= 4;
1026           to_next += 4;
1027           n_left_to_next -= 4;
1028
1029           vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
1030           vlib_buffer_t *b1 = vlib_get_buffer (vm, bi1);
1031           vlib_buffer_t *b2 = vlib_get_buffer (vm, bi2);
1032           vlib_buffer_t *b3 = vlib_get_buffer (vm, bi3);
1033
1034           vlib_buffer_advance (b0, payload_offset);
1035           vlib_buffer_advance (b1, payload_offset);
1036           vlib_buffer_advance (b2, payload_offset);
1037           vlib_buffer_advance (b3, payload_offset);
1038
1039           u16 len0 = vlib_buffer_length_in_chain (vm, b0);
1040           u16 len1 = vlib_buffer_length_in_chain (vm, b1);
1041           u16 len2 = vlib_buffer_length_in_chain (vm, b2);
1042           u16 len3 = vlib_buffer_length_in_chain (vm, b3);
1043
1044           u32 next0 = VXLAN_FLOW_NEXT_L2_INPUT, next1 =
1045             VXLAN_FLOW_NEXT_L2_INPUT, next2 =
1046             VXLAN_FLOW_NEXT_L2_INPUT, next3 = VXLAN_FLOW_NEXT_L2_INPUT;
1047
1048           u8 ip_err0 = vxlan_check_ip (b0, len0);
1049           u8 ip_err1 = vxlan_check_ip (b1, len1);
1050           u8 ip_err2 = vxlan_check_ip (b2, len2);
1051           u8 ip_err3 = vxlan_check_ip (b3, len3);
1052           u8 ip_err = ip_err0 | ip_err1 | ip_err2 | ip_err3;
1053
1054           u8 udp_err0 = vxlan_check_ip_udp_len (b0);
1055           u8 udp_err1 = vxlan_check_ip_udp_len (b1);
1056           u8 udp_err2 = vxlan_check_ip_udp_len (b2);
1057           u8 udp_err3 = vxlan_check_ip_udp_len (b3);
1058           u8 udp_err = udp_err0 | udp_err1 | udp_err2 | udp_err3;
1059
1060           u8 csum_err0 = vxlan_check_udp_csum (vm, b0);
1061           u8 csum_err1 = vxlan_check_udp_csum (vm, b1);
1062           u8 csum_err2 = vxlan_check_udp_csum (vm, b2);
1063           u8 csum_err3 = vxlan_check_udp_csum (vm, b3);
1064           u8 csum_err = csum_err0 | csum_err1 | csum_err2 | csum_err3;
1065
1066           if (PREDICT_FALSE (csum_err))
1067             {
1068               if (csum_err0)
1069                 csum_err0 = !vxlan_validate_udp_csum (vm, b0);
1070               if (csum_err1)
1071                 csum_err1 = !vxlan_validate_udp_csum (vm, b1);
1072               if (csum_err2)
1073                 csum_err2 = !vxlan_validate_udp_csum (vm, b2);
1074               if (csum_err3)
1075                 csum_err3 = !vxlan_validate_udp_csum (vm, b3);
1076               csum_err = csum_err0 | csum_err1 | csum_err2 | csum_err3;
1077             }
1078
1079           if (PREDICT_FALSE (ip_err || udp_err || csum_err))
1080             {
1081               if (ip_err0 || udp_err0 || csum_err0)
1082                 {
1083                   next0 = VXLAN_FLOW_NEXT_DROP;
1084                   u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1085                   b0->error = node->errors[error0];
1086                 }
1087               if (ip_err1 || udp_err1 || csum_err1)
1088                 {
1089                   next1 = VXLAN_FLOW_NEXT_DROP;
1090                   u8 error1 = vxlan_err_code (ip_err1, udp_err1, csum_err1);
1091                   b1->error = node->errors[error1];
1092                 }
1093               if (ip_err2 || udp_err2 || csum_err2)
1094                 {
1095                   next2 = VXLAN_FLOW_NEXT_DROP;
1096                   u8 error2 = vxlan_err_code (ip_err2, udp_err2, csum_err2);
1097                   b2->error = node->errors[error2];
1098                 }
1099               if (ip_err3 || udp_err3 || csum_err3)
1100                 {
1101                   next3 = VXLAN_FLOW_NEXT_DROP;
1102                   u8 error3 = vxlan_err_code (ip_err3, udp_err3, csum_err3);
1103                   b3->error = node->errors[error3];
1104                 }
1105             }
1106
1107           vnet_update_l2_len (b0);
1108           vnet_update_l2_len (b1);
1109           vnet_update_l2_len (b2);
1110           vnet_update_l2_len (b3);
1111
1112           ASSERT (b0->flow_id != 0);
1113           ASSERT (b1->flow_id != 0);
1114           ASSERT (b2->flow_id != 0);
1115           ASSERT (b3->flow_id != 0);
1116
1117           u32 t_index0 = b0->flow_id - vxm->flow_id_start;
1118           u32 t_index1 = b1->flow_id - vxm->flow_id_start;
1119           u32 t_index2 = b2->flow_id - vxm->flow_id_start;
1120           u32 t_index3 = b3->flow_id - vxm->flow_id_start;
1121
1122           vxlan_tunnel_t *t0 = &vxm->tunnels[t_index0];
1123           vxlan_tunnel_t *t1 = &vxm->tunnels[t_index1];
1124           vxlan_tunnel_t *t2 = &vxm->tunnels[t_index2];
1125           vxlan_tunnel_t *t3 = &vxm->tunnels[t_index3];
1126
1127           /* flow id consumed */
1128           b0->flow_id = 0;
1129           b1->flow_id = 0;
1130           b2->flow_id = 0;
1131           b3->flow_id = 0;
1132
1133           u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1134             t0->sw_if_index;
1135           u32 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX] =
1136             t1->sw_if_index;
1137           u32 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_RX] =
1138             t2->sw_if_index;
1139           u32 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_RX] =
1140             t3->sw_if_index;
1141
1142           vlib_increment_combined_counter (rx_counter[next0], thread_index,
1143                                            sw_if_index0, 1, len0);
1144           vlib_increment_combined_counter (rx_counter[next1], thread_index,
1145                                            sw_if_index1, 1, len1);
1146           vlib_increment_combined_counter (rx_counter[next2], thread_index,
1147                                            sw_if_index2, 1, len2);
1148           vlib_increment_combined_counter (rx_counter[next3], thread_index,
1149                                            sw_if_index3, 1, len3);
1150
1151           u32 flags = b0->flags | b1->flags | b2->flags | b3->flags;
1152
1153           if (PREDICT_FALSE (flags & VLIB_BUFFER_IS_TRACED))
1154             {
1155               if (b0->flags & VLIB_BUFFER_IS_TRACED)
1156                 {
1157                   vxlan_rx_trace_t *tr =
1158                     vlib_add_trace (vm, node, b0, sizeof *tr);
1159                   u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1160                   tr->next_index = next0;
1161                   tr->error = error0;
1162                   tr->tunnel_index = t_index0;
1163                   tr->vni = t0->vni;
1164                 }
1165               if (b1->flags & VLIB_BUFFER_IS_TRACED)
1166                 {
1167                   vxlan_rx_trace_t *tr =
1168                     vlib_add_trace (vm, node, b1, sizeof *tr);
1169                   u8 error1 = vxlan_err_code (ip_err1, udp_err1, csum_err1);
1170                   tr->next_index = next1;
1171                   tr->error = error1;
1172                   tr->tunnel_index = t_index1;
1173                   tr->vni = t1->vni;
1174                 }
1175               if (b2->flags & VLIB_BUFFER_IS_TRACED)
1176                 {
1177                   vxlan_rx_trace_t *tr =
1178                     vlib_add_trace (vm, node, b2, sizeof *tr);
1179                   u8 error2 = vxlan_err_code (ip_err2, udp_err2, csum_err2);
1180                   tr->next_index = next2;
1181                   tr->error = error2;
1182                   tr->tunnel_index = t_index2;
1183                   tr->vni = t2->vni;
1184                 }
1185               if (b3->flags & VLIB_BUFFER_IS_TRACED)
1186                 {
1187                   vxlan_rx_trace_t *tr =
1188                     vlib_add_trace (vm, node, b3, sizeof *tr);
1189                   u8 error3 = vxlan_err_code (ip_err3, udp_err3, csum_err3);
1190                   tr->next_index = next3;
1191                   tr->error = error3;
1192                   tr->tunnel_index = t_index3;
1193                   tr->vni = t3->vni;
1194                 }
1195             }
1196           vlib_validate_buffer_enqueue_x4
1197             (vm, node, next_index, to_next, n_left_to_next,
1198              bi0, bi1, bi2, bi3, next0, next1, next2, next3);
1199         }
1200       while (n_left_from > 0 && n_left_to_next > 0)
1201         {
1202           u32 bi0 = to_next[0] = from[0];
1203           from++;
1204           n_left_from--;
1205           to_next++;
1206           n_left_to_next--;
1207
1208           vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
1209           vlib_buffer_advance (b0, payload_offset);
1210
1211           u16 len0 = vlib_buffer_length_in_chain (vm, b0);
1212           u32 next0 = VXLAN_FLOW_NEXT_L2_INPUT;
1213
1214           u8 ip_err0 = vxlan_check_ip (b0, len0);
1215           u8 udp_err0 = vxlan_check_ip_udp_len (b0);
1216           u8 csum_err0 = vxlan_check_udp_csum (vm, b0);
1217
1218           if (csum_err0)
1219             csum_err0 = !vxlan_validate_udp_csum (vm, b0);
1220           if (ip_err0 || udp_err0 || csum_err0)
1221             {
1222               next0 = VXLAN_FLOW_NEXT_DROP;
1223               u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1224               b0->error = node->errors[error0];
1225             }
1226
1227           vnet_update_l2_len (b0);
1228
1229           ASSERT (b0->flow_id != 0);
1230           u32 t_index0 = b0->flow_id - vxm->flow_id_start;
1231           vxlan_tunnel_t *t0 = &vxm->tunnels[t_index0];
1232           b0->flow_id = 0;
1233
1234           u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1235             t0->sw_if_index;
1236           vlib_increment_combined_counter (rx_counter[next0], thread_index,
1237                                            sw_if_index0, 1, len0);
1238
1239           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1240             {
1241               vxlan_rx_trace_t *tr =
1242                 vlib_add_trace (vm, node, b0, sizeof *tr);
1243               u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1244               tr->next_index = next0;
1245               tr->error = error0;
1246               tr->tunnel_index = t_index0;
1247               tr->vni = t0->vni;
1248             }
1249           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1250                                            to_next, n_left_to_next,
1251                                            bi0, next0);
1252         }
1253
1254       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1255     }
1256
1257   return f->n_vectors;
1258 }
1259
1260 /* *INDENT-OFF* */
1261 #ifndef CLIB_MULTIARCH_VARIANT
1262 VLIB_REGISTER_NODE (vxlan4_flow_input_node) = {
1263   .name = "vxlan-flow-input",
1264   .type = VLIB_NODE_TYPE_INTERNAL,
1265   .vector_size = sizeof (u32),
1266
1267   .format_trace = format_vxlan_rx_trace,
1268
1269   .n_errors = VXLAN_FLOW_N_ERROR,
1270   .error_strings = vxlan_flow_error_strings,
1271
1272   .n_next_nodes = VXLAN_FLOW_N_NEXT,
1273   .next_nodes = {
1274 #define _(s,n) [VXLAN_FLOW_NEXT_##s] = n,
1275     foreach_vxlan_flow_input_next
1276 #undef _
1277   },
1278 };
1279 #endif
1280 /* *INDENT-ON* */
1281
1282 /*
1283  * fd.io coding-style-patch-verification: ON
1284  *
1285  * Local Variables:
1286  * eval: (c-set-style "gnu")
1287  * End:
1288  */