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 vlib_node_registration_t gtpu4_input_node;
23 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;
788 gtpu4_input (vlib_main_t * vm,
789 vlib_node_runtime_t * node,
790 vlib_frame_t * from_frame)
792 return gtpu_input(vm, node, from_frame, /* is_ip4 */ 1);
796 gtpu6_input (vlib_main_t * vm,
797 vlib_node_runtime_t * node,
798 vlib_frame_t * from_frame)
800 return gtpu_input(vm, node, from_frame, /* is_ip4 */ 0);
803 static char * gtpu_error_strings[] = {
804 #define gtpu_error(n,s) s,
805 #include <gtpu/gtpu_error.def>
810 VLIB_REGISTER_NODE (gtpu4_input_node) = {
811 .function = gtpu4_input,
812 .name = "gtpu4-input",
813 /* Takes a vector of packets. */
814 .vector_size = sizeof (u32),
816 .n_errors = GTPU_N_ERROR,
817 .error_strings = gtpu_error_strings,
819 .n_next_nodes = GTPU_INPUT_N_NEXT,
821 #define _(s,n) [GTPU_INPUT_NEXT_##s] = n,
822 foreach_gtpu_input_next
826 //temp .format_buffer = format_gtpu_header,
827 .format_trace = format_gtpu_rx_trace,
828 // $$$$ .unformat_buffer = unformat_gtpu_header,
831 VLIB_NODE_FUNCTION_MULTIARCH (gtpu4_input_node, gtpu4_input)
833 VLIB_REGISTER_NODE (gtpu6_input_node) = {
834 .function = gtpu6_input,
835 .name = "gtpu6-input",
836 /* Takes a vector of packets. */
837 .vector_size = sizeof (u32),
839 .n_errors = GTPU_N_ERROR,
840 .error_strings = gtpu_error_strings,
842 .n_next_nodes = GTPU_INPUT_N_NEXT,
844 #define _(s,n) [GTPU_INPUT_NEXT_##s] = n,
845 foreach_gtpu_input_next
849 //temp .format_buffer = format_gtpu_header,
850 .format_trace = format_gtpu_rx_trace,
851 // $$$$ .unformat_buffer = unformat_gtpu_header,
854 VLIB_NODE_FUNCTION_MULTIARCH (gtpu6_input_node, gtpu6_input)
858 IP_GTPU_BYPASS_NEXT_DROP,
859 IP_GTPU_BYPASS_NEXT_GTPU,
860 IP_GTPU_BYPASS_N_NEXT,
861 } ip_vxan_bypass_next_t;
864 ip_gtpu_bypass_inline (vlib_main_t * vm,
865 vlib_node_runtime_t * node,
866 vlib_frame_t * frame,
869 gtpu_main_t * gtm = >pu_main;
870 u32 * from, * to_next, n_left_from, n_left_to_next, next_index;
871 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
872 ip4_address_t addr4; /* last IPv4 address matching a local VTEP address */
873 ip6_address_t addr6; /* last IPv6 address matching a local VTEP address */
875 from = vlib_frame_vector_args (frame);
876 n_left_from = frame->n_vectors;
877 next_index = node->cached_next_index;
879 if (node->flags & VLIB_NODE_FLAG_TRACE)
880 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
882 if (is_ip4) addr4.data_u32 = ~0;
883 else ip6_address_set_zero (&addr6);
885 while (n_left_from > 0)
887 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
889 while (n_left_from >= 4 && n_left_to_next >= 2)
891 vlib_buffer_t * b0, * b1;
892 ip4_header_t * ip40, * ip41;
893 ip6_header_t * ip60, * ip61;
894 udp_header_t * udp0, * udp1;
895 u32 bi0, ip_len0, udp_len0, flags0, next0;
896 u32 bi1, ip_len1, udp_len1, flags1, next1;
897 i32 len_diff0, len_diff1;
898 u8 error0, good_udp0, proto0;
899 u8 error1, good_udp1, proto1;
901 /* Prefetch next iteration. */
903 vlib_buffer_t * p2, * p3;
905 p2 = vlib_get_buffer (vm, from[2]);
906 p3 = vlib_get_buffer (vm, from[3]);
908 vlib_prefetch_buffer_header (p2, LOAD);
909 vlib_prefetch_buffer_header (p3, LOAD);
911 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
912 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
915 bi0 = to_next[0] = from[0];
916 bi1 = to_next[1] = from[1];
922 b0 = vlib_get_buffer (vm, bi0);
923 b1 = vlib_get_buffer (vm, bi1);
926 ip40 = vlib_buffer_get_current (b0);
927 ip41 = vlib_buffer_get_current (b1);
931 ip60 = vlib_buffer_get_current (b0);
932 ip61 = vlib_buffer_get_current (b1);
935 /* Setup packet for next IP feature */
936 vnet_feature_next(&next0, b0);
937 vnet_feature_next(&next1, b1);
941 /* Treat IP frag packets as "experimental" protocol for now
942 until support of IP frag reassembly is implemented */
943 proto0 = ip4_is_fragment(ip40) ? 0xfe : ip40->protocol;
944 proto1 = ip4_is_fragment(ip41) ? 0xfe : ip41->protocol;
948 proto0 = ip60->protocol;
949 proto1 = ip61->protocol;
952 /* Process packet 0 */
953 if (proto0 != IP_PROTOCOL_UDP)
954 goto exit0; /* not UDP packet */
957 udp0 = ip4_next_header (ip40);
959 udp0 = ip6_next_header (ip60);
961 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_GTPU))
962 goto exit0; /* not GTPU packet */
964 /* Validate DIP against VTEPs*/
967 if (addr4.as_u32 != ip40->dst_address.as_u32)
969 if (!hash_get (gtm->vtep4, ip40->dst_address.as_u32))
970 goto exit0; /* no local VTEP for GTPU packet */
971 addr4 = ip40->dst_address;
976 if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
978 if (!hash_get_mem (gtm->vtep6, &ip60->dst_address))
979 goto exit0; /* no local VTEP for GTPU packet */
980 addr6 = ip60->dst_address;
985 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
987 /* Don't verify UDP checksum for packets with explicit zero checksum. */
988 good_udp0 |= udp0->checksum == 0;
990 /* Verify UDP length */
992 ip_len0 = clib_net_to_host_u16 (ip40->length);
994 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
995 udp_len0 = clib_net_to_host_u16 (udp0->length);
996 len_diff0 = ip_len0 - udp_len0;
998 /* Verify UDP checksum */
999 if (PREDICT_FALSE (!good_udp0))
1001 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
1004 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
1006 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
1008 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1014 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
1015 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
1019 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
1020 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
1024 IP_GTPU_BYPASS_NEXT_DROP : IP_GTPU_BYPASS_NEXT_GTPU;
1025 b0->error = error0 ? error_node->errors[error0] : 0;
1027 /* gtpu-input node expect current at GTPU header */
1029 vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t));
1031 vlib_buffer_advance (b0, sizeof(ip6_header_t)+sizeof(udp_header_t));
1034 /* Process packet 1 */
1035 if (proto1 != IP_PROTOCOL_UDP)
1036 goto exit1; /* not UDP packet */
1039 udp1 = ip4_next_header (ip41);
1041 udp1 = ip6_next_header (ip61);
1043 if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_GTPU))
1044 goto exit1; /* not GTPU packet */
1046 /* Validate DIP against VTEPs*/
1049 if (addr4.as_u32 != ip41->dst_address.as_u32)
1051 if (!hash_get (gtm->vtep4, ip41->dst_address.as_u32))
1052 goto exit1; /* no local VTEP for GTPU packet */
1053 addr4 = ip41->dst_address;
1058 if (!ip6_address_is_equal (&addr6, &ip61->dst_address))
1060 if (!hash_get_mem (gtm->vtep6, &ip61->dst_address))
1061 goto exit1; /* no local VTEP for GTPU packet */
1062 addr6 = ip61->dst_address;
1067 good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1069 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1070 good_udp1 |= udp1->checksum == 0;
1072 /* Verify UDP length */
1074 ip_len1 = clib_net_to_host_u16 (ip41->length);
1076 ip_len1 = clib_net_to_host_u16 (ip61->payload_length);
1077 udp_len1 = clib_net_to_host_u16 (udp1->length);
1078 len_diff1 = ip_len1 - udp_len1;
1080 /* Verify UDP checksum */
1081 if (PREDICT_FALSE (!good_udp1))
1083 if ((flags1 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
1086 flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
1088 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
1090 (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1096 error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM;
1097 error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH;
1101 error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM;
1102 error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH;
1106 IP_GTPU_BYPASS_NEXT_DROP : IP_GTPU_BYPASS_NEXT_GTPU;
1107 b1->error = error1 ? error_node->errors[error1] : 0;
1109 /* gtpu-input node expect current at GTPU header */
1111 vlib_buffer_advance (b1, sizeof(ip4_header_t)+sizeof(udp_header_t));
1113 vlib_buffer_advance (b1, sizeof(ip6_header_t)+sizeof(udp_header_t));
1116 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1117 to_next, n_left_to_next,
1118 bi0, bi1, next0, next1);
1121 while (n_left_from > 0 && n_left_to_next > 0)
1124 ip4_header_t * ip40;
1125 ip6_header_t * ip60;
1126 udp_header_t * udp0;
1127 u32 bi0, ip_len0, udp_len0, flags0, next0;
1129 u8 error0, good_udp0, proto0;
1131 bi0 = to_next[0] = from[0];
1135 n_left_to_next -= 1;
1137 b0 = vlib_get_buffer (vm, bi0);
1139 ip40 = vlib_buffer_get_current (b0);
1141 ip60 = vlib_buffer_get_current (b0);
1143 /* Setup packet for next IP feature */
1144 vnet_feature_next(&next0, b0);
1147 /* Treat IP4 frag packets as "experimental" protocol for now
1148 until support of IP frag reassembly is implemented */
1149 proto0 = ip4_is_fragment(ip40) ? 0xfe : ip40->protocol;
1151 proto0 = ip60->protocol;
1153 if (proto0 != IP_PROTOCOL_UDP)
1154 goto exit; /* not UDP packet */
1157 udp0 = ip4_next_header (ip40);
1159 udp0 = ip6_next_header (ip60);
1161 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_GTPU))
1162 goto exit; /* not GTPU packet */
1164 /* Validate DIP against VTEPs*/
1167 if (addr4.as_u32 != ip40->dst_address.as_u32)
1169 if (!hash_get (gtm->vtep4, ip40->dst_address.as_u32))
1170 goto exit; /* no local VTEP for GTPU packet */
1171 addr4 = ip40->dst_address;
1176 if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
1178 if (!hash_get_mem (gtm->vtep6, &ip60->dst_address))
1179 goto exit; /* no local VTEP for GTPU packet */
1180 addr6 = ip60->dst_address;
1185 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1187 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1188 good_udp0 |= udp0->checksum == 0;
1190 /* Verify UDP length */
1192 ip_len0 = clib_net_to_host_u16 (ip40->length);
1194 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
1195 udp_len0 = clib_net_to_host_u16 (udp0->length);
1196 len_diff0 = ip_len0 - udp_len0;
1198 /* Verify UDP checksum */
1199 if (PREDICT_FALSE (!good_udp0))
1201 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
1204 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
1206 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
1208 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1214 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
1215 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
1219 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
1220 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
1224 IP_GTPU_BYPASS_NEXT_DROP : IP_GTPU_BYPASS_NEXT_GTPU;
1225 b0->error = error0 ? error_node->errors[error0] : 0;
1227 /* gtpu-input node expect current at GTPU header */
1229 vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t));
1231 vlib_buffer_advance (b0, sizeof(ip6_header_t)+sizeof(udp_header_t));
1234 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1235 to_next, n_left_to_next,
1239 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1242 return frame->n_vectors;
1246 ip4_gtpu_bypass (vlib_main_t * vm,
1247 vlib_node_runtime_t * node,
1248 vlib_frame_t * frame)
1250 return ip_gtpu_bypass_inline (vm, node, frame, /* is_ip4 */ 1);
1253 VLIB_REGISTER_NODE (ip4_gtpu_bypass_node) = {
1254 .function = ip4_gtpu_bypass,
1255 .name = "ip4-gtpu-bypass",
1256 .vector_size = sizeof (u32),
1258 .n_next_nodes = IP_GTPU_BYPASS_N_NEXT,
1260 [IP_GTPU_BYPASS_NEXT_DROP] = "error-drop",
1261 [IP_GTPU_BYPASS_NEXT_GTPU] = "gtpu4-input",
1264 .format_buffer = format_ip4_header,
1265 .format_trace = format_ip4_forward_next_trace,
1268 VLIB_NODE_FUNCTION_MULTIARCH (ip4_gtpu_bypass_node,ip4_gtpu_bypass)
1270 /* Dummy init function to get us linked in. */
1271 clib_error_t * ip4_gtpu_bypass_init (vlib_main_t * vm)
1274 VLIB_INIT_FUNCTION (ip4_gtpu_bypass_init);
1277 ip6_gtpu_bypass (vlib_main_t * vm,
1278 vlib_node_runtime_t * node,
1279 vlib_frame_t * frame)
1281 return ip_gtpu_bypass_inline (vm, node, frame, /* is_ip4 */ 0);
1284 VLIB_REGISTER_NODE (ip6_gtpu_bypass_node) = {
1285 .function = ip6_gtpu_bypass,
1286 .name = "ip6-gtpu-bypass",
1287 .vector_size = sizeof (u32),
1289 .n_next_nodes = IP_GTPU_BYPASS_N_NEXT,
1291 [IP_GTPU_BYPASS_NEXT_DROP] = "error-drop",
1292 [IP_GTPU_BYPASS_NEXT_GTPU] = "gtpu6-input",
1295 .format_buffer = format_ip6_header,
1296 .format_trace = format_ip6_forward_next_trace,
1299 VLIB_NODE_FUNCTION_MULTIARCH (ip6_gtpu_bypass_node,ip6_gtpu_bypass)
1301 /* Dummy init function to get us linked in. */
1302 clib_error_t * ip6_gtpu_bypass_init (vlib_main_t * vm)
1305 VLIB_INIT_FUNCTION (ip6_gtpu_bypass_init);