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/pg/pg.h>
20 #include <vnet/vxlan/vxlan.h>
22 #ifndef CLIB_MARCH_VARIANT
23 vlib_node_registration_t vxlan4_input_node;
24 vlib_node_registration_t vxlan6_input_node;
36 format_vxlan_rx_trace (u8 * s, va_list * args)
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 *);
42 if (t->tunnel_index == ~0)
43 return format (s, "VXLAN decap error - tunnel for vni %d does not exist",
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);
50 buf_fib_index (vlib_buffer_t * b, u32 is_ip4)
52 u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
53 if (sw_if_index != (u32) ~ 0)
56 u32 *fib_index_by_sw_if_index = is_ip4 ?
57 ip4_main.fib_index_by_sw_if_index : ip6_main.fib_index_by_sw_if_index;
58 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
60 return vec_elt (fib_index_by_sw_if_index, sw_if_index);
63 typedef vxlan4_tunnel_key_t last_tunnel_cache4;
65 static const vxlan_decap_info_t decap_not_found = {
67 .next_index = VXLAN_INPUT_NEXT_DROP,
68 .error = VXLAN_ERROR_NO_SUCH_TUNNEL
71 static const vxlan_decap_info_t decap_bad_flags = {
73 .next_index = VXLAN_INPUT_NEXT_DROP,
74 .error = VXLAN_ERROR_BAD_FLAGS
77 always_inline vxlan_decap_info_t
78 vxlan4_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache4 * cache,
79 u32 fib_index, ip4_header_t * ip4_0,
80 vxlan_header_t * vxlan0, u32 * stats_sw_if_index)
82 if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
83 return decap_bad_flags;
85 /* Make sure VXLAN tunnel exist according to packet S/D IP, VRF, and VNI */
86 u32 dst = ip4_0->dst_address.as_u32;
87 u32 src = ip4_0->src_address.as_u32;
88 vxlan4_tunnel_key_t key4 = {
89 .key[0] = ((u64) dst << 32) | src,
90 .key[1] = ((u64) fib_index << 32) | vxlan0->vni_reserved,
94 (key4.key[0] == cache->key[0] && key4.key[1] == cache->key[1]))
97 vxlan_decap_info_t di = {.as_u64 = cache->value };
98 *stats_sw_if_index = di.sw_if_index;
102 int rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
103 if (PREDICT_TRUE (rv == 0))
106 vxlan_decap_info_t di = {.as_u64 = key4.value };
107 *stats_sw_if_index = di.sw_if_index;
112 if (PREDICT_TRUE (!ip4_address_is_multicast (&ip4_0->dst_address)))
113 return decap_not_found;
115 /* search for mcast decap info by mcast address */
117 rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
119 return decap_not_found;
121 /* search for unicast tunnel using the mcast tunnel local(src) ip */
122 vxlan_decap_info_t mdi = {.as_u64 = key4.value };
123 key4.key[0] = ((u64) mdi.local_ip.as_u32 << 32) | src;
124 rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
125 if (PREDICT_FALSE (rv != 0))
126 return decap_not_found;
128 /* mcast traffic does not update the cache */
129 *stats_sw_if_index = mdi.sw_if_index;
130 vxlan_decap_info_t di = {.as_u64 = key4.value };
134 typedef vxlan6_tunnel_key_t last_tunnel_cache6;
136 always_inline vxlan_decap_info_t
137 vxlan6_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache6 * cache,
138 u32 fib_index, ip6_header_t * ip6_0,
139 vxlan_header_t * vxlan0, u32 * stats_sw_if_index)
141 if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
142 return decap_bad_flags;
144 /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
145 vxlan6_tunnel_key_t key6 = {
146 .key[0] = ip6_0->src_address.as_u64[0],
147 .key[1] = ip6_0->src_address.as_u64[1],
148 .key[2] = (((u64) fib_index) << 32) | vxlan0->vni_reserved,
152 (clib_bihash_key_compare_24_8 (key6.key, cache->key) == 0))
155 clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6);
156 if (PREDICT_FALSE (rv != 0))
157 return decap_not_found;
161 vxlan_tunnel_t *t0 = pool_elt_at_index (vxm->tunnels, cache->value);
163 /* Validate VXLAN tunnel SIP against packet DIP */
164 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address, &t0->src.ip6)))
165 *stats_sw_if_index = t0->sw_if_index;
169 if (PREDICT_TRUE (!ip6_address_is_multicast (&ip6_0->dst_address)))
170 return decap_not_found;
172 /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */
173 key6.key[0] = ip6_0->dst_address.as_u64[0];
174 key6.key[1] = ip6_0->dst_address.as_u64[1];
176 clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6);
177 if (PREDICT_FALSE (rv != 0))
178 return decap_not_found;
180 vxlan_tunnel_t *mcast_t0 = pool_elt_at_index (vxm->tunnels, key6.value);
181 *stats_sw_if_index = mcast_t0->sw_if_index;
184 vxlan_decap_info_t di = {
185 .sw_if_index = t0->sw_if_index,
186 .next_index = t0->decap_next_index,
192 vxlan_input (vlib_main_t * vm,
193 vlib_node_runtime_t * node,
194 vlib_frame_t * from_frame, u32 is_ip4)
196 vxlan_main_t *vxm = &vxlan_main;
197 vnet_main_t *vnm = vxm->vnet_main;
198 vnet_interface_main_t *im = &vnm->interface_main;
199 vlib_combined_counter_main_t *rx_counter =
200 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX;
201 last_tunnel_cache4 last4;
202 last_tunnel_cache6 last6;
203 u32 pkts_dropped = 0;
204 u32 thread_index = vlib_get_thread_index ();
207 clib_memset (&last4, 0xff, sizeof last4);
209 clib_memset (&last6, 0xff, sizeof last6);
211 u32 *from = vlib_frame_vector_args (from_frame);
212 u32 n_left_from = from_frame->n_vectors;
214 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
215 vlib_get_buffers (vm, from, bufs, n_left_from);
217 u32 stats_if0 = ~0, stats_if1 = ~0;
218 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
219 while (n_left_from >= 4)
221 /* Prefetch next iteration. */
222 vlib_prefetch_buffer_header (b[2], LOAD);
223 vlib_prefetch_buffer_header (b[3], LOAD);
225 /* udp leaves current_data pointing at the vxlan header */
226 void *cur0 = vlib_buffer_get_current (b[0]);
227 void *cur1 = vlib_buffer_get_current (b[1]);
228 vxlan_header_t *vxlan0 = cur0;
229 vxlan_header_t *vxlan1 = cur1;
232 ip4_header_t *ip4_0, *ip4_1;
233 ip6_header_t *ip6_0, *ip6_1;
236 ip4_0 = cur0 - sizeof (udp_header_t) - sizeof (ip4_header_t);
237 ip4_1 = cur1 - sizeof (udp_header_t) - sizeof (ip4_header_t);
241 ip6_0 = cur0 - sizeof (udp_header_t) - sizeof (ip6_header_t);
242 ip6_1 = cur1 - sizeof (udp_header_t) - sizeof (ip6_header_t);
246 vlib_buffer_advance (b[0], sizeof *vxlan0);
247 vlib_buffer_advance (b[1], sizeof *vxlan1);
249 u32 fi0 = buf_fib_index (b[0], is_ip4);
250 u32 fi1 = buf_fib_index (b[1], is_ip4);
252 vxlan_decap_info_t di0 = is_ip4 ?
253 vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_if0) :
254 vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_if0);
255 vxlan_decap_info_t di1 = is_ip4 ?
256 vxlan4_find_tunnel (vxm, &last4, fi1, ip4_1, vxlan1, &stats_if1) :
257 vxlan6_find_tunnel (vxm, &last6, fi1, ip6_1, vxlan1, &stats_if1);
259 /* Prefetch next iteration. */
260 CLIB_PREFETCH (b[2]->data, CLIB_CACHE_LINE_BYTES, LOAD);
261 CLIB_PREFETCH (b[3]->data, CLIB_CACHE_LINE_BYTES, LOAD);
263 u32 len0 = vlib_buffer_length_in_chain (vm, b[0]);
264 u32 len1 = vlib_buffer_length_in_chain (vm, b[1]);
266 next[0] = di0.next_index;
267 next[1] = di1.next_index;
269 u8 any_error = di0.error | di1.error;
270 if (PREDICT_TRUE (any_error == 0))
272 /* Required to make the l2 tag push / pop code work on l2 subifs */
273 vnet_update_l2_len (b[0]);
274 vnet_update_l2_len (b[1]);
275 /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
276 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
277 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = di1.sw_if_index;
278 vlib_increment_combined_counter (rx_counter, thread_index,
280 vlib_increment_combined_counter (rx_counter, thread_index,
287 vnet_update_l2_len (b[0]);
288 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
289 vlib_increment_combined_counter (rx_counter, thread_index,
294 b[0]->error = node->errors[di0.error];
300 vnet_update_l2_len (b[1]);
301 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = di1.sw_if_index;
302 vlib_increment_combined_counter (rx_counter, thread_index,
307 b[1]->error = node->errors[di1.error];
312 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
314 vxlan_rx_trace_t *tr =
315 vlib_add_trace (vm, node, b[0], sizeof (*tr));
316 tr->next_index = next[0];
317 tr->error = di0.error;
318 tr->tunnel_index = di0.sw_if_index == ~0 ?
319 ~0 : vxm->tunnel_index_by_sw_if_index[di0.sw_if_index];
320 tr->vni = vnet_get_vni (vxlan0);
322 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
324 vxlan_rx_trace_t *tr =
325 vlib_add_trace (vm, node, b[1], sizeof (*tr));
326 tr->next_index = next[1];
327 tr->error = di1.error;
328 tr->tunnel_index = di1.sw_if_index == ~0 ?
329 ~0 : vxm->tunnel_index_by_sw_if_index[di1.sw_if_index];
330 tr->vni = vnet_get_vni (vxlan1);
337 while (n_left_from > 0)
339 /* udp leaves current_data pointing at the vxlan header */
340 void *cur0 = vlib_buffer_get_current (b[0]);
341 vxlan_header_t *vxlan0 = cur0;
345 ip4_0 = cur0 - sizeof (udp_header_t) - sizeof (ip4_header_t);
347 ip6_0 = cur0 - sizeof (udp_header_t) - sizeof (ip6_header_t);
349 /* pop (ip, udp, vxlan) */
350 vlib_buffer_advance (b[0], sizeof (*vxlan0));
352 u32 fi0 = buf_fib_index (b[0], is_ip4);
354 vxlan_decap_info_t di0 = is_ip4 ?
355 vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_if0) :
356 vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_if0);
358 uword len0 = vlib_buffer_length_in_chain (vm, b[0]);
360 next[0] = di0.next_index;
362 /* Validate VXLAN tunnel encap-fib index against packet */
365 /* Required to make the l2 tag push / pop code work on l2 subifs */
366 vnet_update_l2_len (b[0]);
368 /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
369 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
371 vlib_increment_combined_counter (rx_counter, thread_index,
376 b[0]->error = node->errors[di0.error];
380 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
383 = vlib_add_trace (vm, node, b[0], sizeof (*tr));
384 tr->next_index = next[0];
385 tr->error = di0.error;
386 tr->tunnel_index = di0.sw_if_index == ~0 ?
387 ~0 : vxm->tunnel_index_by_sw_if_index[di0.sw_if_index];
388 tr->vni = vnet_get_vni (vxlan0);
394 vlib_buffer_enqueue_to_next (vm, node, from, nexts, from_frame->n_vectors);
395 /* Do we still need this now that tunnel tx stats is kept? */
396 u32 node_idx = is_ip4 ? vxlan4_input_node.index : vxlan6_input_node.index;
397 vlib_node_increment_counter (vm, node_idx, VXLAN_ERROR_DECAPSULATED,
398 from_frame->n_vectors - pkts_dropped);
400 return from_frame->n_vectors;
403 VLIB_NODE_FN (vxlan4_input_node) (vlib_main_t * vm,
404 vlib_node_runtime_t * node,
405 vlib_frame_t * from_frame)
407 return vxlan_input (vm, node, from_frame, /* is_ip4 */ 1);
410 VLIB_NODE_FN (vxlan6_input_node) (vlib_main_t * vm,
411 vlib_node_runtime_t * node,
412 vlib_frame_t * from_frame)
414 return vxlan_input (vm, node, from_frame, /* is_ip4 */ 0);
417 static char *vxlan_error_strings[] = {
418 #define vxlan_error(n,s) s,
419 #include <vnet/vxlan/vxlan_error.def>
424 VLIB_REGISTER_NODE (vxlan4_input_node) =
426 .name = "vxlan4-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 .format_trace = format_vxlan_rx_trace,
433 #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
434 foreach_vxlan_input_next
439 VLIB_REGISTER_NODE (vxlan6_input_node) =
441 .name = "vxlan6-input",
442 .vector_size = sizeof (u32),
443 .n_errors = VXLAN_N_ERROR,
444 .error_strings = vxlan_error_strings,
445 .n_next_nodes = VXLAN_INPUT_N_NEXT,
447 #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
448 foreach_vxlan_input_next
451 .format_trace = format_vxlan_rx_trace,
457 IP_VXLAN_BYPASS_NEXT_DROP,
458 IP_VXLAN_BYPASS_NEXT_VXLAN,
459 IP_VXLAN_BYPASS_N_NEXT,
460 } ip_vxan_bypass_next_t;
463 ip_vxlan_bypass_inline (vlib_main_t * vm,
464 vlib_node_runtime_t * node,
465 vlib_frame_t * frame, u32 is_ip4)
467 vxlan_main_t *vxm = &vxlan_main;
468 u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
469 vlib_node_runtime_t *error_node =
470 vlib_node_get_runtime (vm, ip4_input_node.index);
471 ip4_address_t addr4; /* last IPv4 address matching a local VTEP address */
472 ip6_address_t addr6; /* last IPv6 address matching a local VTEP address */
474 from = vlib_frame_vector_args (frame);
475 n_left_from = frame->n_vectors;
476 next_index = node->cached_next_index;
478 if (node->flags & VLIB_NODE_FLAG_TRACE)
479 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
484 ip6_address_set_zero (&addr6);
486 while (n_left_from > 0)
488 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
490 while (n_left_from >= 4 && n_left_to_next >= 2)
492 vlib_buffer_t *b0, *b1;
493 ip4_header_t *ip40, *ip41;
494 ip6_header_t *ip60, *ip61;
495 udp_header_t *udp0, *udp1;
496 u32 bi0, ip_len0, udp_len0, flags0, next0;
497 u32 bi1, ip_len1, udp_len1, flags1, next1;
498 i32 len_diff0, len_diff1;
499 u8 error0, good_udp0, proto0;
500 u8 error1, good_udp1, proto1;
502 /* Prefetch next iteration. */
504 vlib_buffer_t *p2, *p3;
506 p2 = vlib_get_buffer (vm, from[2]);
507 p3 = vlib_get_buffer (vm, from[3]);
509 vlib_prefetch_buffer_header (p2, LOAD);
510 vlib_prefetch_buffer_header (p3, LOAD);
512 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
513 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
516 bi0 = to_next[0] = from[0];
517 bi1 = to_next[1] = from[1];
523 b0 = vlib_get_buffer (vm, bi0);
524 b1 = vlib_get_buffer (vm, bi1);
527 ip40 = vlib_buffer_get_current (b0);
528 ip41 = vlib_buffer_get_current (b1);
532 ip60 = vlib_buffer_get_current (b0);
533 ip61 = vlib_buffer_get_current (b1);
536 /* Setup packet for next IP feature */
537 vnet_feature_next (&next0, b0);
538 vnet_feature_next (&next1, b1);
542 /* Treat IP frag packets as "experimental" protocol for now
543 until support of IP frag reassembly is implemented */
544 proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol;
545 proto1 = ip4_is_fragment (ip41) ? 0xfe : ip41->protocol;
549 proto0 = ip60->protocol;
550 proto1 = ip61->protocol;
553 /* Process packet 0 */
554 if (proto0 != IP_PROTOCOL_UDP)
555 goto exit0; /* not UDP packet */
558 udp0 = ip4_next_header (ip40);
560 udp0 = ip6_next_header (ip60);
562 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
563 goto exit0; /* not VXLAN packet */
565 /* Validate DIP against VTEPs */
568 if (addr4.as_u32 != ip40->dst_address.as_u32)
570 if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32))
571 goto exit0; /* no local VTEP for VXLAN packet */
572 addr4 = ip40->dst_address;
577 if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
579 if (!hash_get_mem (vxm->vtep6, &ip60->dst_address))
580 goto exit0; /* no local VTEP for VXLAN packet */
581 addr6 = ip60->dst_address;
586 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
588 /* Don't verify UDP checksum for packets with explicit zero checksum. */
589 good_udp0 |= udp0->checksum == 0;
591 /* Verify UDP length */
593 ip_len0 = clib_net_to_host_u16 (ip40->length);
595 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
596 udp_len0 = clib_net_to_host_u16 (udp0->length);
597 len_diff0 = ip_len0 - udp_len0;
599 /* Verify UDP checksum */
600 if (PREDICT_FALSE (!good_udp0))
602 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
605 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
607 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
609 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
615 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
616 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
620 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
621 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
625 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
626 b0->error = error0 ? error_node->errors[error0] : 0;
628 /* vxlan-input node expect current at VXLAN header */
630 vlib_buffer_advance (b0,
631 sizeof (ip4_header_t) +
632 sizeof (udp_header_t));
634 vlib_buffer_advance (b0,
635 sizeof (ip6_header_t) +
636 sizeof (udp_header_t));
639 /* Process packet 1 */
640 if (proto1 != IP_PROTOCOL_UDP)
641 goto exit1; /* not UDP packet */
644 udp1 = ip4_next_header (ip41);
646 udp1 = ip6_next_header (ip61);
648 if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
649 goto exit1; /* not VXLAN packet */
651 /* Validate DIP against VTEPs */
654 if (addr4.as_u32 != ip41->dst_address.as_u32)
656 if (!hash_get (vxm->vtep4, ip41->dst_address.as_u32))
657 goto exit1; /* no local VTEP for VXLAN packet */
658 addr4 = ip41->dst_address;
663 if (!ip6_address_is_equal (&addr6, &ip61->dst_address))
665 if (!hash_get_mem (vxm->vtep6, &ip61->dst_address))
666 goto exit1; /* no local VTEP for VXLAN packet */
667 addr6 = ip61->dst_address;
672 good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
674 /* Don't verify UDP checksum for packets with explicit zero checksum. */
675 good_udp1 |= udp1->checksum == 0;
677 /* Verify UDP length */
679 ip_len1 = clib_net_to_host_u16 (ip41->length);
681 ip_len1 = clib_net_to_host_u16 (ip61->payload_length);
682 udp_len1 = clib_net_to_host_u16 (udp1->length);
683 len_diff1 = ip_len1 - udp_len1;
685 /* Verify UDP checksum */
686 if (PREDICT_FALSE (!good_udp1))
688 if ((flags1 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
691 flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
693 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
695 (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
701 error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM;
702 error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH;
706 error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM;
707 error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH;
711 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
712 b1->error = error1 ? error_node->errors[error1] : 0;
714 /* vxlan-input node expect current at VXLAN header */
716 vlib_buffer_advance (b1,
717 sizeof (ip4_header_t) +
718 sizeof (udp_header_t));
720 vlib_buffer_advance (b1,
721 sizeof (ip6_header_t) +
722 sizeof (udp_header_t));
725 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
726 to_next, n_left_to_next,
727 bi0, bi1, next0, next1);
730 while (n_left_from > 0 && n_left_to_next > 0)
736 u32 bi0, ip_len0, udp_len0, flags0, next0;
738 u8 error0, good_udp0, proto0;
740 bi0 = to_next[0] = from[0];
746 b0 = vlib_get_buffer (vm, bi0);
748 ip40 = vlib_buffer_get_current (b0);
750 ip60 = vlib_buffer_get_current (b0);
752 /* Setup packet for next IP feature */
753 vnet_feature_next (&next0, b0);
756 /* Treat IP4 frag packets as "experimental" protocol for now
757 until support of IP frag reassembly is implemented */
758 proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol;
760 proto0 = ip60->protocol;
762 if (proto0 != IP_PROTOCOL_UDP)
763 goto exit; /* not UDP packet */
766 udp0 = ip4_next_header (ip40);
768 udp0 = ip6_next_header (ip60);
770 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
771 goto exit; /* not VXLAN packet */
773 /* Validate DIP against VTEPs */
776 if (addr4.as_u32 != ip40->dst_address.as_u32)
778 if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32))
779 goto exit; /* no local VTEP for VXLAN packet */
780 addr4 = ip40->dst_address;
785 if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
787 if (!hash_get_mem (vxm->vtep6, &ip60->dst_address))
788 goto exit; /* no local VTEP for VXLAN packet */
789 addr6 = ip60->dst_address;
794 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
796 /* Don't verify UDP checksum for packets with explicit zero checksum. */
797 good_udp0 |= udp0->checksum == 0;
799 /* Verify UDP length */
801 ip_len0 = clib_net_to_host_u16 (ip40->length);
803 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
804 udp_len0 = clib_net_to_host_u16 (udp0->length);
805 len_diff0 = ip_len0 - udp_len0;
807 /* Verify UDP checksum */
808 if (PREDICT_FALSE (!good_udp0))
810 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
813 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
815 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
817 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
823 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
824 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
828 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
829 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
833 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
834 b0->error = error0 ? error_node->errors[error0] : 0;
836 /* vxlan-input node expect current at VXLAN header */
838 vlib_buffer_advance (b0,
839 sizeof (ip4_header_t) +
840 sizeof (udp_header_t));
842 vlib_buffer_advance (b0,
843 sizeof (ip6_header_t) +
844 sizeof (udp_header_t));
847 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
848 to_next, n_left_to_next,
852 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
855 return frame->n_vectors;
858 VLIB_NODE_FN (ip4_vxlan_bypass_node) (vlib_main_t * vm,
859 vlib_node_runtime_t * node,
860 vlib_frame_t * frame)
862 return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 1);
866 VLIB_REGISTER_NODE (ip4_vxlan_bypass_node) =
868 .name = "ip4-vxlan-bypass",
869 .vector_size = sizeof (u32),
870 .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
872 [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
873 [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan4-input",
875 .format_buffer = format_ip4_header,
876 .format_trace = format_ip4_forward_next_trace,
881 /* Dummy init function to get us linked in. */
882 static clib_error_t *
883 ip4_vxlan_bypass_init (vlib_main_t * vm)
888 VLIB_INIT_FUNCTION (ip4_vxlan_bypass_init);
890 VLIB_NODE_FN (ip6_vxlan_bypass_node) (vlib_main_t * vm,
891 vlib_node_runtime_t * node,
892 vlib_frame_t * frame)
894 return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 0);
898 VLIB_REGISTER_NODE (ip6_vxlan_bypass_node) =
900 .name = "ip6-vxlan-bypass",
901 .vector_size = sizeof (u32),
902 .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
904 [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
905 [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan6-input",
907 .format_buffer = format_ip6_header,
908 .format_trace = format_ip6_forward_next_trace,
913 /* Dummy init function to get us linked in. */
914 static clib_error_t *
915 ip6_vxlan_bypass_init (vlib_main_t * vm)
920 VLIB_INIT_FUNCTION (ip6_vxlan_bypass_init);
922 #define foreach_vxlan_flow_input_next \
923 _(DROP, "error-drop") \
924 _(L2_INPUT, "l2-input")
928 #define _(s,n) VXLAN_FLOW_NEXT_##s,
929 foreach_vxlan_flow_input_next
932 } vxlan_flow_input_next_t;
934 #define foreach_vxlan_flow_error \
935 _(NONE, "no error") \
936 _(IP_CHECKSUM_ERROR, "Rx ip checksum errors") \
937 _(IP_HEADER_ERROR, "Rx ip header errors") \
938 _(UDP_CHECKSUM_ERROR, "Rx udp checksum errors") \
939 _(UDP_LENGTH_ERROR, "Rx udp length errors")
943 #define _(f,s) VXLAN_FLOW_ERROR_##f,
944 foreach_vxlan_flow_error
947 } vxlan_flow_error_t;
949 static char *vxlan_flow_error_strings[] = {
951 foreach_vxlan_flow_error
956 static_always_inline u8
957 vxlan_validate_udp_csum (vlib_main_t * vm, vlib_buffer_t * b)
959 u32 flags = b->flags;
962 sizeof (ip4_header_t) + sizeof (udp_header_t) + sizeof (vxlan_header_t),
965 /* Verify UDP checksum */
966 if ((flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
968 vlib_buffer_advance (b, -offset);
969 flags = ip4_tcp_udp_validate_checksum (vm, b);
970 vlib_buffer_advance (b, offset);
973 return (flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
976 static_always_inline u8
977 vxlan_check_udp_csum (vlib_main_t * vm, vlib_buffer_t * b)
979 ip4_vxlan_header_t *hdr = vlib_buffer_get_current (b) - sizeof *hdr;
980 udp_header_t *udp = &hdr->udp;
981 /* Don't verify UDP checksum for packets with explicit zero checksum. */
982 u8 good_csum = (b->flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0 ||
988 static_always_inline u8
989 vxlan_check_ip (vlib_buffer_t * b, u16 payload_len)
991 ip4_vxlan_header_t *hdr = vlib_buffer_get_current (b) - sizeof *hdr;
992 u16 ip_len = clib_net_to_host_u16 (hdr->ip4.length);
993 u16 expected = payload_len + sizeof *hdr;
994 return ip_len > expected || hdr->ip4.ttl == 0
995 || hdr->ip4.ip_version_and_header_length != 0x45;
998 static_always_inline u8
999 vxlan_check_ip_udp_len (vlib_buffer_t * b)
1001 ip4_vxlan_header_t *hdr = vlib_buffer_get_current (b) - sizeof *hdr;
1002 u16 ip_len = clib_net_to_host_u16 (hdr->ip4.length);
1003 u16 udp_len = clib_net_to_host_u16 (hdr->udp.length);
1004 return udp_len > ip_len;
1007 static_always_inline u8
1008 vxlan_err_code (u8 ip_err0, u8 udp_err0, u8 csum_err0)
1010 u8 error0 = VXLAN_FLOW_ERROR_NONE;
1012 error0 = VXLAN_FLOW_ERROR_IP_HEADER_ERROR;
1014 error0 = VXLAN_FLOW_ERROR_UDP_LENGTH_ERROR;
1016 error0 = VXLAN_FLOW_ERROR_UDP_CHECKSUM_ERROR;
1020 VLIB_NODE_FN (vxlan4_flow_input_node) (vlib_main_t * vm,
1021 vlib_node_runtime_t * node,
1025 { payload_offset = sizeof (ip4_vxlan_header_t) };
1027 vxlan_main_t *vxm = &vxlan_main;
1028 vnet_interface_main_t *im = &vnet_main.interface_main;
1029 vlib_combined_counter_main_t *rx_counter[VXLAN_FLOW_N_NEXT] = {
1030 [VXLAN_FLOW_NEXT_DROP] =
1031 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_DROP,
1032 [VXLAN_FLOW_NEXT_L2_INPUT] =
1033 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
1035 u32 thread_index = vlib_get_thread_index ();
1037 u32 *from = vlib_frame_vector_args (f);
1038 u32 n_left_from = f->n_vectors;
1039 u32 next_index = VXLAN_FLOW_NEXT_L2_INPUT;
1041 while (n_left_from > 0)
1043 u32 n_left_to_next, *to_next;
1045 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1047 while (n_left_from > 3 && n_left_to_next > 3)
1049 u32 bi0 = to_next[0] = from[0];
1050 u32 bi1 = to_next[1] = from[1];
1051 u32 bi2 = to_next[2] = from[2];
1052 u32 bi3 = to_next[3] = from[3];
1056 n_left_to_next -= 4;
1058 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
1059 vlib_buffer_t *b1 = vlib_get_buffer (vm, bi1);
1060 vlib_buffer_t *b2 = vlib_get_buffer (vm, bi2);
1061 vlib_buffer_t *b3 = vlib_get_buffer (vm, bi3);
1063 vlib_buffer_advance (b0, payload_offset);
1064 vlib_buffer_advance (b1, payload_offset);
1065 vlib_buffer_advance (b2, payload_offset);
1066 vlib_buffer_advance (b3, payload_offset);
1068 u16 len0 = vlib_buffer_length_in_chain (vm, b0);
1069 u16 len1 = vlib_buffer_length_in_chain (vm, b1);
1070 u16 len2 = vlib_buffer_length_in_chain (vm, b2);
1071 u16 len3 = vlib_buffer_length_in_chain (vm, b3);
1073 u32 next0 = VXLAN_FLOW_NEXT_L2_INPUT, next1 =
1074 VXLAN_FLOW_NEXT_L2_INPUT, next2 =
1075 VXLAN_FLOW_NEXT_L2_INPUT, next3 = VXLAN_FLOW_NEXT_L2_INPUT;
1077 u8 ip_err0 = vxlan_check_ip (b0, len0);
1078 u8 ip_err1 = vxlan_check_ip (b1, len1);
1079 u8 ip_err2 = vxlan_check_ip (b2, len2);
1080 u8 ip_err3 = vxlan_check_ip (b3, len3);
1081 u8 ip_err = ip_err0 | ip_err1 | ip_err2 | ip_err3;
1083 u8 udp_err0 = vxlan_check_ip_udp_len (b0);
1084 u8 udp_err1 = vxlan_check_ip_udp_len (b1);
1085 u8 udp_err2 = vxlan_check_ip_udp_len (b2);
1086 u8 udp_err3 = vxlan_check_ip_udp_len (b3);
1087 u8 udp_err = udp_err0 | udp_err1 | udp_err2 | udp_err3;
1089 u8 csum_err0 = vxlan_check_udp_csum (vm, b0);
1090 u8 csum_err1 = vxlan_check_udp_csum (vm, b1);
1091 u8 csum_err2 = vxlan_check_udp_csum (vm, b2);
1092 u8 csum_err3 = vxlan_check_udp_csum (vm, b3);
1093 u8 csum_err = csum_err0 | csum_err1 | csum_err2 | csum_err3;
1095 if (PREDICT_FALSE (csum_err))
1098 csum_err0 = !vxlan_validate_udp_csum (vm, b0);
1100 csum_err1 = !vxlan_validate_udp_csum (vm, b1);
1102 csum_err2 = !vxlan_validate_udp_csum (vm, b2);
1104 csum_err3 = !vxlan_validate_udp_csum (vm, b3);
1105 csum_err = csum_err0 | csum_err1 | csum_err2 | csum_err3;
1108 if (PREDICT_FALSE (ip_err || udp_err || csum_err))
1110 if (ip_err0 || udp_err0 || csum_err0)
1112 next0 = VXLAN_FLOW_NEXT_DROP;
1113 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1114 b0->error = node->errors[error0];
1116 if (ip_err1 || udp_err1 || csum_err1)
1118 next1 = VXLAN_FLOW_NEXT_DROP;
1119 u8 error1 = vxlan_err_code (ip_err1, udp_err1, csum_err1);
1120 b1->error = node->errors[error1];
1122 if (ip_err2 || udp_err2 || csum_err2)
1124 next2 = VXLAN_FLOW_NEXT_DROP;
1125 u8 error2 = vxlan_err_code (ip_err2, udp_err2, csum_err2);
1126 b2->error = node->errors[error2];
1128 if (ip_err3 || udp_err3 || csum_err3)
1130 next3 = VXLAN_FLOW_NEXT_DROP;
1131 u8 error3 = vxlan_err_code (ip_err3, udp_err3, csum_err3);
1132 b3->error = node->errors[error3];
1136 vnet_update_l2_len (b0);
1137 vnet_update_l2_len (b1);
1138 vnet_update_l2_len (b2);
1139 vnet_update_l2_len (b3);
1141 ASSERT (b0->flow_id != 0);
1142 ASSERT (b1->flow_id != 0);
1143 ASSERT (b2->flow_id != 0);
1144 ASSERT (b3->flow_id != 0);
1146 u32 t_index0 = b0->flow_id - vxm->flow_id_start;
1147 u32 t_index1 = b1->flow_id - vxm->flow_id_start;
1148 u32 t_index2 = b2->flow_id - vxm->flow_id_start;
1149 u32 t_index3 = b3->flow_id - vxm->flow_id_start;
1151 vxlan_tunnel_t *t0 = &vxm->tunnels[t_index0];
1152 vxlan_tunnel_t *t1 = &vxm->tunnels[t_index1];
1153 vxlan_tunnel_t *t2 = &vxm->tunnels[t_index2];
1154 vxlan_tunnel_t *t3 = &vxm->tunnels[t_index3];
1156 /* flow id consumed */
1162 u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1164 u32 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX] =
1166 u32 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_RX] =
1168 u32 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_RX] =
1171 vlib_increment_combined_counter (rx_counter[next0], thread_index,
1172 sw_if_index0, 1, len0);
1173 vlib_increment_combined_counter (rx_counter[next1], thread_index,
1174 sw_if_index1, 1, len1);
1175 vlib_increment_combined_counter (rx_counter[next2], thread_index,
1176 sw_if_index2, 1, len2);
1177 vlib_increment_combined_counter (rx_counter[next3], thread_index,
1178 sw_if_index3, 1, len3);
1180 u32 flags = b0->flags | b1->flags | b2->flags | b3->flags;
1182 if (PREDICT_FALSE (flags & VLIB_BUFFER_IS_TRACED))
1184 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1186 vxlan_rx_trace_t *tr =
1187 vlib_add_trace (vm, node, b0, sizeof *tr);
1188 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1189 tr->next_index = next0;
1191 tr->tunnel_index = t_index0;
1194 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1196 vxlan_rx_trace_t *tr =
1197 vlib_add_trace (vm, node, b1, sizeof *tr);
1198 u8 error1 = vxlan_err_code (ip_err1, udp_err1, csum_err1);
1199 tr->next_index = next1;
1201 tr->tunnel_index = t_index1;
1204 if (b2->flags & VLIB_BUFFER_IS_TRACED)
1206 vxlan_rx_trace_t *tr =
1207 vlib_add_trace (vm, node, b2, sizeof *tr);
1208 u8 error2 = vxlan_err_code (ip_err2, udp_err2, csum_err2);
1209 tr->next_index = next2;
1211 tr->tunnel_index = t_index2;
1214 if (b3->flags & VLIB_BUFFER_IS_TRACED)
1216 vxlan_rx_trace_t *tr =
1217 vlib_add_trace (vm, node, b3, sizeof *tr);
1218 u8 error3 = vxlan_err_code (ip_err3, udp_err3, csum_err3);
1219 tr->next_index = next3;
1221 tr->tunnel_index = t_index3;
1225 vlib_validate_buffer_enqueue_x4
1226 (vm, node, next_index, to_next, n_left_to_next,
1227 bi0, bi1, bi2, bi3, next0, next1, next2, next3);
1229 while (n_left_from > 0 && n_left_to_next > 0)
1231 u32 bi0 = to_next[0] = from[0];
1237 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
1238 vlib_buffer_advance (b0, payload_offset);
1240 u16 len0 = vlib_buffer_length_in_chain (vm, b0);
1241 u32 next0 = VXLAN_FLOW_NEXT_L2_INPUT;
1243 u8 ip_err0 = vxlan_check_ip (b0, len0);
1244 u8 udp_err0 = vxlan_check_ip_udp_len (b0);
1245 u8 csum_err0 = vxlan_check_udp_csum (vm, b0);
1248 csum_err0 = !vxlan_validate_udp_csum (vm, b0);
1249 if (ip_err0 || udp_err0 || csum_err0)
1251 next0 = VXLAN_FLOW_NEXT_DROP;
1252 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1253 b0->error = node->errors[error0];
1256 vnet_update_l2_len (b0);
1258 ASSERT (b0->flow_id != 0);
1259 u32 t_index0 = b0->flow_id - vxm->flow_id_start;
1260 vxlan_tunnel_t *t0 = &vxm->tunnels[t_index0];
1263 u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1265 vlib_increment_combined_counter (rx_counter[next0], thread_index,
1266 sw_if_index0, 1, len0);
1268 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1270 vxlan_rx_trace_t *tr =
1271 vlib_add_trace (vm, node, b0, sizeof *tr);
1272 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1273 tr->next_index = next0;
1275 tr->tunnel_index = t_index0;
1278 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1279 to_next, n_left_to_next,
1283 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1286 return f->n_vectors;
1290 #ifndef CLIB_MULTIARCH_VARIANT
1291 VLIB_REGISTER_NODE (vxlan4_flow_input_node) = {
1292 .name = "vxlan-flow-input",
1293 .type = VLIB_NODE_TYPE_INTERNAL,
1294 .vector_size = sizeof (u32),
1296 .format_trace = format_vxlan_rx_trace,
1298 .n_errors = VXLAN_FLOW_N_ERROR,
1299 .error_strings = vxlan_flow_error_strings,
1301 .n_next_nodes = VXLAN_FLOW_N_NEXT,
1303 #define _(s,n) [VXLAN_FLOW_NEXT_##s] = n,
1304 foreach_vxlan_flow_input_next
1312 * fd.io coding-style-patch-verification: ON
1315 * eval: (c-set-style "gnu")