2 * decap.c - decapsulate VXLAN GPE
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.
19 * @brief Functions for decapsulating VXLAN GPE tunnels
23 #include <vlib/vlib.h>
24 #include <vnet/pg/pg.h>
25 #include <vnet/vxlan-gpe/vxlan_gpe.h>
27 vlib_node_registration_t vxlan_gpe_input_node;
30 * @brief Struct for VXLAN GPE decap packet tracing
37 } vxlan_gpe_rx_trace_t;
40 * @brief Tracing function for VXLAN GPE packet decapsulation
48 static u8 * format_vxlan_gpe_rx_trace (u8 * s, va_list * args)
50 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
51 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
52 vxlan_gpe_rx_trace_t * t = va_arg (*args, vxlan_gpe_rx_trace_t *);
54 if (t->tunnel_index != ~0)
56 s = format (s, "VXLAN-GPE: tunnel %d next %d error %d", t->tunnel_index,
57 t->next_index, t->error);
61 s = format (s, "VXLAN-GPE: no tunnel next %d error %d\n", t->next_index,
68 * @brief Tracing function for VXLAN GPE packet decapsulation including length
76 static u8 * format_vxlan_gpe_with_length (u8 * s, va_list * args)
78 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
79 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
86 * @brief Common processing for IPv4 and IPv6 VXLAN GPE decap dispatch functions
88 * It is worth noting that other than trivial UDP forwarding (transit), VXLAN GPE
89 * tunnels are "terminate local". This means that there is no "TX" interface for this
90 * decap case, so that field in the buffer_metadata can be "used for something else".
91 * The something else in this case is, for the IPv4/IPv6 inner-packet type case, the
92 * FIB index used to look up the inner-packet's adjacency.
94 * vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
101 * @return from_frame->n_vectors
105 vxlan_gpe_input (vlib_main_t * vm,
106 vlib_node_runtime_t * node,
107 vlib_frame_t * from_frame,
110 u32 n_left_from, next_index, *from, *to_next;
111 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
112 vnet_main_t * vnm = ngm->vnet_main;
113 vnet_interface_main_t * im = &vnm->interface_main;
114 u32 last_tunnel_index = ~0;
115 vxlan4_gpe_tunnel_key_t last_key4;
116 vxlan6_gpe_tunnel_key_t last_key6;
117 u32 pkts_decapsulated = 0;
118 u32 cpu_index = os_get_cpu_number ();
119 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
122 memset (&last_key4, 0xff, sizeof(last_key4));
124 memset (&last_key6, 0xff, sizeof(last_key6));
126 from = vlib_frame_vector_args (from_frame);
127 n_left_from = from_frame->n_vectors;
129 next_index = node->cached_next_index;
130 stats_sw_if_index = node->runtime_data[0];
131 stats_n_packets = stats_n_bytes = 0;
133 while (n_left_from > 0)
137 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
139 while (n_left_from >= 4 && n_left_to_next >= 2)
142 vlib_buffer_t * b0, *b1;
144 ip4_vxlan_gpe_header_t * iuvn4_0, *iuvn4_1;
145 ip6_vxlan_gpe_header_t * iuvn6_0, *iuvn6_1;
147 u32 tunnel_index0, tunnel_index1;
148 vxlan_gpe_tunnel_t * t0, *t1;
149 vxlan4_gpe_tunnel_key_t key4_0, key4_1;
150 vxlan6_gpe_tunnel_key_t key6_0, key6_1;
152 u32 sw_if_index0, sw_if_index1, len0, len1;
154 /* Prefetch next iteration. */
156 vlib_buffer_t * p2, *p3;
158 p2 = vlib_get_buffer (vm, from[2]);
159 p3 = vlib_get_buffer (vm, from[3]);
161 vlib_prefetch_buffer_header(p2, LOAD);
162 vlib_prefetch_buffer_header(p3, LOAD);
164 CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
165 CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
177 b0 = vlib_get_buffer (vm, bi0);
178 b1 = vlib_get_buffer (vm, bi1);
182 /* udp leaves current_data pointing at the vxlan-gpe header */
183 vlib_buffer_advance (b0, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
184 vlib_buffer_advance (b1, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
186 iuvn4_0 = vlib_buffer_get_current (b0);
187 iuvn4_1 = vlib_buffer_get_current (b1);
189 /* pop (ip, udp, vxlan) */
190 vlib_buffer_advance (b0, sizeof(*iuvn4_0));
191 vlib_buffer_advance (b1, sizeof(*iuvn4_1));
195 /* udp leaves current_data pointing at the vxlan-gpe header */
196 vlib_buffer_advance (b0, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
197 vlib_buffer_advance (b1, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
199 iuvn6_0 = vlib_buffer_get_current (b0);
200 iuvn6_1 = vlib_buffer_get_current (b1);
202 /* pop (ip, udp, vxlan) */
203 vlib_buffer_advance (b0, sizeof(*iuvn6_0));
204 vlib_buffer_advance (b1, sizeof(*iuvn6_1));
214 next0 = (iuvn4_0->vxlan.protocol < node->n_next_nodes) ?
215 iuvn4_0->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
216 next1 = (iuvn4_1->vxlan.protocol < node->n_next_nodes) ?
217 iuvn4_1->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
219 key4_0.local = iuvn4_0->ip4.dst_address.as_u32;
220 key4_1.local = iuvn4_1->ip4.dst_address.as_u32;
222 key4_0.remote = iuvn4_0->ip4.src_address.as_u32;
223 key4_1.remote = iuvn4_1->ip4.src_address.as_u32;
225 key4_0.vni = iuvn4_0->vxlan.vni_res;
226 key4_1.vni = iuvn4_1->vxlan.vni_res;
231 /* Processing for key4_0 */
232 if (PREDICT_FALSE((key4_0.as_u64[0] != last_key4.as_u64[0])
233 || (key4_0.as_u64[1] != last_key4.as_u64[1])))
235 p0 = hash_get_mem(ngm->vxlan4_gpe_tunnel_by_key, &key4_0);
239 error0 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
243 last_key4.as_u64[0] = key4_0.as_u64[0];
244 last_key4.as_u64[1] = key4_0.as_u64[1];
245 tunnel_index0 = last_tunnel_index = p0[0];
248 tunnel_index0 = last_tunnel_index;
252 next0 = (iuvn6_0->vxlan.protocol < node->n_next_nodes) ?
253 iuvn6_0->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
254 next1 = (iuvn6_1->vxlan.protocol < node->n_next_nodes) ?
255 iuvn6_1->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
257 key6_0.local.as_u64[0] = iuvn6_0->ip6.dst_address.as_u64[0];
258 key6_0.local.as_u64[1] = iuvn6_0->ip6.dst_address.as_u64[1];
259 key6_1.local.as_u64[0] = iuvn6_1->ip6.dst_address.as_u64[0];
260 key6_1.local.as_u64[1] = iuvn6_1->ip6.dst_address.as_u64[1];
262 key6_0.remote.as_u64[0] = iuvn6_0->ip6.src_address.as_u64[0];
263 key6_0.remote.as_u64[1] = iuvn6_0->ip6.src_address.as_u64[1];
264 key6_1.remote.as_u64[0] = iuvn6_1->ip6.src_address.as_u64[0];
265 key6_1.remote.as_u64[1] = iuvn6_1->ip6.src_address.as_u64[1];
267 key6_0.vni = iuvn6_0->vxlan.vni_res;
268 key6_1.vni = iuvn6_1->vxlan.vni_res;
270 /* Processing for key6_0 */
271 if (PREDICT_FALSE(memcmp (&key6_0, &last_key6, sizeof(last_key6)) != 0))
273 p0 = hash_get_mem(ngm->vxlan6_gpe_tunnel_by_key, &key6_0);
277 error0 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
281 memcpy (&last_key6, &key6_0, sizeof(key6_0));
282 tunnel_index0 = last_tunnel_index = p0[0];
285 tunnel_index0 = last_tunnel_index;
288 t0 = pool_elt_at_index(ngm->tunnels, tunnel_index0);
290 next0 = t0->protocol;
292 sw_if_index0 = t0->sw_if_index;
293 len0 = vlib_buffer_length_in_chain (vm, b0);
295 /* Required to make the l2 tag push / pop code work on l2 subifs */
296 vnet_update_l2_len (b0);
299 * ip[46] lookup in the configured FIB
301 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
304 stats_n_packets += 1;
305 stats_n_bytes += len0;
307 if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
309 stats_n_packets -= 1;
310 stats_n_bytes -= len0;
312 vlib_increment_combined_counter (
313 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
314 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
316 stats_n_bytes = len0;
317 stats_sw_if_index = sw_if_index0;
320 trace0: b0->error = error0 ? node->errors[error0] : 0;
322 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
324 vxlan_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof(*tr));
325 tr->next_index = next0;
327 tr->tunnel_index = tunnel_index0;
330 /* Process packet 1 */
333 /* Processing for key4_1 */
335 (key4_1.as_u64[0] != last_key4.as_u64[0])
336 || (key4_1.as_u64[1] != last_key4.as_u64[1])))
338 p1 = hash_get_mem(ngm->vxlan4_gpe_tunnel_by_key, &key4_1);
342 error1 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
346 last_key4.as_u64[0] = key4_1.as_u64[0];
347 last_key4.as_u64[1] = key4_1.as_u64[1];
348 tunnel_index1 = last_tunnel_index = p1[0];
351 tunnel_index1 = last_tunnel_index;
355 /* Processing for key6_1 */
356 if (PREDICT_FALSE(memcmp (&key6_1, &last_key6, sizeof(last_key6)) != 0))
358 p1 = hash_get_mem(ngm->vxlan6_gpe_tunnel_by_key, &key6_1);
362 error1 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
366 memcpy (&last_key6, &key6_1, sizeof(key6_1));
367 tunnel_index1 = last_tunnel_index = p1[0];
370 tunnel_index1 = last_tunnel_index;
373 t1 = pool_elt_at_index(ngm->tunnels, tunnel_index1);
375 next1 = t1->protocol;
376 sw_if_index1 = t1->sw_if_index;
377 len1 = vlib_buffer_length_in_chain (vm, b1);
379 /* Required to make the l2 tag push / pop code work on l2 subifs */
380 vnet_update_l2_len (b1);
383 * ip[46] lookup in the configured FIB
385 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
388 stats_n_packets += 1;
389 stats_n_bytes += len1;
391 /* Batch stats increment on the same vxlan tunnel so counter
392 is not incremented per packet */
393 if (PREDICT_FALSE(sw_if_index1 != stats_sw_if_index))
395 stats_n_packets -= 1;
396 stats_n_bytes -= len1;
398 vlib_increment_combined_counter (
399 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
400 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
402 stats_n_bytes = len1;
403 stats_sw_if_index = sw_if_index1;
405 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
407 trace1: b1->error = error1 ? node->errors[error1] : 0;
409 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
411 vxlan_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b1, sizeof(*tr));
412 tr->next_index = next1;
414 tr->tunnel_index = tunnel_index1;
417 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
418 n_left_to_next, bi0, bi1, next0, next1);
421 while (n_left_from > 0 && n_left_to_next > 0)
426 ip4_vxlan_gpe_header_t * iuvn4_0;
427 ip6_vxlan_gpe_header_t * iuvn6_0;
430 vxlan_gpe_tunnel_t * t0;
431 vxlan4_gpe_tunnel_key_t key4_0;
432 vxlan6_gpe_tunnel_key_t key6_0;
434 u32 sw_if_index0, len0;
443 b0 = vlib_get_buffer (vm, bi0);
447 /* udp leaves current_data pointing at the vxlan-gpe header */
448 vlib_buffer_advance (
449 b0, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
451 iuvn4_0 = vlib_buffer_get_current (b0);
453 /* pop (ip, udp, vxlan) */
454 vlib_buffer_advance (b0, sizeof(*iuvn4_0));
458 /* udp leaves current_data pointing at the vxlan-gpe header */
459 vlib_buffer_advance (
460 b0, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
462 iuvn6_0 = vlib_buffer_get_current (b0);
464 /* pop (ip, udp, vxlan) */
465 vlib_buffer_advance (b0, sizeof(*iuvn6_0));
474 (iuvn4_0->vxlan.protocol < node->n_next_nodes) ?
475 iuvn4_0->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
477 key4_0.local = iuvn4_0->ip4.dst_address.as_u32;
478 key4_0.remote = iuvn4_0->ip4.src_address.as_u32;
479 key4_0.vni = iuvn4_0->vxlan.vni_res;
482 /* Processing for key4_0 */
484 (key4_0.as_u64[0] != last_key4.as_u64[0])
485 || (key4_0.as_u64[1] != last_key4.as_u64[1])))
487 p0 = hash_get_mem(ngm->vxlan4_gpe_tunnel_by_key, &key4_0);
491 error0 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
495 last_key4.as_u64[0] = key4_0.as_u64[0];
496 last_key4.as_u64[1] = key4_0.as_u64[1];
497 tunnel_index0 = last_tunnel_index = p0[0];
500 tunnel_index0 = last_tunnel_index;
504 next0 = (iuvn6_0->vxlan.protocol < node->n_next_nodes) ?
505 iuvn6_0->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
507 key6_0.local.as_u64[0] = iuvn6_0->ip6.dst_address.as_u64[0];
508 key6_0.local.as_u64[1] = iuvn6_0->ip6.dst_address.as_u64[1];
509 key6_0.remote.as_u64[0] = iuvn6_0->ip6.src_address.as_u64[0];
510 key6_0.remote.as_u64[1] = iuvn6_0->ip6.src_address.as_u64[1];
511 key6_0.vni = iuvn6_0->vxlan.vni_res;
513 /* Processing for key6_0 */
514 if (PREDICT_FALSE(memcmp (&key6_0, &last_key6, sizeof(last_key6)) != 0))
516 p0 = hash_get_mem(ngm->vxlan6_gpe_tunnel_by_key, &key6_0);
520 error0 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
524 memcpy (&last_key6, &key6_0, sizeof(key6_0));
525 tunnel_index0 = last_tunnel_index = p0[0];
528 tunnel_index0 = last_tunnel_index;
531 t0 = pool_elt_at_index(ngm->tunnels, tunnel_index0);
533 next0 = t0->protocol;
535 sw_if_index0 = t0->sw_if_index;
536 len0 = vlib_buffer_length_in_chain (vm, b0);
538 /* Required to make the l2 tag push / pop code work on l2 subifs */
539 vnet_update_l2_len (b0);
542 * ip[46] lookup in the configured FIB
544 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
547 stats_n_packets += 1;
548 stats_n_bytes += len0;
550 /* Batch stats increment on the same vxlan-gpe tunnel so counter
551 is not incremented per packet */
552 if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
554 stats_n_packets -= 1;
555 stats_n_bytes -= len0;
557 vlib_increment_combined_counter (
558 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
559 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
561 stats_n_bytes = len0;
562 stats_sw_if_index = sw_if_index0;
565 trace00: b0->error = error0 ? node->errors[error0] : 0;
567 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
569 vxlan_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof(*tr));
570 tr->next_index = next0;
572 tr->tunnel_index = tunnel_index0;
574 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
575 n_left_to_next, bi0, next0);
578 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
580 vlib_node_increment_counter (vm, vxlan_gpe_input_node.index,
581 VXLAN_GPE_ERROR_DECAPSULATED, pkts_decapsulated);
582 /* Increment any remaining batch stats */
585 vlib_increment_combined_counter (
586 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, cpu_index,
587 stats_sw_if_index, stats_n_packets, stats_n_bytes);
588 node->runtime_data[0] = stats_sw_if_index;
590 return from_frame->n_vectors;
594 * @brief Graph processing dispatch function for IPv4 VXLAN GPE
596 * @node vxlan4-gpe-input
601 * @return from_frame->n_vectors
605 vxlan4_gpe_input (vlib_main_t * vm, vlib_node_runtime_t * node,
606 vlib_frame_t * from_frame)
608 return vxlan_gpe_input (vm, node, from_frame, /* is_ip4 */1);
612 * @brief Graph processing dispatch function for IPv6 VXLAN GPE
614 * @node vxlan6-gpe-input
619 * @return from_frame->n_vectors - uword
623 vxlan6_gpe_input (vlib_main_t * vm, vlib_node_runtime_t * node,
624 vlib_frame_t * from_frame)
626 return vxlan_gpe_input (vm, node, from_frame, /* is_ip4 */0);
630 * @brief VXLAN GPE error strings
632 static char * vxlan_gpe_error_strings[] = {
633 #define vxlan_gpe_error(n,s) s,
634 #include <vnet/vxlan-gpe/vxlan_gpe_error.def>
635 #undef vxlan_gpe_error
639 VLIB_REGISTER_NODE (vxlan4_gpe_input_node) = {
640 .function = vxlan4_gpe_input,
641 .name = "vxlan4-gpe-input",
642 /* Takes a vector of packets. */
643 .vector_size = sizeof (u32),
644 .type = VLIB_NODE_TYPE_INTERNAL,
645 .n_errors = ARRAY_LEN(vxlan_gpe_error_strings),
646 .error_strings = vxlan_gpe_error_strings,
648 .n_next_nodes = VXLAN_GPE_INPUT_N_NEXT,
650 #define _(s,n) [VXLAN_GPE_INPUT_NEXT_##s] = n,
651 foreach_vxlan_gpe_input_next
655 .format_buffer = format_vxlan_gpe_with_length,
656 .format_trace = format_vxlan_gpe_rx_trace,
657 // $$$$ .unformat_buffer = unformat_vxlan_gpe_header,
660 VLIB_NODE_FUNCTION_MULTIARCH (vxlan4_gpe_input_node, vxlan4_gpe_input);
662 VLIB_REGISTER_NODE (vxlan6_gpe_input_node) = {
663 .function = vxlan6_gpe_input,
664 .name = "vxlan6-gpe-input",
665 /* Takes a vector of packets. */
666 .vector_size = sizeof (u32),
667 .type = VLIB_NODE_TYPE_INTERNAL,
668 .n_errors = ARRAY_LEN(vxlan_gpe_error_strings),
669 .error_strings = vxlan_gpe_error_strings,
671 .n_next_nodes = VXLAN_GPE_INPUT_N_NEXT,
673 #define _(s,n) [VXLAN_GPE_INPUT_NEXT_##s] = n,
674 foreach_vxlan_gpe_input_next
678 .format_buffer = format_vxlan_gpe_with_length,
679 .format_trace = format_vxlan_gpe_rx_trace,
680 // $$$$ .unformat_buffer = unformat_vxlan_gpe_header,
683 VLIB_NODE_FUNCTION_MULTIARCH (vxlan6_gpe_input_node, vxlan6_gpe_input);