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,
52 #define foreach_fixed_header4_offset \
55 #define foreach_fixed_header6_offset \
56 _(0) _(1) _(2) _(3) _(4) _(5) _(6)
59 gtpu_encap_inline (vlib_main_t * vm,
60 vlib_node_runtime_t * node,
61 vlib_frame_t * from_frame,
64 u32 n_left_from, next_index, * from, * to_next;
65 gtpu_main_t * gtm = >pu_main;
66 vnet_main_t * vnm = gtm->vnet_main;
67 vnet_interface_main_t * im = &vnm->interface_main;
68 u32 pkts_encapsulated = 0;
69 u16 old_l0 = 0, old_l1 = 0, old_l2 = 0, old_l3 = 0;
70 u32 thread_index = vlib_get_thread_index();
71 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
72 u32 sw_if_index0 = 0, sw_if_index1 = 0, sw_if_index2 = 0, sw_if_index3 = 0;
73 u32 next0 = 0, next1 = 0, next2 = 0, next3 = 0;
74 vnet_hw_interface_t * hi0, * hi1, * hi2, * hi3;
75 gtpu_tunnel_t * t0 = NULL, * t1 = NULL, * t2 = NULL, * t3 = NULL;
77 from = vlib_frame_vector_args (from_frame);
78 n_left_from = from_frame->n_vectors;
80 next_index = node->cached_next_index;
81 stats_sw_if_index = node->runtime_data[0];
82 stats_n_packets = stats_n_bytes = 0;
84 while (n_left_from > 0)
88 vlib_get_next_frame (vm, node, next_index,
89 to_next, n_left_to_next);
91 while (n_left_from >= 8 && n_left_to_next >= 4)
93 u32 bi0, bi1, bi2, bi3;
94 vlib_buffer_t * b0, * b1, * b2, * b3;
95 u32 flow_hash0, flow_hash1, flow_hash2, flow_hash3;
96 u32 len0, len1, len2, len3;
97 ip4_header_t * ip4_0, * ip4_1, * ip4_2, * ip4_3;
98 ip6_header_t * ip6_0, * ip6_1, * ip6_2, * ip6_3;
99 udp_header_t * udp0, * udp1, * udp2, * udp3;
100 gtpu_header_t * gtpu0, * gtpu1, * gtpu2, * gtpu3;
101 u64 * copy_src0, * copy_dst0;
102 u64 * copy_src1, * copy_dst1;
103 u64 * copy_src2, * copy_dst2;
104 u64 * copy_src3, * copy_dst3;
105 u32 * copy_src_last0, * copy_dst_last0;
106 u32 * copy_src_last1, * copy_dst_last1;
107 u32 * copy_src_last2, * copy_dst_last2;
108 u32 * copy_src_last3, * copy_dst_last3;
109 u16 new_l0, new_l1, new_l2, new_l3;
110 ip_csum_t sum0, sum1, sum2, sum3;
112 /* Prefetch next iteration. */
114 vlib_buffer_t * p4, * p5, * p6, * p7;
116 p4 = vlib_get_buffer (vm, from[4]);
117 p5 = vlib_get_buffer (vm, from[5]);
118 p6 = vlib_get_buffer (vm, from[6]);
119 p7 = vlib_get_buffer (vm, from[7]);
121 vlib_prefetch_buffer_header (p4, LOAD);
122 vlib_prefetch_buffer_header (p5, LOAD);
123 vlib_prefetch_buffer_header (p6, LOAD);
124 vlib_prefetch_buffer_header (p7, LOAD);
126 CLIB_PREFETCH (p4->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
127 CLIB_PREFETCH (p5->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
128 CLIB_PREFETCH (p6->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
129 CLIB_PREFETCH (p7->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
145 b0 = vlib_get_buffer (vm, bi0);
146 b1 = vlib_get_buffer (vm, bi1);
147 b2 = vlib_get_buffer (vm, bi2);
148 b3 = vlib_get_buffer (vm, bi3);
150 flow_hash0 = vnet_l2_compute_flow_hash (b0);
151 flow_hash1 = vnet_l2_compute_flow_hash (b1);
152 flow_hash2 = vnet_l2_compute_flow_hash (b2);
153 flow_hash3 = vnet_l2_compute_flow_hash (b3);
155 /* Get next node index and adj index from tunnel next_dpo */
156 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
157 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_TX];
158 sw_if_index2 = vnet_buffer(b2)->sw_if_index[VLIB_TX];
159 sw_if_index3 = vnet_buffer(b3)->sw_if_index[VLIB_TX];
160 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
161 hi1 = vnet_get_sup_hw_interface (vnm, sw_if_index1);
162 hi2 = vnet_get_sup_hw_interface (vnm, sw_if_index2);
163 hi3 = vnet_get_sup_hw_interface (vnm, sw_if_index3);
164 t0 = >m->tunnels[hi0->dev_instance];
165 t1 = >m->tunnels[hi1->dev_instance];
166 t2 = >m->tunnels[hi2->dev_instance];
167 t3 = >m->tunnels[hi3->dev_instance];
169 /* Note: change to always set next0 if it may be set to drop */
170 next0 = t0->next_dpo.dpoi_next_node;
171 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index;
172 next1 = t1->next_dpo.dpoi_next_node;
173 vnet_buffer(b1)->ip.adj_index[VLIB_TX] = t1->next_dpo.dpoi_index;
174 next2 = t2->next_dpo.dpoi_next_node;
175 vnet_buffer(b2)->ip.adj_index[VLIB_TX] = t2->next_dpo.dpoi_index;
176 next3 = t3->next_dpo.dpoi_next_node;
177 vnet_buffer(b3)->ip.adj_index[VLIB_TX] = t3->next_dpo.dpoi_index;
179 /* Apply the rewrite string. $$$$ vnet_rewrite? */
180 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
181 vlib_buffer_advance (b1, -(word)_vec_len(t1->rewrite));
182 vlib_buffer_advance (b2, -(word)_vec_len(t2->rewrite));
183 vlib_buffer_advance (b3, -(word)_vec_len(t3->rewrite));
187 ip4_0 = vlib_buffer_get_current(b0);
188 ip4_1 = vlib_buffer_get_current(b1);
189 ip4_2 = vlib_buffer_get_current(b2);
190 ip4_3 = vlib_buffer_get_current(b3);
192 /* Copy the fixed header */
193 copy_dst0 = (u64 *) ip4_0;
194 copy_src0 = (u64 *) t0->rewrite;
195 copy_dst1 = (u64 *) ip4_1;
196 copy_src1 = (u64 *) t1->rewrite;
197 copy_dst2 = (u64 *) ip4_2;
198 copy_src2 = (u64 *) t2->rewrite;
199 copy_dst3 = (u64 *) ip4_3;
200 copy_src3 = (u64 *) t3->rewrite;
202 /* Copy first 32 octets 8-bytes at a time */
203 #define _(offs) copy_dst0[offs] = copy_src0[offs];
204 foreach_fixed_header4_offset;
206 #define _(offs) copy_dst1[offs] = copy_src1[offs];
207 foreach_fixed_header4_offset;
209 #define _(offs) copy_dst2[offs] = copy_src2[offs];
210 foreach_fixed_header4_offset;
212 #define _(offs) copy_dst3[offs] = copy_src3[offs];
213 foreach_fixed_header4_offset;
215 /* Last 4 octets. Hopefully gcc will be our friend */
216 copy_dst_last0 = (u32 *)(©_dst0[4]);
217 copy_src_last0 = (u32 *)(©_src0[4]);
218 copy_dst_last0[0] = copy_src_last0[0];
219 copy_dst_last1 = (u32 *)(©_dst1[4]);
220 copy_src_last1 = (u32 *)(©_src1[4]);
221 copy_dst_last1[0] = copy_src_last1[0];
222 copy_dst_last2 = (u32 *)(©_dst2[4]);
223 copy_src_last2 = (u32 *)(©_src2[4]);
224 copy_dst_last2[0] = copy_src_last2[0];
225 copy_dst_last3 = (u32 *)(©_dst3[4]);
226 copy_src_last3 = (u32 *)(©_src3[4]);
227 copy_dst_last3[0] = copy_src_last3[0];
229 /* Fix the IP4 checksum and length */
230 sum0 = ip4_0->checksum;
231 new_l0 = /* old_l0 always 0, see the rewrite setup */
232 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
233 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
234 length /* changed member */);
235 ip4_0->checksum = ip_csum_fold (sum0);
236 ip4_0->length = new_l0;
237 sum1 = ip4_1->checksum;
238 new_l1 = /* old_l1 always 0, see the rewrite setup */
239 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
240 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
241 length /* changed member */);
242 ip4_1->checksum = ip_csum_fold (sum1);
243 ip4_1->length = new_l1;
244 sum2 = ip4_2->checksum;
245 new_l2 = /* old_l0 always 0, see the rewrite setup */
246 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b2));
247 sum2 = ip_csum_update (sum2, old_l2, new_l2, ip4_header_t,
248 length /* changed member */);
249 ip4_2->checksum = ip_csum_fold (sum2);
250 ip4_2->length = new_l2;
251 sum3 = ip4_3->checksum;
252 new_l3 = /* old_l1 always 0, see the rewrite setup */
253 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b3));
254 sum3 = ip_csum_update (sum3, old_l3, new_l3, ip4_header_t,
255 length /* changed member */);
256 ip4_3->checksum = ip_csum_fold (sum3);
257 ip4_3->length = new_l3;
259 /* Fix UDP length and set source port */
260 udp0 = (udp_header_t *)(ip4_0+1);
261 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
263 udp0->length = new_l0;
264 udp0->src_port = flow_hash0;
265 udp1 = (udp_header_t *)(ip4_1+1);
266 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
268 udp1->length = new_l1;
269 udp1->src_port = flow_hash1;
270 udp2 = (udp_header_t *)(ip4_2+1);
271 new_l2 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b2)
273 udp2->length = new_l2;
274 udp2->src_port = flow_hash2;
275 udp3 = (udp_header_t *)(ip4_3+1);
276 new_l3 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b3)
278 udp3->length = new_l3;
279 udp3->src_port = flow_hash3;
281 /* Fix GTPU length */
282 gtpu0 = (gtpu_header_t *)(udp0+1);
283 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
284 - sizeof (*ip4_0) - sizeof(*udp0)
286 gtpu0->length = new_l0;
287 gtpu1 = (gtpu_header_t *)(udp1+1);
288 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
289 - sizeof (*ip4_1) - sizeof(*udp1)
291 gtpu1->length = new_l1;
292 gtpu2 = (gtpu_header_t *)(udp2+1);
293 new_l2 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b2)
294 - sizeof (*ip4_2) - sizeof(*udp2)
296 gtpu2->length = new_l2;
297 gtpu3 = (gtpu_header_t *)(udp3+1);
298 new_l3 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b3)
299 - sizeof (*ip4_3) - sizeof(*udp3)
301 gtpu3->length = new_l3;
307 ip6_0 = vlib_buffer_get_current(b0);
308 ip6_1 = vlib_buffer_get_current(b1);
309 ip6_2 = vlib_buffer_get_current(b2);
310 ip6_3 = vlib_buffer_get_current(b3);
312 /* Copy the fixed header */
313 copy_dst0 = (u64 *) ip6_0;
314 copy_src0 = (u64 *) t0->rewrite;
315 copy_dst1 = (u64 *) ip6_1;
316 copy_src1 = (u64 *) t1->rewrite;
317 copy_dst2 = (u64 *) ip6_2;
318 copy_src2 = (u64 *) t2->rewrite;
319 copy_dst3 = (u64 *) ip6_3;
320 copy_src3 = (u64 *) t3->rewrite;
321 /* Copy first 56 (ip6) octets 8-bytes at a time */
322 #define _(offs) copy_dst0[offs] = copy_src0[offs];
323 foreach_fixed_header6_offset;
325 #define _(offs) copy_dst1[offs] = copy_src1[offs];
326 foreach_fixed_header6_offset;
328 #define _(offs) copy_dst2[offs] = copy_src2[offs];
329 foreach_fixed_header6_offset;
331 #define _(offs) copy_dst3[offs] = copy_src3[offs];
332 foreach_fixed_header6_offset;
334 /* Fix IP6 payload length */
336 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
338 ip6_0->payload_length = new_l0;
340 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
342 ip6_1->payload_length = new_l1;
344 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b2)
346 ip6_2->payload_length = new_l2;
348 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b3)
350 ip6_3->payload_length = new_l3;
352 /* Fix UDP length and set source port */
353 udp0 = (udp_header_t *)(ip6_0+1);
354 udp0->length = new_l0;
355 udp0->src_port = flow_hash0;
356 udp1 = (udp_header_t *)(ip6_1+1);
357 udp1->length = new_l1;
358 udp1->src_port = flow_hash1;
359 udp2 = (udp_header_t *)(ip6_2+1);
360 udp2->length = new_l2;
361 udp2->src_port = flow_hash2;
362 udp3 = (udp_header_t *)(ip6_3+1);
363 udp3->length = new_l3;
364 udp3->src_port = flow_hash3;
366 /* Fix GTPU length */
367 gtpu0 = (gtpu_header_t *)(udp0+1);
368 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
369 - sizeof (*ip6_0) - sizeof(*udp0)
371 gtpu0->length = new_l0;
372 gtpu1 = (gtpu_header_t *)(udp1+1);
373 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
374 - sizeof (*ip6_1) - sizeof(*udp1)
376 gtpu1->length = new_l1;
377 gtpu2 = (gtpu_header_t *)(udp2+1);
378 new_l2 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b2)
379 - sizeof (*ip6_2) - sizeof(*udp2)
381 gtpu2->length = new_l2;
382 gtpu3 = (gtpu_header_t *)(udp3+1);
383 new_l3 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b3)
384 - sizeof (*ip6_3) - sizeof(*udp3)
386 gtpu3->length = new_l3;
388 /* IPv6 UDP checksum is mandatory */
389 udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
391 if (udp0->checksum == 0)
392 udp0->checksum = 0xffff;
393 udp1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b1,
395 if (udp1->checksum == 0)
396 udp1->checksum = 0xffff;
397 udp2->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b2,
399 if (udp2->checksum == 0)
400 udp2->checksum = 0xffff;
401 udp3->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b3,
403 if (udp3->checksum == 0)
404 udp3->checksum = 0xffff;
408 pkts_encapsulated += 4;
409 len0 = vlib_buffer_length_in_chain (vm, b0);
410 len1 = vlib_buffer_length_in_chain (vm, b1);
411 len2 = vlib_buffer_length_in_chain (vm, b2);
412 len3 = vlib_buffer_length_in_chain (vm, b3);
413 stats_n_packets += 4;
414 stats_n_bytes += len0 + len1 + len2 + len3;
416 /* Batch stats increment on the same gtpu tunnel so counter is not
417 incremented per packet. Note stats are still incremented for deleted
418 and admin-down tunnel where packets are dropped. It is not worthwhile
419 to check for this rare case and affect normal path performance. */
420 if (PREDICT_FALSE ((sw_if_index0 != stats_sw_if_index) ||
421 (sw_if_index1 != stats_sw_if_index) ||
422 (sw_if_index2 != stats_sw_if_index) ||
423 (sw_if_index3 != stats_sw_if_index) ))
425 stats_n_packets -= 4;
426 stats_n_bytes -= len0 + len1 + len2 + len3;
427 if ( (sw_if_index0 == sw_if_index1 ) &&
428 (sw_if_index1 == sw_if_index2 ) &&
429 (sw_if_index2 == sw_if_index3 ) )
432 vlib_increment_combined_counter
433 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
434 thread_index, stats_sw_if_index,
435 stats_n_packets, stats_n_bytes);
436 stats_sw_if_index = sw_if_index0;
438 stats_n_bytes = len0 + len1 + len2 + len3;
442 vlib_increment_combined_counter
443 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
444 thread_index, sw_if_index0, 1, len0);
445 vlib_increment_combined_counter
446 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
447 thread_index, sw_if_index1, 1, len1);
448 vlib_increment_combined_counter
449 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
450 thread_index, sw_if_index2, 1, len2);
451 vlib_increment_combined_counter
452 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
453 thread_index, sw_if_index3, 1, len3);
457 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
459 gtpu_encap_trace_t *tr =
460 vlib_add_trace (vm, node, b0, sizeof (*tr));
461 tr->tunnel_index = t0 - gtm->tunnels;
465 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
467 gtpu_encap_trace_t *tr =
468 vlib_add_trace (vm, node, b1, sizeof (*tr));
469 tr->tunnel_index = t1 - gtm->tunnels;
473 if (PREDICT_FALSE(b2->flags & VLIB_BUFFER_IS_TRACED))
475 gtpu_encap_trace_t *tr =
476 vlib_add_trace (vm, node, b2, sizeof (*tr));
477 tr->tunnel_index = t2 - gtm->tunnels;
481 if (PREDICT_FALSE(b3->flags & VLIB_BUFFER_IS_TRACED))
483 gtpu_encap_trace_t *tr =
484 vlib_add_trace (vm, node, b3, sizeof (*tr));
485 tr->tunnel_index = t3 - gtm->tunnels;
489 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
490 to_next, n_left_to_next,
492 next0, next1, next2, next3);
495 while (n_left_from > 0 && n_left_to_next > 0)
501 ip4_header_t * ip4_0;
502 ip6_header_t * ip6_0;
504 gtpu_header_t * gtpu0;
505 u64 * copy_src0, * copy_dst0;
506 u32 * copy_src_last0, * copy_dst_last0;
517 b0 = vlib_get_buffer (vm, bi0);
519 flow_hash0 = vnet_l2_compute_flow_hash(b0);
521 /* Get next node index and adj index from tunnel next_dpo */
522 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
523 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
524 t0 = >m->tunnels[hi0->dev_instance];
525 /* Note: change to always set next0 if it may be set to drop */
526 next0 = t0->next_dpo.dpoi_next_node;
527 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index;
529 /* Apply the rewrite string. $$$$ vnet_rewrite? */
530 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
534 ip4_0 = vlib_buffer_get_current(b0);
536 /* Copy the fixed header */
537 copy_dst0 = (u64 *) ip4_0;
538 copy_src0 = (u64 *) t0->rewrite;
539 /* Copy first 32 octets 8-bytes at a time */
540 #define _(offs) copy_dst0[offs] = copy_src0[offs];
541 foreach_fixed_header4_offset;
543 /* Last 4 octets. Hopefully gcc will be our friend */
544 copy_dst_last0 = (u32 *)(©_dst0[4]);
545 copy_src_last0 = (u32 *)(©_src0[4]);
546 copy_dst_last0[0] = copy_src_last0[0];
548 /* Fix the IP4 checksum and length */
549 sum0 = ip4_0->checksum;
550 new_l0 = /* old_l0 always 0, see the rewrite setup */
551 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
552 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
553 length /* changed member */);
554 ip4_0->checksum = ip_csum_fold (sum0);
555 ip4_0->length = new_l0;
557 /* Fix UDP length and set source port */
558 udp0 = (udp_header_t *)(ip4_0+1);
559 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
561 udp0->length = new_l0;
562 udp0->src_port = flow_hash0;
564 /* Fix GTPU length */
565 gtpu0 = (gtpu_header_t *)(udp0+1);
566 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
567 - sizeof (*ip4_0) - sizeof(*udp0)
569 gtpu0->length = new_l0;
576 ip6_0 = vlib_buffer_get_current(b0);
577 /* Copy the fixed header */
578 copy_dst0 = (u64 *) ip6_0;
579 copy_src0 = (u64 *) t0->rewrite;
580 /* Copy first 56 (ip6) octets 8-bytes at a time */
581 #define _(offs) copy_dst0[offs] = copy_src0[offs];
582 foreach_fixed_header6_offset;
584 /* Fix IP6 payload length */
586 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
588 ip6_0->payload_length = new_l0;
590 /* Fix UDP length and set source port */
591 udp0 = (udp_header_t *)(ip6_0+1);
592 udp0->length = new_l0;
593 udp0->src_port = flow_hash0;
595 /* Fix GTPU length */
596 gtpu0 = (gtpu_header_t *)(udp0+1);
597 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
598 - sizeof (*ip4_0) - sizeof(*udp0)
600 gtpu0->length = new_l0;
602 /* IPv6 UDP checksum is mandatory */
603 udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
605 if (udp0->checksum == 0)
606 udp0->checksum = 0xffff;
609 pkts_encapsulated ++;
610 len0 = vlib_buffer_length_in_chain (vm, b0);
611 stats_n_packets += 1;
612 stats_n_bytes += len0;
614 /* Batch stats increment on the same gtpu tunnel so counter is not
615 incremented per packet. Note stats are still incremented for deleted
616 and admin-down tunnel where packets are dropped. It is not worthwhile
617 to check for this rare case and affect normal path performance. */
618 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
620 stats_n_packets -= 1;
621 stats_n_bytes -= len0;
623 vlib_increment_combined_counter
624 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
625 thread_index, stats_sw_if_index,
626 stats_n_packets, stats_n_bytes);
628 stats_n_bytes = len0;
629 stats_sw_if_index = sw_if_index0;
632 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
634 gtpu_encap_trace_t *tr =
635 vlib_add_trace (vm, node, b0, sizeof (*tr));
636 tr->tunnel_index = t0 - gtm->tunnels;
639 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
640 to_next, n_left_to_next,
644 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
647 /* Do we still need this now that tunnel tx stats is kept? */
648 vlib_node_increment_counter (vm, node->node_index,
649 GTPU_ENCAP_ERROR_ENCAPSULATED,
652 /* Increment any remaining batch stats */
655 vlib_increment_combined_counter
656 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
657 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
658 node->runtime_data[0] = stats_sw_if_index;
661 return from_frame->n_vectors;
664 VLIB_NODE_FN (gtpu4_encap_node) (vlib_main_t * vm,
665 vlib_node_runtime_t * node,
666 vlib_frame_t * from_frame)
668 return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 1);
671 VLIB_NODE_FN (gtpu6_encap_node) (vlib_main_t * vm,
672 vlib_node_runtime_t * node,
673 vlib_frame_t * from_frame)
675 return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 0);
678 VLIB_REGISTER_NODE (gtpu4_encap_node) = {
679 .name = "gtpu4-encap",
680 .vector_size = sizeof (u32),
681 .format_trace = format_gtpu_encap_trace,
682 .type = VLIB_NODE_TYPE_INTERNAL,
683 .n_errors = ARRAY_LEN(gtpu_encap_error_strings),
684 .error_strings = gtpu_encap_error_strings,
685 .n_next_nodes = GTPU_ENCAP_N_NEXT,
687 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
688 foreach_gtpu_encap_next
693 VLIB_REGISTER_NODE (gtpu6_encap_node) = {
694 .name = "gtpu6-encap",
695 .vector_size = sizeof (u32),
696 .format_trace = format_gtpu_encap_trace,
697 .type = VLIB_NODE_TYPE_INTERNAL,
698 .n_errors = ARRAY_LEN(gtpu_encap_error_strings),
699 .error_strings = gtpu_encap_error_strings,
700 .n_next_nodes = GTPU_ENCAP_N_NEXT,
702 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
703 foreach_gtpu_encap_next