2 * decap.c: gtpu tunnel decap packet processing
4 * Copyright (c) 2017 Intel 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 <gtpu/gtpu.h>
22 extern vlib_node_registration_t gtpu4_input_node;
23 extern vlib_node_registration_t gtpu6_input_node;
32 static u8 * format_gtpu_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 gtpu_rx_trace_t * t = va_arg (*args, gtpu_rx_trace_t *);
38 if (t->tunnel_index != ~0)
40 s = format (s, "GTPU decap from gtpu_tunnel%d teid %d next %d error %d",
41 t->tunnel_index, t->teid, t->next_index, t->error);
45 s = format (s, "GTPU decap error - tunnel for teid %d does not exist",
52 validate_gtpu_fib (vlib_buffer_t *b, gtpu_tunnel_t *t, u32 is_ip4)
54 u32 fib_index, sw_if_index;
56 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
59 fib_index = (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
60 vec_elt (ip4_main.fib_index_by_sw_if_index, sw_if_index) :
61 vnet_buffer (b)->sw_if_index[VLIB_TX];
63 fib_index = (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
64 vec_elt (ip6_main.fib_index_by_sw_if_index, sw_if_index) :
65 vnet_buffer (b)->sw_if_index[VLIB_TX];
67 return (fib_index == t->encap_fib_index);
71 gtpu_input (vlib_main_t * vm,
72 vlib_node_runtime_t * node,
73 vlib_frame_t * from_frame,
76 u32 n_left_from, next_index, * from, * to_next;
77 gtpu_main_t * gtm = >pu_main;
78 vnet_main_t * vnm = gtm->vnet_main;
79 vnet_interface_main_t * im = &vnm->interface_main;
80 u32 last_tunnel_index = ~0;
81 gtpu4_tunnel_key_t last_key4;
82 gtpu6_tunnel_key_t last_key6;
83 u32 pkts_decapsulated = 0;
84 u32 thread_index = vlib_get_thread_index();
85 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
88 last_key4.as_u64 = ~0;
90 clib_memset (&last_key6, 0xff, sizeof (last_key6));
92 from = vlib_frame_vector_args (from_frame);
93 n_left_from = from_frame->n_vectors;
95 next_index = node->cached_next_index;
96 stats_sw_if_index = node->runtime_data[0];
97 stats_n_packets = stats_n_bytes = 0;
99 while (n_left_from > 0)
103 vlib_get_next_frame (vm, node, next_index,
104 to_next, n_left_to_next);
105 while (n_left_from >= 4 && n_left_to_next >= 2)
108 vlib_buffer_t * b0, * b1;
110 ip4_header_t * ip4_0, * ip4_1;
111 ip6_header_t * ip6_0, * ip6_1;
112 gtpu_header_t * gtpu0, * gtpu1;
113 u32 gtpu_hdr_len0 = 0, gtpu_hdr_len1 =0 ;
115 u32 tunnel_index0, tunnel_index1;
116 gtpu_tunnel_t * t0, * t1, * mt0 = NULL, * mt1 = NULL;
117 gtpu4_tunnel_key_t key4_0, key4_1;
118 gtpu6_tunnel_key_t key6_0, key6_1;
120 u32 sw_if_index0, sw_if_index1, len0, len1;
122 /* Prefetch next iteration. */
124 vlib_buffer_t * p2, * p3;
126 p2 = vlib_get_buffer (vm, from[2]);
127 p3 = vlib_get_buffer (vm, from[3]);
129 vlib_prefetch_buffer_header (p2, LOAD);
130 vlib_prefetch_buffer_header (p3, LOAD);
132 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
133 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
145 b0 = vlib_get_buffer (vm, bi0);
146 b1 = vlib_get_buffer (vm, bi1);
148 /* udp leaves current_data pointing at the gtpu header */
149 gtpu0 = vlib_buffer_get_current (b0);
150 gtpu1 = vlib_buffer_get_current (b1);
153 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
155 (b1, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
156 ip4_0 = vlib_buffer_get_current (b0);
157 ip4_1 = vlib_buffer_get_current (b1);
160 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip6_header_t)));
162 (b1, -(word)(sizeof(udp_header_t)+sizeof(ip6_header_t)));
163 ip6_0 = vlib_buffer_get_current (b0);
164 ip6_1 = vlib_buffer_get_current (b1);
167 /* pop (ip, udp, gtpu) */
170 (b0, sizeof(*ip4_0)+sizeof(udp_header_t));
172 (b1, sizeof(*ip4_1)+sizeof(udp_header_t));
175 (b0, sizeof(*ip6_0)+sizeof(udp_header_t));
177 (b1, sizeof(*ip6_1)+sizeof(udp_header_t));
186 if (PREDICT_FALSE ((gtpu0->ver_flags & GTPU_VER_MASK) != GTPU_V1_VER))
188 error0 = GTPU_ERROR_BAD_VER;
189 next0 = GTPU_INPUT_NEXT_DROP;
193 /* Manipulate packet 0 */
195 key4_0.src = ip4_0->src_address.as_u32;
196 key4_0.teid = gtpu0->teid;
198 /* Make sure GTPU tunnel exist according to packet SIP and teid
199 * SIP identify a GTPU path, and teid identify a tunnel in a given GTPU path */
200 if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64))
202 p0 = hash_get (gtm->gtpu4_tunnel_by_key, key4_0.as_u64);
203 if (PREDICT_FALSE (p0 == NULL))
205 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
206 next0 = GTPU_INPUT_NEXT_DROP;
209 last_key4.as_u64 = key4_0.as_u64;
210 tunnel_index0 = last_tunnel_index = p0[0];
213 tunnel_index0 = last_tunnel_index;
214 t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
216 /* Validate GTPU tunnel encap-fib index against packet */
217 if (PREDICT_FALSE (validate_gtpu_fib (b0, t0, is_ip4) == 0))
219 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
220 next0 = GTPU_INPUT_NEXT_DROP;
224 /* Validate GTPU tunnel SIP against packet DIP */
225 if (PREDICT_TRUE (ip4_0->dst_address.as_u32 == t0->src.ip4.as_u32))
226 goto next0; /* valid packet */
227 if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_0->dst_address)))
229 key4_0.src = ip4_0->dst_address.as_u32;
230 key4_0.teid = gtpu0->teid;
231 /* Make sure mcast GTPU tunnel exist by packet DIP and teid */
232 p0 = hash_get (gtm->gtpu4_tunnel_by_key, key4_0.as_u64);
233 if (PREDICT_TRUE (p0 != NULL))
235 mt0 = pool_elt_at_index (gtm->tunnels, p0[0]);
236 goto next0; /* valid packet */
239 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
240 next0 = GTPU_INPUT_NEXT_DROP;
243 } else /* !is_ip4 */ {
244 key6_0.src.as_u64[0] = ip6_0->src_address.as_u64[0];
245 key6_0.src.as_u64[1] = ip6_0->src_address.as_u64[1];
246 key6_0.teid = gtpu0->teid;
248 /* Make sure GTPU tunnel exist according to packet SIP and teid
249 * SIP identify a GTPU path, and teid identify a tunnel in a given GTPU path */
250 if (PREDICT_FALSE (memcmp(&key6_0, &last_key6, sizeof(last_key6)) != 0))
252 p0 = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6_0);
253 if (PREDICT_FALSE (p0 == NULL))
255 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
256 next0 = GTPU_INPUT_NEXT_DROP;
259 clib_memcpy_fast (&last_key6, &key6_0, sizeof(key6_0));
260 tunnel_index0 = last_tunnel_index = p0[0];
263 tunnel_index0 = last_tunnel_index;
264 t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
266 /* Validate GTPU tunnel encap-fib index against packet */
267 if (PREDICT_FALSE (validate_gtpu_fib (b0, t0, is_ip4) == 0))
269 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
270 next0 = GTPU_INPUT_NEXT_DROP;
274 /* Validate GTPU tunnel SIP against packet DIP */
275 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address,
277 goto next0; /* valid packet */
278 if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_0->dst_address)))
280 key6_0.src.as_u64[0] = ip6_0->dst_address.as_u64[0];
281 key6_0.src.as_u64[1] = ip6_0->dst_address.as_u64[1];
282 key6_0.teid = gtpu0->teid;
283 p0 = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6_0);
284 if (PREDICT_TRUE (p0 != NULL))
286 mt0 = pool_elt_at_index (gtm->tunnels, p0[0]);
287 goto next0; /* valid packet */
290 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
291 next0 = GTPU_INPUT_NEXT_DROP;
296 /* Manipulate gtpu header */
297 if (PREDICT_FALSE((gtpu0->ver_flags & GTPU_E_S_PN_BIT) != 0))
299 gtpu_hdr_len0 = sizeof(gtpu_header_t);
301 /* Manipulate Sequence Number and N-PDU Number */
304 /* Manipulate Next Extension Header */
309 gtpu_hdr_len0 = sizeof(gtpu_header_t) - 4;
312 /* Pop gtpu header */
313 vlib_buffer_advance (b0, gtpu_hdr_len0);
315 next0 = t0->decap_next_index;
316 sw_if_index0 = t0->sw_if_index;
317 len0 = vlib_buffer_length_in_chain (vm, b0);
319 /* Required to make the l2 tag push / pop code work on l2 subifs */
320 if (PREDICT_TRUE(next0 == GTPU_INPUT_NEXT_L2_INPUT))
321 vnet_update_l2_len (b0);
323 /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
324 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
325 sw_if_index0 = (mt0) ? mt0->sw_if_index : sw_if_index0;
327 pkts_decapsulated ++;
328 stats_n_packets += 1;
329 stats_n_bytes += len0;
331 /* Batch stats increment on the same gtpu tunnel so counter
332 is not incremented per packet */
333 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
335 stats_n_packets -= 1;
336 stats_n_bytes -= len0;
338 vlib_increment_combined_counter
339 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
340 thread_index, stats_sw_if_index,
341 stats_n_packets, stats_n_bytes);
343 stats_n_bytes = len0;
344 stats_sw_if_index = sw_if_index0;
348 b0->error = error0 ? node->errors[error0] : 0;
350 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
353 = vlib_add_trace (vm, node, b0, sizeof (*tr));
354 tr->next_index = next0;
356 tr->tunnel_index = tunnel_index0;
357 tr->teid = clib_net_to_host_u32(gtpu0->teid);
360 if (PREDICT_FALSE ((gtpu1->ver_flags & GTPU_VER_MASK) != GTPU_V1_VER))
362 error1 = GTPU_ERROR_BAD_VER;
363 next1 = GTPU_INPUT_NEXT_DROP;
367 /* Manipulate packet 1 */
369 key4_1.src = ip4_1->src_address.as_u32;
370 key4_1.teid = gtpu1->teid;
372 /* Make sure GTPU tunnel exist according to packet SIP and teid
373 * SIP identify a GTPU path, and teid identify a tunnel in a given GTPU path */
374 if (PREDICT_FALSE (key4_1.as_u64 != last_key4.as_u64))
376 p1 = hash_get (gtm->gtpu4_tunnel_by_key, key4_1.as_u64);
377 if (PREDICT_FALSE (p1 == NULL))
379 error1 = GTPU_ERROR_NO_SUCH_TUNNEL;
380 next1 = GTPU_INPUT_NEXT_DROP;
383 last_key4.as_u64 = key4_1.as_u64;
384 tunnel_index1 = last_tunnel_index = p1[0];
387 tunnel_index1 = last_tunnel_index;
388 t1 = pool_elt_at_index (gtm->tunnels, tunnel_index1);
390 /* Validate GTPU tunnel encap-fib index against packet */
391 if (PREDICT_FALSE (validate_gtpu_fib (b1, t1, is_ip4) == 0))
393 error1 = GTPU_ERROR_NO_SUCH_TUNNEL;
394 next1 = GTPU_INPUT_NEXT_DROP;
398 /* Validate GTPU tunnel SIP against packet DIP */
399 if (PREDICT_TRUE (ip4_1->dst_address.as_u32 == t1->src.ip4.as_u32))
400 goto next1; /* valid packet */
401 if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_1->dst_address)))
403 key4_1.src = ip4_1->dst_address.as_u32;
404 key4_1.teid = gtpu1->teid;
405 /* Make sure mcast GTPU tunnel exist by packet DIP and teid */
406 p1 = hash_get (gtm->gtpu4_tunnel_by_key, key4_1.as_u64);
407 if (PREDICT_TRUE (p1 != NULL))
409 mt1 = pool_elt_at_index (gtm->tunnels, p1[0]);
410 goto next1; /* valid packet */
413 error1 = GTPU_ERROR_NO_SUCH_TUNNEL;
414 next1 = GTPU_INPUT_NEXT_DROP;
417 } else /* !is_ip4 */ {
418 key6_1.src.as_u64[0] = ip6_1->src_address.as_u64[0];
419 key6_1.src.as_u64[1] = ip6_1->src_address.as_u64[1];
420 key6_1.teid = gtpu1->teid;
422 /* Make sure GTPU tunnel exist according to packet SIP and teid
423 * SIP identify a GTPU path, and teid identify a tunnel in a given GTPU path */
424 if (PREDICT_FALSE (memcmp(&key6_1, &last_key6, sizeof(last_key6)) != 0))
426 p1 = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6_1);
428 if (PREDICT_FALSE (p1 == NULL))
430 error1 = GTPU_ERROR_NO_SUCH_TUNNEL;
431 next1 = GTPU_INPUT_NEXT_DROP;
435 clib_memcpy_fast (&last_key6, &key6_1, sizeof(key6_1));
436 tunnel_index1 = last_tunnel_index = p1[0];
439 tunnel_index1 = last_tunnel_index;
440 t1 = pool_elt_at_index (gtm->tunnels, tunnel_index1);
442 /* Validate GTPU tunnel encap-fib index against packet */
443 if (PREDICT_FALSE (validate_gtpu_fib (b1, t1, is_ip4) == 0))
445 error1 = GTPU_ERROR_NO_SUCH_TUNNEL;
446 next1 = GTPU_INPUT_NEXT_DROP;
450 /* Validate GTPU tunnel SIP against packet DIP */
451 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_1->dst_address,
453 goto next1; /* valid packet */
454 if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_1->dst_address)))
456 key6_1.src.as_u64[0] = ip6_1->dst_address.as_u64[0];
457 key6_1.src.as_u64[1] = ip6_1->dst_address.as_u64[1];
458 key6_1.teid = gtpu1->teid;
459 p1 = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6_1);
460 if (PREDICT_TRUE (p1 != NULL))
462 mt1 = pool_elt_at_index (gtm->tunnels, p1[0]);
463 goto next1; /* valid packet */
466 error1 = GTPU_ERROR_NO_SUCH_TUNNEL;
467 next1 = GTPU_INPUT_NEXT_DROP;
472 /* Manipulate gtpu header */
473 if (PREDICT_FALSE((gtpu1->ver_flags & GTPU_E_S_PN_BIT) != 0))
475 gtpu_hdr_len1 = sizeof(gtpu_header_t);
477 /* Manipulate Sequence Number and N-PDU Number */
480 /* Manipulate Next Extension Header */
485 gtpu_hdr_len1 = sizeof(gtpu_header_t) - 4;
488 /* Pop gtpu header */
489 vlib_buffer_advance (b1, gtpu_hdr_len1);
491 next1 = t1->decap_next_index;
492 sw_if_index1 = t1->sw_if_index;
493 len1 = vlib_buffer_length_in_chain (vm, b1);
495 /* Required to make the l2 tag push / pop code work on l2 subifs */
496 if (PREDICT_TRUE(next1 == GTPU_INPUT_NEXT_L2_INPUT))
497 vnet_update_l2_len (b1);
499 /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
500 vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
501 sw_if_index1 = (mt1) ? mt1->sw_if_index : sw_if_index1;
503 pkts_decapsulated ++;
504 stats_n_packets += 1;
505 stats_n_bytes += len1;
507 /* Batch stats increment on the same gtpu tunnel so counter
508 is not incremented per packet */
509 if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
511 stats_n_packets -= 1;
512 stats_n_bytes -= len1;
514 vlib_increment_combined_counter
515 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
516 thread_index, stats_sw_if_index,
517 stats_n_packets, stats_n_bytes);
519 stats_n_bytes = len1;
520 stats_sw_if_index = sw_if_index1;
524 b1->error = error1 ? node->errors[error1] : 0;
526 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
529 = vlib_add_trace (vm, node, b1, sizeof (*tr));
530 tr->next_index = next1;
532 tr->tunnel_index = tunnel_index1;
533 tr->teid = clib_net_to_host_u32(gtpu1->teid);
536 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
537 to_next, n_left_to_next,
538 bi0, bi1, next0, next1);
541 while (n_left_from > 0 && n_left_to_next > 0)
546 ip4_header_t * ip4_0;
547 ip6_header_t * ip6_0;
548 gtpu_header_t * gtpu0;
549 u32 gtpu_hdr_len0 = 0;
552 gtpu_tunnel_t * t0, * mt0 = NULL;
553 gtpu4_tunnel_key_t key4_0;
554 gtpu6_tunnel_key_t key6_0;
556 u32 sw_if_index0, len0;
565 b0 = vlib_get_buffer (vm, bi0);
567 /* udp leaves current_data pointing at the gtpu header */
568 gtpu0 = vlib_buffer_get_current (b0);
571 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
572 ip4_0 = vlib_buffer_get_current (b0);
575 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip6_header_t)));
576 ip6_0 = vlib_buffer_get_current (b0);
582 (b0, sizeof(*ip4_0)+sizeof(udp_header_t));
585 (b0, sizeof(*ip6_0)+sizeof(udp_header_t));
590 if (PREDICT_FALSE ((gtpu0->ver_flags & GTPU_VER_MASK) != GTPU_V1_VER))
592 error0 = GTPU_ERROR_BAD_VER;
593 next0 = GTPU_INPUT_NEXT_DROP;
598 key4_0.src = ip4_0->src_address.as_u32;
599 key4_0.teid = gtpu0->teid;
601 /* Make sure GTPU tunnel exist according to packet SIP and teid
602 * SIP identify a GTPU path, and teid identify a tunnel in a given GTPU path */
603 if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64))
605 p0 = hash_get (gtm->gtpu4_tunnel_by_key, key4_0.as_u64);
606 if (PREDICT_FALSE (p0 == NULL))
608 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
609 next0 = GTPU_INPUT_NEXT_DROP;
612 last_key4.as_u64 = key4_0.as_u64;
613 tunnel_index0 = last_tunnel_index = p0[0];
616 tunnel_index0 = last_tunnel_index;
617 t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
619 /* Validate GTPU tunnel encap-fib index against packet */
620 if (PREDICT_FALSE (validate_gtpu_fib (b0, t0, is_ip4) == 0))
622 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
623 next0 = GTPU_INPUT_NEXT_DROP;
627 /* Validate GTPU tunnel SIP against packet DIP */
628 if (PREDICT_TRUE (ip4_0->dst_address.as_u32 == t0->src.ip4.as_u32))
629 goto next00; /* valid packet */
630 if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_0->dst_address)))
632 key4_0.src = ip4_0->dst_address.as_u32;
633 key4_0.teid = gtpu0->teid;
634 /* Make sure mcast GTPU tunnel exist by packet DIP and teid */
635 p0 = hash_get (gtm->gtpu4_tunnel_by_key, key4_0.as_u64);
636 if (PREDICT_TRUE (p0 != NULL))
638 mt0 = pool_elt_at_index (gtm->tunnels, p0[0]);
639 goto next00; /* valid packet */
642 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
643 next0 = GTPU_INPUT_NEXT_DROP;
646 } else /* !is_ip4 */ {
647 key6_0.src.as_u64[0] = ip6_0->src_address.as_u64[0];
648 key6_0.src.as_u64[1] = ip6_0->src_address.as_u64[1];
649 key6_0.teid = gtpu0->teid;
651 /* Make sure GTPU tunnel exist according to packet SIP and teid
652 * SIP identify a GTPU path, and teid identify a tunnel in a given GTPU path */
653 if (PREDICT_FALSE (memcmp(&key6_0, &last_key6, sizeof(last_key6)) != 0))
655 p0 = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6_0);
656 if (PREDICT_FALSE (p0 == NULL))
658 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
659 next0 = GTPU_INPUT_NEXT_DROP;
662 clib_memcpy_fast (&last_key6, &key6_0, sizeof(key6_0));
663 tunnel_index0 = last_tunnel_index = p0[0];
666 tunnel_index0 = last_tunnel_index;
667 t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
669 /* Validate GTPU tunnel encap-fib index against packet */
670 if (PREDICT_FALSE (validate_gtpu_fib (b0, t0, is_ip4) == 0))
672 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
673 next0 = GTPU_INPUT_NEXT_DROP;
677 /* Validate GTPU tunnel SIP against packet DIP */
678 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address,
680 goto next00; /* valid packet */
681 if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_0->dst_address)))
683 key6_0.src.as_u64[0] = ip6_0->dst_address.as_u64[0];
684 key6_0.src.as_u64[1] = ip6_0->dst_address.as_u64[1];
685 key6_0.teid = gtpu0->teid;
686 p0 = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6_0);
687 if (PREDICT_TRUE (p0 != NULL))
689 mt0 = pool_elt_at_index (gtm->tunnels, p0[0]);
690 goto next00; /* valid packet */
693 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
694 next0 = GTPU_INPUT_NEXT_DROP;
699 /* Manipulate gtpu header */
700 if (PREDICT_FALSE((gtpu0->ver_flags & GTPU_E_S_PN_BIT) != 0))
702 gtpu_hdr_len0 = sizeof(gtpu_header_t);
704 /* Manipulate Sequence Number and N-PDU Number */
707 /* Manipulate Next Extension Header */
712 gtpu_hdr_len0 = sizeof(gtpu_header_t) - 4;
715 /* Pop gtpu header */
716 vlib_buffer_advance (b0, gtpu_hdr_len0);
718 next0 = t0->decap_next_index;
719 sw_if_index0 = t0->sw_if_index;
720 len0 = vlib_buffer_length_in_chain (vm, b0);
722 /* Required to make the l2 tag push / pop code work on l2 subifs */
723 if (PREDICT_TRUE(next0 == GTPU_INPUT_NEXT_L2_INPUT))
724 vnet_update_l2_len (b0);
726 /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
727 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
728 sw_if_index0 = (mt0) ? mt0->sw_if_index : sw_if_index0;
730 pkts_decapsulated ++;
731 stats_n_packets += 1;
732 stats_n_bytes += len0;
734 /* Batch stats increment on the same gtpu tunnel so counter
735 is not incremented per packet */
736 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
738 stats_n_packets -= 1;
739 stats_n_bytes -= len0;
741 vlib_increment_combined_counter
742 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
743 thread_index, stats_sw_if_index,
744 stats_n_packets, stats_n_bytes);
746 stats_n_bytes = len0;
747 stats_sw_if_index = sw_if_index0;
751 b0->error = error0 ? node->errors[error0] : 0;
753 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
756 = vlib_add_trace (vm, node, b0, sizeof (*tr));
757 tr->next_index = next0;
759 tr->tunnel_index = tunnel_index0;
760 tr->teid = clib_net_to_host_u32(gtpu0->teid);
762 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
763 to_next, n_left_to_next,
767 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
769 /* Do we still need this now that tunnel tx stats is kept? */
770 vlib_node_increment_counter (vm, is_ip4?
771 gtpu4_input_node.index:gtpu6_input_node.index,
772 GTPU_ERROR_DECAPSULATED,
775 /* Increment any remaining batch stats */
778 vlib_increment_combined_counter
779 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
780 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
781 node->runtime_data[0] = stats_sw_if_index;
784 return from_frame->n_vectors;
787 VLIB_NODE_FN (gtpu4_input_node) (vlib_main_t * vm,
788 vlib_node_runtime_t * node,
789 vlib_frame_t * from_frame)
791 return gtpu_input(vm, node, from_frame, /* is_ip4 */ 1);
794 VLIB_NODE_FN (gtpu6_input_node) (vlib_main_t * vm,
795 vlib_node_runtime_t * node,
796 vlib_frame_t * from_frame)
798 return gtpu_input(vm, node, from_frame, /* is_ip4 */ 0);
801 static char * gtpu_error_strings[] = {
802 #define gtpu_error(n,s) s,
803 #include <gtpu/gtpu_error.def>
808 VLIB_REGISTER_NODE (gtpu4_input_node) = {
809 .name = "gtpu4-input",
810 /* Takes a vector of packets. */
811 .vector_size = sizeof (u32),
813 .n_errors = GTPU_N_ERROR,
814 .error_strings = gtpu_error_strings,
816 .n_next_nodes = GTPU_INPUT_N_NEXT,
818 #define _(s,n) [GTPU_INPUT_NEXT_##s] = n,
819 foreach_gtpu_input_next
823 //temp .format_buffer = format_gtpu_header,
824 .format_trace = format_gtpu_rx_trace,
825 // $$$$ .unformat_buffer = unformat_gtpu_header,
828 VLIB_REGISTER_NODE (gtpu6_input_node) = {
829 .name = "gtpu6-input",
830 /* Takes a vector of packets. */
831 .vector_size = sizeof (u32),
833 .n_errors = GTPU_N_ERROR,
834 .error_strings = gtpu_error_strings,
836 .n_next_nodes = GTPU_INPUT_N_NEXT,
838 #define _(s,n) [GTPU_INPUT_NEXT_##s] = n,
839 foreach_gtpu_input_next
843 //temp .format_buffer = format_gtpu_header,
844 .format_trace = format_gtpu_rx_trace,
845 // $$$$ .unformat_buffer = unformat_gtpu_header,
849 IP_GTPU_BYPASS_NEXT_DROP,
850 IP_GTPU_BYPASS_NEXT_GTPU,
851 IP_GTPU_BYPASS_N_NEXT,
852 } ip_vxan_bypass_next_t;
855 ip_gtpu_bypass_inline (vlib_main_t * vm,
856 vlib_node_runtime_t * node,
857 vlib_frame_t * frame,
860 gtpu_main_t * gtm = >pu_main;
861 u32 * from, * to_next, n_left_from, n_left_to_next, next_index;
862 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
863 ip4_address_t addr4; /* last IPv4 address matching a local VTEP address */
864 ip6_address_t addr6; /* last IPv6 address matching a local VTEP address */
866 from = vlib_frame_vector_args (frame);
867 n_left_from = frame->n_vectors;
868 next_index = node->cached_next_index;
870 if (node->flags & VLIB_NODE_FLAG_TRACE)
871 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
873 if (is_ip4) addr4.data_u32 = ~0;
874 else ip6_address_set_zero (&addr6);
876 while (n_left_from > 0)
878 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
880 while (n_left_from >= 4 && n_left_to_next >= 2)
882 vlib_buffer_t * b0, * b1;
883 ip4_header_t * ip40, * ip41;
884 ip6_header_t * ip60, * ip61;
885 udp_header_t * udp0, * udp1;
886 u32 bi0, ip_len0, udp_len0, flags0, next0;
887 u32 bi1, ip_len1, udp_len1, flags1, next1;
888 i32 len_diff0, len_diff1;
889 u8 error0, good_udp0, proto0;
890 u8 error1, good_udp1, proto1;
892 /* Prefetch next iteration. */
894 vlib_buffer_t * p2, * p3;
896 p2 = vlib_get_buffer (vm, from[2]);
897 p3 = vlib_get_buffer (vm, from[3]);
899 vlib_prefetch_buffer_header (p2, LOAD);
900 vlib_prefetch_buffer_header (p3, LOAD);
902 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
903 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
906 bi0 = to_next[0] = from[0];
907 bi1 = to_next[1] = from[1];
913 b0 = vlib_get_buffer (vm, bi0);
914 b1 = vlib_get_buffer (vm, bi1);
917 ip40 = vlib_buffer_get_current (b0);
918 ip41 = vlib_buffer_get_current (b1);
922 ip60 = vlib_buffer_get_current (b0);
923 ip61 = vlib_buffer_get_current (b1);
926 /* Setup packet for next IP feature */
927 vnet_feature_next(&next0, b0);
928 vnet_feature_next(&next1, b1);
932 /* Treat IP frag packets as "experimental" protocol for now
933 until support of IP frag reassembly is implemented */
934 proto0 = ip4_is_fragment(ip40) ? 0xfe : ip40->protocol;
935 proto1 = ip4_is_fragment(ip41) ? 0xfe : ip41->protocol;
939 proto0 = ip60->protocol;
940 proto1 = ip61->protocol;
943 /* Process packet 0 */
944 if (proto0 != IP_PROTOCOL_UDP)
945 goto exit0; /* not UDP packet */
948 udp0 = ip4_next_header (ip40);
950 udp0 = ip6_next_header (ip60);
952 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_GTPU))
953 goto exit0; /* not GTPU packet */
955 /* Validate DIP against VTEPs*/
958 if (addr4.as_u32 != ip40->dst_address.as_u32)
960 if (!hash_get (gtm->vtep4, ip40->dst_address.as_u32))
961 goto exit0; /* no local VTEP for GTPU packet */
962 addr4 = ip40->dst_address;
967 if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
969 if (!hash_get_mem (gtm->vtep6, &ip60->dst_address))
970 goto exit0; /* no local VTEP for GTPU packet */
971 addr6 = ip60->dst_address;
976 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
978 /* Don't verify UDP checksum for packets with explicit zero checksum. */
979 good_udp0 |= udp0->checksum == 0;
981 /* Verify UDP length */
983 ip_len0 = clib_net_to_host_u16 (ip40->length);
985 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
986 udp_len0 = clib_net_to_host_u16 (udp0->length);
987 len_diff0 = ip_len0 - udp_len0;
989 /* Verify UDP checksum */
990 if (PREDICT_FALSE (!good_udp0))
992 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
995 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
997 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
999 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1005 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
1006 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
1010 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
1011 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
1015 IP_GTPU_BYPASS_NEXT_DROP : IP_GTPU_BYPASS_NEXT_GTPU;
1016 b0->error = error0 ? error_node->errors[error0] : 0;
1018 /* gtpu-input node expect current at GTPU header */
1020 vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t));
1022 vlib_buffer_advance (b0, sizeof(ip6_header_t)+sizeof(udp_header_t));
1025 /* Process packet 1 */
1026 if (proto1 != IP_PROTOCOL_UDP)
1027 goto exit1; /* not UDP packet */
1030 udp1 = ip4_next_header (ip41);
1032 udp1 = ip6_next_header (ip61);
1034 if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_GTPU))
1035 goto exit1; /* not GTPU packet */
1037 /* Validate DIP against VTEPs*/
1040 if (addr4.as_u32 != ip41->dst_address.as_u32)
1042 if (!hash_get (gtm->vtep4, ip41->dst_address.as_u32))
1043 goto exit1; /* no local VTEP for GTPU packet */
1044 addr4 = ip41->dst_address;
1049 if (!ip6_address_is_equal (&addr6, &ip61->dst_address))
1051 if (!hash_get_mem (gtm->vtep6, &ip61->dst_address))
1052 goto exit1; /* no local VTEP for GTPU packet */
1053 addr6 = ip61->dst_address;
1058 good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1060 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1061 good_udp1 |= udp1->checksum == 0;
1063 /* Verify UDP length */
1065 ip_len1 = clib_net_to_host_u16 (ip41->length);
1067 ip_len1 = clib_net_to_host_u16 (ip61->payload_length);
1068 udp_len1 = clib_net_to_host_u16 (udp1->length);
1069 len_diff1 = ip_len1 - udp_len1;
1071 /* Verify UDP checksum */
1072 if (PREDICT_FALSE (!good_udp1))
1074 if ((flags1 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
1077 flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
1079 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
1081 (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1087 error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM;
1088 error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH;
1092 error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM;
1093 error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH;
1097 IP_GTPU_BYPASS_NEXT_DROP : IP_GTPU_BYPASS_NEXT_GTPU;
1098 b1->error = error1 ? error_node->errors[error1] : 0;
1100 /* gtpu-input node expect current at GTPU header */
1102 vlib_buffer_advance (b1, sizeof(ip4_header_t)+sizeof(udp_header_t));
1104 vlib_buffer_advance (b1, sizeof(ip6_header_t)+sizeof(udp_header_t));
1107 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1108 to_next, n_left_to_next,
1109 bi0, bi1, next0, next1);
1112 while (n_left_from > 0 && n_left_to_next > 0)
1115 ip4_header_t * ip40;
1116 ip6_header_t * ip60;
1117 udp_header_t * udp0;
1118 u32 bi0, ip_len0, udp_len0, flags0, next0;
1120 u8 error0, good_udp0, proto0;
1122 bi0 = to_next[0] = from[0];
1126 n_left_to_next -= 1;
1128 b0 = vlib_get_buffer (vm, bi0);
1130 ip40 = vlib_buffer_get_current (b0);
1132 ip60 = vlib_buffer_get_current (b0);
1134 /* Setup packet for next IP feature */
1135 vnet_feature_next(&next0, b0);
1138 /* Treat IP4 frag packets as "experimental" protocol for now
1139 until support of IP frag reassembly is implemented */
1140 proto0 = ip4_is_fragment(ip40) ? 0xfe : ip40->protocol;
1142 proto0 = ip60->protocol;
1144 if (proto0 != IP_PROTOCOL_UDP)
1145 goto exit; /* not UDP packet */
1148 udp0 = ip4_next_header (ip40);
1150 udp0 = ip6_next_header (ip60);
1152 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_GTPU))
1153 goto exit; /* not GTPU packet */
1155 /* Validate DIP against VTEPs*/
1158 if (addr4.as_u32 != ip40->dst_address.as_u32)
1160 if (!hash_get (gtm->vtep4, ip40->dst_address.as_u32))
1161 goto exit; /* no local VTEP for GTPU packet */
1162 addr4 = ip40->dst_address;
1167 if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
1169 if (!hash_get_mem (gtm->vtep6, &ip60->dst_address))
1170 goto exit; /* no local VTEP for GTPU packet */
1171 addr6 = ip60->dst_address;
1176 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1178 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1179 good_udp0 |= udp0->checksum == 0;
1181 /* Verify UDP length */
1183 ip_len0 = clib_net_to_host_u16 (ip40->length);
1185 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
1186 udp_len0 = clib_net_to_host_u16 (udp0->length);
1187 len_diff0 = ip_len0 - udp_len0;
1189 /* Verify UDP checksum */
1190 if (PREDICT_FALSE (!good_udp0))
1192 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
1195 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
1197 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
1199 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1205 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
1206 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
1210 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
1211 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
1215 IP_GTPU_BYPASS_NEXT_DROP : IP_GTPU_BYPASS_NEXT_GTPU;
1216 b0->error = error0 ? error_node->errors[error0] : 0;
1218 /* gtpu-input node expect current at GTPU header */
1220 vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t));
1222 vlib_buffer_advance (b0, sizeof(ip6_header_t)+sizeof(udp_header_t));
1225 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1226 to_next, n_left_to_next,
1230 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1233 return frame->n_vectors;
1236 VLIB_NODE_FN (ip4_gtpu_bypass_node) (vlib_main_t * vm,
1237 vlib_node_runtime_t * node,
1238 vlib_frame_t * frame)
1240 return ip_gtpu_bypass_inline (vm, node, frame, /* is_ip4 */ 1);
1243 VLIB_REGISTER_NODE (ip4_gtpu_bypass_node) = {
1244 .name = "ip4-gtpu-bypass",
1245 .vector_size = sizeof (u32),
1247 .n_next_nodes = IP_GTPU_BYPASS_N_NEXT,
1249 [IP_GTPU_BYPASS_NEXT_DROP] = "error-drop",
1250 [IP_GTPU_BYPASS_NEXT_GTPU] = "gtpu4-input",
1253 .format_buffer = format_ip4_header,
1254 .format_trace = format_ip4_forward_next_trace,
1257 #ifndef CLIB_MARCH_VARIANT
1258 /* Dummy init function to get us linked in. */
1259 clib_error_t * ip4_gtpu_bypass_init (vlib_main_t * vm)
1262 VLIB_INIT_FUNCTION (ip4_gtpu_bypass_init);
1263 #endif /* CLIB_MARCH_VARIANT */
1265 VLIB_NODE_FN (ip6_gtpu_bypass_node) (vlib_main_t * vm,
1266 vlib_node_runtime_t * node,
1267 vlib_frame_t * frame)
1269 return ip_gtpu_bypass_inline (vm, node, frame, /* is_ip4 */ 0);
1272 VLIB_REGISTER_NODE (ip6_gtpu_bypass_node) = {
1273 .name = "ip6-gtpu-bypass",
1274 .vector_size = sizeof (u32),
1276 .n_next_nodes = IP_GTPU_BYPASS_N_NEXT,
1278 [IP_GTPU_BYPASS_NEXT_DROP] = "error-drop",
1279 [IP_GTPU_BYPASS_NEXT_GTPU] = "gtpu6-input",
1282 .format_buffer = format_ip6_header,
1283 .format_trace = format_ip6_forward_next_trace,
1286 #ifndef CLIB_MARCH_VARIANT
1287 /* Dummy init function to get us linked in. */
1288 clib_error_t * ip6_gtpu_bypass_init (vlib_main_t * vm)
1291 VLIB_INIT_FUNCTION (ip6_gtpu_bypass_init);
1292 #endif /* CLIB_MARCH_VARIANT */