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 _(PAYLOAD_ERROR, "Payload type errors") \
1216 _(IP_CHECKSUM_ERROR, "Rx ip checksum errors") \
1217 _(IP_HEADER_ERROR, "Rx ip header errors") \
1218 _(UDP_CHECKSUM_ERROR, "Rx udp checksum errors") \
1219 _(UDP_LENGTH_ERROR, "Rx udp length errors")
1223 #define _(f,s) GTPU_FLOW_ERROR_##f,
1224 foreach_gtpu_flow_error
1226 #define gtpu_error(n,s) GTPU_FLOW_ERROR_##n,
1227 #include <gtpu/gtpu_error.def>
1230 } gtpu_flow_error_t;
1232 static char *gtpu_flow_error_strings[] = {
1234 foreach_gtpu_flow_error
1236 #define gtpu_error(n,s) s,
1237 #include <gtpu/gtpu_error.def>
1243 #define gtpu_local_need_csum_check(_b) \
1244 (!(_b->flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED \
1245 || _b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
1247 #define gtpu_local_csum_is_valid(_b) \
1248 ((_b->flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT \
1249 || _b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM) != 0)
1251 static_always_inline u8
1252 gtpu_validate_udp_csum (vlib_main_t * vm, vlib_buffer_t *b)
1254 u32 flags = b->flags;
1255 enum { offset = sizeof(ip4_header_t) + sizeof(udp_header_t)};
1257 /* Verify UDP checksum */
1258 if ((flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
1260 vlib_buffer_advance (b, -offset);
1261 flags = ip4_tcp_udp_validate_checksum (vm, b);
1262 vlib_buffer_advance (b, offset);
1265 return (flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1268 static_always_inline u8
1269 gtpu_check_ip (vlib_buffer_t *b, u16 payload_len)
1271 ip4_header_t * ip4_hdr = vlib_buffer_get_current(b) -
1272 sizeof(ip4_header_t) - sizeof(udp_header_t);
1273 u16 ip_len = clib_net_to_host_u16 (ip4_hdr->length);
1274 u16 expected = payload_len + sizeof(ip4_header_t) + sizeof(udp_header_t);
1275 return ip_len > expected || ip4_hdr->ttl == 0 || ip4_hdr->ip_version_and_header_length != 0x45;
1278 static_always_inline u8
1279 gtpu_check_ip_udp_len (vlib_buffer_t *b)
1281 ip4_header_t * ip4_hdr = vlib_buffer_get_current(b) -
1282 sizeof(ip4_header_t) - sizeof(udp_header_t);
1283 udp_header_t * udp_hdr = vlib_buffer_get_current(b) - sizeof(udp_header_t);
1284 u16 ip_len = clib_net_to_host_u16 (ip4_hdr->length);
1285 u16 udp_len = clib_net_to_host_u16 (udp_hdr->length);
1286 return udp_len > ip_len;
1289 static_always_inline u8
1290 gtpu_err_code (u8 ip_err0, u8 udp_err0, u8 csum_err0)
1292 u8 error0 = GTPU_FLOW_ERROR_NONE;
1294 error0 = GTPU_FLOW_ERROR_IP_HEADER_ERROR;
1296 error0 = GTPU_FLOW_ERROR_UDP_LENGTH_ERROR;
1298 error0 = GTPU_FLOW_ERROR_UDP_CHECKSUM_ERROR;
1304 gtpu_flow_input (vlib_main_t * vm,
1305 vlib_node_runtime_t * node,
1306 vlib_frame_t * from_frame)
1308 u32 n_left_from, next_index, * from, * to_next;
1309 gtpu_main_t * gtm = >pu_main;
1310 vnet_main_t * vnm = gtm->vnet_main;
1311 vnet_interface_main_t * im = &vnm->interface_main;
1312 u32 pkts_decapsulated = 0;
1313 u32 thread_index = vlib_get_thread_index();
1314 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
1315 u8 ip_err0, ip_err1, udp_err0, udp_err1, csum_err0, csum_err1;
1317 from = vlib_frame_vector_args (from_frame);
1318 n_left_from = from_frame->n_vectors;
1320 next_index = node->cached_next_index;
1321 stats_sw_if_index = node->runtime_data[0];
1322 stats_n_packets = stats_n_bytes = 0;
1324 while (n_left_from > 0)
1328 vlib_get_next_frame (vm, node, next_index,
1329 to_next, n_left_to_next);
1331 while (n_left_from >= 4 && n_left_to_next >= 2)
1334 vlib_buffer_t * b0, * b1;
1336 gtpu_header_t * gtpu0, * gtpu1;
1337 u32 gtpu_hdr_len0, gtpu_hdr_len1;
1338 u32 tunnel_index0, tunnel_index1;
1339 gtpu_tunnel_t * t0, * t1;
1341 u32 sw_if_index0, sw_if_index1, len0, len1;
1342 u8 has_space0 = 0, has_space1 = 0;
1345 /* Prefetch next iteration. */
1347 vlib_buffer_t * p2, * p3;
1349 p2 = vlib_get_buffer (vm, from[2]);
1350 p3 = vlib_get_buffer (vm, from[3]);
1352 vlib_prefetch_buffer_header (p2, LOAD);
1353 vlib_prefetch_buffer_header (p3, LOAD);
1355 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
1356 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
1365 n_left_to_next -= 2;
1368 b0 = vlib_get_buffer (vm, bi0);
1369 b1 = vlib_get_buffer (vm, bi1);
1371 /* udp leaves current_data pointing at the gtpu header */
1372 gtpu0 = vlib_buffer_get_current (b0);
1373 gtpu1 = vlib_buffer_get_current (b1);
1375 len0 = vlib_buffer_length_in_chain (vm, b0);
1376 len1 = vlib_buffer_length_in_chain (vm, b1);
1384 ip_err0 = gtpu_check_ip (b0, len0);
1385 udp_err0 = gtpu_check_ip_udp_len (b0);
1386 ip_err1 = gtpu_check_ip (b1, len1);
1387 udp_err1 = gtpu_check_ip_udp_len (b1);
1389 if (PREDICT_FALSE (gtpu_local_need_csum_check (b0)))
1390 csum_err0 = !gtpu_validate_udp_csum (vm, b0);
1392 csum_err0 = !gtpu_local_csum_is_valid (b0);
1393 if (PREDICT_FALSE (gtpu_local_need_csum_check (b1)))
1394 csum_err1 = !gtpu_validate_udp_csum (vm, b1);
1396 csum_err1 = !gtpu_local_csum_is_valid (b1);
1398 if (ip_err0 || udp_err0 || csum_err0)
1400 next0 = GTPU_INPUT_NEXT_DROP;
1401 error0 = gtpu_err_code (ip_err0, udp_err0, csum_err0);
1405 /* speculatively load gtp header version field */
1406 ver0 = gtpu0->ver_flags;
1409 * Manipulate gtpu header
1410 * TBD: Manipulate Sequence Number and N-PDU Number
1411 * TBD: Manipulate Next Extension Header
1413 gtpu_hdr_len0 = sizeof(gtpu_header_t) - (((ver0 & GTPU_E_S_PN_BIT) == 0) * 4);
1415 has_space0 = vlib_buffer_has_space (b0, gtpu_hdr_len0);
1416 if (PREDICT_FALSE (((ver0 & GTPU_VER_MASK) != GTPU_V1_VER) | (!has_space0)))
1418 error0 = has_space0 ? GTPU_ERROR_BAD_VER : GTPU_ERROR_TOO_SMALL;
1419 next0 = GTPU_INPUT_NEXT_DROP;
1423 /* Manipulate packet 0 */
1424 ASSERT (b0->flow_id != 0);
1425 tunnel_index0 = b0->flow_id - gtm->flow_id_start;
1426 t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
1429 /* Pop gtpu header */
1430 vlib_buffer_advance (b0, gtpu_hdr_len0);
1432 /* assign the next node */
1433 if (PREDICT_FALSE (t0->decap_next_index != GTPU_INPUT_NEXT_IP4_INPUT) &&
1434 (t0->decap_next_index != GTPU_INPUT_NEXT_IP6_INPUT))
1436 error0 = GTPU_FLOW_ERROR_PAYLOAD_ERROR;
1437 next0 = GTPU_INPUT_NEXT_DROP;
1440 next0 = t0->decap_next_index;
1442 sw_if_index0 = t0->sw_if_index;
1444 /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
1445 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
1447 pkts_decapsulated ++;
1448 stats_n_packets += 1;
1449 stats_n_bytes += len0;
1451 /* Batch stats increment on the same gtpu tunnel so counter
1452 is not incremented per packet */
1453 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
1455 stats_n_packets -= 1;
1456 stats_n_bytes -= len0;
1457 if (stats_n_packets)
1458 vlib_increment_combined_counter
1459 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
1460 thread_index, stats_sw_if_index,
1461 stats_n_packets, stats_n_bytes);
1462 stats_n_packets = 1;
1463 stats_n_bytes = len0;
1464 stats_sw_if_index = sw_if_index0;
1468 b0->error = error0 ? node->errors[error0] : 0;
1470 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
1473 = vlib_add_trace (vm, node, b0, sizeof (*tr));
1474 tr->next_index = next0;
1476 tr->tunnel_index = tunnel_index0;
1477 tr->teid = has_space0 ? clib_net_to_host_u32(gtpu0->teid) : ~0;
1480 if (ip_err1 || udp_err1 || csum_err1)
1482 next1 = GTPU_INPUT_NEXT_DROP;
1483 error1 = gtpu_err_code (ip_err1, udp_err1, csum_err1);
1487 /* speculatively load gtp header version field */
1488 ver1 = gtpu1->ver_flags;
1491 * Manipulate gtpu header
1492 * TBD: Manipulate Sequence Number and N-PDU Number
1493 * TBD: Manipulate Next Extension Header
1495 gtpu_hdr_len1 = sizeof(gtpu_header_t) - (((ver1 & GTPU_E_S_PN_BIT) == 0) * 4);
1496 has_space1 = vlib_buffer_has_space (b1, gtpu_hdr_len1);
1497 if (PREDICT_FALSE (((ver1 & GTPU_VER_MASK) != GTPU_V1_VER) | (!has_space1)))
1499 error1 = has_space1 ? GTPU_ERROR_BAD_VER : GTPU_ERROR_TOO_SMALL;
1500 next1 = GTPU_INPUT_NEXT_DROP;
1504 /* Manipulate packet 1 */
1505 ASSERT (b1->flow_id != 0);
1506 tunnel_index1 = b1->flow_id - gtm->flow_id_start;
1507 t1 = pool_elt_at_index (gtm->tunnels, tunnel_index1);
1510 /* Pop gtpu header */
1511 vlib_buffer_advance (b1, gtpu_hdr_len1);
1513 /* assign the next node */
1514 if (PREDICT_FALSE (t1->decap_next_index != GTPU_INPUT_NEXT_IP4_INPUT) &&
1515 (t1->decap_next_index != GTPU_INPUT_NEXT_IP6_INPUT))
1517 next1 = GTPU_FLOW_ERROR_PAYLOAD_ERROR;
1518 next1 = GTPU_INPUT_NEXT_DROP;
1521 next1 = t1->decap_next_index;
1523 sw_if_index1 = t1->sw_if_index;
1525 /* Required to make the l2 tag push / pop code work on l2 subifs */
1526 /* This won't happen in current implementation as only
1527 ipv4/udp/gtpu/IPV4 type packets can be matched */
1528 if (PREDICT_FALSE(next1 == GTPU_INPUT_NEXT_L2_INPUT))
1529 vnet_update_l2_len (b1);
1531 /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
1532 vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
1534 pkts_decapsulated ++;
1535 stats_n_packets += 1;
1536 stats_n_bytes += len1;
1538 /* Batch stats increment on the same gtpu tunnel so counter
1539 is not incremented per packet */
1540 if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
1542 stats_n_packets -= 1;
1543 stats_n_bytes -= len1;
1544 if (stats_n_packets)
1545 vlib_increment_combined_counter
1546 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
1547 thread_index, stats_sw_if_index,
1548 stats_n_packets, stats_n_bytes);
1549 stats_n_packets = 1;
1550 stats_n_bytes = len1;
1551 stats_sw_if_index = sw_if_index1;
1555 b1->error = error1 ? node->errors[error1] : 0;
1557 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
1560 = vlib_add_trace (vm, node, b1, sizeof (*tr));
1561 tr->next_index = next1;
1563 tr->tunnel_index = tunnel_index1;
1564 tr->teid = has_space1 ? clib_net_to_host_u32(gtpu1->teid) : ~0;
1567 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1568 to_next, n_left_to_next,
1569 bi0, bi1, next0, next1);
1572 while (n_left_from > 0 && n_left_to_next > 0)
1577 gtpu_header_t * gtpu0;
1582 u32 sw_if_index0, len0;
1591 n_left_to_next -= 1;
1593 b0 = vlib_get_buffer (vm, bi0);
1594 len0 = vlib_buffer_length_in_chain (vm, b0);
1599 ip_err0 = gtpu_check_ip (b0, len0);
1600 udp_err0 = gtpu_check_ip_udp_len (b0);
1601 if (PREDICT_FALSE (gtpu_local_need_csum_check (b0)))
1602 csum_err0 = !gtpu_validate_udp_csum (vm, b0);
1604 csum_err0 = !gtpu_local_csum_is_valid (b0);
1606 if (ip_err0 || udp_err0 || csum_err0)
1608 next0 = GTPU_INPUT_NEXT_DROP;
1609 error0 = gtpu_err_code (ip_err0, udp_err0, csum_err0);
1613 /* udp leaves current_data pointing at the gtpu header */
1614 gtpu0 = vlib_buffer_get_current (b0);
1616 /* speculatively load gtp header version field */
1617 ver0 = gtpu0->ver_flags;
1620 * Manipulate gtpu header
1621 * TBD: Manipulate Sequence Number and N-PDU Number
1622 * TBD: Manipulate Next Extension Header
1624 gtpu_hdr_len0 = sizeof(gtpu_header_t) - (((ver0 & GTPU_E_S_PN_BIT) == 0) * 4);
1626 has_space0 = vlib_buffer_has_space (b0, gtpu_hdr_len0);
1627 if (PREDICT_FALSE (((ver0 & GTPU_VER_MASK) != GTPU_V1_VER) | (!has_space0)))
1629 error0 = has_space0 ? GTPU_ERROR_BAD_VER : GTPU_ERROR_TOO_SMALL;
1630 next0 = GTPU_INPUT_NEXT_DROP;
1634 ASSERT (b0->flow_id != 0);
1635 tunnel_index0 = b0->flow_id - gtm->flow_id_start;
1636 t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
1639 /* Pop gtpu header */
1640 vlib_buffer_advance (b0, gtpu_hdr_len0);
1642 /* assign the next node */
1643 if (PREDICT_FALSE (t0->decap_next_index != GTPU_INPUT_NEXT_IP4_INPUT) &&
1644 (t0->decap_next_index != GTPU_INPUT_NEXT_IP6_INPUT))
1646 next0 = GTPU_FLOW_ERROR_PAYLOAD_ERROR;
1647 next0 = GTPU_INPUT_NEXT_DROP;
1650 next0 = t0->decap_next_index;
1652 sw_if_index0 = t0->sw_if_index;
1654 /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
1655 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
1657 pkts_decapsulated ++;
1658 stats_n_packets += 1;
1659 stats_n_bytes += len0;
1661 /* Batch stats increment on the same gtpu tunnel so counter
1662 is not incremented per packet */
1663 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
1665 stats_n_packets -= 1;
1666 stats_n_bytes -= len0;
1667 if (stats_n_packets)
1668 vlib_increment_combined_counter
1669 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
1670 thread_index, stats_sw_if_index,
1671 stats_n_packets, stats_n_bytes);
1672 stats_n_packets = 1;
1673 stats_n_bytes = len0;
1674 stats_sw_if_index = sw_if_index0;
1677 b0->error = error0 ? node->errors[error0] : 0;
1679 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
1682 = vlib_add_trace (vm, node, b0, sizeof (*tr));
1683 tr->next_index = next0;
1685 tr->tunnel_index = tunnel_index0;
1686 tr->teid = has_space0 ? clib_net_to_host_u32(gtpu0->teid) : ~0;
1688 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1689 to_next, n_left_to_next,
1693 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1696 /* Do we still need this now that tunnel tx stats is kept? */
1697 vlib_node_increment_counter (vm, gtpu4_flow_input_node.index,
1698 GTPU_ERROR_DECAPSULATED,
1701 /* Increment any remaining batch stats */
1702 if (stats_n_packets)
1704 vlib_increment_combined_counter
1705 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
1706 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
1707 node->runtime_data[0] = stats_sw_if_index;
1710 return from_frame->n_vectors;
1713 VLIB_NODE_FN (gtpu4_flow_input_node) (vlib_main_t * vm,
1714 vlib_node_runtime_t * node,
1715 vlib_frame_t * from_frame)
1717 return gtpu_flow_input(vm, node, from_frame);
1722 #ifndef CLIB_MULTIARCH_VARIANT
1723 VLIB_REGISTER_NODE (gtpu4_flow_input_node) = {
1724 .name = "gtpu4-flow-input",
1725 .type = VLIB_NODE_TYPE_INTERNAL,
1726 .vector_size = sizeof (u32),
1728 .format_trace = format_gtpu_rx_trace,
1730 .n_errors = GTPU_FLOW_N_ERROR,
1731 .error_strings = gtpu_flow_error_strings,
1733 .n_next_nodes = GTPU_INPUT_N_NEXT,
1735 #define _(s,n) [GTPU_INPUT_NEXT_##s] = n,
1736 foreach_gtpu_input_next
1744 #endif /* CLIB_MARCH_VARIANT */