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