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 vxlan_input_node;
31 static u8 * format_vxlan_rx_trace (u8 * s, va_list * args)
33 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
34 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
35 vxlan_rx_trace_t * t = va_arg (*args, vxlan_rx_trace_t *);
37 if (t->tunnel_index != ~0)
39 s = format (s, "VXLAN: tunnel %d vni %d next %d error %d",
40 t->tunnel_index, t->vni, t->next_index, t->error);
44 s = format (s, "VXLAN: no tunnel for vni %d next %d error %d",
45 t->vni, t->next_index, t->error);
51 vxlan_input (vlib_main_t * vm,
52 vlib_node_runtime_t * node,
53 vlib_frame_t * from_frame,
56 u32 n_left_from, next_index, * from, * to_next;
57 vxlan_main_t * vxm = &vxlan_main;
58 vnet_main_t * vnm = vxm->vnet_main;
59 vnet_interface_main_t * im = &vnm->interface_main;
60 u32 last_tunnel_index = ~0;
61 vxlan4_tunnel_key_t last_key4;
62 vxlan6_tunnel_key_t last_key6;
63 u32 pkts_decapsulated = 0;
64 u32 cpu_index = os_get_cpu_number();
65 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
68 last_key4.as_u64 = ~0;
70 memset (&last_key6, 0xff, sizeof (last_key6));
72 from = vlib_frame_vector_args (from_frame);
73 n_left_from = from_frame->n_vectors;
75 next_index = node->cached_next_index;
76 stats_sw_if_index = node->runtime_data[0];
77 stats_n_packets = stats_n_bytes = 0;
79 while (n_left_from > 0)
83 vlib_get_next_frame (vm, node, next_index,
84 to_next, n_left_to_next);
85 while (n_left_from >= 4 && n_left_to_next >= 2)
88 vlib_buffer_t * b0, * b1;
90 ip4_header_t * ip4_0, * ip4_1;
91 ip6_header_t * ip6_0, * ip6_1;
92 vxlan_header_t * vxlan0, * vxlan1;
94 u32 tunnel_index0, tunnel_index1;
95 vxlan_tunnel_t * t0, * t1;
96 vxlan4_tunnel_key_t key4_0, key4_1;
97 vxlan6_tunnel_key_t key6_0, key6_1;
99 u32 sw_if_index0, sw_if_index1, len0, len1;
101 /* Prefetch next iteration. */
103 vlib_buffer_t * p2, * p3;
105 p2 = vlib_get_buffer (vm, from[2]);
106 p3 = vlib_get_buffer (vm, from[3]);
108 vlib_prefetch_buffer_header (p2, LOAD);
109 vlib_prefetch_buffer_header (p3, LOAD);
111 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
112 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
124 b0 = vlib_get_buffer (vm, bi0);
125 b1 = vlib_get_buffer (vm, bi1);
127 /* udp leaves current_data pointing at the vxlan header */
128 vxlan0 = vlib_buffer_get_current (b0);
129 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));
167 key4_0.src = ip4_0->src_address.as_u32;
168 key4_0.vni = vxlan0->vni_reserved;
170 if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64))
172 p0 = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64);
176 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
177 next0 = VXLAN_INPUT_NEXT_DROP;
181 last_key4.as_u64 = key4_0.as_u64;
182 tunnel_index0 = last_tunnel_index = p0[0];
185 tunnel_index0 = last_tunnel_index;
186 } else /* !is_ip4 */ {
187 key6_0.src.as_u64[0] = ip6_0->src_address.as_u64[0];
188 key6_0.src.as_u64[1] = ip6_0->src_address.as_u64[1];
189 key6_0.vni = vxlan0->vni_reserved;
191 if (PREDICT_FALSE (memcmp(&key6_0, &last_key6, sizeof(last_key6)) != 0))
193 p0 = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_0);
197 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
198 next0 = VXLAN_INPUT_NEXT_DROP;
202 clib_memcpy (&last_key6, &key6_0, sizeof(key6_0));
203 tunnel_index0 = last_tunnel_index = p0[0];
206 tunnel_index0 = last_tunnel_index;
209 t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
211 next0 = t0->decap_next_index;
212 sw_if_index0 = t0->sw_if_index;
213 len0 = vlib_buffer_length_in_chain (vm, b0);
215 /* Required to make the l2 tag push / pop code work on l2 subifs */
216 if (PREDICT_TRUE(next0 == VXLAN_INPUT_NEXT_L2_INPUT))
217 vnet_update_l2_len (b0);
219 /* Set input sw_if_index to VXLAN tunnel for learning */
220 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
222 pkts_decapsulated ++;
223 stats_n_packets += 1;
224 stats_n_bytes += len0;
226 /* Batch stats increment on the same vxlan tunnel so counter
227 is not incremented per packet */
228 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
230 stats_n_packets -= 1;
231 stats_n_bytes -= len0;
233 vlib_increment_combined_counter
234 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
235 cpu_index, stats_sw_if_index,
236 stats_n_packets, stats_n_bytes);
238 stats_n_bytes = len0;
239 stats_sw_if_index = sw_if_index0;
243 b0->error = error0 ? node->errors[error0] : 0;
245 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
248 = vlib_add_trace (vm, node, b0, sizeof (*tr));
249 tr->next_index = next0;
251 tr->tunnel_index = tunnel_index0;
252 tr->vni = vnet_get_vni (vxlan0);
257 key4_1.src = ip4_1->src_address.as_u32;
258 key4_1.vni = vxlan1->vni_reserved;
260 if (PREDICT_FALSE (key4_1.as_u64 != last_key4.as_u64))
262 p1 = hash_get (vxm->vxlan4_tunnel_by_key, key4_1.as_u64);
266 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
267 next1 = VXLAN_INPUT_NEXT_DROP;
271 last_key4.as_u64 = key4_1.as_u64;
272 tunnel_index1 = last_tunnel_index = p1[0];
275 tunnel_index1 = last_tunnel_index;
276 } else /* !is_ip4 */ {
277 key6_1.src.as_u64[0] = ip6_1->src_address.as_u64[0];
278 key6_1.src.as_u64[1] = ip6_1->src_address.as_u64[1];
279 key6_1.vni = vxlan1->vni_reserved;
281 if (PREDICT_FALSE (memcmp(&key6_1, &last_key6, sizeof(last_key6)) != 0))
283 p1 = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_1);
287 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
288 next1 = VXLAN_INPUT_NEXT_DROP;
292 clib_memcpy (&last_key6, &key6_1, sizeof(key6_1));
293 tunnel_index1 = last_tunnel_index = p1[0];
296 tunnel_index1 = last_tunnel_index;
299 t1 = pool_elt_at_index (vxm->tunnels, tunnel_index1);
301 next1 = t1->decap_next_index;
302 sw_if_index1 = t1->sw_if_index;
303 len1 = vlib_buffer_length_in_chain (vm, b1);
305 /* Required to make the l2 tag push / pop code work on l2 subifs */
306 if (PREDICT_TRUE(next1 == VXLAN_INPUT_NEXT_L2_INPUT))
307 vnet_update_l2_len (b1);
309 /* Set input sw_if_index to VXLAN tunnel for learning */
310 vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
312 pkts_decapsulated ++;
313 stats_n_packets += 1;
314 stats_n_bytes += len1;
316 /* Batch stats increment on the same vxlan tunnel so counter
317 is not incremented per packet */
318 if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
320 stats_n_packets -= 1;
321 stats_n_bytes -= len1;
323 vlib_increment_combined_counter
324 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
325 cpu_index, stats_sw_if_index,
326 stats_n_packets, stats_n_bytes);
328 stats_n_bytes = len1;
329 stats_sw_if_index = sw_if_index1;
333 b1->error = error1 ? node->errors[error1] : 0;
335 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
338 = vlib_add_trace (vm, node, b1, sizeof (*tr));
339 tr->next_index = next1;
341 tr->tunnel_index = tunnel_index1;
342 tr->vni = vnet_get_vni (vxlan1);
345 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
346 to_next, n_left_to_next,
347 bi0, bi1, next0, next1);
350 while (n_left_from > 0 && n_left_to_next > 0)
355 ip4_header_t * ip4_0;
356 ip6_header_t * ip6_0;
357 vxlan_header_t * vxlan0;
361 vxlan4_tunnel_key_t key4_0;
362 vxlan6_tunnel_key_t key6_0;
364 u32 sw_if_index0, len0;
373 b0 = vlib_get_buffer (vm, bi0);
375 /* udp leaves current_data pointing at the vxlan header */
376 vxlan0 = vlib_buffer_get_current (b0);
380 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
381 ip4_0 = vlib_buffer_get_current (b0);
384 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip6_header_t)));
385 ip6_0 = vlib_buffer_get_current (b0);
388 /* pop (ip, udp, vxlan) */
391 (b0, sizeof(*ip4_0)+sizeof(udp_header_t)+sizeof(*vxlan0));
394 (b0, sizeof(*ip6_0)+sizeof(udp_header_t)+sizeof(*vxlan0));
401 key4_0.src = ip4_0->src_address.as_u32;
402 key4_0.vni = vxlan0->vni_reserved;
404 if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64))
406 p0 = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64);
410 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
411 next0 = VXLAN_INPUT_NEXT_DROP;
415 last_key4.as_u64 = key4_0.as_u64;
416 tunnel_index0 = last_tunnel_index = p0[0];
419 tunnel_index0 = last_tunnel_index;
420 } else /* !is_ip4 */ {
421 key6_0.src.as_u64[0] = ip6_0->src_address.as_u64[0];
422 key6_0.src.as_u64[1] = ip6_0->src_address.as_u64[1];
423 key6_0.vni = vxlan0->vni_reserved;
425 if (PREDICT_FALSE (memcmp(&key6_0, &last_key6, sizeof(last_key6)) != 0))
427 p0 = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_0);
431 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
432 next0 = VXLAN_INPUT_NEXT_DROP;
436 clib_memcpy (&last_key6, &key6_0, sizeof(key6_0));
437 tunnel_index0 = last_tunnel_index = p0[0];
440 tunnel_index0 = last_tunnel_index;
443 t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
445 next0 = t0->decap_next_index;
446 sw_if_index0 = t0->sw_if_index;
447 len0 = vlib_buffer_length_in_chain (vm, b0);
449 /* Required to make the l2 tag push / pop code work on l2 subifs */
450 if (PREDICT_TRUE(next0 == VXLAN_INPUT_NEXT_L2_INPUT))
451 vnet_update_l2_len (b0);
453 /* Set input sw_if_index to VXLAN tunnel for learning */
454 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
456 pkts_decapsulated ++;
457 stats_n_packets += 1;
458 stats_n_bytes += len0;
460 /* Batch stats increment on the same vxlan tunnel so counter
461 is not incremented per packet */
462 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
464 stats_n_packets -= 1;
465 stats_n_bytes -= len0;
467 vlib_increment_combined_counter
468 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
469 cpu_index, stats_sw_if_index,
470 stats_n_packets, stats_n_bytes);
472 stats_n_bytes = len0;
473 stats_sw_if_index = sw_if_index0;
477 b0->error = error0 ? node->errors[error0] : 0;
479 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
482 = vlib_add_trace (vm, node, b0, sizeof (*tr));
483 tr->next_index = next0;
485 tr->tunnel_index = tunnel_index0;
486 tr->vni = vnet_get_vni (vxlan0);
488 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
489 to_next, n_left_to_next,
493 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
495 /* Do we still need this now that tunnel tx stats is kept? */
496 vlib_node_increment_counter (vm, vxlan_input_node.index,
497 VXLAN_ERROR_DECAPSULATED,
500 /* Increment any remaining batch stats */
503 vlib_increment_combined_counter
504 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
505 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
506 node->runtime_data[0] = stats_sw_if_index;
509 return from_frame->n_vectors;
513 vxlan4_input (vlib_main_t * vm,
514 vlib_node_runtime_t * node,
515 vlib_frame_t * from_frame)
517 return vxlan_input(vm, node, from_frame, /* is_ip4 */ 1);
521 vxlan6_input (vlib_main_t * vm,
522 vlib_node_runtime_t * node,
523 vlib_frame_t * from_frame)
525 return vxlan_input(vm, node, from_frame, /* is_ip4 */ 0);
528 static char * vxlan_error_strings[] = {
529 #define vxlan_error(n,s) s,
530 #include <vnet/vxlan/vxlan_error.def>
535 VLIB_REGISTER_NODE (vxlan4_input_node) = {
536 .function = vxlan4_input,
537 .name = "vxlan4-input",
538 /* Takes a vector of packets. */
539 .vector_size = sizeof (u32),
541 .n_errors = VXLAN_N_ERROR,
542 .error_strings = vxlan_error_strings,
544 .n_next_nodes = VXLAN_INPUT_N_NEXT,
546 #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
547 foreach_vxlan_input_next
551 //temp .format_buffer = format_vxlan_header,
552 .format_trace = format_vxlan_rx_trace,
553 // $$$$ .unformat_buffer = unformat_vxlan_header,
556 VLIB_NODE_FUNCTION_MULTIARCH (vxlan4_input_node, vxlan4_input)
558 VLIB_REGISTER_NODE (vxlan6_input_node) = {
559 .function = vxlan6_input,
560 .name = "vxlan6-input",
561 /* Takes a vector of packets. */
562 .vector_size = sizeof (u32),
564 .n_errors = VXLAN_N_ERROR,
565 .error_strings = vxlan_error_strings,
567 .n_next_nodes = VXLAN_INPUT_N_NEXT,
569 #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
570 foreach_vxlan_input_next
574 //temp .format_buffer = format_vxlan_header,
575 .format_trace = format_vxlan_rx_trace,
576 // $$$$ .unformat_buffer = unformat_vxlan_header,
579 VLIB_NODE_FUNCTION_MULTIARCH (vxlan6_input_node, vxlan6_input)