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 vlib_node_registration_t vxlan4_input_node;
23 vlib_node_registration_t vxlan6_input_node;
32 static u8 * format_vxlan_rx_trace (u8 * s, va_list * args)
34 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
35 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
36 vxlan_rx_trace_t * t = va_arg (*args, vxlan_rx_trace_t *);
38 if (t->tunnel_index != ~0)
40 s = format (s, "VXLAN decap from vxlan_tunnel%d vni %d next %d error %d",
41 t->tunnel_index, t->vni, t->next_index, t->error);
45 s = format (s, "VXLAN decap error - tunnel for vni %d does not exist",
52 vxlan_input (vlib_main_t * vm,
53 vlib_node_runtime_t * node,
54 vlib_frame_t * from_frame,
57 u32 n_left_from, next_index, * from, * to_next;
58 vxlan_main_t * vxm = &vxlan_main;
59 vnet_main_t * vnm = vxm->vnet_main;
60 vnet_interface_main_t * im = &vnm->interface_main;
61 u32 last_tunnel_index = ~0;
62 vxlan4_tunnel_key_t last_key4;
63 vxlan6_tunnel_key_t last_key6;
64 u32 pkts_decapsulated = 0;
65 u32 cpu_index = os_get_cpu_number();
66 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
69 last_key4.as_u64 = ~0;
71 memset (&last_key6, 0xff, sizeof (last_key6));
73 from = vlib_frame_vector_args (from_frame);
74 n_left_from = from_frame->n_vectors;
76 next_index = node->cached_next_index;
77 stats_sw_if_index = node->runtime_data[0];
78 stats_n_packets = stats_n_bytes = 0;
80 while (n_left_from > 0)
84 vlib_get_next_frame (vm, node, next_index,
85 to_next, n_left_to_next);
86 while (n_left_from >= 4 && n_left_to_next >= 2)
89 vlib_buffer_t * b0, * b1;
91 ip4_header_t * ip4_0, * ip4_1;
92 ip6_header_t * ip6_0, * ip6_1;
93 vxlan_header_t * vxlan0, * vxlan1;
95 u32 tunnel_index0, tunnel_index1;
96 vxlan_tunnel_t * t0, * t1, * mt0 = NULL, * mt1 = NULL;
97 vxlan4_tunnel_key_t key4_0, key4_1;
98 vxlan6_tunnel_key_t key6_0, key6_1;
100 u32 sw_if_index0, sw_if_index1, len0, len1;
102 /* Prefetch next iteration. */
104 vlib_buffer_t * p2, * p3;
106 p2 = vlib_get_buffer (vm, from[2]);
107 p3 = vlib_get_buffer (vm, from[3]);
109 vlib_prefetch_buffer_header (p2, LOAD);
110 vlib_prefetch_buffer_header (p3, LOAD);
112 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
113 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
125 b0 = vlib_get_buffer (vm, bi0);
126 b1 = vlib_get_buffer (vm, bi1);
128 /* udp leaves current_data pointing at the vxlan header */
129 vxlan0 = vlib_buffer_get_current (b0);
130 vxlan1 = vlib_buffer_get_current (b1);
133 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
135 (b1, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
136 ip4_0 = vlib_buffer_get_current (b0);
137 ip4_1 = vlib_buffer_get_current (b1);
140 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip6_header_t)));
142 (b1, -(word)(sizeof(udp_header_t)+sizeof(ip6_header_t)));
143 ip6_0 = vlib_buffer_get_current (b0);
144 ip6_1 = vlib_buffer_get_current (b1);
147 /* pop (ip, udp, vxlan) */
150 (b0, sizeof(*ip4_0)+sizeof(udp_header_t)+sizeof(*vxlan0));
152 (b1, sizeof(*ip4_1)+sizeof(udp_header_t)+sizeof(*vxlan1));
155 (b0, sizeof(*ip6_0)+sizeof(udp_header_t)+sizeof(*vxlan0));
157 (b1, sizeof(*ip6_1)+sizeof(udp_header_t)+sizeof(*vxlan1));
166 if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
168 error0 = VXLAN_ERROR_BAD_FLAGS;
169 next0 = VXLAN_INPUT_NEXT_DROP;
174 key4_0.src = ip4_0->src_address.as_u32;
175 key4_0.vni = vxlan0->vni_reserved;
177 /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
178 if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64))
180 p0 = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64);
181 if (PREDICT_FALSE (p0 == NULL))
183 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
184 next0 = VXLAN_INPUT_NEXT_DROP;
187 last_key4.as_u64 = key4_0.as_u64;
188 tunnel_index0 = last_tunnel_index = p0[0];
191 tunnel_index0 = last_tunnel_index;
192 t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
194 /* Validate VXLAN tunnel SIP against packet DIP */
195 if (PREDICT_TRUE (ip4_0->dst_address.as_u32 == t0->src.ip4.as_u32))
196 goto next0; /* valid packet */
197 if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_0->dst_address)))
199 key4_0.src = ip4_0->dst_address.as_u32;
200 key4_0.vni = vxlan0->vni_reserved;
201 /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */
202 p0 = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64);
203 if (PREDICT_TRUE (p0 != NULL))
205 mt0 = pool_elt_at_index (vxm->tunnels, p0[0]);
206 goto next0; /* valid packet */
209 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
210 next0 = VXLAN_INPUT_NEXT_DROP;
213 } else /* !is_ip4 */ {
214 key6_0.src.as_u64[0] = ip6_0->src_address.as_u64[0];
215 key6_0.src.as_u64[1] = ip6_0->src_address.as_u64[1];
216 key6_0.vni = vxlan0->vni_reserved;
218 /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
219 if (PREDICT_FALSE (memcmp(&key6_0, &last_key6, sizeof(last_key6)) != 0))
221 p0 = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_0);
222 if (PREDICT_FALSE (p0 == NULL))
224 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
225 next0 = VXLAN_INPUT_NEXT_DROP;
228 clib_memcpy (&last_key6, &key6_0, sizeof(key6_0));
229 tunnel_index0 = last_tunnel_index = p0[0];
232 tunnel_index0 = last_tunnel_index;
233 t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
235 /* Validate VXLAN tunnel SIP against packet DIP */
236 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address,
238 goto next0; /* valid packet */
239 if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_0->dst_address)))
241 key6_0.src.as_u64[0] = ip6_0->dst_address.as_u64[0];
242 key6_0.src.as_u64[1] = ip6_0->dst_address.as_u64[1];
243 key6_0.vni = vxlan0->vni_reserved;
244 p0 = hash_get (vxm->vxlan6_tunnel_by_key, &key6_0);
245 if (PREDICT_TRUE (p0 != NULL))
247 mt0 = pool_elt_at_index (vxm->tunnels, p0[0]);
248 goto next0; /* valid packet */
251 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
252 next0 = VXLAN_INPUT_NEXT_DROP;
257 next0 = t0->decap_next_index;
258 sw_if_index0 = t0->sw_if_index;
259 len0 = vlib_buffer_length_in_chain (vm, b0);
261 /* Required to make the l2 tag push / pop code work on l2 subifs */
262 if (PREDICT_TRUE(next0 == VXLAN_INPUT_NEXT_L2_INPUT))
263 vnet_update_l2_len (b0);
265 /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
266 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
267 sw_if_index0 = (mt0) ? mt0->sw_if_index : sw_if_index0;
269 pkts_decapsulated ++;
270 stats_n_packets += 1;
271 stats_n_bytes += len0;
273 /* Batch stats increment on the same vxlan tunnel so counter
274 is not incremented per packet */
275 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
277 stats_n_packets -= 1;
278 stats_n_bytes -= len0;
280 vlib_increment_combined_counter
281 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
282 cpu_index, stats_sw_if_index,
283 stats_n_packets, stats_n_bytes);
285 stats_n_bytes = len0;
286 stats_sw_if_index = sw_if_index0;
290 b0->error = error0 ? node->errors[error0] : 0;
292 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
295 = vlib_add_trace (vm, node, b0, sizeof (*tr));
296 tr->next_index = next0;
298 tr->tunnel_index = tunnel_index0;
299 tr->vni = vnet_get_vni (vxlan0);
302 if (PREDICT_FALSE (vxlan1->flags != VXLAN_FLAGS_I))
304 error1 = VXLAN_ERROR_BAD_FLAGS;
305 next1 = VXLAN_INPUT_NEXT_DROP;
310 key4_1.src = ip4_1->src_address.as_u32;
311 key4_1.vni = vxlan1->vni_reserved;
313 /* Make sure unicast VXLAN tunnel exist by packet SIP and VNI */
314 if (PREDICT_FALSE (key4_1.as_u64 != last_key4.as_u64))
316 p1 = hash_get (vxm->vxlan4_tunnel_by_key, key4_1.as_u64);
317 if (PREDICT_FALSE (p1 == NULL))
319 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
320 next1 = VXLAN_INPUT_NEXT_DROP;
323 last_key4.as_u64 = key4_1.as_u64;
324 tunnel_index1 = last_tunnel_index = p1[0];
327 tunnel_index1 = last_tunnel_index;
328 t1 = pool_elt_at_index (vxm->tunnels, tunnel_index1);
330 /* Validate VXLAN tunnel SIP against packet DIP */
331 if (PREDICT_TRUE (ip4_1->dst_address.as_u32 == t1->src.ip4.as_u32))
332 goto next1; /* valid packet */
333 if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_1->dst_address)))
335 key4_1.src = ip4_1->dst_address.as_u32;
336 key4_1.vni = vxlan1->vni_reserved;
337 /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */
338 p1 = hash_get (vxm->vxlan4_tunnel_by_key, key4_1.as_u64);
339 if (PREDICT_TRUE (p1 != NULL))
341 mt1 = pool_elt_at_index (vxm->tunnels, p1[0]);
342 goto next1; /* valid packet */
345 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
346 next1 = VXLAN_INPUT_NEXT_DROP;
349 } else /* !is_ip4 */ {
350 key6_1.src.as_u64[0] = ip6_1->src_address.as_u64[0];
351 key6_1.src.as_u64[1] = ip6_1->src_address.as_u64[1];
352 key6_1.vni = vxlan1->vni_reserved;
354 /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
355 if (PREDICT_FALSE (memcmp(&key6_1, &last_key6, sizeof(last_key6)) != 0))
357 p1 = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_1);
359 if (PREDICT_FALSE (p1 == NULL))
361 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
362 next1 = VXLAN_INPUT_NEXT_DROP;
366 clib_memcpy (&last_key6, &key6_1, sizeof(key6_1));
367 tunnel_index1 = last_tunnel_index = p1[0];
370 tunnel_index1 = last_tunnel_index;
371 t1 = pool_elt_at_index (vxm->tunnels, tunnel_index1);
373 /* Validate VXLAN tunnel SIP against packet DIP */
374 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_1->dst_address,
376 goto next1; /* valid packet */
377 if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_1->dst_address)))
379 key6_1.src.as_u64[0] = ip6_1->dst_address.as_u64[0];
380 key6_1.src.as_u64[1] = ip6_1->dst_address.as_u64[1];
381 key6_1.vni = vxlan1->vni_reserved;
382 p1 = hash_get (vxm->vxlan6_tunnel_by_key, &key6_1);
383 if (PREDICT_TRUE (p1 != NULL))
385 mt1 = pool_elt_at_index (vxm->tunnels, p1[0]);
386 goto next1; /* valid packet */
389 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
390 next1 = VXLAN_INPUT_NEXT_DROP;
395 next1 = t1->decap_next_index;
396 sw_if_index1 = t1->sw_if_index;
397 len1 = vlib_buffer_length_in_chain (vm, b1);
399 /* Required to make the l2 tag push / pop code work on l2 subifs */
400 if (PREDICT_TRUE(next1 == VXLAN_INPUT_NEXT_L2_INPUT))
401 vnet_update_l2_len (b1);
403 /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
404 vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
405 sw_if_index1 = (mt1) ? mt1->sw_if_index : sw_if_index1;
407 pkts_decapsulated ++;
408 stats_n_packets += 1;
409 stats_n_bytes += len1;
411 /* Batch stats increment on the same vxlan tunnel so counter
412 is not incremented per packet */
413 if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
415 stats_n_packets -= 1;
416 stats_n_bytes -= len1;
418 vlib_increment_combined_counter
419 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
420 cpu_index, stats_sw_if_index,
421 stats_n_packets, stats_n_bytes);
423 stats_n_bytes = len1;
424 stats_sw_if_index = sw_if_index1;
428 b1->error = error1 ? node->errors[error1] : 0;
430 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
433 = vlib_add_trace (vm, node, b1, sizeof (*tr));
434 tr->next_index = next1;
436 tr->tunnel_index = tunnel_index1;
437 tr->vni = vnet_get_vni (vxlan1);
440 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
441 to_next, n_left_to_next,
442 bi0, bi1, next0, next1);
445 while (n_left_from > 0 && n_left_to_next > 0)
450 ip4_header_t * ip4_0;
451 ip6_header_t * ip6_0;
452 vxlan_header_t * vxlan0;
455 vxlan_tunnel_t * t0, * mt0 = NULL;
456 vxlan4_tunnel_key_t key4_0;
457 vxlan6_tunnel_key_t key6_0;
459 u32 sw_if_index0, len0;
468 b0 = vlib_get_buffer (vm, bi0);
470 /* udp leaves current_data pointing at the vxlan header */
471 vxlan0 = vlib_buffer_get_current (b0);
474 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
475 ip4_0 = vlib_buffer_get_current (b0);
478 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip6_header_t)));
479 ip6_0 = vlib_buffer_get_current (b0);
482 /* pop (ip, udp, vxlan) */
485 (b0, sizeof(*ip4_0)+sizeof(udp_header_t)+sizeof(*vxlan0));
488 (b0, sizeof(*ip6_0)+sizeof(udp_header_t)+sizeof(*vxlan0));
494 if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
496 error0 = VXLAN_ERROR_BAD_FLAGS;
497 next0 = VXLAN_INPUT_NEXT_DROP;
502 key4_0.src = ip4_0->src_address.as_u32;
503 key4_0.vni = vxlan0->vni_reserved;
505 /* Make sure unicast VXLAN tunnel exist by packet SIP and VNI */
506 if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64))
508 p0 = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64);
509 if (PREDICT_FALSE (p0 == NULL))
511 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
512 next0 = VXLAN_INPUT_NEXT_DROP;
515 last_key4.as_u64 = key4_0.as_u64;
516 tunnel_index0 = last_tunnel_index = p0[0];
519 tunnel_index0 = last_tunnel_index;
520 t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
522 /* Validate VXLAN tunnel SIP against packet DIP */
523 if (PREDICT_TRUE (ip4_0->dst_address.as_u32 == t0->src.ip4.as_u32))
524 goto next00; /* valid packet */
525 if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_0->dst_address)))
527 key4_0.src = ip4_0->dst_address.as_u32;
528 key4_0.vni = vxlan0->vni_reserved;
529 /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */
530 p0 = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64);
531 if (PREDICT_TRUE (p0 != NULL))
533 mt0 = pool_elt_at_index (vxm->tunnels, p0[0]);
534 goto next00; /* valid packet */
537 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
538 next0 = VXLAN_INPUT_NEXT_DROP;
541 } else /* !is_ip4 */ {
542 key6_0.src.as_u64[0] = ip6_0->src_address.as_u64[0];
543 key6_0.src.as_u64[1] = ip6_0->src_address.as_u64[1];
544 key6_0.vni = vxlan0->vni_reserved;
546 /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
547 if (PREDICT_FALSE (memcmp(&key6_0, &last_key6, sizeof(last_key6)) != 0))
549 p0 = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_0);
550 if (PREDICT_FALSE (p0 == NULL))
552 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
553 next0 = VXLAN_INPUT_NEXT_DROP;
556 clib_memcpy (&last_key6, &key6_0, sizeof(key6_0));
557 tunnel_index0 = last_tunnel_index = p0[0];
560 tunnel_index0 = last_tunnel_index;
561 t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
563 /* Validate VXLAN tunnel SIP against packet DIP */
564 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address,
566 goto next00; /* valid packet */
567 if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_0->dst_address)))
569 key6_0.src.as_u64[0] = ip6_0->dst_address.as_u64[0];
570 key6_0.src.as_u64[1] = ip6_0->dst_address.as_u64[1];
571 key6_0.vni = vxlan0->vni_reserved;
572 p0 = hash_get (vxm->vxlan6_tunnel_by_key, &key6_0);
573 if (PREDICT_TRUE (p0 != NULL))
575 mt0 = pool_elt_at_index (vxm->tunnels, p0[0]);
576 goto next00; /* valid packet */
579 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
580 next0 = VXLAN_INPUT_NEXT_DROP;
585 next0 = t0->decap_next_index;
586 sw_if_index0 = t0->sw_if_index;
587 len0 = vlib_buffer_length_in_chain (vm, b0);
589 /* Required to make the l2 tag push / pop code work on l2 subifs */
590 if (PREDICT_TRUE(next0 == VXLAN_INPUT_NEXT_L2_INPUT))
591 vnet_update_l2_len (b0);
593 /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
594 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
595 sw_if_index0 = (mt0) ? mt0->sw_if_index : sw_if_index0;
597 pkts_decapsulated ++;
598 stats_n_packets += 1;
599 stats_n_bytes += len0;
601 /* Batch stats increment on the same vxlan tunnel so counter
602 is not incremented per packet */
603 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
605 stats_n_packets -= 1;
606 stats_n_bytes -= len0;
608 vlib_increment_combined_counter
609 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
610 cpu_index, stats_sw_if_index,
611 stats_n_packets, stats_n_bytes);
613 stats_n_bytes = len0;
614 stats_sw_if_index = sw_if_index0;
618 b0->error = error0 ? node->errors[error0] : 0;
620 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
623 = vlib_add_trace (vm, node, b0, sizeof (*tr));
624 tr->next_index = next0;
626 tr->tunnel_index = tunnel_index0;
627 tr->vni = vnet_get_vni (vxlan0);
629 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
630 to_next, n_left_to_next,
634 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
636 /* Do we still need this now that tunnel tx stats is kept? */
637 vlib_node_increment_counter (vm, is_ip4?
638 vxlan4_input_node.index:vxlan6_input_node.index,
639 VXLAN_ERROR_DECAPSULATED,
642 /* Increment any remaining batch stats */
645 vlib_increment_combined_counter
646 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
647 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
648 node->runtime_data[0] = stats_sw_if_index;
651 return from_frame->n_vectors;
655 vxlan4_input (vlib_main_t * vm,
656 vlib_node_runtime_t * node,
657 vlib_frame_t * from_frame)
659 return vxlan_input(vm, node, from_frame, /* is_ip4 */ 1);
663 vxlan6_input (vlib_main_t * vm,
664 vlib_node_runtime_t * node,
665 vlib_frame_t * from_frame)
667 return vxlan_input(vm, node, from_frame, /* is_ip4 */ 0);
670 static char * vxlan_error_strings[] = {
671 #define vxlan_error(n,s) s,
672 #include <vnet/vxlan/vxlan_error.def>
677 VLIB_REGISTER_NODE (vxlan4_input_node) = {
678 .function = vxlan4_input,
679 .name = "vxlan4-input",
680 /* Takes a vector of packets. */
681 .vector_size = sizeof (u32),
683 .n_errors = VXLAN_N_ERROR,
684 .error_strings = vxlan_error_strings,
686 .n_next_nodes = VXLAN_INPUT_N_NEXT,
688 #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
689 foreach_vxlan_input_next
693 //temp .format_buffer = format_vxlan_header,
694 .format_trace = format_vxlan_rx_trace,
695 // $$$$ .unformat_buffer = unformat_vxlan_header,
698 VLIB_NODE_FUNCTION_MULTIARCH (vxlan4_input_node, vxlan4_input)
700 VLIB_REGISTER_NODE (vxlan6_input_node) = {
701 .function = vxlan6_input,
702 .name = "vxlan6-input",
703 /* Takes a vector of packets. */
704 .vector_size = sizeof (u32),
706 .n_errors = VXLAN_N_ERROR,
707 .error_strings = vxlan_error_strings,
709 .n_next_nodes = VXLAN_INPUT_N_NEXT,
711 #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
712 foreach_vxlan_input_next
716 //temp .format_buffer = format_vxlan_header,
717 .format_trace = format_vxlan_rx_trace,
718 // $$$$ .unformat_buffer = unformat_vxlan_header,
721 VLIB_NODE_FUNCTION_MULTIARCH (vxlan6_input_node, vxlan6_input)
725 IP_VXLAN_BYPASS_NEXT_DROP,
726 IP_VXLAN_BYPASS_NEXT_VXLAN,
727 IP_VXLAN_BYPASS_N_NEXT,
728 } ip_vxan_bypass_next_t;
731 ip_vxlan_bypass_inline (vlib_main_t * vm,
732 vlib_node_runtime_t * node,
733 vlib_frame_t * frame,
736 vxlan_main_t * vxm = &vxlan_main;
737 u32 * from, * to_next, n_left_from, n_left_to_next, next_index;
738 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
739 ip4_address_t addr4; /* last IPv4 address matching a local VTEP address */
740 ip6_address_t addr6; /* last IPv6 address matching a local VTEP address */
742 from = vlib_frame_vector_args (frame);
743 n_left_from = frame->n_vectors;
744 next_index = node->cached_next_index;
746 if (node->flags & VLIB_NODE_FLAG_TRACE)
747 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
749 if (is_ip4) addr4.data_u32 = ~0;
750 else ip6_address_set_zero (&addr6);
752 while (n_left_from > 0)
754 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
756 while (n_left_from >= 4 && n_left_to_next >= 2)
758 vlib_buffer_t * b0, * b1;
759 ip4_header_t * ip0, * ip1;
760 udp_header_t * udp0, * udp1;
761 u32 bi0, ip_len0, udp_len0, flags0, next0;
762 u32 bi1, ip_len1, udp_len1, flags1, next1;
763 i32 len_diff0, len_diff1;
764 u8 error0, good_udp0, proto0;
765 u8 error1, good_udp1, proto1;
767 /* Prefetch next iteration. */
769 vlib_buffer_t * p2, * p3;
771 p2 = vlib_get_buffer (vm, from[2]);
772 p3 = vlib_get_buffer (vm, from[3]);
774 vlib_prefetch_buffer_header (p2, LOAD);
775 vlib_prefetch_buffer_header (p3, LOAD);
777 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
778 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
781 bi0 = to_next[0] = from[0];
782 bi1 = to_next[1] = from[1];
788 b0 = vlib_get_buffer (vm, bi0);
789 b1 = vlib_get_buffer (vm, bi1);
790 ip0 = vlib_buffer_get_current (b0);
791 ip1 = vlib_buffer_get_current (b1);
793 /* Setup packet for next IP feature */
794 vnet_feature_next(vnet_buffer(b0)->sw_if_index[VLIB_RX], &next0, b0);
795 vnet_feature_next(vnet_buffer(b1)->sw_if_index[VLIB_RX], &next1, b1);
797 /* Treat IP frag packets as "experimental" protocol for now
798 until support of IP frag reassembly is implemented */
799 proto0 = ip4_is_fragment(ip0) ? 0xfe : ip0->protocol;
800 proto1 = ip4_is_fragment(ip1) ? 0xfe : ip1->protocol;
802 /* Process packet 0 */
803 if (proto0 != IP_PROTOCOL_UDP)
804 goto exit0; /* not UDP packet */
806 udp0 = ip4_next_header (ip0);
807 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
808 goto exit0; /* not VXLAN packet */
812 if (addr4.as_u32 != ip0->dst_address.as_u32)
814 if (!hash_get (vxm->vtep4, ip0->dst_address.as_u32))
815 goto exit0; /* no local VTEP for VXLAN packet */
816 addr4 = ip0->dst_address;
819 else goto exit0; /* IP6 VXLAN bypass not yet supported */
821 /* vxlan-input node expect current at VXLAN header */
822 vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t));
825 good_udp0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
827 /* Don't verify UDP checksum for packets with explicit zero checksum. */
828 good_udp0 |= udp0->checksum == 0;
830 /* Verify UDP length */
831 ip_len0 = clib_net_to_host_u16 (ip0->length);
832 udp_len0 = clib_net_to_host_u16 (udp0->length);
834 len_diff0 = ip_len0 - udp_len0;
836 /* Verify UDP checksum */
837 if (PREDICT_FALSE (!good_udp0))
839 if (!(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED))
840 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
842 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
845 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
846 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
849 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
850 b0->error = error0 ? error_node->errors[error0] : 0;
853 /* Process packet 1 */
854 if (proto1 != IP_PROTOCOL_UDP)
855 goto exit1; /* not UDP packet */
857 udp1 = ip4_next_header (ip1);
858 if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
859 goto exit1; /* not VXLAN packet */
863 if (addr4.as_u32 != ip1->dst_address.as_u32)
865 if (!hash_get (vxm->vtep4, ip1->dst_address.as_u32))
866 goto exit1; /* no local VTEP for VXLAN packet */
867 addr4 = ip1->dst_address;
870 else goto exit1; /* IP6 VXLAN bypass not yet supported */
872 /* vxlan-input node expect current at VXLAN header */
873 vlib_buffer_advance (b1, sizeof(ip4_header_t)+sizeof(udp_header_t));
876 good_udp1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
878 /* Don't verify UDP checksum for packets with explicit zero checksum. */
879 good_udp1 |= udp1->checksum == 0;
881 /* Verify UDP length */
882 ip_len1 = clib_net_to_host_u16 (ip1->length);
883 udp_len1 = clib_net_to_host_u16 (udp1->length);
885 len_diff1 = ip_len1 - udp_len1;
887 /* Verify UDP checksum */
888 if (PREDICT_FALSE (!good_udp1))
890 if (!(flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED))
891 flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
893 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
896 error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM;
897 error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH;
900 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
901 b1->error = error1 ? error_node->errors[error1] : 0;
904 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
905 to_next, n_left_to_next,
906 bi0, bi1, next0, next1);
909 while (n_left_from > 0 && n_left_to_next > 0)
914 u32 bi0, ip_len0, udp_len0, flags0, next0;
916 u8 error0, good_udp0, proto0;
918 bi0 = to_next[0] = from[0];
924 b0 = vlib_get_buffer (vm, bi0);
925 ip0 = vlib_buffer_get_current (b0);
927 /* Setup packet for next IP feature */
928 vnet_feature_next(vnet_buffer(b0)->sw_if_index[VLIB_RX], &next0, b0);
930 /* Treat IP frag packets as "experimental" protocol for now
931 until support of IP frag reassembly is implemented */
932 proto0 = ip4_is_fragment(ip0) ? 0xfe : ip0->protocol;
934 if (proto0 != IP_PROTOCOL_UDP)
935 goto exit; /* not UDP packet */
937 udp0 = ip4_next_header (ip0);
938 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
939 goto exit; /* not VXLAN packet */
943 if (addr4.as_u32 != ip0->dst_address.as_u32)
945 if (!hash_get (vxm->vtep4, ip0->dst_address.as_u32))
946 goto exit; /* no local VTEP for VXLAN packet */
947 addr4 = ip0->dst_address;
950 else goto exit; /* IP6 VXLAN bypass not yet supported */
952 /* vxlan-input node expect current at VXLAN header */
953 vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t));
956 good_udp0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
958 /* Don't verify UDP checksum for packets with explicit zero checksum. */
959 good_udp0 |= udp0->checksum == 0;
961 /* Verify UDP length */
962 ip_len0 = clib_net_to_host_u16 (ip0->length);
963 udp_len0 = clib_net_to_host_u16 (udp0->length);
965 len_diff0 = ip_len0 - udp_len0;
967 /* Verify UDP checksum */
968 if (PREDICT_FALSE (!good_udp0))
970 if (!(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED))
971 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
973 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
976 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
977 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
980 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
981 b0->error = error0 ? error_node->errors[error0] : 0;
984 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
985 to_next, n_left_to_next,
989 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
992 return frame->n_vectors;
996 ip4_vxlan_bypass (vlib_main_t * vm,
997 vlib_node_runtime_t * node,
998 vlib_frame_t * frame)
1000 return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 1);
1003 VLIB_REGISTER_NODE (ip4_vxlan_bypass_node) = {
1004 .function = ip4_vxlan_bypass,
1005 .name = "ip4-vxlan-bypass",
1006 .vector_size = sizeof (u32),
1008 .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
1010 [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
1011 [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan4-input",
1014 .format_buffer = format_ip4_header,
1015 .format_trace = format_ip4_forward_next_trace,
1018 VLIB_NODE_FUNCTION_MULTIARCH (ip4_vxlan_bypass_node,ip4_vxlan_bypass)
1021 static clib_error_t *
1022 set_ip_vxlan_bypass (vlib_main_t * vm,
1023 unformat_input_t * input,
1024 vlib_cli_command_t * cmd)
1026 unformat_input_t _line_input, * line_input = &_line_input;
1027 vnet_main_t * vnm = vnet_get_main();
1028 clib_error_t * error = 0;
1029 u32 sw_if_index, is_del;
1034 if (! unformat_user (input, unformat_line_input, line_input))
1037 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1039 if (unformat_user (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1041 else if (unformat (line_input, "del"))
1045 error = unformat_parse_error (line_input);
1050 if (~0 == sw_if_index)
1052 error = clib_error_return (0, "unknown interface `%U'",
1053 format_unformat_error, line_input);
1057 vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-bypass", sw_if_index,
1064 * This command adds the 'ip4-vxlan-bypass' graph node for a given interface.
1065 * By adding the IPv4 vxlan-bypass graph node to an interface, the node checks
1066 * for and validate input vxlan packet and bypass ip4-lookup, ip4-local,
1067 * ip4-udp-lookup nodes to speedup vxlan packet forwarding. This node will
1068 * cause extra overhead to for non-vxlan packets which is kept at a minimum.
1072 * Example of graph node before ip4-vxlan-bypass is enabled:
1073 * @cliexstart{show vlib graph ip4-vxlan-bypass}
1074 * Name Next Previous
1075 * ip4-vxlan-bypass error-drop [0]
1080 * Example of how to enable ip4-vxlan-bypass on an interface:
1081 * @cliexcmd{set interface ip vxlan-bypass GigabitEthernet2/0/0}
1083 * Example of graph node after ip4-vxlan-bypass is enabled:
1084 * @cliexstart{show vlib graph ip4-vxlan-bypass}
1085 * Name Next Previous
1086 * ip4-vxlan-bypass error-drop [0] ip4-input
1087 * vxlan4-input [1] ip4-input-no-checksum
1091 * Example of how to display the feature enabed on an interface:
1092 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1093 * IP feature paths configured on GigabitEthernet2/0/0...
1100 * ip4-lookup-multicast
1115 * Example of how to disable unicast source checking on an interface:
1116 * @cliexcmd{set interface ip vxlan-bypass GigabitEthernet2/0/0 del}
1120 VLIB_CLI_COMMAND (set_interface_ip_vxlan_bypass_command, static) = {
1121 .path = "set interface ip vxlan-bypass",
1122 .function = set_ip_vxlan_bypass,
1123 .short_help = "set interface ip vxlan-bypass <interface> [del]",
1126 /* Dummy init function to get us linked in. */
1127 clib_error_t * ip4_vxlan_bypass_init (vlib_main_t * vm)
1130 VLIB_INIT_FUNCTION (ip4_vxlan_bypass_init);