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));
302 gtpu0->length = new_l0;
303 gtpu1 = (gtpu_header_t *)(udp1+1);
304 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
305 - sizeof (*ip4_1) - sizeof(*udp1));
306 gtpu1->length = new_l1;
307 gtpu2 = (gtpu_header_t *)(udp2+1);
308 new_l2 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b2)
309 - sizeof (*ip4_2) - sizeof(*udp2));
310 gtpu2->length = new_l2;
311 gtpu3 = (gtpu_header_t *)(udp1+3);
312 new_l3 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b3)
313 - sizeof (*ip4_3) - sizeof(*udp3));
314 gtpu3->length = new_l3;
320 ip6_0 = vlib_buffer_get_current(b0);
321 ip6_1 = vlib_buffer_get_current(b1);
322 ip6_2 = vlib_buffer_get_current(b2);
323 ip6_3 = vlib_buffer_get_current(b3);
325 /* Copy the fixed header */
326 copy_dst0 = (u64 *) ip6_0;
327 copy_src0 = (u64 *) t0->rewrite;
328 copy_dst1 = (u64 *) ip6_1;
329 copy_src1 = (u64 *) t1->rewrite;
330 copy_dst2 = (u64 *) ip6_2;
331 copy_src2 = (u64 *) t2->rewrite;
332 copy_dst3 = (u64 *) ip6_3;
333 copy_src3 = (u64 *) t3->rewrite;
334 /* Copy first 56 (ip6) octets 8-bytes at a time */
335 #define _(offs) copy_dst0[offs] = copy_src0[offs];
336 foreach_fixed_header6_offset;
338 #define _(offs) copy_dst1[offs] = copy_src1[offs];
339 foreach_fixed_header6_offset;
341 #define _(offs) copy_dst2[offs] = copy_src2[offs];
342 foreach_fixed_header6_offset;
344 #define _(offs) copy_dst3[offs] = copy_src3[offs];
345 foreach_fixed_header6_offset;
347 /* Fix IP6 payload length */
349 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
351 ip6_0->payload_length = new_l0;
353 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
355 ip6_1->payload_length = new_l1;
357 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b2)
359 ip6_2->payload_length = new_l2;
361 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b3)
363 ip6_3->payload_length = new_l3;
365 /* Fix UDP length and set source port */
366 udp0 = (udp_header_t *)(ip6_0+1);
367 udp0->length = new_l0;
368 udp0->src_port = flow_hash0;
369 udp1 = (udp_header_t *)(ip6_1+1);
370 udp1->length = new_l1;
371 udp1->src_port = flow_hash1;
372 udp2 = (udp_header_t *)(ip6_2+1);
373 udp2->length = new_l2;
374 udp2->src_port = flow_hash2;
375 udp3 = (udp_header_t *)(ip6_3+1);
376 udp3->length = new_l3;
377 udp3->src_port = flow_hash3;
379 /* IPv6 UDP checksum is mandatory */
380 udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
382 if (udp0->checksum == 0)
383 udp0->checksum = 0xffff;
384 udp1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b1,
386 if (udp1->checksum == 0)
387 udp1->checksum = 0xffff;
388 udp2->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b2,
390 if (udp2->checksum == 0)
391 udp2->checksum = 0xffff;
392 udp3->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b3,
394 if (udp3->checksum == 0)
395 udp3->checksum = 0xffff;
397 /* Fix GTPU length */
398 gtpu0 = (gtpu_header_t *)(udp0+1);
399 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
400 - sizeof (*ip4_0) - sizeof(*udp0));
401 gtpu0->length = new_l0;
402 gtpu1 = (gtpu_header_t *)(udp1+1);
403 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
404 - sizeof (*ip4_1) - sizeof(*udp1));
405 gtpu1->length = new_l1;
406 gtpu2 = (gtpu_header_t *)(udp2+1);
407 new_l2 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b2)
408 - sizeof (*ip4_2) - sizeof(*udp2));
409 gtpu2->length = new_l2;
410 gtpu3 = (gtpu_header_t *)(udp3+1);
411 new_l3 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b3)
412 - sizeof (*ip4_3) - sizeof(*udp3));
413 gtpu3->length = new_l3;
416 pkts_encapsulated += 4;
417 len0 = vlib_buffer_length_in_chain (vm, b0);
418 len1 = vlib_buffer_length_in_chain (vm, b1);
419 len2 = vlib_buffer_length_in_chain (vm, b2);
420 len3 = vlib_buffer_length_in_chain (vm, b3);
421 stats_n_packets += 4;
422 stats_n_bytes += len0 + len1 + len2 + len3;
424 /* Batch stats increment on the same gtpu tunnel so counter is not
425 incremented per packet. Note stats are still incremented for deleted
426 and admin-down tunnel where packets are dropped. It is not worthwhile
427 to check for this rare case and affect normal path performance. */
428 if (PREDICT_FALSE ((sw_if_index0 != stats_sw_if_index) ||
429 (sw_if_index1 != stats_sw_if_index) ||
430 (sw_if_index2 != stats_sw_if_index) ||
431 (sw_if_index3 != stats_sw_if_index) ))
433 stats_n_packets -= 4;
434 stats_n_bytes -= len0 + len1 + len2 + len3;
435 if ( (sw_if_index0 == sw_if_index1 ) &&
436 (sw_if_index1 == sw_if_index2 ) &&
437 (sw_if_index2 == sw_if_index3 ) )
440 vlib_increment_combined_counter
441 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
442 thread_index, stats_sw_if_index,
443 stats_n_packets, stats_n_bytes);
444 stats_sw_if_index = sw_if_index0;
446 stats_n_bytes = len0 + len1 + len2 + len3;
450 vlib_increment_combined_counter
451 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
452 thread_index, sw_if_index0, 1, len0);
453 vlib_increment_combined_counter
454 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
455 thread_index, sw_if_index1, 1, len1);
456 vlib_increment_combined_counter
457 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
458 thread_index, sw_if_index2, 1, len2);
459 vlib_increment_combined_counter
460 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
461 thread_index, sw_if_index3, 1, len3);
465 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
467 gtpu_encap_trace_t *tr =
468 vlib_add_trace (vm, node, b0, sizeof (*tr));
469 tr->tunnel_index = t0 - gtm->tunnels;
473 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
475 gtpu_encap_trace_t *tr =
476 vlib_add_trace (vm, node, b1, sizeof (*tr));
477 tr->tunnel_index = t1 - gtm->tunnels;
481 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
482 to_next, n_left_to_next,
484 next0, next1, next2, next3);
487 while (n_left_from > 0 && n_left_to_next > 0)
493 ip4_header_t * ip4_0;
494 ip6_header_t * ip6_0;
496 gtpu_header_t * gtpu0;
497 u64 * copy_src0, * copy_dst0;
498 u32 * copy_src_last0, * copy_dst_last0;
509 b0 = vlib_get_buffer (vm, bi0);
511 flow_hash0 = vnet_l2_compute_flow_hash(b0);
513 /* Get next node index and adj index from tunnel next_dpo */
514 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
515 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
516 t0 = >m->tunnels[hi0->dev_instance];
517 /* Note: change to always set next0 if it may be set to drop */
518 next0 = t0->next_dpo.dpoi_next_node;
519 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index;
521 /* Apply the rewrite string. $$$$ vnet_rewrite? */
522 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
526 ip4_0 = vlib_buffer_get_current(b0);
528 /* Copy the fixed header */
529 copy_dst0 = (u64 *) ip4_0;
530 copy_src0 = (u64 *) t0->rewrite;
531 /* Copy first 32 octets 8-bytes at a time */
532 #define _(offs) copy_dst0[offs] = copy_src0[offs];
533 foreach_fixed_header4_offset;
535 /* Last 4 octets. Hopefully gcc will be our friend */
536 copy_dst_last0 = (u32 *)(©_dst0[4]);
537 copy_src_last0 = (u32 *)(©_src0[4]);
538 copy_dst_last0[0] = copy_src_last0[0];
540 /* Fix the IP4 checksum and length */
541 sum0 = ip4_0->checksum;
542 new_l0 = /* old_l0 always 0, see the rewrite setup */
543 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
544 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
545 length /* changed member */);
546 ip4_0->checksum = ip_csum_fold (sum0);
547 ip4_0->length = new_l0;
549 /* Fix UDP length and set source port */
550 udp0 = (udp_header_t *)(ip4_0+1);
551 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
553 udp0->length = new_l0;
554 udp0->src_port = flow_hash0;
556 /* Fix GTPU length */
557 gtpu0 = (gtpu_header_t *)(udp0+1);
558 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
559 - sizeof (*ip4_0) - sizeof(*udp0));
560 gtpu0->length = new_l0;
567 ip6_0 = vlib_buffer_get_current(b0);
568 /* Copy the fixed header */
569 copy_dst0 = (u64 *) ip6_0;
570 copy_src0 = (u64 *) t0->rewrite;
571 /* Copy first 56 (ip6) octets 8-bytes at a time */
572 #define _(offs) copy_dst0[offs] = copy_src0[offs];
573 foreach_fixed_header6_offset;
575 /* Fix IP6 payload length */
577 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
579 ip6_0->payload_length = new_l0;
581 /* Fix UDP length and set source port */
582 udp0 = (udp_header_t *)(ip6_0+1);
583 udp0->length = new_l0;
584 udp0->src_port = flow_hash0;
586 /* IPv6 UDP checksum is mandatory */
587 udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
589 if (udp0->checksum == 0)
590 udp0->checksum = 0xffff;
592 /* Fix GTPU length */
593 gtpu0 = (gtpu_header_t *)(udp0+1);
594 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
595 - sizeof (*ip4_0) - sizeof(*udp0));
596 gtpu0->length = new_l0;
599 pkts_encapsulated ++;
600 len0 = vlib_buffer_length_in_chain (vm, b0);
601 stats_n_packets += 1;
602 stats_n_bytes += len0;
604 /* Batch stats increment on the same gtpu tunnel so counter is not
605 incremented per packet. Note stats are still incremented for deleted
606 and admin-down tunnel where packets are dropped. It is not worthwhile
607 to check for this rare case and affect normal path performance. */
608 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
610 stats_n_packets -= 1;
611 stats_n_bytes -= len0;
613 vlib_increment_combined_counter
614 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
615 thread_index, stats_sw_if_index,
616 stats_n_packets, stats_n_bytes);
618 stats_n_bytes = len0;
619 stats_sw_if_index = sw_if_index0;
622 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
624 gtpu_encap_trace_t *tr =
625 vlib_add_trace (vm, node, b0, sizeof (*tr));
626 tr->tunnel_index = t0 - gtm->tunnels;
629 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
630 to_next, n_left_to_next,
634 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
637 /* Do we still need this now that tunnel tx stats is kept? */
638 vlib_node_increment_counter (vm, node->node_index,
639 GTPU_ENCAP_ERROR_ENCAPSULATED,
642 /* Increment any remaining batch stats */
645 vlib_increment_combined_counter
646 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
647 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
648 node->runtime_data[0] = stats_sw_if_index;
651 return from_frame->n_vectors;
655 gtpu4_encap (vlib_main_t * vm,
656 vlib_node_runtime_t * node,
657 vlib_frame_t * from_frame)
659 return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 1);
663 gtpu6_encap (vlib_main_t * vm,
664 vlib_node_runtime_t * node,
665 vlib_frame_t * from_frame)
667 return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 0);
670 VLIB_REGISTER_NODE (gtpu4_encap_node) = {
671 .function = gtpu4_encap,
672 .name = "gtpu4-encap",
673 .vector_size = sizeof (u32),
674 .format_trace = format_gtpu_encap_trace,
675 .type = VLIB_NODE_TYPE_INTERNAL,
676 .n_errors = ARRAY_LEN(gtpu_encap_error_strings),
677 .error_strings = gtpu_encap_error_strings,
678 .n_next_nodes = GTPU_ENCAP_N_NEXT,
680 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
681 foreach_gtpu_encap_next
686 VLIB_NODE_FUNCTION_MULTIARCH (gtpu4_encap_node, gtpu4_encap)
688 VLIB_REGISTER_NODE (gtpu6_encap_node) = {
689 .function = gtpu6_encap,
690 .name = "gtpu6-encap",
691 .vector_size = sizeof (u32),
692 .format_trace = format_gtpu_encap_trace,
693 .type = VLIB_NODE_TYPE_INTERNAL,
694 .n_errors = ARRAY_LEN(gtpu_encap_error_strings),
695 .error_strings = gtpu_encap_error_strings,
696 .n_next_nodes = GTPU_ENCAP_N_NEXT,
698 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
699 foreach_gtpu_encap_next
704 VLIB_NODE_FUNCTION_MULTIARCH (gtpu6_encap_node, gtpu6_encap)