2 * Copyright (c) 2017 Intel and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vppinfra/error.h>
16 #include <vppinfra/hash.h>
17 #include <vnet/vnet.h>
18 #include <vnet/ip/ip.h>
19 #include <vnet/ethernet/ethernet.h>
20 #include <gtpu/gtpu.h>
22 /* Statistics (not all errors) */
23 #define foreach_gtpu_encap_error \
24 _(ENCAPSULATED, "good packets encapsulated")
26 static char * gtpu_encap_error_strings[] = {
27 #define _(sym,string) string,
28 foreach_gtpu_encap_error
33 #define _(sym,str) GTPU_ENCAP_ERROR_##sym,
34 foreach_gtpu_encap_error
39 #define foreach_gtpu_encap_next \
40 _(DROP, "error-drop") \
41 _(IP4_LOOKUP, "ip4-lookup") \
42 _(IP6_LOOKUP, "ip6-lookup")
46 GTPU_ENCAP_NEXT_IP4_LOOKUP,
47 GTPU_ENCAP_NEXT_IP6_LOOKUP,
56 u8 * format_gtpu_encap_trace (u8 * s, va_list * args)
58 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
59 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
60 gtpu_encap_trace_t * t
61 = va_arg (*args, gtpu_encap_trace_t *);
63 s = format (s, "GTPU encap to gtpu_tunnel%d teid %d",
64 t->tunnel_index, t->teid);
69 #define foreach_fixed_header4_offset \
72 #define foreach_fixed_header6_offset \
73 _(0) _(1) _(2) _(3) _(4) _(5) _(6)
76 gtpu_encap_inline (vlib_main_t * vm,
77 vlib_node_runtime_t * node,
78 vlib_frame_t * from_frame,
81 u32 n_left_from, next_index, * from, * to_next;
82 gtpu_main_t * gtm = >pu_main;
83 vnet_main_t * vnm = gtm->vnet_main;
84 vnet_interface_main_t * im = &vnm->interface_main;
85 u32 pkts_encapsulated = 0;
86 u16 old_l0 = 0, old_l1 = 0, old_l2 = 0, old_l3 = 0;
87 u32 thread_index = vlib_get_thread_index();
88 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
89 u32 sw_if_index0 = 0, sw_if_index1 = 0, sw_if_index2 = 0, sw_if_index3 = 0;
90 u32 next0 = 0, next1 = 0, next2 = 0, next3 = 0;
91 vnet_hw_interface_t * hi0, * hi1, * hi2, * hi3;
92 gtpu_tunnel_t * t0 = NULL, * t1 = NULL, * t2 = NULL, * t3 = NULL;
94 from = vlib_frame_vector_args (from_frame);
95 n_left_from = from_frame->n_vectors;
97 next_index = node->cached_next_index;
98 stats_sw_if_index = node->runtime_data[0];
99 stats_n_packets = stats_n_bytes = 0;
101 while (n_left_from > 0)
105 vlib_get_next_frame (vm, node, next_index,
106 to_next, n_left_to_next);
108 while (n_left_from >= 8 && n_left_to_next >= 4)
110 u32 bi0, bi1, bi2, bi3;
111 vlib_buffer_t * b0, * b1, * b2, * b3;
112 u32 flow_hash0, flow_hash1, flow_hash2, flow_hash3;
113 u32 len0, len1, len2, len3;
114 ip4_header_t * ip4_0, * ip4_1, * ip4_2, * ip4_3;
115 ip6_header_t * ip6_0, * ip6_1, * ip6_2, * ip6_3;
116 udp_header_t * udp0, * udp1, * udp2, * udp3;
117 gtpu_header_t * gtpu0, * gtpu1, * gtpu2, * gtpu3;
118 u64 * copy_src0, * copy_dst0;
119 u64 * copy_src1, * copy_dst1;
120 u64 * copy_src2, * copy_dst2;
121 u64 * copy_src3, * copy_dst3;
122 u32 * copy_src_last0, * copy_dst_last0;
123 u32 * copy_src_last1, * copy_dst_last1;
124 u32 * copy_src_last2, * copy_dst_last2;
125 u32 * copy_src_last3, * copy_dst_last3;
126 u16 new_l0, new_l1, new_l2, new_l3;
127 ip_csum_t sum0, sum1, sum2, sum3;
129 /* Prefetch next iteration. */
131 vlib_buffer_t * p4, * p5, * p6, * p7;
133 p4 = vlib_get_buffer (vm, from[4]);
134 p5 = vlib_get_buffer (vm, from[5]);
135 p6 = vlib_get_buffer (vm, from[6]);
136 p7 = vlib_get_buffer (vm, from[7]);
138 vlib_prefetch_buffer_header (p4, LOAD);
139 vlib_prefetch_buffer_header (p5, LOAD);
140 vlib_prefetch_buffer_header (p6, LOAD);
141 vlib_prefetch_buffer_header (p7, LOAD);
143 CLIB_PREFETCH (p4->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
144 CLIB_PREFETCH (p5->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
145 CLIB_PREFETCH (p6->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
146 CLIB_PREFETCH (p7->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
162 b0 = vlib_get_buffer (vm, bi0);
163 b1 = vlib_get_buffer (vm, bi1);
164 b2 = vlib_get_buffer (vm, bi2);
165 b3 = vlib_get_buffer (vm, bi3);
167 flow_hash0 = vnet_l2_compute_flow_hash (b0);
168 flow_hash1 = vnet_l2_compute_flow_hash (b1);
169 flow_hash2 = vnet_l2_compute_flow_hash (b2);
170 flow_hash3 = vnet_l2_compute_flow_hash (b3);
172 /* Get next node index and adj index from tunnel next_dpo */
173 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
174 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_TX];
175 sw_if_index2 = vnet_buffer(b2)->sw_if_index[VLIB_TX];
176 sw_if_index3 = vnet_buffer(b3)->sw_if_index[VLIB_TX];
177 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
178 hi1 = vnet_get_sup_hw_interface (vnm, sw_if_index1);
179 hi2 = vnet_get_sup_hw_interface (vnm, sw_if_index2);
180 hi3 = vnet_get_sup_hw_interface (vnm, sw_if_index3);
181 t0 = >m->tunnels[hi0->dev_instance];
182 t1 = >m->tunnels[hi1->dev_instance];
183 t2 = >m->tunnels[hi2->dev_instance];
184 t3 = >m->tunnels[hi3->dev_instance];
186 /* Note: change to always set next0 if it may be set to drop */
187 next0 = t0->next_dpo.dpoi_next_node;
188 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index;
189 next1 = t1->next_dpo.dpoi_next_node;
190 vnet_buffer(b1)->ip.adj_index[VLIB_TX] = t1->next_dpo.dpoi_index;
191 next2 = t2->next_dpo.dpoi_next_node;
192 vnet_buffer(b2)->ip.adj_index[VLIB_TX] = t2->next_dpo.dpoi_index;
193 next3 = t3->next_dpo.dpoi_next_node;
194 vnet_buffer(b3)->ip.adj_index[VLIB_TX] = t3->next_dpo.dpoi_index;
196 /* Apply the rewrite string. $$$$ vnet_rewrite? */
197 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
198 vlib_buffer_advance (b1, -(word)_vec_len(t1->rewrite));
199 vlib_buffer_advance (b2, -(word)_vec_len(t2->rewrite));
200 vlib_buffer_advance (b3, -(word)_vec_len(t3->rewrite));
204 ip4_0 = vlib_buffer_get_current(b0);
205 ip4_1 = vlib_buffer_get_current(b1);
206 ip4_2 = vlib_buffer_get_current(b2);
207 ip4_3 = vlib_buffer_get_current(b3);
209 /* Copy the fixed header */
210 copy_dst0 = (u64 *) ip4_0;
211 copy_src0 = (u64 *) t0->rewrite;
212 copy_dst1 = (u64 *) ip4_1;
213 copy_src1 = (u64 *) t1->rewrite;
214 copy_dst2 = (u64 *) ip4_2;
215 copy_src2 = (u64 *) t2->rewrite;
216 copy_dst3 = (u64 *) ip4_3;
217 copy_src3 = (u64 *) t3->rewrite;
219 /* Copy first 32 octets 8-bytes at a time */
220 #define _(offs) copy_dst0[offs] = copy_src0[offs];
221 foreach_fixed_header4_offset;
223 #define _(offs) copy_dst1[offs] = copy_src1[offs];
224 foreach_fixed_header4_offset;
226 #define _(offs) copy_dst2[offs] = copy_src2[offs];
227 foreach_fixed_header4_offset;
229 #define _(offs) copy_dst3[offs] = copy_src3[offs];
230 foreach_fixed_header4_offset;
232 /* Last 4 octets. Hopefully gcc will be our friend */
233 copy_dst_last0 = (u32 *)(©_dst0[4]);
234 copy_src_last0 = (u32 *)(©_src0[4]);
235 copy_dst_last0[0] = copy_src_last0[0];
236 copy_dst_last1 = (u32 *)(©_dst1[4]);
237 copy_src_last1 = (u32 *)(©_src1[4]);
238 copy_dst_last1[0] = copy_src_last1[0];
239 copy_dst_last2 = (u32 *)(©_dst2[4]);
240 copy_src_last2 = (u32 *)(©_src2[4]);
241 copy_dst_last2[0] = copy_src_last2[0];
242 copy_dst_last3 = (u32 *)(©_dst3[4]);
243 copy_src_last3 = (u32 *)(©_src3[4]);
244 copy_dst_last3[0] = copy_src_last3[0];
246 /* Fix the IP4 checksum and length */
247 sum0 = ip4_0->checksum;
248 new_l0 = /* old_l0 always 0, see the rewrite setup */
249 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
250 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
251 length /* changed member */);
252 ip4_0->checksum = ip_csum_fold (sum0);
253 ip4_0->length = new_l0;
254 sum1 = ip4_1->checksum;
255 new_l1 = /* old_l1 always 0, see the rewrite setup */
256 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
257 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
258 length /* changed member */);
259 ip4_1->checksum = ip_csum_fold (sum1);
260 ip4_1->length = new_l1;
261 sum2 = ip4_2->checksum;
262 new_l2 = /* old_l0 always 0, see the rewrite setup */
263 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b2));
264 sum2 = ip_csum_update (sum2, old_l2, new_l2, ip4_header_t,
265 length /* changed member */);
266 ip4_2->checksum = ip_csum_fold (sum2);
267 ip4_2->length = new_l2;
268 sum3 = ip4_3->checksum;
269 new_l3 = /* old_l1 always 0, see the rewrite setup */
270 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b3));
271 sum3 = ip_csum_update (sum3, old_l3, new_l3, ip4_header_t,
272 length /* changed member */);
273 ip4_3->checksum = ip_csum_fold (sum3);
274 ip4_3->length = new_l3;
276 /* Fix UDP length and set source port */
277 udp0 = (udp_header_t *)(ip4_0+1);
278 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
280 udp0->length = new_l0;
281 udp0->src_port = flow_hash0;
282 udp1 = (udp_header_t *)(ip4_1+1);
283 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
285 udp1->length = new_l1;
286 udp1->src_port = flow_hash1;
287 udp2 = (udp_header_t *)(ip4_2+1);
288 new_l2 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b2)
290 udp2->length = new_l2;
291 udp2->src_port = flow_hash2;
292 udp3 = (udp_header_t *)(ip4_3+1);
293 new_l3 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b3)
295 udp3->length = new_l3;
296 udp3->src_port = flow_hash3;
298 /* Fix GTPU length */
299 gtpu0 = (gtpu_header_t *)(udp0+1);
300 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
301 - sizeof (*ip4_0) - sizeof(*udp0)
303 gtpu0->length = new_l0;
304 gtpu1 = (gtpu_header_t *)(udp1+1);
305 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
306 - sizeof (*ip4_1) - sizeof(*udp1)
308 gtpu1->length = new_l1;
309 gtpu2 = (gtpu_header_t *)(udp2+1);
310 new_l2 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b2)
311 - sizeof (*ip4_2) - sizeof(*udp2)
313 gtpu2->length = new_l2;
314 gtpu3 = (gtpu_header_t *)(udp3+1);
315 new_l3 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b3)
316 - sizeof (*ip4_3) - sizeof(*udp3)
318 gtpu3->length = new_l3;
324 ip6_0 = vlib_buffer_get_current(b0);
325 ip6_1 = vlib_buffer_get_current(b1);
326 ip6_2 = vlib_buffer_get_current(b2);
327 ip6_3 = vlib_buffer_get_current(b3);
329 /* Copy the fixed header */
330 copy_dst0 = (u64 *) ip6_0;
331 copy_src0 = (u64 *) t0->rewrite;
332 copy_dst1 = (u64 *) ip6_1;
333 copy_src1 = (u64 *) t1->rewrite;
334 copy_dst2 = (u64 *) ip6_2;
335 copy_src2 = (u64 *) t2->rewrite;
336 copy_dst3 = (u64 *) ip6_3;
337 copy_src3 = (u64 *) t3->rewrite;
338 /* Copy first 56 (ip6) octets 8-bytes at a time */
339 #define _(offs) copy_dst0[offs] = copy_src0[offs];
340 foreach_fixed_header6_offset;
342 #define _(offs) copy_dst1[offs] = copy_src1[offs];
343 foreach_fixed_header6_offset;
345 #define _(offs) copy_dst2[offs] = copy_src2[offs];
346 foreach_fixed_header6_offset;
348 #define _(offs) copy_dst3[offs] = copy_src3[offs];
349 foreach_fixed_header6_offset;
351 /* Fix IP6 payload length */
353 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
355 ip6_0->payload_length = new_l0;
357 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
359 ip6_1->payload_length = new_l1;
361 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b2)
363 ip6_2->payload_length = new_l2;
365 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b3)
367 ip6_3->payload_length = new_l3;
369 /* Fix UDP length and set source port */
370 udp0 = (udp_header_t *)(ip6_0+1);
371 udp0->length = new_l0;
372 udp0->src_port = flow_hash0;
373 udp1 = (udp_header_t *)(ip6_1+1);
374 udp1->length = new_l1;
375 udp1->src_port = flow_hash1;
376 udp2 = (udp_header_t *)(ip6_2+1);
377 udp2->length = new_l2;
378 udp2->src_port = flow_hash2;
379 udp3 = (udp_header_t *)(ip6_3+1);
380 udp3->length = new_l3;
381 udp3->src_port = flow_hash3;
383 /* Fix GTPU length */
384 gtpu0 = (gtpu_header_t *)(udp0+1);
385 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
386 - sizeof (*ip6_0) - sizeof(*udp0)
388 gtpu0->length = new_l0;
389 gtpu1 = (gtpu_header_t *)(udp1+1);
390 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
391 - sizeof (*ip6_1) - sizeof(*udp1)
393 gtpu1->length = new_l1;
394 gtpu2 = (gtpu_header_t *)(udp2+1);
395 new_l2 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b2)
396 - sizeof (*ip6_2) - sizeof(*udp2)
398 gtpu2->length = new_l2;
399 gtpu3 = (gtpu_header_t *)(udp3+1);
400 new_l3 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b3)
401 - sizeof (*ip6_3) - sizeof(*udp3)
403 gtpu3->length = new_l3;
405 /* IPv6 UDP checksum is mandatory */
406 udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
408 if (udp0->checksum == 0)
409 udp0->checksum = 0xffff;
410 udp1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b1,
412 if (udp1->checksum == 0)
413 udp1->checksum = 0xffff;
414 udp2->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b2,
416 if (udp2->checksum == 0)
417 udp2->checksum = 0xffff;
418 udp3->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b3,
420 if (udp3->checksum == 0)
421 udp3->checksum = 0xffff;
425 pkts_encapsulated += 4;
426 len0 = vlib_buffer_length_in_chain (vm, b0);
427 len1 = vlib_buffer_length_in_chain (vm, b1);
428 len2 = vlib_buffer_length_in_chain (vm, b2);
429 len3 = vlib_buffer_length_in_chain (vm, b3);
430 stats_n_packets += 4;
431 stats_n_bytes += len0 + len1 + len2 + len3;
433 /* Batch stats increment on the same gtpu tunnel so counter is not
434 incremented per packet. Note stats are still incremented for deleted
435 and admin-down tunnel where packets are dropped. It is not worthwhile
436 to check for this rare case and affect normal path performance. */
437 if (PREDICT_FALSE ((sw_if_index0 != stats_sw_if_index) ||
438 (sw_if_index1 != stats_sw_if_index) ||
439 (sw_if_index2 != stats_sw_if_index) ||
440 (sw_if_index3 != stats_sw_if_index) ))
442 stats_n_packets -= 4;
443 stats_n_bytes -= len0 + len1 + len2 + len3;
444 if ( (sw_if_index0 == sw_if_index1 ) &&
445 (sw_if_index1 == sw_if_index2 ) &&
446 (sw_if_index2 == sw_if_index3 ) )
449 vlib_increment_combined_counter
450 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
451 thread_index, stats_sw_if_index,
452 stats_n_packets, stats_n_bytes);
453 stats_sw_if_index = sw_if_index0;
455 stats_n_bytes = len0 + len1 + len2 + len3;
459 vlib_increment_combined_counter
460 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
461 thread_index, sw_if_index0, 1, len0);
462 vlib_increment_combined_counter
463 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
464 thread_index, sw_if_index1, 1, len1);
465 vlib_increment_combined_counter
466 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
467 thread_index, sw_if_index2, 1, len2);
468 vlib_increment_combined_counter
469 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
470 thread_index, sw_if_index3, 1, len3);
474 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
476 gtpu_encap_trace_t *tr =
477 vlib_add_trace (vm, node, b0, sizeof (*tr));
478 tr->tunnel_index = t0 - gtm->tunnels;
482 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
484 gtpu_encap_trace_t *tr =
485 vlib_add_trace (vm, node, b1, sizeof (*tr));
486 tr->tunnel_index = t1 - gtm->tunnels;
490 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
491 to_next, n_left_to_next,
493 next0, next1, next2, next3);
496 while (n_left_from > 0 && n_left_to_next > 0)
502 ip4_header_t * ip4_0;
503 ip6_header_t * ip6_0;
505 gtpu_header_t * gtpu0;
506 u64 * copy_src0, * copy_dst0;
507 u32 * copy_src_last0, * copy_dst_last0;
518 b0 = vlib_get_buffer (vm, bi0);
520 flow_hash0 = vnet_l2_compute_flow_hash(b0);
522 /* Get next node index and adj index from tunnel next_dpo */
523 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
524 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
525 t0 = >m->tunnels[hi0->dev_instance];
526 /* Note: change to always set next0 if it may be set to drop */
527 next0 = t0->next_dpo.dpoi_next_node;
528 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index;
530 /* Apply the rewrite string. $$$$ vnet_rewrite? */
531 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
535 ip4_0 = vlib_buffer_get_current(b0);
537 /* Copy the fixed header */
538 copy_dst0 = (u64 *) ip4_0;
539 copy_src0 = (u64 *) t0->rewrite;
540 /* Copy first 32 octets 8-bytes at a time */
541 #define _(offs) copy_dst0[offs] = copy_src0[offs];
542 foreach_fixed_header4_offset;
544 /* Last 4 octets. Hopefully gcc will be our friend */
545 copy_dst_last0 = (u32 *)(©_dst0[4]);
546 copy_src_last0 = (u32 *)(©_src0[4]);
547 copy_dst_last0[0] = copy_src_last0[0];
549 /* Fix the IP4 checksum and length */
550 sum0 = ip4_0->checksum;
551 new_l0 = /* old_l0 always 0, see the rewrite setup */
552 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
553 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
554 length /* changed member */);
555 ip4_0->checksum = ip_csum_fold (sum0);
556 ip4_0->length = new_l0;
558 /* Fix UDP length and set source port */
559 udp0 = (udp_header_t *)(ip4_0+1);
560 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
562 udp0->length = new_l0;
563 udp0->src_port = flow_hash0;
565 /* Fix GTPU length */
566 gtpu0 = (gtpu_header_t *)(udp0+1);
567 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
568 - sizeof (*ip4_0) - sizeof(*udp0)
570 gtpu0->length = new_l0;
577 ip6_0 = vlib_buffer_get_current(b0);
578 /* Copy the fixed header */
579 copy_dst0 = (u64 *) ip6_0;
580 copy_src0 = (u64 *) t0->rewrite;
581 /* Copy first 56 (ip6) octets 8-bytes at a time */
582 #define _(offs) copy_dst0[offs] = copy_src0[offs];
583 foreach_fixed_header6_offset;
585 /* Fix IP6 payload length */
587 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
589 ip6_0->payload_length = new_l0;
591 /* Fix UDP length and set source port */
592 udp0 = (udp_header_t *)(ip6_0+1);
593 udp0->length = new_l0;
594 udp0->src_port = flow_hash0;
596 /* Fix GTPU length */
597 gtpu0 = (gtpu_header_t *)(udp0+1);
598 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
599 - sizeof (*ip4_0) - sizeof(*udp0)
601 gtpu0->length = new_l0;
603 /* IPv6 UDP checksum is mandatory */
604 udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
606 if (udp0->checksum == 0)
607 udp0->checksum = 0xffff;
610 pkts_encapsulated ++;
611 len0 = vlib_buffer_length_in_chain (vm, b0);
612 stats_n_packets += 1;
613 stats_n_bytes += len0;
615 /* Batch stats increment on the same gtpu tunnel so counter is not
616 incremented per packet. Note stats are still incremented for deleted
617 and admin-down tunnel where packets are dropped. It is not worthwhile
618 to check for this rare case and affect normal path performance. */
619 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
621 stats_n_packets -= 1;
622 stats_n_bytes -= len0;
624 vlib_increment_combined_counter
625 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
626 thread_index, stats_sw_if_index,
627 stats_n_packets, stats_n_bytes);
629 stats_n_bytes = len0;
630 stats_sw_if_index = sw_if_index0;
633 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
635 gtpu_encap_trace_t *tr =
636 vlib_add_trace (vm, node, b0, sizeof (*tr));
637 tr->tunnel_index = t0 - gtm->tunnels;
640 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
641 to_next, n_left_to_next,
645 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
648 /* Do we still need this now that tunnel tx stats is kept? */
649 vlib_node_increment_counter (vm, node->node_index,
650 GTPU_ENCAP_ERROR_ENCAPSULATED,
653 /* Increment any remaining batch stats */
656 vlib_increment_combined_counter
657 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
658 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
659 node->runtime_data[0] = stats_sw_if_index;
662 return from_frame->n_vectors;
666 gtpu4_encap (vlib_main_t * vm,
667 vlib_node_runtime_t * node,
668 vlib_frame_t * from_frame)
670 return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 1);
674 gtpu6_encap (vlib_main_t * vm,
675 vlib_node_runtime_t * node,
676 vlib_frame_t * from_frame)
678 return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 0);
681 VLIB_REGISTER_NODE (gtpu4_encap_node) = {
682 .function = gtpu4_encap,
683 .name = "gtpu4-encap",
684 .vector_size = sizeof (u32),
685 .format_trace = format_gtpu_encap_trace,
686 .type = VLIB_NODE_TYPE_INTERNAL,
687 .n_errors = ARRAY_LEN(gtpu_encap_error_strings),
688 .error_strings = gtpu_encap_error_strings,
689 .n_next_nodes = GTPU_ENCAP_N_NEXT,
691 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
692 foreach_gtpu_encap_next
697 VLIB_NODE_FUNCTION_MULTIARCH (gtpu4_encap_node, gtpu4_encap)
699 VLIB_REGISTER_NODE (gtpu6_encap_node) = {
700 .function = gtpu6_encap,
701 .name = "gtpu6-encap",
702 .vector_size = sizeof (u32),
703 .format_trace = format_gtpu_encap_trace,
704 .type = VLIB_NODE_TYPE_INTERNAL,
705 .n_errors = ARRAY_LEN(gtpu_encap_error_strings),
706 .error_strings = gtpu_encap_error_strings,
707 .n_next_nodes = GTPU_ENCAP_N_NEXT,
709 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
710 foreach_gtpu_encap_next
715 VLIB_NODE_FUNCTION_MULTIARCH (gtpu6_encap_node, gtpu6_encap)