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 return t->encap_fib_index == vlib_buffer_get_ip_fib_index (b, is_ip4);
58 gtpu_input (vlib_main_t * vm,
59 vlib_node_runtime_t * node,
60 vlib_frame_t * from_frame,
63 u32 n_left_from, next_index, * from, * to_next;
64 gtpu_main_t * gtm = >pu_main;
65 vnet_main_t * vnm = gtm->vnet_main;
66 vnet_interface_main_t * im = &vnm->interface_main;
67 u32 last_tunnel_index = ~0;
68 gtpu4_tunnel_key_t last_key4;
69 gtpu6_tunnel_key_t last_key6;
70 u32 pkts_decapsulated = 0;
71 u32 thread_index = vlib_get_thread_index();
72 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
75 last_key4.as_u64 = ~0;
77 clib_memset (&last_key6, 0xff, sizeof (last_key6));
79 from = vlib_frame_vector_args (from_frame);
80 n_left_from = from_frame->n_vectors;
82 next_index = node->cached_next_index;
83 stats_sw_if_index = node->runtime_data[0];
84 stats_n_packets = stats_n_bytes = 0;
86 while (n_left_from > 0)
90 vlib_get_next_frame (vm, node, next_index,
91 to_next, n_left_to_next);
92 while (n_left_from >= 4 && n_left_to_next >= 2)
95 vlib_buffer_t * b0, * b1;
97 ip4_header_t * ip4_0, * ip4_1;
98 ip6_header_t * ip6_0, * ip6_1;
99 gtpu_header_t * gtpu0, * gtpu1;
100 u32 gtpu_hdr_len0, gtpu_hdr_len1;
102 u32 tunnel_index0, tunnel_index1;
103 gtpu_tunnel_t * t0, * t1, * mt0 = NULL, * mt1 = NULL;
104 gtpu4_tunnel_key_t key4_0, key4_1;
105 gtpu6_tunnel_key_t key6_0, key6_1;
107 u32 sw_if_index0, sw_if_index1, len0, len1;
108 u8 has_space0, has_space1;
111 /* Prefetch next iteration. */
113 vlib_buffer_t * p2, * p3;
115 p2 = vlib_get_buffer (vm, from[2]);
116 p3 = vlib_get_buffer (vm, from[3]);
118 vlib_prefetch_buffer_header (p2, LOAD);
119 vlib_prefetch_buffer_header (p3, LOAD);
121 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
122 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
134 b0 = vlib_get_buffer (vm, bi0);
135 b1 = vlib_get_buffer (vm, bi1);
137 /* udp leaves current_data pointing at the gtpu header */
138 gtpu0 = vlib_buffer_get_current (b0);
139 gtpu1 = vlib_buffer_get_current (b1);
142 ip4_0 = (void *)((u8*)gtpu0 - sizeof(udp_header_t) - sizeof(ip4_header_t));
143 ip4_1 = (void *)((u8*)gtpu1 - sizeof(udp_header_t) - sizeof(ip4_header_t));
147 ip6_0 = (void *)((u8*)gtpu0 - sizeof(udp_header_t) - sizeof(ip6_header_t));
148 ip6_1 = (void *)((u8*)gtpu1 - sizeof(udp_header_t) - sizeof(ip6_header_t));
157 /* speculatively load gtp header version field */
158 ver0 = gtpu0->ver_flags;
159 ver1 = gtpu1->ver_flags;
162 * Manipulate gtpu header
163 * TBD: Manipulate Sequence Number and N-PDU Number
164 * TBD: Manipulate Next Extension Header
166 gtpu_hdr_len0 = sizeof(gtpu_header_t) - (((ver0 & GTPU_E_S_PN_BIT) == 0) * 4);
167 gtpu_hdr_len1 = sizeof(gtpu_header_t) - (((ver1 & GTPU_E_S_PN_BIT) == 0) * 4);
169 has_space0 = vlib_buffer_has_space (b0, gtpu_hdr_len0);
170 has_space1 = vlib_buffer_has_space (b1, gtpu_hdr_len1);
172 if (PREDICT_FALSE (((ver0 & GTPU_VER_MASK) != GTPU_V1_VER) | (!has_space0)))
174 error0 = has_space0 ? GTPU_ERROR_BAD_VER : GTPU_ERROR_TOO_SMALL;
175 next0 = GTPU_INPUT_NEXT_DROP;
179 /* Manipulate packet 0 */
181 key4_0.src = ip4_0->src_address.as_u32;
182 key4_0.teid = gtpu0->teid;
184 /* Make sure GTPU tunnel exist according to packet SIP and teid
185 * SIP identify a GTPU path, and teid identify a tunnel in a given GTPU path */
186 if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64))
188 p0 = hash_get (gtm->gtpu4_tunnel_by_key, key4_0.as_u64);
189 if (PREDICT_FALSE (p0 == NULL))
191 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
192 next0 = GTPU_INPUT_NEXT_DROP;
195 last_key4.as_u64 = key4_0.as_u64;
196 tunnel_index0 = last_tunnel_index = p0[0];
199 tunnel_index0 = last_tunnel_index;
200 t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
202 /* Validate GTPU tunnel encap-fib index against packet */
203 if (PREDICT_FALSE (validate_gtpu_fib (b0, t0, is_ip4) == 0))
205 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
206 next0 = GTPU_INPUT_NEXT_DROP;
210 /* Validate GTPU tunnel SIP against packet DIP */
211 if (PREDICT_TRUE (ip4_0->dst_address.as_u32 == t0->src.ip4.as_u32))
212 goto next0; /* valid packet */
213 if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_0->dst_address)))
215 key4_0.src = ip4_0->dst_address.as_u32;
216 key4_0.teid = gtpu0->teid;
217 /* Make sure mcast GTPU tunnel exist by packet DIP and teid */
218 p0 = hash_get (gtm->gtpu4_tunnel_by_key, key4_0.as_u64);
219 if (PREDICT_TRUE (p0 != NULL))
221 mt0 = pool_elt_at_index (gtm->tunnels, p0[0]);
222 goto next0; /* valid packet */
225 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
226 next0 = GTPU_INPUT_NEXT_DROP;
229 } else /* !is_ip4 */ {
230 key6_0.src.as_u64[0] = ip6_0->src_address.as_u64[0];
231 key6_0.src.as_u64[1] = ip6_0->src_address.as_u64[1];
232 key6_0.teid = gtpu0->teid;
234 /* Make sure GTPU tunnel exist according to packet SIP and teid
235 * SIP identify a GTPU path, and teid identify a tunnel in a given GTPU path */
236 if (PREDICT_FALSE (memcmp(&key6_0, &last_key6, sizeof(last_key6)) != 0))
238 p0 = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6_0);
239 if (PREDICT_FALSE (p0 == NULL))
241 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
242 next0 = GTPU_INPUT_NEXT_DROP;
245 clib_memcpy_fast (&last_key6, &key6_0, sizeof(key6_0));
246 tunnel_index0 = last_tunnel_index = p0[0];
249 tunnel_index0 = last_tunnel_index;
250 t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
252 /* Validate GTPU tunnel encap-fib index against packet */
253 if (PREDICT_FALSE (validate_gtpu_fib (b0, t0, is_ip4) == 0))
255 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
256 next0 = GTPU_INPUT_NEXT_DROP;
260 /* Validate GTPU tunnel SIP against packet DIP */
261 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address,
263 goto next0; /* valid packet */
264 if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_0->dst_address)))
266 key6_0.src.as_u64[0] = ip6_0->dst_address.as_u64[0];
267 key6_0.src.as_u64[1] = ip6_0->dst_address.as_u64[1];
268 key6_0.teid = gtpu0->teid;
269 p0 = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6_0);
270 if (PREDICT_TRUE (p0 != NULL))
272 mt0 = pool_elt_at_index (gtm->tunnels, p0[0]);
273 goto next0; /* valid packet */
276 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
277 next0 = GTPU_INPUT_NEXT_DROP;
282 /* Pop gtpu header */
283 vlib_buffer_advance (b0, gtpu_hdr_len0);
285 next0 = t0->decap_next_index;
286 sw_if_index0 = t0->sw_if_index;
287 len0 = vlib_buffer_length_in_chain (vm, b0);
289 /* Required to make the l2 tag push / pop code work on l2 subifs */
290 if (PREDICT_TRUE(next0 == GTPU_INPUT_NEXT_L2_INPUT))
291 vnet_update_l2_len (b0);
293 /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
294 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
295 sw_if_index0 = (mt0) ? mt0->sw_if_index : sw_if_index0;
297 pkts_decapsulated ++;
298 stats_n_packets += 1;
299 stats_n_bytes += len0;
301 /* Batch stats increment on the same gtpu tunnel so counter
302 is not incremented per packet */
303 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
305 stats_n_packets -= 1;
306 stats_n_bytes -= len0;
308 vlib_increment_combined_counter
309 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
310 thread_index, stats_sw_if_index,
311 stats_n_packets, stats_n_bytes);
313 stats_n_bytes = len0;
314 stats_sw_if_index = sw_if_index0;
318 b0->error = error0 ? node->errors[error0] : 0;
320 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
323 = vlib_add_trace (vm, node, b0, sizeof (*tr));
324 tr->next_index = next0;
326 tr->tunnel_index = tunnel_index0;
327 tr->teid = has_space0 ? clib_net_to_host_u32(gtpu0->teid) : ~0;
330 if (PREDICT_FALSE (((ver1 & GTPU_VER_MASK) != GTPU_V1_VER) | (!has_space1)))
332 error1 = has_space1 ? GTPU_ERROR_BAD_VER : GTPU_ERROR_TOO_SMALL;
333 next1 = GTPU_INPUT_NEXT_DROP;
337 /* Manipulate packet 1 */
339 key4_1.src = ip4_1->src_address.as_u32;
340 key4_1.teid = gtpu1->teid;
342 /* Make sure GTPU tunnel exist according to packet SIP and teid
343 * SIP identify a GTPU path, and teid identify a tunnel in a given GTPU path */
344 if (PREDICT_FALSE (key4_1.as_u64 != last_key4.as_u64))
346 p1 = hash_get (gtm->gtpu4_tunnel_by_key, key4_1.as_u64);
347 if (PREDICT_FALSE (p1 == NULL))
349 error1 = GTPU_ERROR_NO_SUCH_TUNNEL;
350 next1 = GTPU_INPUT_NEXT_DROP;
353 last_key4.as_u64 = key4_1.as_u64;
354 tunnel_index1 = last_tunnel_index = p1[0];
357 tunnel_index1 = last_tunnel_index;
358 t1 = pool_elt_at_index (gtm->tunnels, tunnel_index1);
360 /* Validate GTPU tunnel encap-fib index against packet */
361 if (PREDICT_FALSE (validate_gtpu_fib (b1, t1, is_ip4) == 0))
363 error1 = GTPU_ERROR_NO_SUCH_TUNNEL;
364 next1 = GTPU_INPUT_NEXT_DROP;
368 /* Validate GTPU tunnel SIP against packet DIP */
369 if (PREDICT_TRUE (ip4_1->dst_address.as_u32 == t1->src.ip4.as_u32))
370 goto next1; /* valid packet */
371 if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_1->dst_address)))
373 key4_1.src = ip4_1->dst_address.as_u32;
374 key4_1.teid = gtpu1->teid;
375 /* Make sure mcast GTPU tunnel exist by packet DIP and teid */
376 p1 = hash_get (gtm->gtpu4_tunnel_by_key, key4_1.as_u64);
377 if (PREDICT_TRUE (p1 != NULL))
379 mt1 = pool_elt_at_index (gtm->tunnels, p1[0]);
380 goto next1; /* valid packet */
383 error1 = GTPU_ERROR_NO_SUCH_TUNNEL;
384 next1 = GTPU_INPUT_NEXT_DROP;
387 } else /* !is_ip4 */ {
388 key6_1.src.as_u64[0] = ip6_1->src_address.as_u64[0];
389 key6_1.src.as_u64[1] = ip6_1->src_address.as_u64[1];
390 key6_1.teid = gtpu1->teid;
392 /* Make sure GTPU tunnel exist according to packet SIP and teid
393 * SIP identify a GTPU path, and teid identify a tunnel in a given GTPU path */
394 if (PREDICT_FALSE (memcmp(&key6_1, &last_key6, sizeof(last_key6)) != 0))
396 p1 = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6_1);
398 if (PREDICT_FALSE (p1 == NULL))
400 error1 = GTPU_ERROR_NO_SUCH_TUNNEL;
401 next1 = GTPU_INPUT_NEXT_DROP;
405 clib_memcpy_fast (&last_key6, &key6_1, sizeof(key6_1));
406 tunnel_index1 = last_tunnel_index = p1[0];
409 tunnel_index1 = last_tunnel_index;
410 t1 = pool_elt_at_index (gtm->tunnels, tunnel_index1);
412 /* Validate GTPU tunnel encap-fib index against packet */
413 if (PREDICT_FALSE (validate_gtpu_fib (b1, t1, is_ip4) == 0))
415 error1 = GTPU_ERROR_NO_SUCH_TUNNEL;
416 next1 = GTPU_INPUT_NEXT_DROP;
420 /* Validate GTPU tunnel SIP against packet DIP */
421 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_1->dst_address,
423 goto next1; /* valid packet */
424 if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_1->dst_address)))
426 key6_1.src.as_u64[0] = ip6_1->dst_address.as_u64[0];
427 key6_1.src.as_u64[1] = ip6_1->dst_address.as_u64[1];
428 key6_1.teid = gtpu1->teid;
429 p1 = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6_1);
430 if (PREDICT_TRUE (p1 != NULL))
432 mt1 = pool_elt_at_index (gtm->tunnels, p1[0]);
433 goto next1; /* valid packet */
436 error1 = GTPU_ERROR_NO_SUCH_TUNNEL;
437 next1 = GTPU_INPUT_NEXT_DROP;
442 /* Pop gtpu header */
443 vlib_buffer_advance (b1, gtpu_hdr_len1);
445 next1 = t1->decap_next_index;
446 sw_if_index1 = t1->sw_if_index;
447 len1 = vlib_buffer_length_in_chain (vm, b1);
449 /* Required to make the l2 tag push / pop code work on l2 subifs */
450 if (PREDICT_TRUE(next1 == GTPU_INPUT_NEXT_L2_INPUT))
451 vnet_update_l2_len (b1);
453 /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
454 vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
455 sw_if_index1 = (mt1) ? mt1->sw_if_index : sw_if_index1;
457 pkts_decapsulated ++;
458 stats_n_packets += 1;
459 stats_n_bytes += len1;
461 /* Batch stats increment on the same gtpu tunnel so counter
462 is not incremented per packet */
463 if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
465 stats_n_packets -= 1;
466 stats_n_bytes -= len1;
468 vlib_increment_combined_counter
469 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
470 thread_index, stats_sw_if_index,
471 stats_n_packets, stats_n_bytes);
473 stats_n_bytes = len1;
474 stats_sw_if_index = sw_if_index1;
478 b1->error = error1 ? node->errors[error1] : 0;
480 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
483 = vlib_add_trace (vm, node, b1, sizeof (*tr));
484 tr->next_index = next1;
486 tr->tunnel_index = tunnel_index1;
487 tr->teid = has_space1 ? clib_net_to_host_u32(gtpu1->teid) : ~0;
490 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
491 to_next, n_left_to_next,
492 bi0, bi1, next0, next1);
495 while (n_left_from > 0 && n_left_to_next > 0)
500 ip4_header_t * ip4_0;
501 ip6_header_t * ip6_0;
502 gtpu_header_t * gtpu0;
506 gtpu_tunnel_t * t0, * mt0 = NULL;
507 gtpu4_tunnel_key_t key4_0;
508 gtpu6_tunnel_key_t key6_0;
510 u32 sw_if_index0, len0;
521 b0 = vlib_get_buffer (vm, bi0);
523 /* udp leaves current_data pointing at the gtpu header */
524 gtpu0 = vlib_buffer_get_current (b0);
526 ip4_0 = (void *)((u8*)gtpu0 - sizeof(udp_header_t) - sizeof(ip4_header_t));
528 ip6_0 = (void *)((u8*)gtpu0 - sizeof(udp_header_t) - sizeof(ip6_header_t));
534 /* speculatively load gtp header version field */
535 ver0 = gtpu0->ver_flags;
538 * Manipulate gtpu header
539 * TBD: Manipulate Sequence Number and N-PDU Number
540 * TBD: Manipulate Next Extension Header
542 gtpu_hdr_len0 = sizeof(gtpu_header_t) - (((ver0 & GTPU_E_S_PN_BIT) == 0) * 4);
544 has_space0 = vlib_buffer_has_space (b0, gtpu_hdr_len0);
546 if (PREDICT_FALSE (((ver0 & GTPU_VER_MASK) != GTPU_V1_VER) | (!has_space0)))
548 error0 = has_space0 ? GTPU_ERROR_BAD_VER : GTPU_ERROR_TOO_SMALL;
549 next0 = GTPU_INPUT_NEXT_DROP;
554 key4_0.src = ip4_0->src_address.as_u32;
555 key4_0.teid = gtpu0->teid;
557 /* Make sure GTPU tunnel exist according to packet SIP and teid
558 * SIP identify a GTPU path, and teid identify a tunnel in a given GTPU path */
559 if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64))
561 p0 = hash_get (gtm->gtpu4_tunnel_by_key, key4_0.as_u64);
562 if (PREDICT_FALSE (p0 == NULL))
564 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
565 next0 = GTPU_INPUT_NEXT_DROP;
568 last_key4.as_u64 = key4_0.as_u64;
569 tunnel_index0 = last_tunnel_index = p0[0];
572 tunnel_index0 = last_tunnel_index;
573 t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
575 /* Validate GTPU tunnel encap-fib index against packet */
576 if (PREDICT_FALSE (validate_gtpu_fib (b0, t0, is_ip4) == 0))
578 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
579 next0 = GTPU_INPUT_NEXT_DROP;
583 /* Validate GTPU tunnel SIP against packet DIP */
584 if (PREDICT_TRUE (ip4_0->dst_address.as_u32 == t0->src.ip4.as_u32))
585 goto next00; /* valid packet */
586 if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_0->dst_address)))
588 key4_0.src = ip4_0->dst_address.as_u32;
589 key4_0.teid = gtpu0->teid;
590 /* Make sure mcast GTPU tunnel exist by packet DIP and teid */
591 p0 = hash_get (gtm->gtpu4_tunnel_by_key, key4_0.as_u64);
592 if (PREDICT_TRUE (p0 != NULL))
594 mt0 = pool_elt_at_index (gtm->tunnels, p0[0]);
595 goto next00; /* valid packet */
598 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
599 next0 = GTPU_INPUT_NEXT_DROP;
602 } else /* !is_ip4 */ {
603 key6_0.src.as_u64[0] = ip6_0->src_address.as_u64[0];
604 key6_0.src.as_u64[1] = ip6_0->src_address.as_u64[1];
605 key6_0.teid = gtpu0->teid;
607 /* Make sure GTPU tunnel exist according to packet SIP and teid
608 * SIP identify a GTPU path, and teid identify a tunnel in a given GTPU path */
609 if (PREDICT_FALSE (memcmp(&key6_0, &last_key6, sizeof(last_key6)) != 0))
611 p0 = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6_0);
612 if (PREDICT_FALSE (p0 == NULL))
614 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
615 next0 = GTPU_INPUT_NEXT_DROP;
618 clib_memcpy_fast (&last_key6, &key6_0, sizeof(key6_0));
619 tunnel_index0 = last_tunnel_index = p0[0];
622 tunnel_index0 = last_tunnel_index;
623 t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
625 /* Validate GTPU tunnel encap-fib index against packet */
626 if (PREDICT_FALSE (validate_gtpu_fib (b0, t0, is_ip4) == 0))
628 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
629 next0 = GTPU_INPUT_NEXT_DROP;
633 /* Validate GTPU tunnel SIP against packet DIP */
634 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address,
636 goto next00; /* valid packet */
637 if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_0->dst_address)))
639 key6_0.src.as_u64[0] = ip6_0->dst_address.as_u64[0];
640 key6_0.src.as_u64[1] = ip6_0->dst_address.as_u64[1];
641 key6_0.teid = gtpu0->teid;
642 p0 = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6_0);
643 if (PREDICT_TRUE (p0 != NULL))
645 mt0 = pool_elt_at_index (gtm->tunnels, p0[0]);
646 goto next00; /* valid packet */
649 error0 = GTPU_ERROR_NO_SUCH_TUNNEL;
650 next0 = GTPU_INPUT_NEXT_DROP;
655 /* Pop gtpu header */
656 vlib_buffer_advance (b0, gtpu_hdr_len0);
658 next0 = t0->decap_next_index;
659 sw_if_index0 = t0->sw_if_index;
660 len0 = vlib_buffer_length_in_chain (vm, b0);
662 /* Required to make the l2 tag push / pop code work on l2 subifs */
663 if (PREDICT_TRUE(next0 == GTPU_INPUT_NEXT_L2_INPUT))
664 vnet_update_l2_len (b0);
666 /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
667 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
668 sw_if_index0 = (mt0) ? mt0->sw_if_index : sw_if_index0;
670 pkts_decapsulated ++;
671 stats_n_packets += 1;
672 stats_n_bytes += len0;
674 /* Batch stats increment on the same gtpu tunnel so counter
675 is not incremented per packet */
676 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
678 stats_n_packets -= 1;
679 stats_n_bytes -= len0;
681 vlib_increment_combined_counter
682 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
683 thread_index, stats_sw_if_index,
684 stats_n_packets, stats_n_bytes);
686 stats_n_bytes = len0;
687 stats_sw_if_index = sw_if_index0;
691 b0->error = error0 ? node->errors[error0] : 0;
693 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
696 = vlib_add_trace (vm, node, b0, sizeof (*tr));
697 tr->next_index = next0;
699 tr->tunnel_index = tunnel_index0;
700 tr->teid = has_space0 ? clib_net_to_host_u32(gtpu0->teid) : ~0;
702 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
703 to_next, n_left_to_next,
707 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
709 /* Do we still need this now that tunnel tx stats is kept? */
710 vlib_node_increment_counter (vm, is_ip4?
711 gtpu4_input_node.index:gtpu6_input_node.index,
712 GTPU_ERROR_DECAPSULATED,
715 /* Increment any remaining batch stats */
718 vlib_increment_combined_counter
719 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
720 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
721 node->runtime_data[0] = stats_sw_if_index;
724 return from_frame->n_vectors;
727 VLIB_NODE_FN (gtpu4_input_node) (vlib_main_t * vm,
728 vlib_node_runtime_t * node,
729 vlib_frame_t * from_frame)
731 return gtpu_input(vm, node, from_frame, /* is_ip4 */ 1);
734 VLIB_NODE_FN (gtpu6_input_node) (vlib_main_t * vm,
735 vlib_node_runtime_t * node,
736 vlib_frame_t * from_frame)
738 return gtpu_input(vm, node, from_frame, /* is_ip4 */ 0);
741 static char * gtpu_error_strings[] = {
742 #define gtpu_error(n,s) s,
743 #include <gtpu/gtpu_error.def>
748 VLIB_REGISTER_NODE (gtpu4_input_node) = {
749 .name = "gtpu4-input",
750 /* Takes a vector of packets. */
751 .vector_size = sizeof (u32),
753 .n_errors = GTPU_N_ERROR,
754 .error_strings = gtpu_error_strings,
756 .n_next_nodes = GTPU_INPUT_N_NEXT,
758 #define _(s,n) [GTPU_INPUT_NEXT_##s] = n,
759 foreach_gtpu_input_next
763 //temp .format_buffer = format_gtpu_header,
764 .format_trace = format_gtpu_rx_trace,
765 // $$$$ .unformat_buffer = unformat_gtpu_header,
768 VLIB_REGISTER_NODE (gtpu6_input_node) = {
769 .name = "gtpu6-input",
770 /* Takes a vector of packets. */
771 .vector_size = sizeof (u32),
773 .n_errors = GTPU_N_ERROR,
774 .error_strings = gtpu_error_strings,
776 .n_next_nodes = GTPU_INPUT_N_NEXT,
778 #define _(s,n) [GTPU_INPUT_NEXT_##s] = n,
779 foreach_gtpu_input_next
783 //temp .format_buffer = format_gtpu_header,
784 .format_trace = format_gtpu_rx_trace,
785 // $$$$ .unformat_buffer = unformat_gtpu_header,
789 IP_GTPU_BYPASS_NEXT_DROP,
790 IP_GTPU_BYPASS_NEXT_GTPU,
791 IP_GTPU_BYPASS_N_NEXT,
792 } ip_vxan_bypass_next_t;
795 ip_gtpu_bypass_inline (vlib_main_t * vm,
796 vlib_node_runtime_t * node,
797 vlib_frame_t * frame,
800 gtpu_main_t * gtm = >pu_main;
801 u32 * from, * to_next, n_left_from, n_left_to_next, next_index;
802 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
803 vtep4_key_t last_vtep4; /* last IPv4 address / fib index
804 matching a local VTEP address */
805 vtep6_key_t last_vtep6; /* last IPv6 address / fib index
806 matching a local VTEP address */
808 from = vlib_frame_vector_args (frame);
809 n_left_from = frame->n_vectors;
810 next_index = node->cached_next_index;
812 if (node->flags & VLIB_NODE_FLAG_TRACE)
813 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
816 vtep4_key_init (&last_vtep4);
818 vtep6_key_init (&last_vtep6);
820 while (n_left_from > 0)
822 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
824 while (n_left_from >= 4 && n_left_to_next >= 2)
826 vlib_buffer_t * b0, * b1;
827 ip4_header_t * ip40, * ip41;
828 ip6_header_t * ip60, * ip61;
829 udp_header_t * udp0, * udp1;
830 u32 bi0, ip_len0, udp_len0, flags0, next0;
831 u32 bi1, ip_len1, udp_len1, flags1, next1;
832 i32 len_diff0, len_diff1;
833 u8 error0, good_udp0, proto0;
834 u8 error1, good_udp1, proto1;
836 /* Prefetch next iteration. */
838 vlib_buffer_t * p2, * p3;
840 p2 = vlib_get_buffer (vm, from[2]);
841 p3 = vlib_get_buffer (vm, from[3]);
843 vlib_prefetch_buffer_header (p2, LOAD);
844 vlib_prefetch_buffer_header (p3, LOAD);
846 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
847 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
850 bi0 = to_next[0] = from[0];
851 bi1 = to_next[1] = from[1];
857 b0 = vlib_get_buffer (vm, bi0);
858 b1 = vlib_get_buffer (vm, bi1);
861 ip40 = vlib_buffer_get_current (b0);
862 ip41 = vlib_buffer_get_current (b1);
866 ip60 = vlib_buffer_get_current (b0);
867 ip61 = vlib_buffer_get_current (b1);
870 /* Setup packet for next IP feature */
871 vnet_feature_next(&next0, b0);
872 vnet_feature_next(&next1, b1);
876 /* Treat IP frag packets as "experimental" protocol for now
877 until support of IP frag reassembly is implemented */
878 proto0 = ip4_is_fragment(ip40) ? 0xfe : ip40->protocol;
879 proto1 = ip4_is_fragment(ip41) ? 0xfe : ip41->protocol;
883 proto0 = ip60->protocol;
884 proto1 = ip61->protocol;
887 /* Process packet 0 */
888 if (proto0 != IP_PROTOCOL_UDP)
889 goto exit0; /* not UDP packet */
892 udp0 = ip4_next_header (ip40);
894 udp0 = ip6_next_header (ip60);
896 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_GTPU))
897 goto exit0; /* not GTPU packet */
899 /* Validate DIP against VTEPs*/
902 if (!vtep4_check (>m->vtep_table, b0, ip40, &last_vtep4))
903 goto exit0; /* no local VTEP for GTPU packet */
907 if (!vtep6_check (>m->vtep_table, b0, ip60, &last_vtep6))
908 goto exit0; /* no local VTEP for GTPU packet */
912 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
914 /* Don't verify UDP checksum for packets with explicit zero checksum. */
915 good_udp0 |= udp0->checksum == 0;
917 /* Verify UDP length */
919 ip_len0 = clib_net_to_host_u16 (ip40->length);
921 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
922 udp_len0 = clib_net_to_host_u16 (udp0->length);
923 len_diff0 = ip_len0 - udp_len0;
925 /* Verify UDP checksum */
926 if (PREDICT_FALSE (!good_udp0))
928 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
931 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
933 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
935 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
941 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
942 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
946 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
947 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
951 IP_GTPU_BYPASS_NEXT_DROP : IP_GTPU_BYPASS_NEXT_GTPU;
952 b0->error = error0 ? error_node->errors[error0] : 0;
954 /* gtpu-input node expect current at GTPU header */
956 vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t));
958 vlib_buffer_advance (b0, sizeof(ip6_header_t)+sizeof(udp_header_t));
961 /* Process packet 1 */
962 if (proto1 != IP_PROTOCOL_UDP)
963 goto exit1; /* not UDP packet */
966 udp1 = ip4_next_header (ip41);
968 udp1 = ip6_next_header (ip61);
970 if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_GTPU))
971 goto exit1; /* not GTPU packet */
973 /* Validate DIP against VTEPs*/
976 if (!vtep4_check (>m->vtep_table, b1, ip41, &last_vtep4))
977 goto exit1; /* no local VTEP for GTPU packet */
981 if (!vtep6_check (>m->vtep_table, b1, ip61, &last_vtep6))
982 goto exit1; /* no local VTEP for GTPU packet */
986 good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
988 /* Don't verify UDP checksum for packets with explicit zero checksum. */
989 good_udp1 |= udp1->checksum == 0;
991 /* Verify UDP length */
993 ip_len1 = clib_net_to_host_u16 (ip41->length);
995 ip_len1 = clib_net_to_host_u16 (ip61->payload_length);
996 udp_len1 = clib_net_to_host_u16 (udp1->length);
997 len_diff1 = ip_len1 - udp_len1;
999 /* Verify UDP checksum */
1000 if (PREDICT_FALSE (!good_udp1))
1002 if ((flags1 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
1005 flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
1007 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
1009 (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1015 error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM;
1016 error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH;
1020 error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM;
1021 error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH;
1025 IP_GTPU_BYPASS_NEXT_DROP : IP_GTPU_BYPASS_NEXT_GTPU;
1026 b1->error = error1 ? error_node->errors[error1] : 0;
1028 /* gtpu-input node expect current at GTPU header */
1030 vlib_buffer_advance (b1, sizeof(ip4_header_t)+sizeof(udp_header_t));
1032 vlib_buffer_advance (b1, sizeof(ip6_header_t)+sizeof(udp_header_t));
1035 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1036 to_next, n_left_to_next,
1037 bi0, bi1, next0, next1);
1040 while (n_left_from > 0 && n_left_to_next > 0)
1043 ip4_header_t * ip40;
1044 ip6_header_t * ip60;
1045 udp_header_t * udp0;
1046 u32 bi0, ip_len0, udp_len0, flags0, next0;
1048 u8 error0, good_udp0, proto0;
1050 bi0 = to_next[0] = from[0];
1054 n_left_to_next -= 1;
1056 b0 = vlib_get_buffer (vm, bi0);
1058 ip40 = vlib_buffer_get_current (b0);
1060 ip60 = vlib_buffer_get_current (b0);
1062 /* Setup packet for next IP feature */
1063 vnet_feature_next(&next0, b0);
1066 /* Treat IP4 frag packets as "experimental" protocol for now
1067 until support of IP frag reassembly is implemented */
1068 proto0 = ip4_is_fragment(ip40) ? 0xfe : ip40->protocol;
1070 proto0 = ip60->protocol;
1072 if (proto0 != IP_PROTOCOL_UDP)
1073 goto exit; /* not UDP packet */
1076 udp0 = ip4_next_header (ip40);
1078 udp0 = ip6_next_header (ip60);
1080 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_GTPU))
1081 goto exit; /* not GTPU packet */
1083 /* Validate DIP against VTEPs*/
1086 if (!vtep4_check (>m->vtep_table, b0, ip40, &last_vtep4))
1087 goto exit; /* no local VTEP for GTPU packet */
1091 if (!vtep6_check (>m->vtep_table, b0, ip60, &last_vtep6))
1092 goto exit; /* no local VTEP for GTPU packet */
1096 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1098 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1099 good_udp0 |= udp0->checksum == 0;
1101 /* Verify UDP length */
1103 ip_len0 = clib_net_to_host_u16 (ip40->length);
1105 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
1106 udp_len0 = clib_net_to_host_u16 (udp0->length);
1107 len_diff0 = ip_len0 - udp_len0;
1109 /* Verify UDP checksum */
1110 if (PREDICT_FALSE (!good_udp0))
1112 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
1115 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
1117 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
1119 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1125 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
1126 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
1130 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
1131 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
1135 IP_GTPU_BYPASS_NEXT_DROP : IP_GTPU_BYPASS_NEXT_GTPU;
1136 b0->error = error0 ? error_node->errors[error0] : 0;
1138 /* gtpu-input node expect current at GTPU header */
1140 vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t));
1142 vlib_buffer_advance (b0, sizeof(ip6_header_t)+sizeof(udp_header_t));
1145 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1146 to_next, n_left_to_next,
1150 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1153 return frame->n_vectors;
1156 VLIB_NODE_FN (ip4_gtpu_bypass_node) (vlib_main_t * vm,
1157 vlib_node_runtime_t * node,
1158 vlib_frame_t * frame)
1160 return ip_gtpu_bypass_inline (vm, node, frame, /* is_ip4 */ 1);
1163 VLIB_REGISTER_NODE (ip4_gtpu_bypass_node) = {
1164 .name = "ip4-gtpu-bypass",
1165 .vector_size = sizeof (u32),
1167 .n_next_nodes = IP_GTPU_BYPASS_N_NEXT,
1169 [IP_GTPU_BYPASS_NEXT_DROP] = "error-drop",
1170 [IP_GTPU_BYPASS_NEXT_GTPU] = "gtpu4-input",
1173 .format_buffer = format_ip4_header,
1174 .format_trace = format_ip4_forward_next_trace,
1177 #ifndef CLIB_MARCH_VARIANT
1178 /* Dummy init function to get us linked in. */
1179 clib_error_t * ip4_gtpu_bypass_init (vlib_main_t * vm)
1182 VLIB_INIT_FUNCTION (ip4_gtpu_bypass_init);
1183 #endif /* CLIB_MARCH_VARIANT */
1185 VLIB_NODE_FN (ip6_gtpu_bypass_node) (vlib_main_t * vm,
1186 vlib_node_runtime_t * node,
1187 vlib_frame_t * frame)
1189 return ip_gtpu_bypass_inline (vm, node, frame, /* is_ip4 */ 0);
1192 VLIB_REGISTER_NODE (ip6_gtpu_bypass_node) = {
1193 .name = "ip6-gtpu-bypass",
1194 .vector_size = sizeof (u32),
1196 .n_next_nodes = IP_GTPU_BYPASS_N_NEXT,
1198 [IP_GTPU_BYPASS_NEXT_DROP] = "error-drop",
1199 [IP_GTPU_BYPASS_NEXT_GTPU] = "gtpu6-input",
1202 .format_buffer = format_ip6_header,
1203 .format_trace = format_ip6_forward_next_trace,
1206 #ifndef CLIB_MARCH_VARIANT
1207 /* Dummy init function to get us linked in. */
1208 clib_error_t * ip6_gtpu_bypass_init (vlib_main_t * vm)
1211 VLIB_INIT_FUNCTION (ip6_gtpu_bypass_init);
1213 #define foreach_gtpu_flow_error \
1214 _(NONE, "no error") \
1215 _(IP_CHECKSUM_ERROR, "Rx ip checksum errors") \
1216 _(IP_HEADER_ERROR, "Rx ip header errors") \
1217 _(UDP_CHECKSUM_ERROR, "Rx udp checksum errors") \
1218 _(UDP_LENGTH_ERROR, "Rx udp length errors")
1222 #define _(f,s) GTPU_FLOW_ERROR_##f,
1223 foreach_gtpu_flow_error
1225 #define gtpu_error(n,s) GTPU_FLOW_ERROR_##n,
1226 #include <gtpu/gtpu_error.def>
1229 } gtpu_flow_error_t;
1231 static char *gtpu_flow_error_strings[] = {
1233 foreach_gtpu_flow_error
1235 #define gtpu_error(n,s) s,
1236 #include <gtpu/gtpu_error.def>
1242 #define gtpu_local_need_csum_check(_b) \
1243 (!(_b->flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED \
1244 || _b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
1246 #define gtpu_local_csum_is_valid(_b) \
1247 ((_b->flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT \
1248 || _b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM) != 0)
1250 static_always_inline u8
1251 gtpu_validate_udp_csum (vlib_main_t * vm, vlib_buffer_t *b)
1253 u32 flags = b->flags;
1254 enum { offset = sizeof(ip4_header_t) + sizeof(udp_header_t)};
1256 /* Verify UDP checksum */
1257 if ((flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
1259 vlib_buffer_advance (b, -offset);
1260 flags = ip4_tcp_udp_validate_checksum (vm, b);
1261 vlib_buffer_advance (b, offset);
1264 return (flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1267 static_always_inline u8
1268 gtpu_check_ip (vlib_buffer_t *b, u16 payload_len)
1270 ip4_header_t * ip4_hdr = vlib_buffer_get_current(b) -
1271 sizeof(ip4_header_t) - sizeof(udp_header_t);
1272 u16 ip_len = clib_net_to_host_u16 (ip4_hdr->length);
1273 u16 expected = payload_len + sizeof(ip4_header_t) + sizeof(udp_header_t);
1274 return ip_len > expected || ip4_hdr->ttl == 0 || ip4_hdr->ip_version_and_header_length != 0x45;
1277 static_always_inline u8
1278 gtpu_check_ip_udp_len (vlib_buffer_t *b)
1280 ip4_header_t * ip4_hdr = vlib_buffer_get_current(b) -
1281 sizeof(ip4_header_t) - sizeof(udp_header_t);
1282 udp_header_t * udp_hdr = vlib_buffer_get_current(b) - sizeof(udp_header_t);
1283 u16 ip_len = clib_net_to_host_u16 (ip4_hdr->length);
1284 u16 udp_len = clib_net_to_host_u16 (udp_hdr->length);
1285 return udp_len > ip_len;
1288 static_always_inline u8
1289 gtpu_err_code (u8 ip_err0, u8 udp_err0, u8 csum_err0)
1291 u8 error0 = GTPU_FLOW_ERROR_NONE;
1293 error0 = GTPU_FLOW_ERROR_IP_HEADER_ERROR;
1295 error0 = GTPU_FLOW_ERROR_UDP_LENGTH_ERROR;
1297 error0 = GTPU_FLOW_ERROR_UDP_CHECKSUM_ERROR;
1303 gtpu_flow_input (vlib_main_t * vm,
1304 vlib_node_runtime_t * node,
1305 vlib_frame_t * from_frame)
1307 u32 n_left_from, next_index, * from, * to_next;
1308 gtpu_main_t * gtm = >pu_main;
1309 vnet_main_t * vnm = gtm->vnet_main;
1310 vnet_interface_main_t * im = &vnm->interface_main;
1311 u32 pkts_decapsulated = 0;
1312 u32 thread_index = vlib_get_thread_index();
1313 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
1314 u8 ip_err0, ip_err1, udp_err0, udp_err1, csum_err0, csum_err1;
1316 from = vlib_frame_vector_args (from_frame);
1317 n_left_from = from_frame->n_vectors;
1319 next_index = node->cached_next_index;
1320 stats_sw_if_index = node->runtime_data[0];
1321 stats_n_packets = stats_n_bytes = 0;
1323 while (n_left_from > 0)
1327 vlib_get_next_frame (vm, node, next_index,
1328 to_next, n_left_to_next);
1330 while (n_left_from >= 4 && n_left_to_next >= 2)
1333 vlib_buffer_t * b0, * b1;
1335 gtpu_header_t * gtpu0, * gtpu1;
1336 u32 gtpu_hdr_len0, gtpu_hdr_len1;
1337 u32 tunnel_index0, tunnel_index1;
1338 gtpu_tunnel_t * t0, * t1;
1340 u32 sw_if_index0, sw_if_index1, len0, len1;
1341 u8 has_space0 = 0, has_space1 = 0;
1344 /* Prefetch next iteration. */
1346 vlib_buffer_t * p2, * p3;
1348 p2 = vlib_get_buffer (vm, from[2]);
1349 p3 = vlib_get_buffer (vm, from[3]);
1351 vlib_prefetch_buffer_header (p2, LOAD);
1352 vlib_prefetch_buffer_header (p3, LOAD);
1354 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
1355 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
1364 n_left_to_next -= 2;
1367 b0 = vlib_get_buffer (vm, bi0);
1368 b1 = vlib_get_buffer (vm, bi1);
1370 /* udp leaves current_data pointing at the gtpu header */
1371 gtpu0 = vlib_buffer_get_current (b0);
1372 gtpu1 = vlib_buffer_get_current (b1);
1374 len0 = vlib_buffer_length_in_chain (vm, b0);
1375 len1 = vlib_buffer_length_in_chain (vm, b1);
1383 ip_err0 = gtpu_check_ip (b0, len0);
1384 udp_err0 = gtpu_check_ip_udp_len (b0);
1385 ip_err1 = gtpu_check_ip (b1, len1);
1386 udp_err1 = gtpu_check_ip_udp_len (b1);
1388 if (PREDICT_FALSE (gtpu_local_need_csum_check (b0)))
1389 csum_err0 = !gtpu_validate_udp_csum (vm, b0);
1391 csum_err0 = !gtpu_local_csum_is_valid (b0);
1392 if (PREDICT_FALSE (gtpu_local_need_csum_check (b1)))
1393 csum_err1 = !gtpu_validate_udp_csum (vm, b1);
1395 csum_err1 = !gtpu_local_csum_is_valid (b1);
1397 if (ip_err0 || udp_err0 || csum_err0)
1399 next0 = GTPU_INPUT_NEXT_DROP;
1400 error0 = gtpu_err_code (ip_err0, udp_err0, csum_err0);
1404 /* speculatively load gtp header version field */
1405 ver0 = gtpu0->ver_flags;
1408 * Manipulate gtpu header
1409 * TBD: Manipulate Sequence Number and N-PDU Number
1410 * TBD: Manipulate Next Extension Header
1412 gtpu_hdr_len0 = sizeof(gtpu_header_t) - (((ver0 & GTPU_E_S_PN_BIT) == 0) * 4);
1414 has_space0 = vlib_buffer_has_space (b0, gtpu_hdr_len0);
1415 if (PREDICT_FALSE (((ver0 & GTPU_VER_MASK) != GTPU_V1_VER) | (!has_space0)))
1417 error0 = has_space0 ? GTPU_ERROR_BAD_VER : GTPU_ERROR_TOO_SMALL;
1418 next0 = GTPU_INPUT_NEXT_DROP;
1422 /* Manipulate packet 0 */
1423 ASSERT (b0->flow_id != 0);
1424 tunnel_index0 = b0->flow_id - gtm->flow_id_start;
1425 t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
1428 /* Pop gtpu header */
1429 vlib_buffer_advance (b0, gtpu_hdr_len0);
1431 next0 = GTPU_INPUT_NEXT_IP4_INPUT;
1432 sw_if_index0 = t0->sw_if_index;
1434 /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
1435 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
1437 pkts_decapsulated ++;
1438 stats_n_packets += 1;
1439 stats_n_bytes += len0;
1441 /* Batch stats increment on the same gtpu tunnel so counter
1442 is not incremented per packet */
1443 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
1445 stats_n_packets -= 1;
1446 stats_n_bytes -= len0;
1447 if (stats_n_packets)
1448 vlib_increment_combined_counter
1449 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
1450 thread_index, stats_sw_if_index,
1451 stats_n_packets, stats_n_bytes);
1452 stats_n_packets = 1;
1453 stats_n_bytes = len0;
1454 stats_sw_if_index = sw_if_index0;
1458 b0->error = error0 ? node->errors[error0] : 0;
1460 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
1463 = vlib_add_trace (vm, node, b0, sizeof (*tr));
1464 tr->next_index = next0;
1466 tr->tunnel_index = tunnel_index0;
1467 tr->teid = has_space0 ? clib_net_to_host_u32(gtpu0->teid) : ~0;
1470 if (ip_err1 || udp_err1 || csum_err1)
1472 next1 = GTPU_INPUT_NEXT_DROP;
1473 error1 = gtpu_err_code (ip_err1, udp_err1, csum_err1);
1477 /* speculatively load gtp header version field */
1478 ver1 = gtpu1->ver_flags;
1481 * Manipulate gtpu header
1482 * TBD: Manipulate Sequence Number and N-PDU Number
1483 * TBD: Manipulate Next Extension Header
1485 gtpu_hdr_len1 = sizeof(gtpu_header_t) - (((ver1 & GTPU_E_S_PN_BIT) == 0) * 4);
1486 has_space1 = vlib_buffer_has_space (b1, gtpu_hdr_len1);
1487 if (PREDICT_FALSE (((ver1 & GTPU_VER_MASK) != GTPU_V1_VER) | (!has_space1)))
1489 error1 = has_space1 ? GTPU_ERROR_BAD_VER : GTPU_ERROR_TOO_SMALL;
1490 next1 = GTPU_INPUT_NEXT_DROP;
1494 /* Manipulate packet 1 */
1495 ASSERT (b1->flow_id != 0);
1496 tunnel_index1 = b1->flow_id - gtm->flow_id_start;
1497 t1 = pool_elt_at_index (gtm->tunnels, tunnel_index1);
1500 /* Pop gtpu header */
1501 vlib_buffer_advance (b1, gtpu_hdr_len1);
1503 next1 = GTPU_INPUT_NEXT_IP4_INPUT;
1504 sw_if_index1 = t1->sw_if_index;
1506 /* Required to make the l2 tag push / pop code work on l2 subifs */
1507 /* This won't happen in current implementation as only
1508 ipv4/udp/gtpu/IPV4 type packets can be matched */
1509 if (PREDICT_FALSE(next1 == GTPU_INPUT_NEXT_L2_INPUT))
1510 vnet_update_l2_len (b1);
1512 /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
1513 vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
1515 pkts_decapsulated ++;
1516 stats_n_packets += 1;
1517 stats_n_bytes += len1;
1519 /* Batch stats increment on the same gtpu tunnel so counter
1520 is not incremented per packet */
1521 if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
1523 stats_n_packets -= 1;
1524 stats_n_bytes -= len1;
1525 if (stats_n_packets)
1526 vlib_increment_combined_counter
1527 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
1528 thread_index, stats_sw_if_index,
1529 stats_n_packets, stats_n_bytes);
1530 stats_n_packets = 1;
1531 stats_n_bytes = len1;
1532 stats_sw_if_index = sw_if_index1;
1536 b1->error = error1 ? node->errors[error1] : 0;
1538 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
1541 = vlib_add_trace (vm, node, b1, sizeof (*tr));
1542 tr->next_index = next1;
1544 tr->tunnel_index = tunnel_index1;
1545 tr->teid = has_space1 ? clib_net_to_host_u32(gtpu1->teid) : ~0;
1548 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1549 to_next, n_left_to_next,
1550 bi0, bi1, next0, next1);
1553 while (n_left_from > 0 && n_left_to_next > 0)
1558 gtpu_header_t * gtpu0;
1563 u32 sw_if_index0, len0;
1572 n_left_to_next -= 1;
1574 b0 = vlib_get_buffer (vm, bi0);
1575 len0 = vlib_buffer_length_in_chain (vm, b0);
1580 ip_err0 = gtpu_check_ip (b0, len0);
1581 udp_err0 = gtpu_check_ip_udp_len (b0);
1582 if (PREDICT_FALSE (gtpu_local_need_csum_check (b0)))
1583 csum_err0 = !gtpu_validate_udp_csum (vm, b0);
1585 csum_err0 = !gtpu_local_csum_is_valid (b0);
1587 if (ip_err0 || udp_err0 || csum_err0)
1589 next0 = GTPU_INPUT_NEXT_DROP;
1590 error0 = gtpu_err_code (ip_err0, udp_err0, csum_err0);
1594 /* udp leaves current_data pointing at the gtpu header */
1595 gtpu0 = vlib_buffer_get_current (b0);
1597 /* speculatively load gtp header version field */
1598 ver0 = gtpu0->ver_flags;
1601 * Manipulate gtpu header
1602 * TBD: Manipulate Sequence Number and N-PDU Number
1603 * TBD: Manipulate Next Extension Header
1605 gtpu_hdr_len0 = sizeof(gtpu_header_t) - (((ver0 & GTPU_E_S_PN_BIT) == 0) * 4);
1607 has_space0 = vlib_buffer_has_space (b0, gtpu_hdr_len0);
1608 if (PREDICT_FALSE (((ver0 & GTPU_VER_MASK) != GTPU_V1_VER) | (!has_space0)))
1610 error0 = has_space0 ? GTPU_ERROR_BAD_VER : GTPU_ERROR_TOO_SMALL;
1611 next0 = GTPU_INPUT_NEXT_DROP;
1615 ASSERT (b0->flow_id != 0);
1616 tunnel_index0 = b0->flow_id - gtm->flow_id_start;
1617 t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
1620 /* Pop gtpu header */
1621 vlib_buffer_advance (b0, gtpu_hdr_len0);
1623 next0 = GTPU_INPUT_NEXT_IP4_INPUT;
1624 sw_if_index0 = t0->sw_if_index;
1626 /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
1627 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
1629 pkts_decapsulated ++;
1630 stats_n_packets += 1;
1631 stats_n_bytes += len0;
1633 /* Batch stats increment on the same gtpu tunnel so counter
1634 is not incremented per packet */
1635 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
1637 stats_n_packets -= 1;
1638 stats_n_bytes -= len0;
1639 if (stats_n_packets)
1640 vlib_increment_combined_counter
1641 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
1642 thread_index, stats_sw_if_index,
1643 stats_n_packets, stats_n_bytes);
1644 stats_n_packets = 1;
1645 stats_n_bytes = len0;
1646 stats_sw_if_index = sw_if_index0;
1649 b0->error = error0 ? node->errors[error0] : 0;
1651 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
1654 = vlib_add_trace (vm, node, b0, sizeof (*tr));
1655 tr->next_index = next0;
1657 tr->tunnel_index = tunnel_index0;
1658 tr->teid = has_space0 ? clib_net_to_host_u32(gtpu0->teid) : ~0;
1660 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1661 to_next, n_left_to_next,
1665 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1668 /* Do we still need this now that tunnel tx stats is kept? */
1669 vlib_node_increment_counter (vm, gtpu4_flow_input_node.index,
1670 GTPU_ERROR_DECAPSULATED,
1673 /* Increment any remaining batch stats */
1674 if (stats_n_packets)
1676 vlib_increment_combined_counter
1677 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
1678 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
1679 node->runtime_data[0] = stats_sw_if_index;
1682 return from_frame->n_vectors;
1685 VLIB_NODE_FN (gtpu4_flow_input_node) (vlib_main_t * vm,
1686 vlib_node_runtime_t * node,
1687 vlib_frame_t * from_frame)
1689 return gtpu_flow_input(vm, node, from_frame);
1694 #ifndef CLIB_MULTIARCH_VARIANT
1695 VLIB_REGISTER_NODE (gtpu4_flow_input_node) = {
1696 .name = "gtpu4-flow-input",
1697 .type = VLIB_NODE_TYPE_INTERNAL,
1698 .vector_size = sizeof (u32),
1700 .format_trace = format_gtpu_rx_trace,
1702 .n_errors = GTPU_FLOW_N_ERROR,
1703 .error_strings = gtpu_flow_error_strings,
1705 .n_next_nodes = GTPU_INPUT_N_NEXT,
1707 #define _(s,n) [GTPU_INPUT_NEXT_##s] = n,
1708 foreach_gtpu_input_next
1716 #endif /* CLIB_MARCH_VARIANT */