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.
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/vxlan-gpe/vxlan_gpe.h>
22 vlib_node_registration_t vxlan_gpe_input_node;
28 } vxlan_gpe_rx_trace_t;
30 static u8 * format_vxlan_gpe_rx_trace (u8 * s, va_list * args)
32 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
33 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
34 vxlan_gpe_rx_trace_t * t = va_arg (*args, vxlan_gpe_rx_trace_t *);
36 if (t->tunnel_index != ~0)
38 s = format (s, "VXLAN-GPE: tunnel %d next %d error %d", t->tunnel_index,
39 t->next_index, t->error);
43 s = format (s, "VXLAN-GPE: no tunnel next %d error %d\n", t->next_index,
50 static u8 * format_vxlan_gpe_with_length (u8 * s, va_list * args)
52 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
60 vxlan_gpe_input (vlib_main_t * vm,
61 vlib_node_runtime_t * node,
62 vlib_frame_t * from_frame,
65 u32 n_left_from, next_index, *from, *to_next;
66 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
67 vnet_main_t * vnm = ngm->vnet_main;
68 vnet_interface_main_t * im = &vnm->interface_main;
69 u32 last_tunnel_index = ~0;
70 vxlan4_gpe_tunnel_key_t last_key4;
71 vxlan6_gpe_tunnel_key_t last_key6;
72 u32 pkts_decapsulated = 0;
73 u32 cpu_index = os_get_cpu_number ();
74 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
77 memset (&last_key4, 0xff, sizeof(last_key4));
79 memset (&last_key6, 0xff, sizeof(last_key6));
81 from = vlib_frame_vector_args (from_frame);
82 n_left_from = from_frame->n_vectors;
84 next_index = node->cached_next_index;
85 stats_sw_if_index = node->runtime_data[0];
86 stats_n_packets = stats_n_bytes = 0;
88 while (n_left_from > 0)
92 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
94 while (n_left_from >= 4 && n_left_to_next >= 2)
97 vlib_buffer_t * b0, *b1;
99 ip4_vxlan_gpe_header_t * iuvn4_0, *iuvn4_1;
100 ip6_vxlan_gpe_header_t * iuvn6_0, *iuvn6_1;
102 u32 tunnel_index0, tunnel_index1;
103 vxlan_gpe_tunnel_t * t0, *t1;
104 vxlan4_gpe_tunnel_key_t key4_0, key4_1;
105 vxlan6_gpe_tunnel_key_t key6_0, key6_1;
107 u32 sw_if_index0, sw_if_index1, len0, len1;
109 /* Prefetch next iteration. */
111 vlib_buffer_t * p2, *p3;
113 p2 = vlib_get_buffer (vm, from[2]);
114 p3 = vlib_get_buffer (vm, from[3]);
116 vlib_prefetch_buffer_header(p2, LOAD);
117 vlib_prefetch_buffer_header(p3, LOAD);
119 CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
120 CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
132 b0 = vlib_get_buffer (vm, bi0);
133 b1 = vlib_get_buffer (vm, bi1);
137 /* udp leaves current_data pointing at the vxlan-gpe header */
138 vlib_buffer_advance (b0, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
139 vlib_buffer_advance (b1, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
141 iuvn4_0 = vlib_buffer_get_current (b0);
142 iuvn4_1 = vlib_buffer_get_current (b1);
144 /* pop (ip, udp, vxlan) */
145 vlib_buffer_advance (b0, sizeof(*iuvn4_0));
146 vlib_buffer_advance (b1, sizeof(*iuvn4_1));
150 /* udp leaves current_data pointing at the vxlan-gpe header */
151 vlib_buffer_advance (b0, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
152 vlib_buffer_advance (b1, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
154 iuvn6_0 = vlib_buffer_get_current (b0);
155 iuvn6_1 = vlib_buffer_get_current (b1);
157 /* pop (ip, udp, vxlan) */
158 vlib_buffer_advance (b0, sizeof(*iuvn6_0));
159 vlib_buffer_advance (b1, sizeof(*iuvn6_1));
169 next0 = (iuvn4_0->vxlan.protocol < node->n_next_nodes) ?
170 iuvn4_0->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
171 next1 = (iuvn4_1->vxlan.protocol < node->n_next_nodes) ?
172 iuvn4_1->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
174 key4_0.local = iuvn4_0->ip4.dst_address.as_u32;
175 key4_1.local = iuvn4_1->ip4.dst_address.as_u32;
177 key4_0.remote = iuvn4_0->ip4.src_address.as_u32;
178 key4_1.remote = iuvn4_1->ip4.src_address.as_u32;
180 key4_0.vni = iuvn4_0->vxlan.vni_res;
181 key4_1.vni = iuvn4_1->vxlan.vni_res;
186 /* Processing for key4_0 */
187 if (PREDICT_FALSE((key4_0.as_u64[0] != last_key4.as_u64[0])
188 || (key4_0.as_u64[1] != last_key4.as_u64[1])))
190 p0 = hash_get_mem(ngm->vxlan4_gpe_tunnel_by_key, &key4_0);
194 error0 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
198 last_key4.as_u64[0] = key4_0.as_u64[0];
199 last_key4.as_u64[1] = key4_0.as_u64[1];
200 tunnel_index0 = last_tunnel_index = p0[0];
203 tunnel_index0 = last_tunnel_index;
207 next0 = (iuvn6_0->vxlan.protocol < node->n_next_nodes) ?
208 iuvn6_0->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
209 next1 = (iuvn6_1->vxlan.protocol < node->n_next_nodes) ?
210 iuvn6_1->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
212 key6_0.local.as_u64[0] = iuvn6_0->ip6.dst_address.as_u64[0];
213 key6_0.local.as_u64[1] = iuvn6_0->ip6.dst_address.as_u64[1];
214 key6_1.local.as_u64[0] = iuvn6_1->ip6.dst_address.as_u64[0];
215 key6_1.local.as_u64[1] = iuvn6_1->ip6.dst_address.as_u64[1];
217 key6_0.remote.as_u64[0] = iuvn6_0->ip6.src_address.as_u64[0];
218 key6_0.remote.as_u64[1] = iuvn6_0->ip6.src_address.as_u64[1];
219 key6_1.remote.as_u64[0] = iuvn6_1->ip6.src_address.as_u64[0];
220 key6_1.remote.as_u64[1] = iuvn6_1->ip6.src_address.as_u64[1];
222 key6_0.vni = iuvn6_0->vxlan.vni_res;
223 key6_1.vni = iuvn6_1->vxlan.vni_res;
225 /* Processing for key6_0 */
226 if (PREDICT_FALSE(memcmp (&key6_0, &last_key6, sizeof(last_key6)) != 0))
228 p0 = hash_get_mem(ngm->vxlan6_gpe_tunnel_by_key, &key6_0);
232 error0 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
236 memcpy (&last_key6, &key6_0, sizeof(key6_0));
237 tunnel_index0 = last_tunnel_index = p0[0];
240 tunnel_index0 = last_tunnel_index;
243 t0 = pool_elt_at_index(ngm->tunnels, tunnel_index0);
245 next0 = t0->protocol;
247 sw_if_index0 = t0->sw_if_index;
248 len0 = vlib_buffer_length_in_chain (vm, b0);
250 /* Required to make the l2 tag push / pop code work on l2 subifs */
251 vnet_update_l2_len (b0);
254 * ip[46] lookup in the configured FIB
256 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
259 stats_n_packets += 1;
260 stats_n_bytes += len0;
262 if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
264 stats_n_packets -= 1;
265 stats_n_bytes -= len0;
267 vlib_increment_combined_counter (
268 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
269 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
271 stats_n_bytes = len0;
272 stats_sw_if_index = sw_if_index0;
275 trace0: b0->error = error0 ? node->errors[error0] : 0;
277 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
279 vxlan_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof(*tr));
280 tr->next_index = next0;
282 tr->tunnel_index = tunnel_index0;
285 /* Process packet 1 */
288 /* Processing for key4_1 */
290 (key4_1.as_u64[0] != last_key4.as_u64[0])
291 || (key4_1.as_u64[1] != last_key4.as_u64[1])))
293 p1 = hash_get_mem(ngm->vxlan4_gpe_tunnel_by_key, &key4_1);
297 error1 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
301 last_key4.as_u64[0] = key4_1.as_u64[0];
302 last_key4.as_u64[1] = key4_1.as_u64[1];
303 tunnel_index1 = last_tunnel_index = p1[0];
306 tunnel_index1 = last_tunnel_index;
310 /* Processing for key6_1 */
311 if (PREDICT_FALSE(memcmp (&key6_1, &last_key6, sizeof(last_key6)) != 0))
313 p1 = hash_get_mem(ngm->vxlan6_gpe_tunnel_by_key, &key6_1);
317 error1 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
321 memcpy (&last_key6, &key6_1, sizeof(key6_1));
322 tunnel_index1 = last_tunnel_index = p1[0];
325 tunnel_index1 = last_tunnel_index;
328 t1 = pool_elt_at_index(ngm->tunnels, tunnel_index1);
330 next1 = t1->protocol;
331 sw_if_index1 = t1->sw_if_index;
332 len1 = vlib_buffer_length_in_chain (vm, b1);
334 /* Required to make the l2 tag push / pop code work on l2 subifs */
335 vnet_update_l2_len (b1);
338 * ip[46] lookup in the configured FIB
340 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
343 stats_n_packets += 1;
344 stats_n_bytes += len1;
346 /* Batch stats increment on the same vxlan tunnel so counter
347 is not incremented per packet */
348 if (PREDICT_FALSE(sw_if_index1 != stats_sw_if_index))
350 stats_n_packets -= 1;
351 stats_n_bytes -= len1;
353 vlib_increment_combined_counter (
354 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
355 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
357 stats_n_bytes = len1;
358 stats_sw_if_index = sw_if_index1;
360 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
362 trace1: b1->error = error1 ? node->errors[error1] : 0;
364 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
366 vxlan_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b1, sizeof(*tr));
367 tr->next_index = next1;
369 tr->tunnel_index = tunnel_index1;
372 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
373 n_left_to_next, bi0, bi1, next0, next1);
376 while (n_left_from > 0 && n_left_to_next > 0)
381 ip4_vxlan_gpe_header_t * iuvn4_0;
382 ip6_vxlan_gpe_header_t * iuvn6_0;
385 vxlan_gpe_tunnel_t * t0;
386 vxlan4_gpe_tunnel_key_t key4_0;
387 vxlan6_gpe_tunnel_key_t key6_0;
389 u32 sw_if_index0, len0;
398 b0 = vlib_get_buffer (vm, bi0);
402 /* udp leaves current_data pointing at the vxlan-gpe header */
403 vlib_buffer_advance (
404 b0, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
406 iuvn4_0 = vlib_buffer_get_current (b0);
408 /* pop (ip, udp, vxlan) */
409 vlib_buffer_advance (b0, sizeof(*iuvn4_0));
413 /* udp leaves current_data pointing at the vxlan-gpe header */
414 vlib_buffer_advance (
415 b0, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
417 iuvn6_0 = vlib_buffer_get_current (b0);
419 /* pop (ip, udp, vxlan) */
420 vlib_buffer_advance (b0, sizeof(*iuvn6_0));
429 (iuvn4_0->vxlan.protocol < node->n_next_nodes) ?
430 iuvn4_0->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
432 key4_0.local = iuvn4_0->ip4.dst_address.as_u32;
433 key4_0.remote = iuvn4_0->ip4.src_address.as_u32;
434 key4_0.vni = iuvn4_0->vxlan.vni_res;
437 /* Processing for key4_0 */
439 (key4_0.as_u64[0] != last_key4.as_u64[0])
440 || (key4_0.as_u64[1] != last_key4.as_u64[1])))
442 p0 = hash_get_mem(ngm->vxlan4_gpe_tunnel_by_key, &key4_0);
446 error0 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
450 last_key4.as_u64[0] = key4_0.as_u64[0];
451 last_key4.as_u64[1] = key4_0.as_u64[1];
452 tunnel_index0 = last_tunnel_index = p0[0];
455 tunnel_index0 = last_tunnel_index;
459 next0 = (iuvn6_0->vxlan.protocol < node->n_next_nodes) ?
460 iuvn6_0->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
462 key6_0.local.as_u64[0] = iuvn6_0->ip6.dst_address.as_u64[0];
463 key6_0.local.as_u64[1] = iuvn6_0->ip6.dst_address.as_u64[1];
464 key6_0.remote.as_u64[0] = iuvn6_0->ip6.src_address.as_u64[0];
465 key6_0.remote.as_u64[1] = iuvn6_0->ip6.src_address.as_u64[1];
466 key6_0.vni = iuvn6_0->vxlan.vni_res;
468 /* Processing for key6_0 */
469 if (PREDICT_FALSE(memcmp (&key6_0, &last_key6, sizeof(last_key6)) != 0))
471 p0 = hash_get_mem(ngm->vxlan6_gpe_tunnel_by_key, &key6_0);
475 error0 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
479 memcpy (&last_key6, &key6_0, sizeof(key6_0));
480 tunnel_index0 = last_tunnel_index = p0[0];
483 tunnel_index0 = last_tunnel_index;
486 t0 = pool_elt_at_index(ngm->tunnels, tunnel_index0);
488 next0 = t0->protocol;
490 sw_if_index0 = t0->sw_if_index;
491 len0 = vlib_buffer_length_in_chain (vm, b0);
493 /* Required to make the l2 tag push / pop code work on l2 subifs */
494 vnet_update_l2_len (b0);
497 * ip[46] lookup in the configured FIB
499 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
502 stats_n_packets += 1;
503 stats_n_bytes += len0;
505 /* Batch stats increment on the same vxlan-gpe tunnel so counter
506 is not incremented per packet */
507 if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
509 stats_n_packets -= 1;
510 stats_n_bytes -= len0;
512 vlib_increment_combined_counter (
513 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
514 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
516 stats_n_bytes = len0;
517 stats_sw_if_index = sw_if_index0;
520 trace00: b0->error = error0 ? node->errors[error0] : 0;
522 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
524 vxlan_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof(*tr));
525 tr->next_index = next0;
527 tr->tunnel_index = tunnel_index0;
529 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
530 n_left_to_next, bi0, next0);
533 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
535 vlib_node_increment_counter (vm, vxlan_gpe_input_node.index,
536 VXLAN_GPE_ERROR_DECAPSULATED, pkts_decapsulated);
537 /* Increment any remaining batch stats */
540 vlib_increment_combined_counter (
541 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, cpu_index,
542 stats_sw_if_index, stats_n_packets, stats_n_bytes);
543 node->runtime_data[0] = stats_sw_if_index;
545 return from_frame->n_vectors;
549 vxlan4_gpe_input (vlib_main_t * vm, vlib_node_runtime_t * node,
550 vlib_frame_t * from_frame)
552 return vxlan_gpe_input (vm, node, from_frame, /* is_ip4 */1);
556 vxlan6_gpe_input (vlib_main_t * vm, vlib_node_runtime_t * node,
557 vlib_frame_t * from_frame)
559 return vxlan_gpe_input (vm, node, from_frame, /* is_ip4 */0);
562 static char * vxlan_gpe_error_strings[] = {
563 #define vxlan_gpe_error(n,s) s,
564 #include <vnet/vxlan-gpe/vxlan_gpe_error.def>
565 #undef vxlan_gpe_error
569 VLIB_REGISTER_NODE (vxlan4_gpe_input_node) = {
570 .function = vxlan4_gpe_input,
571 .name = "vxlan4-gpe-input",
572 /* Takes a vector of packets. */
573 .vector_size = sizeof (u32),
574 .type = VLIB_NODE_TYPE_INTERNAL,
575 .n_errors = ARRAY_LEN(vxlan_gpe_error_strings),
576 .error_strings = vxlan_gpe_error_strings,
578 .n_next_nodes = VXLAN_GPE_INPUT_N_NEXT,
580 #define _(s,n) [VXLAN_GPE_INPUT_NEXT_##s] = n,
581 foreach_vxlan_gpe_input_next
585 .format_buffer = format_vxlan_gpe_with_length,
586 .format_trace = format_vxlan_gpe_rx_trace,
587 // $$$$ .unformat_buffer = unformat_vxlan_gpe_header,
590 VLIB_NODE_FUNCTION_MULTIARCH (vxlan4_gpe_input_node, vxlan4_gpe_input)
592 VLIB_REGISTER_NODE (vxlan6_gpe_input_node) = {
593 .function = vxlan6_gpe_input,
594 .name = "vxlan6-gpe-input",
595 /* Takes a vector of packets. */
596 .vector_size = sizeof (u32),
597 .type = VLIB_NODE_TYPE_INTERNAL,
598 .n_errors = ARRAY_LEN(vxlan_gpe_error_strings),
599 .error_strings = vxlan_gpe_error_strings,
601 .n_next_nodes = VXLAN_GPE_INPUT_N_NEXT,
603 #define _(s,n) [VXLAN_GPE_INPUT_NEXT_##s] = n,
604 foreach_vxlan_gpe_input_next
608 .format_buffer = format_vxlan_gpe_with_length,
609 .format_trace = format_vxlan_gpe_rx_trace,
610 // $$$$ .unformat_buffer = unformat_vxlan_gpe_header,
613 VLIB_NODE_FUNCTION_MULTIARCH (vxlan6_gpe_input_node, vxlan6_gpe_input)