2 * decap.c: vxlan tunnel decap packet processing
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlib/vlib.h>
19 #include <vnet/vxlan/vxlan.h>
21 #ifndef CLIB_MARCH_VARIANT
22 vlib_node_registration_t vxlan4_input_node;
23 vlib_node_registration_t vxlan6_input_node;
35 format_vxlan_rx_trace (u8 * s, va_list * args)
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 *);
41 if (t->tunnel_index == ~0)
42 return format (s, "VXLAN decap error - tunnel for vni %d does not exist",
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);
48 typedef vxlan4_tunnel_key_t last_tunnel_cache4;
50 static const vxlan_decap_info_t decap_not_found = {
52 .next_index = VXLAN_INPUT_NEXT_DROP,
53 .error = VXLAN_ERROR_NO_SUCH_TUNNEL
56 static const vxlan_decap_info_t decap_bad_flags = {
58 .next_index = VXLAN_INPUT_NEXT_DROP,
59 .error = VXLAN_ERROR_BAD_FLAGS
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)
67 if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
68 return decap_bad_flags;
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,
79 (key4.key[0] == cache->key[0] && key4.key[1] == cache->key[1]))
82 vxlan_decap_info_t di = {.as_u64 = cache->value };
83 *stats_sw_if_index = di.sw_if_index;
87 int rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
88 if (PREDICT_TRUE (rv == 0))
91 vxlan_decap_info_t di = {.as_u64 = key4.value };
92 *stats_sw_if_index = di.sw_if_index;
97 if (PREDICT_TRUE (!ip4_address_is_multicast (&ip4_0->dst_address)))
98 return decap_not_found;
100 /* search for mcast decap info by mcast address */
102 rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
104 return decap_not_found;
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;
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 };
119 typedef vxlan6_tunnel_key_t last_tunnel_cache6;
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)
126 if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
127 return decap_bad_flags;
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,
137 (clib_bihash_key_compare_24_8 (key6.key, cache->key) == 0))
140 clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6);
141 if (PREDICT_FALSE (rv != 0))
142 return decap_not_found;
146 vxlan_tunnel_t *t0 = pool_elt_at_index (vxm->tunnels, cache->value);
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;
154 if (PREDICT_TRUE (!ip6_address_is_multicast (&ip6_0->dst_address)))
155 return decap_not_found;
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];
161 clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6);
162 if (PREDICT_FALSE (rv != 0))
163 return decap_not_found;
165 vxlan_tunnel_t *mcast_t0 = pool_elt_at_index (vxm->tunnels, key6.value);
166 *stats_sw_if_index = mcast_t0->sw_if_index;
169 vxlan_decap_info_t di = {
170 .sw_if_index = t0->sw_if_index,
171 .next_index = t0->decap_next_index,
177 vxlan_input (vlib_main_t * vm,
178 vlib_node_runtime_t * node,
179 vlib_frame_t * from_frame, u32 is_ip4)
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 ();
192 clib_memset (&last4, 0xff, sizeof last4);
194 clib_memset (&last6, 0xff, sizeof last6);
196 u32 *from = vlib_frame_vector_args (from_frame);
197 u32 n_left_from = from_frame->n_vectors;
199 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
200 vlib_get_buffers (vm, from, bufs, n_left_from);
202 u32 stats_if0 = ~0, stats_if1 = ~0;
203 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
204 while (n_left_from >= 4)
206 /* Prefetch next iteration. */
207 vlib_prefetch_buffer_header (b[2], LOAD);
208 vlib_prefetch_buffer_header (b[3], LOAD);
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;
217 ip4_header_t *ip4_0, *ip4_1;
218 ip6_header_t *ip6_0, *ip6_1;
221 ip4_0 = cur0 - sizeof (udp_header_t) - sizeof (ip4_header_t);
222 ip4_1 = cur1 - sizeof (udp_header_t) - sizeof (ip4_header_t);
226 ip6_0 = cur0 - sizeof (udp_header_t) - sizeof (ip6_header_t);
227 ip6_1 = cur1 - sizeof (udp_header_t) - sizeof (ip6_header_t);
231 vlib_buffer_advance (b[0], sizeof *vxlan0);
232 vlib_buffer_advance (b[1], sizeof *vxlan1);
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);
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);
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);
248 u32 len0 = vlib_buffer_length_in_chain (vm, b[0]);
249 u32 len1 = vlib_buffer_length_in_chain (vm, b[1]);
251 next[0] = di0.next_index;
252 next[1] = di1.next_index;
254 u8 any_error = di0.error | di1.error;
255 if (PREDICT_TRUE (any_error == 0))
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,
265 vlib_increment_combined_counter (rx_counter, thread_index,
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,
279 b[0]->error = node->errors[di0.error];
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,
292 b[1]->error = node->errors[di1.error];
297 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
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);
307 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
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);
322 while (n_left_from > 0)
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;
330 ip4_0 = cur0 - sizeof (udp_header_t) - sizeof (ip4_header_t);
332 ip6_0 = cur0 - sizeof (udp_header_t) - sizeof (ip6_header_t);
334 /* pop (ip, udp, vxlan) */
335 vlib_buffer_advance (b[0], sizeof (*vxlan0));
337 u32 fi0 = vlib_buffer_get_ip_fib_index (b[0], is_ip4);
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);
343 uword len0 = vlib_buffer_length_in_chain (vm, b[0]);
345 next[0] = di0.next_index;
347 /* Validate VXLAN tunnel encap-fib index against packet */
350 /* Required to make the l2 tag push / pop code work on l2 subifs */
351 vnet_update_l2_len (b[0]);
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;
356 vlib_increment_combined_counter (rx_counter, thread_index,
361 b[0]->error = node->errors[di0.error];
365 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
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);
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);
385 return from_frame->n_vectors;
388 VLIB_NODE_FN (vxlan4_input_node) (vlib_main_t * vm,
389 vlib_node_runtime_t * node,
390 vlib_frame_t * from_frame)
392 return vxlan_input (vm, node, from_frame, /* is_ip4 */ 1);
395 VLIB_NODE_FN (vxlan6_input_node) (vlib_main_t * vm,
396 vlib_node_runtime_t * node,
397 vlib_frame_t * from_frame)
399 return vxlan_input (vm, node, from_frame, /* is_ip4 */ 0);
402 static char *vxlan_error_strings[] = {
403 #define vxlan_error(n,s) s,
404 #include <vnet/vxlan/vxlan_error.def>
409 VLIB_REGISTER_NODE (vxlan4_input_node) =
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,
418 #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
419 foreach_vxlan_input_next
424 VLIB_REGISTER_NODE (vxlan6_input_node) =
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,
432 #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
433 foreach_vxlan_input_next
436 .format_trace = format_vxlan_rx_trace,
442 IP_VXLAN_BYPASS_NEXT_DROP,
443 IP_VXLAN_BYPASS_NEXT_VXLAN,
444 IP_VXLAN_BYPASS_N_NEXT,
445 } ip_vxlan_bypass_next_t;
448 ip_vxlan_bypass_inline (vlib_main_t * vm,
449 vlib_node_runtime_t * node,
450 vlib_frame_t * frame, u32 is_ip4)
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;
462 #ifdef CLIB_HAVE_VEC512
463 vtep4_cache_t vtep4_u512;
464 clib_memset (&vtep4_u512, 0, sizeof (vtep4_u512));
467 from = vlib_frame_vector_args (frame);
468 n_left_from = frame->n_vectors;
469 next_index = node->cached_next_index;
471 vlib_get_buffers (vm, from, bufs, n_left_from);
473 if (node->flags & VLIB_NODE_FLAG_TRACE)
474 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
477 vtep4_key_init (&last_vtep4);
479 vtep6_key_init (&last_vtep6);
481 while (n_left_from > 0)
483 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
485 while (n_left_from >= 4 && n_left_to_next >= 2)
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;
497 /* Prefetch next iteration. */
499 vlib_prefetch_buffer_header (b[2], LOAD);
500 vlib_prefetch_buffer_header (b[3], LOAD);
502 CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
503 CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
506 bi0 = to_next[0] = from[0];
507 bi1 = to_next[1] = from[1];
518 ip40 = vlib_buffer_get_current (b0);
519 ip41 = vlib_buffer_get_current (b1);
523 ip60 = vlib_buffer_get_current (b0);
524 ip61 = vlib_buffer_get_current (b1);
527 /* Setup packet for next IP feature */
528 vnet_feature_next (&next0, b0);
529 vnet_feature_next (&next1, b1);
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;
540 proto0 = ip60->protocol;
541 proto1 = ip61->protocol;
544 /* Process packet 0 */
545 if (proto0 != IP_PROTOCOL_UDP)
546 goto exit0; /* not UDP packet */
549 udp0 = ip4_next_header (ip40);
551 udp0 = ip6_next_header (ip60);
553 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
554 goto exit0; /* not VXLAN packet */
556 /* Validate DIP against VTEPs */
559 #ifdef CLIB_HAVE_VEC512
560 if (!vtep4_check_vector
561 (&vxm->vtep_table, b0, ip40, &last_vtep4, &vtep4_u512))
563 if (!vtep4_check (&vxm->vtep_table, b0, ip40, &last_vtep4))
565 goto exit0; /* no local VTEP for VXLAN packet */
569 if (!vtep6_check (&vxm->vtep_table, b0, ip60, &last_vtep6))
570 goto exit0; /* no local VTEP for VXLAN packet */
574 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
576 /* Don't verify UDP checksum for packets with explicit zero checksum. */
577 good_udp0 |= udp0->checksum == 0;
579 /* Verify UDP length */
581 ip_len0 = clib_net_to_host_u16 (ip40->length);
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;
587 /* Verify UDP checksum */
588 if (PREDICT_FALSE (!good_udp0))
591 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
593 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
594 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
599 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
600 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
604 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
605 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
609 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
610 b0->error = error0 ? error_node->errors[error0] : 0;
612 /* vxlan-input node expect current at VXLAN header */
614 vlib_buffer_advance (b0,
615 sizeof (ip4_header_t) +
616 sizeof (udp_header_t));
618 vlib_buffer_advance (b0,
619 sizeof (ip6_header_t) +
620 sizeof (udp_header_t));
623 /* Process packet 1 */
624 if (proto1 != IP_PROTOCOL_UDP)
625 goto exit1; /* not UDP packet */
628 udp1 = ip4_next_header (ip41);
630 udp1 = ip6_next_header (ip61);
632 if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
633 goto exit1; /* not VXLAN packet */
635 /* Validate DIP against VTEPs */
638 #ifdef CLIB_HAVE_VEC512
639 if (!vtep4_check_vector
640 (&vxm->vtep_table, b1, ip41, &last_vtep4, &vtep4_u512))
642 if (!vtep4_check (&vxm->vtep_table, b1, ip41, &last_vtep4))
644 goto exit1; /* no local VTEP for VXLAN packet */
648 if (!vtep6_check (&vxm->vtep_table, b1, ip61, &last_vtep6))
649 goto exit1; /* no local VTEP for VXLAN packet */
653 good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
655 /* Don't verify UDP checksum for packets with explicit zero checksum. */
656 good_udp1 |= udp1->checksum == 0;
658 /* Verify UDP length */
660 ip_len1 = clib_net_to_host_u16 (ip41->length);
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;
666 /* Verify UDP checksum */
667 if (PREDICT_FALSE (!good_udp1))
670 flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
672 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
673 good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
678 error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM;
679 error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH;
683 error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM;
684 error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH;
688 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
689 b1->error = error1 ? error_node->errors[error1] : 0;
691 /* vxlan-input node expect current at VXLAN header */
693 vlib_buffer_advance (b1,
694 sizeof (ip4_header_t) +
695 sizeof (udp_header_t));
697 vlib_buffer_advance (b1,
698 sizeof (ip6_header_t) +
699 sizeof (udp_header_t));
702 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
703 to_next, n_left_to_next,
704 bi0, bi1, next0, next1);
707 while (n_left_from > 0 && n_left_to_next > 0)
713 u32 bi0, ip_len0, udp_len0, flags0, next0;
715 u8 error0, good_udp0, proto0;
717 bi0 = to_next[0] = from[0];
726 ip40 = vlib_buffer_get_current (b0);
728 ip60 = vlib_buffer_get_current (b0);
730 /* Setup packet for next IP feature */
731 vnet_feature_next (&next0, b0);
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;
738 proto0 = ip60->protocol;
740 if (proto0 != IP_PROTOCOL_UDP)
741 goto exit; /* not UDP packet */
744 udp0 = ip4_next_header (ip40);
746 udp0 = ip6_next_header (ip60);
748 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
749 goto exit; /* not VXLAN packet */
751 /* Validate DIP against VTEPs */
754 #ifdef CLIB_HAVE_VEC512
755 if (!vtep4_check_vector
756 (&vxm->vtep_table, b0, ip40, &last_vtep4, &vtep4_u512))
758 if (!vtep4_check (&vxm->vtep_table, b0, ip40, &last_vtep4))
760 goto exit; /* no local VTEP for VXLAN packet */
764 if (!vtep6_check (&vxm->vtep_table, b0, ip60, &last_vtep6))
765 goto exit; /* no local VTEP for VXLAN packet */
769 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
771 /* Don't verify UDP checksum for packets with explicit zero checksum. */
772 good_udp0 |= udp0->checksum == 0;
774 /* Verify UDP length */
776 ip_len0 = clib_net_to_host_u16 (ip40->length);
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;
782 /* Verify UDP checksum */
783 if (PREDICT_FALSE (!good_udp0))
786 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
788 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
789 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
794 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
795 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
799 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
800 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
804 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
805 b0->error = error0 ? error_node->errors[error0] : 0;
807 /* vxlan-input node expect current at VXLAN header */
809 vlib_buffer_advance (b0,
810 sizeof (ip4_header_t) +
811 sizeof (udp_header_t));
813 vlib_buffer_advance (b0,
814 sizeof (ip6_header_t) +
815 sizeof (udp_header_t));
818 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
819 to_next, n_left_to_next,
823 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
826 return frame->n_vectors;
829 VLIB_NODE_FN (ip4_vxlan_bypass_node) (vlib_main_t * vm,
830 vlib_node_runtime_t * node,
831 vlib_frame_t * frame)
833 return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 1);
837 VLIB_REGISTER_NODE (ip4_vxlan_bypass_node) =
839 .name = "ip4-vxlan-bypass",
840 .vector_size = sizeof (u32),
841 .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
843 [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
844 [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan4-input",
846 .format_buffer = format_ip4_header,
847 .format_trace = format_ip4_forward_next_trace,
852 /* Dummy init function to get us linked in. */
853 static clib_error_t *
854 ip4_vxlan_bypass_init (vlib_main_t * vm)
859 VLIB_INIT_FUNCTION (ip4_vxlan_bypass_init);
861 VLIB_NODE_FN (ip6_vxlan_bypass_node) (vlib_main_t * vm,
862 vlib_node_runtime_t * node,
863 vlib_frame_t * frame)
865 return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 0);
869 VLIB_REGISTER_NODE (ip6_vxlan_bypass_node) =
871 .name = "ip6-vxlan-bypass",
872 .vector_size = sizeof (u32),
873 .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
875 [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
876 [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan6-input",
878 .format_buffer = format_ip6_header,
879 .format_trace = format_ip6_forward_next_trace,
884 /* Dummy init function to get us linked in. */
885 static clib_error_t *
886 ip6_vxlan_bypass_init (vlib_main_t * vm)
891 VLIB_INIT_FUNCTION (ip6_vxlan_bypass_init);
893 #define foreach_vxlan_flow_input_next \
894 _(DROP, "error-drop") \
895 _(L2_INPUT, "l2-input")
899 #define _(s,n) VXLAN_FLOW_NEXT_##s,
900 foreach_vxlan_flow_input_next
903 } vxlan_flow_input_next_t;
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")
914 #define _(f,s) VXLAN_FLOW_ERROR_##f,
915 foreach_vxlan_flow_error
918 } vxlan_flow_error_t;
920 static char *vxlan_flow_error_strings[] = {
922 foreach_vxlan_flow_error
927 static_always_inline u8
928 vxlan_validate_udp_csum (vlib_main_t * vm, vlib_buffer_t * b)
930 u32 flags = b->flags;
933 sizeof (ip4_header_t) + sizeof (udp_header_t) + sizeof (vxlan_header_t),
936 /* Verify UDP checksum */
937 if ((flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
939 vlib_buffer_advance (b, -offset);
940 flags = ip4_tcp_udp_validate_checksum (vm, b);
941 vlib_buffer_advance (b, offset);
944 return (flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
947 static_always_inline u8
948 vxlan_check_udp_csum (vlib_main_t * vm, vlib_buffer_t * b)
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 ||
959 static_always_inline u8
960 vxlan_check_ip (vlib_buffer_t * b, u16 payload_len)
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;
969 static_always_inline u8
970 vxlan_check_ip_udp_len (vlib_buffer_t * b)
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;
978 static_always_inline u8
979 vxlan_err_code (u8 ip_err0, u8 udp_err0, u8 csum_err0)
981 u8 error0 = VXLAN_FLOW_ERROR_NONE;
983 error0 = VXLAN_FLOW_ERROR_IP_HEADER_ERROR;
985 error0 = VXLAN_FLOW_ERROR_UDP_LENGTH_ERROR;
987 error0 = VXLAN_FLOW_ERROR_UDP_CHECKSUM_ERROR;
991 VLIB_NODE_FN (vxlan4_flow_input_node) (vlib_main_t * vm,
992 vlib_node_runtime_t * node,
996 { payload_offset = sizeof (ip4_vxlan_header_t) };
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,
1006 u32 thread_index = vlib_get_thread_index ();
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;
1012 while (n_left_from > 0)
1014 u32 n_left_to_next, *to_next;
1016 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1018 while (n_left_from > 3 && n_left_to_next > 3)
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];
1027 n_left_to_next -= 4;
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);
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);
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);
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;
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;
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;
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;
1066 if (PREDICT_FALSE (csum_err))
1069 csum_err0 = !vxlan_validate_udp_csum (vm, b0);
1071 csum_err1 = !vxlan_validate_udp_csum (vm, b1);
1073 csum_err2 = !vxlan_validate_udp_csum (vm, b2);
1075 csum_err3 = !vxlan_validate_udp_csum (vm, b3);
1076 csum_err = csum_err0 | csum_err1 | csum_err2 | csum_err3;
1079 if (PREDICT_FALSE (ip_err || udp_err || csum_err))
1081 if (ip_err0 || udp_err0 || csum_err0)
1083 next0 = VXLAN_FLOW_NEXT_DROP;
1084 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1085 b0->error = node->errors[error0];
1087 if (ip_err1 || udp_err1 || csum_err1)
1089 next1 = VXLAN_FLOW_NEXT_DROP;
1090 u8 error1 = vxlan_err_code (ip_err1, udp_err1, csum_err1);
1091 b1->error = node->errors[error1];
1093 if (ip_err2 || udp_err2 || csum_err2)
1095 next2 = VXLAN_FLOW_NEXT_DROP;
1096 u8 error2 = vxlan_err_code (ip_err2, udp_err2, csum_err2);
1097 b2->error = node->errors[error2];
1099 if (ip_err3 || udp_err3 || csum_err3)
1101 next3 = VXLAN_FLOW_NEXT_DROP;
1102 u8 error3 = vxlan_err_code (ip_err3, udp_err3, csum_err3);
1103 b3->error = node->errors[error3];
1107 vnet_update_l2_len (b0);
1108 vnet_update_l2_len (b1);
1109 vnet_update_l2_len (b2);
1110 vnet_update_l2_len (b3);
1112 ASSERT (b0->flow_id != 0);
1113 ASSERT (b1->flow_id != 0);
1114 ASSERT (b2->flow_id != 0);
1115 ASSERT (b3->flow_id != 0);
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;
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];
1127 /* flow id consumed */
1133 u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1135 u32 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX] =
1137 u32 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_RX] =
1139 u32 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_RX] =
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);
1151 u32 flags = b0->flags | b1->flags | b2->flags | b3->flags;
1153 if (PREDICT_FALSE (flags & VLIB_BUFFER_IS_TRACED))
1155 if (b0->flags & VLIB_BUFFER_IS_TRACED)
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;
1162 tr->tunnel_index = t_index0;
1165 if (b1->flags & VLIB_BUFFER_IS_TRACED)
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;
1172 tr->tunnel_index = t_index1;
1175 if (b2->flags & VLIB_BUFFER_IS_TRACED)
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;
1182 tr->tunnel_index = t_index2;
1185 if (b3->flags & VLIB_BUFFER_IS_TRACED)
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;
1192 tr->tunnel_index = t_index3;
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);
1200 while (n_left_from > 0 && n_left_to_next > 0)
1202 u32 bi0 = to_next[0] = from[0];
1208 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
1209 vlib_buffer_advance (b0, payload_offset);
1211 u16 len0 = vlib_buffer_length_in_chain (vm, b0);
1212 u32 next0 = VXLAN_FLOW_NEXT_L2_INPUT;
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);
1219 csum_err0 = !vxlan_validate_udp_csum (vm, b0);
1220 if (ip_err0 || udp_err0 || csum_err0)
1222 next0 = VXLAN_FLOW_NEXT_DROP;
1223 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1224 b0->error = node->errors[error0];
1227 vnet_update_l2_len (b0);
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];
1234 u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1236 vlib_increment_combined_counter (rx_counter[next0], thread_index,
1237 sw_if_index0, 1, len0);
1239 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
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;
1246 tr->tunnel_index = t_index0;
1249 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1250 to_next, n_left_to_next,
1254 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1257 return f->n_vectors;
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),
1267 .format_trace = format_vxlan_rx_trace,
1269 .n_errors = VXLAN_FLOW_N_ERROR,
1270 .error_strings = vxlan_flow_error_strings,
1272 .n_next_nodes = VXLAN_FLOW_N_NEXT,
1274 #define _(s,n) [VXLAN_FLOW_NEXT_##s] = n,
1275 foreach_vxlan_flow_input_next
1283 * fd.io coding-style-patch-verification: ON
1286 * eval: (c-set-style "gnu")